Browse Source

!8019 [lite] add or fix onnx parser, including convertion of which

Merge pull request !8019 from 徐安越/master
tags/v1.1.0
mindspore-ci-bot Gitee 5 years ago
parent
commit
f020543f02
13 changed files with 181 additions and 44 deletions
  1. +2
    -1
      mindspore/lite/schema/model.fbs
  2. +6
    -0
      mindspore/lite/schema/ops.fbs
  3. +1
    -0
      mindspore/lite/src/model_common.cc
  4. +1
    -0
      mindspore/lite/src/runtime/kernel/arm/fp32/cast.cc
  5. +2
    -2
      mindspore/lite/src/scheduler.cc
  6. +3
    -3
      mindspore/lite/tools/anf_importer/import_from_meta_graphT.cc
  7. +3
    -1
      mindspore/lite/tools/common/node_util.cc
  8. +1
    -4
      mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc
  9. +57
    -0
      mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.cc
  10. +34
    -0
      mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.h
  11. +13
    -32
      mindspore/lite/tools/converter/parser/onnx/onnx_lrn_parser.cc
  12. +57
    -0
      mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc
  13. +1
    -1
      mindspore/lite/tools/optimizer/common/gllo_utils.cc

+ 2
- 1
mindspore/lite/schema/model.fbs View File

@@ -232,7 +232,8 @@ union PrimitiveType {
AssignAdd,
OnesLike,
BinaryCrossEntropyGrad,
BinaryCrossEntropy
BinaryCrossEntropy,
LpNormalization,
}

enum QuantType: int {


+ 6
- 0
mindspore/lite/schema/ops.fbs View File

@@ -553,6 +553,7 @@ table Slice {
axes: [int];
begin: [int];
size: [int];
step: [int];
}

table Floor {
@@ -1128,3 +1129,8 @@ table BinaryCrossEntropy {
table BinaryCrossEntropyGrad {
reduction : int = 1;
}

table LpNormalization {
axis : int;
p : int;
}

+ 1
- 0
mindspore/lite/src/model_common.cc View File

@@ -92,6 +92,7 @@ Model *ImportFromBuffer(const char *model_buf, size_t size, bool take_buf) {
} else {
if (size == 0) {
MS_LOG(ERROR) << "malloc size is equal to 0";
delete (model);
return nullptr;
}
model->buf = reinterpret_cast<char *>(malloc(size));


+ 1
- 0
mindspore/lite/src/runtime/kernel/arm/fp32/cast.cc View File

@@ -165,6 +165,7 @@ REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_Cast, CpuCastFp32KernelCreato
REG_KERNEL(kCPU, kNumberTypeUInt8, PrimitiveType_Cast, CpuCastFp32KernelCreator)
REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_Cast, CpuCastFp32KernelCreator)
REG_KERNEL(kCPU, kNumberTypeInt32, PrimitiveType_Cast, CpuCastFp32KernelCreator)
REG_KERNEL(kCPU, kNumberTypeBool, PrimitiveType_Cast, CpuCastFp32KernelCreator)
#ifndef ENABLE_ARM
REG_KERNEL(kCPU, kNumberTypeFloat16, PrimitiveType_Cast, CpuCastFp32KernelCreator)
#endif


+ 2
- 2
mindspore/lite/src/scheduler.cc View File

@@ -300,7 +300,7 @@ TypeId Scheduler::GetFirstFp32Fp16OrInt8Type(const std::vector<Tensor *> &in_ten
for (const auto &tensor : in_tensors) {
auto dtype = tensor->data_type();
if (dtype == kNumberTypeFloat32 || dtype == kNumberTypeFloat16 || dtype == kNumberTypeInt8 ||
dtype == kNumberTypeInt32) {
dtype == kNumberTypeInt32 || dtype == kNumberTypeBool) {
return dtype;
}
}
@@ -346,7 +346,7 @@ kernel::SubGraphType Scheduler::GetKernelSubGraphType(kernel::LiteKernel *kernel
if (desc.data_type == kNumberTypeFloat16) {
return kernel::kCpuFP16SubGraph;
} else if (desc.data_type == kNumberTypeFloat32 || desc.data_type == kNumberTypeInt8 ||
desc.data_type == kNumberTypeInt32) {
desc.data_type == kNumberTypeInt32 || desc.data_type == kNumberTypeBool) {
return kernel::kCpuFP32SubGraph;
}
}


+ 3
- 3
mindspore/lite/tools/anf_importer/import_from_meta_graphT.cc View File

@@ -170,7 +170,7 @@ int AnfImporterFromMetaGraphT::ConverterCNode() {
auto node = GetNode(j);
if (nullptr == node) {
MS_LOG(ERROR) << "Can't find input node.";
return RET_NOT_FIND_OP;
return RET_ERROR;
}
op_inputs.push_back(node);
}
@@ -203,7 +203,7 @@ int AnfImporterFromMetaGraphT::AddReturnCNode() {
auto cNode = GetNode(tensor_id);
if (nullptr == cNode) {
MS_LOG(ERROR) << "Can't find input node.";
return RET_NOT_FIND_OP;
return RET_ERROR;
}
make_tuple_inputs.emplace_back(cNode);
}
@@ -233,7 +233,7 @@ int AnfImporterFromMetaGraphT::AddReturnCNode() {
auto cnode = GetNode(meta_graph_->outputIndex.front());
if (nullptr == cnode) {
MS_LOG(ERROR) << "Can't find input node.";
return RET_NOT_FIND_OP;
return RET_ERROR;
}
op_inputs.emplace_back(cnode);
auto return_cnode = func_graph_->NewCNode(op_inputs);


+ 3
- 1
mindspore/lite/tools/common/node_util.cc View File

@@ -48,7 +48,9 @@ static const std::vector<schema::PrimitiveType> nhwcOpList = {
schema::PrimitiveType_FusedBatchNorm,
schema::PrimitiveType_PReLU,
schema::PrimitiveType_BiasAdd,
schema::PrimitiveType_InstanceNorm};
schema::PrimitiveType_InstanceNorm,
schema::PrimitiveType_SpaceToDepth,
schema::PrimitiveType_DepthToSpace};

static const std::vector<schema::PrimitiveType> nhwcOpDualInputList = {
#ifdef SUPPORT_TRAIN


+ 1
- 4
mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc View File

@@ -24,9 +24,6 @@ namespace lite {
constexpr int32_t kSingleGroup = 1;
bool OnnxConvParser::ParseGroupConvolution(const std::unique_ptr<schema::Conv2DT> &attr, schema::CNodeT *op) {
MS_LOG(DEBUG) << "onnx DepthwiseConvParser";
if (attr == nullptr || attr->group != attr->channelIn) {
return false;
}
std::unique_ptr<schema::DepthwiseConv2DT> depthwiseConv2DParam = std::make_unique<schema::DepthwiseConv2DT>();
if (depthwiseConv2DParam == nullptr) {
MS_LOG(ERROR) << "new op failed";
@@ -172,7 +169,7 @@ STATUS OnnxConvParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::Nod
attr->activationType = schema::ActivationType_NO_ACTIVATION;
}

if (attr->group > kSingleGroup && attr->group == attr->channelIn) {
if (attr != nullptr && attr->group > kSingleGroup && attr->group == attr->channelIn) {
if (!ParseGroupConvolution(attr, op)) {
MS_LOG(ERROR) << "Convert Convolution to Depthwise failed";
return RET_ERROR;


+ 57
- 0
mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.cc View File

@@ -0,0 +1,57 @@
/**
* 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 "tools/converter/parser/onnx/onnx_lp_norm_parser.h"
#include <memory>

namespace mindspore {
namespace lite {
STATUS OnnxLpNormParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::NodeProto &onnx_node,
schema::CNodeT *op) {
MS_LOG(DEBUG) << "onnx LpNormParser";
if (op == nullptr) {
MS_LOG(ERROR) << "op is null";
return RET_NULL_PTR;
}
op->primitive = std::make_unique<schema::PrimitiveT>();
if (op->primitive == nullptr) {
MS_LOG(ERROR) << "op->primitive is null";
return RET_NULL_PTR;
}

std::unique_ptr<schema::LpNormalizationT> attr = std::make_unique<schema::LpNormalizationT>();
if (attr == nullptr) {
MS_LOG(ERROR) << "new op failed";
return RET_NULL_PTR;
}

auto onnx_node_attr = onnx_node.attribute();
for (int i = 0; i < onnx_node_attr.size(); ++i) {
if (onnx_node_attr.at(i).name() == "axis") {
attr->axis = onnx_node_attr.at(i).i();
} else if (onnx_node_attr.at(i).name() == "p") {
attr->p = onnx_node_attr.at(i).i();
}
}

op->primitive->value.type = schema::PrimitiveType_LpNormalization;
op->primitive->value.value = attr.release();
return RET_OK;
}

OnnxNodeRegistrar g_onnxLpNormParser("LpNormalization", new OnnxLpNormParser());
} // namespace lite
} // namespace mindspore

+ 34
- 0
mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.h View File

@@ -0,0 +1,34 @@
/**
* 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_LITE_TOOLS_CONVERTER_PARSER_ONNX_LP_NORM_PARSER_H
#define MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_ONNX_LP_NORM_PARSER_H

#include "tools/converter/parser/onnx/onnx_node_parser.h"
#include "tools/converter/parser/onnx/onnx_node_parser_registry.h"

namespace mindspore {
namespace lite {
class OnnxLpNormParser : public OnnxNodeParser {
public:
OnnxLpNormParser() : OnnxNodeParser("LpNorm") {}
~OnnxLpNormParser() = default;

STATUS Parse(const onnx::GraphProto &onnx_graph, const onnx::NodeProto &onnx_node, schema::CNodeT *op) override;
};
} // namespace lite
} // namespace mindspore
#endif // MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_ONNX_LP_NORM_PARSER_H

+ 13
- 32
mindspore/lite/tools/converter/parser/onnx/onnx_lrn_parser.cc View File

@@ -37,40 +37,21 @@ STATUS OnnxLrnParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::Node
return RET_NULL_PTR;
}

auto onnx_node_attr = onnx_node.attribute().at(0);
auto onnx_node_attr = onnx_node.attribute();
int32_t size = 0;
if (onnx_node_attr.name() == "size") {
size = static_cast<int32_t>(onnx_node_attr.i());
attr->depth_radius = static_cast<int32_t>(size / 2);
} else {
MS_LOG(ERROR) << "the first attr is not size";
return RET_ERROR;
}

onnx_node_attr = onnx_node.attribute().at(1);
if (onnx_node_attr.name() == "alpha") {
auto alpha = onnx_node_attr.f();
attr->alpha = alpha / size;
} else {
MS_LOG(ERROR) << "the second attr is not alpha";
return RET_ERROR;
}

onnx_node_attr = onnx_node.attribute().at(2);
if (onnx_node_attr.name() == "beta") {
attr->beta = onnx_node_attr.f();
} else {
MS_LOG(ERROR) << "the third attr is not beta";
return RET_ERROR;
}

onnx_node_attr = onnx_node.attribute().at(3);
if (onnx_node_attr.name() == "bias") {
attr->bias = onnx_node_attr.f();
} else {
MS_LOG(ERROR) << "the third attr is not bias";
return RET_ERROR;
for (int i = 0; i < onnx_node_attr.size(); ++i) {
if (onnx_node_attr.at(i).name() == "alpha") {
attr->alpha = onnx_node_attr.at(i).f();
} else if (onnx_node_attr.at(i).name() == "beta") {
attr->beta = onnx_node_attr.at(i).f();
} else if (onnx_node_attr.at(i).name() == "bias") {
attr->bias = onnx_node_attr.at(i).f();
} else if (onnx_node_attr.at(i).name() == "size") {
size = static_cast<int32_t>(onnx_node_attr.at(i).i());
attr->depth_radius = size / 2;
}
}
attr->alpha /= size;

op->primitive->value.type = schema::PrimitiveType_LocalResponseNormalization;
op->primitive->value.value = attr.release();


+ 57
- 0
mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc View File

@@ -42,6 +42,7 @@ STATUS OnnxSliceParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::No
std::vector<int> axes;
std::vector<int> starts;
std::vector<int> ends;
std::vector<int> steps;
for (const auto &onnx_node_attr : onnx_node.attribute()) {
const auto &attribute_name = onnx_node_attr.name();
if (attribute_name == "starts") {
@@ -62,8 +63,63 @@ STATUS OnnxSliceParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::No
for (int i = 0; i < num; ++i) {
ends.push_back(static_cast<int>(onnx_node_attr.ints()[i]));
}
} else if (attribute_name == "steps") {
const int num = onnx_node_attr.ints_size();
steps.clear();
for (int i = 0; i < num; ++i) {
steps.push_back(static_cast<int>(onnx_node_attr.ints()[i]));
}
}
}

if (onnx_node.input_size() > 1) {
const auto &starts_name = onnx_node.input(1);
for (const auto &it : onnx_graph.initializer()) {
if (it.name() == starts_name) {
starts.clear();
for (int i = 0; i < it.int32_data_size(); ++i) {
starts.push_back(it.int32_data(i));
}
}
}
}

if (onnx_node.input_size() > 2) {
const auto &ends_name = onnx_node.input(2);
for (const auto &it : onnx_graph.initializer()) {
if (it.name() == ends_name) {
ends.clear();
for (int i = 0; i < it.int32_data_size(); ++i) {
ends.push_back(it.int32_data(i));
}
}
}
}

if (onnx_node.input_size() > 3) {
const auto &axes_name = onnx_node.input(3);
for (const auto &it : onnx_graph.initializer()) {
if (it.name() == axes_name) {
axes.clear();
for (int i = 0; i < it.int32_data_size(); ++i) {
axes.push_back(it.int32_data(i));
}
}
}
}

if (onnx_node.input_size() > 4) {
const auto &steps_name = onnx_node.input(4);
for (const auto &it : onnx_graph.initializer()) {
if (it.name() == steps_name) {
steps.clear();
for (int i = 0; i < it.int32_data_size(); ++i) {
steps.push_back(it.int32_data(i));
}
}
}
}

std::vector<int> sizes(starts.size(), -1);
for (size_t i = 0; i < starts.size(); ++i) {
sizes[i] = (ends[i] < 0 ? ends[i] : ends[i] - starts[i]);
@@ -71,6 +127,7 @@ STATUS OnnxSliceParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::No
attr->axes = axes;
attr->begin = starts;
attr->size = sizes;
attr->step = steps;
op->primitive->value.type = schema::PrimitiveType_Slice;
op->primitive->value.value = attr.release();
return RET_OK;


+ 1
- 1
mindspore/lite/tools/optimizer/common/gllo_utils.cc View File

@@ -501,7 +501,7 @@ std::shared_ptr<std::vector<std::pair<AnfNodePtr, int>>> GetRealNodeUsedList(con
auto iter = manager->node_users().find(node);
if (iter == manager->node_users().end()) {
MS_LOG(ERROR) << "node has no output in manager";
lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(lite::RET_NOT_FIND_OP);
lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(lite::RET_ERROR);
return nullptr;
}
auto output_info_list = iter->second;


Loading…
Cancel
Save