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.

ascend_inference_session.cc 11 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <algorithm>
  17. #include "backend/session/ascend_inference_session.h"
  18. #include "ir/tensor.h"
  19. #include "ir/anf.h"
  20. #include "ir/param_info.h"
  21. #include "runtime/device/kernel_runtime.h"
  22. #include "backend/session/anf_runtime_algorithm.h"
  23. #include "utils/ms_utils.h"
  24. #include "common/trans.h"
  25. #include "utils/config_manager.h"
  26. namespace mindspore {
  27. namespace session {
  28. void AscendInferenceSession::LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  29. const std::vector<tensor::TensorPtr> &inputs_const) const {
  30. MS_EXCEPTION_IF_NULL(kernel_graph);
  31. std::vector<tensor::TensorPtr> inputs(inputs_const);
  32. auto input_nodes = kernel_graph->inputs();
  33. size_t no_weight_input = 0;
  34. for (size_t i = 0; i < input_nodes.size(); ++i) {
  35. tensor::TensorPtr tensor = nullptr;
  36. if (!input_nodes[i]->isa<Parameter>()) {
  37. MS_LOG(ERROR) << "Kernel graph inputs have anfnode which is not Parameter";
  38. continue;
  39. }
  40. auto pk_node = input_nodes[i]->cast<ParameterPtr>();
  41. MS_EXCEPTION_IF_NULL(pk_node);
  42. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  43. MS_EXCEPTION_IF_NULL(device_address);
  44. if (!AnfAlgo::IsParameterWeight(pk_node)) {
  45. tensor = inputs[no_weight_input++];
  46. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  47. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  48. tensor->data_c())) {
  49. MS_LOG(EXCEPTION) << "SyncHostToDevice failed.";
  50. }
  51. }
  52. }
  53. }
  54. GraphId AscendInferenceSession::CompileGraphImpl(NotNull<FuncGraphPtr> func_graph) {
  55. auto graph_id = AscendSession::CompileGraphImpl(func_graph);
  56. auto kernel_graph = GetGraph(graph_id);
  57. MS_EXCEPTION_IF_NULL(kernel_graph);
  58. // load weight data to device
  59. auto input_nodes = kernel_graph->inputs();
  60. for (size_t i = 0; i < input_nodes.size(); ++i) {
  61. if (!input_nodes[i]->isa<Parameter>()) {
  62. MS_LOG(ERROR) << "Kernel graph inputs have anfnode which is not Parameter";
  63. continue;
  64. }
  65. auto pk_node = input_nodes[i]->cast<ParameterPtr>();
  66. MS_EXCEPTION_IF_NULL(pk_node);
  67. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  68. MS_EXCEPTION_IF_NULL(device_address);
  69. if (AnfAlgo::IsParameterWeight(pk_node)) {
  70. const auto &param_value = pk_node->default_param();
  71. MS_EXCEPTION_IF_NULL(param_value);
  72. auto tensor = std::dynamic_pointer_cast<tensor::Tensor>(param_value);
  73. MS_EXCEPTION_IF_NULL(tensor);
  74. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  75. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  76. tensor->data_c())) {
  77. MS_LOG(EXCEPTION) << "SyncHostToDevice failed.";
  78. }
  79. }
  80. }
  81. return graph_id;
  82. }
  83. bool AscendInferenceSession::CheckModelInputs(uint32_t graph_id, const std::vector<tensor::TensorPtr> &inputs,
  84. std::string *error_msg) const {
  85. MS_LOG(INFO) << "Start check client inputs, graph id : " << graph_id;
  86. auto kernel_graph = GetGraph(graph_id);
  87. MS_EXCEPTION_IF_NULL(kernel_graph);
  88. auto kernel_graph_inputs = kernel_graph->inputs();
  89. size_t no_weight_input = 0;
  90. vector<ParameterPtr> paras;
  91. // find parameters of graph inputs
  92. for (size_t i = 0; i < kernel_graph_inputs.size(); ++i) {
  93. if (!kernel_graph_inputs[i]->isa<Parameter>()) {
  94. MS_LOG(ERROR) << "Kernel graph inputs have anfnode which is not Parameter.";
  95. continue;
  96. }
  97. auto parameter = kernel_graph_inputs[i]->cast<ParameterPtr>();
  98. if (!AnfAlgo::IsParameterWeight(parameter)) {
  99. paras.push_back(parameter);
  100. }
  101. }
  102. // check inputs
  103. for (size_t i = 0; i < paras.size(); ++i) {
  104. // compare input number
  105. if (paras.size() != inputs.size()) {
  106. MS_LOG(ERROR) << "Input number is inconsistent. The actual input number [" << inputs.size()
  107. << "] but the graph input number is [" << paras.size() << "]";
  108. MS_LOG(ERROR) << "InputsInfo --" << InputsInfo(paras, inputs);
  109. if (error_msg != nullptr) {
  110. std::stringstream str_stream;
  111. str_stream << "Input number is inconsistent. The given input number [" << inputs.size()
  112. << "] but the graph input number is [" << paras.size() << "]\n";
  113. str_stream << "InputsInfo --" << InputsInfo(paras, inputs);
  114. *error_msg = str_stream.str();
  115. }
  116. return false;
  117. }
  118. auto input = inputs[no_weight_input++];
  119. if (!CompareInput(input, paras[i])) {
  120. MS_LOG(ERROR) << "Please check the input information.";
  121. MS_LOG(ERROR) << "InputsInfo --" << InputsInfo(paras, inputs);
  122. if (error_msg != nullptr) {
  123. std::stringstream str_stream;
  124. str_stream << "Please check the input information.\n";
  125. str_stream << "InputsInfo --" << InputsInfo(paras, inputs);
  126. *error_msg = str_stream.str();
  127. }
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. bool AscendInferenceSession::CompareInput(const tensor::TensorPtr &input, const ParameterPtr &parameter) const {
  134. MS_EXCEPTION_IF_NULL(input);
  135. MS_EXCEPTION_IF_NULL(parameter);
  136. // compare dims
  137. auto parameter_shape = AnfAlgo::GetOutputDeviceShape(parameter, 0);
  138. // compare shape
  139. auto input_shape = input->shape();
  140. vector<size_t> trans_input;
  141. (void)std::transform(input_shape.begin(), input_shape.end(), std::back_inserter(trans_input),
  142. [](const int dim) { return static_cast<size_t>(dim); });
  143. auto is_scalar_shape = [](const vector<size_t> &shape) {
  144. return shape.empty() || (shape.size() == 1 && shape[0] == 1);
  145. };
  146. if ((!is_scalar_shape(trans_input) || !is_scalar_shape(parameter_shape)) && (trans_input != parameter_shape)) {
  147. MS_LOG(ERROR) << "Input shape is inconsistent. The actual shape is " << PrintInputShape(trans_input)
  148. << ", but the parameter shape is " << PrintInputShape(parameter_shape)
  149. << ". parameter : " << parameter->DebugString();
  150. return false;
  151. }
  152. // compare data type
  153. auto kernel_build_info = AnfAlgo::GetSelectKernelBuildInfo(parameter);
  154. if (input->data_type() != kernel_build_info->GetOutputDeviceType(0)) {
  155. MS_LOG(ERROR) << "Input data type is inconsistent. The actual data type is " << input->data_type()
  156. << ", but the parameter data type is " << kernel_build_info->GetOutputDeviceType(0)
  157. << ". parameter : " << parameter->DebugString();
  158. return false;
  159. }
  160. return true;
  161. }
  162. template <typename T>
  163. std::string AscendInferenceSession::PrintInputShape(std::vector<T> shape) const {
  164. string res = "[";
  165. for (auto dim : shape) {
  166. res += " " + std::to_string(dim);
  167. }
  168. return res + " ]";
  169. }
  170. std::string AscendInferenceSession::InputsInfo(const std::vector<ParameterPtr> &paras,
  171. const std::vector<tensor::TensorPtr> &inputs) const {
  172. const std::map<TypeId, std::string> dtype_name_map{
  173. {TypeId::kNumberTypeBegin, "Unknown"}, {TypeId::kNumberTypeBool, "Bool"},
  174. {TypeId::kNumberTypeFloat64, "Float64"}, {TypeId::kNumberTypeInt8, "Int8"},
  175. {TypeId::kNumberTypeUInt8, "Uint8"}, {TypeId::kNumberTypeInt16, "Int16"},
  176. {TypeId::kNumberTypeUInt16, "Uint16"}, {TypeId::kNumberTypeInt32, "Int32"},
  177. {TypeId::kNumberTypeUInt32, "Uint32"}, {TypeId::kNumberTypeInt64, "Int64"},
  178. {TypeId::kNumberTypeUInt64, "Uint64"}, {TypeId::kNumberTypeFloat16, "Float16"},
  179. {TypeId::kNumberTypeFloat32, "Float32"},
  180. };
  181. auto data_type_to_string = [&dtype_name_map](TypeId type_id) {
  182. auto it = dtype_name_map.find(type_id);
  183. if (it == dtype_name_map.end()) {
  184. return std::string("Unknown");
  185. }
  186. return it->second;
  187. };
  188. std::string graph = "graph inputs:{ ";
  189. for (size_t i = 0; i < paras.size(); ++i) {
  190. auto &para = paras[i];
  191. graph += std::to_string(i) + ": dims " + std::to_string(AnfAlgo::GetOutputDeviceShape(para, 0).size()) +
  192. ", shape " + PrintInputShape(AnfAlgo::GetOutputDeviceShape(para, 0)) + ", data type " +
  193. data_type_to_string(AnfAlgo::GetSelectKernelBuildInfo(para)->GetOutputDeviceType(0)) + " }";
  194. }
  195. std::string actual = "given inputs:{ ";
  196. for (size_t i = 0; i < inputs.size(); ++i) {
  197. actual += std::to_string(i) + ": dims " + std::to_string(inputs[i]->shape().size()) + ", shape " +
  198. PrintInputShape(inputs[i]->shape()) + ", data type " + data_type_to_string(inputs[i]->data_type()) + " }";
  199. }
  200. return graph + " " + actual;
  201. }
  202. void AscendInferenceSession::GetModelInputsInfo(uint32_t graph_id, std::vector<tensor::TensorPtr> *inputs) const {
  203. MS_LOG(INFO) << "Start get model inputs, graph id : " << graph_id;
  204. auto kernel_graph = GetGraph(graph_id);
  205. MS_EXCEPTION_IF_NULL(kernel_graph);
  206. auto kernel_graph_inputs = kernel_graph->inputs();
  207. vector<ParameterPtr> paras;
  208. // find parameters of graph inputs
  209. for (size_t i = 0; i < kernel_graph_inputs.size(); ++i) {
  210. if (!kernel_graph_inputs[i]->isa<Parameter>()) {
  211. MS_LOG(ERROR) << "Kernel graph inputs have anfnode which is not Parameter.";
  212. continue;
  213. }
  214. auto parameter = kernel_graph_inputs[i]->cast<ParameterPtr>();
  215. if (!AnfAlgo::IsParameterWeight(parameter)) {
  216. vector<int> input_shape;
  217. auto parameter_shape = AnfAlgo::GetOutputDeviceShape(parameter, 0);
  218. (void)std::transform(parameter_shape.begin(), parameter_shape.end(), std::back_inserter(input_shape),
  219. [](const size_t dim) { return static_cast<int>(dim); });
  220. auto kernel_build_info = AnfAlgo::GetSelectKernelBuildInfo(parameter);
  221. auto data_type = kernel_build_info->GetOutputDeviceType(0);
  222. auto ms_tensor = std::make_shared<tensor::Tensor>(data_type, input_shape);
  223. inputs->push_back(ms_tensor);
  224. }
  225. }
  226. }
  227. } // namespace session
  228. } // namespace mindspore