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

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