LCOV - code coverage report
Current view: top level - j_BsjSWu5Y/api/session/session - inner_session.cc Coverage Total Hit
Test: CHG Lines: 100.0 % 11 11
Test Date: 2026-08-28 17:36:07
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "session/inner_session.h"
      12              : #include <cinttypes>
      13              : 
      14              : #include <map>
      15              : #include <memory>
      16              : #include <vector>
      17              : 
      18              : #include "analyzer/analyzer.h"
      19              : #include "adx_datadump_server.h"
      20              : #include "common/checker.h"
      21              : #include "acl/acl_rt.h"
      22              : #include "common/dump/dump_properties.h"
      23              : #include "common/dump/dump_manager.h"
      24              : #include "framework/common/util.h"
      25              : #include "framework/common/debug/ge_log.h"
      26              : #include "framework/common/helper/model_helper.h"
      27              : #include "graph/ge_context.h"
      28              : #include "graph/ge_global_options.h"
      29              : #include "graph/ge_local_context.h"
      30              : #include "common/context/local_context.h"
      31              : #include "graph/manager/graph_var_manager.h"
      32              : #include "graph/utils/tensor_adapter.h"
      33              : #include "runtime/om2/om2_rt_var_manager.h"
      34              : #include "runtime/om2/om2_external_weight_manager.h"
      35              : #include "graph/utils/graph_utils_ex.h"
      36              : #include "api/aclgrph/option_utils.h"
      37              : #include "common/profiling/profiling_manager.h"
      38              : #include "common/profiling/profiling_init.h"
      39              : #include "common/model/external_allocator_manager.h"
      40              : #include "graph/manager/active_memory_allocator.h"
      41              : #include "graph/load/graph_loader.h"
      42              : #include "common/platform_info_util/platform_info_util.h"
      43              : #include <api/gelib/gelib.h>
      44              : #include "common/memory/tensor_trans_utils.h"
      45              : #include "register/core_num_utils.h"
      46              : #include "common/helper/om2/om2_utils.h"
      47              : 
      48              : namespace ge {
      49              : void CopyGeOutputsMemToUserOutputs(const std::vector<GeTensor> &ge_outputs, std::vector<Tensor> &outputs) {
      50              :   if (outputs.size() != 0U) {
      51              :     return;
      52              :   }
      53              : 
      54              :   outputs.reserve(ge_outputs.size());
      55              :   for (size_t i = 0UL; i < ge_outputs.size(); i++) {
      56              :     outputs.emplace_back(TensorAdapter::AsTensor(ge_outputs[i]));
      57              :     GELOGI("Return outputs memory malloc by allocator success, mem:%p, size:%u", outputs[i].GetData(),
      58              :            outputs[i].GetSize());
      59              :   }
      60              : }
      61              : namespace {
      62              : constexpr int32_t kDumpStatus = 0;
      63              : constexpr int32_t kDecimalSystem = 10;
      64              : 
      65              : Status CheckReuseMemoryOption(const std::map<std::string, std::string> &options) {
      66              :   auto iter = options.find(OPTION_EXEC_DISABLE_REUSED_MEMORY);
      67              :   if (iter != options.end()) {
      68              :     if (iter->second == "0") {
      69              :       GELOGD("%s=0, reuse memory is open", OPTION_EXEC_DISABLE_REUSED_MEMORY);
      70              :     } else if (iter->second == "1") {
      71              :       GELOGD("%s=1, reuse memory is close", OPTION_EXEC_DISABLE_REUSED_MEMORY);
      72              :     } else {
      73              :       GELOGE(PARAM_INVALID, "[CheckReuse][MemoryOption]option %s=%s is invalid", OPTION_EXEC_DISABLE_REUSED_MEMORY,
      74              :              iter->second.c_str());
      75              :       const auto readable_name = ge::GetContext().GetReadableName(OPTION_EXEC_DISABLE_REUSED_MEMORY);
      76              :       std::string reason = readable_name + " only support 0 or 1";
      77              :       REPORT_PREDEFINED_ERR_MSG(
      78              :           "E10001", std::vector<const char *>({"parameter", "value", "reason"}),
      79              :           std::vector<const char *>({readable_name.c_str(), iter->second.c_str(), reason.c_str()}));
      80              :       return FAILED;
      81              :     }
      82              :   }
      83              :   return SUCCESS;
      84              : }
      85              : 
      86              : Status CheckAutoTuneMode(const std::map<std::string, std::string> &options) {
      87              :   auto option_key = options.find("ge.autoTuneMode");
      88              :   if (option_key != options.end() && !option_key->second.empty()) {
      89              :     const auto readable_name = ge::GetContext().GetReadableName("ge.autoTuneMode");
      90              :     REPORT_PREDEFINED_ERR_MSG(
      91              :         "E10001", std::vector<const char *>({"parameter", "value", "reason"}),
      92              :         std::vector<const char *>({readable_name.c_str(), option_key->second.c_str(),
      93              :                                    "The Auto Tune function has been discarded. Please use the AOE tool for tuning."}));
      94              :     GELOGE(
      95              :         FAILED,
      96              :         "[Check][Param]Options[%s] unsupported, The Auto Tune function has been discarded. Please use the AOE tool for "
      97              :         "tuning.",
      98              :         option_key->first.c_str());
      99              :     return FAILED;
     100              :   }
     101              :   return SUCCESS;
     102              : }
     103              : 
     104              : Status CheckOpPrecisionMode(const std::map<std::string, std::string> &options) {
     105              :   auto iter = options.find(ge::OP_PRECISION_MODE);
     106              :   if (iter != options.end() && !iter->second.empty() && !ge::CheckInputPathValid(iter->second)) {
     107              :     REPORT_PREDEFINED_ERR_MSG(
     108              :         "E10001", std::vector<const char *>({"parameter", "value", "reason"}),
     109              :         std::vector<const char *>({ge::OP_PRECISION_MODE.c_str(), iter->second.c_str(), "path is not found."}));
     110              :     GELOGE(PARAM_INVALID, "[Check][OP_PRECISION_MODE] %s not found", iter->second.c_str());
     111              :     return FAILED;
     112              :   }
     113              :   if (iter != options.end()) {
     114              :     GELOGI("Option set successfully, option = %s, value=%s", ge::OP_PRECISION_MODE.c_str(), iter->second.c_str());
     115              :   }
     116              :   return CheckPrecisionModeParamValid(options);
     117              : }
     118              : 
     119              : void SetSessionDeviceId() {
     120              :   std::string str_session_device_id;
     121              :   if (GetContext().GetOption("ge.session_device_id", str_session_device_id) == SUCCESS) {
     122              :     GELOGI("Option session device id has set, value is %s.", str_session_device_id.c_str());
     123              :     try {
     124              :       const uint32_t session_device_id = static_cast<uint32_t>(std::stoi(str_session_device_id.c_str()));
     125              :       GetContext().SetCtxDeviceId(session_device_id);
     126              :     } catch (...) {
     127              :       GELOGW("Option session device id is invalid, value is %s.", str_session_device_id.c_str());
     128              :     }
     129              :   }
     130              : }
     131              : 
     132              : }  // namespace
     133              : 
     134              : static std::mutex mutex_;  // BuildGraph and RunGraph use
     135              : bool InnerSession::is_dump_server_inited_ = false;
     136              : std::mutex InnerSession::dump_server_mutex_;
     137              : InnerSession::InnerSession(uint64_t session_id, const std::map<std::string, std::string> &options)
     138              :     : is_initialized_(false), session_id_(session_id), options_(options) {}
     139              : 
     140              : Status InnerSession::InitializeVarManager() {
     141              :   constexpr uint32_t version = static_cast<uint32_t>(SessionVersion::ClOUD_VERSION);
     142              :   constexpr uint32_t DEFAULT_JOB_ID = 0;
     143              :   GE_CHECK_NOTNULL(VarManager::Instance(session_id_));
     144              :   const Status ret =
     145              :       VarManager::Instance(session_id_)->Init(version, session_id_, GetContext().DeviceId(), DEFAULT_JOB_ID);
     146              :   if (ret != SUCCESS) {
     147              :     GELOGE(ret, "[Init][VarManager] failed.");
     148              :     REPORT_INNER_ERR_MSG("E19999", "VarManager init failed, InnerSession:%" PRIu64 ".", session_id_);
     149              :     GE_CHK_STATUS(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
     150              :   }
     151              :   return ret;
     152              : }
     153              : 
     154              : Status InnerSession::Initialize() {
     155              :   if (is_initialized_) {
     156              :     GELOGW("[InnerSession:%" PRIu64 "] session already initialize.", session_id_);
     157              :     return SUCCESS;
     158              :   }
     159              :   user_graphs_manager_ = MakeShared<UserGraphsManager>(graph_manager_);
     160              :   if (user_graphs_manager_ == nullptr) {
     161              :     return MEMALLOC_FAILED;
     162              :   }
     163              :   user_hybrid_graph_manager_ = MakeShared<UserHybridGraphManager>(*user_graphs_manager_);
     164              :   if (user_hybrid_graph_manager_ == nullptr) {
     165              :     return MEMALLOC_FAILED;
     166              :   }
     167              :   GE_CHK_STATUS_RET(CoreNumUtils::ParseAicoreNumFromOption(options_));
     168              : 
     169              :   const std::map<std::string, std::string>::const_iterator it = options_.find(ge::SOC_VERSION);
     170              :   if (it == options_.cend()) {
     171              :     const char *version = aclrtGetSocName();
     172              :     GE_IF_BOOL_EXEC(version == nullptr, REPORT_INNER_ERR_MSG("E19999", "aclrtGetSocName failed.");
     173              :                     GELOGE(FAILED, "[Get][SocVersion]aclrtGetSocName failed"); return FAILED;)
     174              :     GELOGI("Succeeded in getting SOC_VERSION[%s] from runtime in InnerSession::Initialize.", version);
     175              :     options_.insert(std::make_pair(ge::SOC_VERSION, version));
     176              :   }
     177              : 
     178              :   logLevel_ = static_cast<uint8_t>(dlog_getlevel(GE_MODULE_NAME, nullptr));
     179              :   // If the global options and the session options are duplicated, the session options is preferred.
     180              :   auto all_options = options_;
     181              :   {
     182              :     auto &global_options_mutex = GetGlobalOptionsMutex();
     183              :     const std::lock_guard<std::mutex> lock(global_options_mutex);
     184              :     all_options.insert(GetMutableGlobalOptions().cbegin(), GetMutableGlobalOptions().cend());
     185              :   }
     186              : 
     187              :   GE_ASSERT_SUCCESS(CheckAutoTuneMode(all_options));
     188              : 
     189              :   Status ret = CheckReuseMemoryOption(all_options);
     190              :   if (ret != SUCCESS) {
     191              :     GELOGE(ret, "[CheckReuse][MemoryOption] failed, [InnerSession:%" PRIu64 "].", session_id_);
     192              :     return ret;
     193              :   }
     194              : 
     195              :   GE_ASSERT_SUCCESS(CheckOpPrecisionMode(all_options));
     196              : 
     197              :   // Check option modify_mixlist
     198              :   if (ge::CheckModifyMixlistParamValid(all_options) != ge::SUCCESS) {
     199              :     return FAILED;
     200              :   }
     201              :   GE_ASSERT_SUCCESS(CheckOptionValidValues(all_options, OPTION_FEATURE_BASE_REFRESHABLE, kFeatureMapRefreshOptions));
     202              :   GE_ASSERT_SUCCESS(CheckOptionValidValues(all_options, OPTION_CONST_LIFECYCLE, kConstLifecycleOptions));
     203              :   GE_ASSERT_SUCCESS(CheckOptionValidThreshold(all_options, OPTION_HOST_SCHEDULING_MAX_THRESHOLD));
     204              :   GE_ASSERT_SUCCESS(CheckOptionValidValues(all_options, TILING_SCHEDULE_OPTIMIZE, kStateOptions));
     205              :   GE_ASSERT_GRAPH_SUCCESS(CheckOptimizationOptionValid(all_options));
     206              : 
     207              :   UpdateGlobalSessionContext();
     208              :   GetThreadLocalContext().SetGraphOption({});
     209              :   SetSessionDeviceId();
     210              :   GE_CHK_STATUS_RET(aclrtSetDevice(static_cast<int32_t>(GetContext().DeviceId())), "Set device failed.");
     211              : 
     212              :   ModelHelper model_helper;
     213              :   GE_CHK_STATUS_RET(model_helper.GetHardwareInfo(options_),
     214              :                     "[Get][Hardware]InnerSession Initialize: Get hardware info failed.");
     215              : 
     216              :   DumpProperties dump_properties;
     217              :   GE_CHK_STATUS_RET(dump_properties.InitByOptions(), "Init dump properties failed.");
     218              :   GE_CHK_STATUS_RET(AddDumpProperties(dump_properties), "[Add][DumpProperties] failed.");
     219              : 
     220              :   ret = InnerInitialize();
     221              :   if (ret != SUCCESS) {
     222              :     GELOGE(ret, "[Init][GraphManager] failed, InnerSession:%" PRIu64 ".", session_id_);
     223              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager initialize failed, InnerSession:%" PRIu64 ".", session_id_);
     224              :     GE_CHK_STATUS(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
     225              :     return ret;
     226              :   }
     227              : 
     228              :   GE_ASSERT_SUCCESS(InitializeVarManager());
     229              :   is_initialized_ = true;
     230              :   return SUCCESS;
     231              : }
     232              : 
     233              : Status InnerSession::Finalize() {
     234              :   std::lock_guard<std::mutex> lock(resource_mutex_);
     235              :   if (!is_initialized_) {
     236              :     GELOGW("[InnerSession:%" PRIu64 "] session does not initialize.", session_id_);
     237              :     return SUCCESS;
     238              :   }
     239              :   UpdateGlobalSessionContext();
     240              :   GetThreadLocalContext().SetGraphOption({});
     241              :   if (user_hybrid_graph_manager_ != nullptr) {
     242              :     user_hybrid_graph_manager_->Finalize();
     243              :   }
     244              :   if (user_graphs_manager_ != nullptr) {
     245              :     user_graphs_manager_->Finalize();
     246              :   }
     247              :   Status ret = InnerFinalize();
     248              :   if (ret != SUCCESS) {
     249              :     // Subsequent code execution is required, so no return is required
     250              :     GELOGE(ret, "[Finalize][GraphManager] failed, InnerSession:%" PRIu64 ".", session_id_);
     251              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager Finalize failed, InnerSession:%" PRIu64 ".", session_id_);
     252              :   }
     253              : 
     254              :   is_initialized_ = false;
     255              :   // release analyzer saved info(Session Level)
     256              :   Analyzer::GetInstance()->DestroySessionJsonObject(session_id_);
     257              : 
     258              :   GE_CHK_RT(aclrtResetDevice(static_cast<int32_t>(GetContext().DeviceId())));
     259              :   GE_CHK_STATUS_RET(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
     260              :   VarManagerPool::Instance().RemoveVarManager(session_id_);
     261              :   gert::Om2RTVarManagerPool::Instance().RemoveManager(session_id_);
     262              :   gert::Om2ExternalWeightManagerPool::Instance().RemoveManager(session_id_);
     263              :   SessionMemAllocator<ExpandableActiveMemoryAllocator>::Instance().RemoveAllocator(session_id_);
     264              :   SessionMemAllocator<FixedBaseExpandableAllocator>::Instance().RemoveAllocator(session_id_);
     265              :   SessionMemAllocator<ActiveMemoryAllocator>::Instance().RemoveAllocator(session_id_);
     266              :   return ret;
     267              : }
     268              : 
     269              : Status InnerSession::InnerInitialize() {
     270              :   Status ret = model_executor_.Initialize(options_, session_id_);
     271              :   if (ret != SUCCESS) {
     272              :     GELOGE(ret, "[Init][GraphExecutor] failed, InnerSession:%" PRIu64 ".", session_id_);
     273              :     REPORT_INNER_ERR_MSG("E19999", "GraphExecutor initialize failed, InnerSession:%" PRIu64 ".", session_id_);
     274              :     GE_CHK_STATUS(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
     275              :     return ret;
     276              :   }
     277              : 
     278              :   ret = graph_manager_.Initialize(options_, &model_executor_);
     279              :   if (ret != SUCCESS) {
     280              :     GELOGE(ret, "[Init][GraphManager] failed, InnerSession:%" PRIu64 ".", session_id_);
     281              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager initialize failed, InnerSession:%" PRIu64 ".", session_id_);
     282              :     GE_CHK_STATUS(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
     283              :     return ret;
     284              :   }
     285              :   // model executor thread should run later, in case graph_manager init failed.
     286              :   model_executor_.StartRunThread();
     287              :   return SUCCESS;
     288              : }
     289              : 
     290              : Status InnerSession::InnerFinalize() {
     291              :   Status ret = graph_manager_.Finalize();
     292              :   if (ret != SUCCESS) {
     293              :     // Subsequent code execution is required, so no return is required
     294              :     GELOGE(ret, "[Finalize][GraphManager] failed, InnerSession:%" PRIu64 ".", session_id_);
     295              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager Finalize failed, InnerSession:%" PRIu64 ".", session_id_);
     296              :   }
     297              : 
     298              :   ret = model_executor_.Finalize();
     299              :   if (ret != SUCCESS) {
     300              :     // Subsequent code execution is required, so no return is required
     301              :     GELOGE(ret, "[Finalize][GraphExecutor] failed, InnerSession:%" PRIu64 ".", session_id_);
     302              :     REPORT_INNER_ERR_MSG("E19999", "GraphExecutor Finalize failed, InnerSession:%" PRIu64 ".", session_id_);
     303              :   }
     304              :   return SUCCESS;
     305              : }
     306              : 
     307              : Status InnerSession::AddGraph(uint32_t graph_id, const Graph &graph) {
     308              :   std::map<std::string, std::string> options;
     309              :   return AddGraph(graph_id, graph, options);
     310              : }
     311              : 
     312              : Status InnerSession::AddGraph(uint32_t graph_id, const Graph &graph,
     313              :                               const std::map<std::string, std::string> &options) {
     314              :   std::lock_guard<std::mutex> lock(resource_mutex_);
     315              : 
     316              :   PrintOptionsWithLengthLimit(options, "AddGraph option");
     317              : 
     318              :   auto iter = options.find("ge.autoTuneMode");
     319              :   if ((iter != options.end()) && (!iter->second.empty())) {
     320              :     const auto readable_name = ge::GetContext().GetReadableName("ge.autoTuneMode");
     321              :     REPORT_PREDEFINED_ERR_MSG(
     322              :         "E10001", std::vector<const char *>({"parameter", "value", "reason"}),
     323              :         std::vector<const char *>({readable_name.c_str(), iter->second.c_str(),
     324              :                                    "The Auto Tune function has been discarded. Please use the AOE tool for tuning."}));
     325              :     GELOGE(
     326              :         FAILED,
     327              :         "[Check][Param]Options[%s] unsupported, The Auto Tune function has been discarded. Please use the AOE tool for "
     328              :         "tuning.",
     329              :         iter->first.c_str());
     330              :     return FAILED;
     331              :   }
     332              :   GE_ASSERT_SUCCESS(SetSessionGraphId(graph, session_id_, graph_id));
     333              :   UpdateGlobalSessionContext();
     334              : 
     335              :   GE_ASSERT_NOTNULL(user_hybrid_graph_manager_);
     336              :   Status ret = user_hybrid_graph_manager_->AddGraph(graph_id, graph, options);
     337              :   if (ret != SUCCESS) {
     338              :     GELOGE(ret, "[Add][Graph] failed, InnerSession:%" PRIu64 " graphid: %u.", session_id_, graph_id);
     339              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager AddGraph failed, InnerSession:%" PRIu64 " graphid: %u.", session_id_,
     340              :                          graph_id);
     341              :     return ret;
     342              :   }
     343              :   const uint32_t device_id = GetContext().DeviceId();
     344              :   GELOGD("The device id is %u", device_id);
     345              :   (void)ProfilingInit::Instance().SetDeviceIdByModelId(graph_id, device_id);
     346              :   ProfilingManager::Instance().SetGraphIdToDeviceMap(graph_id, device_id);
     347              :   GELOGI("[InnerSession:%" PRIu64 "] Add graph success, graph_id=%u.", session_id_, graph_id);
     348              :   return SUCCESS;
     349              : }
     350              : 
     351              : Status InnerSession::SetSessionGraphId(const Graph &graph, uint64_t session_id, uint32_t graph_id) {
     352              :   auto compute_graph = GraphUtilsEx::GetComputeGraph(graph);
     353              :   GE_CHECK_NOTNULL(compute_graph);
     354              :   std::string session_graph_id = std::to_string(session_id) + "_" + std::to_string(graph_id);
     355              :   if (!AttrUtils::SetStr(*compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
     356              :     GELOGW("Set graph session_graph_id attr failed.");
     357              :   } else {
     358              :     GELOGD("Set graph session_graph_id attr to [%s]", session_graph_id.c_str());
     359              :   }
     360              :   for (auto sub_graph : compute_graph->GetAllSubgraphs()) {
     361              :     (void)AttrUtils::SetStr(*sub_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id);
     362              :   }
     363              :   return SUCCESS;
     364              : }
     365              : 
     366              : Status InnerSession::LoadGraph(const uint32_t graph_id, const std::map<AscendString, AscendString> &options,
     367              :                                void *stream) {
     368              :   GELOGI("[InnerSession] Load graph by graph_id=%u, stream = %p", graph_id, stream);
     369              :   PrintOptionsWithLengthLimit(options, "LoadGraph option");
     370              :   UpdateGlobalSessionContext();
     371              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     372              :   const auto ret = user_graphs_manager_->LoadGraph(graph_id, options, stream);
     373              :   if (ret != SUCCESS) {
     374              :     GELOGE(ret, "[Load][Graph] Failed, graph_id:%u.", graph_id);
     375              :     return ret;
     376              :   }
     377              :   return SUCCESS;
     378              : }
     379              : 
     380              : Status InnerSession::AddGraphWithCopy(uint32_t graph_id, const Graph &graph,
     381              :                                       const std::map<std::string, std::string> &options) {
     382              :   std::lock_guard<std::mutex> lock(resource_mutex_);
     383              :   GE_ASSERT_SUCCESS(SetSessionGraphId(graph, session_id_, graph_id));
     384              :   PrintOptionsWithLengthLimit(options, "AddGraphWithCopy option");
     385              :   UpdateGlobalSessionContext();
     386              :   Status ret = graph_manager_.AddGraphWithCopy(graph_id, graph, options, domi::GetContext());
     387              :   if (ret != SUCCESS) {
     388              :     GELOGE(ret, "[Add][Graph] failed, InnerSession:%" PRIu64 " graphid: %u.", session_id_, graph_id);
     389              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager AddGraphWithCopy failed, InnerSession:%" PRIu64 " graphid: %u.",
     390              :                          session_id_, graph_id);
     391              :     return ret;
     392              :   }
     393              : 
     394              :   GELOGI("[InnerSession:%" PRIu64 "] add graph success, graph_id=%u.", session_id_, graph_id);
     395              :   return SUCCESS;
     396              : }
     397              : 
     398              : Status InnerSession::RunGraph(uint32_t graph_id, const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs) {
     399              :   GELOGI("[InnerSession:%" PRIu64 "] Run graph on session, graph_id=%u.", session_id_, graph_id);
     400              :   if (std::unique_lock<std::mutex> lock{mutex_, std::try_to_lock}) {
     401              :     UpdateGlobalSessionContext();
     402              :     GE_ASSERT_NOTNULL(user_graphs_manager_);
     403              :     return user_graphs_manager_->RunGraph(graph_id, inputs, outputs, GetSessionId());
     404              :   } else {
     405              :     GELOGE(GE_SESS_ALREADY_RUNNING, "[Run][Graph]failed, InnerSession:%" PRIu64 ", graph_id=%u.", session_id_,
     406              :            graph_id);
     407              :     REPORT_INNER_ERR_MSG("E19999",
     408              :                          "RunGraph failed because mutex try_lock false, InnerSession:%" PRIu64 ", graph_id=%u.",
     409              :                          session_id_, graph_id);
     410              :     return GE_SESS_ALREADY_RUNNING;
     411              :   }
     412              : }
     413              : 
     414              : Status InnerSession::RunGraph(uint32_t graph_id, const std::vector<gert::Tensor> &inputs,
     415              :                               std::vector<gert::Tensor> &outputs) {
     416              :   GELOGI("[InnerSession:%" PRIu64 "] Run graph on session, graph_id=%u.", session_id_, graph_id);
     417              :   if (std::unique_lock<std::mutex> lock{mutex_, std::try_to_lock}) {
     418              :     UpdateGlobalSessionContext();
     419              :     GE_ASSERT_NOTNULL(user_graphs_manager_);
     420              :     return user_graphs_manager_->RunGraph(graph_id, inputs, outputs, GetSessionId());
     421              :   } else {
     422              :     GELOGE(GE_SESS_ALREADY_RUNNING, "[Run][Graph]failed, InnerSession:%" PRIu64 ", graph_id=%u.", session_id_,
     423              :            graph_id);
     424              :     REPORT_INNER_ERR_MSG("E19999",
     425              :                          "RunGraph failed because mutex try_lock false, InnerSession:%" PRIu64 ", graph_id=%u.",
     426              :                          session_id_, graph_id);
     427              :     return GE_SESS_ALREADY_RUNNING;
     428              :   }
     429              : }
     430              : 
     431              : Status InnerSession::ExecuteGraphWithStreamAsync(uint32_t graph_id, const aclrtStream stream,
     432              :                                                  const std::vector<gert::Tensor> &inputs,
     433              :                                                  std::vector<gert::Tensor> &outputs) {
     434              :   if (logLevel_ <= DLOG_INFO) {
     435              :     GELOGI("Execute graph with stream begin, session id = %" PRIu64
     436              :            ", graph id = %u,"
     437              :            "stream = %p, input size = %zu, output size = %zu",
     438              :            session_id_, graph_id, stream, inputs.size(), outputs.size());
     439              :   }
     440              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     441              :   const Status res = user_graphs_manager_->ExecuteGraphWithStreamAsync(graph_id, stream, inputs, outputs, session_id_);
     442              :   if (res != SUCCESS) {
     443              :     GELOGE(res,
     444              :            "[Execute][GraphWithStreamAsync]failed,"
     445              :            "session id = %" PRIu64 ", graph id = %u, stream = %p.",
     446              :            session_id_, graph_id, stream);
     447              :     REPORT_INNER_ERR_MSG("E19999",
     448              :                          "GraphManager ExecuteGraphWithStreamAsync failed,"
     449              :                          "session id = %" PRIu64 ", graph id = %u, stream = %p.",
     450              :                          session_id_, graph_id, stream);
     451              :     return res;
     452              :   }
     453              : 
     454              :   if (logLevel_ <= DLOG_INFO) {
     455              :     GELOGI("Execute graph with stream async success, session id = %" PRIu64 ", graph id = %u, stream = %p.",
     456              :            session_id_, graph_id, stream);
     457              :   }
     458              : 
     459              :   return SUCCESS;
     460              : }
     461              : 
     462              : Status InnerSession::RunGraphWithStreamAsync(uint32_t graph_id, aclrtStream stream, const std::vector<Tensor> &inputs,
     463              :                                              std::vector<Tensor> &outputs) {
     464              :   if (logLevel_ <= DLOG_INFO) {
     465              :     GELOGI("Run graph with stream begin, session id = %" PRIu64
     466              :            ", graph id = %u,"
     467              :            "stream = %p, input size = %zu, output size = %zu",
     468              :            session_id_, graph_id, stream, inputs.size(), outputs.size());
     469              :   }
     470              :   UpdateGlobalSessionContext();
     471              :   std::vector<GeTensor> ge_inputs;
     472              :   ge_inputs.reserve(inputs.size());
     473              :   for (auto &item : inputs) {
     474              :     ge_inputs.emplace_back(TensorAdapter::AsGeTensorShared(item));
     475              :   }
     476              :   std::vector<GeTensor> ge_outputs;
     477              :   ge_outputs.reserve(outputs.size());
     478              :   for (auto &item : outputs) {
     479              :     ge_outputs.emplace_back(TensorAdapter::AsGeTensorShared(item));
     480              :   }
     481              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     482              :   const Status res =
     483              :       user_graphs_manager_->RunGraphWithStreamAsync(graph_id, stream, ge_inputs, ge_outputs, session_id_);
     484              :   if (res != SUCCESS) {
     485              :     GELOGE(res,
     486              :            "[Run][GraphWithStreamAsync]failed,"
     487              :            "session id = %" PRIu64 ", graph id = %u, stream = %p.",
     488              :            session_id_, graph_id, stream);
     489              :     REPORT_INNER_ERR_MSG("E19999",
     490              :                          "GraphManager RunGraphWithStreamAsync failed,"
     491              :                          "session id = %" PRIu64 ", graph id = %u, stream = %p.",
     492              :                          session_id_, graph_id, stream);
     493              :     return res;
     494              :   }
     495              : 
     496              :   // if alloc output memory by external allocator, should return to user.
     497              :   CopyGeOutputsMemToUserOutputs(ge_outputs, outputs);
     498              :   if (logLevel_ <= DLOG_INFO) {
     499              :     GELOGI("Run graph with stream async success, session id = %" PRIu64 ", graph id = %u, stream = %p.", session_id_,
     500              :            graph_id, stream);
     501              :   }
     502              :   return SUCCESS;
     503              : }
     504              : 
     505              : Status InnerSession::RemoveGraph(uint32_t graph_id) {
     506              :   std::lock_guard<std::mutex> lock(resource_mutex_);
     507              :   const auto device_id = GetContext().DeviceId();
     508              :   GELOGD("Remove device id %u", device_id);
     509              :   (void)ProfilingInit::Instance().UnsetDeviceIdByModelId(graph_id, device_id);
     510              :   UpdateGlobalSessionContext();
     511              :   GE_ASSERT_NOTNULL(user_hybrid_graph_manager_);
     512              :   const Status ret = user_hybrid_graph_manager_->RemoveGraph(graph_id);
     513              :   if (ret != SUCCESS) {
     514              :     GELOGE(ret, "[Remove][Graph] failed, InnerSession:%" PRIu64 ", graph_id=%u.", session_id_, graph_id);
     515              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager RemoveGraph failed, InnerSession:%" PRIu64 ", graph_id=%u.",
     516              :                          session_id_, graph_id);
     517              :     return ret;
     518              :   }
     519              :   GELOGI("[InnerSession:%" PRIu64 "] Remove graph success, graph_id=%u.", session_id_, graph_id);
     520              :   return SUCCESS;
     521              : }
     522              : 
     523              : Status InnerSession::RegisterCallBackFunc(
     524              :     const std::string &key,
     525              :     const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback) {
     526              :   std::lock_guard<std::mutex> lock(resource_mutex_);
     527              :   UpdateGlobalSessionContext();
     528              :   GetThreadLocalContext().SetGraphOption({});
     529              :   auto callback_func = [callback](uint32_t graph_id, const std::map<AscendString, gert::Tensor> &params_list) {
     530              :     std::map<std::string, ge::Tensor> para_map;
     531              :     for (const auto &item : params_list) {
     532              :       ge::Tensor tensor;
     533              :       if (ge::TensorTransUtils::GertTensor2Tensor(item.second, tensor) != SUCCESS) {
     534              :         GELOGE(FAILED, "convert ge::Tensor to gert::Tensor failed");
     535              :         return FAILED;
     536              :       }
     537              :       para_map[item.first.GetString()] = std::move(tensor);
     538              :     }
     539              :     return callback(graph_id, para_map);
     540              :   };
     541              :   const Status ret = graph_manager_.RegisterCallBackFunc(key, callback_func);
     542              :   if (ret != SUCCESS) {
     543              :     GELOGE(ret, "[Register][CallBackFunc] failed, InnerSession:%" PRIu64 " register %s.", session_id_, key.c_str());
     544              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager RegisterCallBackFunc failed, InnerSession:%" PRIu64 " register %s.",
     545              :                          session_id_, key.c_str());
     546              :     return ret;
     547              :   }
     548              : 
     549              :   GELOGI("[InnerSession:%" PRIu64 "] register %s callback function success.", session_id_, key.c_str());
     550              :   return SUCCESS;
     551              : }
     552              : 
     553              : Status InnerSession::RegisterCallBackFunc(
     554              :     const std::string &key,
     555              :     const std::function<Status(uint32_t, const std::map<AscendString, ge::Tensor> &)> &callback) {
     556              :   std::lock_guard<std::mutex> lock(resource_mutex_);
     557              :   UpdateGlobalSessionContext();
     558              :   GetThreadLocalContext().SetGraphOption({});
     559              :   auto callback_func = [callback](uint32_t graph_id, const std::map<AscendString, gert::Tensor> &params_list) {
     560              :     std::map<AscendString, ge::Tensor> para_map;
     561              :     for (const auto &item : params_list) {
     562              :       ge::Tensor tensor;
     563              :       if (ge::TensorTransUtils::GertTensor2Tensor(item.second, tensor) != SUCCESS) {
     564              :         GELOGE(FAILED, "convert ge::Tensor to gert::Tensor failed");
     565              :         return FAILED;
     566              :       }
     567              :       para_map[item.first] = std::move(tensor);
     568              :     }
     569              :     return callback(graph_id, para_map);
     570              :   };
     571              :   const Status ret = graph_manager_.RegisterCallBackFunc(key, callback_func);
     572              :   if (ret != SUCCESS) {
     573              :     GELOGE(ret, "[Register][CallBackFunc] failed, InnerSession:%" PRIu64 " register %s.", session_id_, key.c_str());
     574              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager RegisterCallBackFunc failed, InnerSession:%" PRIu64 " register %s.",
     575              :                          session_id_, key.c_str());
     576              :     return ret;
     577              :   }
     578              : 
     579              :   GELOGI("[InnerSession:%" PRIu64 "] register %s callback function success.", session_id_, key.c_str());
     580              :   return SUCCESS;
     581              : }
     582              : 
     583              : Status InnerSession::RegisterCallBackFunc(
     584              :     const std::string &key,
     585              :     const std::function<Status(uint32_t, const std::map<AscendString, gert::Tensor> &)> &callback) {
     586              :   std::lock_guard<std::mutex> lock(resource_mutex_);
     587              :   UpdateGlobalSessionContext();
     588              :   GetThreadLocalContext().SetGraphOption({});
     589              :   const Status ret = graph_manager_.RegisterCallBackFunc(key, callback);
     590              :   if (ret != SUCCESS) {
     591              :     GELOGE(ret, "[Register][CallBackFunc] failed, InnerSession:%" PRIu64 " register %s.", session_id_, key.c_str());
     592              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager RegisterCallBackFunc failed, InnerSession:%" PRIu64 " register %s.",
     593              :                          session_id_, key.c_str());
     594              :     return ret;
     595              :   }
     596              : 
     597              :   GELOGI("[InnerSession:%" PRIu64 "] register %s callback function success.", session_id_, key.c_str());
     598              :   return SUCCESS;
     599              : }
     600              : 
     601              : Status InnerSession::BuildGraph(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
     602              :   GELOGI("[InnerSession:%" PRIu64 "] Build graph on session, graph_id=%u.", session_id_, graph_id);
     603              :   std::vector<ge::GeTensor> ge_inputs;
     604              :   for (auto const &input : inputs) {
     605              :     std::vector<int64_t> input_dims;
     606              :     (void)std::transform(input.dims.begin(), input.dims.end(), std::back_inserter(input_dims),
     607              :                          [](int64_t x) -> int64_t { return x; });
     608              :     GeShape input_shape(input_dims);
     609              :     GeTensorDesc input_tensor_desc;
     610              :     input_tensor_desc.SetShape(input_shape);
     611              :     input_tensor_desc.SetDataType(static_cast<ge::DataType>(input.data_type));
     612              :     ge_inputs.emplace_back(input_tensor_desc);
     613              :   }
     614              :   UpdateGlobalSessionContext();
     615              :   GeRootModelPtr ge_root_model = nullptr;
     616              :   Status ret = graph_manager_.BuildGraph(graph_id, ge_inputs, ge_root_model, session_id_, true);
     617              :   if (ret != SUCCESS) {
     618              :     GELOGE(ret, "[Build][Graph] failed, InnerSession:%" PRIu64 " graph_id=%u.", session_id_, graph_id);
     619              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager BuildGraph failed, InnerSession:%" PRIu64 " graph_id=%u.", session_id_,
     620              :                          graph_id);
     621              :     return ret;
     622              :   }
     623              :   GELOGI("[InnerSession:%" PRIu64 "] build graph success, graph_id=%u.", session_id_, graph_id);
     624              :   return ret;
     625              : }
     626              : 
     627              : Status InnerSession::BuildGraph(uint32_t graph_id, const std::vector<ge::Tensor> &inputs) {
     628              :   GELOGI("[InnerSession:%" PRIu64 "] build graph on session, graph_id=%u.", session_id_, graph_id);
     629              : 
     630              :   std::vector<ge::GeTensor> ge_inputs;
     631              :   for (const auto &input : inputs) {
     632              :     ge_inputs.emplace_back(TensorAdapter::AsGeTensor(input));
     633              :   }
     634              :   UpdateGlobalSessionContext();
     635              :   GE_ASSERT_NOTNULL(user_hybrid_graph_manager_);
     636              :   GeRootModelPtr ge_root_model = nullptr;
     637              :   Status ret = user_hybrid_graph_manager_->BuildGraph(graph_id, ge_inputs, session_id_);
     638              :   if (ret != SUCCESS) {
     639              :     GELOGE(ret, "[Build][Graph] failed, InnerSession:%" PRIu64 " graph_id=%u.", session_id_, graph_id);
     640              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager BuildGraph failed, InnerSession:%" PRIu64 " graph_id=%u.", session_id_,
     641              :                          graph_id);
     642              :     return ret;
     643              :   }
     644              :   GELOGI("[InnerSession:%" PRIu64 "] build graph success, graph_id=%u.", session_id_, graph_id);
     645              :   return ret;
     646              : }
     647              : 
     648              : Status InnerSession::RunGraphAsync(uint32_t graph_id, std::vector<gert::Tensor> &&inputs,
     649              :                                    const RunAsyncCallbackV2 &callback) {
     650              :   GELOGI("[InnerSession:%" PRIu64 "] run graph on session, graph_id=%u.", session_id_, graph_id);
     651              :   UpdateGlobalSessionContext();
     652              : 
     653              :   GE_ASSERT_NOTNULL(user_hybrid_graph_manager_);
     654              :   Status ret = user_hybrid_graph_manager_->RunGraphAsync(graph_id, std::move(inputs), session_id_, callback);
     655              :   if (ret != SUCCESS) {
     656              :     GELOGE(ret, "[Run][GraphAsync]failed, InnerSession:%" PRIu64 " graph_id=%u.", session_id_, graph_id);
     657              :     REPORT_INNER_ERR_MSG("E19999", "GraphManager RunGraphAsync failed, InnerSession:%" PRIu64 " graph_id=%u.",
     658              :                          session_id_, graph_id);
     659              :     return ret;
     660              :   }
     661              :   GELOGI("[InnerSession:%" PRIu64 "] run graph async submit success, graph_id=%u.", session_id_, graph_id);
     662              :   return ret;
     663              : }
     664              : 
     665              : const GraphManager &InnerSession::getGraphManagerObj() const {
     666              :   return graph_manager_;
     667              : }
     668              : 
     669              : void InnerSession::UpdateGlobalSessionContext() const {
     670              :   {
     671              :     auto &global_options_mutex = GetGlobalOptionsMutex();
     672              :     const std::lock_guard<std::mutex> lock(global_options_mutex);
     673              :     GetThreadLocalContext().SetGlobalOption(GetMutableGlobalOptions());
     674              :   }
     675              :   GetThreadLocalContext().SetSessionOption(options_);
     676              :   GetContext().SetSessionId(session_id_);
     677              :   SetTrainFlagOption();
     678              :   SetRtSocVersion();
     679              : }
     680              : 
     681              : bool InnerSession::IsGraphNeedRebuild(uint32_t graph_id) {
     682              :   GE_ASSERT_NOTNULL(user_hybrid_graph_manager_);
     683              :   return user_hybrid_graph_manager_->IsGraphNeedRebuild(graph_id);
     684              : }
     685              : 
     686              : Status InnerSession::GetAllVariables(std::map<std::string, GeTensorDesc> &all_variables) {
     687              :   const auto &instance = VarManager::Instance(session_id_);
     688              :   GE_ASSERT_NOTNULL(instance);
     689              :   return instance->GetAllVariables(all_variables);
     690              : }
     691              : 
     692              : Status InnerSession::GenCheckPointGraph(const std::map<std::string, GeTensorDesc> &all_variables, Graph &graph) {
     693              :   return graph_manager_.GenCheckPointGraph(all_variables, graph);
     694              : }
     695              : 
     696              : Status InnerSession::SaveVariables(const Graph &graph, const std::vector<std::string> &var_names,
     697              :                                    const std::vector<Tensor> &outputs, std::vector<Tensor> &var_values) {
     698              :   return graph_manager_.SaveVariables(graph, var_names, outputs, var_values);
     699              : }
     700              : 
     701              : Status InnerSession::AddDumpProperties(const DumpProperties &dump_properties) {
     702              :   {
     703         1062 :     std::lock_guard<std::mutex> lock(dump_server_mutex_);
     704         1062 :     if (!is_dump_server_inited_) {
     705          969 :       if ((dump_properties.IsDumpOpen() || dump_properties.IsOpDebugOpen())) {
     706           19 :         GE_IF_BOOL_EXEC(AdxDataDumpServerInit() != kDumpStatus,
     707              :                         GELOGE(PARAM_INVALID, "[Init][AdxDataDumpServer] failed, session_id:%" PRIu64 ".", session_id_);
     708              :                         return PARAM_INVALID)
     709           19 :         GELOGI("Init adx data dump server success");
     710           19 :         is_dump_server_inited_ = true;
     711              :       }
     712              :     }
     713              :   }
     714              :   if ((!dump_properties.GetEnableDump().empty()) || (!dump_properties.GetEnableDumpDebug().empty())) {
     715              :     // if dump option set, add dump property
     716              :     GE_IF_BOOL_EXEC(DumpManager::GetInstance().AddDumpProperties(session_id_, dump_properties) != SUCCESS,
     717              :                     GELOGE(PARAM_INVALID, "[Add][DumpProperties] failed, session_id:%" PRIu64 ".", session_id_);
     718              :                     return PARAM_INVALID);
     719              :     if (DumpManager::GetInstance().CheckIfAclDumpSet()) {
     720              :       GELOGW("Set dump by options and acl simultaneously, will use the option setting.");
     721              :     }
     722              :     DumpManager::GetInstance().ClearAclDumpSet();
     723              :   }
     724              :   return SUCCESS;
     725              : }
     726              : 
     727              : Status InnerSession::RemoveDumpProperties() {
     728              :   DumpManager::GetInstance().RemoveDumpProperties(session_id_);
     729              :   {
     730         1056 :     std::lock_guard<std::mutex> lock(dump_server_mutex_);
     731         1056 :     if (is_dump_server_inited_ && DumpManager::GetInstance().GetDumpPropertiesMap().empty()) {
     732           18 :       GE_IF_BOOL_EXEC(AdxDataDumpServerUnInit() != kDumpStatus,
     733              :                       GELOGE(PARAM_INVALID, "[UnInit][AdxDataDumpServer] failed, session_id:%" PRIu64 ".", session_id_);
     734              :                       REPORT_INNER_ERR_MSG("E19999",
     735              :                                            "RemoveDumpProperties failed because AdxDataDumpServerUnInit failed,"
     736              :                                            "session_id:%" PRIu64 ".",
     737              :                                            session_id_);
     738              :                       return PARAM_INVALID)
     739           18 :       GELOGI("UnInit adx data dump server success");
     740           18 :       is_dump_server_inited_ = false;
     741              :     }
     742              :   }
     743              :   return SUCCESS;
     744              : }
     745              : 
     746              : void InnerSession::SetRtSocVersion() {
     747              :   auto &global_options_mutex = GetGlobalOptionsMutex();
     748              :   const std::lock_guard<std::mutex> lock(global_options_mutex);
     749              :   const auto &global_options = GetMutableGlobalOptions();
     750              :   auto it = global_options.find(ge::SOC_VERSION);
     751              :   if (it != global_options.end()) {
     752              :     rtError_t rt_ret = rtSetSocVersion(it->second.c_str());
     753              :     if (rt_ret != RT_ERROR_NONE) {
     754              :       GELOGW("Set soc version %s failed. ret:0x%X", it->second.c_str(), rt_ret);
     755              :     }
     756              :     GELOGI("Set soc version %s success.", it->second.c_str());
     757              :   }
     758              : }
     759              : 
     760              : void InnerSession::SetTrainFlagOption() {
     761              :   auto train_flag = false;
     762              :   std::string run_mode;
     763              :   if ((GetContext().GetOption(ge::OPTION_GRAPH_RUN_MODE, run_mode) == SUCCESS) && (!run_mode.empty())) {
     764              :     if (GraphRunMode(std::strtol(run_mode.c_str(), nullptr, kDecimalSystem)) >= TRAIN) {
     765              :       train_flag = true;
     766              :     }
     767              :   }
     768              :   domi::GetContext().train_flag = train_flag;
     769              :   GELOGI("train flag is %d in session", train_flag);
     770              : }
     771              : 
     772              : Status InnerSession::CompileGraph(uint32_t graph_id, const vector<ge::Tensor> &inputs) {
     773              :   UpdateGlobalSessionContext();
     774              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     775              :   const auto ret = user_graphs_manager_->CompileGraph(graph_id, session_id_, inputs);
     776              :   GE_CHK_STATUS_RET(ret, "[Compile][Graph]Failed, InnerSession:%" PRIu64 ", graph_id:%u, inputs size:%zu.", session_id_,
     777              :                     graph_id, inputs.size());
     778              :   GELOGI("[InnerSession:%" PRIu64 "]Compile graph success, session_id:%" PRIu64 ", graph_id:%u, inputs size:%zu.",
     779              :          session_id_, graph_id, inputs.size());
     780              :   return SUCCESS;
     781              : }
     782              : 
     783              : Status InnerSession::GetCompiledGraphSummary(uint32_t graph_id, CompiledGraphSummaryPtr &summary) {
     784              :   UpdateGlobalSessionContext();
     785              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     786              :   return user_graphs_manager_->GetCompiledGraphSummary(graph_id, summary);
     787              : }
     788              : 
     789              : Status InnerSession::SetGraphConstMemoryBase(uint32_t graph_id, const void *const memory, size_t size) {
     790              :   UpdateGlobalSessionContext();
     791              :   const auto ret = graph_manager_.SetConstMemoryBase(graph_id, memory, size);
     792              :   GE_CHK_STATUS_RET(ret, "[Set][Memory]Failed, InnerSession:%" PRIu64 ", graph_id:%u, memory:%p, size:%zu.",
     793              :                     session_id_, graph_id, memory, size);
     794              :   GELOGI("[InnerSession:%" PRIu64 "]Set graph const memory base success, graph_id:%u, memory:%p, size:%zu.",
     795              :          session_id_, graph_id, memory, size);
     796              :   return SUCCESS;
     797              : }
     798              : 
     799              : Status InnerSession::UpdateGraphFeatureMemoryBase(uint32_t graph_id, const void *const memory, size_t size) {
     800              :   UpdateGlobalSessionContext();
     801              :   const auto ret = graph_manager_.UpdateFeatureMemoryBase(graph_id, memory, size);
     802              :   GE_CHK_STATUS_RET(ret, "[Update][Memory]Failed, InnerSession:%" PRIu64 ", graph_id:%u, memory:%p, size:%zu.",
     803              :                     session_id_, graph_id, memory, size);
     804              :   GELOGI("[InnerSession:%" PRIu64 "]Update graph feature memory base success, graph_id:%u, memory:%p, size:%zu.",
     805              :          session_id_, graph_id, memory, size);
     806              :   return SUCCESS;
     807              : }
     808              : 
     809              : Status InnerSession::SetGraphFixedFeatureMemoryBase(uint32_t graph_id, MemoryType type, const void *const memory,
     810              :                                                     size_t size) {
     811              :   UpdateGlobalSessionContext();
     812              :   const auto ret = graph_manager_.SetFixedFeatureMemoryBase(graph_id, type, memory, size);
     813              :   GE_CHK_STATUS_RET(ret, "[Set][Memory]Failed, InnerSession:%" PRIu64 ", graph_id:%u, type:%d, memory:%p, size:%zu.",
     814              :                     session_id_, graph_id, type, memory, size);
     815              :   return SUCCESS;
     816              : }
     817              : 
     818              : Status InnerSession::UpdateGraphRefreshableFeatureMemoryBase(uint32_t graph_id, const void *const memory, size_t size) {
     819              :   UpdateGlobalSessionContext();
     820              :   const auto ret = graph_manager_.UpdateRefreshableFeatureMemoryBase(graph_id, memory, size);
     821              :   GE_CHK_STATUS_RET(ret, "[Update][Memory]Failed, InnerSession:%" PRIu64 ", graph_id:%u, memory:%p, size:%zu.",
     822              :                     session_id_, graph_id, memory, size);
     823              :   GELOGI("[InnerSession:%" PRIu64
     824              :          "]Update graph refreshable feature memory base success, graph_id:%u, memory:%p, size:%zu.",
     825              :          session_id_, graph_id, memory, size);
     826              :   return SUCCESS;
     827              : }
     828              : 
     829              : Status InnerSession::RegisterExternalAllocator(const void *const stream, AllocatorPtr allocator) const {
     830              :   GE_ASSERT_NOTNULL(stream, "stream is nullptr, session_id:%u.", session_id_);
     831              :   GE_ASSERT_NOTNULL(allocator, "allocator is nullptr, session_id:%u.", session_id_);
     832              : 
     833              :   return graph_manager_.RegisterExternalAllocator(stream, allocator);
     834              : }
     835              : 
     836              : Status InnerSession::UnregisterExternalAllocator(const void *const stream) const {
     837              :   GE_ASSERT_NOTNULL(stream, "stream is nullptr, session_id:%u.", session_id_);
     838              :   return graph_manager_.UnregisterExternalAllocator(stream);
     839              : }
     840              : 
     841              : Status InnerSession::PaRemapped(const uint64_t va, const uint64_t new_pa, const uint64_t len) const {
     842              :   if (IsOm2OnlineMode()) {
     843              :     GELOGE(GE_GRAPH_UNSUPPORTED,
     844              :            "[OM2][Check] PaRemapped is unsupported in OM2 online mode, va:%" PRIu64 ", new_pa:%" PRIu64 ", len:%" PRIu64
     845              :            ".",
     846              :            va, new_pa, len);
     847              :     return GE_GRAPH_UNSUPPORTED;
     848              :   }
     849              :   const auto &ordered_graph_ids = graph_manager_.GetOrderedGraphIds();
     850              :   GE_ASSERT_TRUE(!(ordered_graph_ids.empty()), "[PaRemapped][Graph]there is no graph, InnerSession:%ld", session_id_);
     851              :   Status ret;
     852              :   std::vector<std::pair<uint64_t, uint64_t>> cross_ranges;
     853              :   for (const GraphId graph_id : ordered_graph_ids) {
     854              :     ret = graph_manager_.PaRemapped(graph_id, va, new_pa, len, cross_ranges);
     855              :     if (ret == FAILED) {
     856              :       GELOGW("[PaRemapped] va[%" PRIu64 "] pa[%" PRIu64 "] cannot remap, graph id:%u.", va, new_pa, graph_id);
     857              :       return FAILED;
     858              :     }
     859              :   }
     860              :   return CheckPaRemappedResult(va, len, cross_ranges);
     861              : }
     862              : 
     863              : Status InnerSession::CheckPaRemappedResult(const uint64_t va, const uint64_t len,
     864              :                                            std::vector<std::pair<uint64_t, uint64_t>> &cross_ranges) const {
     865              :   if (cross_ranges.empty()) {
     866              :     return PARAM_INVALID;
     867              :   }
     868              :   std::vector<std::pair<uint64_t, uint64_t>> merged_ranges;
     869              :   // 排序的参数使用了lambda表达式
     870              :   std::sort(
     871              :       cross_ranges.begin(), cross_ranges.end(),
     872              :       [](const std::pair<uint64_t, uint64_t> &a, const std::pair<uint64_t, uint64_t> &b) { return a.first < b.first; });
     873              : 
     874              :   // 第一个区间就可以放进结果集里,后面如果重叠,在merged_ranges上直接合并
     875              :   merged_ranges.push_back(cross_ranges[0]);
     876              :   for (size_t i = 1UL; i < cross_ranges.size(); i++) {
     877              :     // 发现重叠区间或者区间相邻 地址为整数,相邻区间也合并
     878              :     if (merged_ranges.back().second >= cross_ranges[i].first ||
     879              :         merged_ranges.back().second + 1UL == cross_ranges[i].first) {
     880              :       // 合并区间,只更新右边界就好,因为merged_ranges.back()的左边界一定是最小值,因为我们按照左边界排序的
     881              :       merged_ranges.back().second = std::max(merged_ranges.back().second, cross_ranges[i].second);
     882              :     } else {
     883              :       merged_ranges.push_back(cross_ranges[i]);  // 区间不重叠
     884              :     }
     885              :   }
     886              : 
     887              :   if ((merged_ranges.size() == 1UL) && (merged_ranges[0].first == va) &&
     888              :       (merged_ranges[0].second == (va + len - 1UL))) {
     889              :     return SUCCESS;
     890              :   }
     891              :   return PARAM_INVALID;
     892              : }
     893              : Status InnerSession::ForkGraph(uint32_t origin_graph_id, uint32_t forked_graph_id) {
     894              :   return graph_manager_.ForkGraph(origin_graph_id, forked_graph_id);
     895              : }
     896              : 
     897              : Status InnerSession::GetCompiledFlag(uint32_t graph_id, bool &flag) const {
     898              :   flag = false;
     899              :   GE_ASSERT_NOTNULL(user_hybrid_graph_manager_);
     900              :   return user_hybrid_graph_manager_->GetCompiledFlag(graph_id, flag);
     901              : }
     902              : 
     903              : Status InnerSession::DumpDebugJSONPrint(uint32_t graph_id, uint32_t flags, AscendString &json_result) const {
     904              :   UpdateGlobalSessionContext();
     905              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     906              :   const auto ret = user_graphs_manager_->DumpDebugJSONPrint(graph_id, flags, json_result);
     907              :   if (ret != SUCCESS) {
     908              :     GELOGE(ret, "[Dump][DebugJSONPrint] failed, InnerSession:%" PRIu64 ", graph_id=%u.", session_id_, graph_id);
     909              :     REPORT_INNER_ERR_MSG("E19999", "DumpDebugJSONPrint failed, InnerSession:%" PRIu64 ", graph_id=%u.", session_id_,
     910              :                          graph_id);
     911              :     return ret;
     912              :   }
     913              :   return SUCCESS;
     914              : }
     915              : 
     916              : Status InnerSession::SetCompiledFlag(uint32_t graph_id, bool flag) {
     917              :   GE_ASSERT_NOTNULL(user_hybrid_graph_manager_);
     918              :   return user_hybrid_graph_manager_->SetCompiledFlag(graph_id, flag);
     919              : }
     920              : 
     921              : std::shared_ptr<DFlowSessionImpl> InnerSession::GetDFlowSession() const {
     922              :   return dflow_session_impl_;
     923              : }
     924              : 
     925              : void InnerSession::SetDFlowSession(const std::shared_ptr<DFlowSessionImpl> &dflow_session_impl) {
     926              :   dflow_session_impl_ = dflow_session_impl;
     927              : }
     928              : 
     929              : Status InnerSession::GetRunGraphMode(uint32_t graph_id, RunGraphMode &mode) const {
     930              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     931              :   return user_graphs_manager_->GetRunGraphMode(graph_id, mode);
     932              : }
     933              : 
     934              : Status InnerSession::SetRunGraphMode(uint32_t graph_id, const RunGraphMode &mode) {
     935              :   GE_ASSERT_NOTNULL(user_graphs_manager_);
     936              :   return user_graphs_manager_->SetRunGraphMode(graph_id, mode);
     937              : }
     938              : 
     939              : Status InnerSession::GetCompiledModel(uint32_t graph_id, ModelBufferData &model_buffer) {
     940              :   GELOGI("Start to get the compiled model. graph_id: %u.", graph_id);
     941              :   UpdateGlobalSessionContext();
     942              :   return graph_manager_.GetCompiledModel(graph_id, model_buffer);
     943              : }
     944              : 
     945              : bool InnerSession::GetBuildFlag(uint32_t graph_id) const {
     946              :   return graph_manager_.GetBuildFlag(graph_id);
     947              : }
     948              : 
     949              : bool InnerSession::GetLoadFlag(uint32_t graph_id) const {
     950              :   return graph_manager_.GetLoadFlag(graph_id);
     951              : }
     952              : }  // namespace ge
        

Generated by: LCOV version 2.3.2-1