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.

gpu_inference_session.cc 9.3 kB

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