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