diff --git a/ge/CMakeLists.txt b/ge/CMakeLists.txt index 0236e8bd..f98297d8 100755 --- a/ge/CMakeLists.txt +++ b/ge/CMakeLists.txt @@ -262,7 +262,6 @@ set(COMPILER_SRC_LIST "common/dump/dump_op.cc" "common/ge/op_tiling_manager.cc" "common/ge/plugin_manager.cc" - "common/helper/model_cache_helper.cc" "common/profiling/profiling_manager.cc" "engine_manager/dnnengine_manager.cc" "ge_local_engine/engine/host_cpu_engine.cc" @@ -300,7 +299,6 @@ set(COMPILER_SRC_LIST "graph/manager/graph_var_manager.cc" "graph/manager/host_mem_allocator.cc" "graph/manager/host_mem_manager.cc" - "graph/manager/model_manager/event_manager.cc" "graph/manager/rdma_pool_allocator.cc" "graph/manager/session_scope_mem_allocator.cc" "graph/manager/trans_var_data_utils.cc" diff --git a/ge/common/helper/model_cache_helper.cc b/ge/common/helper/model_cache_helper.cc deleted file mode 100755 index 0e6c6329..00000000 --- a/ge/common/helper/model_cache_helper.cc +++ /dev/null @@ -1,1721 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "common/helper/model_cache_helper.h" - -#include -#include -#include - -#include "common/model_parser/model_parser.h" -#include "framework/common/helper/model_helper.h" -#include "graph/detail/model_serialize_imp.h" -#include "graph/utils/graph_utils.h" -#include "graph/utils/tensor_utils.h" -#include "init/gelib.h" -#include "proto/ge_ir.pb.h" - -using namespace std; - -namespace { -const char *const kTbeKernelInfoStoreName = "AIcoreEngine"; -const char *const kGraphName = "temp_name"; -// Keys of json -const char *const kNodeNum = "nodeNum"; -const char *const kEdgeNum = "edgeNum"; -const char *const kGraphHash = "graphHash"; -const char *const kNodeHash = "nodeHash"; -const char *const kHash = "hash"; -const char *const kSessionId = "sessionId"; -const char *const kDeviceId = "deviceId"; -const char *const kJobId = "jobId"; -const char *const kGraphMemMaxSize = "graphMemMaxSize"; -const char *const kVarMemMaxSize = "varMemMaxSize"; -const char *const kVarMemLogicBase = "varMemLogicBase"; -const char *const kUseMaxMemSize = "useMaxMemSize"; -const char *const kMemResourceMap = "memResourceMap"; -const char *const kMemType = "memType"; -const char *const kTotalSize = "totalSize"; -const char *const kVarMemSize = "varMemSize"; -const char *const kVarResource = "varResource"; -const char *const kVarAddrMgrMap = "varAddrMgrMap"; -const char *const kName = "name"; -const char *const kAddress = "address"; -const char *const kOffset = "offset"; -const char *const kMemoryType = "memoryType"; -const char *const kTensorDesc = "tensorDesc"; -const char *const kDataType = "dataType"; -const char *const kShape = "shape"; -const char *const kLayout = "layout"; -const char *const kOriginDataType = "originDataType"; -const char *const kOriginShape = "originShape"; -const char *const kOriginLayout = "originLayout"; -const char *const kRealDimCnt = "realDimCnt"; -const char *const kCurVarTensorDescMap = "curVarTensorDescMap"; -const char *const kTransRoads = "transRoads"; -const char *const kTransRoad = "transRoad"; -const char *const kNodeType = "nodeType"; -const char *const kInputTensorDesc = "inputTensorDesc"; -const char *const kOutputTensorDesc = "outputTensorDesc"; -const char *const kChangedGraphId = "changedGraphId"; -const char *const kAllocatedGraphId = "allocatedGraphId"; -const char *const kGraphId = "graphId"; -const char *const kVarBroadcastInfo = "varBroadcastInfo"; -const char *const kBroadcastName = "broadcastName"; -const char *const kIdx = "idx"; -const char *const kInputOffset = "inputOffset"; -const char *const kInputSize = "inputSize"; -const char *const kOutputOffset = "outputOffset"; -const char *const kOutputSize = "outputSize"; -// Suffix of cache files -const char *const kBeforeVarManagerSuffix = "_before_build_var_manager.json"; -const char *const kAfterVarManagerSuffix = "_after_build_var_manager.json"; -const char *const kManifestSuffix = ".manifest"; -const char *const kOmSuffix = ".om"; -} // namespace - -namespace ge { -map ModelCacheHelper::graph_id_run_times_; -ModelCacheHelper::ModelCacheHelper(uint64_t session_id, uint32_t graph_id, ComputeGraphPtr &compute_graph) - : session_id_(session_id), - graph_id_(graph_id), - compute_graph_(compute_graph), - is_cache_path_valid_for_output(false) { - if (graph_id_run_times_.count(graph_id) == 0) { - graph_id_run_times_[graph_id] = 1; - } else { - graph_id_run_times_[graph_id] = graph_id_run_times_[graph_id] + 1; - } - for (const auto &node : compute_graph_->GetDirectNode()) { - bool is_variable = (node->GetType() == VARIABLE) || (node->GetType() == VARIABLEV2) || - (node->GetType() == VARHANDLEOP) || (node->GetType() == CONSTANTOP); - if (!is_variable) { - continue; - } - var_names_.insert(node->GetName()); - } - std::shared_ptr instance_ptr = ge::GELib::GetInstance(); - if (instance_ptr != nullptr && instance_ptr->IsIncreBuild()) { - std::string cache_path = instance_ptr->GetIncreBuildCachePath(); - GELOGD("Incre build path conf: %s", cache_path.c_str()); - string fake_file_path = cache_path + to_string(graph_id_) + kManifestSuffix; - if (CheckOutputPathValid(fake_file_path)) { - is_cache_path_valid_for_output = true; - } else { - GELOGW("Invalid cache path for output."); - } - std::string real_cache_path = RealPath(cache_path.c_str()); - if (real_cache_path.empty()) { - GELOGW("Invalid incre build cache path conf: %s", cache_path.c_str()); - return; - } - cache_path_ = real_cache_path + '/'; - GELOGD("Try to use incre build cache path: %s", cache_path_.c_str()); - } -} - -ModelCacheHelper::~ModelCacheHelper() { var_names_.clear(); } - -bool ModelCacheHelper::IsModelCacheHit() const { - CacheInfo cache_info; - if (GetCacheInfo(cache_info) != SUCCESS) { - GELOGI("Get cache info of graph id[%u] failed.", graph_id_); - return false; - } - // Check number of nodes and edges first. - if (cache_info.node_num != compute_graph_->GetDirectNodesSize()) { - GELOGI("Graph id[%u] cache miss: the node number of the graph does not match the cache info.", graph_id_); - return false; - } - size_t edge_num = 0; - for (const auto &node : compute_graph_->GetDirectNode()) { - for (const auto &anchor : node->GetAllInAnchors()) { - edge_num += anchor->GetPeerAnchors().size(); - } - } - if (cache_info.edge_num != edge_num) { - GELOGI("Graph id[%u] cache miss: the edge number of the graph does not match the cache info.", graph_id_); - return false; - } - size_t compute_graph_hash; - auto ret = GetComputeGraphHash(compute_graph_hash); - if (ret != SUCCESS || cache_info.graph_hash != compute_graph_hash) { - GELOGI("Graph id[%u] cache miss: the hash code of the graph does not match the cache info.", graph_id_); - return false; - } - if (!IsNodeHashSameAsCache(cache_info.nodes_hash)) { - GELOGI("Graph id[%u] cache miss: the hash code of node does not match the cache info.", graph_id_); - return false; - } - - string var_manager_cache = - to_string(graph_id_) + "_" + to_string(graph_id_run_times_[graph_id_]) + kBeforeVarManagerSuffix; - Json var_manager_json; - if (LoadJsonFromFile(var_manager_cache, var_manager_json) != SUCCESS) { - GELOGW("Fail to load json from cache file: %s", var_manager_cache.c_str()); - return false; - } - if (!IsVarManagerSameAsCache(var_manager_json)) { - GELOGI("Graph id[%u] cache miss: the VarManager does not match the cache info.", graph_id_); - return false; - } - GELOGI("Graph id[%u] cache hit.", graph_id_); - return true; -} - -Status ModelCacheHelper::RefreshComputeGraph(const ComputeGraphPtr &compute_graph) { - if (compute_graph->IsValid()) { - compute_graph_ = compute_graph; - var_names_.clear(); - for (const auto &node : compute_graph_->GetDirectNode()) { - bool is_variable = (node->GetType() == VARIABLE) || (node->GetType() == VARIABLEV2) || - (node->GetType() == VARHANDLEOP) || (node->GetType() == CONSTANTOP); - if (!is_variable) { - continue; - } - var_names_.insert(node->GetName()); - } - return SUCCESS; - } else { - GELOGW("Invalid compute graph."); - return FAILED; - } -} - -Status ModelCacheHelper::ClearCache(uint32_t graph_id) const { - if (!is_cache_path_valid_for_output) { - GELOGW("Invalid cache path."); - return SUCCESS; - } - string manifest_file = cache_path_ + to_string(graph_id) + kManifestSuffix; - string manifest_file_path = RealPath(manifest_file.c_str()); - int ret; - if (!manifest_file_path.empty()) { - ret = remove(manifest_file_path.c_str()); - // If remove file failed, print the warning log - if (ret != 0) { - GELOGW("Clear cache [%s] failed.", manifest_file_path.c_str()); - } - } - string before_var_manager_file = cache_path_ + to_string(graph_id) + kManifestSuffix; - string before_var_manager_file_path = RealPath(before_var_manager_file.c_str()); - if (!before_var_manager_file_path.empty()) { - ret = remove(before_var_manager_file_path.c_str()); - if (ret != 0) { - GELOGW("Clear cache [%s] failed.", before_var_manager_file_path.c_str()); - } - } - string after_var_manager_file = cache_path_ + to_string(graph_id) + kManifestSuffix; - string after_var_manager_file_path = RealPath(after_var_manager_file.c_str()); - if (!after_var_manager_file_path.empty()) { - ret = remove(after_var_manager_file_path.c_str()); - if (ret != 0) { - GELOGW("Clear cache [%s] failed.", after_var_manager_file_path.c_str()); - } - } - string om_file = cache_path_ + to_string(graph_id) + kManifestSuffix; - string om_file_path = RealPath(om_file.c_str()); - if (!om_file_path.empty()) { - ret = remove(om_file_path.c_str()); - if (ret != 0) { - GELOGW("Clear cache [%s] failed.", om_file_path.c_str()); - } - } - return SUCCESS; -} - -Status ModelCacheHelper::RecoverVarManagerFromCache() const { - string var_manager_cache = - to_string(graph_id_) + "_" + to_string(graph_id_run_times_[graph_id_]) + kAfterVarManagerSuffix; - Json var_manager_json; - if (LoadJsonFromFile(var_manager_cache, var_manager_json) != SUCCESS) { - GELOGW("Fail to load json from cache file: %s", var_manager_cache.c_str()); - return FAILED; - } - - Json mem_resource_json = move(var_manager_json[kMemResourceMap]); - auto ret = RecoverMemResource(mem_resource_json); - if (ret != SUCCESS) { - GELOGW("Recover VarManager from cache failed.[MemResource]"); - return FAILED; - } - Json var_resource_json = move(var_manager_json[kVarResource]); - ret = RecoverAllocatedGraphId(var_resource_json[kAllocatedGraphId]); - if (ret != SUCCESS) { - GELOGW("Recover VarManager from cache failed.[AllocatedGraphId]"); - return FAILED; - } - ret = RecoverChangedGraphId(var_resource_json[kChangedGraphId]); - if (ret != SUCCESS) { - GELOGW("Recover VarManager from cache failed.[ChangedGraphId]"); - return FAILED; - } - ret = RecoverBroadcastInfo(var_resource_json[kVarBroadcastInfo]); - if (ret != SUCCESS) { - GELOGW("Recover VarManager from cache failed.[VarBroadcastInfo]"); - return FAILED; - } - ret = RecoverVarAddrAndTensorDesc(var_resource_json[kVarAddrMgrMap]); - if (ret != SUCCESS) { - GELOGW("Recover VarManager from cache failed.[VarAddrMgrMap & CurVarTensorDesc]"); - return FAILED; - } - ret = RecoverTransRoads(var_resource_json[kTransRoads]); - if (ret != SUCCESS) { - GELOGW("Recover VarManager from cache failed.[TransRoads]"); - return FAILED; - } - GELOGI("Recover VarManager from cache[%s] success.", cache_path_.c_str()); - return SUCCESS; -} - -Status ModelCacheHelper::GetNodesNeedRecompile(ComputeGraphPtr &graph, vector &nodes) { - std::shared_ptr instance = ge::GELib::GetInstance(); - if (instance == nullptr || !instance->InitFlag()) { - GELOGW("RecompileNodes failed."); - return ge::GE_CLI_GE_NOT_INITIALIZED; - } - // Collect aicore ops for recompile - for (auto &node : graph->GetDirectNode()) { - if (node == nullptr) { - continue; - } - auto op_desc = node->GetOpDesc(); - if (op_desc == nullptr) { - continue; - } - // Get op kernel lib name - string kernel_lib_name = op_desc->GetOpKernelLibName(); - if (kernel_lib_name.empty()) { - // reset op kernel lib - (void)instance->DNNEngineManagerObj().GetDNNEngineName(node); - kernel_lib_name = op_desc->GetOpKernelLibName(); - if (kernel_lib_name.empty()) { - GELOGW("Get node:%s, type:%s kernel lib failed.", node->GetName().c_str(), op_desc->GetType().c_str()); - continue; - } - } - } - return SUCCESS; -} - -Status ModelCacheHelper::RecompileNodes(GeModelPtr &ge_model) { - std::shared_ptr instance = ge::GELib::GetInstance(); - if (instance == nullptr || !instance->InitFlag()) { - GELOGW("RecompileNodes failed."); - return ge::GE_CLI_GE_NOT_INITIALIZED; - } - // Get aicore ops kernel info store. - OpsKernelInfoStorePtr kernel_info = instance->OpsKernelManagerObj().GetOpsKernelInfoStore(kTbeKernelInfoStoreName); - if (kernel_info == nullptr) { - GELOGW("Get %s ops kernel info store failed", kTbeKernelInfoStoreName); - return INTERNAL_ERROR; - } - - auto compute_graph = GraphUtils::GetComputeGraph(ge_model->GetGraph()); - vector node_vec; - auto ret = GetNodesNeedRecompile(compute_graph, node_vec); - GE_CHK_BOOL_EXEC_WARN(ret == ge::SUCCESS, return ret, "Get nodes need recompiling failed"); - // Recompile aicore ops - ret = kernel_info->CompileOp(node_vec); - GE_CHK_BOOL_EXEC_WARN(ret == ge::SUCCESS, return ret, "Recompile op failed"); - const TBEKernelStore &tbekernel_store = ge_model->GetTBEKernelStore(); - TBEKernelStore tbe_kernel_store; - for (const ge::NodePtr &n : compute_graph->GetDirectNode()) { - auto node_op_desc = n->GetOpDesc(); - GE_IF_BOOL_EXEC(node_op_desc == nullptr, continue); - TBEKernelPtr tbe_kernel = node_op_desc->TryGetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, TBEKernelPtr()); - if (tbe_kernel == nullptr) { - // Load tbe kernel from tbe_kernel_store to op if op was not recompiled - auto op_desc = n->GetOpDesc(); - tbekernel_store.LoadTBEKernelBinToOpDesc(op_desc); - GELOGD("LoadOmModelFromCache: Load tbe kernel bin to op desc[%s].", op_desc->GetName().c_str()); - } - tbe_kernel = node_op_desc->TryGetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, TBEKernelPtr()); - GE_IF_BOOL_EXEC(tbe_kernel == nullptr, continue); - // Refresh tbe kernel in tbe_kernel_store - tbe_kernel_store.AddTBEKernel(tbe_kernel); - GELOGD("Add tbe kernel bin %s", tbe_kernel->GetName().c_str()); - } - GE_CHK_BOOL_EXEC_WARN(tbe_kernel_store.Build(), return FAILED, "TBE Kernels store build failed!"); - ge_model->SetTBEKernelStore(tbe_kernel_store); - return SUCCESS; -} - -Status ModelCacheHelper::GetNodesHash(map &hash_map) const { - vector nodes; - GraphUtils::TopologicalSortingByName(compute_graph_, nodes); - ModelSerializeImp model_serialize_imp; - std::hash node_hash; - for (const auto &node : nodes) { - if (node == nullptr) { - continue; - } - proto::OpDef op_def; - bool is_framework_op = (node->GetType() == FRAMEWORKOP); - int32_t framework_type = 0; - if (is_framework_op) { - AttrUtils::GetInt(node->GetOpDesc(), ge::ATTR_NAME_FRAMEWORK_FWK_TYPE, framework_type); - AttrUtils::SetInt(node->GetOpDesc(), ge::ATTR_NAME_FRAMEWORK_FWK_TYPE, 0); - } - bool ret = model_serialize_imp.SerializeNode(node, &op_def, is_framework_op); - op_def.set_id(0); // Id of op is not stable because of parallel parsing - // Clear weights attr in constant. - auto attr = op_def.mutable_attr(); - if (op_def.type() == CONSTANT || op_def.type() == CONSTANTOP) { - attr->erase(ATTR_NAME_WEIGHTS); - } - if (is_framework_op) { - AttrUtils::SetInt(node->GetOpDesc(), ge::ATTR_NAME_FRAMEWORK_FWK_TYPE, framework_type); - } - if (!ret) { - GELOGW("Fail to serialize node[%s].", node->GetName().c_str()); - return INTERNAL_ERROR; - } - string prototxt; - ret = google::protobuf::TextFormat::PrintToString(op_def, &prototxt); - if (!ret) { - GELOGW("Print OpDef to string failed."); - hash_map.clear(); - return INTERNAL_ERROR; - } - size_t hash_code = node_hash(prototxt); - hash_map[node->GetName()] = hash_code; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetComputeGraphHash(size_t &hash) const { - proto::GraphDef graph_proto; - ModelSerializeImp model_serialize_imp; - // The name of compute graph may be generated randomly, so replace it temporarily. - const string origin_name = compute_graph_->GetName(); - compute_graph_->SetName(kGraphName); - bool serialize_ret = model_serialize_imp.SerializeGraph(compute_graph_, &graph_proto); - graph_proto.clear_op(); - if (!serialize_ret) { - GELOGW("Serialize graph failed."); - hash = 0; - return INTERNAL_ERROR; - } - compute_graph_->SetName(origin_name); - // Generate proto text of GraphDef - string prototxt; - bool print_ret = google::protobuf::TextFormat::PrintToString(graph_proto, &prototxt); - if (!print_ret) { - GELOGW("Print GraphDef to string failed."); - hash = 0; - return INTERNAL_ERROR; - } - // Get the hash code of proto text - std::hash graph_hash; - hash = graph_hash(prototxt); - return SUCCESS; -} - -Status ModelCacheHelper::SaveJsonToFile(const string &file_name, const Json &json) const { - if (!is_cache_path_valid_for_output) { - GELOGW("Invalid cache path."); - return PARAM_INVALID; - } - // Check whether the manifest exists, if not, create it. - string real_path = RealPath(cache_path_.c_str()); - if (real_path.empty()) { - GELOGW("File path is invalid. please check cache path: %s", cache_path_.c_str()); - return FAILED; - } - const string path = cache_path_ + file_name; - const int FILE_AUTHORITY = 0600; - int fd = mmOpen2(path.c_str(), M_WRONLY | M_CREAT | O_TRUNC, FILE_AUTHORITY); - if (fd < 0) { - GELOGW("Fail to open the file:%s. errmsg:%s", path.c_str(), strerror(errno)); - return INTERNAL_ERROR; - } - if (mmClose(fd) != 0) { - GELOGW("Fail to close the file:%s. errmsg:%s", path.c_str(), strerror(errno)); - return INTERNAL_ERROR; - } - - // Write json into cache file - ofstream ofs; - ofs.open(path); - if (!ofs.is_open()) { - GELOGW("Fail to open the file: %s.", path.c_str()); - return INTERNAL_ERROR; - } - ofs << json << std::endl; - ofs.close(); - return SUCCESS; -} - -Status ModelCacheHelper::LoadJsonFromFile(const string &file_name, Json &json) const { - if (!json.is_null()) { - GELOGW("Input param json type should be null."); - return PARAM_INVALID; - } - string real_path = RealPath(cache_path_.c_str()); - if (real_path.empty()) { - GELOGW("File path is invalid. please check cache path: %s", cache_path_.c_str()); - return FAILED; - } - const string path = cache_path_ + file_name; - if (!CheckInputPathValid(path)) { - GELOGW("Invalid cache path for input:%s.", path.c_str()); - return FAILED; - } - string cache_real_path = RealPath(path.c_str()); - if (cache_real_path.empty()) { - GELOGI("File[%s] is not found.", path.c_str()); - return FAILED; - } - // Read json from cache file - ifstream ifs; - ifs.open(path); - if (!ifs.is_open()) { - GELOGW("Fail to open the file: %s.", path.c_str()); - return INTERNAL_ERROR; - } - try { - ifs >> json; - } catch (nlohmann::detail::parse_error e) { - GELOGW("Fail to load json from file, json throw an error:%s.", e.what()); - return INTERNAL_ERROR; - } catch (nlohmann::detail::invalid_iterator e) { - GELOGW("Fail to load json from file, json throw an error:%s.", e.what()); - return INTERNAL_ERROR; - } catch (nlohmann::detail::type_error e) { - GELOGW("Fail to load json from file, json throw an error:%s.", e.what()); - return INTERNAL_ERROR; - } catch (nlohmann::detail::out_of_range e) { - GELOGW("Fail to load json from file, json throw an error:%s.", e.what()); - return INTERNAL_ERROR; - } catch (nlohmann::detail::other_error e) { - GELOGW("Fail to load json from file, json throw an error:%s.", e.what()); - return INTERNAL_ERROR; - } - - if (!json.is_object()) { - GELOGW("Fail to load the json file: %s.", path.c_str()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::SaveCacheInfoToCache() const { - // Generate cache json - // example: {"edgeNum":6,"nodeNum":7,"graphCache":134714827475991356} - Json cache_json; - try { - cache_json[kNodeNum] = compute_graph_->GetDirectNodesSize(); - size_t edge_num = 0; - for (const auto &node : compute_graph_->GetDirectNode()) { - for (const auto &anchor : node->GetAllInAnchors()) { - edge_num += anchor->GetPeerAnchors().size(); - } - } - cache_json[kEdgeNum] = edge_num; - size_t hash = 0; - auto ret = GetComputeGraphHash(hash); - if (ret != SUCCESS) { - GELOGW("Error occur when generate graph hash code."); - return ret; - } - cache_json[kGraphHash] = hash; - Json nodes_hash_json; - ret = GetNodesHashMapJson(nodes_hash_json); - if (ret != SUCCESS) { - GELOGW("Error occur when generate nodes hash code."); - return ret; - } - cache_json[kNodeHash] = nodes_hash_json; - } catch (const std::exception &e) { - GELOGW("Fail to generate cache info json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - string cache_manifest = to_string(graph_id_) + "_" + to_string(graph_id_run_times_[graph_id_]) + kManifestSuffix; - - auto ret = SaveJsonToFile(cache_manifest, cache_json); - if (ret != SUCCESS) { - GELOGW("Fail to save cache info to json file, path: %s.", cache_path_.c_str()); - return ret; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetCacheInfo(CacheInfo &cache_info) const { - string cache_manifest = to_string(graph_id_) + "_" + to_string(graph_id_run_times_[graph_id_]) + kManifestSuffix; - Json cache_json; - if (LoadJsonFromFile(cache_manifest, cache_json) != SUCCESS) { - GELOGW("Fail to load json from cache file: %s", cache_manifest.c_str()); - return INTERNAL_ERROR; - } - if (!cache_json.is_object()) { - GELOGW("Manifest should be a json object"); - return INTERNAL_ERROR; - } - try { - cache_info.node_num = cache_json[kNodeNum]; - cache_info.edge_num = cache_json[kEdgeNum]; - cache_info.graph_hash = cache_json[kGraphHash]; - Json nodes_hash_json = cache_json[kNodeHash]; - if (!(nodes_hash_json.is_null() || nodes_hash_json.is_array())) { - GELOGW("Nodes hash in cache should be null or array."); - return FAILED; - } - for (const auto &iter : nodes_hash_json) { - cache_info.nodes_hash[iter[kName].get()] = iter[kHash].get(); - } - } catch (const std::exception &e) { - GELOGW("Fail to get info from json file. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -bool ModelCacheHelper::IsAllocatedGraphIdSameAsCache(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return false; - } - // Compare allocated graph id info between json and VarManager - std::map allocated_graph_id; - auto ret = ParseAllocatedGraphIdFromJson(json, allocated_graph_id); - if (ret != SUCCESS) { - GELOGW("Fail to parse AllocatedGraphId from Json."); - return false; - } - for (const auto &iter : allocated_graph_id) { - uint32_t graph_id = 0; - ret = VarManager::Instance(session_id_)->GetAllocatedGraphId(iter.first, graph_id); - if (ret != SUCCESS) { - GELOGW("Fail to find allocated graph id of var[%s].", iter.first.c_str()); - return false; - } - if (graph_id != iter.second) { - GELOGW("The allocated graph id of variable[%s] in cache is different from VarManager.", iter.first.c_str()); - return false; - } - } - return true; -} - -bool ModelCacheHelper::IsNodeHashSameAsCache(const map &hash_map) const { - map cur_hash_map; - GetNodesHash(cur_hash_map); - if (hash_map.size() != cur_hash_map.size()) { - GELOGI("The number of hash code is different from cache info."); - return false; - } - for (const auto &iter : cur_hash_map) { - if (hash_map.count(iter.first) == 0) { - GELOGI("Node[%s] is not found in cache info.", iter.first.c_str()); - return false; - } - if (hash_map.at(iter.first) != iter.second) { - GELOGI("The hash code of node[%s] is different from cache info.", iter.first.c_str()); - return false; - } - } - return true; -} - -bool ModelCacheHelper::IsMemResourceSameAsCache(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return false; - } - // Compare var mem size info between json and VarManager - std::map var_mem_size; - auto ret = ParseMemResourceFromJson(json, var_mem_size); - if (ret != SUCCESS) { - GELOGW("Fail to parse MemResource from Json."); - return false; - } - for (const auto &iter : var_mem_size) { - int64_t mem_size = VarManager::Instance(session_id_)->GetVarMemSize(iter.first); - if (mem_size != iter.second) { - GELOGW("The var mem size of memory_type[%u] in cache is different from VarManager.", iter.first); - return false; - } - } - return true; -} - -bool ModelCacheHelper::IsChangedGraphIdSameAsCache(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return false; - } - // Compare variable changed graph id info between json and VarManager - std::map changed_graph_id; - auto ret = ParseChangedGraphIdFromJson(json, changed_graph_id); - if (ret != SUCCESS) { - GELOGW("Fail to parse ChangedGraphId from Json."); - return false; - } - for (const auto &iter : changed_graph_id) { - uint32_t graph_id = 0; - ret = VarManager::Instance(session_id_)->GetChangedGraphId(iter.first, graph_id); - if (ret != SUCCESS) { - GELOGW("Fail to find changed graph id of var[%s].", iter.first.c_str()); - return false; - } - if (graph_id != iter.second) { - GELOGW("The changed graph id of variable[%s] in cache is different from VarManager.", iter.first.c_str()); - return false; - } - } - return true; -} - -bool ModelCacheHelper::IsCurVarTensorDescSameAsCache(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return false; - } - // Compare variable tensor desc info between json and VarManager - std::unordered_map cur_var_tensor_desc; - auto ret = ParseCurVarTensorDescMapFromJson(json, cur_var_tensor_desc); - if (ret != SUCCESS) { - GELOGW("Fail to parse CurVarTensorDesc from Json."); - return false; - } - for (const auto &iter : cur_var_tensor_desc) { - GeTensorDesc tensor_desc; - ret = VarManager::Instance(session_id_)->GetCurVarDesc(iter.first, tensor_desc); - if (ret != SUCCESS) { - GELOGW("Fail to find tensor desc of var[%s].", iter.first.c_str()); - return false; - } - uint32_t l_real_dim_cnt = 0; - uint32_t r_real_dim_cnt = 0; - TensorUtils::GetRealDimCnt(tensor_desc, l_real_dim_cnt); - TensorUtils::GetRealDimCnt(iter.second, r_real_dim_cnt); - if ((tensor_desc.GetDataType() != iter.second.GetDataType()) || - (tensor_desc.GetOriginDataType() != iter.second.GetOriginDataType()) || - (tensor_desc.GetFormat() != iter.second.GetFormat()) || - (tensor_desc.GetOriginFormat() != iter.second.GetOriginFormat()) || - (tensor_desc.GetShape().ToString() != iter.second.GetShape().ToString()) || - (tensor_desc.GetOriginShape().ToString() != iter.second.GetOriginShape().ToString()) || - (l_real_dim_cnt != r_real_dim_cnt)) { - GELOGW("The var tensor desc of variable[%s] in cache is different from VarManager.", iter.first.c_str()); - return false; - } - } - return true; -} - -bool ModelCacheHelper::IsVarAddrMgrMapSameAsCache(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return false; - } - // Compare variable address info between json and VarManager - std::vector> var_addr_mgr_vector; - std::set var_offset_set; - auto ret = ParseVarAddrMgrMapFromJson(json, var_addr_mgr_vector, var_offset_set); - if (ret != SUCCESS) { - GELOGW("Fail to parse VarAddrMgrMap from Json."); - return false; - } - for (const auto &iter : var_addr_mgr_vector) { - uint8_t *dev_ptr = nullptr; - rtMemType_t memory_type; - ret = VarManager::Instance(session_id_)->GetVarAddr(iter.first, iter.second.tensor_desc, &dev_ptr, memory_type); - if (ret != SUCCESS) { - GELOGW("Fail to find tensor desc of var[%s].", iter.first.c_str()); - return false; - } - // Compare memory type and logic address - if (iter.second.memory_type != memory_type || iter.second.address != dev_ptr) { - GELOGW("The VarAddrMgr of variable[%s] in cache is different from VarManager.", iter.first.c_str()); - return false; - } - } - return true; -} - -bool ModelCacheHelper::IsBroadcastInfoSameAsCache(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return false; - } - // Compare broadcast info between json and VarManager - std::unordered_map var_broadcast_info; - auto ret = ParseBroadcastInfoFromJson(json, var_broadcast_info); - if (ret != SUCCESS) { - GELOGW("Fail to parse BroadcastInfo from Json."); - return false; - } - for (const auto &iter : var_broadcast_info) { - VarBroadCastInfo broadcast_info; - if (VarManager::Instance(session_id_)->GetBroadCastInfo(graph_id_, iter.first, broadcast_info) != SUCCESS) { - GELOGW("Fail to find broadcast info of var[%s].", iter.first.c_str()); - return false; - } - if (iter.second.var_name != broadcast_info.var_name || iter.second.idx != broadcast_info.idx || - iter.second.input_size != broadcast_info.input_size || - iter.second.input_offset != broadcast_info.input_offset || - iter.second.output_size != broadcast_info.output_size || - iter.second.output_offset != broadcast_info.output_offset) { - GELOGW("The BroadcastInfo of variable[%s] in cache is different from VarManager.", iter.first.c_str()); - return false; - } - } - return true; -} - -bool ModelCacheHelper::IsTransRoadsSameAsCache(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return false; - } - // Compare trans road between json and VarManager - std::unordered_map> trans_roads; - auto ret = ParseTransRoadsFromJson(json, trans_roads); - if (ret != SUCCESS) { - GELOGW("Fail to parse TransRoads from Json."); - return false; - } - for (const auto &iter : trans_roads) { - VarTransRoad *trans_road; - trans_road = VarManager::Instance(session_id_)->GetTransRoad(iter.first); - if (trans_road == nullptr) { - GELOGW("Fail to find trans road of var[%s].", iter.first.c_str()); - return false; - } - if (trans_road->size() != iter.second.size()) { - GELOGW("The TransRoad of variable[%s] in cache is different from VarManager.", iter.first.c_str()); - return false; - } - // Compare every trans node in trans road. - for (size_t idx = 0; idx < trans_road->size(); idx += 1) { - if (!(trans_road->at(idx).node_type == iter.second.at(idx).node_type && - trans_road->at(idx).input == iter.second.at(idx).input && - trans_road->at(idx).output == iter.second.at(idx).output)) { - GELOGW("The TransRoad of variable[%s] in cache is different from VarManager.", iter.first.c_str()); - return false; - } - } - } - return true; -} - -bool ModelCacheHelper::IsVarManagerParamSameAsCache(Json &json) const { - if (!json.is_object()) { - GELOGW("Input param json type should be object."); - return false; - } - try { - if (json[kSessionId].get() != session_id_) { - GELOGW("Check VarManager cache failed.[sessionId]"); - return false; - } - if (json[kDeviceId].get() != VarManager::Instance(session_id_)->DeviceId()) { - GELOGW("Check VarManager cache failed.[deviceId]"); - return false; - } - if (json[kJobId].get() != VarManager::Instance(session_id_)->JobId()) { - GELOGW("Check VarManager cache failed.[jobId]"); - return false; - } - if (json[kGraphMemMaxSize].get() != VarManager::Instance(session_id_)->GetGraphMemoryMaxSize()) { - GELOGW("Check VarManager cache failed.[graphMemMaxSize]"); - return false; - } - if (json[kVarMemMaxSize].get() != VarManager::Instance(session_id_)->GetVarMemMaxSize()) { - GELOGW("Check VarManager cache failed.[varMemMaxSize]"); - return false; - } - if (json[kVarMemLogicBase].get() != VarManager::Instance(session_id_)->GetVarMemLogicBase()) { - GELOGW("Check VarManager cache failed.[varMemLogicBase]"); - return false; - } - if (json[kUseMaxMemSize].get() != VarManager::Instance(session_id_)->GetUseMaxMemorySize()) { - GELOGW("Check VarManager cache failed.[useMaxMemSize]"); - return false; - } - } catch (const std::exception &e) { - GELOGW("Fail to check VarManager json. Error message: %s", e.what()); - return false; - } - return true; -} - -bool ModelCacheHelper::IsVarManagerSameAsCache(Json &json) const { - if (!json.is_object()) { - GELOGW("Input param json type should be object."); - return false; - } - try { - if (!IsVarManagerParamSameAsCache(json)) { - GELOGW("Check VarManager cache failed.[Param]"); - return false; - } - Json mem_resource_json = move(json[kMemResourceMap]); - auto ret = IsMemResourceSameAsCache(mem_resource_json); - if (!ret) { - GELOGW("Check VarManager cache failed.[MemResource]"); - return false; - } - Json var_resource_json = move(json[kVarResource]); - ret = IsAllocatedGraphIdSameAsCache(var_resource_json[kAllocatedGraphId]); - if (!ret) { - GELOGW("Check VarManager cache failed.[AllocatedGraphId]"); - return false; - } - ret = IsChangedGraphIdSameAsCache(var_resource_json[kChangedGraphId]); - if (!ret) { - GELOGW("Check VarManager cache failed.[ChangedGraphId]"); - return false; - } - ret = IsBroadcastInfoSameAsCache(var_resource_json[kVarBroadcastInfo]); - if (!ret) { - GELOGW("Check VarManager cache failed.[VarBroadcastInfo]"); - return false; - } - ret = IsCurVarTensorDescSameAsCache(var_resource_json[kCurVarTensorDescMap]); - if (!ret) { - GELOGW("Check VarManager cache failed.[CurVarTensorDesc]"); - return false; - } - ret = IsVarAddrMgrMapSameAsCache(var_resource_json[kVarAddrMgrMap]); - if (!ret) { - GELOGW("Check VarManager cache failed.[VarAddrMgrMap]"); - return false; - } - ret = IsTransRoadsSameAsCache(var_resource_json[kTransRoads]); - if (!ret) { - GELOGW("Check VarManager cache failed.[TransRoads]"); - return false; - } - } catch (const std::exception &e) { - GELOGW("Fail to check VarManager json. Error message: %s", e.what()); - return false; - } - return true; -} - -Status ModelCacheHelper::RecoverMemResource(const Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - std::map var_mem_size; - auto ret = ParseMemResourceFromJson(json, var_mem_size); - if (ret != SUCCESS) { - GELOGW("Fail to parse MemResource from Json."); - return ret; - } - for (const auto &iter : var_mem_size) { - ret = VarManager::Instance(session_id_)->UpdateVarMemSize(iter.first, iter.second); - if (ret != SUCCESS) { - GELOGW("Fail to recover var mem size."); - return ret; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::RecoverAllocatedGraphId(const Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - std::map allocated_graph_id; - auto ret = ParseAllocatedGraphIdFromJson(json, allocated_graph_id); - if (ret != SUCCESS) { - GELOGW("Fail to parse AllocatedGraphId from Json."); - return ret; - } - for (const auto &iter : allocated_graph_id) { - ret = VarManager::Instance(session_id_)->SetAllocatedGraphId(iter.first, iter.second); - if (ret != SUCCESS) { - GELOGW("Fail to recover allocated graph id."); - return ret; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::RecoverChangedGraphId(const Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - std::map changed_graph_id; - auto ret = ParseChangedGraphIdFromJson(json, changed_graph_id); - if (ret != SUCCESS) { - GELOGW("Fail to parse AllocatedGraphId from Json."); - return ret; - } - for (const auto &iter : changed_graph_id) { - ret = VarManager::Instance(session_id_)->SetChangedGraphId(iter.first, iter.second); - if (ret != SUCCESS) { - GELOGW("Fail to recover changed graph id."); - return ret; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::RecoverVarAddrAndTensorDesc(const Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - std::vector> var_addr_mgr_vector; - std::set var_offset_set; - auto ret = ParseVarAddrMgrMapFromJson(json, var_addr_mgr_vector, var_offset_set); - if (ret != SUCCESS) { - GELOGW("Fail to parse VarAddrMgrMap from Json."); - return ret; - } - for (const auto &iter : var_addr_mgr_vector) { - const VarAddrMgr &tensor_addr_mgr = iter.second; - const bool var_exist = VarManager::Instance(session_id_)->IsVarExist(iter.first, tensor_addr_mgr.tensor_desc); - // SaveVarVddr if var does not exist, the logic address will be recorded by VarManager - if (!var_exist) { - auto logic_address = reinterpret_cast(reinterpret_cast(tensor_addr_mgr.address)); - auto offset = (tensor_addr_mgr.offset); - // Check logic address and offset - if (logic_address - offset != VarManager::Instance(session_id_)->GetVarMemLogicBase()) { - GELOGW("Check logic_address[%lu] and offset [%lu] of %s failed, var mem logic base is %lu, abandon", - logic_address, offset, iter.first.c_str(), VarManager::Instance(session_id_)->GetVarMemLogicBase()); - return PARAM_INVALID; - } - // Offset is needed by SaveVarVddr instead of logic address - ret = VarManager::Instance(session_id_)->SaveVarAddr(iter.first, tensor_addr_mgr.tensor_desc, - reinterpret_cast(reinterpret_cast(offset)), - tensor_addr_mgr.memory_type); - if (ret != SUCCESS) { - GELOGW("Fail to recover VarAddr or TensorDesc of var[%s].", iter.first.c_str()); - return ret; - } - } - // SetVarAddr to update cur_var_tensor_desc_map_ - ret = VarManager::Instance(session_id_) - ->SetVarAddr(iter.first, tensor_addr_mgr.tensor_desc, tensor_addr_mgr.address, tensor_addr_mgr.memory_type); - if (ret != SUCCESS) { - GELOGW("Fail to recover VarAddr or TensorDesc desc of var[%s].", iter.first.c_str()); - return ret; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::RecoverBroadcastInfo(const Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - std::unordered_map var_broadcast_info; - auto ret = ParseBroadcastInfoFromJson(json, var_broadcast_info); - if (ret != SUCCESS) { - GELOGW("Fail to parse BroadcastInfo from Json."); - return ret; - } - for (const auto &iter : var_broadcast_info) { - VarBroadCastInfo broadcast_info; - ret = VarManager::Instance(session_id_)->SaveBroadCastInfo(graph_id_, iter.second); - if (ret != SUCCESS) { - GELOGW("Fail to recover broadcast info of var[%s].", iter.first.c_str()); - return ret; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::RecoverTransRoads(const Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - std::unordered_map> trans_roads; - auto ret = ParseTransRoadsFromJson(json, trans_roads); - if (ret != SUCCESS) { - GELOGW("Fail to parse TransRoads from Json."); - return ret; - } - for (const auto &iter : trans_roads) { - ret = VarManager::Instance(session_id_)->SetTransRoad(iter.first, iter.second); - if (ret != SUCCESS) { - GELOGW("Fail to find trans road of var[%s].", iter.first.c_str()); - return ret; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::TensorDescToJson(const GeTensorDesc &ge_tensor_desc, Json &json) { - if (!(json.is_null() || json.is_object())) { - GELOGW("Input param json type should be null or object."); - return PARAM_INVALID; - } - try { - json[kDataType] = static_cast(ge_tensor_desc.GetDataType()); - json[kOriginDataType] = static_cast(ge_tensor_desc.GetOriginDataType()); - json[kLayout] = static_cast(ge_tensor_desc.GetFormat()); - json[kOriginLayout] = static_cast(ge_tensor_desc.GetOriginFormat()); - json[kShape] = ge_tensor_desc.GetShape().GetDims(); - json[kOriginShape] = ge_tensor_desc.GetOriginShape().GetDims(); - uint32_t real_dim_cnt = 0; - (void)TensorUtils::GetRealDimCnt(ge_tensor_desc, real_dim_cnt); // [No need to check value] - json[kRealDimCnt] = real_dim_cnt; - } catch (const std::exception &e) { - GELOGW("Fail to trans GeTensorDesc to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::JsonToTensorDesc(const Json &json, ge::GeTensorDesc &ge_tensor_desc) { - if (!json.is_object()) { - GELOGW("Input param json type should be object."); - return PARAM_INVALID; - } - try { - ge_tensor_desc.SetDataType(static_cast(json[kDataType].get())); - ge_tensor_desc.SetOriginDataType(static_cast(json[kOriginDataType].get())); - ge_tensor_desc.SetFormat(static_cast(json[kLayout].get())); - ge_tensor_desc.SetOriginFormat(static_cast(json[kOriginLayout].get())); - GeShape shape(json[kShape].get>()); - ge_tensor_desc.SetShape(shape); - GeShape origin_shape(json[kOriginShape].get>()); - ge_tensor_desc.SetOriginShape(origin_shape); - auto real_dim_cnt = json[kRealDimCnt].get(); - (void)TensorUtils::SetRealDimCnt(ge_tensor_desc, real_dim_cnt); // [No need to check value] - } catch (const std::exception &e) { - GELOGW("Fail to trans Json to GeTensorDesc. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetNodesHashMapJson(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - map hash_map; - GetNodesHash(hash_map); - for (const auto &iter : hash_map) { - Json node_hash_json; - try { - node_hash_json[kName] = iter.first; - node_hash_json[kHash] = iter.second; - json.emplace_back(move(node_hash_json)); - } catch (const std::exception &e) { - GELOGW("Fail to trans node cache to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::GetMemResourceMap(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - const auto total_size = VarManager::Instance(session_id_)->GetVarMemMaxSize(); - const auto var_mem_size = VarManager::Instance(session_id_)->GetVarMemSize(RT_MEMORY_HBM); - Json mem_resource_json; - try { - mem_resource_json[kMemType] = RT_MEMORY_HBM; - mem_resource_json[kTotalSize] = total_size; - mem_resource_json[kVarMemSize] = var_mem_size; - json.emplace_back(move(mem_resource_json)); - } catch (const std::exception &e) { - GELOGW("Fail to trans MemResourceMap to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetVarAddrMgrMapJson(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - std::unordered_map var_addr_mgr_map; - VarManager::Instance(session_id_)->GetAllVarAddrMgr(var_addr_mgr_map); - try { - for (const auto &iter : var_addr_mgr_map) { - Json var_addr_json; - string name; - GetVarNameFromVarKey(iter.first, iter.second.tensor_desc, name); - var_addr_json[kName] = name; - var_addr_json[kAddress] = static_cast(reinterpret_cast(iter.second.address)); - var_addr_json[kMemoryType] = iter.second.memory_type; - var_addr_json[kOffset] = iter.second.offset; - - // Copy tensor desc to json. - Json tensor_desc_json; - auto ret = TensorDescToJson(iter.second.tensor_desc, tensor_desc_json); - if (ret != SUCCESS) { - GELOGW("Fail to trans tensor desc to json."); - return INTERNAL_ERROR; - } - var_addr_json[kTensorDesc] = move(tensor_desc_json); - - json.emplace_back(move(var_addr_json)); - } - } catch (const std::exception &e) { - GELOGW("Fail to trans VarAddrMgrMap to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetCurVarTensorDescMapJson(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - try { - for (const auto &name : var_names_) { - Json cur_tensor_desc_json; - GeTensorDesc tensor_desc; - auto ret = VarManager::Instance(session_id_)->GetCurVarDesc(name, tensor_desc); - if (ret != SUCCESS) { - GELOGI("Get variable[%s] current tensor desc failed. It will be skipped.", name.c_str()); - continue; - } - cur_tensor_desc_json[kName] = name; - - Json tensor_desc_json; - ret = TensorDescToJson(tensor_desc, tensor_desc_json); - if (ret != SUCCESS) { - GELOGW("Fail to trans tensor desc to json."); - return INTERNAL_ERROR; - } - cur_tensor_desc_json[kTensorDesc] = move(tensor_desc_json); - json.emplace_back(move(cur_tensor_desc_json)); - } - } catch (const std::exception &e) { - GELOGW("Fail to trans CurVarTensorDescMap to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetTransRoadsJson(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - try { - for (const auto &name : var_names_) { - auto trans_road = VarManager::Instance(session_id_)->GetTransRoad(name); - if (trans_road == nullptr) { - continue; - } - // Json object, variable name and trans road - Json trans_road_map_json; - trans_road_map_json[kName] = name; - - Json trans_road_json; - Status ret; - // Add nodes' info to json - for (const auto &trans_node_info : *trans_road) { - Json trans_node_info_json; - trans_node_info_json[kNodeType] = trans_node_info.node_type; - Json input_tensor_desc_json; - ret = TensorDescToJson(trans_node_info.input, input_tensor_desc_json); - if (ret != SUCCESS) { - GELOGW("Fail to trans tensor desc to json."); - return INTERNAL_ERROR; - } - trans_node_info_json[kInputTensorDesc] = move(input_tensor_desc_json); - Json output_tensor_desc_json; - ret = TensorDescToJson(trans_node_info.output, output_tensor_desc_json); - if (ret != SUCCESS) { - GELOGW("Fail to trans tensor desc to json."); - return INTERNAL_ERROR; - } - trans_node_info_json[kOutputTensorDesc] = move(output_tensor_desc_json); - trans_road_json.emplace_back(move(trans_node_info_json)); - } - trans_road_map_json[kTransRoad] = move(trans_road_json); - json.emplace_back(move(trans_road_map_json)); - } - } catch (const std::exception &e) { - GELOGW("Fail to trans VarToTransRoad to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetChangedGraphIdJson(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - for (const auto &name : var_names_) { - uint32_t changed_graph_id = 0; - Status ret = VarManager::Instance(session_id_)->GetChangedGraphId(name, changed_graph_id); - if (ret != SUCCESS) { - continue; - } - Json name_and_changed_graph_id; - try { - name_and_changed_graph_id[kName] = name; - name_and_changed_graph_id[kGraphId] = changed_graph_id; - json.emplace_back(move(name_and_changed_graph_id)); - } catch (const std::exception &e) { - GELOGW("Fail to trans ChangedGraphId to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::GetAllocatedGraphIdJson(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - for (const auto &name : var_names_) { - uint32_t allocated_graph_id = 0; - Status ret = VarManager::Instance(session_id_)->GetAllocatedGraphId(name, allocated_graph_id); - if (ret != SUCCESS) { - continue; - } - Json name_and_allocated_graph_id; - try { - name_and_allocated_graph_id[kName] = name; - name_and_allocated_graph_id[kGraphId] = allocated_graph_id; - json.emplace_back(move(name_and_allocated_graph_id)); - } catch (const std::exception &e) { - GELOGW("Fail to trans AllocatedGraphId to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::GetBroadcastInfoJson(Json &json) const { - if (!(json.is_null() || json.is_array())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - for (const auto &name : var_names_) { - VarBroadCastInfo var_broadcast_info; - Status ret = VarManager::Instance(session_id_)->GetBroadCastInfo(graph_id_, name, var_broadcast_info); - if (ret != SUCCESS) { - continue; - } - Json var_broadcast_info_json; - try { - var_broadcast_info_json[kName] = name; - var_broadcast_info_json[kBroadcastName] = var_broadcast_info.broadcast_name; - var_broadcast_info_json[kIdx] = var_broadcast_info.idx; - var_broadcast_info_json[kInputOffset] = var_broadcast_info.input_offset; - var_broadcast_info_json[kInputSize] = var_broadcast_info.input_size; - var_broadcast_info_json[kOutputOffset] = var_broadcast_info.output_offset; - var_broadcast_info_json[kOutputSize] = var_broadcast_info.output_size; - json.emplace_back(move(var_broadcast_info_json)); - } catch (const std::exception &e) { - GELOGW("Fail to trans VarBroadcastInfo to json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::GetVarResourceJson(Json &json) const { - if (!(json.is_null() || json.is_object())) { - GELOGW("Input param json type should be null or object."); - return PARAM_INVALID; - } - Json var_addr_mgr_map_json; - Status ret = GetVarAddrMgrMapJson(var_addr_mgr_map_json); - if (ret != SUCCESS) { - GELOGW("GetVarAddrMgrMapJson failed."); - return INTERNAL_ERROR; - } - - Json cur_var_tensor_desc_map_json; - ret = GetCurVarTensorDescMapJson(cur_var_tensor_desc_map_json); - if (ret != SUCCESS) { - GELOGW("GetCurVarTensorDescMapJson failed."); - return INTERNAL_ERROR; - } - - Json trans_roads_json; - ret = GetTransRoadsJson(trans_roads_json); - if (ret != SUCCESS) { - GELOGW("GetTransRoadsJson failed."); - return INTERNAL_ERROR; - } - - Json changed_graph_id_json; - ret = GetChangedGraphIdJson(changed_graph_id_json); - if (ret != SUCCESS) { - GELOGW("GetChangedGraphIdJson failed."); - return INTERNAL_ERROR; - } - - Json allocated_graph_id_json; - ret = GetAllocatedGraphIdJson(allocated_graph_id_json); - if (ret != SUCCESS) { - GELOGW("GetAllocatedGraphIdJson failed."); - return INTERNAL_ERROR; - } - - Json var_broadcast_info_json; - ret = GetBroadcastInfoJson(var_broadcast_info_json); - if (ret != SUCCESS) { - GELOGW("GetBroadcastInfoJson failed."); - return INTERNAL_ERROR; - } - - try { - json[kVarAddrMgrMap] = move(var_addr_mgr_map_json); - json[kCurVarTensorDescMap] = move(cur_var_tensor_desc_map_json); - json[kTransRoads] = move(trans_roads_json); - json[kChangedGraphId] = move(changed_graph_id_json); - json[kAllocatedGraphId] = move(allocated_graph_id_json); - json[kVarBroadcastInfo] = move(var_broadcast_info_json); - } catch (const exception &e) { - GELOGW("Fail to generate VarResource json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetVarManagerJson(Json &json) const { - if (!(json.is_null() || json.is_object())) { - GELOGW("Input param json type should be null or object."); - return PARAM_INVALID; - } - - Json mem_resource_map_json; - auto ret = GetMemResourceMap(mem_resource_map_json); - if (ret != SUCCESS) { - GELOGW("GetMemResourceMap failed."); - return INTERNAL_ERROR; - } - - Json var_resource_json; - ret = GetVarResourceJson(var_resource_json); - if (ret != SUCCESS) { - GELOGW("GetVarResourceJson failed."); - return INTERNAL_ERROR; - } - - try { - json[kSessionId] = session_id_; - json[kDeviceId] = VarManager::Instance(session_id_)->DeviceId(); - json[kJobId] = VarManager::Instance(session_id_)->JobId(); - json[kGraphMemMaxSize] = VarManager::Instance(session_id_)->GetGraphMemoryMaxSize(); - json[kVarMemMaxSize] = VarManager::Instance(session_id_)->GetVarMemMaxSize(); - json[kVarMemLogicBase] = VarManager::Instance(session_id_)->GetVarMemLogicBase(); - json[kUseMaxMemSize] = VarManager::Instance(session_id_)->GetUseMaxMemorySize(); - json[kMemResourceMap] = move(mem_resource_map_json); - json[kVarResource] = move(var_resource_json); - } catch (const exception &e) { - GELOGW("Fail to generate VarManager json. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::SaveVarManagerToCache(bool before_build) const { - if (!is_cache_path_valid_for_output) { - GELOGW("Invalid cache path."); - return FAILED; - } - Json var_manager_json; - auto ret = GetVarManagerJson(var_manager_json); - if (ret != SUCCESS) { - GELOGW("Fail to generate VarManager json."); - return FAILED; - } - string var_manager_path = to_string(graph_id_) + "_" + to_string(graph_id_run_times_[graph_id_]) + - (before_build ? kBeforeVarManagerSuffix : kAfterVarManagerSuffix); - ret = SaveJsonToFile(var_manager_path, var_manager_json); - if (ret != SUCCESS) { - GELOGW("Fail to save VarManager info to json file, path: %s.", cache_path_.c_str()); - return ret; - } - return SUCCESS; -} - -Status ModelCacheHelper::SaveOmModelToCache(const GeModelPtr &ge_model) const { - if (!is_cache_path_valid_for_output) { - GELOGW("Invalid cache path."); - return FAILED; - } - string om_path = RealPath(cache_path_.c_str()); - if (om_path.empty()) { - GELOGW("file path is invalid. please check path om: %s", cache_path_.c_str()); - return FAILED; - } - string cache_om_path = cache_path_; - cache_om_path += (to_string(graph_id_) + "_" + to_string(graph_id_run_times_[graph_id_]) + kOmSuffix); - GELOGI("SaveOmModelToCache: start to save om model : %s", cache_om_path.c_str()); - ModelHelper model_helper; - SaveParam save_param; - ModelBufferData model; - Status ret = model_helper.SaveToOmModel(ge_model, save_param, cache_om_path, model); - if (ret != SUCCESS) { - GELOGW("SaveOmModelToCache: save mode failed. ret = %u", ret); - return ret; - } - return SUCCESS; -} - -Status ModelCacheHelper::ParseMemResourceFromJson(const Json &json, map &mem_resource) { - if (!(json.is_array() || json.is_null())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - mem_resource.clear(); - for (const Json &mem_resource_json : json) { - try { - rtMemType_t mem_type = mem_resource_json[kMemType].get(); - uint64_t var_mem_size = mem_resource_json[kVarMemSize].get(); - mem_resource[mem_type] = var_mem_size; - } catch (const exception &e) { - GELOGW("Fail to trans Json to MemResource. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::ParseVarAddrMgrMapFromJson( - const Json &json, std::vector> &var_addr_mgr_vector, - std::set &var_offset_set) { - if (!(json.is_array() || json.is_null())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - var_addr_mgr_vector.clear(); - var_offset_set.clear(); - for (const Json &var_addr_json : json) { - VarAddrMgr var_addr_mgr; - try { - auto logic_address = var_addr_json[kAddress].get(); - auto address = reinterpret_cast(reinterpret_cast(logic_address)); - var_addr_mgr.address = address; - var_addr_mgr.offset = var_addr_json[kOffset].get(); - var_addr_mgr.memory_type = var_addr_json[kMemoryType].get(); - auto ret = JsonToTensorDesc(var_addr_json[kTensorDesc], var_addr_mgr.tensor_desc); - if (ret != SUCCESS) { - GELOGW("Fail to trans json to tensor desc."); - return ret; - } - var_addr_mgr_vector.emplace_back(var_addr_json[kName].get(), move(var_addr_mgr)); - var_offset_set.insert(logic_address); - } catch (const exception &e) { - GELOGW("Fail to trans Json to VarAddrMgr. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::ParseCurVarTensorDescMapFromJson( - const Json &json, std::unordered_map &cur_var_tensor_desc_map) { - if (!(json.is_array() || json.is_null())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - cur_var_tensor_desc_map.clear(); - for (const Json &tensor_desc_json : json) { - GeTensorDesc tensor_desc; - try { - auto ret = JsonToTensorDesc(tensor_desc_json[kTensorDesc], tensor_desc); - if (ret != SUCCESS) { - GELOGW("Fail to trans json to tensor desc."); - return ret; - } - cur_var_tensor_desc_map[tensor_desc_json[kName].get()] = move(tensor_desc); - } catch (const exception &e) { - GELOGW("Fail to trans Json to VarAddrMgr. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::ParseTransRoadsFromJson( - const Json &json, std::unordered_map> &trans_roads) { - if (!(json.is_array() || json.is_null())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - trans_roads.clear(); - try { - for (const Json &name_trans_road_json : json) { - const Json &trans_road_json = name_trans_road_json[kTransRoad]; - if (!(trans_road_json.is_array() || trans_road_json.is_null())) { - GELOGW("%s json type should be null or object.", kTransRoad); - return PARAM_INVALID; - } - vector trans_road; - for (const Json &trans_node_json : trans_road_json) { - TransNodeInfo trans_node_info; - trans_node_info.node_type = trans_node_json[kNodeType]; - GeTensorDesc input_tensor_desc; - auto ret = JsonToTensorDesc(trans_node_json[kInputTensorDesc], input_tensor_desc); - if (ret != SUCCESS) { - GELOGW("Fail to trans json to tensor desc."); - return ret; - } - trans_node_info.input = move(input_tensor_desc); - GeTensorDesc output_tensor_desc; - ret = JsonToTensorDesc(trans_node_json[kOutputTensorDesc], output_tensor_desc); - if (ret != SUCCESS) { - GELOGW("Fail to trans json to tensor desc."); - return ret; - } - trans_node_info.output = move(output_tensor_desc); - trans_road.emplace_back(move(trans_node_info)); - } - trans_roads[name_trans_road_json[kName].get()] = move(trans_road); - } - } catch (const exception &e) { - GELOGW("Fail to trans Json to TransRoads. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - return SUCCESS; -} - -Status ModelCacheHelper::ParseChangedGraphIdFromJson(const Json &json, - std::map &changed_graph_id) { - if (!(json.is_array() || json.is_null())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - changed_graph_id.clear(); - for (const Json &name_graph_id_json : json) { - try { - changed_graph_id[name_graph_id_json[kName].get()] = name_graph_id_json[kGraphId].get(); - } catch (const exception &e) { - GELOGW("Fail to trans Json to changed graph id. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::ParseAllocatedGraphIdFromJson(const Json &json, - std::map &allocated_graph_id) { - if (!(json.is_array() || json.is_null())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - allocated_graph_id.clear(); - for (const Json &name_graph_id_json : json) { - try { - allocated_graph_id[name_graph_id_json[kName].get()] = name_graph_id_json[kGraphId].get(); - } catch (const exception &e) { - GELOGW("Fail to trans Json to allocated graph id. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - } - return SUCCESS; -} - -Status ModelCacheHelper::ParseBroadcastInfoFromJson( - const Json &json, std::unordered_map &var_broadcast_info) { - if (!(json.is_array() || json.is_null())) { - GELOGW("Input param json type should be null or array."); - return PARAM_INVALID; - } - for (const Json &broadcast_info_json : json) { - VarBroadCastInfo broadcast_info; - try { - broadcast_info.var_name = broadcast_info_json[kName].get(); - broadcast_info.broadcast_name = broadcast_info_json[kBroadcastName].get(); - broadcast_info.idx = broadcast_info_json[kIdx].get(); - broadcast_info.input_offset = broadcast_info_json[kInputOffset].get(); - broadcast_info.input_size = broadcast_info_json[kInputSize].get(); - broadcast_info.output_offset = broadcast_info_json[kOutputOffset].get(); - broadcast_info.output_size = broadcast_info_json[kOutputSize].get(); - } catch (const exception &e) { - GELOGW("Fail to trans Json to VarBroadCastInfo. Error message: %s", e.what()); - return INTERNAL_ERROR; - } - var_broadcast_info[broadcast_info.var_name] = broadcast_info; - } - return SUCCESS; -} - -Status ModelCacheHelper::LoadOmModelFromCache(GeModelPtr &ge_model) const { - string cache_om = cache_path_ + to_string(graph_id_) + "_" + to_string(graph_id_run_times_[graph_id_]) + kOmSuffix; - if (!CheckInputPathValid(cache_om)) { - GELOGW("Invalid cache path for input:%s.", cache_om.c_str()); - return FAILED; - } - string om_path = RealPath(cache_om.c_str()); - if (om_path.empty()) { - GELOGW("file path is invalid. please check file om: %s", om_path.c_str()); - return FAILED; - } - GELOGI("load model data from file: %s", om_path.c_str()); - Status ret; - int32_t priority = 0; - ModelData model_data; - ret = ModelParserBase::LoadFromFile(om_path.c_str(), priority, model_data); - if (ret != SUCCESS) { - GELOGW("LoadOmModelFromCache: Load model from file failed. ret = %u", ret); - return ret; - } - std::function callback = [&]() { - if (model_data.model_data != nullptr) { - delete[] reinterpret_cast(model_data.model_data); - model_data.model_data = nullptr; - } - }; - GE_MAKE_GUARD(release, callback); - - ModelHelper model_helper; - ret = model_helper.LoadModel(model_data); - if (ret != SUCCESS) { - GELOGW("LoadOmModelFromCache: Load model from data failed. ret = %u", ret); - return ret; - } - ge_model = model_helper.GetGeModel(); - ret = RecompileNodes(ge_model); - if (ret != SUCCESS) { - GELOGW("LoadOmModelFromCache: recompile nodes failed. ret = %u", ret); - return ret; - } - return SUCCESS; -} - -Status ModelCacheHelper::GetVarNameFromVarKey(const string &var_key, const GeTensorDesc &tensor_desc, - string &var_name) { - std::string::size_type underline_idx = var_key.rfind('_'); - if (underline_idx == std::string::npos) { - GELOGW("Invalid var key: underline not found"); - return FAILED; - } - std::string::size_type format_idx = - var_key.rfind(std::to_string(static_cast(tensor_desc.GetFormat())), underline_idx); - if (format_idx == std::string::npos) { - GELOGW("Invalid var key: format not found"); - return FAILED; - } - var_name = var_key.substr(0, format_idx); - return SUCCESS; -} -} // namespace ge diff --git a/ge/common/helper/model_cache_helper.h b/ge/common/helper/model_cache_helper.h deleted file mode 100755 index f0831075..00000000 --- a/ge/common/helper/model_cache_helper.h +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 GE_COMMON_HELPER_MODEL_CACHE_HELPER_H_ -#define GE_COMMON_HELPER_MODEL_CACHE_HELPER_H_ - -#include -#include -#include - -#include "external/ge/ge_api_error_codes.h" -#include "graph/compute_graph.h" -#include "graph/manager/graph_var_manager.h" -#include "common/model/ge_model.h" - -namespace ge { -using Json = nlohmann::json; - -struct CacheInfo { - size_t node_num; - size_t edge_num; - size_t graph_hash; - map nodes_hash; - CacheInfo() : node_num(0), edge_num(0), graph_hash(0) {} -}; - -class ModelCacheHelper { - public: - ModelCacheHelper(uint64_t session_id, uint32_t graph_id, ComputeGraphPtr &compute_graph); - ~ModelCacheHelper(); - - Status SaveCacheInfoToCache () const; - Status SaveVarManagerToCache(bool before_build) const; - Status SaveOmModelToCache(const GeModelPtr &ge_model) const; - bool IsModelCacheHit() const; - Status RecoverVarManagerFromCache() const; - Status LoadOmModelFromCache(GeModelPtr &ge_model) const; - Status RefreshComputeGraph(const ComputeGraphPtr &compute_graph); - Status ClearCache(uint32_t graph_id) const; - - private: - Status GetComputeGraphHash(size_t &hash) const; - Status GetNodesHash(map &hash_map) const; - Status GetCacheInfo(CacheInfo &cache_info) const; - - Status RecoverMemResource(const Json &json) const; - Status RecoverAllocatedGraphId(const Json &json) const; - Status RecoverChangedGraphId(const Json &json) const; - Status RecoverVarAddrAndTensorDesc(const Json &json) const; - Status RecoverBroadcastInfo(const Json &json) const; - Status RecoverTransRoads(const Json &json) const; - static Status GetNodesNeedRecompile(ComputeGraphPtr &graph, vector &nodes); - static Status RecompileNodes(GeModelPtr &ge_model); - - bool IsNodeHashSameAsCache(const map &hash_map) const; - bool IsMemResourceSameAsCache(Json &json) const; - bool IsChangedGraphIdSameAsCache(Json &json) const; - bool IsAllocatedGraphIdSameAsCache(Json &json) const; - bool IsCurVarTensorDescSameAsCache(Json &json) const; - bool IsVarAddrMgrMapSameAsCache(Json &json) const; - bool IsBroadcastInfoSameAsCache(Json &json) const; - bool IsTransRoadsSameAsCache(Json &json) const; - bool IsVarManagerSameAsCache(Json &json) const; - bool IsVarManagerParamSameAsCache(Json &json) const; - - Status SaveJsonToFile(const string &file_name, const Json &json) const; - Status LoadJsonFromFile(const string &file_name, Json &json) const; - - Status GetNodesHashMapJson(Json &json) const; - Status GetMemResourceMap(Json &json) const; - Status GetVarAddrMgrMapJson(Json &json) const; - Status GetCurVarTensorDescMapJson(Json &json) const; - Status GetTransRoadsJson(Json &json) const; - Status GetChangedGraphIdJson(Json &json) const; - Status GetAllocatedGraphIdJson(Json &json) const; - Status GetBroadcastInfoJson(Json &json) const; - Status GetVarResourceJson(Json &json) const; - Status GetVarManagerJson(Json &json) const; - - static Status TensorDescToJson(const GeTensorDesc &ge_tensor_desc, Json &json); - static Status JsonToTensorDesc(const Json &json, GeTensorDesc &ge_tensor_desc); - static Status ParseMemResourceFromJson(const Json &json, map &mem_resource); - static Status ParseVarAddrMgrMapFromJson(const Json &json, - std::vector> &var_addr_mgr_vector, - std::set &var_offset_set); - static Status ParseCurVarTensorDescMapFromJson( - const Json &json, std::unordered_map &cur_var_tensor_desc_map); - static Status ParseTransRoadsFromJson(const Json &json, - std::unordered_map> &trans_roads); - static Status ParseChangedGraphIdFromJson(const Json &json, - std::map &changed_graph_id); - static Status ParseAllocatedGraphIdFromJson(const Json &json, - std::map &allocated_graph_id); - static Status ParseBroadcastInfoFromJson(const Json &json, - std::unordered_map &var_broadcast_info); - static Status GetVarNameFromVarKey(const string &var_key, const GeTensorDesc &tensor_desc, string &var_name); - - uint64_t session_id_; - uint32_t graph_id_; - string cache_path_; - ComputeGraphPtr compute_graph_; - std::set var_names_; - bool is_cache_path_valid_for_output; - static map graph_id_run_times_; -}; - -using ModelCacheHelperPtr = std::shared_ptr; -} // namespace ge - -#endif // GE_COMMON_HELPER_MODEL_CACHE_HELPER_H_ diff --git a/ge/executor/ge_executor.cc b/ge/executor/ge_executor.cc index 73cd7bb5..76cde2b9 100755 --- a/ge/executor/ge_executor.cc +++ b/ge/executor/ge_executor.cc @@ -27,6 +27,7 @@ #include "graph/load/graph_loader.h" #include "graph/load/model_manager/model_manager.h" #include "graph/manager/graph_mem_manager.h" +#include "graph/manager/graph_var_manager.h" #include "single_op/single_op_manager.h" #include "graph/load/model_manager/davinci_model.h" #include "opskernel_manager/ops_kernel_builder_manager.h" diff --git a/ge/generator/ge_generator.cc b/ge/generator/ge_generator.cc index 45eaed59..1a80a3e0 100644 --- a/ge/generator/ge_generator.cc +++ b/ge/generator/ge_generator.cc @@ -30,6 +30,7 @@ #include "graph/debug/ge_attr_define.h" #include "graph/ge_context.h" #include "graph/manager/graph_manager.h" +#include "graph/manager/graph_var_manager.h" #include "graph/manager/util/rt_context_util.h" #include "graph/operator_factory_impl.h" #include "graph/opsproto_manager.h" diff --git a/ge/graph/manager/graph_manager.cc b/ge/graph/manager/graph_manager.cc index 7d72d85b..d1237f4e 100755 --- a/ge/graph/manager/graph_manager.cc +++ b/ge/graph/manager/graph_manager.cc @@ -248,7 +248,6 @@ Status GraphManager::Finalize() { Analyzer::GetInstance()->DestroyGraphJsonObject(session_id, graph_id); } graph_map_.clear(); - cache_helper_map_.clear(); graph_count_.clear(); // graph context @@ -1005,13 +1004,6 @@ Status GraphManager::PreRun(const GraphNodePtr &graph_node, const std::vectorGetGraphId(), compute_graph, ge_model); - if (save_ret != SUCCESS) { - GELOGW("Fail to save cache."); - } GEEVENT("[GEPERFTRACE] GE PreRun End"); return SUCCESS; } @@ -1063,18 +1055,15 @@ Status GraphManager::StartForRunGraph(const GraphNodePtr &graph_node, const std: graph_node->GetGraphId()); return PARAM_INVALID; } - GeModelPtr ge_model = nullptr; - // check need incre build. - ret = IncreBuild(graph_node, ge_model); + + ret = PreRun(graph_node, inputs, ge_root_model, session_id); + // release rts generate context + RtContextUtil::GetInstance().DestroyRtContexts(session_id, graph_node->GetGraphId()); if (ret != SUCCESS) { - ret = PreRun(graph_node, inputs, ge_root_model, session_id); - // release rts generate context - RtContextUtil::GetInstance().DestroyRtContexts(session_id, graph_node->GetGraphId()); - if (ret != SUCCESS) { - GELOGE(ret, "[Call][PreRun] Failed, graph_id:%u, session_id:%lu.", graph_node->GetGraphId(), session_id); - return ret; - } + GELOGE(ret, "[Call][PreRun] Failed, graph_id:%u, session_id:%lu.", graph_node->GetGraphId(), session_id); + return ret; } + ret = LoadGraph(ge_root_model, graph_node); if (ret != SUCCESS) { GELOGE(ret, "[Load][Graph] Failed, graph_id:%u.", graph_node->GetGraphId()); @@ -1104,91 +1093,6 @@ Status GraphManager::LoadGraph(const GeRootModelPtr &ge_root_model, const GraphN return executor_->LoadGraph(ge_root_model, graph_node); } -Status GraphManager::LoadFromCache(const GraphNodePtr &graph_node, const ModelCacheHelperPtr &cache_helper, - GeModelPtr &ge_model) { - auto graph_id = graph_node->GetGraphId(); - auto ret = cache_helper->LoadOmModelFromCache(ge_model); - if (ret != SUCCESS) { - GELOGW("Fail to load om model from cache."); - if (cache_helper->ClearCache(graph_id) != SUCCESS) { - GELOGW("Fail to clear cache of graph %u.", graph_id); - } - return FAILED; - } - ret = cache_helper->RecoverVarManagerFromCache(); - if (ret != SUCCESS) { - GELOGW("Fail to recover VarManager from cache."); - if (cache_helper->ClearCache(graph_id) != SUCCESS) { - GELOGW("Fail to clear cache of graph %u.", graph_id); - } - return FAILED; - } - ComputeGraphPtr compute_graph_in_model = GraphUtils::GetComputeGraph(ge_model->GetGraph()); - if (compute_graph_in_model == nullptr) { - GELOGW("Error occurred when get compute graph from om, abandon."); - return FAILED; - } else { - graph_node->SetComputeGraph(compute_graph_in_model); - graph_node->SetGeModel(ge_model); - GELOGI("Load model and graph form cache om file."); - } - return SUCCESS; -} - -Status GraphManager::SaveCacheBeforeBuild(uint32_t graph_id, const ModelCacheHelperPtr &cache_helper) { - auto ret = cache_helper->SaveCacheInfoToCache(); - if (ret != SUCCESS) { - GELOGW("Fail to save cache info of graph[%d] to cache.", graph_id); - return FAILED; - } - ret = cache_helper->SaveVarManagerToCache(true); - if (ret != SUCCESS) { - GELOGW("Fail to save var manager to cache."); - cache_helper->ClearCache(graph_id); - return FAILED; - } - GELOGI("Cache files have been saved."); - return SUCCESS; -} - -Status GraphManager::SaveCacheAfterBuild(uint32_t graph_id, ge::ComputeGraphPtr graph, GeModelPtr &ge_model) { - std::shared_ptr instance_ptr = ge::GELib::GetInstance(); - if ((instance_ptr == nullptr) || !instance_ptr->InitFlag()) { - GELOGW("GELib not initialized."); - return FAILED; - } - - if (instance_ptr->IsIncreBuild()) { - std::lock_guard lock(member_mutex_); - auto iter = cache_helper_map_.find(graph_id); - if (iter == cache_helper_map_.end()) { - GELOGW("Can not find ModelCacheHelper of graph[%u]", graph_id); - return FAILED; - } else { - ModelCacheHelperPtr cache_helper = iter->second; - auto ret = cache_helper->RefreshComputeGraph(graph); - if (ret != SUCCESS) { - cache_helper->ClearCache(graph_id); - GELOGW("Fail to refresh cache helper's compute graph"); - return FAILED; - } - ret = cache_helper->SaveVarManagerToCache(false); - if (ret != SUCCESS) { - cache_helper->ClearCache(graph_id); - GELOGW("Fail to save VarManager to cache"); - return FAILED; - } - ret = cache_helper->SaveOmModelToCache(ge_model); - if (ret != SUCCESS) { - cache_helper->ClearCache(graph_id); - GELOGW("Fail to save om model to cache"); - return FAILED; - } - } - } - return SUCCESS; -} - Status GraphManager::InnerRunGraph(GraphNodePtr &graph_node, const GraphId &graph_id, const std::vector &inputs, std::vector &outputs) { GE_CHECK_NOTNULL(executor_); @@ -1239,8 +1143,6 @@ Status GraphManager::RunGraphWithStreamAsync(const GraphId &graph_id, rtStream_t graph_node->SetIsSpecificStream(true); ComputeGraphPtr compute_graph_tmp = GraphUtils::GetComputeGraph(*(graph_node->GetGraph())); - // when set incre build, add cache helper map - AddModelCacheHelperToMap(graph_id, session_id, compute_graph_tmp); if (options_.local_fmk_op_flag) { GetCompilerStages(graph_id).optimizer.TranFrameOp(compute_graph_tmp); } @@ -1299,9 +1201,6 @@ Status GraphManager::RunGraph(const GraphId &graph_id, const std::vector lock(member_mutex_); - auto iter = cache_helper_map_.find(graph_id); - if (iter != cache_helper_map_.end()) { - cache_helper_map_.erase(iter); - } else { - GELOGW("[GraphManager] cache helper does not exist, graph_id = %u", graph_id); - } -} - bool GraphManager::CheckModelLoad(const GeRootModelPtr &ge_root_model, bool load_flag) { return ((ge_root_model != nullptr) && (ge_root_model->GetModelId() != INVALID_MODEL_ID) && load_flag); } @@ -1555,7 +1444,6 @@ Status GraphManager::RemoveGraph(const GraphId &graph_id) { var_acc_ctrl_.RemoveGraph(graph_id); RemoveGraphNode(graph_id); - RemoveModelCacheHelper(graph_id); auto ge_root_model = graph_node->GetGeRootModel(); if (CheckModelLoad(ge_root_model, graph_node->GetLoadFlag())) { @@ -2727,61 +2615,6 @@ Status GraphManager::RunGraphAsync(const GraphId &graph_id, const std::vector instance_ptr = ge::GELib::GetInstance(); - if (instance_ptr != nullptr && instance_ptr->IsIncreBuild()) { - std::lock_guard lock(member_mutex_); - auto iter = cache_helper_map_.find(graph_id); - if (iter == cache_helper_map_.end()) { - ModelCacheHelperPtr cache_helper = MakeShared(session_id, graph_id, compute_graph); - if (cache_helper != nullptr) { - cache_helper_map_.emplace(std::make_pair(graph_id, cache_helper)); - } else { - GELOGW("Cache helper make shared failed, graph_id = %u.", graph_id); - } - } - } -} - -ModelCacheHelperPtr GraphManager::FindModelCacheHelper(GraphId graph_id) { - std::lock_guard lock(member_mutex_); - auto iter = cache_helper_map_.find(graph_id); - if (iter != cache_helper_map_.end()) { - return iter->second; - } - - return nullptr; -} - -Status GraphManager::IncreBuild(const GraphNodePtr &graph_node, GeModelPtr &ge_model) { - std::shared_ptr instance_ptr = ge::GELib::GetInstance(); - if (instance_ptr == nullptr || !instance_ptr->IsIncreBuild()) { - return FAILED; - } - const uint32_t graph_id = graph_node->GetGraphId(); - ModelCacheHelperPtr cache_helper = FindModelCacheHelper(graph_id); - if (cache_helper == nullptr) { - GELOGW("Can not find ModelCacheHelper of graph[%u]", graph_id); - return FAILED; - } - if (cache_helper->IsModelCacheHit()) { - GEEVENT("Model cache hit."); - Status ret = LoadFromCache(graph_node, cache_helper, ge_model); - if (ret == SUCCESS) { - return SUCCESS; - } else { - GELOGW("Error occurred when load from cache, abandon."); - } - } else { - GEEVENT("Model cache miss."); - } - if (SaveCacheBeforeBuild(graph_node->GetGraphId(), cache_helper) != SUCCESS) { - GELOGW("Error occurred when save cache."); - } - return FAILED; -} - Status GraphManager::CheckIncreBuildAndPreRun(const PreRunArgs &args, GraphNodePtr &graph_node, GeRootModelPtr &ge_root_model) { if (!IsGraphNeedBuild(graph_node)) { @@ -2796,20 +2629,18 @@ Status GraphManager::CheckIncreBuildAndPreRun(const PreRunArgs &args, return PARAM_INVALID; } // check need incre build. - GeModelPtr ge_model = nullptr; - if (IncreBuild(graph_node, ge_model) != SUCCESS) { - std::vector ge_inputs; - for (const auto &item: args.input_tensor) { - ge_inputs.emplace_back(TensorAdapter::AsGeTensor(item)); - } - Status ret = PreRun(graph_node, ge_inputs, ge_root_model, args.session_id); - // release rts generate context - RtContextUtil::GetInstance().DestroyRtContexts(args.session_id, graph_node->GetGraphId()); - if (ret != SUCCESS) { - ReturnError(args.callback, ret, "PreRun Failed."); - return ret; - } + std::vector ge_inputs; + for (const auto &item: args.input_tensor) { + ge_inputs.emplace_back(TensorAdapter::AsGeTensor(item)); } + Status ret = PreRun(graph_node, ge_inputs, ge_root_model, args.session_id); + // release rts generate context + RtContextUtil::GetInstance().DestroyRtContexts(args.session_id, graph_node->GetGraphId()); + if (ret != SUCCESS) { + ReturnError(args.callback, ret, "PreRun Failed."); + return ret; + } + graph_node->SetBuildFlag(true); var_acc_ctrl_.SetGraphBuildEnd(graph_node->GetGraphId()); return SUCCESS; @@ -2878,10 +2709,6 @@ void GraphManager::PreRunThread() { graph_node->Unlock(); return; } - // when set incre build, save cache helper. - AddModelCacheHelperToMap(args.graph_id, args.session_id, compute_graph_tmp); - - std::vector ge_models; if (options_.local_fmk_op_flag) { GetCompilerStages(graph_node->GetGraphId()).optimizer.TranFrameOp(compute_graph_tmp); diff --git a/ge/graph/manager/graph_manager.h b/ge/graph/manager/graph_manager.h index 84d2b11e..e7cd88a9 100644 --- a/ge/graph/manager/graph_manager.h +++ b/ge/graph/manager/graph_manager.h @@ -27,7 +27,6 @@ #include "common/blocking_queue.h" #include "framework/common/ge_inner_error_codes.h" -#include "common/helper/model_cache_helper.h" #include "external/graph/types.h" #include "external/ge/ge_api_types.h" #include "graph/build/graph_builder.h" @@ -339,14 +338,6 @@ class GraphManager { bool IsGraphNeedBuild(const GraphNodePtr &graph_node); - Status LoadFromCache(const GraphNodePtr &graph_node, const ModelCacheHelperPtr &cache_helper, GeModelPtr &ge_model); - Status SaveCacheBeforeBuild(uint32_t graph_id, const ModelCacheHelperPtr &cache_helper); - Status SaveCacheAfterBuild(uint32_t graph_id, ComputeGraphPtr graph, GeModelPtr &ge_model); - void AddModelCacheHelperToMap(const GraphId &graph_id, uint64_t session_id, ComputeGraphPtr &compute_graph); - Status IncreBuild(const GraphNodePtr &graph_node, GeModelPtr &ge_model); - void RemoveModelCacheHelper(const GraphId &graph_id); - ModelCacheHelperPtr FindModelCacheHelper(GraphId graph_id); - void SetRunContext(const GraphNodePtr &graph_node); void PushGraph(const RunArgs &args); @@ -411,7 +402,6 @@ class GraphManager { std::thread prerun_thread_; ComputeGraphPtr compute_graph_; std::map graph_map_; - std::map cache_helper_map_; // summary and checkpoint callback function list for ME, key is summary or checkpoint std::map &)>> me_callback_map_; diff --git a/ge/graph/manager/graph_manager_utils.cc b/ge/graph/manager/graph_manager_utils.cc index 42251b10..225a748a 100644 --- a/ge/graph/manager/graph_manager_utils.cc +++ b/ge/graph/manager/graph_manager_utils.cc @@ -70,45 +70,9 @@ void GraphNode::IncreaseLoadCount() { ++load_count_; } -SubGraphInfo::SubGraphInfo() : subgraph_ptr_(nullptr), ge_model_ptr_(nullptr), malloc_flag_(false) {} +SubGraphInfo::SubGraphInfo() : subgraph_ptr_(nullptr), ge_model_ptr_(nullptr) {} SubGraphInfo::~SubGraphInfo() { - if (malloc_flag_) { - for (auto &buffer_addr : buffer_addr_) { - if (buffer_addr == nullptr) { - continue; - } - rtError_t rt_ret; - rt_ret = rtFreeHost(buffer_addr); - buffer_addr = nullptr; - if (rt_ret != RT_ERROR_NONE) { - GELOGE(rt_ret, "[Call][RtFreeHost] subgraph free buffer failed, modelId = %u", - model_id_info_.model_id); - } - } - } -} - -Status SubGraphInfo::FreeInOutBuffer() { - if (malloc_flag_) { - for (auto iter = buffer_addr_.begin(); iter != buffer_addr_.end(); ++iter) { - rtError_t rt_ret; - rt_ret = rtFreeHost(*iter); - if (rt_ret != RT_ERROR_NONE) { - REPORT_CALL_ERROR("E19999", "Call rtFreeHost fail, ret:%d", rt_ret); - GELOGE(rt_ret, "[Call][RtFreeHost] subgraph free buffer failed, modelId = %u", model_id_info_.model_id); - buffer_addr_.erase(buffer_addr_.begin(), iter); - return GE_GRAPH_FREE_FAILED; - } - } - buffer_addr_.clear(); - - malloc_flag_ = false; - return SUCCESS; - } else { - GELOGI("[GraphManager] not malloc buffer, modelId = %u", model_id_info_.model_id); - return SUCCESS; - } } GraphModelListener::GraphModelListener(std::mutex &mutex, std::condition_variable &cond) diff --git a/ge/graph/manager/graph_manager_utils.h b/ge/graph/manager/graph_manager_utils.h index 14eb67f2..efdbecf8 100644 --- a/ge/graph/manager/graph_manager_utils.h +++ b/ge/graph/manager/graph_manager_utils.h @@ -86,8 +86,6 @@ class SubGraphInfo { void SetGeModelPtr(const GeModelPtr &ge_model_ptr) { ge_model_ptr_ = ge_model_ptr; } bool GeModelIsValid() const { return ge_model_ptr_ != nullptr; } - Status FreeInOutBuffer(); - void SetOutputContext(const std::string &output) { output_names_ = output; } std::string GetOutputContext() const { return output_names_; } diff --git a/ge/graph/manager/graph_var_manager.cc b/ge/graph/manager/graph_var_manager.cc index d0669254..ce5b335e 100755 --- a/ge/graph/manager/graph_var_manager.cc +++ b/ge/graph/manager/graph_var_manager.cc @@ -429,10 +429,6 @@ ge::Status VarManager::GetVarAddr(const std::string &var_name, const ge::GeTenso return GetVarAddr(var_name, tensor_desc, dev_ptr, memory_type); } -void VarManager::GetAllVarAddrMgr(std::unordered_map &var_addr_mgr_map) { - var_resource_->GetAllVarAddrMgr(var_addr_mgr_map); -} - int64_t VarManager::GetVarMemSize(rtMemType_t memory_type) { std::lock_guard lock(mutex_); MemResource *mem_resource = nullptr; @@ -453,36 +449,6 @@ int64_t VarManager::GetVarMemSize(rtMemType_t memory_type) { return mem_resource->GetVarMemSize(); } -Status VarManager::UpdateVarMemSize(rtMemType_t memory_type, int64_t mem_size) { - std::lock_guard lock(mutex_); - MemResource *mem_resource = nullptr; - auto iter = mem_resource_map_.find(memory_type); - if (iter == mem_resource_map_.end()) { - mem_resource = MemResource::BuildMemResourceFromType(memory_type); - if (mem_resource == nullptr) { - REPORT_CALL_ERROR("E19999", "memory_type:%d invalid or New MemResource fail, session_id:%lu", - memory_type, session_id_); - GELOGE(ge::INTERNAL_ERROR, "[Alloc][MemResource] failed, memory_type:%u, session_id:%lu", - memory_type, session_id_); - return ge::INTERNAL_ERROR; - } else { - mem_resource_map_[memory_type] = mem_resource; - } - } else { - mem_resource = iter->second; - } - - if (mem_resource == nullptr) { - REPORT_INNER_ERROR("E19999", "MemResource is invalid, memory_type:%d, session_id:%lu", - memory_type, session_id_); - GELOGE(ge::INTERNAL_ERROR, "[Check][Param] MemResource is invalid, memory_type:%u, session_id:%lu", - memory_type, session_id_); - return FAILED; - } - mem_resource->UpdateVarMemSize(mem_size); - return SUCCESS; -} - ge::Status VarManager::AssignVarMem(const std::string &var_name, const ge::GeTensorDesc &tensor_desc, rtMemType_t memory_type) { std::lock_guard lock(mutex_); @@ -638,16 +604,6 @@ ge::Status VarManager::SaveBroadCastInfo(uint32_t graph_id, const VarBroadCastIn return SUCCESS; } -ge::Status VarManager::GetBroadCastInfo(uint32_t graph_id, const string &var_name, VarBroadCastInfo &broad_cast_info) { - std::lock_guard lock(mutex_); - - if (var_resource_ == nullptr) { - GELOGW("VarManager has not been init."); - return ge::INTERNAL_ERROR; - } - return var_resource_->GetBroadCastInfo(graph_id, var_name, broad_cast_info); -} - ge::Status VarManager::RenewCurVarDesc(const std::string &var_name, ge::OpDescPtr op_desc) { std::lock_guard lock(mutex_); GELOGD("VarManager::RenewCurVarDesc var_name = %s.", var_name.c_str()); diff --git a/ge/graph/manager/graph_var_manager.h b/ge/graph/manager/graph_var_manager.h index a1b45959..f0e3b89b 100755 --- a/ge/graph/manager/graph_var_manager.h +++ b/ge/graph/manager/graph_var_manager.h @@ -223,14 +223,10 @@ class FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY VarManager { ge::Status GetVarAddr(const std::string &var_name, const ge::GeTensorDesc &tensor_desc, uint8_t **dev_ptr, rtMemType_t &memory_type); - void GetAllVarAddrMgr(std::unordered_map &var_addr_mgr_map); - ge::Status GetVarAddr(const std::string &var_name, const ge::GeTensorDesc &tensor_desc, uint8_t **dev_ptr); ge::Status SaveBroadCastInfo(uint32_t graph_id, const VarBroadCastInfo &broad_cast_info); - ge::Status GetBroadCastInfo(uint32_t graph_id, const string &var_name, VarBroadCastInfo &broad_cast_info); - ge::Status GetCurVarDesc(const std::string &var_name, ge::GeTensorDesc &tensor_desc); ge::Status RenewCurVarDesc(const std::string &var_name, ge::OpDescPtr op_desc); @@ -273,8 +269,6 @@ class FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY VarManager { int64_t GetVarMemSize(rtMemType_t memory_type); - Status UpdateVarMemSize(rtMemType_t memory_type, int64_t mem_size); - bool IsVarExist(const std::string &var_name, const ge::GeTensorDesc &tensor_desc); bool IsVarExist(const std::string &var_name); diff --git a/ge/graph/manager/model_manager/event_manager.cc b/ge/graph/manager/model_manager/event_manager.cc deleted file mode 100644 index 339e9894..00000000 --- a/ge/graph/manager/model_manager/event_manager.cc +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "graph/manager/model_manager/event_manager.h" - -#define RETURN_IF_COND_NOT_MET(condition, ...) \ - do { \ - if (!(condition)) { \ - GELOGE(FAILED, __VA_ARGS__); \ - return; \ - } \ - } while (0); - -namespace ge { -Status EventManager::Init(size_t event_num) { - if (this->inited_) { - return SUCCESS; - } - - rtEvent_t event = nullptr; - current_idx_ = 0; - for (size_t i = 0; i < event_num; ++i) { - GE_CHK_RT_RET(rtEventCreate(&event)); - this->event_list_.push_back(event); - } - - this->inited_ = true; - - return SUCCESS; -} - -void EventManager::Release() noexcept { - for (size_t i = 0; i < this->event_list_.size(); ++i) { - rtError_t rt_ret = rtEventDestroy(this->event_list_[i]); - RETURN_IF_COND_NOT_MET(rt_ret == RT_ERROR_NONE, "[Destroy][Event] failed, idx is %zu, ret is 0x%x.", i, rt_ret); - } - this->event_list_.clear(); - - this->inited_ = false; -} - -Status EventManager::EventRecord(size_t event_idx, rtStream_t stream) { - GE_CHK_BOOL_RET_STATUS_NOLOG(this->inited_, INTERNAL_ERROR); - - GE_CHK_BOOL_RET_STATUS_NOLOG(event_idx < this->event_list_.size(), PARAM_INVALID); - - GE_CHK_RT_RET(rtEventRecord(this->event_list_[event_idx], stream)); - - current_idx_ = static_cast(event_idx); - return SUCCESS; -} - -Status EventManager::EventElapsedTime(size_t start_event_idx, size_t stop_event_idx, float &time) { - GE_CHK_BOOL_RET_STATUS_NOLOG(this->inited_, INTERNAL_ERROR); - - GE_CHK_BOOL_RET_STATUS_NOLOG(start_event_idx < this->event_list_.size() && - stop_event_idx < this->event_list_.size() && start_event_idx <= stop_event_idx, - PARAM_INVALID); - - GE_CHK_RT_RET(rtEventElapsedTime(&time, this->event_list_[start_event_idx], this->event_list_[stop_event_idx])); - - return SUCCESS; -} - -Status EventManager::GetEvent(uint32_t index, rtEvent_t &event) { - GE_CHK_BOOL_RET_STATUS_NOLOG(index < this->event_list_.size(), PARAM_INVALID); - event = this->event_list_[index]; - return SUCCESS; -} -} // namespace ge diff --git a/ge/graph/manager/model_manager/event_manager.h b/ge/graph/manager/model_manager/event_manager.h deleted file mode 100644 index 2cb1c3f6..00000000 --- a/ge/graph/manager/model_manager/event_manager.h +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 GE_GRAPH_MANAGER_MODEL_MANAGER_EVENT_MANAGER_H_ -#define GE_GRAPH_MANAGER_MODEL_MANAGER_EVENT_MANAGER_H_ - - -#include - -#include "framework/common/fmk_error_codes.h" -#include "framework/common/fmk_types.h" -#include "framework/common/util.h" -#include "runtime/event.h" - -namespace ge { -class EventManager { - public: - /// - /// @ingroup domi_ome - /// @brief constructor - /// - EventManager() : inited_(false), current_idx_(0) {} - /// - /// @ingroup domi_ome - /// @brief destructor - /// - ~EventManager() { this->Release(); } - - /// - /// @ingroup domi_ome - /// @brief init and create event list - /// @param [in] event_num event number created - /// @return exec result - /// - Status Init(size_t event_num); - - /// - /// @ingroup domi_ome - /// @brief event record - /// @param [in] event_idx event index - /// @param [in] stream related stream - /// @return exec result - /// - Status EventRecord(size_t event_idx, rtStream_t stream); - - /// - /// @ingroup domi_ome - /// @brief time between start and end in ms - /// @param [in] start_event_idx start event index - /// @param [in] stop_event_idx stop event index - /// @param [out] time - /// @return exec result - /// - Status EventElapsedTime(size_t start_event_idx, size_t stop_event_idx, float &time); - - /// - /// @ingroup domi_ome - /// @brief current event index - /// @return - /// - uint32_t CurrentIdx() const { return current_idx_; } - - /// - /// @ingroup domi_ome - /// @brief get event at specific loc - /// @param [in] index event index - /// @return - /// - Status GetEvent(uint32_t index, rtEvent_t &event); - - /// - /// @ingroup domi_ome - /// @brief release event list - /// @param [in] - /// @return - /// - void Release() noexcept; - - private: - std::vector event_list_; - bool inited_; - uint32_t current_idx_; -}; // EventManager -} // namespace ge -#endif // GE_GRAPH_MANAGER_MODEL_MANAGER_EVENT_MANAGER_H_ diff --git a/ge/graph/manager/trans_var_data_utils.h b/ge/graph/manager/trans_var_data_utils.h index 174efbb3..f5a89a50 100755 --- a/ge/graph/manager/trans_var_data_utils.h +++ b/ge/graph/manager/trans_var_data_utils.h @@ -24,7 +24,6 @@ #include "graph/utils/tensor_utils.h" #include "graph/node.h" #include "runtime/context.h" -#include "graph/manager/graph_var_manager.h" namespace ge { class TransVarDataUtils { diff --git a/ge/graph/passes/global_step_insert_pass.cc b/ge/graph/passes/global_step_insert_pass.cc index 297e4ee2..ada4e12a 100755 --- a/ge/graph/passes/global_step_insert_pass.cc +++ b/ge/graph/passes/global_step_insert_pass.cc @@ -24,7 +24,6 @@ #include "framework/common/util.h" #include "graph/debug/ge_attr_define.h" #include "common/ge/ge_util.h" -#include "graph/manager/graph_var_manager.h" #include "graph/passes/pass_utils.h" #include "graph/ge_context.h" diff --git a/ge/init/gelib.h b/ge/init/gelib.h index 5e66be51..226dd4c8 100644 --- a/ge/init/gelib.h +++ b/ge/init/gelib.h @@ -28,7 +28,6 @@ #include "graph/debug/ge_attr_define.h" #include "graph/utils/graph_utils.h" #include "graph/utils/anchor_utils.h" -#include "graph/manager/graph_var_manager.h" #include "framework/common/ge_inner_error_codes.h" #include "framework/common/ge_types.h" @@ -63,13 +62,7 @@ class GE_FUNC_VISIBILITY GELib { bool InitFlag() const { return init_flag_; } // get TrainMode flag - bool isTrainMode() { return is_train_mode_; } - - // get incre build flag - bool IsIncreBuild() const { return is_incre_build_; } - - // get incre build cache path - const std::string &GetIncreBuildCachePath() const { return incre_build_cache_path_; } + bool IsTrainMode() { return is_train_mode_; } void InitProfiling(Options &options); void ShutDownProfiling(); @@ -100,8 +93,6 @@ class GE_FUNC_VISIBILITY GELib { bool is_system_inited = false; bool is_shutdown = false; bool is_use_hcom = false; - bool is_incre_build_ = false; - std::string incre_build_cache_path_; }; } // namespace ge diff --git a/tests/ut/ge/CMakeLists.txt b/tests/ut/ge/CMakeLists.txt index 49c9161d..a0790cf2 100755 --- a/tests/ut/ge/CMakeLists.txt +++ b/tests/ut/ge/CMakeLists.txt @@ -140,7 +140,6 @@ set(COMMON_SRC_FILES "${GE_CODE_DIR}/ge/graph/optimize/graph_optimize.cc" "${GE_CODE_DIR}/ge/graph/build/graph_builder.cc" "${GE_CODE_DIR}/ge/graph/partition/graph_partition.cc" - "${GE_CODE_DIR}/ge/common/helper/model_cache_helper.cc" "${GE_CODE_DIR}/ge/ir_build/ge_ir_build.cc" "${GE_CODE_DIR}/ge/ir_build/attr_options/utils.cc" "${GE_CODE_DIR}/ge/ir_build/attr_options/keep_dtype_option.cc" @@ -248,7 +247,6 @@ set(GRAPH_DAVINCI_MODEL_SRC_FILES "${GE_CODE_DIR}/ge/graph/load/model_manager/task_info/super_kernel/super_kernel.cc" "${GE_CODE_DIR}/ge/graph/load/model_manager/task_info/super_kernel/super_kernel_factory.cc" "${GE_CODE_DIR}/ge/hybrid/node_executor/aicpu/aicpu_ext_info.cc" - "${GE_CODE_DIR}/ge/graph/manager/model_manager/event_manager.cc" ) set(GRAPH_EXECUTE_COMMON_SRC_FILES @@ -520,13 +518,9 @@ set(COMMON_TEST_FILES set(DISTINCT_GRAPH_LOAD_TEST_FILES "graph/load/data_dumper_unittest.cc" - #"graph/load/new_model_manager_data_inputer_unittest.cc" - #"graph/load/new_model_manager_davinci_model_unittest.cc" "graph/load/model_manager_unittest.cc" "graph/load/new_model_manager_model_manager_aicpu_unittest.cc" "graph/load/end_graph_task_unittest.cc" - "graph/load/new_model_manager_event_manager_unittest.cc" - #"graph/load/output_net_output_unittest.cc" "graph/load/davinci_model_unittest.cc" "graph/load/tbe_handle_store_unittest.cc" "graph/load/hccl_task_info_unittest.cc" @@ -536,7 +530,6 @@ set(DISTINCT_GRAPH_LOAD_TEST_FILES "graph/load/memcpy_addr_async_task_info_unittest.cc" "graph/load/memcpy_async_task_info_unittest.cc" "graph/load/cpu_queue_schedule_unittest.cc" - #"graph/graph_load_unittest.cc" "graph/ge_executor_unittest.cc" "graph/load/model_helper_unittest.cc" "graph/load/model_utils_unittest.cc" diff --git a/tests/ut/ge/graph/execute/model_executor_unittest.cc b/tests/ut/ge/graph/execute/model_executor_unittest.cc index d4e0e3a4..cd907e99 100644 --- a/tests/ut/ge/graph/execute/model_executor_unittest.cc +++ b/tests/ut/ge/graph/execute/model_executor_unittest.cc @@ -20,6 +20,7 @@ #define private public #include "graph/execute/model_executor.h" #include "graph/manager/graph_manager.h" +#include "graph/manager/graph_var_manager.h" #include "graph/load/model_manager/model_manager.h" #include "graph/load/model_manager/davinci_model.h" diff --git a/tests/ut/ge/graph/graph_load_unittest.cc b/tests/ut/ge/graph/graph_load_unittest.cc deleted file mode 100644 index 93282a5e..00000000 --- a/tests/ut/ge/graph/graph_load_unittest.cc +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include -#include -#include -#include - -#include "common/debug/log.h" -#include "common/helper/model_helper.h" -#include "common/op/ge_op_utils.h" -#include "common/types.h" -#include "graph/op_desc.h" -#include "graph/types.h" -#include "graph/utils/attr_utils.h" -#include "graph/utils/op_desc_utils.h" - -#define protected public -#define private public -#include "graph/load/graph_loader.h" - -#include "framework/common/ge_inner_error_codes.h" -#include "graph/load/model_manager/model_manager.h" -#include "graph/manager/graph_manager_utils.h" -#include "common/model/ge_model.h" -#undef private -#undef protected - -using namespace testing; -namespace ge { - -class UtestGraphGraphLoad : public testing::Test { - protected: - void SetUp() {} - - void TearDown() {} -}; - -TEST_F(UtestGraphGraphLoad, load_graph_param_invalid1) { - std::shared_ptr graph_run_listener = nullptr; - SubGraphInfo sub_graph1; - ge::SubGraphInfoPtr sub_graph_ptr1 = std::make_shared(sub_graph1); - ModelIdInfo model_Id_info; - model_Id_info.model_id = 1; - - GeModelPtr ge_model_ptr = std::make_shared(); - sub_graph_ptr1->SetGeModelPtr(ge_model_ptr); - - std::vector input_flag; - input_flag.push_back(false); - sub_graph_ptr1->SetInputFlag(input_flag); - - ge::GraphLoader graph_load; - EXPECT_EQ(GE_GRAPH_PARAM_NULLPTR, graph_load.LoadGraph(sub_graph_ptr1->ge_model_ptr_, graph_run_listener, model_Id_info)); - sub_graph_ptr1->SetModelIdInfo(model_Id_info); -} - -TEST_F(UtestGraphGraphLoad, load_graph_param_invalid2) { - std::mutex sync_run_mutex; - std::condition_variable condition; - std::shared_ptr listener = std::make_shared(sync_run_mutex, condition); - - SubGraphInfo sub_graph1; - ge::SubGraphInfoPtr sub_graph_ptr1 = std::make_shared(sub_graph1); - ModelIdInfo model_Id_info; - model_Id_info.model_id = 1; - - GeModelPtr ge_model_ptr = std::make_shared(); - sub_graph_ptr1->SetGeModelPtr(ge_model_ptr); - - std::vector input_flag; - input_flag.push_back(false); - sub_graph_ptr1->SetInputFlag(input_flag); - - ge::GraphLoader graph_load; - EXPECT_EQ(GE_GRAPH_PARAM_NULLPTR, graph_load.LoadGraph(sub_graph_ptr1->ge_model_ptr_, listener, model_Id_info)); - sub_graph_ptr1->SetModelIdInfo(model_Id_info); -} -} // namespace ge diff --git a/tests/ut/ge/graph/load/new_model_manager_data_inputer_unittest.cc b/tests/ut/ge/graph/load/new_model_manager_data_inputer_unittest.cc deleted file mode 100644 index 43c2ad15..00000000 --- a/tests/ut/ge/graph/load/new_model_manager_data_inputer_unittest.cc +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 - -#include "graph/load/model_manager/data_inputer.h" - -#include "common/debug/log.h" -#include "common/debug/memory_dumper.h" -#include "common/types.h" -#include "new_op_test_utils.h" - -using namespace std; -using namespace testing; - -namespace ge { - -class UtestModelManagerDataInputer : public testing::Test { - protected: - void SetUp() {} - - void TearDown() {} -}; - -/// InputDataWrapper -/// constructor -/// GetInput -TEST_F(UtestModelManagerDataInputer, inputdatawrapper_construct) { - InputDataWrapper *input_data_wrapper = new InputDataWrapper(); - - input_data_wrapper->GetInput(); - - delete input_data_wrapper; -} - -/// InputDataWrapper -/// Init func with correct input -TEST_F(UtestModelManagerDataInputer, success_inputdatawrapper_init) { - InputDataWrapper *input_data_wrapper = new InputDataWrapper(); - ge::InputData input_data; - ge::OutputData output_data; - Status ret = input_data_wrapper->Init(input_data, output_data); - - EXPECT_EQ(ret, SUCCESS); - - delete input_data_wrapper; - input_data_wrapper = NULL; -} - -} // namespace ge diff --git a/tests/ut/ge/graph/load/new_model_manager_davinci_model_unittest.cc b/tests/ut/ge/graph/load/new_model_manager_davinci_model_unittest.cc deleted file mode 100644 index 38a250ad..00000000 --- a/tests/ut/ge/graph/load/new_model_manager_davinci_model_unittest.cc +++ /dev/null @@ -1,1433 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include "common/debug/log.h" -#include "common/debug/memory_dumper.h" -#include "common/types.h" - -#define private public -#define protected public -#include "graph/compute_graph.h" -#include "graph/utils/graph_utils.h" -#include "graph/model_serialize.h" -#include "graph/load/model_manager/davinci_model.h" -#include "common/properties_manager.h" -#include "common/op/ge_op_utils.h" -#include -#include "runtime/dev.h" -#include "runtime/kernel.h" -#include "cce/fwk_adpt_struct.h" -#include "graph/load/model_manager/task_info/task_info_factory.h" -#include "graph/load/model_manager/task_info/task_info.h" -#include "graph/load/model_manager/task_info/stream_active_task_info.h" -#include "graph/load/model_manager/task_info/stream_switch_task_info.h" -#include "graph/load/model_manager/task_info/profiler_trace_task_info.h" -#include "graph/load/model_manager/task_info/memcpy_async_task_info.h" -#include "graph/load/model_manager/task_info/label_set_task_info.h" -#include "graph/load/model_manager/task_info/kernel_ex_task_info.h" -#include "graph/load/model_manager/task_info/kernel_task_info.h" -#include "graph/load/model_manager/task_info/hccl_task_info.h" -#include "graph/load/model_manager/task_info/fusion_start_task_info.h" -#include "graph/load/model_manager/task_info/fusion_stop_task_info.h" -#include "graph/load/model_manager/task_info/event_record_task_info.h" -#include "graph/load/model_manager/task_info/event_wait_task_info.h" -#include "graph/manager/graph_var_manager.h" -#include "graph/load/model_manager/model_manager.h" -#undef private -#undef protected - -#include "new_op_test_utils.h" -#include "graph/debug/ge_attr_define.h" -using namespace std; -using namespace testing; -using domi::EventExDef; -using domi::KernelContext; -using domi::KernelDef; -using domi::LogTimeStampDef; -using domi::ModelTaskDef; -using domi::StreamActiveDef; -using domi::TaskDef; - -namespace ge { -class UtestModelManagerDavinciModel : public testing::Test { - protected: - void SetUp() {} - - void TearDown() {} -}; - -class DModelListener : public ge::ModelListener { - public: - DModelListener(){}; - uint32_t OnComputeDone(uint32_t model_id, uint32_t data_index, uint32_t resultCode) { - GELOGI("In Call back. OnComputeDone"); - return 0; - } -}; - -shared_ptr g_label_call_back(new DModelListener()); - -static ge::OpDescPtr CreateOpDesc(string name = "", string type = "") { - auto op_desc = std::make_shared(name, type); - op_desc->SetStreamId(0); - op_desc->SetId(0); - - ge::AttrUtils::SetFloat(op_desc, ge::ATTR_NAME_ALPHA, 0); - ge::AttrUtils::SetFloat(op_desc, ge::ATTR_NAME_BETA, 0); - - op_desc->SetWorkspace({}); - ; - op_desc->SetWorkspaceBytes({}); - op_desc->SetInputOffset({}); - op_desc->SetOutputOffset({}); - - ge::AttrUtils::SetListStr(op_desc, ge::ATTR_NAME_WEIGHT_NAME, {}); - ge::AttrUtils::SetInt(op_desc, ge::POOLING_ATTR_MODE, 0); - ge::AttrUtils::SetInt(op_desc, ge::POOLING_ATTR_PAD_MODE, 0); - ge::AttrUtils::SetInt(op_desc, ge::POOLING_ATTR_DATA_MODE, 0); - ge::AttrUtils::SetInt(op_desc, ge::POOLING_ATTR_CEIL_MODE, 0); - ge::AttrUtils::SetInt(op_desc, ge::POOLING_ATTR_NAN_OPT, 0); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_WINDOW, {}); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_PAD, {}); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_STRIDE, {}); - ge::AttrUtils::SetListInt(op_desc, ge::ATTR_NAME_ACTIVE_STREAM_LIST, {1, 1}); - ge::AttrUtils::SetInt(op_desc, ge::ATTR_NAME_STREAM_SWITCH_COND, 0); - ge::AttrUtils::SetInt(op_desc, ge::ATTR_NAME_FRAMEWORK_FWK_TYPE, FMK_TYPE_T); - return op_desc; -} - -// tset failed_rt_free_host -TEST_F(UtestModelManagerDavinciModel, failed_rt_free_host) { - DavinciModel model(0, g_label_call_back); - - OutputData output_data; - - auto op_desc = CreateOpDesc("Pooling", "Pooling"); - op_desc->SetOutputOffset({1}); - op_desc->SetInputOffset({1}); - - { - ge::GeTensorDesc in_desc(ge::GeShape({1, 1, 1, 1})); - ge::TensorUtils::SetSize(in_desc, 16); - ge::TensorUtils::SetOutputTensor(in_desc, false); - ge::TensorUtils::SetInputTensor(in_desc, true); - op_desc->AddInputDesc(in_desc); - } - - { - ge::GeTensorDesc out_desc(ge::GeShape({1, 1, 1, 1})); - ge::TensorUtils::SetSize(out_desc, 16); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetInputTensor(out_desc, false); - op_desc->AddOutputDesc(out_desc); - } - ge::AttrUtils::SetInt(op_desc, ge::POOLING_ATTR_PAD_MODE, cce::CC_PADDING_DIRECTASSIGN); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_PAD, vector({1, 1, 1, 1})); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_WINDOW, vector({1, 1})); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_STRIDE, vector({1, 1})); - - auto compute_graph = make_shared("g"); - auto node = compute_graph->AddNode(op_desc); - - OmeTestOpUtils::InitModel(model); - - model.data_op_list_.push_back(op_desc); - - EXPECT_EQ(ge::INTERNAL_ERROR, model.ReturnResult(1, false, false, &output_data)); -} - -// test modeldef_fail -TEST_F(UtestModelManagerDavinciModel, contruct_modeldef_createfail) { - DavinciModel model(0, g_label_call_back); - - OmeTestOpUtils::InitModel(model); - - auto op_desc = CreateOpDesc("Pooling", "Pooling"); - op_desc->SetOutputOffset({1}); - op_desc->SetInputOffset({1}); - - { - ge::GeTensorDesc in_desc(ge::GeShape({1, 1, 1, 1})); - ge::TensorUtils::SetSize(in_desc, 16); - ge::TensorUtils::SetOutputTensor(in_desc, false); - ge::TensorUtils::SetInputTensor(in_desc, true); - op_desc->AddInputDesc(in_desc); - } - - { - ge::GeTensorDesc out_desc(ge::GeShape({1, 1, 1, 1})); - ge::TensorUtils::SetSize(out_desc, 16); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetInputTensor(out_desc, false); - op_desc->AddOutputDesc(out_desc); - } - ge::AttrUtils::SetInt(op_desc, ge::POOLING_ATTR_PAD_MODE, cce::CC_PADDING_DIRECTASSIGN); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_PAD, vector({1, 1, 1, 1})); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_WINDOW, vector({1, 1})); - ge::AttrUtils::SetListInt(op_desc, ge::POOLING_ATTR_STRIDE, vector({1, 1})); - - model.GetEventList(); -} - -// test CopyInputDataToModel -TEST_F(UtestModelManagerDavinciModel, copy_input_data_to_model_fail) { - DavinciModel model(0, g_label_call_back); - - ge::InputData input_data; - ge::DataBuffer data_buffer; - data_buffer.data = new char[16]; - data_buffer.length = 16; - input_data.index = 0; - input_data.model_id = 1; - input_data.blobs.push_back(data_buffer); - - model.op_list_.clear(); - - delete[](char *) data_buffer.data; -} - -// test StreamNum -TEST_F(UtestModelManagerDavinciModel, streamnum_success) { - DavinciModel *model = new DavinciModel(0, g_label_call_back); - - OmeTestOpUtils::InitModel(*model); - - EXPECT_EQ(0, model->StreamNum()); - EXPECT_EQ(ge::INTERNAL_ERROR, model->ModelRunStart()); - - EXPECT_EQ(ge::SUCCESS, model->ModelRunStop()); - - delete model; -} - -// test EventNum -TEST_F(UtestModelManagerDavinciModel, eventnum_success) { - DavinciModel *model = new DavinciModel(0, g_label_call_back); - - OmeTestOpUtils::InitModel(*model); - - EXPECT_EQ(0, model->EventNum()); - EXPECT_EQ(ge::INTERNAL_ERROR, model->ModelRunStart()); - - EXPECT_EQ(ge::SUCCESS, model->ModelRunStop()); - - delete model; -} - -TEST_F(UtestModelManagerDavinciModel, handlelist_success) { - DavinciModel *model = new DavinciModel(0, g_label_call_back); - - OmeTestOpUtils::InitModel(*model); - - EXPECT_EQ(ge::INTERNAL_ERROR, model->ModelRunStart()); - - EXPECT_EQ(ge::SUCCESS, model->ModelRunStop()); - - delete model; -} - -// test GetEventList -TEST_F(UtestModelManagerDavinciModel, eventlist_success) { - DavinciModel *model = new DavinciModel(0, g_label_call_back); - - OmeTestOpUtils::InitModel(*model); - - EXPECT_EQ(true, model->GetEventList().empty()); - EXPECT_EQ(ge::INTERNAL_ERROR, model->ModelRunStart()); - - EXPECT_EQ(ge::SUCCESS, model->ModelRunStop()); - - delete model; -} - -// test Shrink -TEST_F(UtestModelManagerDavinciModel, shrink_success) { - DavinciModel model(0, g_label_call_back); - OpDescPtr op_desc_ptr = make_shared("Cast", "Cast"); - void *addr = nullptr; - rtMalloc(&addr, 128, RT_MEMORY_HBM); - model.saved_task_addrs_.emplace(op_desc_ptr, addr); - model.Shrink(); - EXPECT_EQ(model.saved_task_addrs_.isEmpty(), true); -} - -// test rtMalloc -TEST_F(UtestModelManagerDavinciModel, failed_reset_device) { - DavinciModel model(0, g_label_call_back); - ge::OutputData output_data; - ge::DataBuffer buf_data; - rtMalloc(&buf_data.data, 128, RT_MEMORY_HBM); - buf_data.length = 128; - output_data.blobs.push_back(buf_data); - EXPECT_EQ(ge::INTERNAL_ERROR, model.ReturnResult(1, true, false, &output_data)); - rtFree(buf_data.data); -} - -// test priority -TEST_F(UtestModelManagerDavinciModel, init_not_support_priority) { - int32_t priority = 8; - DavinciModel model(priority, g_label_call_back); -} - -// test GetInputOutputDescInfo -TEST_F(UtestModelManagerDavinciModel, success_GetInputOutputDescInfo_without_netoutput) { - DavinciModel model(0, g_label_call_back); - - auto op_desc = CreateOpDesc("Data", "Data"); - op_desc->SetOutputOffset({1}); - op_desc->SetInputOffset({1}); - op_desc->SetStreamId(0); - - { - ge::GeTensorDesc in_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(in_desc, false); - ge::TensorUtils::SetInputTensor(in_desc, true); - op_desc->AddInputDesc(in_desc); - } - - { - ge::GeTensorDesc out_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetInputTensor(out_desc, false); - op_desc->AddOutputDesc(out_desc); - } - - op_desc->SetSrcName({"Pooling1", "Pooling0"}); - op_desc->SetSrcIndex({0, 1}); - - auto compute_graph = make_shared("g"); - auto node = compute_graph->AddNode(op_desc); - - model.data_op_list_.push_back(op_desc); - model.output_size_list_.push_back(32); - - model.op_list_[0] = op_desc; - - model.output_op_list_.push_back(op_desc); - - vector input_shapes; - vector output_shapes; - EXPECT_EQ(ge::SUCCESS, model.GetInputOutputDescInfo(input_shapes, output_shapes)); -} - -TEST_F(UtestModelManagerDavinciModel, CopyTensorFromSrcVarNode_input_is_nullptr) { - NodePtr src_node = nullptr; - NodePtr dst_node = nullptr; - DavinciModel model(0, g_label_call_back); - Status ret = model.CopyTensorFromSrcVarNode(src_node, dst_node); - EXPECT_EQ(FAILED, ret); -} - -TEST_F(UtestModelManagerDavinciModel, CopyTensorFromSrcVarNode_success) { - ge::ComputeGraphPtr graph = std::make_shared("default"); - OpDescPtr op_desc_ptr = make_shared("Cast", "Cast"); - GeTensorDesc dims_tensor_desc(GeShape({1, 1, 1, 1}), FORMAT_NCHW, DT_FLOAT16); - GeTensorDesc dims_tensor_desc_in(GeShape({1, 1, 1, 1}), FORMAT_NCHW, DT_FLOAT); - op_desc_ptr->AddInputDesc(dims_tensor_desc_in); - op_desc_ptr->AddOutputDesc(dims_tensor_desc); - - NodePtr src_node = graph->AddNode(op_desc_ptr); - NodePtr dst_node = graph->AddNode(op_desc_ptr); - DavinciModel model(0, g_label_call_back); - Status ret = model.CopyTensorFromSrcVarNode(src_node, dst_node); -} - -TEST_F(UtestModelManagerDavinciModel, CopyVarData_graph_is_nullptr) { - ge::ComputeGraphPtr graph = nullptr; - DavinciModel model(0, g_label_call_back); - Status ret = model.CopyVarData(graph); - EXPECT_EQ(FAILED, ret); -} - -TEST_F(UtestModelManagerDavinciModel, copy_var_data_success) { - ge::ComputeGraphPtr graph = std::make_shared("default"); - OpDescPtr op_desc_ptr = make_shared("Variable", "Variable"); - GeTensorDesc dims_tensor_desc(GeShape({1, 1, 1, 1}), FORMAT_NCHW, DT_FLOAT16); - GeTensorDesc dims_tensor_desc_in(GeShape({1, 1, 1, 1}), FORMAT_NCHW, DT_FLOAT16); - op_desc_ptr->AddInputDesc(dims_tensor_desc_in); - op_desc_ptr->AddOutputDesc(dims_tensor_desc); - - NodePtr src_node = graph->AddNode(op_desc_ptr); - (void)ge::AttrUtils::SetStr(src_node->GetOpDesc(), "_copy_from_var_node", "abc"); - (void)ge::AttrUtils::SetBool(src_node->GetOpDesc(), "_copy_value", false); - - DavinciModel model(0, g_label_call_back); - Status ret = model.CopyVarData(graph); -} - -TEST_F(UtestModelManagerDavinciModel, get_input_output_desc_info_without_data_op_list) { - DavinciModel model(0, g_label_call_back); - vector input_list; - vector output_list; - Status ret = model.GetInputOutputDescInfo(input_list, output_list); - EXPECT_EQ(SUCCESS, ret); -} - -// test GetInputOutputDescInfo -TEST_F(UtestModelManagerDavinciModel, success_get_input_output_descInfo_with_net_output) { - DavinciModel model(0, g_label_call_back); - - auto op_desc = CreateOpDesc("Data", "Data"); - op_desc->SetOutputOffset({1}); - op_desc->SetInputOffset({1}); - op_desc->SetStreamId(0); - - { - ge::GeTensorDesc in_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(in_desc, false); - ge::TensorUtils::SetInputTensor(in_desc, true); - op_desc->AddInputDesc(in_desc); - } - - { - ge::GeTensorDesc out_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetInputTensor(out_desc, false); - op_desc->AddOutputDesc(out_desc); - } - op_desc->SetSrcName({"Pooling1", "Pooling0"}); - op_desc->SetSrcIndex({0, 1}); - - auto compute_graph = make_shared("g"); - auto data_node = compute_graph->AddNode(op_desc); - - model.data_op_list_.push_back(op_desc); - - op_desc->SetType("NetOutput"); - - auto no_node = compute_graph->AddNode(op_desc); - - model.op_list_[0] = op_desc; - - model.output_op_list_.push_back(op_desc); - model.output_size_list_.push_back(32); - - vector input_shapes; - vector output_shapes; - EXPECT_EQ(ge::SUCCESS, model.GetInputOutputDescInfo(input_shapes, output_shapes)); -} - -TEST_F(UtestModelManagerDavinciModel, success_get_input_output_desc_info_for_zero_copy_with_net_output) { - DavinciModel model(0, g_label_call_back); - - auto op_desc = CreateOpDesc("Data", "Data"); - op_desc->SetOutputOffset({1}); - op_desc->SetInputOffset({1}); - op_desc->SetStreamId(0); - - { - ge::GeTensorDesc in_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(in_desc, false); - ge::TensorUtils::SetInputTensor(in_desc, true); - op_desc->AddInputDesc(in_desc); - } - - { - ge::GeTensorDesc out_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetInputTensor(out_desc, false); - op_desc->AddOutputDesc(out_desc); - } - - op_desc->SetSrcName({"Pooling1", "Pooling0"}); - op_desc->SetSrcIndex({0, 1}); - - auto compute_graph = make_shared("g"); - auto data_node = compute_graph->AddNode(op_desc); - - model.data_op_list_.push_back(op_desc); - - op_desc->SetType("NetOutput"); - - auto net_out_node = compute_graph->AddNode(op_desc); - model.op_list_[0] = op_desc; - - model.output_op_list_.push_back(op_desc); - model.output_size_list_.push_back(32); - model.output_memory_size_list_.push_back(64); - - vector input_shapes; - vector output_shapes; - EXPECT_EQ(ge::SUCCESS, model.GetInputOutputDescInfoForZeroCopy(input_shapes, output_shapes)); -} - -TEST_F(UtestModelManagerDavinciModel, success_get_input_output_desc_info_dim_size_not4) { - DavinciModel model(0, g_label_call_back); - - auto op_desc = CreateOpDesc("Data", "Data"); - op_desc->SetOutputOffset({1}); - op_desc->SetInputOffset({1}); - op_desc->SetStreamId(0); - - { - ge::GeTensorDesc in_desc(ge::GeShape({1, 1, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(in_desc, false); - ge::TensorUtils::SetInputTensor(in_desc, true); - op_desc->AddInputDesc(in_desc); - } - - { - ge::GeTensorDesc out_desc(ge::GeShape({1, 1, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetInputTensor(out_desc, false); - op_desc->AddOutputDesc(out_desc); - } - - op_desc->SetSrcName({"Pooling1", "Pooling0"}); - op_desc->SetSrcIndex({0, 1}); - - auto compute_graph = make_shared("g"); - auto data_node = compute_graph->AddNode(op_desc); - - model.data_op_list_.push_back(op_desc); - - op_desc->SetType("NetOutput"); - - auto net_out_node = compute_graph->AddNode(op_desc); - model.op_list_[0] = op_desc; - - model.output_op_list_.push_back(op_desc); - model.output_size_list_.push_back(32); - - vector input_shapes; - vector output_shapes; - EXPECT_EQ(ge::SUCCESS, model.GetInputOutputDescInfo(input_shapes, output_shapes)); -} - -// test GetLabelList -TEST_F(UtestModelManagerDavinciModel, get_label_list_success) { - DavinciModel model(0, g_label_call_back); - OmeTestOpUtils::InitModel(model); - vector label_list; - model.label_list_ = label_list; - EXPECT_EQ(label_list, model.GetLabelList()); -} - -// test GetInputListSize -TEST_F(UtestModelManagerDavinciModel, get_label_list_size_success) { - DavinciModel model(0, g_label_call_back); - OmeTestOpUtils::InitModel(model); - vector data_op_list; - data_op_list.push_back(std::make_shared()); - model.data_op_list_ = data_op_list; -} - -// test GetFlowctrlOpList -TEST_F(UtestModelManagerDavinciModel, get_flow_ctrl_op_list_success) { - DavinciModel model(0, g_label_call_back); - OmeTestOpUtils::InitModel(model); - std::map flowctrl_op_index_internal_map; - flowctrl_op_index_internal_map.insert(pair(1, 1)); - model.flowctrl_op_index_internal_map_ = flowctrl_op_index_internal_map; -} - -// test SetFlowctrlOpList -TEST_F(UtestModelManagerDavinciModel, get_flow_ctrl_index_success) { - DavinciModel model(0, g_label_call_back); - OmeTestOpUtils::InitModel(model); - EXPECT_EQ(0, model.GetFlowctrlIndex(0)); - EXPECT_EQ(1, model.GetFlowctrlIndex(0)); - EXPECT_EQ(0, model.GetFlowctrlIndex(1)); - EXPECT_EQ(1, model.GetFlowctrlIndex(1)); - EXPECT_EQ(2, model.GetFlowctrlIndex(0)); -} - -// test GetRegisterStub -TEST_F(UtestModelManagerDavinciModel, success_get_register_stub) { - DavinciModel model(0, g_label_call_back); - OmeTestOpUtils::InitModel(model); - std::string binfile = "tvmbin"; - string ret = model.GetRegisterStub(binfile); - EXPECT_EQ("tvmbin", ret); - model.tvm_bin_kernel_.insert("tvmbin"); - ret = model.GetRegisterStub(binfile); - EXPECT_EQ("tvmbin", ret); -} - -// test InitTbeHandle -TEST_F(UtestModelManagerDavinciModel, success_init_tbe_handle) { - DavinciModel model(0, g_label_call_back); - OmeTestOpUtils::InitModel(model); - std::shared_ptr op_desc = std::make_shared(); - Status ret = model.InitTbeHandle(op_desc); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); -} - -// test InitTVMTask failed -TEST_F(UtestModelManagerDavinciModel, init_tvm_task_failed1) { - DavinciModel model(0, g_label_call_back); - uint16_t offset = 0; - TaskDef *task_def = new TaskDef(); - KernelDef *kernel_def = task_def->mutable_kernel(); - map op_list; - model.op_list_ = op_list; - - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - Status ret = kernel_task_info->InitTVMTask(&model, offset, kernel_def[0]); - EXPECT_EQ(INTERNAL_ERROR, ret); - task_def->clear_kernel(); - delete kernel_task_info; - delete task_def; -} - -TEST_F(UtestModelManagerDavinciModel, kernel_taskInfo_init_cce_task_failed1) { - DavinciModel model(0, g_label_call_back); - - TaskDef *task_def = new TaskDef(); - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - KernelDef *kernel_def = task_def->mutable_kernel(); - Status ret = kernel_task_info->InitCceTask(&model, kernel_def[0]); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); - task_def->clear_kernel(); - delete kernel_task_info; - delete task_def; -} - -// test SetContext success -TEST_F(UtestModelManagerDavinciModel, success_kernel_taskInfo_init_set_context) { - DavinciModel model(0, g_label_call_back); - - TaskDef *task_def = new TaskDef(); - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - KernelDef *kernel_def = task_def->mutable_kernel(); - KernelContext *context = kernel_def->mutable_context(); - context->set_op_id(1); - context->set_kernel_func_id(1); - context->set_is_flowtable(true); - context->set_args_count(1); - context->set_args_offset("args111111", 10); - - Status ret = kernel_task_info->SetContext(kernel_def[0]); - EXPECT_EQ(ge::SUCCESS, ret); - - ret = kernel_task_info->Release(); - EXPECT_EQ(ge::SUCCESS, ret); - kernel_def->clear_context(); - task_def->clear_kernel(); - delete kernel_task_info; - delete task_def; -} - -// test SetContext failed -TEST_F(UtestModelManagerDavinciModel, kernel_taskInfo_init_set_context_failed1) { - DavinciModel model(0, g_label_call_back); - - TaskDef *task_def = new TaskDef(); - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - KernelDef *kernel_def = task_def->mutable_kernel(); - KernelContext *context = kernel_def->mutable_context(); - context->set_op_id(1); - context->set_kernel_func_id(1); - context->set_is_flowtable(true); - context->set_args_count(0); - Status ret = kernel_task_info->SetContext(kernel_def[0]); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); - - kernel_def->clear_context(); - task_def->clear_kernel(); - delete kernel_task_info; - delete task_def; -} - -TEST_F(UtestModelManagerDavinciModel, kernel_taskInfo_init_set_context_failed2) { - DavinciModel model(0, g_label_call_back); - - TaskDef *task_def = new TaskDef(); - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - KernelDef *kernel_def = task_def->mutable_kernel(); - KernelContext *context = kernel_def->mutable_context(); - context->set_op_id(1); - context->set_kernel_func_id(1); - context->set_is_flowtable(true); - context->set_args_count(5); - context->set_args_offset("\0\0"); // args_offset = 0 - - Status ret = kernel_task_info->SetContext(kernel_def[0]); - EXPECT_EQ(ge::PARAM_INVALID, ret); - - kernel_def->clear_context(); - task_def->clear_kernel(); - delete kernel_task_info; - delete task_def; -} - -// test success DistributeDumpTask -TEST_F(UtestModelManagerDavinciModel, success_distribute_dump_task) { - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - KernelDef *kernel_def = task_def->mutable_kernel(); - - kernel_def->set_stub_func("kerneltaskinfo"); - kernel_def->set_block_dim(10); - kernel_def->set_args("args111111", 10); - kernel_def->set_args_size(10); - rtSmDesc_t l2CtrlInfo; - l2CtrlInfo.data[0].L2_mirror_addr = 1024; - kernel_def->set_sm_desc((void *)&l2CtrlInfo, sizeof(rtSmDesc_t)); - - // for SetStream - rtStream_t stream = nullptr; - rtStreamCreate(&stream, 0); - std::vector stream_list; - stream_list.push_back(stream); - Status ret = kernel_task_info->SetStream(0, stream_list); - EXPECT_EQ(SUCCESS, ret); - - ret = kernel_task_info->Release(); - EXPECT_EQ(SUCCESS, ret); - rtStreamDestroy(stream); - task_def->clear_kernel(); - delete kernel_task_info; - delete task_def; -} - -// test success GetTaskID -TEST_F(UtestModelManagerDavinciModel, success_get_task_id) { - ModelTaskDef *model_task_def = new ModelTaskDef(); - TaskDef *task = model_task_def->add_task(); - task->set_type(RT_MODEL_TASK_KERNEL); - TaskInfoPtr task_info = TaskInfoFactory::Instance().Create(static_cast(task->type())); - - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - uint32_t ret = task_info->GetTaskID(); - EXPECT_EQ(0, ret); - ret = kernel_task_info->GetTaskID(); - EXPECT_EQ(0, ret); - HcclTaskInfo *hccl_task_info = new HcclTaskInfo(); - ret = hccl_task_info->GetTaskID(); - EXPECT_EQ(0, ret); - - delete hccl_task_info; - delete kernel_task_info; - delete model_task_def; -} - -// test StoreInputOutputTensor success -TEST_F(UtestModelManagerDavinciModel, success_store_input_output_tensor) { - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - KernelTaskInfo *kernel_task_info = new KernelTaskInfo(); - - std::vector input_data_addrs; - std::vector output_data_addrs; - std::vector<::tagCcAICPUTensor> input_descs; - std::vector<::tagCcAICPUTensor> output_descs; - - int test = 1; - int *addr = &test; - void *input; - void *output; - input = addr; - output = addr; - input_data_addrs.push_back(&input); - output_data_addrs.push_back(output); - - tagCcAICPUTensor input_desc; - tagCcAICPUTensor output_desc; - input_descs.push_back(input_desc); - output_descs.push_back(output_desc); - - Status ret = kernel_task_info->StoreInputOutputTensor(input_data_addrs, output_data_addrs, input_descs, output_descs); - EXPECT_EQ(SUCCESS, ret); - ret = kernel_task_info->Release(); - EXPECT_EQ(SUCCESS, ret); - delete kernel_task_info; - delete task_def; -} - -// test init EventRecordTaskInfo -TEST_F(UtestModelManagerDavinciModel, success_event_record_task_init) { - DavinciModel *model1 = nullptr; - TaskDef *task_def1 = new TaskDef(); - EventRecordTaskInfo *eventRecordTaskInfo1 = new EventRecordTaskInfo(); - Status ret1 = eventRecordTaskInfo1->Init(task_def1[0], model1); - EXPECT_EQ(PARAM_INVALID, ret1); - - delete eventRecordTaskInfo1; - delete task_def1; - delete model1; - DavinciModel model(0, g_label_call_back); - - ModelTaskDef *model_task_info = new ModelTaskDef(); - TaskDef *task = model_task_info->add_task(); - task->set_type(RT_MODEL_TASK_EVENT_RECORD); - TaskInfoPtr task_info = TaskInfoFactory::Instance().Create(static_cast(task->type())); - - task->stream_id_ = 0; - rtStream_t rt_stream; - rtStreamCreate(&rt_stream, 1); - vector stream_list; - stream_list.push_back(rt_stream); - model.stream_list_ = stream_list; - - task->set_event_id(1); - model.runtime_param_.event_num = 1; - Status ret = task_info->Init(task[0], &model); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); - - model.runtime_param_.event_num = 2; - rtEvent_t event1; - rtEvent_t event2; - rtEventCreate(&event1); - rtEventCreate(&event2); - model.event_list_.push_back(event1); - model.event_list_.push_back(event2); - - EventExDef *event_ex_def = task->mutable_event_ex(); - event_ex_def->set_event_type(1); - - ret = task_info->Init(task[0], &model); - EXPECT_EQ(SUCCESS, ret); - - task->clear_event_ex(); - task_info->Release(); - delete model_task_info; -} - -// test init EventWaitTaskInfo -TEST_F(UtestModelManagerDavinciModel, success_event_wait_task_init) { - DavinciModel *model1 = nullptr; - TaskDef *task_def1 = new TaskDef(); - EventWaitTaskInfo *event_wait_task_info1 = new EventWaitTaskInfo(); - Status ret1 = event_wait_task_info1->Init(task_def1[0], model1); - EXPECT_EQ(PARAM_INVALID, ret1); - - delete event_wait_task_info1; - delete task_def1; - delete model1; - DavinciModel model(0, g_label_call_back); - - ModelTaskDef *model_task_info = new ModelTaskDef(); - TaskDef *task = model_task_info->add_task(); - task->set_type(RT_MODEL_TASK_EVENT_WAIT); - TaskInfoPtr task_info = TaskInfoFactory::Instance().Create(static_cast(task->type())); - - task->stream_id_ = 0; - rtStream_t rt_stream; - rtStreamCreate(&rt_stream, 1); - vector stream_list; - stream_list.push_back(rt_stream); - model.stream_list_ = stream_list; - - task->set_event_id(1); - model.runtime_param_.event_num = 1; - Status ret = task_info->Init(task[0], &model); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); - - model.runtime_param_.event_num = 2; - rtEvent_t event1; - rtEvent_t event2; - rtEventCreate(&event1); - rtEventCreate(&event2); - model.event_list_.push_back(event1); - model.event_list_.push_back(event2); - - EventExDef *event_ex_def = task->mutable_event_ex(); - event_ex_def->set_event_type(1); - - ret = task_info->Init(task[0], &model); - EXPECT_EQ(SUCCESS, ret); - - task->clear_event_ex(); - task_info->Release(); - delete model_task_info; -} - -// test fusion_start_task Init -TEST_F(UtestModelManagerDavinciModel, success_fusion_start_task_init) { - DavinciModel *model1 = nullptr; - TaskDef *task_def1 = new TaskDef(); - FusionStartTaskInfo *fusion_start_task_info1 = new FusionStartTaskInfo(); - Status ret1 = fusion_start_task_info1->Init(task_def1[0], model1); - EXPECT_EQ(PARAM_INVALID, ret1); - - delete fusion_start_task_info1; - delete task_def1; - delete model1; - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - FusionStartTaskInfo *fusion_start_task_info = new FusionStartTaskInfo(); - task_def->set_stream_id(0); - rtStream_t stream; - rtStreamCreate(&stream, 0); - model.stream_list_.push_back(stream); - - Status ret = fusion_start_task_info->Init(task_def[0], &model); - EXPECT_EQ(SUCCESS, ret); - delete fusion_start_task_info; - delete task_def; -} - -// test fusion_end_task Init -TEST_F(UtestModelManagerDavinciModel, success_fusion_end_task_rinit) { - DavinciModel *model1 = nullptr; - TaskDef *task_def1 = new TaskDef(); - FusionStopTaskInfo *fusion_stop_task_info1 = new FusionStopTaskInfo(); - Status ret1 = fusion_stop_task_info1->Init(task_def1[0], model1); - EXPECT_EQ(PARAM_INVALID, ret1); - - delete fusion_stop_task_info1; - delete task_def1; - delete model1; - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - FusionStopTaskInfo *fusion_stop_task_info = new FusionStopTaskInfo(); - task_def->set_stream_id(0); - rtStream_t stream; - rtStreamCreate(&stream, 0); - model.stream_list_.push_back(stream); - - Status ret = fusion_stop_task_info->Init(task_def[0], &model); - EXPECT_EQ(SUCCESS, ret); - delete fusion_stop_task_info; - delete task_def; -} - -// test kernel_ex_task_Release -TEST_F(UtestModelManagerDavinciModel, success_kernel_ex_task_release) { - KernelExTaskInfo *kernel_ex_task_info = new KernelExTaskInfo(); - Status ret = kernel_ex_task_info->Release(); - EXPECT_EQ(SUCCESS, ret); - - delete kernel_ex_task_info; -} - -// test hccl_Distribute -TEST_F(UtestModelManagerDavinciModel, success_Distribute7) { - DavinciModel model(0, g_label_call_back); - - ModelTaskDef *model_task_def = new ModelTaskDef(); - TaskDef *task7 = model_task_def->add_task(); - task7->set_type(RT_MODEL_TASK_HCCL); - TaskInfoPtr task_info7 = TaskInfoFactory::Instance().Create(static_cast(task7->type())); - Status ret = task_info7->Init(task7[0], &model); - EXPECT_EQ(FAILED, ret); - - std::vector task_list; - task_list.push_back(task_info7); - model.task_list_ = task_list; - - task_info7->Release(); - delete model_task_def; -} - -// test hccl_GetPrivateDefByTaskDef -TEST_F(UtestModelManagerDavinciModel, success_hccl_get_private_def_by_task_def) { - DavinciModel model(0, g_label_call_back); - - ModelTaskDef *model_task_def = new ModelTaskDef(); - TaskDef *task7 = model_task_def->add_task(); - task7->set_type(RT_MODEL_TASK_HCCL); - // for SetStream - rtStream_t stream = nullptr; - rtStreamCreate(&stream, 0); - model.stream_list_.push_back(stream); - // for GetPrivateDefByTaskDef - task7->set_ops_kernel_store_ptr(10); - std::string value = "hccl_task"; - task7->set_private_def(value); - - TaskInfoPtr task_info7 = TaskInfoFactory::Instance().Create(static_cast(task7->type())); - // for Distribute - Status ret = task_info7->Init(task7[0], &model); - EXPECT_EQ(ge::PARAM_INVALID, ret); - - task_info7->Release(); - delete model_task_def; -} - -// test hccl_task_TransToGETaskInfo -TEST_F(UtestModelManagerDavinciModel, success_hccl_trans_to_ge_task_info) { - DavinciModel model(0, g_label_call_back); - - ModelTaskDef *model_task_def = new ModelTaskDef(); - TaskDef *task7 = model_task_def->add_task(); - // for type - task7->set_type(RT_MODEL_TASK_HCCL); - TaskInfoPtr task_info7 = TaskInfoFactory::Instance().Create(static_cast(task7->type())); - - GETaskInfo ge_task; - HcclTaskInfo *hccl_task_info = new HcclTaskInfo(); - hccl_task_info->TransToGETaskInfo(ge_task); - - delete hccl_task_info; - delete model_task_def; -} - -// test stream_active_task Init -TEST_F(UtestModelManagerDavinciModel, success_stream_active_task_init) { - DavinciModel *model1 = nullptr; - TaskDef *task_def1 = new TaskDef(); - StreamActiveTaskInfo *stream_active_task_info1 = new StreamActiveTaskInfo(); - Status ret1 = stream_active_task_info1->Init(task_def1[0], model1); - EXPECT_EQ(PARAM_INVALID, ret1); - delete stream_active_task_info1; - delete task_def1; - delete model1; - - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - task_def->set_stream_id(0); - rtStream_t stream1, stream2; - rtStreamCreate(&stream1, 0); - rtStreamCreate(&stream2, 0); - model.stream_list_.push_back(stream1); - - StreamActiveTaskInfo *stream_active_task_info = new StreamActiveTaskInfo(); - - StreamActiveDef *stream_active_def = task_def->mutable_stream_active(); - stream_active_def->set_op_index(0); - stream_active_def->set_active_stream_id(0); - - std::map flowctrl; - flowctrl.insert(pair(1, 1)); - model.flowctrl_op_index_internal_map_ = flowctrl; - - auto opDef = CreateOpDesc("", ""); - model.op_list_[0] = opDef; - - Status ret = stream_active_task_info->Init(task_def[0], &model); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); // line 51 - - model.stream_list_.push_back(stream2); - ret = stream_active_task_info->Init(task_def[0], &model); - EXPECT_EQ(SUCCESS, ret); - - task_def->clear_stream_active(); - delete stream_active_task_info; - delete task_def; -} - -// test label_set_task Init -TEST_F(UtestModelManagerDavinciModel, success_label_set_task_init) { - DavinciModel *model1 = nullptr; - TaskDef *task_def1 = new TaskDef(); - LabelSetTaskInfo *label_set_task_info1 = new LabelSetTaskInfo(); - Status ret1 = label_set_task_info1->Init(task_def1[0], model1); - EXPECT_EQ(PARAM_INVALID, ret1); - delete label_set_task_info1; - delete task_def1; - delete model1; - - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - LabelSetTaskInfo *label_set_task_info = new LabelSetTaskInfo(); - task_def->set_stream_id(0); - rtStream_t stream; - rtStreamCreate(&stream, 0); - model.stream_list_.push_back(stream); - - task_def->set_label_id(1); - model.runtime_param_.batch_num = 0; - Status ret = label_set_task_info->Init(task_def[0], &model); - EXPECT_EQ(PARAM_INVALID, ret); - - task_def->clear_label_id(); - task_def->set_label_id(0); - model.runtime_param_.batch_num = 1; - rtLabel_t label; - rtLabelCreate(&label); - model.label_list_.push_back(label); - - ret = label_set_task_info->Init(task_def[0], &model); - EXPECT_EQ(SUCCESS, ret); - delete label_set_task_info; - delete task_def; -} - -// test label_goto_task init -TEST_F(UtestModelManagerDavinciModel, success_label_goto_task_init) { - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - LabelGotoTaskInfo *label_goto_task_info = new LabelGotoTaskInfo(); - task_def->set_stream_id(0); - - rtStream_t stream; - rtStreamCreate(&stream, 0); - model.stream_list_.push_back(stream); - - rtLabel_t label; - rtLabelCreate(&label); - model.label_list_.push_back(label); - - Status ret = label_goto_task_info->Init(task_def[0], &model); - EXPECT_EQ(SUCCESS, ret); - - delete label_goto_task_info; - delete task_def; -} - -// test profiler_trace_task init -TEST_F(UtestModelManagerDavinciModel, success_profiler_trace_task_init) { - DavinciModel *model1 = nullptr; - TaskDef *task_def1 = new TaskDef(); - ProfilerTraceTaskInfo *profiler_trace_task_info1 = new ProfilerTraceTaskInfo(); - Status ret1 = profiler_trace_task_info1->Init(task_def1[0], model1); - EXPECT_EQ(PARAM_INVALID, ret1); - - delete profiler_trace_task_info1; - delete task_def1; - delete model1; - DavinciModel model(0, g_label_call_back); - TaskDef *task_def = new TaskDef(); - task_def->set_stream_id(0); - rtStream_t stream; - rtStreamCreate(&stream, 0); - model.stream_list_.push_back(stream); - LogTimeStampDef *logTimeStampDef = task_def->mutable_log_timestamp(); - logTimeStampDef->set_logid(1); - logTimeStampDef->set_notify(1); - logTimeStampDef->set_flat(1); - ProfilerTraceTaskInfo *profiler_trace_task_info = new ProfilerTraceTaskInfo(); - Status ret = profiler_trace_task_info->Init(task_def[0], &model); - EXPECT_EQ(SUCCESS, ret); - - task_def->clear_log_timestamp(); - delete profiler_trace_task_info; - delete task_def; -} - -TEST_F(UtestModelManagerDavinciModel, profiling_model_success) { - rtStream_t stream = nullptr; - rtStreamCreate(&stream, 0); - - DavinciModel model(0, g_label_call_back); - model.model_id_ = 1; - model.name_ = "test"; - model.version_ = 0x01; - - model.stream_list_.push_back(stream); - - ge::ModelData data; - rtMallocHost(&data.model_data, 128); - data.model_len = 128; - - ModelDef *model_def = new ModelDef(); - auto op_def = CreateOpDesc("", "Data"); - op_def->SetInputOffset({1}); - op_def->SetOutputOffset({100}); - - ge::GeTensorDesc descin(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT); - ge::TensorUtils::SetSize(descin, 4); - op_def->AddInputDesc(descin); - ge::GeTensorDesc desc_out(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetSize(desc_out, 32); - op_def->AddInputDesc(desc_out); - op_def->SetId(0); - - model.data_op_list_.push_back(op_def); - model.op_list_[0] = op_def; - - auto opdef1 = CreateOpDesc("", "Relu"); - opdef1->SetInputOffset({1}); - opdef1->SetOutputOffset({100}); - - ge::GeTensorDesc desc_in1(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT); - ge::TensorUtils::SetSize(desc_in1, 4); - opdef1->AddInputDesc(desc_in1); - ge::GeTensorDesc desc_out1(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetSize(desc_out1, 32); - opdef1->AddInputDesc(desc_out1); - op_def->SetId(1); - - model.op_list_[1] = opdef1; - - auto opdef2 = CreateOpDesc("", "Relu"); - opdef2->SetInputOffset({1}); - opdef2->SetOutputOffset({100}); - - ge::GeTensorDesc desc_in2(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT); - ge::TensorUtils::SetSize(desc_in2, 4); - opdef2->AddInputDesc(desc_in2); - ge::GeTensorDesc desc_out2(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetSize(desc_out2, 32); - opdef2->AddInputDesc(desc_out2); - op_def->SetId(2); - - model.op_list_[2] = opdef2; - - auto opdef3 = CreateOpDesc("", "Relu"); - opdef3->SetInputOffset({1}); - opdef3->SetOutputOffset({100}); - - ge::GeTensorDesc desc_in3(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT); - ge::TensorUtils::SetSize(desc_in3, 4); - opdef3->AddInputDesc(desc_in3); - ge::GeTensorDesc desc_out3(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetSize(desc_out3, 32); - opdef3->AddInputDesc(desc_out3); - op_def->SetId(3); - - model.op_list_[3] = opdef3; - - auto opdef4 = CreateOpDesc("", "Relu"); - opdef4->SetInputOffset({1}); - opdef4->SetOutputOffset({100}); - - ge::GeTensorDesc desc_in4(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT); - ge::TensorUtils::SetSize(desc_in4, 4); - opdef4->AddInputDesc(desc_in4); - ge::GeTensorDesc desc_out4(ge::GeShape({1, 1, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT16); - ge::TensorUtils::SetSize(desc_out4, 32); - opdef4->AddInputDesc(desc_out4); - op_def->SetId(4); - - model.op_list_[4] = opdef4; - - ge::InputData input_data; - ge::DataBuffer data_buffer; - data_buffer.data = new char[4]; - data_buffer.length = 4; - input_data.index = 0; - input_data.model_id = 1; - input_data.blobs.push_back(data_buffer); - - rtFreeHost(data.model_data); - delete[](char *) data_buffer.data; - delete model_def; -} - -TEST_F(UtestModelManagerDavinciModel, success_output_list_0) { - DavinciModel model(0, g_label_call_back); - - uint32_t version = 0; - uint64_t session_id = 0; - uint32_t device_id = 0; - uint64_t job_id = 0; - Status ret = VarManager::Instance(session_id)->Init(version, session_id, device_id, job_id); - EXPECT_EQ(ret, ge::SUCCESS); - - ret = model.ReturnNoOutput(1); - EXPECT_EQ(ret, ge::SUCCESS); - - VarManagerPool::Instance().Destroy(); -} - -// test dyncbatch_distributeTask_SUCCESS -TEST_F(UtestModelManagerDavinciModel, dyncbatch_distribute_task_success) { - DavinciModel model(0, g_label_call_back); - - rtStream_t stream = nullptr; - rtStreamCreate(&stream, 0); - - rtLabel_t label = nullptr; - rtLabelCreate(&label); - model.label_list_.push_back(label); - rtLabelCreate(&label); - model.label_list_.push_back(label); - rtLabelCreate(&label); - model.label_list_.push_back(label); - - rtLabelDestroy(label); - rtStreamDestroy(stream); -} - -// test GetOutputDescInfo -TEST_F(UtestModelManagerDavinciModel, success_get_output_desc_info_with_netoutput) { - setenv("GE_TRAIN", "1", true); - DavinciModel model(0, g_label_call_back); - - auto op_desc = CreateOpDesc("Data", "Data"); - op_desc->SetOutputOffset({1}); - op_desc->SetInputOffset({1}); - op_desc->SetStreamId(0); - - { - ge::GeTensorDesc in_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_FRACTAL_Z, ge::DT_FLOAT16); - ge::TensorUtils::SetOutputTensor(in_desc, false); - ge::TensorUtils::SetInputTensor(in_desc, true); - op_desc->AddInputDesc(in_desc); - } - - { - ge::GeTensorDesc out_desc(ge::GeShape({1, 1, 10, 10}), ge::FORMAT_NCHW, ge::DT_FLOAT); - ge::TensorUtils::SetOutputTensor(out_desc, true); - ge::TensorUtils::SetInputTensor(out_desc, false); - op_desc->AddOutputDesc(out_desc); - } - - op_desc->SetSrcName({"Pooling1", "Pooling0"}); - op_desc->SetSrcIndex({0, 1}); - - auto compute_graph = make_shared("g"); - - op_desc->SetType("NetOutput"); - - auto net_out_node = compute_graph->AddNode(op_desc); - model.op_list_[0] = op_desc; - - model.output_op_list_.push_back(op_desc); - model.output_size_list_.push_back(32); - model.output_memory_size_list_.push_back(64); - - vector output_shapes; - vector formats; - EXPECT_EQ(ge::SUCCESS, model.GetOutputDescInfo(output_shapes, formats)); - - setenv("GE_TRAIN", "0", true); -} - -TEST_F(UtestModelManagerDavinciModel, device_runtime_success_Run) { - rtStream_t stream = nullptr; - rtStreamCreate(&stream, 0); - - DavinciModel model(0, g_label_call_back); - - model.stream_list_.push_back(stream); - auto model_def = make_shared(); - - auto op_def = CreateOpDesc("", "Data"); - - auto compute_graph = make_shared("g"); - compute_graph->AddNode(op_def); - - model_def->SetGraph(ge::GraphUtils::CreateGraphFromComputeGraph(compute_graph)); - - model.data_op_list_.push_back(op_def); - - model.data_inputer_ = new DataInputer(); - - model.ModelRunStart(); - - OutputData output_data; - ge::InputData input_data; - - ge::DataBuffer data_buffer; - data_buffer.data = new char[16]; - data_buffer.length = 16; - - input_data.index = 0; - input_data.model_id = 1; - input_data.blobs.push_back(data_buffer); - - model.ModelRunStop(); - - delete[](char *) data_buffer.data; -} - -TEST_F(UtestModelManagerDavinciModel, run_failed) { - rtStream_t stream = nullptr; - rtStreamCreate(&stream, 0); - - DavinciModel model(0, g_label_call_back); - - model.stream_list_.push_back(stream); - auto model_def = make_shared(); - - auto op_def = CreateOpDesc("", "Data"); - - auto compute_graph = make_shared("g"); - compute_graph->AddNode(op_def); - - model_def->SetGraph(ge::GraphUtils::CreateGraphFromComputeGraph(compute_graph)); - - model.data_op_list_.push_back(op_def); - - model.data_inputer_ = new DataInputer(); - - model.ModelRunStart(); - - OutputData output_data; - ge::InputData input_data; - - ge::DataBuffer data_buffer; - data_buffer.data = new char[16]; - data_buffer.length = 16; - - input_data.index = 0; - input_data.model_id = 1; - input_data.blobs.push_back(data_buffer); - - model.ModelRunStop(); - delete[](char *) data_buffer.data; -} - -TEST_F(UtestModelManagerDavinciModel, run_failed01) { - rtStream_t stream = nullptr; - rtStreamCreate(&stream, 0); - - DavinciModel model(0, g_label_call_back); - - model.stream_list_.push_back(stream); - auto model_def = make_shared(); - - auto op_def = CreateOpDesc("", "Data"); - - auto compute_graph = make_shared("g"); - compute_graph->AddNode(op_def); - - model_def->SetGraph(ge::GraphUtils::CreateGraphFromComputeGraph(compute_graph)); - - model.data_op_list_.push_back(op_def); - - model.data_inputer_ = nullptr; - model.ModelRunStart(); - - model.ModelRunStop(); -} - -TEST_F(UtestModelManagerDavinciModel, init_tbe_handle_fe_registered) { - DavinciModel::tvm_bin_kernel_.clear(); - DavinciModel model(0, g_label_call_back); - OpDescPtr op_desc = CreateOpDesc("MatMul", "MatMul"); - - std::vector kernelBin; - TBEKernelPtr tbe_kernel = std::make_shared("name/MatMul", std::move(kernelBin)); - op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel); - - std::string kernel_name("kernel/MatMul"); - AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name); - - EXPECT_EQ(model.InitTbeHandle(op_desc), SUCCESS); - EXPECT_EQ(model.InitTbeHandle(op_desc), SUCCESS); - - EXPECT_EQ(model.used_tbe_handle_map_.size(), 0); - DavinciModel::tvm_bin_kernel_.clear(); -} - -TEST_F(UtestModelManagerDavinciModel, init_tbe_handle_ge_registered) { - DavinciModel::tvm_bin_kernel_.clear(); - DavinciModel model(0, g_label_call_back); - OpDescPtr op_desc = CreateOpDesc("MatMul", "MatMul"); - - std::vector kernelBin; - TBEKernelPtr tbe_kernel = std::make_shared("name/MatMul", std::move(kernelBin)); - op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel); - - std::string kernel_name("kernel/MatMul"); - AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name); - - string session_graph_id; - AttrUtils::GetStr(op_desc, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id); - const char *bin_file_key = DavinciModel::GetRegisterStub(op_desc->GetName(), session_graph_id); - model.used_tbe_handle_map_[bin_file_key] = 1; // test first register. - - EXPECT_EQ(model.InitTbeHandle(op_desc), SUCCESS); - EXPECT_EQ(model.InitTbeHandle(op_desc), SUCCESS); - - EXPECT_EQ(model.used_tbe_handle_map_.size(), 1); - - auto it = model.used_tbe_handle_map_.find(bin_file_key); - EXPECT_NE(it, model.used_tbe_handle_map_.end()); - EXPECT_EQ(it->second, 3); - DavinciModel::tvm_bin_kernel_.clear(); -} -} // namespace ge diff --git a/tests/ut/ge/graph/load/new_model_manager_event_manager_unittest.cc b/tests/ut/ge/graph/load/new_model_manager_event_manager_unittest.cc deleted file mode 100644 index ee708501..00000000 --- a/tests/ut/ge/graph/load/new_model_manager_event_manager_unittest.cc +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 - -#include "common/debug/log.h" -#include "common/debug/memory_dumper.h" -#include "common/types.h" - -#define private public -#include "graph/manager/model_manager/event_manager.h" -#undef private - -using namespace ge; -using namespace std; -using namespace testing; - -class UtestModelManagerEventManager : public testing::Test { - protected: - void SetUp() {} - - void TearDown() {} -}; - -// test repeat initialize -TEST_F(UtestModelManagerEventManager, repeat_initialization) { - ge::EventManager event_manager; - size_t event_num = 1; - event_manager.Init(event_num); - Status ret = event_manager.Init(event_num); - EXPECT_EQ(ret, SUCCESS); -} - -TEST_F(UtestModelManagerEventManager, call_event_record_normal) { - ge::EventManager event_manager; - size_t event_num = 1; - Status ret = event_manager.Init(event_num); - EXPECT_EQ(SUCCESS, ret); - EXPECT_NE(event_manager.event_list_.size(), 0); - - ret = event_manager.EventRecord(0, NULL); - EXPECT_EQ(SUCCESS, ret); -} - -// test load EventRecore when uninited -TEST_F(UtestModelManagerEventManager, call_event_record_while_uninited) { - ge::EventManager event_manager; - Status ret = event_manager.EventRecord(1, NULL); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); -} - -// test with invalid param when load EventRecord -TEST_F(UtestModelManagerEventManager, call_event_record_with_invalid_param) { - ge::EventManager event_manager; - Status ret = event_manager.Init(1); - EXPECT_EQ(SUCCESS, ret); - ret = event_manager.EventRecord(1, NULL); - EXPECT_EQ(ge::PARAM_INVALID, ret); -} - -// test load EventElapsedTime when uninited -TEST_F(UtestModelManagerEventManager, call_event_elapsed_time_while_uninited) { - ge::EventManager event_manager; - float time = .0f; - Status ret = event_manager.EventElapsedTime(1, 2, time); - EXPECT_EQ(ge::INTERNAL_ERROR, ret); -} - -// test with invalid param when load EventElapsedTime -TEST_F(UtestModelManagerEventManager, call_event_elapsed_time_with_invalid_param) { - ge::EventManager *event_manager = new ge::EventManager; - size_t event_num = 2; - Status ret = event_manager->Init(event_num); - EXPECT_EQ(SUCCESS, ret); - float time = .0f; - - // normal load - ret = event_manager->EventElapsedTime(0, 1, time); - EXPECT_EQ(SUCCESS, ret); - - // startevent_idx overstep boundary - ret = event_manager->EventElapsedTime(2, 1, time); - EXPECT_EQ(ge::PARAM_INVALID, ret); - - // stopevent_idx overstep boundary - ret = event_manager->EventElapsedTime(1, 2, time); - EXPECT_EQ(ge::PARAM_INVALID, ret); - - // startevent_idx > stopevent_idx - ret = event_manager->EventElapsedTime(1, 0, time); - EXPECT_EQ(ge::PARAM_INVALID, ret); - - delete event_manager; -} -TEST_F(UtestModelManagerEventManager, call_get_event) { - ge::EventManager event_manager; - size_t event_num = 1; - event_manager.Init(event_num); - rtEvent_t event = nullptr; - Status ret = event_manager.GetEvent(2, event); - EXPECT_EQ(ge::PARAM_INVALID, ret); - ret = event_manager.GetEvent(0, event); - EXPECT_EQ(SUCCESS, ret); -} diff --git a/tests/ut/ge/graph/load/new_model_manager_task_build_unittest.cc b/tests/ut/ge/graph/load/new_model_manager_task_build_unittest.cc deleted file mode 100644 index f10ccd7f..00000000 --- a/tests/ut/ge/graph/load/new_model_manager_task_build_unittest.cc +++ /dev/null @@ -1,115 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 - -#include "common/debug/log.h" -#include "common/debug/memory_dumper.h" -#include "common/types.h" -#include "new_op_test_utils.h" -#include "graph/debug/ge_attr_define.h" -#include "graph/utils/attr_utils.h" -#include "graph/detail/model_serialize_imp.h" -#include "proto/ge_ir.pb.h" - -#define private public -#define protected public -#include "graph/compute_graph.h" -#include "graph/utils/graph_utils.h" -#include "graph/model_serialize.h" -#include "graph/load/model_manager/davinci_model.h" -#include "common/properties_manager.h" -#include "common/op/ge_op_utils.h" -#include -#include "runtime/dev.h" -#include "runtime/kernel.h" -#include "cce/fwk_adpt_struct.h" -#undef private -#undef protected - -using namespace std; -using namespace testing; - -namespace ge { -class UtestModelManagerTaskBuilder : public testing::Test { - protected: - void SetUp() {} - - void TearDown() {} - - /// data weight - /// | | | | - /// |-conv-| | | - /// | | | - /// conv2d | - /// | | - /// |-resApply - - void BuildGraph(ComputeGraphPtr graph) { - OpDescPtr data = std::make_shared("DATA1", "data"); - OpDescPtr weight = std::make_shared("WEIGHT", "weight"); - OpDescPtr conv_op = std::make_shared("conv", "conv"); - OpDescPtr conv_2D = std::make_shared("conv_2D", "conv2d"); - OpDescPtr res_apply_op = std::make_shared("res_apply_op", "resapply"); - // add descriptor - vector dim(4, 4); - GeShape shape(dim); - GeTensorDesc out_desc(shape); - int32_t blockSize = 4096; - - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 1); - data->AddOutputDesc(out_desc); - - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 2); - weight->AddOutputDesc(out_desc); - - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 1); - conv_op->AddInputDesc(out_desc); - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 2); - conv_op->AddInputDesc(out_desc); - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 3); - conv_op->AddOutputDesc(out_desc); - - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 3); - conv_2D->AddInputDesc(out_desc); - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 2); - conv_2D->AddInputDesc(out_desc); - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 4); - conv_2D->AddOutputDesc(out_desc); - - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 4); - res_apply_op->AddInputDesc(out_desc); - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 1); - res_apply_op->AddInputDesc(out_desc); - ge::TensorUtils::SetDataOffset(out_desc, blockSize * 5); - res_apply_op->AddOutputDesc(out_desc); - - NodePtr data_node = graph->AddNode(data); - NodePtr weigth_node = graph->AddNode(weight); - NodePtr conv_node = graph->AddNode(conv_op); - NodePtr conv_2D_node = graph->AddNode(conv_2D); - NodePtr res_node = graph->AddNode(res_apply_op); - - GraphUtils::AddEdge(data_node->GetOutDataAnchor(0), conv_node->GetInDataAnchor(0)); - GraphUtils::AddEdge(weigth_node->GetOutDataAnchor(0), conv_node->GetInDataAnchor(1)); - GraphUtils::AddEdge(conv_node->GetOutDataAnchor(0), conv_2D_node->GetInDataAnchor(0)); - GraphUtils::AddEdge(weigth_node->GetOutDataAnchor(0), conv_2D_node->GetInDataAnchor(1)); - GraphUtils::AddEdge(conv_2D_node->GetOutDataAnchor(0), res_node->GetInDataAnchor(0)); - GraphUtils::AddEdge(weigth_node->GetOutDataAnchor(0), res_node->GetInDataAnchor(1)); - return; - } -}; -} // namespace ge diff --git a/tests/ut/ge/graph/load/output_net_output_unittest.cc b/tests/ut/ge/graph/load/output_net_output_unittest.cc deleted file mode 100644 index 97246dad..00000000 --- a/tests/ut/ge/graph/load/output_net_output_unittest.cc +++ /dev/null @@ -1,300 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include - -#include "securec.h" - -#define protected public -#define private public -#include "common/debug/memory_dumper.h" -#include "common/op/ge_op_utils.h" -#include "graph/load/model_manager/davinci_model.h" -#include "graph/load/model_manager/model_utils.h" -#include "graph/manager/graph_var_manager.h" -#include "new_op_test_utils.h" -#include "proto/om.pb.h" - -using namespace std; - -namespace ge { -class UtestNetOutput : public testing::Test { - protected: - void TearDown() {} - shared_ptr GenOpdef(OpDescPtr &op_desc, int flag) { - shared_ptr builder = make_shared(op_desc); - builder->SetStreamId(0); - builder->AddInput(1); - builder->SetType("NetOutput"); - - if (flag == 1) { - auto input_desc_1 = builder->AddInputDesc({1, 1, 10, 10}, FORMAT_NCHW, DT_FLOAT16); - } - auto input_desc_1 = builder->AddInputDesc({1, 1, 10, 10}, FORMAT_NCHW, DT_FLOAT16); - - if (flag == 2) { - auto input_desc_2 = builder->AddInputDesc({1, 1, 10, 10}, FORMAT_NCHW, DT_FLOAT16); - } - if (flag == 3) { - builder->AddInput(10); - } - - return builder; - } - shared_ptr GenOpdef2(OpDescPtr &op_desc) { - shared_ptr builder = make_shared(op_desc); - builder->SetStreamId(0); - builder->SetType("NetOutput"); - builder->AddInput(10); - - auto input_desc_1 = builder->AddInputDesc({64, 32, 5, 5}, FORMAT_FRACTAL_Z, DT_FLOAT); - - builder->AddInput(1000000); - auto input_desc_2 = builder->AddInputDesc({1, 10, 10, 1}, FORMAT_NHWC, DT_FLOAT); - - builder->AddOutput(2000000); - auto output_desc_1 = builder->AddOutputDesc({64, 32, 5, 5}, FORMAT_NCHW, DT_FLOAT); - - builder->AddOutput(2100000); - output_desc_1 = builder->AddOutputDesc({1, 10, 10, 1}, FORMAT_NHWC, DT_FLOAT); - - return builder; - } - - public: - shared_ptr dav_model_; -}; - -TEST_F(UtestNetOutput, test_get_input_size) { - shared_ptr custom_op_desc = make_shared(); - OmeTestOpDescBuilder builder(custom_op_desc); - builder.SetName("netoutput"); - builder.SetStreamId(0); - builder.SetType("NetOutput"); - - auto input_desc_1 = builder.AddInputDesc({1, 1, 1, 1}, FORMAT_FRACTAL_Z, DT_FLOAT); - builder.AddInput(1); - auto output_desc = builder.AddOutputDesc({1, 1, 1, 1}, FORMAT_NCHW, DT_FLOAT); - builder.AddOutput(1); - builder.Finish(); - - vector v_output_size = ModelUtils::GetInputSize(custom_op_desc); - EXPECT_EQ(v_output_size.size(), 1); -} - -// test ModelUtils::IsOutput -TEST_F(UtestNetOutput, success_is_output) { - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - OmeTestOpDescBuilder builder(op_desc); - builder.SetType("NetOutput"); - vector outputs_desc; - std::shared_ptr desc = std::make_shared(); - outputs_desc.push_back(desc); - op_desc->outputs_desc_ = outputs_desc; - bool ret = model_utils->IsOutput(op_desc); - EXPECT_EQ(false, ret); - - delete model_utils; -} - -// test ModelUtils::IsOutput -TEST_F(UtestNetOutput, true_is_output) { - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - OmeTestOpDescBuilder builder(op_desc); - builder.SetType("NetOutput"); - vector outputs_desc; - std::shared_ptr desc = std::make_shared(); - outputs_desc.push_back(desc); - op_desc->outputs_desc_ = outputs_desc; - ge::TensorUtils::SetOutputTensor(*(outputs_desc[0].get()), true); - bool ret = model_utils->IsOutput(op_desc); - EXPECT_EQ(true, ret); - - delete model_utils; -} - -// test ModelUtils::IsInputTensorNeedTrans -TEST_F(UtestNetOutput, success_is_output_tensor_need_trans) { - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - OmeTestOpDescBuilder builder(op_desc); - builder.SetType("NetOutput"); - size_t tensor_index = 1; - vector outputs_desc; - std::shared_ptr desc = std::make_shared(); - outputs_desc.push_back(desc); - op_desc->outputs_desc_ = outputs_desc; - op_desc->inputs_desc_ = outputs_desc; - - bool ret = model_utils->IsInputTensorNeedTrans(op_desc, tensor_index); - EXPECT_EQ(false, ret); - - delete model_utils; -} - -// test ModelUtils::GetOutputSize -TEST_F(UtestNetOutput, success_get_output_size) { - vector v_output_size; - - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - vector outputs_desc; - std::shared_ptr desc = std::make_shared(); - outputs_desc.push_back(desc); - op_desc->outputs_desc_ = outputs_desc; - EXPECT_EQ(v_output_size, model_utils->GetOutputSize(op_desc)); - - vector output = {1}; - op_desc->SetOutputOffset(output); - uint32_t tensor_size = 0; - v_output_size.push_back(tensor_size); - EXPECT_EQ(v_output_size, model_utils->GetOutputSize(op_desc)); - delete model_utils; -} - -// test ModelUtils::GetWorkspaceSize -TEST_F(UtestNetOutput, success_get_workspace_size) { - vector v_workspace_size; - - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - vector workspace = {1}; - op_desc->SetWorkspace(workspace); - EXPECT_EQ(v_workspace_size, model_utils->GetWorkspaceSize(op_desc)); - - op_desc->SetWorkspaceBytes(workspace); - v_workspace_size.push_back(1); - EXPECT_EQ(v_workspace_size, model_utils->GetWorkspaceSize(op_desc)); - delete model_utils; -} - -// test ModelUtils::GetWeightSize -TEST_F(UtestNetOutput, success_get_weight_size) { - vector v_weight_size; - - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - op_desc->SetType("Const"); - EXPECT_EQ(v_weight_size, model_utils->GetWeightSize(op_desc)); - - op_desc->SetType("NetOutput"); - vector inputs_desc; - std::shared_ptr desc = std::make_shared(); - inputs_desc.push_back(desc); - op_desc->inputs_desc_ = inputs_desc; - - vector is_input_const = {true}; - op_desc->SetIsInputConst(is_input_const); - v_weight_size.push_back(0); - EXPECT_EQ(v_weight_size, model_utils->GetWeightSize(op_desc)); - - delete model_utils; -} - -// test ModelUtils::GetWeights -TEST_F(UtestNetOutput, success_get_weights) { - vector v_weights; - - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - op_desc->SetType("Const"); - EXPECT_EQ(v_weights, model_utils->GetWeights(op_desc)); - - op_desc->SetType("NetOutput"); - vector inputs_desc; - std::shared_ptr desc = std::make_shared(); - inputs_desc.push_back(desc); - op_desc->inputs_desc_ = inputs_desc; - - vector is_input_const = {true}; - op_desc->SetIsInputConst(is_input_const); - GeTensorDesc tensor_desc; - EXPECT_EQ(v_weights, model_utils->GetWeights(op_desc)); - - delete model_utils; -} - -// test ModelUtils::GetInputDescs -TEST_F(UtestNetOutput, success_get_input_descs) { - vector<::opTensor_t> v_input_descs; - vector<::tagCcAICPUTensor> ret; - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - ret = model_utils->GetInputDescs(op_desc); - EXPECT_EQ(v_input_descs.size(), ret.size()); - - vector inputs_desc; - std::shared_ptr desc = std::make_shared(); - inputs_desc.push_back(desc); - op_desc->inputs_desc_ = inputs_desc; - vector is_input_const = {false}; - op_desc->SetIsInputConst(is_input_const); - - opTensor_t tmp; - tmp.format = OP_TENSOR_FORMAT_NC1HWC0; - tmp.dim_cnt = 0; - tmp.data_type = OP_DATA_FLOAT; - v_input_descs.push_back(tmp); - ret = model_utils->GetInputDescs(op_desc); - EXPECT_EQ(v_input_descs.size(), ret.size()); - - delete model_utils; -} - -// test ModelUtils::GetOutputDescs -TEST_F(UtestNetOutput, success_get_output_descs) { - vector<::opTensor_t> v_output_descs; - vector<::tagCcAICPUTensor> ret; - ModelUtils *model_utils = new ModelUtils(); - std::shared_ptr op_desc = std::make_shared(); - ret = model_utils->GetOutputDescs(op_desc); - EXPECT_EQ(v_output_descs.size(), ret.size()); - - vector outputs_desc; - std::shared_ptr desc = std::make_shared(); - outputs_desc.push_back(desc); - op_desc->outputs_desc_ = outputs_desc; - - opTensor_t tmp; - tmp.format = OP_TENSOR_FORMAT_NC1HWC0; - tmp.dim_cnt = 0; - tmp.data_type = OP_DATA_FLOAT; - v_output_descs.push_back(tmp); - ret = model_utils->GetOutputDescs(op_desc); - EXPECT_EQ(v_output_descs.size(), ret.size()); - - delete model_utils; -} - -// test Output::GetOutputData -TEST_F(UtestNetOutput, success_get_output_data) { - Output *output = new Output(nullptr, nullptr); - output->v_input_data_addr_.push_back((void *)1); - output->v_input_size_.push_back(1); - output->input_num_ = 1; - - vector v_data_addr; - vector v_data_size; - output->GetOutputData(v_data_addr, v_data_size); - - EXPECT_EQ(output->v_input_data_addr_, v_data_addr); - EXPECT_EQ(output->v_input_size_, v_data_size); - delete output; -} -} // namespace ge diff --git a/tests/ut/ge/graph/manager/graph_manager_unittest.cc b/tests/ut/ge/graph/manager/graph_manager_unittest.cc index 518cfdcd..b40690e2 100644 --- a/tests/ut/ge/graph/manager/graph_manager_unittest.cc +++ b/tests/ut/ge/graph/manager/graph_manager_unittest.cc @@ -30,9 +30,6 @@ #define protected public #define private public #include "graph/manager/graph_manager.h" -#define const -#include "common/helper/model_cache_helper.h" -#undef const #include "init/gelib.h" #include "common/math/math_util.h"