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.1 kB

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