/* 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. */ #include "oneflow/core/framework/to_string.h" #include "oneflow/core/job/scope.h" #include "oneflow/core/job/scope.cfg.h" #include "oneflow/core/job/scope.pb.h" #include "oneflow/core/operator/operator.h" #include "oneflow/core/vm/symbol_storage.h" #include "oneflow/core/framework/interpreter.h" #include "oneflow/core/framework/instructions_builder.h" #include "oneflow/core/framework/symbol_id_cache.h" namespace oneflow { Scope::Scope(const ScopeProto& scope_proto) : auto_increment_id_(0), symbol_id_(NullOpt), scope_proto_(scope_proto) { CHECK_OK(Init()) << scope_proto_.DebugString(); } Scope::Scope(int64_t symbol_id, const ScopeProto& scope_proto) : auto_increment_id_(0), symbol_id_(symbol_id), scope_proto_(scope_proto) {} Maybe Scope::New(int64_t symbol_id, const ScopeProto& scope_proto) { auto* ptr = new Scope(symbol_id, scope_proto); std::shared_ptr scope(ptr); JUST(scope->Init()); return scope; } Maybe Scope::Init() { { const auto& storage = *Global>::Get(); job_desc_ = JUST(storage.MaybeGetPtr(scope_proto_.job_desc_symbol_id())); } { const auto& storage = *Global>::Get(); const auto& device_parallel_desc = SymbolOf(*JUST(storage.MaybeGetPtr(scope_proto_.device_parallel_desc_symbol_id()))); const auto& host_parallel_desc = SymbolOf(*JUST(storage.MaybeGetPtr(scope_proto_.host_parallel_desc_symbol_id()))); placement_scope_ = SymbolOf(PlacementScope(device_parallel_desc, host_parallel_desc)); } { const auto& storage = *Global>::Get(); if (scope_proto_.has_parent_scope_symbol_id()) { parent_scope_symbol_ = JUST(storage.MaybeGetPtr(scope_proto_.parent_scope_symbol_id())); } } return Maybe::Ok(); } Maybe Scope::job_desc() const { CHECK_NOTNULL_OR_RETURN(job_desc_.get()); return job_desc_.get(); } Maybe Scope::GetParallelDescSymbolId(const OperatorConf& op_conf) const { if (op_conf.device_tag() == "cpu" || IsCpuOnly(op_conf)) { return scope_proto_.host_parallel_desc_symbol_id(); } else { return scope_proto_.device_parallel_desc_symbol_id(); } } Maybe> Scope::GetParallelDesc(const OperatorConf& op_conf) const { return placement_scope_->GetParallelDesc(op_conf.device_tag(), op_conf); } const AttrValue& Scope::GetAttrValue(const std::string& attr_name) const { const auto& iter = scope_proto_.attr_name2attr_value().find(attr_name); if (iter != scope_proto_.attr_name2attr_value().end()) { return iter->second; } const auto& attr_name2attr_def = GlobalScopeConfigDef().attr_name2attr_def(); const auto& def_iter = attr_name2attr_def.find(attr_name); CHECK(def_iter != attr_name2attr_def.end()); return def_iter->second.default_val(); } Maybe Scope::MakeChildScopeProto() const { auto child = std::make_shared(scope_proto_); child->set_parent_scope_symbol_id(JUST(symbol_id())); return child; } Maybe NewScopeSymbolId( int64_t old_scope_symbol_id, const std::function new_scope)>& InitNewScopeProto) { CHECK_OR_RETURN(Global>::Get()->Has(old_scope_symbol_id)); const Scope& old_scope = Global>::Get()->Get(old_scope_symbol_id); std::shared_ptr new_scope = JUST(old_scope.MakeChildScopeProto()); InitNewScopeProto(new_scope); int64_t symbol_id = 0; JUST(LogicalInterpreter().Run([&](InstructionsBuilder* builder) -> Maybe { symbol_id = JUST(builder->FindOrCreateSymbolId(*new_scope)); return Maybe::Ok(); })); auto* id_cache = Global>::Get(); if (!id_cache->Has(*new_scope)) { JUST( id_cache->FindOrCreate(*new_scope, [&symbol_id]() -> Maybe { return symbol_id; })); ScopeProto scope_proto; new_scope->ToProto(&scope_proto); JUST(Global>::Get()->TryAdd(symbol_id, scope_proto)); } return symbol_id; } } // namespace oneflow