!40891 fix dynamic shape bug on ascend

Merge pull request !40891 from zhengyuanhua/br3
This commit is contained in:
i-robot 2022-08-25 12:36:14 +00:00 committed by Gitee
commit 698830f867
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 100 additions and 8 deletions

View File

@ -54,7 +54,31 @@ Status ModelImpl::Build(const std::string &model_path, ModelType model_type,
}
Status ModelImpl::Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims) {
return kSuccess;
MS_EXCEPTION_IF_NULL(session_);
if (inputs.empty()) {
MS_LOG(ERROR) << "Inputs is null.";
return kLiteInputParamInvalid;
}
if (dims.empty()) {
MS_LOG(ERROR) << "Dims is null.";
return kLiteInputParamInvalid;
}
if (inputs.size() != dims.size()) {
MS_LOG(ERROR) << "The size of inputs does not match the size of dims.";
return kLiteInputParamInvalid;
}
auto model_inputs = session_->GetInputs();
if (model_inputs.empty()) {
MS_LOG(ERROR) << "The inputs of model is null.";
return kLiteParamInvalid;
}
if (inputs.size() != model_inputs.size()) {
MS_LOG(ERROR) << "The size of inputs is incorrect.";
return kLiteInputParamInvalid;
}
std::vector<mindspore::tensor::TensorPtr> resize_inputs = TensorUtils::MSTensorToTensorPtr(inputs);
return session_->Resize(resize_inputs, dims);
}
std::vector<MSTensor> ModelImpl::GetInputs() {

View File

@ -177,7 +177,11 @@ int DynShapeProcess::GetRealImageSize(std::vector<KernelTensorPtr> *const inputs
return lite::RET_OK;
}
void DynShapeProcess::DestroyDynamicInput() {
void DynShapeProcess::DestroyDynamicInput(std::vector<KernelTensorPtr> *const inputs) {
if (inputs == nullptr) {
MS_LOG(ERROR) << "Inputs ptr is nullptr.";
return;
}
if (batch_size_ptr_ != nullptr && batch_size_ptr_->addr != nullptr) {
free(batch_size_ptr_->addr);
batch_size_ptr_->addr = nullptr;
@ -188,6 +192,9 @@ void DynShapeProcess::DestroyDynamicInput() {
image_size_ptr_->addr = nullptr;
image_size_ptr_->size = 0;
}
if (!inputs->empty()) {
(*inputs).pop_back();
}
}
} // namespace acl
} // namespace mindspore::kernel

View File

@ -31,7 +31,7 @@ class DynShapeProcess {
: acl_options_(options), input_data_idx_(input_data_idx), batch_size_ptr_(nullptr), image_size_ptr_(nullptr) {}
int ProcDynamicInput(std::vector<KernelTensorPtr> *const inputs);
void DestroyDynamicInput();
void DestroyDynamicInput(std::vector<KernelTensorPtr> *const inputs);
private:
int AddBatchSizeInput(std::vector<KernelTensorPtr> *const inputs);

View File

@ -120,16 +120,16 @@ std::vector<Format> ModelProcess::GetInputFormat() {
return std::vector<Format>();
}
std::vector<Format> input_formats;
static const std::map<aclFormat, enum Format> acl_format_map = {{ACL_FORMAT_NCHW, NCHW}, {ACL_FORMAT_NHWC, NHWC}};
static const std::map<aclFormat, enum Format> acl_format_map = {
{ACL_FORMAT_NCHW, NCHW}, {ACL_FORMAT_NHWC, NHWC}, {ACL_FORMAT_ND, NCHW}};
size_t input_size = aclmdlGetNumInputs(model_desc_);
for (size_t i = 0; i < input_size; ++i) {
aclFormat format = aclmdlGetInputFormat(model_desc_, i);
auto iter = acl_format_map.find(format);
if (iter != acl_format_map.end()) {
input_formats.emplace_back(iter->second);
} else {
MS_LOG(WARNING) << "Find input " << i << " format failed, cur format: " << static_cast<int32_t>(format);
}
MS_LOG(DEBUG) << "Format of Input " << i << " is " << static_cast<int32_t>(format);
}
return input_formats;
}

View File

@ -163,6 +163,7 @@ int CustomAscendKernelMod::Resize(const BaseOperatorPtr &base_operator, const st
return lite::RET_ERROR;
}
}
inputs_.assign(inputs.begin(), inputs.end() - 1);
return lite::RET_OK;
}
@ -237,7 +238,7 @@ bool CustomAscendKernelMod::Launch(const std::vector<AddressPtr> &inputs, const
return false;
}
if (IsDynamicInput()) {
dyn_shape_proc_->DestroyDynamicInput();
dyn_shape_proc_->DestroyDynamicInput(&inputs_);
}
UpdateOutputAddr(outputs);
return true;

View File

@ -58,7 +58,7 @@ Status SingleOpInferSession::Init(const std::shared_ptr<Context> context) {
kernel_graph_utils_ = std::make_shared<mindspore::KernelGraphUtils>();
if (AscendInit(context) != kSuccess) {
MS_LOG(ERROR) << "Init ascend failed.";
return kMEInvalidInput;
return kLiteError;
}
return kSuccess;
}
@ -183,10 +183,68 @@ Status SingleOpInferSession::RunGraph(const std::vector<tensor::TensorPtr> &inpu
return kSuccess;
}
Status SingleOpInferSession::ResizeGraphInputs(const std::vector<tensor::TensorPtr> &inputs,
const std::vector<std::vector<int64_t>> &dims) {
if (inputs_.size() != inputs.size()) {
MS_LOG(ERROR) << "Graph inputs tensor size[" << inputs_.size() << " is not equal with user input tensor size["
<< inputs.size() << "]";
return kLiteError;
}
auto graph_inputs = RuntimeUtils::GetGraphDataInputs(kernel_graph_);
if (graph_inputs.size() != inputs.size()) {
MS_LOG(ERROR) << "Graph inputs size[" << graph_inputs.size() << " is not equal with user input size["
<< inputs.size() << "]";
return kLiteError;
}
for (size_t i = 0; i < graph_inputs.size(); ++i) {
auto graph_input = graph_inputs[i];
auto graph_input_addr = AnfAlgo::GetMutableOutputAddr(graph_input, 0);
auto type_id = graph_input_addr->type_id();
size_t type_size = GetTypeByte(TypeIdToType(type_id));
size_t tensor_size = dims[i].empty()
? type_size
: std::accumulate(dims[i].begin(), dims[i].end(), type_size, std::multiplies<size_t>());
// update input size
if (graph_input_addr->ptr_ != nullptr) {
free(graph_input_addr->ptr_);
auto new_addr = malloc(tensor_size);
if (new_addr == nullptr) {
MS_LOG(ERROR) << " malloc memory of input " << i << " failed, memory size " << inputs[i]->Size();
return kLiteError;
}
graph_input_addr->set_ptr(new_addr);
graph_input_addr->SetSize(tensor_size);
}
// update input shape
inputs_[i]->set_shape(dims[i]);
auto abstract = std::make_shared<abstract::AbstractTensor>(TypeIdToType(type_id), dims[i]);
graph_input->set_abstract(abstract);
}
return kSuccess;
} // namespace mindspore
Status SingleOpInferSession::Resize(const std::vector<tensor::TensorPtr> &inputs,
const std::vector<std::vector<int64_t>> &dims) {
if (ResizeGraphInputs(inputs, dims) != kSuccess) {
MS_LOG(EXCEPTION) << "Resize graph input error. ";
}
auto &kernel_nodes = kernel_graph_->execution_order();
for (const auto &kernel_node : kernel_nodes) {
std::string kernel_name = common::AnfAlgo::GetCNodeName(kernel_node);
MS_LOG(INFO) << "SingleOpInferSession::Resize " << kernel_name;
auto kernel_mod = AnfAlgo::GetKernelMod(kernel_node);
if (kernel_mod == nullptr) {
MS_LOG(EXCEPTION) << "Kernel mod is nullptr, kernel name: " << kernel_name;
}
auto args = kernel::AbstractArgsFromCNode(kernel_node);
if (kernel_mod->Resize(args.op, args.inputs, args.outputs) != kSuccess) {
MS_LOG(EXCEPTION) << "Kernel mod resize failed, kernel name: " << kernel_name;
}
}
return kSuccess;
}
std::vector<tensor::TensorPtr> SingleOpInferSession::GetOutputs() { return outputs_; }
std::vector<tensor::TensorPtr> SingleOpInferSession::GetInputs() { return inputs_; }
std::vector<std::string> SingleOpInferSession::GetOutputNames() { return output_names_; }

View File

@ -43,6 +43,8 @@ class SingleOpInferSession : public InferSession {
tensor::TensorPtr GetInputByTensorName(const std::string &name) override;
private:
Status ResizeGraphInputs(const std::vector<tensor::TensorPtr> &inputs, const std::vector<std::vector<int64_t>> &dims);
KernelGraphUtilsPtr kernel_graph_utils_;
KernelGraphPtr kernel_graph_;
std::vector<tensor::TensorPtr> inputs_;