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.

aicpu_kernel_build.cc 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "kernel/aicpu/aicpu_kernel_build.h"
  17. #include <google/protobuf/text_format.h>
  18. #include <fstream>
  19. #include <utility>
  20. #include <string>
  21. #include <vector>
  22. #include <memory>
  23. #include <algorithm>
  24. #include <map>
  25. #include "device/kernel_runtime.h"
  26. #include "kernel/aicpu/aicpu_kernel_mod.h"
  27. #include "kernel/akg/akgkernelbuild.h"
  28. #include "proto/tensor.pb.h"
  29. #include "proto/tensor_shape.pb.h"
  30. #include "proto/attr.pb.h"
  31. #include "proto/node_def.pb.h"
  32. #include "session/anf_runtime_algorithm.h"
  33. #include "common/utils.h"
  34. #include "kernel/aicpu/aicpu_util.h"
  35. #include "session/kernel_graph.h"
  36. #include "kernel/common_utils.h"
  37. namespace mindspore {
  38. namespace kernel {
  39. using FNodeAttrHandle = std::function<void(const std::shared_ptr<AnfNode> &anf_node, mindspore::NodeDef *proto)>;
  40. bool SetIOIputSize(const std::shared_ptr<AnfNode> &anf_node, const size_t &input_num,
  41. std::vector<size_t> *input_size_list) {
  42. MS_EXCEPTION_IF_NULL(anf_node);
  43. MS_EXCEPTION_IF_NULL(input_size_list);
  44. for (size_t i = 0; i < input_num; i++) {
  45. std::vector<size_t> shape_i = AnfAlgo::GetInputDeviceShape(anf_node, i);
  46. if (AnfAlgo::GetInputDeviceDataType(anf_node, i) == kObjectTypeString) {
  47. if (!anf_node->isa<CNode>()) {
  48. MS_LOG(EXCEPTION) << "anf_node is not CNode.";
  49. }
  50. auto cnode = anf_node->cast<CNodePtr>();
  51. auto input_node = cnode->inputs()[i + 1];
  52. if (input_node->isa<ValueNode>()) {
  53. auto value_ptr = GetValueNode(input_node);
  54. auto value = GetValue<std::string>(value_ptr);
  55. input_size_list->push_back(value.size());
  56. }
  57. } else {
  58. auto type_ptr = TypeIdToType(AnfAlgo::GetInputDeviceDataType(anf_node, i));
  59. MS_EXCEPTION_IF_NULL(type_ptr);
  60. int size_i = 1;
  61. for (size_t j = 0; j < shape_i.size(); j++) {
  62. IntMulWithOverflowCheck(size_i, static_cast<int>(shape_i[j]), &size_i);
  63. }
  64. size_t type_byte = GetTypeByte(type_ptr);
  65. if (type_byte == 0) {
  66. return false;
  67. }
  68. IntMulWithOverflowCheck(size_i, SizeToInt(type_byte), &size_i);
  69. input_size_list->push_back(IntToSize(size_i));
  70. }
  71. }
  72. return true;
  73. }
  74. bool SetIOSize(const std::shared_ptr<AnfNode> &anf_node, const std::shared_ptr<AicpuOpKernelMod> &kernel_mod_ptr) {
  75. MS_EXCEPTION_IF_NULL(anf_node);
  76. MS_EXCEPTION_IF_NULL(kernel_mod_ptr);
  77. std::vector<size_t> input_size_list;
  78. std::vector<size_t> output_size_list;
  79. size_t input_num = AnfAlgo::GetInputTensorNum(anf_node);
  80. size_t output_num = AnfAlgo::GetOutputTensorNum(anf_node);
  81. if (!SetIOIputSize(anf_node, input_num, &input_size_list)) {
  82. return false;
  83. }
  84. kernel_mod_ptr->SetInputSizeList(input_size_list);
  85. for (size_t i = 0; i < output_num; i++) {
  86. std::vector<size_t> shape_i = AnfAlgo::GetOutputDeviceShape(anf_node, i);
  87. TypePtr type_ptr = TypeIdToType(AnfAlgo::GetOutputDeviceDataType(anf_node, i));
  88. MS_EXCEPTION_IF_NULL(type_ptr);
  89. int size_i = 1;
  90. for (size_t j = 0; j < shape_i.size(); j++) {
  91. IntMulWithOverflowCheck(size_i, static_cast<int>(shape_i[j]), &size_i);
  92. }
  93. size_t type_byte = GetTypeByte(type_ptr);
  94. if (type_byte == 0) {
  95. return false;
  96. }
  97. IntMulWithOverflowCheck(size_i, SizeToInt(type_byte), &size_i);
  98. output_size_list.push_back(IntToSize(size_i));
  99. }
  100. kernel_mod_ptr->SetOutputSizeList(output_size_list);
  101. return true;
  102. }
  103. void ParseAttrValue(const std::string &type, const std::string &attr_name, const mindspore::ValuePtr &value,
  104. ::google::protobuf::Map<::std::string, ::mindspore::AttrValue> *node_attr) {
  105. MS_EXCEPTION_IF_NULL(node_attr);
  106. if (type == "int") {
  107. auto attr_value = GetValue<int>(value);
  108. (*node_attr)[attr_name].set_i(attr_value);
  109. } else if (type == "str") {
  110. auto attr_value = GetValue<std::string>(value);
  111. (*node_attr)[attr_name].set_s(attr_value);
  112. } else if (type == "bool") {
  113. auto attr_value = GetValue<bool>(value);
  114. (*node_attr)[attr_name].set_b(attr_value);
  115. } else if (type == "float") {
  116. auto attr_value = GetValue<float>(value);
  117. (*node_attr)[attr_name].set_f(attr_value);
  118. } else if (type == "listInt") {
  119. std::vector<int> attr_value;
  120. auto value_type = value->type();
  121. MS_EXCEPTION_IF_NULL(value_type);
  122. auto value_type_str = value_type->ToString();
  123. if (value_type_str == "Int32") {
  124. int data = GetValue<int>(value);
  125. attr_value.push_back(data);
  126. } else {
  127. attr_value = GetValue<std::vector<int>>(value);
  128. }
  129. mindspore::AttrValue input_shape_attr;
  130. mindspore::AttrValue_ArrayValue *input_shape_attr_list = input_shape_attr.mutable_array();
  131. MS_EXCEPTION_IF_NULL(input_shape_attr_list);
  132. for (const auto shape : attr_value) {
  133. input_shape_attr_list->add_i(shape);
  134. }
  135. (*node_attr)[attr_name] = input_shape_attr;
  136. } else {
  137. MS_LOG(EXCEPTION) << "type: " << type << "not support";
  138. }
  139. }
  140. void SetNodeAttr(const std::shared_ptr<AnfNode> &anf_node, mindspore::NodeDef *proto) {
  141. std::string op_name = AnfAlgo::GetCNodeName(anf_node);
  142. if (op_name == kInitDataSetQueue) {
  143. op_name = kInitData;
  144. }
  145. if (op_name == kPrint) {
  146. return;
  147. }
  148. auto op_info_ptr = mindspore::kernel::OpLib::FindOp(op_name, OpImplyType::kAICPU);
  149. MS_EXCEPTION_IF_NULL(op_info_ptr);
  150. auto attrs_ptr = op_info_ptr->attrs_ptr();
  151. auto primitive = AnfAlgo::GetCNodePrimitive(anf_node);
  152. MS_EXCEPTION_IF_NULL(primitive);
  153. ::google::protobuf::Map<::std::string, ::mindspore::AttrValue> *node_attr = proto->mutable_attrs();
  154. for (const auto &attr_ptr : attrs_ptr) {
  155. std::string attr_name = attr_ptr->name();
  156. auto value = primitive->GetAttr(attr_name);
  157. if (value != nullptr) {
  158. if (attr_name == kQueueName || attr_name == kSharedName) {
  159. attr_name = kChannelName;
  160. } else if (attr_name == kSeed) {
  161. attr_name = "seed";
  162. } else if (attr_name == kSeed2) {
  163. attr_name = "seed2";
  164. }
  165. std::string type = attr_ptr->type();
  166. ParseAttrValue(type, attr_name, value, node_attr);
  167. }
  168. }
  169. MS_LOG(INFO) << "Set node attr end!";
  170. }
  171. void SetNodeInputs(const std::shared_ptr<AnfNode> &anf_node, mindspore::NodeDef *proto) {
  172. size_t input_num = AnfAlgo::GetInputTensorNum(anf_node);
  173. if (input_num == 0) {
  174. MS_LOG(INFO) << "Node [" << AnfAlgo::GetCNodeName(anf_node) << "] does not have input.";
  175. return;
  176. }
  177. for (size_t input_index = 0; input_index < input_num; input_index++) {
  178. ::mindspore::Tensor *node_inputs = proto->add_inputs();
  179. MS_EXCEPTION_IF_NULL(node_inputs);
  180. TypeId input_type = AnfAlgo::GetInputDeviceDataType(anf_node, input_index);
  181. std::vector<size_t> input_shape;
  182. int32_t input_data_type;
  183. if (input_type == kObjectTypeString) {
  184. auto cnode = anf_node->cast<CNodePtr>();
  185. auto input_node = cnode->inputs()[input_index + 1];
  186. auto value_ptr = GetValueNode(input_node);
  187. auto value = GetValue<std::string>(value_ptr);
  188. input_shape.push_back(1);
  189. input_shape.push_back(value.size());
  190. input_data_type = AicpuOpUtil::MsTypeToProtoType(kTypeUnknown);
  191. } else {
  192. input_shape = AnfAlgo::GetInputDeviceShape(anf_node, input_index);
  193. input_data_type = AicpuOpUtil::MsTypeToProtoType(input_type);
  194. }
  195. mindspore::TensorShape *tensorShape = node_inputs->mutable_tensor_shape();
  196. for (auto item : input_shape) {
  197. mindspore::TensorShape_Dim *dim = tensorShape->add_dim();
  198. dim->set_size((::google::protobuf::int64)item);
  199. }
  200. node_inputs->set_tensor_type((mindspore::DataType)input_data_type);
  201. node_inputs->set_mem_device("HBM");
  202. }
  203. }
  204. void SetNodeOutputs(const std::shared_ptr<AnfNode> &anf_node, mindspore::NodeDef *proto) {
  205. size_t output_num = AnfAlgo::GetOutputTensorNum(anf_node);
  206. if (output_num == 0) {
  207. MS_LOG(INFO) << "Node [" << AnfAlgo::GetCNodeName(anf_node) << "] does not have output. ";
  208. return;
  209. }
  210. for (size_t output_index = 0; output_index < output_num; output_index++) {
  211. ::mindspore::Tensor *node_outputs = proto->add_outputs();
  212. std::vector<size_t> output_shape = AnfAlgo::GetOutputDeviceShape(anf_node, output_index);
  213. mindspore::TensorShape *tensorShape = node_outputs->mutable_tensor_shape();
  214. for (auto item : output_shape) {
  215. mindspore::TensorShape_Dim *dim = tensorShape->add_dim();
  216. dim->set_size((::google::protobuf::int64)item);
  217. }
  218. TypeId output_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index);
  219. int32_t output_data_type = AicpuOpUtil::MsTypeToProtoType(output_type);
  220. node_outputs->set_tensor_type((mindspore::DataType)output_data_type);
  221. node_outputs->set_mem_device("HBM");
  222. }
  223. }
  224. void SetNodedefProto(const std::shared_ptr<AnfNode> &anf_node, mindspore::NodeDef *proto) {
  225. MS_LOG(INFO) << "SetNodedefProto entry";
  226. MS_EXCEPTION_IF_NULL(anf_node);
  227. MS_EXCEPTION_IF_NULL(proto);
  228. std::string op_name = AnfAlgo::GetCNodeName(anf_node);
  229. if (op_name == "InitDataSetQueue") {
  230. op_name = "InitData";
  231. }
  232. // set op name
  233. proto->set_op(op_name);
  234. // set inputs tensor
  235. SetNodeInputs(anf_node, proto);
  236. // set outputs tensor
  237. SetNodeOutputs(anf_node, proto);
  238. // set node attr
  239. SetNodeAttr(anf_node, proto);
  240. MS_LOG(INFO) << "SetNodedefProto end!";
  241. }
  242. bool CreateNodeDefBytes(const std::shared_ptr<AnfNode> &anf_node,
  243. const std::shared_ptr<AicpuOpKernelMod> &kernel_mod_ptr) {
  244. MS_LOG(INFO) << "CreateNodeDefBytes entry";
  245. MS_EXCEPTION_IF_NULL(anf_node);
  246. MS_EXCEPTION_IF_NULL(kernel_mod_ptr);
  247. mindspore::NodeDef proto;
  248. SetNodedefProto(anf_node, &proto);
  249. std::string nodeDefStr;
  250. if (!proto.SerializeToString(&nodeDefStr)) {
  251. MS_LOG(ERROR) << "Serialize nodeDef to string failed.";
  252. return false;
  253. }
  254. kernel_mod_ptr->SetNodeDef(nodeDefStr);
  255. MS_LOG(INFO) << "CreateNodeDefBytes end!";
  256. return true;
  257. }
  258. KernelModPtr AicpuOpBuild(const std::shared_ptr<AnfNode> &anf_node) {
  259. MS_EXCEPTION_IF_NULL(anf_node);
  260. std::string op_name = AnfAlgo::GetCNodeName(anf_node);
  261. if (op_name == "InitDataSetQueue") {
  262. op_name = "InitData";
  263. }
  264. auto kernel_mod_ptr = std::make_shared<AicpuOpKernelMod>();
  265. MS_EXCEPTION_IF_NULL(kernel_mod_ptr);
  266. kernel_mod_ptr->SetAnfNode(anf_node);
  267. kernel_mod_ptr->SetNodeName(op_name);
  268. if (!CreateNodeDefBytes(anf_node, kernel_mod_ptr)) {
  269. MS_LOG(EXCEPTION) << "Create nodeDefBytes faild!";
  270. }
  271. if (!SetIOSize(anf_node, kernel_mod_ptr)) {
  272. MS_LOG(EXCEPTION) << "Set input output size list failed.";
  273. }
  274. return kernel_mod_ptr;
  275. }
  276. } // namespace kernel
  277. } // namespace mindspore