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.

callbacks_ge.cc 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "utils/callbacks_ge.h"
  17. #include "pybind11/pybind11.h"
  18. #include "ir/param_value_py.h"
  19. #include "transform/df_graph_manager.h"
  20. #include "transform/util.h"
  21. #include "pipeline/parse/data_converter.h"
  22. #include "pipeline/parse/python_adapter.h"
  23. #include "utils/visible.h"
  24. namespace mindspore {
  25. namespace callbacks {
  26. const char PYTHON_MOD_CALLBACK_MODULE[] = "mindspore.train.callback";
  27. const char PYTHON_FUN_PROCESS_CHECKPOINT[] = "_checkpoint_cb_for_save_op";
  28. const char PYTHON_FUN_PROCESS_SUMMARY[] = "_summary_cb_for_save_op";
  29. const char kSummary[] = "Summary";
  30. const char kCheckPoint[] = "Save";
  31. const int ONE_SHAPE = 1;
  32. using mindspore::transform::Status;
  33. using mindspore::transform::TransformUtil;
  34. bool GetParameterShape(const FuncGraphPtr &graph, const std::string &param_name,
  35. const std::shared_ptr<std::vector<int>> &shape) {
  36. if (graph == nullptr) {
  37. MS_LOG(ERROR) << "Graph is null, can not get graph parameter";
  38. return false;
  39. }
  40. auto parameter_nodes = graph->parameters();
  41. for (auto &node : parameter_nodes) {
  42. ParameterPtr param_node = std::static_pointer_cast<Parameter>(node);
  43. if (param_node == nullptr) {
  44. MS_LOG(ERROR) << "Parameter node is null, can not get graph parameter";
  45. return false;
  46. }
  47. if (param_node->name() == param_name) {
  48. py::object parameter;
  49. if (param_node->has_default()) {
  50. auto param_value = std::dynamic_pointer_cast<ParamValuePy>(param_node->default_param());
  51. parameter = param_value->value();
  52. }
  53. ValuePtr value = parse::data_converter::PyDataToValue(parameter);
  54. TensorPtr tensor = std::dynamic_pointer_cast<tensor::Tensor>(value);
  55. if (tensor == nullptr) {
  56. shape->push_back(ONE_SHAPE);
  57. } else {
  58. *shape = tensor->shape();
  59. }
  60. return true;
  61. }
  62. }
  63. MS_LOG(ERROR) << "Can not find parameter of name:" << param_name;
  64. return false;
  65. }
  66. static TensorPtr GetMeTensorTransformed(uint32_t graph_id, const std::string &parameter_name,
  67. const std::shared_ptr<ge::Tensor> &ge_tensor_ptr) {
  68. FuncGraphPtr anf_graph = transform::DfGraphManager::GetInstance().GetAnfGraph(graph_id);
  69. if (anf_graph == nullptr) {
  70. MS_LOG(ERROR) << "Get anf graph failed during callback";
  71. return nullptr;
  72. }
  73. std::shared_ptr<std::vector<int>> parameter_shape_ptr = std::make_shared<std::vector<int>>();
  74. if (!GetParameterShape(anf_graph, parameter_name, parameter_shape_ptr)) {
  75. MS_LOG(ERROR) << "Can not get parameter shape during callback";
  76. return nullptr;
  77. }
  78. return TransformUtil::ConvertGeTensor(ge_tensor_ptr, *parameter_shape_ptr);
  79. }
  80. uint32_t CheckpointSaveCallback(uint32_t graph_id, const std::map<std::string, ge::Tensor> &params_list) {
  81. // Acquire GIL before calling Python code
  82. py::gil_scoped_acquire acquire;
  83. MS_LOG(DEBUG) << "Start the checkpoint save callback function in checkpoint save process.";
  84. py::list parameter_list = py::list();
  85. for (auto &item : params_list) {
  86. std::string name = item.first;
  87. std::shared_ptr<ge::Tensor> ge_tensor_ptr = std::make_shared<ge::Tensor>(item.second);
  88. TensorPtr tensor_ptr = GetMeTensorTransformed(graph_id, name, ge_tensor_ptr);
  89. if (tensor_ptr == nullptr) {
  90. MS_LOG(EXCEPTION) << "Transform ge tensor to me tensor failed";
  91. }
  92. py::dict param_dict;
  93. param_dict["name"] = name;
  94. param_dict["data"] = tensor_ptr;
  95. parameter_list.append(param_dict);
  96. }
  97. py::bool_ ret =
  98. parse::python_adapter::CallPyFn(PYTHON_MOD_CALLBACK_MODULE, PYTHON_FUN_PROCESS_CHECKPOINT, parameter_list);
  99. auto bool_ret = py::cast<bool>(ret);
  100. uint32_t status = Status::SUCCESS;
  101. if (!bool_ret) {
  102. status = Status::FAILED;
  103. MS_LOG(ERROR) << "Python checkpoint return false during callback";
  104. }
  105. return status;
  106. }
  107. static TensorPtr GetMeTensorForSummary(const std::string &name, const std::shared_ptr<ge::Tensor> &ge_tensor_ptr) {
  108. // confirm the type by name
  109. // Format: xxx[:Scalar] xxx[:Image] xxx[:Tensor]
  110. if (name.empty()) {
  111. MS_LOG(EXCEPTION) << "The summary name is empty.";
  112. }
  113. auto bpos = name.rfind("[:");
  114. if (bpos >= name.size()) {
  115. MS_LOG(EXCEPTION) << "The summary name(" << name << ") is invalid.";
  116. }
  117. auto tname = name.substr(bpos);
  118. if (tname == "[:Scalar]") {
  119. MS_LOG(DEBUG) << "The summary(" << name << ") is Scalar";
  120. // process the scalar type summary
  121. // Because the ge tensor is dim = 4, so set the (1,1,1,1)-->(1,)
  122. // We do the (1,) shape is scalar
  123. auto shape = std::vector<int>({ONE_SHAPE});
  124. return TransformUtil::ConvertGeTensor(ge_tensor_ptr, shape);
  125. }
  126. if (tname == "[:Tensor]" || tname == "[:Histogram]") {
  127. MS_LOG(DEBUG) << "The summary(" << name << ") is Tensor";
  128. // process the tensor summary
  129. // Now we can't get the real shape, so we keep same shape with GE
  130. return TransformUtil::ConvertGeTensor(ge_tensor_ptr);
  131. }
  132. if (tname == "[:Image]") {
  133. MS_LOG(DEBUG) << "The summary(" << name << ") is Image";
  134. // process the Image summary
  135. // Image dim = 4, is same with ge, so we keep same shape with GE
  136. return TransformUtil::ConvertGeTensor(ge_tensor_ptr);
  137. }
  138. MS_LOG(EXCEPTION) << "The summary name(" << name << ") is invalid.";
  139. }
  140. // Cache the summary callback data
  141. // Output Format: [{"name": tag_name, "data": tensor}, {"name": tag_name, "data": tensor},...]
  142. uint32_t MS_EXPORT SummarySaveCallback(uint32_t graph_id, const std::map<std::string, ge::Tensor> &params_list) {
  143. // Acquire GIL before calling Python code
  144. py::gil_scoped_acquire acquire;
  145. MS_LOG(DEBUG) << "Start the summary save callback function for graph " << graph_id << ".";
  146. py::list summary_list = py::list();
  147. MS_LOG(DEBUG) << "Param list size = " << params_list.size();
  148. for (auto &item : params_list) {
  149. std::string tag_name = item.first;
  150. std::shared_ptr<ge::Tensor> ge_tensor_ptr = std::make_shared<ge::Tensor>(item.second);
  151. TensorPtr tensor_ptr = GetMeTensorForSummary(tag_name, ge_tensor_ptr);
  152. if (tensor_ptr == nullptr) {
  153. MS_LOG(EXCEPTION) << "ConvertGeTensor return tensor is null";
  154. }
  155. py::dict summary_value_dict;
  156. summary_value_dict["name"] = tag_name;
  157. summary_value_dict["data"] = tensor_ptr;
  158. summary_list.append(summary_value_dict);
  159. }
  160. py::bool_ ret = parse::python_adapter::CallPyFn(PYTHON_MOD_CALLBACK_MODULE, PYTHON_FUN_PROCESS_SUMMARY, summary_list);
  161. auto bool_ret = py::cast<bool>(ret);
  162. if (!bool_ret) {
  163. MS_LOG(ERROR) << "Python checkpoint return false during callback";
  164. return Status::FAILED;
  165. }
  166. MS_LOG(DEBUG) << "End the summary save callback function.";
  167. return Status::SUCCESS;
  168. }
  169. } // namespace callbacks
  170. } // namespace mindspore