diff --git a/mindspore/lite/schema/model.fbs b/mindspore/lite/schema/model.fbs index 98bc3f5e81..813d51dfdf 100644 --- a/mindspore/lite/schema/model.fbs +++ b/mindspore/lite/schema/model.fbs @@ -232,7 +232,8 @@ union PrimitiveType { AssignAdd, OnesLike, BinaryCrossEntropyGrad, - BinaryCrossEntropy + BinaryCrossEntropy, + LpNormalization, } enum QuantType: int { diff --git a/mindspore/lite/schema/ops.fbs b/mindspore/lite/schema/ops.fbs index 9043c10874..a66cd7be38 100644 --- a/mindspore/lite/schema/ops.fbs +++ b/mindspore/lite/schema/ops.fbs @@ -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; +} diff --git a/mindspore/lite/src/model_common.cc b/mindspore/lite/src/model_common.cc index b734823368..55853c94b5 100644 --- a/mindspore/lite/src/model_common.cc +++ b/mindspore/lite/src/model_common.cc @@ -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(malloc(size)); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/cast.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/cast.cc index e98e95deba..0df744465c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/cast.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/cast.cc @@ -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 diff --git a/mindspore/lite/src/scheduler.cc b/mindspore/lite/src/scheduler.cc index e782ad0223..da83f9556e 100644 --- a/mindspore/lite/src/scheduler.cc +++ b/mindspore/lite/src/scheduler.cc @@ -300,7 +300,7 @@ TypeId Scheduler::GetFirstFp32Fp16OrInt8Type(const std::vector &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; } } diff --git a/mindspore/lite/tools/anf_importer/import_from_meta_graphT.cc b/mindspore/lite/tools/anf_importer/import_from_meta_graphT.cc index 99fef04948..c0576c92d5 100644 --- a/mindspore/lite/tools/anf_importer/import_from_meta_graphT.cc +++ b/mindspore/lite/tools/anf_importer/import_from_meta_graphT.cc @@ -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); diff --git a/mindspore/lite/tools/common/node_util.cc b/mindspore/lite/tools/common/node_util.cc index 2ca56c4d85..5d320e198e 100644 --- a/mindspore/lite/tools/common/node_util.cc +++ b/mindspore/lite/tools/common/node_util.cc @@ -48,7 +48,9 @@ static const std::vector 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 nhwcOpDualInputList = { #ifdef SUPPORT_TRAIN diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc index 746642b194..c8e6293882 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc @@ -24,9 +24,6 @@ namespace lite { constexpr int32_t kSingleGroup = 1; bool OnnxConvParser::ParseGroupConvolution(const std::unique_ptr &attr, schema::CNodeT *op) { MS_LOG(DEBUG) << "onnx DepthwiseConvParser"; - if (attr == nullptr || attr->group != attr->channelIn) { - return false; - } std::unique_ptr depthwiseConv2DParam = std::make_unique(); 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; diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.cc new file mode 100644 index 0000000000..d02c4ec4fb --- /dev/null +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.cc @@ -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 + +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(); + if (op->primitive == nullptr) { + MS_LOG(ERROR) << "op->primitive is null"; + return RET_NULL_PTR; + } + + std::unique_ptr attr = std::make_unique(); + 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 diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.h b/mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.h new file mode 100644 index 0000000000..03cbeb3005 --- /dev/null +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_lp_norm_parser.h @@ -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 diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_lrn_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_lrn_parser.cc index 356df10005..484a12ba1e 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_lrn_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_lrn_parser.cc @@ -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(onnx_node_attr.i()); - attr->depth_radius = static_cast(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(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(); diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc index c9b3eb0076..9629057ab4 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_slice_parser.cc @@ -42,6 +42,7 @@ STATUS OnnxSliceParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::No std::vector axes; std::vector starts; std::vector ends; + std::vector 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(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(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 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; diff --git a/mindspore/lite/tools/optimizer/common/gllo_utils.cc b/mindspore/lite/tools/optimizer/common/gllo_utils.cc index 9f0cad9dfe..9bcedaa571 100644 --- a/mindspore/lite/tools/optimizer/common/gllo_utils.cc +++ b/mindspore/lite/tools/optimizer/common/gllo_utils.cc @@ -501,7 +501,7 @@ std::shared_ptr>> 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;