From 892668389f9c589593387b8706fa4946124c3a96 Mon Sep 17 00:00:00 2001 From: kai00 Date: Sun, 16 Aug 2020 16:31:03 +0800 Subject: [PATCH] anf exporter fixed --- mindspore/lite/test/models_mindspore.cfg | 1 + mindspore/lite/test/run_benchmark_nets.sh | 32 +++++++++++++++++ .../lite/tools/anf_exporter/anf_exporter.cc | 4 +++ .../anf_depthwiseconv2d_populater.cc | 4 +-- .../anf_populater/anf_make_tuple_populater.cc | 35 +++++++++++++++++++ .../anf_populater/anf_make_tuple_populater.h | 29 +++++++++++++++ .../anf_populater/anf_reducemean_populater.cc | 8 ++--- .../anf_populater/anf_transpose_populater.cc | 4 +-- .../anf_tuple_getitem_populater.h | 4 +-- .../anf_importer/import_from_protobuf.cc | 16 +++++++-- .../optimizer/fusion/conv_transform_fusion.cc | 3 ++ 11 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 mindspore/lite/test/models_mindspore.cfg create mode 100644 mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.cc create mode 100644 mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.h diff --git a/mindspore/lite/test/models_mindspore.cfg b/mindspore/lite/test/models_mindspore.cfg new file mode 100644 index 0000000000..802f5f33ee --- /dev/null +++ b/mindspore/lite/test/models_mindspore.cfg @@ -0,0 +1 @@ +ssd.pb diff --git a/mindspore/lite/test/run_benchmark_nets.sh b/mindspore/lite/test/run_benchmark_nets.sh index eddd86e154..7ad20e4b56 100644 --- a/mindspore/lite/test/run_benchmark_nets.sh +++ b/mindspore/lite/test/run_benchmark_nets.sh @@ -65,6 +65,26 @@ function Run_x86() { fi done < ${models_onnx_config} + # Run mindspore converted models: + while read line; do + model_name=${line} + if [[ $model_name == \#* ]]; then + continue + fi + echo ${model_name} + echo 'cd '${convertor_path}'/MSLite-*-linux_x86_64' + cd ${convertor_path}/MSLite-*-linux_x86_64 || return 1 + echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib;./benchmark/benchmark --modelPath='${ms_models_path}'/'${model_name}'.ms --inDataPath=/home/workspace/mindspore_dataset/mslite/models/hiai/input_output/input/'${model_name}'.ms.bin --calibDataPath=/home/workspace/mindspore_dataset/mslite/models/hiai/input_output/output/'${model_name}'.ms.out --warmUpLoopCount=1 --loopCount=1' || return 1 + export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib;./benchmark/benchmark --modelPath=${ms_models_path}/${model_name}.ms --inDataPath=/home/workspace/mindspore_dataset/mslite/models/hiai/input_output/input/${model_name}.ms.bin --calibDataPath=/home/workspace/mindspore_dataset/mslite/models/hiai/input_output/output/${model_name}.ms.out --warmUpLoopCount=1 --loopCount=1 --accuracyThreshold=1.5 + if [ $? = 0 ]; then + run_result='Run_x86: '${model_name}' pass' + echo ${run_result} >> ${run_benchmark_result_file} + else + run_result='Run_x86: '${model_name}' fail <<===========================this is the failed case' + echo ${run_result} >> ${run_benchmark_result_file} + return 1 + fi + done < ${models_mindspore_config} } # Run on arm64 platform: @@ -176,6 +196,7 @@ models_tflite_config=${basepath}/models_tflite.cfg models_caffe_config=${basepath}/models_caffe.cfg models_tflite_posttraining_config=${basepath}/models_tflite_posttraining.cfg models_onnx_config=${basepath}/models_onnx.cfg +models_mindspore_config=${basepath}/models_mindspore.cfg rm -rf ${basepath}/ms_models mkdir -p ${basepath}/ms_models @@ -216,6 +237,17 @@ while read line; do ./converter_lite --fmk=ONNX --modelFile=${models_path}/${model_name} --outputFile=${ms_models_path}/${model_name} || exit 1 done < ${models_onnx_config} +# Convert mindspore models: +while read line; do + model_name=${line} + if [[ $model_name == \#* ]]; then + continue + fi + echo ${model_name} + pwd + echo './converter_lite --fmk=MS --modelFile='${models_path}'/'${model_name}' --outputFile='${ms_models_path}'/'${model_name}'' + ./converter_lite --fmk=MS --modelFile=${models_path}/${model_name} --outputFile=${ms_models_path}/${model_name} || exit 1 +done < ${models_mindspore_config} # Convert TFLite PostTraining models: while read line; do diff --git a/mindspore/lite/tools/anf_exporter/anf_exporter.cc b/mindspore/lite/tools/anf_exporter/anf_exporter.cc index ee70a8c2ff..0b89bd05e2 100644 --- a/mindspore/lite/tools/anf_exporter/anf_exporter.cc +++ b/mindspore/lite/tools/anf_exporter/anf_exporter.cc @@ -396,6 +396,10 @@ void AnfExporter::SetOpOutputNode(const CNodePtr &cnode, const std::unique_ptrallTensors.size(); } meta_graphT->allTensors.emplace_back(msTensor); + if (IsPrimitiveCNode(cnode, schema::PrimitiveType_Conv2D) + || IsPrimitiveCNode(cnode, schema::PrimitiveType_DepthwiseConv2D)) { + break; + } } } else { auto ms_tensor = new schema::TensorT(); diff --git a/mindspore/lite/tools/anf_importer/anf_populater/anf_depthwiseconv2d_populater.cc b/mindspore/lite/tools/anf_importer/anf_populater/anf_depthwiseconv2d_populater.cc index 6cf45542e9..874a52df5c 100644 --- a/mindspore/lite/tools/anf_importer/anf_populater/anf_depthwiseconv2d_populater.cc +++ b/mindspore/lite/tools/anf_importer/anf_populater/anf_depthwiseconv2d_populater.cc @@ -155,8 +155,8 @@ int AnfDepwiseconv2DPopulater::Populate(const PrimitivePtr &prim, PrimitiveTValu auto channel_multiplier = GetValue(prim->GetAttr("channel_multiplier")); attr->channelMultiplier = channel_multiplier; - MS_ASSERT(inputs.size() == kAnfPopulaterThree); - auto inputNode = inputs[kAnfPopulaterTwo]; + MS_ASSERT(inputs.size() == kAnfPopulaterTwo); + auto inputNode = inputs[kAnfPopulaterOne]; MS_ASSERT(inputNode != nullptr); if (inputNode->isa()) { auto paramNode = inputNode->cast(); diff --git a/mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.cc b/mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.cc new file mode 100644 index 0000000000..5fae271e4a --- /dev/null +++ b/mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.cc @@ -0,0 +1,35 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "tools/anf_importer/anf_populater/anf_node_populater_registry.h" +#include "tools/anf_importer/anf_populater/anf_make_tuple_populater.h" +#include "ir/func_graph.h" +#include "ir/primitive.h" + +namespace mindspore::lite { +int AnfMakeTuplePopulater::Populate(const PrimitivePtr &prim, PrimitiveTValue *primitiveTValuePtr, + const std::vector &inputs) { + auto primitive = std::make_unique(); + auto attr = std::make_unique(); + primitive->value.type = schema::PrimitiveType_MakeTuple; + primitive->value.value = attr.release(); + MS_ASSERT(primitiveTValuePtr != nullptr); + primitiveTValuePtr->SetPrimitiveT(primitive.release()); + return 0; +} +AnfNodePopulaterRegistrar anfMakeTuplePopulater("make_tuple", new AnfMakeTuplePopulater()); +} // namespace mindspore::lite diff --git a/mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.h b/mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.h new file mode 100644 index 0000000000..973ae8fc15 --- /dev/null +++ b/mindspore/lite/tools/anf_importer/anf_populater/anf_make_tuple_populater.h @@ -0,0 +1,29 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MINDSPORE_ANF_MAKE_TUPLE_PARSER_H +#define MINDSPORE_ANF_MAKE_TUPLE_PARSER_H +#include "tools/anf_importer/anf_populater/anf_node_populater.h" +#include +namespace mindspore::lite { +class AnfMakeTuplePopulater : public AnfNodePopulater { + public: + AnfMakeTuplePopulater() = default; + ~AnfMakeTuplePopulater() override = default; + int Populate(const PrimitivePtr &prim, PrimitiveTValue *primitiveTValuePtr, + const std::vector &inputs) override; +}; +} // namespace mindspore::lite +#endif // MINDSPORE_ANF_MAKE_TUPLE_PARSER_H diff --git a/mindspore/lite/tools/anf_importer/anf_populater/anf_reducemean_populater.cc b/mindspore/lite/tools/anf_importer/anf_populater/anf_reducemean_populater.cc index 5da3735db4..9c24375dd5 100644 --- a/mindspore/lite/tools/anf_importer/anf_populater/anf_reducemean_populater.cc +++ b/mindspore/lite/tools/anf_importer/anf_populater/anf_reducemean_populater.cc @@ -21,10 +21,6 @@ #include "ir/primitive.h" namespace mindspore::lite { -namespace { -constexpr int kReduceInputNum = 3; -constexpr int kReduceInputIndex = 2; -} // namespace int AnfReduceMeanPopulater::Populate(const PrimitivePtr &prim, PrimitiveTValue *primitiveTValuePtr, const std::vector &inputs) { auto primitive = std::make_unique(); @@ -32,8 +28,8 @@ int AnfReduceMeanPopulater::Populate(const PrimitivePtr &prim, PrimitiveTValue * attr->mode = schema::ReduceMode_ReduceMean; attr->keepDims = GetValue(prim->GetAttr("keep_dims")); - if (inputs.size() == kReduceInputNum) { - auto inputNode = inputs[kReduceInputIndex]; + if (inputs.size() == kAnfPopulaterTwo) { + auto inputNode = inputs[kAnfPopulaterOne]; MS_ASSERT(inputNode != nullptr); if (inputNode->isa()) { auto valueNode = inputNode->cast(); diff --git a/mindspore/lite/tools/anf_importer/anf_populater/anf_transpose_populater.cc b/mindspore/lite/tools/anf_importer/anf_populater/anf_transpose_populater.cc index 9df1f97b33..9eb790f4d6 100644 --- a/mindspore/lite/tools/anf_importer/anf_populater/anf_transpose_populater.cc +++ b/mindspore/lite/tools/anf_importer/anf_populater/anf_transpose_populater.cc @@ -26,8 +26,8 @@ int AnfTransposePopulater::Populate(const PrimitivePtr &prim, PrimitiveTValue *p const std::vector &inputs) { auto primitive = std::make_unique(); auto attr = std::make_unique(); - MS_ASSERT(inputs.size() == kAnfPopulaterThree); - auto inputNode = inputs[kAnfPopulaterTwo]; + MS_ASSERT(inputs.size() == kAnfPopulaterTwo); + auto inputNode = inputs[kAnfPopulaterOne]; if (inputNode->isa()) { auto valNode = inputNode->cast(); MS_ASSERT(valNode != nullptr); diff --git a/mindspore/lite/tools/anf_importer/anf_populater/anf_tuple_getitem_populater.h b/mindspore/lite/tools/anf_importer/anf_populater/anf_tuple_getitem_populater.h index 40f4c0a15d..8e0c835d49 100644 --- a/mindspore/lite/tools/anf_importer/anf_populater/anf_tuple_getitem_populater.h +++ b/mindspore/lite/tools/anf_importer/anf_populater/anf_tuple_getitem_populater.h @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MINDSPORE_ANF_BATCHNORM_PARSER_H -#define MINDSPORE_ANF_BATCHNORM_PARSER_H +#ifndef MINDSPORE_TUPLE_GETITEM_PARSER_H +#define MINDSPORE_TUPLE_GETITEM_PARSER_H #include "tools/anf_importer/anf_populater/anf_node_populater.h" #include namespace mindspore::lite { diff --git a/mindspore/lite/tools/anf_importer/import_from_protobuf.cc b/mindspore/lite/tools/anf_importer/import_from_protobuf.cc index 3f41d3a197..3b5b8c40b0 100644 --- a/mindspore/lite/tools/anf_importer/import_from_protobuf.cc +++ b/mindspore/lite/tools/anf_importer/import_from_protobuf.cc @@ -718,6 +718,8 @@ bool AnfImporterFromProtobuf::BuildParameterForFuncGraph(const ParameterPtr &nod MS_EXCEPTION_IF_NULL(param_value); param_value->set_tensor_addr(tensor_data_buf); param_value->set_tensor_size(tensor_info->Size()); + param_value->set_tensor_type(tensor_info->data_type()); + param_value->set_tensor_shape(tensor_info->shape()); node->set_default_param(param_value); } anfnode_build_map_[value_proto.name()] = node; @@ -1088,7 +1090,12 @@ bool AnfImporterFromProtobuf::BuildReturnForFuncGraph(const FuncGraphPtr &output std::vector inputs; if (importProto.output_size() > 1) { inputs.clear(); - inputs.push_back(NewValueNode(prim::kPrimMakeTuple)); + auto primitiveT = std::make_unique(); + MS_ASSERT(primitiveT != nullptr); + primitiveT->value.type = schema::PrimitiveType_MakeTuple; + std::shared_ptr primitiveTValuePtr = std::make_shared(primitiveT.release()); + MS_ASSERT(primitiveTValuePtr != nullptr); + inputs.push_back(NewValueNode(primitiveTValuePtr)); AbstractBasePtrList elem; for (int out_size = 0; out_size < importProto.output_size(); ++out_size) { const onnx::ValueInfoProto &output_node = importProto.output(out_size); @@ -1099,7 +1106,12 @@ bool AnfImporterFromProtobuf::BuildReturnForFuncGraph(const FuncGraphPtr &output auto maketuple_ptr = outputFuncGraph->NewCNode(inputs); maketuple_ptr->set_abstract(std::make_shared(elem)); inputs.clear(); - inputs.push_back(NewValueNode(prim::kPrimReturn)); + auto primReturn = std::make_unique(); + MS_ASSERT(primReturn != nullptr); + primReturn->value.type = schema::PrimitiveType_Return; + std::shared_ptr primitiveTReturnValuePtr = std::make_shared(primReturn.release()); + MS_ASSERT(primitiveTReturnValuePtr != nullptr); + inputs.push_back(NewValueNode(primitiveTReturnValuePtr)); inputs.push_back(maketuple_ptr); auto return_node = outputFuncGraph->NewCNode(inputs); MS_EXCEPTION_IF_NULL(return_node); diff --git a/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc b/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc index c858214fb6..5f9f512010 100644 --- a/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc +++ b/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc @@ -69,6 +69,8 @@ const AnfNodePtr ConvTransformFusion::Process(const FuncGraphPtr &func_graph, co if (IsMultiOutputTensors(func_graph, conv_node)) { return transform_node; } + + auto abstr = transform_node->abstract(); int kernel_nums = Get_Kenrnel_nums(conv_node); if (kernel_nums <= 0) { MS_LOG(ERROR) << "Unsupported conv node, " << conv_node->DebugString(); @@ -90,6 +92,7 @@ const AnfNodePtr ConvTransformFusion::Process(const FuncGraphPtr &func_graph, co } else { MS_LOG(EXCEPTION) << "Unsupported opType, " << type; } + pre_node->set_abstract(abstr); return pre_node; }