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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.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. namespace mindspore {
  25. namespace callbacks {
  26. const char PYTHON_MOD_CALLBACK_MODULE[] = "mindspore.train.callback._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. TensorPtr tensor;
  49. if (param_node->has_default()) {
  50. tensor = std::dynamic_pointer_cast<tensor::Tensor>(param_node->default_param()->value());
  51. }
  52. if (tensor == nullptr) {
  53. shape->push_back(ONE_SHAPE);
  54. } else {
  55. *shape = tensor->shape();
  56. }
  57. return true;
  58. }
  59. }
  60. MS_LOG(ERROR) << "Can not find parameter of name:" << param_name;
  61. return false;
  62. }
  63. static TensorPtr GetMeTensorTransformed(uint32_t graph_id, const std::string &parameter_name,
  64. const std::shared_ptr<ge::Tensor> &ge_tensor_ptr) {
  65. FuncGraphPtr anf_graph = transform::DfGraphManager::GetInstance().GetAnfGraph(graph_id);
  66. if (anf_graph == nullptr) {
  67. MS_LOG(ERROR) << "Get anf graph failed during callback";
  68. return nullptr;
  69. }
  70. std::shared_ptr<std::vector<int>> parameter_shape_ptr = std::make_shared<std::vector<int>>();
  71. if (!GetParameterShape(anf_graph, parameter_name, parameter_shape_ptr)) {
  72. MS_LOG(ERROR) << "Can not get parameter shape during callback";
  73. return nullptr;
  74. }
  75. return TransformUtil::ConvertGeTensor(ge_tensor_ptr, *parameter_shape_ptr);
  76. }
  77. uint32_t CheckpointSaveCallback(uint32_t graph_id, const std::map<std::string, ge::Tensor> &params_list) {
  78. // Acquire GIL before calling Python code
  79. py::gil_scoped_acquire acquire;
  80. MS_LOG(DEBUG) << "Start the checkpoint save callback function in checkpoint save process.";
  81. py::list parameter_list = py::list();
  82. for (auto &item : params_list) {
  83. std::string name = item.first;
  84. std::shared_ptr<ge::Tensor> ge_tensor_ptr = std::make_shared<ge::Tensor>(item.second);
  85. if (name.size() > 5 && name.compare(name.size() - 5, 5, "_temp") == 0) {
  86. continue;
  87. } else {
  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. }
  98. py::bool_ ret =
  99. parse::python_adapter::CallPyFn(PYTHON_MOD_CALLBACK_MODULE, PYTHON_FUN_PROCESS_CHECKPOINT, parameter_list);
  100. auto bool_ret = py::cast<bool>(ret);
  101. uint32_t status = Status::SUCCESS;
  102. if (!bool_ret) {
  103. status = Status::FAILED;
  104. MS_LOG(ERROR) << "Python checkpoint return false during callback";
  105. }
  106. return status;
  107. }
  108. static TensorPtr GetMeTensorForSummary(const std::string &name, const std::shared_ptr<ge::Tensor> &ge_tensor_ptr) {
  109. // confirm the type by name
  110. // Format: xxx[:Scalar] xxx[:Image] xxx[:Tensor]
  111. if (name.empty()) {
  112. MS_LOG(EXCEPTION) << "The summary name is empty.";
  113. }
  114. auto bpos = name.rfind("[:");
  115. if (bpos >= name.size()) {
  116. MS_LOG(EXCEPTION) << "The summary name(" << name << ") is invalid.";
  117. }
  118. auto tname = name.substr(bpos);
  119. if (tname == "[:Scalar]") {
  120. MS_LOG(DEBUG) << "The summary(" << name << ") is Scalar";
  121. // process the scalar type summary
  122. // Because the ge tensor is dim = 4, so set the (1,1,1,1)-->(1,)
  123. // We do the (1,) shape is scalar
  124. auto shape = std::vector<int>({ONE_SHAPE});
  125. return TransformUtil::ConvertGeTensor(ge_tensor_ptr, shape);
  126. }
  127. if (tname == "[:Tensor]" || tname == "[:Histogram]") {
  128. MS_LOG(DEBUG) << "The summary(" << name << ") is Tensor";
  129. // process the tensor summary
  130. // Now we can't get the real shape, so we keep same shape with GE
  131. return TransformUtil::ConvertGeTensor(ge_tensor_ptr);
  132. }
  133. if (tname == "[:Image]") {
  134. MS_LOG(DEBUG) << "The summary(" << name << ") is Image";
  135. // process the Image summary
  136. // Image dim = 4, is same with ge, so we keep same shape with GE
  137. return TransformUtil::ConvertGeTensor(ge_tensor_ptr);
  138. }
  139. MS_LOG(EXCEPTION) << "The summary name(" << name << ") is invalid.";
  140. }
  141. // Cache the summary callback data
  142. // Output Format: [{"name": tag_name, "data": tensor}, {"name": tag_name, "data": tensor},...]
  143. uint32_t MS_EXPORT SummarySaveCallback(uint32_t graph_id, const std::map<std::string, ge::Tensor> &params_list) {
  144. // Acquire GIL before calling Python code
  145. py::gil_scoped_acquire acquire;
  146. MS_LOG(DEBUG) << "Start the summary save callback function for graph " << graph_id << ".";
  147. py::list summary_list = py::list();
  148. MS_LOG(DEBUG) << "Param list size = " << params_list.size();
  149. for (auto &item : params_list) {
  150. std::string tag_name = item.first;
  151. std::shared_ptr<ge::Tensor> ge_tensor_ptr = std::make_shared<ge::Tensor>(item.second);
  152. TensorPtr tensor_ptr = GetMeTensorForSummary(tag_name, ge_tensor_ptr);
  153. if (tensor_ptr == nullptr) {
  154. MS_LOG(EXCEPTION) << "ConvertGeTensor return tensor is null";
  155. }
  156. py::dict summary_value_dict;
  157. summary_value_dict["name"] = tag_name;
  158. summary_value_dict["data"] = tensor_ptr;
  159. summary_list.append(summary_value_dict);
  160. }
  161. py::bool_ ret = parse::python_adapter::CallPyFn(PYTHON_MOD_CALLBACK_MODULE, PYTHON_FUN_PROCESS_SUMMARY, summary_list);
  162. auto bool_ret = py::cast<bool>(ret);
  163. if (!bool_ret) {
  164. MS_LOG(ERROR) << "Python checkpoint return false during callback";
  165. return Status::FAILED;
  166. }
  167. MS_LOG(DEBUG) << "End the summary save callback function.";
  168. return Status::SUCCESS;
  169. }
  170. } // namespace callbacks
  171. } // namespace mindspore