LCOV - code coverage report
Current view: top level - j_6shHsYoJ/dflow/runner/compiler/model - dflow_graph_manager.cc Coverage Total Hit
Test: CHG Lines: 100.0 % 1 1
Test Date: 2026-08-28 11:31:20
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 "dflow/runner/compiler/model/dflow_graph_manager.h"
      12              : #include "framework/common/debug/ge_log.h"
      13              : #include "graph/manager/graph_manager.h"
      14              : #include "graph/utils/tensor_adapter.h"
      15              : #include "graph/utils/graph_utils_ex.h"
      16              : #include "opt_info/ge_opt_info.h"
      17              : #include "graph/ge_context.h"
      18              : #include "common/context/local_context.h"
      19              : #include "common/compile_profiling/ge_call_wrapper.h"
      20              : 
      21              : namespace ge {
      22              : DflowGraphManager::~DflowGraphManager() {
      23              :   // set thread local omg_context to default value, to avoid other use invalid memory
      24              :   SetLocalOmgContext(domi::GetContext());
      25              : }
      26              : 
      27              : Status DflowGraphManager::Initialize(const std::map<std::string, std::string> &options,
      28              :                                      const std::shared_ptr<ProcessNodeEngineImpl> &pneImpl) {
      29              :   std::lock_guard<std::mutex> lock(mutex_);
      30              :   if (is_initialized_) {
      31              :     GELOGW("[DflowGraphManager] Already initialized.");
      32              :     return SUCCESS;
      33              :   }
      34              :   options_ = options;
      35              :   GE_CHK_STATUS_RET(flow_model_builder_.InitProcessNodeEngines(options, pneImpl),
      36              :                     "Flow model builder init process node engines failed.");
      37              :   is_initialized_ = true;
      38              :   GELOGI("[DflowGraphManager] Initialize success.");
      39              :   return SUCCESS;
      40              : }
      41              : 
      42              : void DflowGraphManager::Finalize() {
      43              :   std::lock_guard<std::mutex> lock(mutex_);
      44              :   if (!is_initialized_) {
      45              :     GELOGW("[DflowGraphManager] Not initialized.");
      46              :     return;
      47              :   }
      48              : 
      49              :   graph_options_map_.clear();
      50              :   is_initialized_ = false;
      51              : 
      52              :   GELOGI("[DflowGraphManager] Finalize success.");
      53              : }
      54              : 
      55              : Status DflowGraphManager::AddGraph(uint32_t graph_id, const Graph &graph,
      56              :                                    const std::map<std::string, std::string> &options) {
      57              :   std::lock_guard<std::mutex> lock(mutex_);
      58              :   if (!is_initialized_) {
      59              :     GELOGE(ACL_ERROR_GE_EXEC_NOT_INIT, "[Add][Graph] DflowGraphManager not initialized.");
      60              :     return ACL_ERROR_GE_EXEC_NOT_INIT;
      61              :   }
      62              :   {
      63              :     std::lock_guard<std::mutex> graph_lock(graph_mutex_);
      64              :     if (flow_graph_map_.find(graph_id) != flow_graph_map_.end()) {
      65              :       GELOGE(FAILED, "[Add][Graph] Graph already added, graph_id=%u.", graph_id);
      66              :       return FAILED;
      67              :     } else {
      68              :       graph_options_map_.emplace(graph_id, options);
      69              :       flow_graph_map_.emplace(graph_id, graph);
      70              :       omg_contexts_.emplace(graph_id, domi::GetContext());
      71              :       SetLocalOmgContext(omg_contexts_[graph_id]);
      72              :     }
      73              :   }
      74              :   GELOGI("[DflowGraphManager] Add graph success, graph_id=%u.", graph_id);
      75              :   return SUCCESS;
      76              : }
      77              : 
      78              : Status DflowGraphManager::RemoveGraph(uint32_t graph_id) {
      79              :   std::lock_guard<std::mutex> lock(mutex_);
      80              :   if (!is_initialized_) {
      81              :     GELOGE(ACL_ERROR_GE_EXEC_NOT_INIT, "[Remove][Graph] DflowGraphManager not initialized.");
      82              :     return ACL_ERROR_GE_EXEC_NOT_INIT;
      83              :   }
      84              :   Status ret = SUCCESS;
      85              :   {
      86              :     std::lock_guard<std::mutex> graph_lock(graph_mutex_);
      87              :     if ((flow_graph_map_.find(graph_id) == flow_graph_map_.end()) ||
      88              :         (omg_contexts_.find(graph_id) == omg_contexts_.end()) ||
      89              :         (graph_options_map_.find(graph_id) == graph_options_map_.end())) {
      90              :       GELOGE(FAILED, "[Remove][Graph] Graph id [%u] not found. AddGraph should be called before RemoveGraph.",
      91              :              graph_id);
      92              :       ret = FAILED;
      93              :     }
      94              :     (void)flow_graph_map_.erase(graph_id);
      95              :     (void)omg_contexts_.erase(graph_id);
      96              :     (void)graph_options_map_.erase(graph_id);
      97              :   }
      98              :   {
      99              :     std::lock_guard<std::mutex> flow_model_lock(model_mutex_);
     100              :     (void)flow_model_map_.erase(graph_id);
     101              :   }
     102              :   GELOGI("[DflowGraphManager] Remove graph finished, graph_id=%u.", graph_id);
     103              :   return ret;
     104              : }
     105              : 
     106              : Status DflowGraphManager::CompileGraph(uint32_t graph_id, const std::vector<GeTensor> &inputs) {
     107              :   if (!is_initialized_) {
     108              :     GELOGE(ACL_ERROR_GE_EXEC_NOT_INIT, "[Build][Graph] DflowGraphManager not initialized.");
     109              :     return ACL_ERROR_GE_EXEC_NOT_INIT;
     110              :   }
     111              :   GELOGD("Start compile graph in dflow graph manager, graph_id=%u.");
     112              :   {
     113              :     std::lock_guard<std::mutex> lock(model_mutex_);
     114              :     if (flow_model_map_.find(graph_id) != flow_model_map_.end()) {
     115              :       GEEVENT("Graph id %u has already been compiled.", graph_id);
     116              :       return SUCCESS;
     117              :     }
     118              :   }
     119              :   Graph graph;
     120              :   std::map<std::string, std::string> graph_options;
     121              :   {
     122              :     std::lock_guard<std::mutex> lock(graph_mutex_);
     123              :     const auto iter = graph_options_map_.find(graph_id);
     124              :     const auto graph_iter = flow_graph_map_.find(graph_id);
     125              :     if ((graph_iter == flow_graph_map_.end()) || (iter == graph_options_map_.end())) {
     126              :       GELOGE(FAILED, "Graph id[%u] cannot be found in graph map. AddGraph should be called before CompileGraph.",
     127              :              graph_id);
     128              :       return GE_GRAPH_GRAPH_NOT_EXIST;
     129              :     }
     130              :     graph = graph_iter->second;
     131              :     graph_options = iter->second;
     132              :   }
     133              : 
     134              :   GE_CHK_STATUS_RET(GeOptInfo::SetOptInfo(), "Set opt info failed.");
     135              :   FlowModelPtr flow_model = nullptr;
     136              :   GE_TIMESTAMP_START(BuildModel);
     137              :   GE_CHK_STATUS_RET(flow_model_builder_.BuildModel(graph, inputs, graph_options, flow_model),
     138              :                     "Build graph failed, graph_id=%u.", graph_id);
     139              :   GE_TIMESTAMP_EVENT_END(BuildModel, "FlowModelBuild");
     140              :   GE_CHECK_NOTNULL(flow_model);
     141              :   {
     142              :     std::lock_guard<std::mutex> lock(model_mutex_);
     143              :     flow_model_map_.emplace(graph_id, flow_model);
     144              :   }
     145              :   GELOGI("[DflowGraphManager] Compile graph success, graph_id=%u.", graph_id);
     146              :   return SUCCESS;
     147              : }
     148              : 
     149              : const std::map<std::string, std::string> *DflowGraphManager::GetGraphOptions(uint32_t graph_id) {
     150              :   std::lock_guard<std::mutex> lock(graph_mutex_);
     151              :   auto it = graph_options_map_.find(graph_id);
     152              :   if (it != graph_options_map_.end()) {
     153              :     return &(it->second);
     154              :   }
     155              :   return nullptr;
     156              : }
     157              : 
     158              : Status DflowGraphManager::GetGraphModelId(uint32_t graph_id, uint32_t &model_id) {
     159              :   model_id = INVALID_MODEL_ID;
     160              :   if (!is_initialized_) {
     161              :     GELOGE(ACL_ERROR_GE_EXEC_NOT_INIT, "[Get][GraphModelId] DflowGraphManager not initialized.");
     162              :     return ACL_ERROR_GE_EXEC_NOT_INIT;
     163              :   }
     164              :   std::lock_guard<std::mutex> lock(model_mutex_);
     165              :   const auto iter = flow_model_map_.find(graph_id);
     166              :   if (iter != flow_model_map_.end()) {
     167              :     const auto flow_model = iter->second;
     168              :     GE_CHECK_NOTNULL(flow_model);
     169              :     model_id = flow_model->GetModelId();
     170              :   }
     171              :   return SUCCESS;
     172              : }
     173              : 
     174              : FlowModelPtr DflowGraphManager::GetFlowModel(uint32_t graph_id) const {
     175              :   if (!is_initialized_) {
     176              :     GELOGE(ACL_ERROR_GE_EXEC_NOT_INIT, "[Get][FlowModel] DflowGraphManager not initialized.");
     177              :     return nullptr;
     178              :   }
     179              :   std::lock_guard<std::mutex> lock(model_mutex_);
     180              :   const auto iter = flow_model_map_.find(graph_id);
     181              :   if (iter != flow_model_map_.end()) {
     182              :     return iter->second;
     183              :   }
     184              :   return nullptr;
     185              : }
     186              : 
     187           87 : bool DflowGraphManager::GetOptionsRunGraphFlag() const {
     188              :   if (!is_initialized_) {
     189              :     GELOGW("[Get][OptionsRunGraphFlag] DflowGraphManager not initialized.");
     190              :     return false;
     191              :   }
     192              :   const auto iter = options_.find(RUN_FLAG);
     193              :   if ((iter != options_.end()) && (iter->second == "0")) {
     194              :     return false;
     195              :   }
     196              :   return true;
     197              : }
     198              : }  // namespace ge
        

Generated by: LCOV version 2.3.2-1