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.

primitive_py.cc 19 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * Copyright 2019-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 "pybind_api/ir/primitive_py.h"
  17. #include <mutex>
  18. #include <map>
  19. #include <utility>
  20. #include "ir/signature.h"
  21. #include "pipeline/jit/parse/data_converter.h"
  22. #include "pipeline/jit/parse/python_adapter.h"
  23. #include "pybind11/pytypes.h"
  24. #include "pybind_api/api_register.h"
  25. #include "pybind_api/export_flags.h"
  26. #include "pybind_api/ir/base_ref_py.h"
  27. #include "utils/convert_utils_base.h"
  28. #include "utils/convert_utils_py.h"
  29. #include "utils/ms_context.h"
  30. #include "utils/primitive_utils.h"
  31. #include "utils/check_convert_utils.h"
  32. #include "pipeline/jit/resource.h"
  33. #include "pipeline/pynative/pynative_execute.h"
  34. namespace mindspore {
  35. namespace {
  36. constexpr auto kBpropAttrName = "bprop";
  37. constexpr auto kCellHookAttrName = "cell_hook";
  38. constexpr auto kCellIDAttrName = "cell_id";
  39. std::map<std::string, std::string> kOpAttrNameReplaceMap = {
  40. {"data_format", "format"},
  41. };
  42. void SyncData(const py::object &arg) {
  43. if (py::isinstance<py::tuple>(arg)) {
  44. py::tuple arg_list = py::cast<py::tuple>(arg);
  45. for (size_t i = 0; i < arg_list.size(); i++) {
  46. SyncData(arg_list[i]);
  47. }
  48. }
  49. if (py::isinstance<tensor::Tensor>(arg)) {
  50. auto tensor = py::cast<tensor::TensorPtr>(arg);
  51. tensor->data_sync();
  52. }
  53. }
  54. } // namespace
  55. std::map<std::string, py::object> PrimitivePy::hook_grad_;
  56. PrimitivePy::PrimitivePy(const std::string &name) : Primitive(name, false), python_obj_(py::none()) {}
  57. PrimitivePy::PrimitivePy(const py::object &python_obj, const PrimitivePyAdapterPtr &adapter)
  58. : Primitive(adapter->name_, false), python_obj_(python_obj), adapter_(adapter) {
  59. MS_LOG(DEBUG) << "New primitive:" << adapter->name_;
  60. set_signatures(adapter->signatures_);
  61. Primitive::SetAttrs(adapter->attrs_);
  62. Primitive::set_prim_type(adapter->prim_type_);
  63. Primitive::set_const_prim(adapter->is_const_prim_);
  64. Primitive::set_const_input_indexes(adapter->const_input_indexes_);
  65. set_hook(adapter->hook_);
  66. set_instance_name(adapter->instance_name_);
  67. }
  68. PrimitivePy::~PrimitivePy() { MS_LOG(DEBUG) << "Release:" << ToString(); }
  69. void PrimitivePy::set_signatures(const std::vector<Signature> &signatures) {
  70. signatures_ = signatures;
  71. set_has_signature(!signatures.empty());
  72. }
  73. py::function PrimitivePy::GetBpropFunction() {
  74. static const char *const get_bprop_func_name = "get_bprop";
  75. if (py::hasattr(python_obj_, get_bprop_func_name)) {
  76. py::function fn = python_obj_.attr(get_bprop_func_name)().cast<py::function>();
  77. return fn;
  78. } else {
  79. auto fn = GetBpropFunctionByObj(python_obj_);
  80. return fn;
  81. }
  82. }
  83. py::tuple check_bprop_out(const py::object &grads_obj, const py::tuple &py_args) {
  84. py::tuple grads;
  85. if (!py::isinstance<py::tuple>(grads_obj)) {
  86. grads = py::make_tuple(grads_obj);
  87. } else {
  88. grads = py::cast<py::tuple>(grads_obj);
  89. }
  90. if (grads.size() != py_args.size() - 2) {
  91. MS_EXCEPTION(ValueError) << "For user define net bprop, the gradients number: " << grads.size()
  92. << " is not equal to the args number: " << py_args.size() - 2 << ".";
  93. }
  94. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_CHECK_BPROP_FLAG)) {
  95. for (size_t i = 0; i < grads.size(); i++) {
  96. if (py::isinstance<tensor::Tensor>(py_args[i])) {
  97. if (!py::isinstance<tensor::Tensor>(grads[i])) {
  98. MS_EXCEPTION(ValueError) << "When user defines the net bprop,, the gradient of the " << i
  99. << "th arg should be Tensor, but got "
  100. << py::cast<std::string>(grads[i].attr("__class__").attr("__name__"))
  101. << ", and the value is " << py::cast<py::str>(grads[i]) << ".";
  102. }
  103. py::object arg_dtype = py_args[i].attr("dtype");
  104. py::object grad_dtype = grads[i].attr("dtype");
  105. py::tuple arg_shape = py_args[i].attr("shape");
  106. py::tuple grad_shape = grads[i].attr("shape");
  107. if (!grad_dtype.equal(arg_dtype)) {
  108. MS_EXCEPTION(TypeError) << "When user defines the net bprop, the gradient of the " << i
  109. << "th arg should have the same dtype as the " << i << "th arg, but the " << i
  110. << "th arg dtype is: " << py::cast<py::str>(arg_dtype)
  111. << ", the gradient dtype is: " << py::cast<py::str>(grad_dtype) << ".";
  112. }
  113. if (!grad_shape.equal(arg_shape)) {
  114. MS_EXCEPTION(ValueError) << "When user defines the net bprop, the gradient of the " << i
  115. << "th arg should have the same shape as the " << i << "th arg, but the " << i
  116. << "th arg shape is: " << py::cast<py::str>(arg_shape)
  117. << ", the gradient shape is: " << py::cast<py::str>(grad_shape) << ".";
  118. }
  119. }
  120. }
  121. }
  122. return grads;
  123. }
  124. void PrimitivePy::ConvertCTensorToPyTensor(const py::tuple &input_args, py::tuple *convert_args) const {
  125. MS_EXCEPTION_IF_NULL(convert_args);
  126. if (input_args.size() != (*convert_args).size()) {
  127. MS_LOG(EXCEPTION) << "The size of input_args: " << input_args.size()
  128. << " should be equal to the size of convert_args: " << (*convert_args).size();
  129. }
  130. for (size_t i = 0; i < input_args.size(); ++i) {
  131. (*convert_args)[i] = py::isinstance<tensor::Tensor>(input_args[i])
  132. ? parse::python_adapter::CallPyFn(parse::PYTHON_MOD_PARSE_MODULE,
  133. parse::PYTHON_MOD_CONVERT_TO_MS_TENSOR, input_args[i])
  134. : input_args[i];
  135. }
  136. }
  137. void PrimitivePy::CheckHookConsistency(const py::object &grad_out, const py::object &expected_grad_out) const {
  138. if (py::isinstance<py::tuple>(expected_grad_out)) {
  139. if (!py::isinstance<py::tuple>(grad_out)) {
  140. hook_grad_.clear();
  141. MS_EXCEPTION(TypeError) << "The output gradient should be a tuple!";
  142. }
  143. auto actual_out_tuple = py::cast<py::tuple>(grad_out);
  144. auto expected_out_tuple = py::cast<py::tuple>(expected_grad_out);
  145. if (actual_out_tuple.size() != expected_out_tuple.size()) {
  146. hook_grad_.clear();
  147. MS_EXCEPTION(ValueError) << "The tuple size of output gradient should be " << expected_out_tuple.size()
  148. << ", but it is " << actual_out_tuple.size();
  149. }
  150. for (size_t i = 0; i < expected_out_tuple.size(); ++i) {
  151. CheckHookConsistency(actual_out_tuple[i], expected_out_tuple[i]);
  152. }
  153. }
  154. if (py::isinstance<tensor::Tensor>(expected_grad_out)) {
  155. if (!py::isinstance<tensor::Tensor>(grad_out)) {
  156. hook_grad_.clear();
  157. MS_EXCEPTION(TypeError) << "The output gradient should be a tensor!";
  158. }
  159. auto actual_out_tensor = py::cast<tensor::TensorPtr>(grad_out);
  160. auto expected_out_tensor = py::cast<tensor::TensorPtr>(expected_grad_out);
  161. MS_EXCEPTION_IF_NULL(actual_out_tensor);
  162. MS_EXCEPTION_IF_NULL(expected_out_tensor);
  163. if (actual_out_tensor->GetShapeAndDataTypeInfo() != expected_out_tensor->GetShapeAndDataTypeInfo()) {
  164. hook_grad_.clear();
  165. MS_EXCEPTION(ValueError) << "The output gradient is not consistent with the expected, it should be "
  166. << expected_out_tensor->GetShapeAndDataTypeInfo() << ", but it is "
  167. << actual_out_tensor->GetShapeAndDataTypeInfo();
  168. }
  169. }
  170. }
  171. BaseRef PrimitivePy::RunBpropHookFunction(const py::tuple &py_args) const {
  172. SyncData(py_args);
  173. auto size = py_args.size();
  174. py::tuple input_args(size - 2);
  175. for (size_t i = 0; i < size - 2; ++i) {
  176. input_args[i] = py_args[i];
  177. }
  178. py::tuple convert_args(py_args.size());
  179. ConvertCTensorToPyTensor(py_args, &convert_args);
  180. auto inst = pynative::PynativeExecutor::GetInstance();
  181. MS_EXCEPTION_IF_NULL(inst);
  182. try {
  183. MS_LOG(DEBUG) << "Run bprop function start";
  184. inst->NewGraph(hook_, input_args.cast<py::args>());
  185. py::object grads_obj = hook_(*convert_args);
  186. py::tuple grads = check_bprop_out(grads_obj, py_args);
  187. inst->EndGraph(hook_, grads_obj, input_args.cast<py::args>());
  188. MS_LOG(DEBUG) << "Run bprop function end";
  189. return std::make_shared<PyObjectRef>(grads);
  190. } catch (std::exception &bt) {
  191. inst->ClearRes();
  192. std::rethrow_exception(std::current_exception());
  193. }
  194. }
  195. BaseRef PrimitivePy::RunHookFunction(const VectorRef &args) const {
  196. py::tuple py_args = ConvertDatatoPyTuple(args);
  197. bool is_bprop = this->HasAttr(kBpropAttrName);
  198. if (is_bprop) {
  199. return RunBpropHookFunction(py_args);
  200. }
  201. SyncData(py_args[2]);
  202. bool is_cell = this->HasAttr(kCellHookAttrName);
  203. py::object obj;
  204. if (is_cell) {
  205. auto cell_id = GetValue<std::string>(this->GetAttr(kCellIDAttrName));
  206. auto iter = hook_grad_.find(cell_id);
  207. if (iter != hook_grad_.end()) {
  208. py::tuple convert_args(2);
  209. py::tuple input_args(2);
  210. input_args[0] = iter->second;
  211. input_args[1] = py_args[2];
  212. ConvertCTensorToPyTensor(input_args, &convert_args);
  213. auto hook_args = py::tuple(3);
  214. hook_args[0] = cell_id;
  215. hook_args[1] = py::make_tuple(convert_args[0]);
  216. hook_args[2] = py::make_tuple(convert_args[1]);
  217. obj = hook_(*hook_args);
  218. if (py::isinstance<py::none>(obj)) {
  219. obj = py_args[2];
  220. }
  221. CheckHookConsistency(obj, py_args[2]);
  222. (void)hook_grad_.erase(cell_id);
  223. } else {
  224. hook_grad_[cell_id] = py_args[2];
  225. obj = py_args[2];
  226. }
  227. } else {
  228. // Hook operator for execute variable hook function
  229. obj = hook_(py::make_tuple(py_args[2]));
  230. if (py::isinstance<py::none>(obj)) {
  231. obj = py_args[2];
  232. }
  233. CheckHookConsistency(obj, py_args[2]);
  234. }
  235. obj = py::make_tuple(obj);
  236. return std::make_shared<PyObjectRef>(obj);
  237. }
  238. py::function PrimitivePy::GetComputeFunction() const {
  239. static const char *const compute_func_name = "vm_impl";
  240. if (py::hasattr(python_obj_, compute_func_name)) {
  241. MS_LOG(INFO) << name() << " compute_func_name";
  242. py::function fn = python_obj_.attr(compute_func_name).cast<py::function>();
  243. return fn;
  244. }
  245. static const std::string vm_module = "mindspore.ops.vm_impl_registry";
  246. static const std::string get_vm_impl_fn = "get_vm_impl_fn";
  247. MS_LOG(INFO) << name() << ": get_vm_impl_fn";
  248. py::function get_fn = parse::python_adapter::GetPyFn(vm_module, get_vm_impl_fn);
  249. py::function vm_fn = get_fn(python_obj_);
  250. if (py::isinstance<py::none>(vm_fn)) {
  251. MS_LOG(INFO) << "Cannot find " << python_obj_.attr("__class__").attr("__name__").cast<std::string>();
  252. vm_fn = mindspore::GetComputeFunction(Primitive::name());
  253. }
  254. return vm_fn;
  255. }
  256. py::dict PrimitivePy::GetAttrDict() {
  257. py::dict attr_dict;
  258. for (auto &attr : attrs_) {
  259. attr_dict[py::str(attr.first)] = ValuePtrToPyData(attr.second);
  260. }
  261. return attr_dict;
  262. }
  263. void PrimitivePy::CopyHookFunction(const PrimitivePtr &primitive) {
  264. MS_EXCEPTION_IF_NULL(primitive);
  265. if (!primitive->isa<PrimitivePy>()) {
  266. MS_LOG(EXCEPTION) << "Cannot copy a primtive which is not python primitive hook function to python primitive!";
  267. }
  268. auto primitive_py = primitive->cast<PrimitivePyPtr>();
  269. MS_EXCEPTION_IF_NULL(primitive_py);
  270. this->set_hook(primitive_py->hook());
  271. }
  272. BaseRef PrimitivePy::RunComputeFunction(const VectorRef &args) const {
  273. auto py_args = ConvertDatatoPyTuple(args);
  274. auto result = this->RunPyComputeFunction(py_args);
  275. if (py::isinstance<py::none>(result)) {
  276. return std::make_shared<BaseRef>(nullptr);
  277. }
  278. return std::make_shared<PyObjectRef>(result);
  279. }
  280. py::object PrimitivePy::RunPyComputeFunction(const py::tuple &py_args) const {
  281. auto func = this->GetComputeFunction();
  282. if (py::isinstance<py::none>(func)) {
  283. return py::none();
  284. }
  285. auto result = func(*py_args);
  286. return result;
  287. }
  288. bool PrimitivePy::HasComputeFunction() const {
  289. auto func = GetComputeFunction();
  290. return !py::isinstance<py::none>(func);
  291. }
  292. PrimitivePtr PrimitivePy::Clone() {
  293. auto clone_fn = python_obj_.attr("_clone");
  294. py::object obj_adapter = clone_fn();
  295. auto prim_adapter = obj_adapter.cast<PrimitivePyAdapterPtr>();
  296. auto prim = std::make_shared<PrimitivePy>(obj_adapter, prim_adapter);
  297. prim_adapter->set_attached_primitive(prim);
  298. return prim;
  299. }
  300. py::dict PrimitivePy::RunInfer(const py::tuple &args) {
  301. if (!HasPyObj()) {
  302. MS_LOG(EXCEPTION) << "[" << this->ToString() << "]: pyobj is empty";
  303. }
  304. // Python obj could be replaced as None, so it will losed the original info when throw exception in python.
  305. if (!py::hasattr(python_obj_, PY_PRIM_METHOD_INFER)) {
  306. MS_LOG(EXCEPTION) << "prim:" << ToString() << " has no attr:" << PY_PRIM_METHOD_INFER;
  307. }
  308. auto infer_fuc = python_obj_.attr(PY_PRIM_METHOD_INFER);
  309. return infer_fuc(*args);
  310. }
  311. void PrimitivePy::RunCheck(const py::tuple &args) {
  312. if (!HasPyObj()) {
  313. MS_LOG(EXCEPTION) << "[" << this->ToString() << "]: pyobj is empty";
  314. }
  315. // Python obj could be replaced as None, so it will losed the original info when throw exception in python.
  316. if (!py::hasattr(python_obj_, PY_PRIM_METHOD_CHECK)) {
  317. MS_LOG(EXCEPTION) << "prim:" << ToString() << " has no attr:" << PY_PRIM_METHOD_CHECK;
  318. }
  319. auto check_func = python_obj_.attr(PY_PRIM_METHOD_CHECK);
  320. (void)check_func(*args);
  321. }
  322. py::object PrimitivePy::RunInferValue(const py::tuple &args) {
  323. if (!HasPyObj()) {
  324. MS_LOG(EXCEPTION) << "[" << this->ToString() << "]: pyobj is empty";
  325. }
  326. // Python obj could be replaced as None, so it will losed the original info when throw exception in python.
  327. if (!py::hasattr(python_obj_, PY_PRIM_METHOD_INFER_VALUE)) {
  328. MS_LOG(EXCEPTION) << "prim:" << ToString() << " has no attr:" << PY_PRIM_METHOD_INFER_VALUE;
  329. }
  330. auto infer_value = python_obj_.attr(PY_PRIM_METHOD_INFER_VALUE);
  331. return infer_value(*args);
  332. }
  333. PrimitivePyAdapter::PrimitivePyAdapter(const py::str &name) : name_(name) {}
  334. void PrimitivePyAdapter::AddPyAttr(const py::str &name, const py::object &obj) {
  335. std::string attr_name = name;
  336. ValuePtr converted_ret = nullptr;
  337. if (py::isinstance<py::module>(obj)) {
  338. MS_LOG(EXCEPTION) << "AddPyAttr failed, obj should not be py::module";
  339. }
  340. bool converted = parse::ConvertData(obj, &converted_ret);
  341. if (!converted) {
  342. MS_LOG(EXCEPTION) << "Attribute convert error with type: " << std::string(py::str(obj));
  343. }
  344. if (kOpAttrNameReplaceMap.find(attr_name) != kOpAttrNameReplaceMap.end()) {
  345. attr_name = kOpAttrNameReplaceMap[attr_name];
  346. }
  347. CheckAndConvertUtils::ConvertAttrValueToInt(name_, name, &converted_ret);
  348. auto prim = attached_primitive_.lock();
  349. if (prim != nullptr) {
  350. prim->AddAttr(attr_name, converted_ret);
  351. } else {
  352. attrs_[attr_name] = converted_ret;
  353. }
  354. }
  355. void PrimitivePyAdapter::DelPyAttr(const py::str &name) {
  356. std::string attr_name = name;
  357. auto prim = attached_primitive_.lock();
  358. if (prim != nullptr) {
  359. prim->DelAttr(attr_name);
  360. } else {
  361. attrs_.erase(attr_name);
  362. }
  363. }
  364. py::dict PrimitivePyAdapter::GetAttrDict() {
  365. auto prim = attached_primitive_.lock();
  366. if (prim != nullptr) {
  367. return prim->GetAttrDict();
  368. }
  369. py::dict attr_dict;
  370. for (auto &attr : attrs_) {
  371. attr_dict[py::str(attr.first)] = ValuePtrToPyData(attr.second);
  372. }
  373. return attr_dict;
  374. }
  375. void PrimitivePyAdapter::set_prim_type(const PrimType t) {
  376. auto prim = attached_primitive_.lock();
  377. if (prim != nullptr) {
  378. prim->set_prim_type(t);
  379. } else {
  380. prim_type_ = t;
  381. }
  382. }
  383. void PrimitivePyAdapter::set_const_prim(bool is_const_prim) {
  384. auto prim = attached_primitive_.lock();
  385. if (prim != nullptr) {
  386. prim->set_const_prim(is_const_prim);
  387. } else {
  388. is_const_prim_ = is_const_prim;
  389. }
  390. }
  391. void PrimitivePyAdapter::set_const_input_indexes(const std::vector<size_t> &const_input_indexes) {
  392. auto prim = attached_primitive_.lock();
  393. if (prim != nullptr) {
  394. prim->set_const_input_indexes(const_input_indexes);
  395. } else {
  396. const_input_indexes_ = const_input_indexes;
  397. }
  398. }
  399. void PrimitivePyAdapter::set_signatures(const std::vector<Signature> &signatures) {
  400. auto prim = attached_primitive_.lock();
  401. if (prim != nullptr) {
  402. prim->set_signatures(signatures);
  403. } else {
  404. signatures_ = signatures;
  405. }
  406. }
  407. void PrimitivePyAdapter::set_hook(const py::function &hook) {
  408. auto prim = attached_primitive_.lock();
  409. if (prim != nullptr) {
  410. prim->set_hook(hook);
  411. } else {
  412. hook_ = hook;
  413. }
  414. }
  415. void PrimitivePyAdapter::set_instance_name(const std::string &s) {
  416. auto prim = attached_primitive_.lock();
  417. if (prim != nullptr) {
  418. prim->set_instance_name(s);
  419. } else {
  420. instance_name_ = s;
  421. }
  422. }
  423. void PrimitivePyAdapter::set_attached_primitive(const PrimitivePyPtr &prim) {
  424. if (attached_primitive_.lock() != nullptr) {
  425. MS_LOG(EXCEPTION) << "PrimitivePyAdapter can't attach to multi Primitive.";
  426. }
  427. MS_EXCEPTION_IF_NULL(prim);
  428. attached_primitive_ = prim;
  429. }
  430. REGISTER_PYBIND_DEFINE(Primitive_, ([](const py::module *m) {
  431. (void)py::enum_<PrimType>(*m, "prim_type", py::arithmetic())
  432. .value("unknown", PrimType::kPrimTypeUnknown)
  433. .value("builtin", PrimType::kPrimTypeBuiltIn)
  434. .value("py_infer_shape", PrimType::kPrimTypePyInfer)
  435. .value("user_custom", PrimType::kPrimTypeUserCustom)
  436. .value("py_infer_check", PrimType::kPrimTypePyCheck);
  437. (void)py::class_<PrimitivePyAdapter, std::shared_ptr<PrimitivePyAdapter>>(*m, "Primitive_")
  438. .def_readonly(PYTHON_PRIMITIVE_FLAG, &PrimitivePyAdapter::parse_info_)
  439. .def(py::init<py::str &>())
  440. .def("add_attr", &PrimitivePyAdapter::AddPyAttr, "add primitive attr")
  441. .def("del_attr", &PrimitivePyAdapter::DelPyAttr, "del primitive attr")
  442. .def("get_attr_dict", &PrimitivePyAdapter::GetAttrDict, "get primitive attr")
  443. .def("set_prim_type", &PrimitivePyAdapter::set_prim_type, "Set primitive type.")
  444. .def("set_const_prim", &PrimitivePyAdapter::set_const_prim, "Set primitive is const.")
  445. .def("set_const_input_indexes", &PrimitivePyAdapter::set_const_input_indexes,
  446. "Set primitive const input indexes.")
  447. .def("set_signatures", &PrimitivePyAdapter::set_signatures,
  448. "Set primitive inputs signature.")
  449. .def("register_hook", &PrimitivePyAdapter::set_hook, "Set primitive hook function.")
  450. .def("set_instance_name", &PrimitivePyAdapter::set_instance_name,
  451. "Set primitive instance name.");
  452. }));
  453. } // namespace mindspore