You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ge_generator.cc 50 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "framework/generator/ge_generator.h"
  17. #include <atomic>
  18. #include "common/ge/ge_util.h"
  19. #include "common/ge/plugin_manager.h"
  20. #include "framework/common/helper/model_helper.h"
  21. #include "framework/common/helper/om_file_helper.h"
  22. #include "framework/common/util.h"
  23. #include "common/util/error_manager/error_manager.h"
  24. #include "framework/common/debug/ge_log.h"
  25. #include "framework/common/debug/log.h"
  26. #include "external/ge/ge_api.h"
  27. #include "graph/debug/ge_attr_define.h"
  28. #include "graph/ge_context.h"
  29. #include "graph/manager/graph_manager.h"
  30. #include "graph/manager/util/rt_context_util.h"
  31. #include "graph/operator_factory_impl.h"
  32. #include "graph/opsproto_manager.h"
  33. #include "graph/utils/graph_utils.h"
  34. #include "graph/utils/type_utils.h"
  35. #include "init/gelib.h"
  36. #include "common/model/ge_model.h"
  37. #include "analyzer/analyzer.h"
  38. using std::map;
  39. using std::string;
  40. using std::vector;
  41. namespace {
  42. const char *const kAttrOpType = "op_type";
  43. const char *const kEngineNameDefault = "default";
  44. const char *const kVectorEngine = "VectorEngine";
  45. const char *const kAIcoreEngine = "AIcoreEngine";
  46. const char *const kFileNameSuffix = "online";
  47. const char *const kAicpuAllshape = "_AllShape";
  48. constexpr char const *kAttrSupportDynamicShape = "support_dynamicshape";
  49. const int64_t kDynamicDimValue = -2;
  50. const int kDefaultDeviceId = 0;
  51. const int kDefaultJobId = 0;
  52. const int32_t kFuzzBuildPattern = 1;
  53. std::map<ge::OpEngineType, std::string> engine_type_map{
  54. {ge::ENGINE_SYS, kEngineNameDefault},
  55. {ge::ENGINE_AICORE, kAIcoreEngine},
  56. {ge::ENGINE_VECTOR, kVectorEngine}};
  57. bool ContainsDynamicInpus(const ge::OpDesc &op_desc) {
  58. for (auto &tensor_desc : op_desc.GetAllInputsDescPtr()) {
  59. if (tensor_desc->MutableShape().IsUnknownShape()) {
  60. GELOGI("Contains unknown shape input. set is_dynamic_input to true.");
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. // if optional in/out, format is format_reserved and dtype is dt_undefined
  67. bool IsOptional(const ge::GeTensorDesc &tensor_desc) {
  68. return tensor_desc.GetFormat() == ge::FORMAT_RESERVED && tensor_desc.GetDataType() == ge::DT_UNDEFINED;
  69. }
  70. } // namespace
  71. namespace ge {
  72. static Status CheckEngineTypeSupport(const NodePtr &node, OpEngineType engine_type) {
  73. const OpDescPtr &op_desc = node->GetOpDesc();
  74. GE_CHECK_NOTNULL_EXEC(op_desc, return PARAM_INVALID);
  75. if (engine_type == ENGINE_SYS) {
  76. GELOGI("CheckEngineType: use default engine.");
  77. return SUCCESS;
  78. }
  79. // get op engine name
  80. string op_engine_name;
  81. auto iter = engine_type_map.find(engine_type);
  82. if (iter != engine_type_map.end()) {
  83. op_engine_name = iter->second;
  84. GELOGI("CheckEngineType: engine type: %d", static_cast<int>(engine_type));
  85. } else {
  86. ErrorManager::GetInstance().ATCReportErrMessage("E14001", {"opname", "optype", "value", "reason"},
  87. {op_desc->GetName(), op_desc->GetType(), "engine type",
  88. "it only support default/AIcoreEngine/VectorEngine"});
  89. GELOGE(FAILED, "[Check][Param] value:%d not support, "
  90. "only support default/AIcoreEngine/VectorEngine now", static_cast<int>(engine_type));
  91. return FAILED;
  92. }
  93. if (op_desc->HasAttr(ATTR_NAME_UNREGST_OPPATH)) {
  94. op_desc->SetOpEngineName(op_engine_name);
  95. op_desc->SetOpKernelLibName(op_engine_name);
  96. return SUCCESS;
  97. }
  98. // set op engine name and opkernelLib. when engine support
  99. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  100. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  101. REPORT_INNER_ERROR("E19999", "get gelib failed, as get instance failed or initflag failed.");
  102. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] CheckEngineType failed, as get gelib failed.");
  103. return FAILED;
  104. }
  105. OpsKernelManager &ops_kernel_manager = instance_ptr->OpsKernelManagerObj();
  106. std::vector<OpInfo> op_infos = ops_kernel_manager.GetOpsKernelInfo(op_desc->GetType());
  107. if (op_infos.empty()) {
  108. ErrorManager::GetInstance().ATCReportErrMessage("E14001", {"opname", "optype", "value", "reason"},
  109. {op_desc->GetName(), op_desc->GetType(), "optype", "it can not find"});
  110. GELOGE(FAILED, "[Get][OpInfo] by op type %s failed.", op_desc->GetType().c_str());
  111. return FAILED;
  112. }
  113. string kernel_name;
  114. for (const auto &it : op_infos) {
  115. if (it.engine == op_engine_name) {
  116. kernel_name = it.opKernelLib;
  117. break;
  118. }
  119. }
  120. if (kernel_name.empty()) {
  121. ErrorManager::GetInstance().ATCReportErrMessage("E14001", {"opname", "optype", "value", "reason"},
  122. {op_desc->GetName(), op_desc->GetType(), "engine name" + FmtToStr(op_engine_name), "it can not find"});
  123. GELOGE(FAILED, "[Check][Param] Can not find ops kernel, engine name:%s. op:%s(%s)",
  124. op_engine_name.c_str(), op_desc->GetName().c_str(), op_desc->GetType().c_str());
  125. return FAILED;
  126. }
  127. auto &kernel_map = ops_kernel_manager.GetAllOpsKernelInfoStores();
  128. auto kernel_info_store = kernel_map.find(kernel_name);
  129. if (kernel_info_store != kernel_map.end()) {
  130. std::string unsupported_reason;
  131. if (kernel_info_store->second->CheckSupported(node, unsupported_reason)) {
  132. op_desc->SetOpEngineName(op_engine_name);
  133. op_desc->SetOpKernelLibName(kernel_name);
  134. GELOGI("CheckEngineType:Set OpKernelLibName %s and engine name %s into op_desc %s", kernel_name.c_str(),
  135. op_engine_name.c_str(), op_desc->GetName().c_str());
  136. return SUCCESS;
  137. } else {
  138. ErrorManager::GetInstance().ATCReportErrMessage(
  139. "E13002", {"optype", "opskernel", "reason"}, {op_desc->GetType(), kernel_name, unsupported_reason});
  140. GELOGE(FAILED, "[Call][CheckSupported] failed, Op type %s of ops kernel %s is unsupported, reason:%s",
  141. op_desc->GetType().c_str(), kernel_name.c_str(), unsupported_reason.c_str());
  142. return FAILED;
  143. }
  144. } else {
  145. ErrorManager::GetInstance().ATCReportErrMessage(
  146. "E13003", {"opname", "optype"}, {op_desc->GetName(), op_desc->GetType()});
  147. GELOGE(FAILED, "[Check][Param] Can not find any supported ops kernel info store by kernel_name %s,"
  148. "op type is %s, op name is %s",
  149. kernel_name.c_str(), op_desc->GetType().c_str(), op_desc->GetName().c_str());
  150. }
  151. return FAILED;
  152. }
  153. static Status AddInputs(const ComputeGraphPtr &graph, const NodePtr &node, const GeTensorDesc &tensor, int32_t index,
  154. bool attr, int32_t &data_index) {
  155. GE_CHECK_NOTNULL_EXEC(graph, return PARAM_INVALID);
  156. GE_CHECK_NOTNULL_EXEC(node, return PARAM_INVALID);
  157. auto format = tensor.GetFormat();
  158. auto data_type = tensor.GetDataType();
  159. if (format == FORMAT_RESERVED && data_type == DT_UNDEFINED) {
  160. return SUCCESS;
  161. }
  162. string op_type;
  163. bool is_const = false;
  164. (void)AttrUtils::GetBool(tensor, CONST_ATTR_NAME_INPUT, is_const);
  165. if (is_const) {
  166. GELOGD("Get input[%d] is const", index);
  167. op_type = CONSTANTOP;
  168. } else if (!AttrUtils::GetStr(tensor, kAttrOpType, op_type) || op_type.empty()) {
  169. op_type = DATA;
  170. }
  171. string op_name = node->GetName() + "_in_" + std::to_string(index);
  172. OpDescPtr data_op = MakeShared<ge::OpDesc>(op_name, op_type);
  173. if (data_op == nullptr) {
  174. REPORT_CALL_ERROR("E19999", "create OpDesc failed, name:%s", op_name.c_str());
  175. GELOGE(FAILED, "[Create][OpDesc] failed, name:%s", op_name.c_str());
  176. return FAILED;
  177. }
  178. if (is_const) {
  179. ConstGeTensorPtr tensor_value;
  180. if (!AttrUtils::GetTensor(tensor, ge::ATTR_NAME_WEIGHTS, tensor_value)) {
  181. REPORT_CALL_ERROR("E19999", "get attr %s failed, tensor:%s.",
  182. ge::ATTR_NAME_WEIGHTS.c_str(), tensor.GetName().c_str());
  183. GELOGE(FAILED, "[Get][Attr] %s failed, tensor:%s.", ge::ATTR_NAME_WEIGHTS.c_str(), tensor.GetName().c_str());
  184. return FAILED;
  185. }
  186. if (!AttrUtils::SetTensor(data_op, ge::ATTR_NAME_WEIGHTS, tensor_value)) {
  187. REPORT_CALL_ERROR("E19999", "set attr %s failed, op:%s.", ge::ATTR_NAME_WEIGHTS.c_str(), op_name.c_str());
  188. GELOGE(FAILED, "[Set][Attr] %s failed, op:%s.", ge::ATTR_NAME_WEIGHTS.c_str(), op_name.c_str());
  189. return FAILED;
  190. }
  191. }
  192. (void)AttrUtils::SetBool(data_op, "_is_single_op", true);
  193. (void)AttrUtils::SetBool(data_op, ATTR_NAME_IS_ORIGINAL_INPUT, true);
  194. GE_CHK_BOOL_EXEC(data_op->AddInputDesc(tensor) == GRAPH_SUCCESS,
  195. REPORT_CALL_ERROR("E19999", "AddInputDesc failed for node:%s", data_op->GetName().c_str());
  196. return FAILED, "[Add][InputDesc] fail for node:%s", data_op->GetName().c_str());
  197. GE_CHK_BOOL_EXEC(data_op->AddOutputDesc(tensor) == GRAPH_SUCCESS,
  198. REPORT_CALL_ERROR("E19999", "AddOutputDesc failed for node:%s", data_op->GetName().c_str());
  199. return FAILED, "[Add][OutputDesc] fail for node:%s", data_op->GetName().c_str());
  200. if (attr && !is_const) {
  201. GE_CHK_BOOL_EXEC(AttrUtils::SetInt(data_op, ATTR_NAME_INDEX, data_index),
  202. REPORT_CALL_ERROR("E19999", "set attr %s failed for node:%s",
  203. ATTR_NAME_INDEX.c_str(), data_op->GetName().c_str());
  204. return FAILED,
  205. "[Set][Attr:%s] fail for node:%s", ATTR_NAME_INDEX.c_str(), data_op->GetName().c_str());
  206. ++data_index;
  207. }
  208. ge::NodePtr arg_node = graph->AddNode(data_op);
  209. GE_CHK_BOOL_EXEC(arg_node != nullptr,
  210. REPORT_CALL_ERROR("E19999", "add node:%s to graph:%s failed", data_op->GetName().c_str(),
  211. graph->GetName().c_str());
  212. return FAILED, "[Add][Node] Insert Data node:%s fail", data_op->GetName().c_str());
  213. GE_CHK_STATUS(GraphUtils::AddEdge(arg_node->GetOutDataAnchor(0), node->GetInDataAnchor(index)),
  214. "[Add][Edge]fail from node:%s to node:%s", data_op->GetName().c_str(), node->GetName().c_str());
  215. return SUCCESS;
  216. }
  217. static Status AddOutputs(const ComputeGraphPtr &graph, const NodePtr &node, const vector<GeTensor> &outputs) {
  218. OpDescPtr op_desc = MakeShared<ge::OpDesc>(graph->GetName() + "_" + NODE_NAME_NET_OUTPUT, NETOUTPUT);
  219. if (op_desc == nullptr) {
  220. REPORT_CALL_ERROR("E19999", "create OpDesc failed, graph:%s", graph->GetName().c_str());
  221. GELOGE(FAILED, "[Create][OpDesc] failed, graph:%s", graph->GetName().c_str());
  222. return FAILED;
  223. }
  224. (void)AttrUtils::SetBool(op_desc, "_is_single_op", true);
  225. int32_t count = 0;
  226. for (const auto &out_desc : outputs) {
  227. GeTensorDesc tensor = out_desc.GetTensorDesc();
  228. TensorUtils::SetInputTensor(tensor, true);
  229. GE_CHK_BOOL_EXEC(op_desc->AddInputDesc(tensor) == GRAPH_SUCCESS,
  230. REPORT_CALL_ERROR("E19999", "AddInputDesc failed for node:%s", op_desc->GetName().c_str());
  231. return FAILED, "[Add][InputDesc]fail for node:%s", op_desc->GetName().c_str());
  232. TensorUtils::SetInputTensor(tensor, false);
  233. TensorUtils::SetOutputTensor(tensor, true);
  234. GE_CHK_BOOL_EXEC(op_desc->AddOutputDesc(tensor) == GRAPH_SUCCESS,
  235. REPORT_CALL_ERROR("E19999", "AddOutputDesc failed for node:%s", op_desc->GetName().c_str());
  236. return FAILED, "[Add][OutputDesc]fail for node:%s", op_desc->GetName().c_str());
  237. count++;
  238. }
  239. GE_CHECK_NOTNULL_EXEC(graph, return PARAM_INVALID);
  240. ge::NodePtr out_node = graph->AddNode(op_desc);
  241. GE_CHK_BOOL_EXEC(out_node != nullptr,
  242. REPORT_CALL_ERROR("E19999", "add node:%s to graph:%u failed.",
  243. op_desc->GetName().c_str(), graph->GetGraphID());
  244. return FAILED,
  245. "[Add][Node:%s]fail in graph:%u", op_desc->GetName().c_str(), graph->GetGraphID());
  246. GE_CHECK_NOTNULL_EXEC(node, return PARAM_INVALID);
  247. for (int32_t i = 0; i < count; ++i) {
  248. GE_CHK_STATUS(GraphUtils::AddEdge(node->GetOutDataAnchor(i), out_node->GetInDataAnchor(i)),
  249. "[Add][Edge]fail from node:%s to node:%s", node->GetName().c_str(), out_node->GetName().c_str());
  250. }
  251. return SUCCESS;
  252. }
  253. static void GetOpsProtoPath(string &opsproto_path) {
  254. const char *path_env = std::getenv("ASCEND_OPP_PATH");
  255. if (path_env != nullptr) {
  256. string path = path_env;
  257. string file_path = RealPath(path.c_str());
  258. if (file_path.empty()) {
  259. REPORT_CALL_ERROR("E19999", "File path %s is invalid.", path.c_str());
  260. GELOGE(FAILED, "[Call][RealPath] File path %s is invalid.", path.c_str());
  261. return;
  262. }
  263. opsproto_path = (path + "/op_proto/custom/" + ":") + (path + "/op_proto/built-in/");
  264. GELOGI("Get opsproto so path from env : %s", path.c_str());
  265. return;
  266. }
  267. string path_base = PluginManager::GetPath();
  268. GELOGI("path_base is %s", path_base.c_str());
  269. path_base = path_base.substr(0, path_base.rfind('/'));
  270. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  271. opsproto_path = (path_base + "ops/op_proto/custom/" + ":") + (path_base + "ops/op_proto/built-in/");
  272. }
  273. static Status ResetTensorVecShape(const vector<GeTensor> &inputs, vector<GeTensor> &inputs_dynamic) {
  274. for (auto input : inputs) {
  275. auto input_desc = input.GetTensorDesc();
  276. GeShape shape_ori = input_desc.GetShape();
  277. std::vector<int64_t> dynamic_shape_dims = {kDynamicDimValue};
  278. GeShape dynamic_shape(dynamic_shape_dims);
  279. std::vector<std::pair<int64_t, int64_t>> dynamic_shape_range;
  280. ge::GeTensor inputTensor;
  281. ge::GeTensorDesc desc(input_desc);
  282. bool is_const = false;
  283. (void)AttrUtils::GetBool(input_desc, CONST_ATTR_NAME_INPUT, is_const);
  284. if (!is_const) {
  285. int64_t storage_format = FORMAT_NCHW;
  286. if (ge::AttrUtils::GetInt(desc, ge::ATTR_NAME_STORAGE_FORMAT, storage_format) &&
  287. !ge::AttrUtils::SetListInt(desc, ge::ATTR_NAME_STORAGE_SHAPE, dynamic_shape_dims)) {
  288. REPORT_CALL_ERROR("E19999", "Set attr ATTR_NAME_STORAGE_SHAPE failed to op:%s.", desc.GetName().c_str());
  289. GELOGE(FAILED, "[Set][Attr] ATTR_NAME_STORAGE_SHAPE fail.");
  290. return FAILED;
  291. }
  292. desc.SetShape(dynamic_shape);
  293. desc.SetShapeRange(dynamic_shape_range);
  294. }
  295. inputTensor.SetTensorDesc(desc);
  296. inputs_dynamic.push_back(inputTensor);
  297. }
  298. return SUCCESS;
  299. }
  300. static Status GetFuzzBuildAttrs(const OpDescPtr &op_desc, const GeRootModelPtr &ge_root_model,
  301. GeAttrValue::LIST_NAMED_ATTRS &fuzz_build_attrs) {
  302. GELOGD("Start get fuzz build attrs of %s.", op_desc->GetName().c_str());
  303. GE_CHECK_NOTNULL(ge_root_model->GetRootGraph());
  304. for (const auto &node : ge_root_model->GetRootGraph()->GetAllNodes()) {
  305. GE_CHECK_NOTNULL(node);
  306. GE_CHECK_NOTNULL(node->GetOpDesc());
  307. GELOGD("Delete fuzz build attr of %s after build.", node->GetName().c_str());
  308. node->GetOpDesc()->DelAttr(ATTR_NAME_FUZZ_BUILD);
  309. }
  310. (void)AttrUtils::GetListNamedAttrs(op_desc, ATTR_NAME_FUZZ_BUILD_RES_ATTRS, fuzz_build_attrs);
  311. if (!fuzz_build_attrs.empty()) {
  312. GELOGD("%s has split, get ATTR_NAME_FUZZ_BUILD_RES_ATTRS directly.", op_desc->GetName().c_str());
  313. return SUCCESS;
  314. } else {
  315. GELOGW("%s build with fuzz build pattern, but not set ATTR_NAME_FUZZ_BUILD_RES_ATTRS.", op_desc->GetName().c_str());
  316. }
  317. return SUCCESS;
  318. }
  319. static bool HasShapeRange(const vector<GeTensor> &inputs) {
  320. for (const auto &input : inputs) {
  321. vector<pair<int64_t, int64_t>> shape_range;
  322. (void)input.GetTensorDesc().GetShapeRange(shape_range);
  323. if (!shape_range.empty()) {
  324. GELOGD("Has set shape range.");
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330. class GeGenerator::Impl {
  331. public:
  332. Impl(OmgContext &omg_context) : omg_context_(omg_context) {}
  333. ~Impl() = default;
  334. Status BuildModel(const Graph &graph, const vector<GeTensor> &inputs, GeRootModelPtr &ge_models);
  335. Status SaveModel(const string &file_name_prefix, GeModelPtr &models, ModelBufferData &model);
  336. Status SaveRootModel(const string &file_name_prefix, GeRootModelPtr &model, ModelBufferData &model_buff);
  337. Status SaveParams(GeModelPtr &ge_model, const string &type, const map<string, GeAttrValue> &attrs,
  338. const vector<GeTensor> &inputs, const vector<GeTensor> &outputs);
  339. Status GenerateInfershapeGraph(const Graph &graph);
  340. OmgContext &omg_context_;
  341. GraphManager graph_manager_;
  342. SaveParam save_param_;
  343. bool is_offline_ = true;
  344. bool is_singleop_unregistered_ = false;
  345. std::string build_mode_;
  346. std::string build_step_;
  347. static std::mutex mutex_;
  348. private:
  349. static std::string Trim(const std::string &str);
  350. bool ParseVersion(const std::string &line, std::string &version);
  351. bool GetVersionFromPath(const std::string &file_path, std::string &version);
  352. bool SetAtcVersionInfo(AttrHolder &obj);
  353. bool SetOppVersionInfo(AttrHolder &obj);
  354. bool SetOmSystemInfo(AttrHolder &obj);
  355. };
  356. Status GeGenerator::Initialize(const map<string, string> &options) {
  357. return Initialize(options, domi::GetContext());
  358. }
  359. Status GeGenerator::Initialize(const map<string, string> &options, OmgContext &omg_context) {
  360. impl_ = ge::MakeShared<Impl>(omg_context);
  361. if (impl_ == nullptr) {
  362. REPORT_CALL_ERROR("E19999", "create Impl failed.");
  363. GELOGE(MEMALLOC_FAILED, "[Create][Impl] Make shared failed");
  364. return MEMALLOC_FAILED;
  365. }
  366. ErrorManager::GetInstance().SetStage(error_message::kInitialize, error_message::kOpsProtoInit);
  367. string opsproto_path;
  368. GetOpsProtoPath(opsproto_path);
  369. GELOGI("Get opsproto path is %s", opsproto_path.c_str());
  370. OpsProtoManager *manager = OpsProtoManager::Instance();
  371. map<string, string> option_tmp;
  372. option_tmp.emplace(std::pair<string, string>(string("ge.opsProtoLibPath"), opsproto_path));
  373. (void)manager->Initialize(option_tmp);
  374. Status ret = impl_->graph_manager_.Initialize(options);
  375. if (ret != SUCCESS) {
  376. GELOGE(GE_GENERATOR_GRAPH_MANAGER_INIT_FAILED, "[Call][Initialize] Graph manager initialize failed.");
  377. return GE_GENERATOR_GRAPH_MANAGER_INIT_FAILED;
  378. }
  379. // get ek file
  380. auto iter = options.find(EK_FILE);
  381. if (iter != options.end()) {
  382. impl_->save_param_.ek_file = iter->second;
  383. }
  384. // get cert file
  385. iter = options.find(CERT_FILE);
  386. if (iter != options.end()) {
  387. impl_->save_param_.cert_file = iter->second;
  388. }
  389. // get hw key file
  390. iter = options.find(HW_KEY_FILE);
  391. if (iter != options.end()) {
  392. impl_->save_param_.hw_key_file = iter->second;
  393. }
  394. // get private file
  395. iter = options.find(PRIVATE_KEY_FILE);
  396. if (iter != options.end()) {
  397. impl_->save_param_.pri_key_file = iter->second;
  398. }
  399. // get build mode
  400. iter = options.find(BUILD_MODE);
  401. if (iter != options.end()) {
  402. impl_->build_mode_ = iter->second;
  403. }
  404. // get build step
  405. iter = options.find(BUILD_STEP);
  406. if (iter != options.end()) {
  407. impl_->build_step_ = iter->second;
  408. }
  409. return SUCCESS;
  410. }
  411. Status GeGenerator::Finalize() {
  412. ErrorManager::GetInstance().SetStage(error_message::kFinalize, error_message::kFinalize);
  413. if (impl_ == nullptr) {
  414. return SUCCESS;
  415. }
  416. Status ret = impl_->graph_manager_.Finalize();
  417. if (ret != SUCCESS) {
  418. GELOGE(GE_GENERATOR_GRAPH_MANAGER_FINALIZE_FAILED, "[Call][Finalize] Graph manager finalize failed.");
  419. return GE_GENERATOR_GRAPH_MANAGER_FINALIZE_FAILED;
  420. }
  421. return SUCCESS;
  422. }
  423. Status GeGenerator::GenerateOfflineModel(const Graph &graph, const string &file_name_prefix,
  424. const vector<GeTensor> &inputs) {
  425. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  426. GELOGI("Start to generate offline model.");
  427. ModelBufferData model;
  428. return GenerateModel(graph, file_name_prefix, inputs, model, true);
  429. }
  430. Status GeGenerator::GenerateOnlineModel(const Graph &graph, const vector<GeTensor> &inputs, ModelBufferData &model) {
  431. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  432. return GenerateModel(graph, "online", inputs, model, false);
  433. }
  434. Status GeGenerator::GenerateInfershapeGraph(const Graph &graph) {
  435. GE_CHECK_NOTNULL_EXEC(impl_, return PARAM_INVALID);
  436. Status ret = impl_->GenerateInfershapeGraph(graph);
  437. if (ret != SUCCESS) {
  438. GELOGE(ret, "[Call][GenerateInfershapeGraph] Dump infershape json failed");
  439. if (impl_->graph_manager_.Finalize() != SUCCESS) {
  440. GELOGE(FAILED, "[Call][Finalize] graph_manager finalize fail.");
  441. }
  442. return ret;
  443. }
  444. GELOGI("Generate infer shape graph success");
  445. return SUCCESS;
  446. }
  447. std::mutex GeGenerator::Impl::mutex_;
  448. // Remove the space and tab before and after the string
  449. std::string GeGenerator::Impl::Trim(const std::string &str) {
  450. if (str.empty()) {
  451. return str;
  452. }
  453. std::string::size_type start = str.find_first_not_of(" \t\r\n");
  454. if (start == std::string::npos) {
  455. return str;
  456. }
  457. std::string::size_type end = str.find_last_not_of(" \t\r\n") + 1;
  458. return str.substr(start, end);
  459. }
  460. // Parsing the command line
  461. bool GeGenerator::Impl::ParseVersion(const std::string &line, std::string &version) {
  462. std::string flag = "Version=";
  463. std::string temp = Trim(line);
  464. if (temp.empty()) {
  465. GELOGW("line is empty.");
  466. return false;
  467. }
  468. std::string::size_type pos = temp.find(flag);
  469. if (pos == std::string::npos) {
  470. GELOGW("Incorrect line [%s], it must include [%s].", line.c_str(), flag.c_str());
  471. return false;
  472. }
  473. if (temp.size() == flag.size()) {
  474. GELOGW("version information is empty. %s", line.c_str());
  475. return false;
  476. }
  477. version = temp.substr(pos + flag.size());
  478. return true;
  479. }
  480. bool GeGenerator::Impl::GetVersionFromPath(const std::string &file_path, std::string &version) {
  481. // Normalize the path
  482. string resolved_file_path = RealPath(file_path.c_str());
  483. if (resolved_file_path.empty()) {
  484. GELOGW("Invalid input file path [%s], make sure that the file path is correct.", file_path.c_str());
  485. return false;
  486. }
  487. std::ifstream fs(resolved_file_path, std::ifstream::in);
  488. if (!fs.is_open()) {
  489. GELOGW("Open %s failed.", file_path.c_str());
  490. return false;
  491. }
  492. std::string line;
  493. if (getline(fs, line)) {
  494. if (!ParseVersion(line, version)) {
  495. GELOGW("Parse version failed. content is [%s].", line.c_str());
  496. fs.close();
  497. return false;
  498. }
  499. } else {
  500. GELOGW("No version information found in the file path:%s", file_path.c_str());
  501. fs.close();
  502. return false;
  503. }
  504. fs.close(); // close the file
  505. return true;
  506. }
  507. // Set package version information in the model
  508. bool GeGenerator::Impl::SetAtcVersionInfo(AttrHolder &obj) {
  509. std::string path_base = ge::GELib::GetPath();
  510. path_base = path_base.substr(0, path_base.rfind('/'));
  511. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  512. std::string version_path = path_base + "version.info";
  513. std::string version;
  514. if (!GetVersionFromPath(version_path, version)) {
  515. GELOGW("Get atc version information failed!");
  516. return false;
  517. }
  518. // set version info
  519. if (!ge::AttrUtils::SetStr(obj, ATTR_MODEL_ATC_VERSION, version)) {
  520. GELOGW("Ge model set atc version failed!");
  521. return false;
  522. }
  523. return true;
  524. }
  525. // Set package version information in the model
  526. bool GeGenerator::Impl::SetOppVersionInfo(AttrHolder &obj) {
  527. const char *path_env = std::getenv("ASCEND_OPP_PATH");
  528. if (path_env == nullptr) {
  529. GELOGW("Get environment variable ASCEND_OPP_PATH failed!");
  530. return false;
  531. }
  532. std::string version_path = path_env;
  533. version_path += "/version.info";
  534. std::string version;
  535. if (!GetVersionFromPath(version_path, version)) {
  536. GELOGW("Get opp version information failed!");
  537. return false;
  538. }
  539. // set version info
  540. if (!ge::AttrUtils::SetStr(obj, ATTR_MODEL_OPP_VERSION, version)) {
  541. GELOGW("Ge model set opp version failed!");
  542. return false;
  543. }
  544. return true;
  545. }
  546. bool GeGenerator::Impl::SetOmSystemInfo(AttrHolder &obj) {
  547. std::string soc_version;
  548. (void)ge::GetContext().GetOption(ge::SOC_VERSION, soc_version);
  549. GELOGI("SetOmSystemInfo soc_version: %s", soc_version.c_str());
  550. if (!ge::AttrUtils::SetStr(obj, "soc_version", soc_version)) {
  551. GELOGW("SetStr of soc_version failed.");
  552. return false;
  553. }
  554. std::string framework_type;
  555. (void)ge::GetContext().GetOption(ge::FRAMEWORK_TYPE, framework_type);
  556. GELOGI("SetOmSystemInfo framework_type: %s", framework_type.c_str());
  557. auto iter = ge::kFwkTypeToStr.find(framework_type);
  558. if (iter == ge::kFwkTypeToStr.end()) {
  559. GELOGW("Can not find framework_type in the map.");
  560. return false;
  561. }
  562. if (!ge::AttrUtils::SetStr(obj, "framework_type", iter->second)) {
  563. GELOGW("SetStr of framework_type failed.");
  564. return false;
  565. }
  566. return true;
  567. }
  568. Status GeGenerator::SetModelNameForDump(const GeRootModelPtr &ge_root_model) {
  569. bool is_unknown_shape = false;
  570. Status ret = ge_root_model->CheckIsUnknownShape(is_unknown_shape);
  571. if (ret != SUCCESS) {
  572. GELOGE(FAILED, "[Check][IsUnknownShape]Check root model is unknown shape failed, model id:%u",
  573. ge_root_model->GetModelId());
  574. REPORT_CALL_ERROR("E19999", "Check root model is unknown shape failed, model id:%u",
  575. ge_root_model->GetModelId());
  576. return FAILED;
  577. }
  578. GeModelPtr model_root = nullptr;
  579. if (is_unknown_shape) {
  580. model_root = MakeShared<GeModel>();
  581. GE_CHECK_NOTNULL(model_root);
  582. model_root->SetGraph(GraphUtils::CreateGraphFromComputeGraph(ge_root_model->GetRootGraph()));
  583. ge_root_model->SetSubgraphInstanceNameToModel(ge_root_model->GetRootGraph()->GetName(), model_root);
  584. }
  585. ModelHelper model_helper;
  586. string model_name;
  587. GE_CHECK_NOTNULL(ge_root_model->GetRootGraph());
  588. Status name_ret = model_helper.GetModelNameFromMergedGraphName(ge_root_model->GetRootGraph()->GetName(),
  589. model_name);
  590. if (name_ret != SUCCESS) {
  591. ErrorManager::GetInstance().ATCReportErrMessage("E10000", {"parameter"}, {"output"});
  592. GELOGE(FAILED, "[Check][GetModelNameStep]Get model_name failed. Param --output is invalid, root graph name: %s",
  593. ge_root_model->GetRootGraph()->GetName().c_str());
  594. return PARAM_INVALID;
  595. }
  596. map<string, GeModelPtr> name_to_ge_model = ge_root_model->GetSubgraphInstanceNameToModel();
  597. GeModelPtr &ge_model = name_to_ge_model[ge_root_model->GetRootGraph()->GetName()];
  598. GE_CHECK_NOTNULL(ge_model);
  599. ge_model->SetName(model_name);
  600. return SUCCESS;
  601. }
  602. Status GeGenerator::GenerateModel(const Graph &graph, const string &file_name_prefix, const vector<GeTensor> &inputs,
  603. ModelBufferData &model, bool is_offline) {
  604. rtContext_t ctx = nullptr;
  605. auto rt = rtCtxGetCurrent(&ctx);
  606. if (rt != RT_ERROR_NONE) {
  607. GELOGD("Current ctx is null.");
  608. ctx = nullptr;
  609. }
  610. std::function<void()> callback = [&]() {
  611. if (ctx != nullptr) {
  612. (void)rtCtxSetCurrent(ctx);
  613. }
  614. };
  615. GE_MAKE_GUARD(restore, callback);
  616. GeRootModelPtr ge_root_model = nullptr;
  617. GE_CHECK_NOTNULL_EXEC(impl_, return PARAM_INVALID);
  618. impl_->is_offline_ = is_offline;
  619. Status ret = impl_->BuildModel(graph, inputs, ge_root_model);
  620. if (ret != SUCCESS) {
  621. GELOGE(ret, "[Build][Model] failed, ret:%d.", ret);
  622. if (impl_->graph_manager_.Finalize() != SUCCESS) {
  623. GELOGE(FAILED, "[Call][Finalize] graph_manager finalize fail.");
  624. }
  625. return ret;
  626. }
  627. /// BUILD_MODE_TUNING with BUILD_STEP_BEFORE_UB_MATCH no need save model;
  628. /// BUILD_MODE_TUNING with BUILD_STEP_AFTER_BUILDER no need save model;
  629. /// BUILD_MODE_TUNING with BUILD_STEP_AFTER_BUILDER_SUB no need save model.
  630. if ((impl_->build_mode_ == BUILD_MODE_TUNING) &&
  631. (impl_->build_step_ == BUILD_STEP_BEFORE_UB_MATCH || impl_->build_step_ == BUILD_STEP_AFTER_BUILDER ||
  632. impl_->build_step_ == BUILD_STEP_AFTER_BUILDER_SUB)) {
  633. GELOGI("Build mode:%s with step:%s no need SaveModel.",
  634. impl_->build_mode_.c_str(),
  635. impl_->build_step_.c_str());
  636. return SUCCESS;
  637. }
  638. GE_CHECK_NOTNULL(ge_root_model);
  639. ret = SetModelNameForDump(ge_root_model);
  640. if (ret != SUCCESS) {
  641. return ret;
  642. }
  643. ret = impl_->SaveRootModel(file_name_prefix, ge_root_model, model);
  644. if (ret != SUCCESS) {
  645. GELOGE(ret, "[Save][RootModel] failed, ret:%d, file:%s", ret, file_name_prefix.c_str());
  646. if (impl_->graph_manager_.Finalize() != SUCCESS) {
  647. GELOGE(FAILED, "graph_manager finalize fail.");
  648. }
  649. return ret;
  650. }
  651. return SUCCESS;
  652. }
  653. namespace {
  654. bool IsNeedConnectInputOpForSingleOp(GeTensorDesc &tensor_desc) {
  655. bool is_need = true;
  656. // format and dtype is all reserved, stand for Optional input. When singleop scene
  657. if (tensor_desc.GetFormat() == FORMAT_RESERVED && tensor_desc.GetDataType() == DT_UNDEFINED) {
  658. is_need = false;
  659. }
  660. return is_need;
  661. }
  662. Status CheckDynamicSupport(GeModelPtr &ge_model, const ComputeGraphPtr &graph) {
  663. bool support_dynamic = true;
  664. bool is_dynamic = false;
  665. for (const auto &node : graph->GetDirectNode()) {
  666. GE_CHECK_NOTNULL(node);
  667. auto op_desc = node->GetOpDesc();
  668. GE_CHECK_NOTNULL(op_desc);
  669. if (op_desc->GetOpEngineName() != kAIcoreEngine) {
  670. continue;
  671. }
  672. if (AttrUtils::HasAttr(op_desc, kAttrSupportDynamicShape)) {
  673. is_dynamic = true;
  674. (void) AttrUtils::GetBool(op_desc, kAttrSupportDynamicShape, support_dynamic);
  675. if (!support_dynamic) {
  676. GELOGW("Node[%s] doesn't support dynamic shape.", node->GetName().c_str());
  677. break;
  678. }
  679. }
  680. }
  681. if (is_dynamic) {
  682. (void) AttrUtils::SetBool(ge_model, kAttrSupportDynamicShape, support_dynamic);
  683. }
  684. return SUCCESS;
  685. }
  686. }
  687. bool GeGenerator::CheckNoAicore(const ComputeGraphPtr &graph) {
  688. for (const auto &node : graph->GetDirectNode()) {
  689. if (node == nullptr) {
  690. continue;
  691. }
  692. auto op_desc = node->GetOpDesc();
  693. if (op_desc == nullptr) {
  694. continue;
  695. }
  696. if (op_desc->GetOpEngineName() == kAIcoreEngine) {
  697. return false;
  698. }
  699. }
  700. return true;
  701. }
  702. void GeGenerator::RemoveConst(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  703. for (auto &input : inputs) {
  704. GeTensorDesc input_desc = input.GetTensorDesc();
  705. bool is_const = false;
  706. (void)AttrUtils::GetBool(input_desc, CONST_ATTR_NAME_INPUT, is_const);
  707. bool is_optional = IsOptional(input_desc);
  708. if (!is_optional && !is_const) {
  709. outputs.emplace_back(input);
  710. }
  711. }
  712. }
  713. Status GeGenerator::CheckForSingleOp(OpDescPtr &op_desc, const vector<GeTensor> &inputs,
  714. const vector<GeTensor> &outputs) {
  715. GE_CHECK_NOTNULL_EXEC(op_desc, return PARAM_INVALID);
  716. if (!inputs.empty() && (inputs.size() != op_desc->GetAllInputsSize())) {
  717. ErrorManager::GetInstance().ATCReportErrMessage("E14001", {"opname", "optype", "value", "reason"},
  718. {op_desc->GetName(), op_desc->GetType(), "inputs size" + FmtToStr(op_desc->GetAllInputsSize()),
  719. "tensor size is " + FmtToStr(inputs.size())});
  720. GELOGE(PARAM_INVALID, "[Check][Param] Tensor size: %zu, op:%s(%s) Inputs size: %zu, not equal",
  721. inputs.size(), op_desc->GetName().c_str(), op_desc->GetType().c_str(), op_desc->GetAllInputsSize());
  722. return PARAM_INVALID;
  723. }
  724. if (!outputs.empty() && (outputs.size() != op_desc->GetOutputsSize())) {
  725. ErrorManager::GetInstance().ATCReportErrMessage("E14001", {"opname", "optype", "value", "reason"},
  726. {op_desc->GetName(), op_desc->GetType(), "outputs size" + FmtToStr(op_desc->GetOutputsSize()),
  727. "tensor size is " + FmtToStr(outputs.size())});
  728. GELOGE(PARAM_INVALID, "[Check][Param] Tensor size: %zu, op:%s(%s) Outputs size: %zu, not equal",
  729. outputs.size(), op_desc->GetName().c_str(), op_desc->GetType().c_str(), op_desc->GetOutputsSize());
  730. return PARAM_INVALID;
  731. }
  732. return SUCCESS;
  733. }
  734. Status GeGenerator::InferFormatForSingleOp(OpDescPtr &op_desc, Graph &graph) {
  735. GE_CHECK_NOTNULL(op_desc);
  736. if (OperatorFactoryImpl::GetInferFormatFunc(op_desc->GetType()) != nullptr) {
  737. auto node_op = ge::OperatorFactoryImpl::CreateOperator("node_op", op_desc->GetType());
  738. if (node_op.IsEmpty()) {
  739. GELOGW("get op from OperatorFactory fail. op type: %s", op_desc->GetType().c_str());
  740. } else {
  741. GELOGD("get op from OperatorFactory success. op type: %s", op_desc->GetType().c_str());
  742. auto temp_op_desc = ge::OpDescUtils::GetOpDescFromOperator(node_op);
  743. if (temp_op_desc == nullptr) {
  744. REPORT_INNER_ERROR("E19999", "GetOpDescFromOperator failed, as return nullptr, type:%s",
  745. op_desc->GetType().c_str());
  746. GELOGE(FAILED, "[Get][OpDesc] temp op desc is null, type:%s", op_desc->GetType().c_str());
  747. return FAILED;
  748. }
  749. if (!op_desc->UpdateInputName(temp_op_desc->GetAllInputName())) {
  750. GELOGW("InferFormatForSingleOp UpdateInputName failed");
  751. }
  752. if (!op_desc->UpdateOutputName(temp_op_desc->GetAllOutputName())) {
  753. GELOGW("InferFormatForSingleOp UpdateOutputName failed");
  754. }
  755. }
  756. node_op.BreakConnect();
  757. }
  758. auto comp_graph = GraphUtils::GetComputeGraph(graph);
  759. GE_CHECK_NOTNULL(comp_graph);
  760. auto node = comp_graph->FindNode(op_desc->GetName());
  761. GE_CHECK_NOTNULL(node);
  762. auto op = OpDescUtils::CreateOperatorFromNode(node);
  763. auto ret = op_desc->CallInferFormatFunc(op);
  764. if (ret != GRAPH_SUCCESS) {
  765. REPORT_INNER_ERROR("E19999", "call InferFormatFunc for single op:%s fail",
  766. op_desc->GetName().c_str());
  767. GELOGE(FAILED, "[Call][InferFormatFunc] for single op:%s fail.", op_desc->GetName().c_str());
  768. return FAILED;
  769. }
  770. return SUCCESS;
  771. }
  772. Status GeGenerator::BuildSingleOp(OpDescPtr &op_desc, const vector<GeTensor> &inputs, const vector<GeTensor> &outputs,
  773. const string &model_file_name, OpEngineType engine_type, ModelBufferData &model_buff,
  774. bool is_offline, int32_t compile_flag) {
  775. GELOGD("Inputs size is %zu, outputs size is %zu.", inputs.size(), outputs.size());
  776. GE_CHECK_NOTNULL_EXEC(impl_, return PARAM_INVALID);
  777. impl_->is_offline_ = is_offline;
  778. (void)AttrUtils::SetBool(op_desc, ATTR_SINGLE_OP_SCENE, true);
  779. if (CheckForSingleOp(op_desc, inputs, outputs) != SUCCESS) {
  780. GELOGE(PARAM_INVALID, "[Check][Param] input param is invalid when build single op:%s!",
  781. op_desc->GetName().c_str());
  782. return PARAM_INVALID;
  783. }
  784. OmgContext &omg_context = impl_->omg_context_;
  785. omg_context.is_dynamic_input = ContainsDynamicInpus(*op_desc);
  786. if (op_desc->HasAttr(ATTR_NAME_UNREGST_OPPATH)) {
  787. impl_->is_singleop_unregistered_ = true;
  788. }
  789. // 0. Save original attributes.
  790. OpDescPtr op_desc_tmp = AttrUtils::CloneOpDesc(op_desc);
  791. GE_CHECK_NOTNULL(op_desc_tmp);
  792. bool fuzz_compile_flag = false;
  793. if (!HasShapeRange(inputs) && compile_flag == kFuzzBuildPattern) {
  794. fuzz_compile_flag = true;
  795. }
  796. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_FUZZ_BUILD, fuzz_compile_flag);
  797. impl_->omg_context_.fuzz_compile_flag = fuzz_compile_flag;
  798. // 1. Create ComputeGraph.
  799. string name = ge::CurrentTimeInStr() + "_" + model_file_name;
  800. Graph graph;
  801. GE_CHK_STATUS(BuildSingleOpGraph(op_desc, inputs, outputs, name, graph),
  802. "[Build][Graph] for single op:%s fail.", op_desc->GetName().c_str());
  803. GE_CHK_STATUS_RET_NOLOG(InferFormatForSingleOp(op_desc, graph));
  804. // 2. check engine type when compile online
  805. if (model_file_name == kFileNameSuffix) {
  806. auto comp_graph = GraphUtils::GetComputeGraph(graph);
  807. GE_CHECK_NOTNULL(comp_graph);
  808. auto node = comp_graph->FindNode(op_desc->GetName());
  809. Status ret = CheckEngineTypeSupport(node, engine_type);
  810. if (ret != SUCCESS) {
  811. GELOGE(ret, "[Check][EngineType]not support node:%s with engine of %d.", node->GetName().c_str(), engine_type);
  812. return ret;
  813. }
  814. }
  815. GELOGI("ATC parser success in single op build.");
  816. GeRootModelPtr ge_root_model = nullptr;
  817. vector<GeTensor> data_inputs;
  818. RemoveConst(inputs, data_inputs);
  819. GE_CHK_STATUS_RET_NOLOG(impl_->BuildModel(graph, data_inputs, ge_root_model));
  820. map<string, GeAttrValue> op_attrs = op_desc_tmp->GetAllAttrs();
  821. GE_CHECK_NOTNULL(ge_root_model);
  822. GE_CHECK_NOTNULL(ge_root_model->GetRootGraph());
  823. map<string, GeModelPtr> name_to_ge_model = ge_root_model->GetSubgraphInstanceNameToModel();
  824. if (name_to_ge_model.empty()) {
  825. REPORT_CALL_ERROR("E19999", "GetSubgraphInstanceNameToModel failed.");
  826. GELOGE(PARAM_INVALID, "[Get][Name] GetSubgraphInstanceNameToModel is empty.");
  827. return PARAM_INVALID;
  828. }
  829. const ComputeGraphPtr root_graph = ge_root_model->GetRootGraph();
  830. GeModelPtr &ge_model = name_to_ge_model.begin()->second;
  831. GE_CHK_STATUS_RET_NOLOG(CheckDynamicSupport(ge_model, root_graph));
  832. GELOGI("After build model, The opType in op_desc_tmp is [%s]", op_desc_tmp->GetType().c_str());
  833. bool all_shape = false;
  834. (void)AttrUtils::GetBool(op_desc, kAicpuAllshape, all_shape);
  835. GELOGD("Node: %s, all_shape is %d, compile_flag is %d.", op_desc->GetName().c_str(), all_shape, compile_flag);
  836. (void)AttrUtils::SetInt(ge_model, ATTR_NAME_BUILD_MODE, fuzz_compile_flag);
  837. if (all_shape) {
  838. (void)AttrUtils::SetBool(ge_model, kAicpuAllshape, all_shape);
  839. }
  840. if (all_shape && CheckNoAicore(root_graph)) {
  841. GELOGD("Get aicpu all_shape kernel!");
  842. vector<GeTensor> inputs_dynamic;
  843. vector<GeTensor> outputs_dynamic;
  844. GE_CHK_STATUS_RET_NOLOG(ResetTensorVecShape(inputs, inputs_dynamic));
  845. GE_CHK_STATUS_RET_NOLOG(ResetTensorVecShape(outputs, outputs_dynamic));
  846. GE_CHK_STATUS_RET_NOLOG(
  847. impl_->SaveParams(ge_model, op_desc_tmp->GetType(), op_attrs, inputs_dynamic, outputs_dynamic));
  848. } else if (fuzz_compile_flag) {
  849. GeAttrValue::LIST_NAMED_ATTRS fuzz_build_attrs;
  850. if (GetFuzzBuildAttrs(op_desc, ge_root_model, fuzz_build_attrs) != SUCCESS) {
  851. GELOGE(FAILED, "[Get][FuzzRet]Failed to get fuzz build result of %s.", op_desc->GetName().c_str());
  852. return FAILED;
  853. }
  854. if (!fuzz_build_attrs.empty()) {
  855. GE_CHK_BOOL_EXEC(AttrUtils::SetListNamedAttrs(ge_model, ATTR_NAME_FUZZ_BUILD_RES_ATTRS, fuzz_build_attrs),
  856. REPORT_CALL_ERROR("E19999", "Set model:%s(id:%u) attr:%s failed.",
  857. ge_model->GetName().c_str(), ge_model->GetModelId(),
  858. ATTR_NAME_FUZZ_BUILD_RES_ATTRS.c_str());
  859. return FAILED, "Set model:%s(id:%u) attr:%s failed.",
  860. ge_model->GetName().c_str(), ge_model->GetModelId(), ATTR_NAME_FUZZ_BUILD_RES_ATTRS.c_str());
  861. }
  862. GE_CHK_STATUS_RET_NOLOG(impl_->SaveParams(ge_model, op_desc_tmp->GetType(), op_attrs, inputs, outputs));
  863. } else {
  864. GE_CHK_STATUS_RET_NOLOG(impl_->SaveParams(ge_model, op_desc_tmp->GetType(), op_attrs, inputs, outputs));
  865. }
  866. GELOGI("Start save GeModel to Model buffer");
  867. GE_CHK_STATUS_RET_NOLOG(impl_->SaveModel(model_file_name, ge_model, model_buff));
  868. return SUCCESS;
  869. }
  870. /**
  871. * @ingroup ge
  872. * @brief Compiling a single operator into an offline model
  873. * @param [in] OpDescPtr &op_desc: Operator description info that needs to be compiled into an offline model file
  874. * @param [in] vector<GeTensor> &inputs: Operator input data description information.
  875. * @param [in] vector<GeTensor> &outputs: Operator output data description information.
  876. * @param [in] const string &model_file_name: Offline model filename.
  877. * @param [in] compile_flag: op build flag from atc
  878. * @return SUCCESS handle successfully / others handle failed
  879. */
  880. Status GeGenerator::BuildSingleOpModel(OpDescPtr &op_desc, const vector<GeTensor> &inputs,
  881. const vector<GeTensor> &outputs, const string &model_file_name,
  882. int32_t compile_flag) {
  883. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  884. GELOGI("Start to build single op offline model, input size: %zu, output size: %zu", inputs.size(), outputs.size());
  885. ModelBufferData model_buff;
  886. OpEngineType engine_type = ENGINE_SYS;
  887. Status status = BuildSingleOp(op_desc, inputs, outputs, model_file_name, engine_type, model_buff, true, compile_flag);
  888. GELOGI("Finish build single offline model, status: %u", status);
  889. return status;
  890. }
  891. /**
  892. * @ingroup ge
  893. * @brief Compiling a single operator into online buffer
  894. * @param [in] OpDescPtr &op_desc: Operator description info that needs to be compiled into an offline model file
  895. * @param [in] vector<GeTensor> &inputs: Operator input data description information.
  896. * @param [in] vector<GeTensor> &outputs: Operator output data description information.
  897. * @param [in] engine_type: specific engine.
  898. * @param [in] compile_flag: op build flag, compile flag by acl
  899. * @param [out] ModelBufferData &Model_buff: Model_buff: model buffer of the op.
  900. * @return SUCCESS handle successfully / others handle failed
  901. */
  902. Status GeGenerator::BuildSingleOpModel(OpDescPtr &op_desc, const vector<GeTensor> &inputs,
  903. const vector<GeTensor> &outputs, OpEngineType engine_type,
  904. ModelBufferData &model_buff) {
  905. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  906. GELOGI("Start to build single op online, input size: %zu, output size: %zu", inputs.size(), outputs.size());
  907. Status status = BuildSingleOp(op_desc, inputs, outputs, kFileNameSuffix, engine_type, model_buff, false);
  908. GELOGI("Finish build single online model, status: %u", status);
  909. return status;
  910. }
  911. Status GeGenerator::BuildSingleOpModel(OpDescPtr &op_desc, const vector<GeTensor> &inputs,
  912. const vector<GeTensor> &outputs, OpEngineType engine_type, int32_t compile_flag,
  913. ModelBufferData &model_buff) {
  914. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  915. GELOGI("Start to build single op online, input size: %zu, output size: %zu", inputs.size(), outputs.size());
  916. Status status = BuildSingleOp(op_desc, inputs, outputs, kFileNameSuffix, engine_type, model_buff, false,
  917. compile_flag);
  918. GELOGI("Finish build single online model, status: %u", status);
  919. return status;
  920. }
  921. Status GeGenerator::BuildSingleOpGraph(OpDescPtr &op_desc, const vector<GeTensor> &inputs,
  922. const vector<GeTensor> &outputs, std::string graph_name, Graph &graph) {
  923. ge::ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>(graph_name);
  924. GE_CHECK_NOTNULL_EXEC(compute_graph, return INTERNAL_ERROR);
  925. // 1. Add Node to ComputeGraph.
  926. NodePtr op_node = compute_graph->AddNode(op_desc);
  927. GE_CHECK_NOTNULL_EXEC(op_node, return INTERNAL_ERROR);
  928. // 2. Create InputData node.
  929. int32_t arg_index = 0;
  930. int32_t data_index = 0;
  931. if (inputs.empty()) {
  932. for (const auto &input_desc : op_desc->GetAllInputsDescPtr()) {
  933. GE_CHECK_NOTNULL_EXEC(input_desc, return INTERNAL_ERROR);
  934. if (!IsNeedConnectInputOpForSingleOp(*input_desc)) {
  935. continue;
  936. }
  937. GE_CHK_STATUS_RET_NOLOG(AddInputs(compute_graph, op_node, *input_desc, arg_index, false, data_index));
  938. arg_index++;
  939. }
  940. } else {
  941. for (const auto &in_desc : inputs) {
  942. GE_CHK_STATUS_RET_NOLOG(AddInputs(compute_graph, op_node, in_desc.GetTensorDesc(), arg_index, true, data_index));
  943. arg_index++;
  944. }
  945. }
  946. // 3. Create Output node.
  947. if (!outputs.empty()) {
  948. GE_CHK_STATUS_RET_NOLOG(AddOutputs(compute_graph, op_node, outputs));
  949. }
  950. // dump ComputeGraph node.
  951. compute_graph->Dump();
  952. graph = ge::GraphUtils::CreateGraphFromComputeGraph(compute_graph);
  953. return SUCCESS;
  954. }
  955. Status GeGenerator::Impl::SaveParams(GeModelPtr &ge_model, const string &type, const map<string, GeAttrValue> &attrs,
  956. const vector<GeTensor> &inputs, const vector<GeTensor> &outputs) {
  957. GE_CHECK_NOTNULL_EXEC(ge_model, return PARAM_INVALID);
  958. GE_CHK_BOOL_EXEC_NOLOG(graph_manager_.SaveParams(*ge_model, type, attrs, inputs, outputs) == SUCCESS,
  959. (void)graph_manager_.Finalize();
  960. return FAILED);
  961. return SUCCESS;
  962. }
  963. Status GeGenerator::Impl::SaveModel(const string &file_name_prefix, GeModelPtr &model, ModelBufferData &model_buff) {
  964. // set atc version
  965. if (!SetAtcVersionInfo(*(model.get()))) {
  966. GELOGW("SetPackageVersionInfo of atc failed!");
  967. }
  968. // set opp version
  969. if (!SetOppVersionInfo(*(model.get()))) {
  970. GELOGW("SetPackageVersionInfo of ops failed!");
  971. }
  972. ModelHelper model_helper;
  973. model_helper.SetSaveMode(is_offline_);
  974. Status ret = model_helper.SaveToOmModel(model, save_param_, file_name_prefix, model_buff);
  975. if (ret != SUCCESS) {
  976. GELOGE(ret, "[Call][SaveToOmModel] Save to om model failed");
  977. return ret;
  978. }
  979. return SUCCESS;
  980. }
  981. Status GeGenerator::Impl::SaveRootModel(const string &file_name_prefix, GeRootModelPtr &ge_root_model,
  982. ModelBufferData &model_buff) {
  983. bool is_unknown_shape = false;
  984. auto ret = ge_root_model->CheckIsUnknownShape(is_unknown_shape);
  985. if (ret != SUCCESS) {
  986. REPORT_CALL_ERROR("E19999", "root model(id:%u) CheckIsUnknownShape failed, ret:%d",
  987. ge_root_model->GetModelId(), ret);
  988. GELOGE(FAILED, "[Check][RootModel] is unkonwn shape failed, ret:%d", ret);
  989. return FAILED;
  990. }
  991. GELOGD("begin save root model, cur model is unkonwn shape model ? : %d", is_unknown_shape);
  992. GE_CHK_BOOL_EXEC(!ge_root_model->GetSubgraphInstanceNameToModel().empty(),
  993. REPORT_CALL_ERROR("E19999", "root model(id:%u) has no sub model.", ge_root_model->GetModelId());
  994. return FAILED, "[Get][SubModel] ge root model has no sub model")
  995. GeModelPtr model_root = nullptr;
  996. if (is_unknown_shape) {
  997. auto name_to_ge_model = ge_root_model->GetSubgraphInstanceNameToModel();
  998. model_root = name_to_ge_model[ge_root_model->GetRootGraph()->GetName()];
  999. } else {
  1000. model_root = ge_root_model->GetSubgraphInstanceNameToModel().begin()->second;
  1001. }
  1002. GE_CHECK_NOTNULL(model_root);
  1003. // set atc version
  1004. if (!SetAtcVersionInfo(*(model_root.get()))) {
  1005. GELOGW("SetPackageVersionInfo of atc failed!");
  1006. }
  1007. // set opp version
  1008. if (!SetOppVersionInfo(*(model_root.get()))) {
  1009. GELOGW("SetPackageVersionInfo of ops failed!");
  1010. }
  1011. if (!SetOmSystemInfo(*(model_root.get()))) {
  1012. GELOGW("SetOmsystemInfo failed!");
  1013. }
  1014. ModelHelper model_helper;
  1015. model_helper.SetSaveMode(is_offline_);
  1016. ret = model_helper.SaveToOmRootModel(ge_root_model, save_param_, file_name_prefix, model_buff, is_unknown_shape);
  1017. if (ret != SUCCESS) {
  1018. REPORT_CALL_ERROR("E19999", "SaveToOmRootModel failed, ret:%d, model id:%u", ret, ge_root_model->GetModelId());
  1019. GELOGE(ret, "[Call][SaveToOmRootModel] failed, ret:%d, model id:%u", ret, ge_root_model->GetModelId());
  1020. return ret;
  1021. }
  1022. return SUCCESS;
  1023. }
  1024. Status GeGenerator::Impl::BuildModel(const Graph &graph, const vector<GeTensor> &inputs,
  1025. GeRootModelPtr &ge_root_model) {
  1026. static std::atomic<GraphId> atomic_graph_id(0);
  1027. auto graph_id = atomic_graph_id.fetch_add(1);
  1028. const std::map<std::string, std::string> options;
  1029. Status ret = graph_manager_.AddGraph(graph_id, graph, options, omg_context_);
  1030. if (ret != SUCCESS) {
  1031. REPORT_CALL_ERROR("E19999", "add graph(id:%u) failed, ret:%d", graph_id, ret);
  1032. GELOGE(GE_GENERATOR_GRAPH_MANAGER_ADD_GRAPH_FAILED, "[Add][Graph] fail, graph id: %u", graph_id);
  1033. (void)graph_manager_.Finalize();
  1034. return GE_GENERATOR_GRAPH_MANAGER_ADD_GRAPH_FAILED;
  1035. }
  1036. graph_manager_.SetOptionsRunGraphFlag(false);
  1037. static std::atomic<uint64_t> atomic_session_id(0);
  1038. auto session_id = atomic_session_id.fetch_add(1);
  1039. // This is a temporary add for graph with variable
  1040. auto version = static_cast<int32_t>(SessionVersion::ClOUD_VERSION);
  1041. ret = VarManager::Instance(session_id)->Init(version, session_id, kDefaultDeviceId, kDefaultJobId);
  1042. GELOGI("Start init var instance, session_id %lu", session_id);
  1043. if (ret != SUCCESS) {
  1044. GELOGW("Failed init var instance, session_id %lu", session_id);
  1045. }
  1046. if (is_singleop_unregistered_) {
  1047. ret = graph_manager_.BuildGraphForUnregisteredOp(graph_id, inputs, ge_root_model, session_id);
  1048. } else {
  1049. ret = graph_manager_.BuildGraph(graph_id, inputs, ge_root_model, session_id);
  1050. }
  1051. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  1052. if (ret != SUCCESS) {
  1053. REPORT_CALL_ERROR("E19999", "build graph failed, graph id:%u, ret:%d", graph_id, ret);
  1054. GELOGE(GE_GENERATOR_GRAPH_MANAGER_BUILD_GRAPH_FAILED, "[Build][Graph] fail, graph id: %u", graph_id);
  1055. ret = GE_GENERATOR_GRAPH_MANAGER_BUILD_GRAPH_FAILED;
  1056. }
  1057. RtContextUtil::GetInstance().DestroyRtContexts(session_id);
  1058. Analyzer::GetInstance()->DestroySessionJsonObject(session_id);
  1059. VarManagerPool::Instance().RemoveVarManager(session_id);
  1060. return ret;
  1061. }
  1062. Status GeGenerator::Impl::GenerateInfershapeGraph(const Graph &graph) {
  1063. static std::atomic<GraphId> atomic_graph_id(0);
  1064. auto graph_id = atomic_graph_id.fetch_add(1);
  1065. const std::map<std::string, std::string> options;
  1066. Status ret = graph_manager_.AddGraph(graph_id, graph, options, omg_context_);
  1067. if (ret != SUCCESS) {
  1068. REPORT_CALL_ERROR("E19999", "add graph failed, graph id:%u, ret:%d", graph_id, ret);
  1069. GELOGE(GE_GENERATOR_GRAPH_MANAGER_ADD_GRAPH_FAILED, "[Add][Graph] failed, graph id: %u", graph_id);
  1070. (void)graph_manager_.Finalize();
  1071. return GE_GENERATOR_GRAPH_MANAGER_ADD_GRAPH_FAILED;
  1072. }
  1073. ret = graph_manager_.GenerateInfershapeGraph(graph_id);
  1074. if (ret != SUCCESS) {
  1075. REPORT_CALL_ERROR("E19999", "GenerateInfershapeGraph failed, graph id:%u, ret:%d", graph_id, ret);
  1076. GELOGE(GE_GENERATOR_GRAPH_MANAGER_BUILD_GRAPH_FAILED,
  1077. "[Generate][Graph] failed, graph id:%u, ret:%d", graph_id, ret);
  1078. return GE_GENERATOR_GRAPH_MANAGER_BUILD_GRAPH_FAILED;
  1079. }
  1080. return SUCCESS;
  1081. }
  1082. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示