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

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