/* Copyright 2020 The OneFlow Authors. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifndef ONEFLOW_API_PYTHON_SESSION_SESSION_H_ #define ONEFLOW_API_PYTHON_SESSION_SESSION_H_ #include #include #include "oneflow/core/common/protobuf.h" #include "oneflow/core/control/ctrl_client.h" #include "oneflow/core/control/global_process_ctx.h" #include "oneflow/core/job/global_for.h" #include "oneflow/core/job/env_global_objects_scope.h" #include "oneflow/core/job/session_global_objects_scope.h" #include "oneflow/core/job/cluster_instruction.h" #include "oneflow/core/job/oneflow.h" #include "oneflow/core/job/job_build_and_infer_ctx_mgr.h" #include "oneflow/core/job/resource_desc.h" #include "oneflow/core/framework/config_def.h" #include "oneflow/core/framework/multi_client_session_context.h" #include "oneflow/core/framework/nn_graph.h" #include "oneflow/core/persistence/tee_persistent_log_stream.h" namespace oneflow { inline Maybe IsSessionInited() { return Singleton::Get() != nullptr; } inline void FixCpuDeviceNum(ConfigProto* config_proto) { if (config_proto->resource().cpu_device_num() > 0) { return; } config_proto->mutable_resource()->set_cpu_device_num(std::thread::hardware_concurrency()); } inline Maybe InitEagerGlobalSession(const std::string& config_proto_str) { CHECK_NOTNULL_OR_RETURN(Singleton::Get()) << "env not found"; ConfigProto config_proto; CHECK_OR_RETURN(TxtString2PbMessage(config_proto_str, &config_proto)) << "failed to parse config_proto: " << config_proto_str; FixCpuDeviceNum(&config_proto); Singleton::Get()->PushKV("config_proto", config_proto); CHECK_ISNULL_OR_RETURN(Singleton::Get()); Singleton::SetAllocated(new SessionGlobalObjectsScope()); JUST(Singleton::Get()->EagerInit(config_proto)); VLOG(3) << "NewGlobal " << typeid(SessionGlobalObjectsScope).name(); return Maybe::Ok(); } inline Maybe InitLazyGlobalSession(const std::string& config_proto_str) { CHECK_NOTNULL_OR_RETURN(Singleton::Get()) << "env not found"; CHECK_OR_RETURN(GlobalProcessCtx::IsThisProcessMaster()); ClusterInstruction::MasterSendSessionStart(); ConfigProto config_proto; CHECK_OR_RETURN(TxtString2PbMessage(config_proto_str, &config_proto)) << "failed to parse config_proto: " << config_proto_str; FixCpuDeviceNum(&config_proto); Singleton::Get()->PushKV("config_proto", config_proto); CHECK_ISNULL_OR_RETURN(Singleton::Get()); Singleton::SetAllocated(new SessionGlobalObjectsScope()); JUST(Singleton::Get()->Init(config_proto)); VLOG(3) << "NewGlobal " << typeid(SessionGlobalObjectsScope).name(); return Maybe::Ok(); } inline Maybe DestroyLazyGlobalSession() { if (Singleton::Get() == nullptr) { return Maybe::Ok(); } CHECK_OR_RETURN(GlobalProcessCtx::IsThisProcessMaster()); Singleton::Delete(); return Maybe::Ok(); } inline Maybe StartLazyGlobalSession() { CHECK_NOTNULL_OR_RETURN(Singleton::Get()) << "session not found"; CHECK_OR_RETURN(GlobalProcessCtx::IsThisProcessMaster()); const JobSet& job_set = Singleton::Get()->job_set(); if (Singleton::Get()->enable_debug_mode()) { TeePersistentLogStream::Create("job_set.prototxt")->Write(job_set); } if (job_set.job().empty()) { return Error::JobSetEmptyError() << "no function defined"; } CHECK_ISNULL_OR_RETURN(Singleton::Get()); Singleton::Get()->PushKV("session_job_set", job_set); Singleton::New(job_set.inter_job_reuse_mem_strategy()); Singleton::New(); JUST(Singleton::Get()->Init(job_set)); return Maybe::Ok(); } inline Maybe StopLazyGlobalSession() { if (Singleton::Get() == nullptr) { return Maybe::Ok(); } CHECK_OR_RETURN(GlobalProcessCtx::IsThisProcessMaster()); CHECK_NOTNULL_OR_RETURN(Singleton::Get()); Singleton::Delete(); Singleton::Delete(); return Maybe::Ok(); } } // namespace oneflow #endif // ONEFLOW_API_PYTHON_SESSION_SESSION_H_