From cf716751acc4b489db6c5b5fdec3cc2871b11640 Mon Sep 17 00:00:00 2001 From: gongdaguo Date: Wed, 18 Nov 2020 20:39:29 +0800 Subject: [PATCH] add adder and range parser, fix clip and range --- mindspore/lite/nnacl/adder.c | 18 +++++ mindspore/lite/nnacl/adder.h | 34 +++++++++ mindspore/lite/nnacl/fp32/activation_fp32.c | 20 +++--- mindspore/lite/schema/model.fbs | 1 + mindspore/lite/schema/ops.fbs | 7 +- mindspore/lite/src/ops/adder.cc | 71 +++++++++++++++++++ mindspore/lite/src/ops/adder.h | 43 +++++++++++ .../lite/src/ops/populate/adder_populate.cc | 38 ++++++++++ mindspore/lite/src/ops/primitive_c.cc | 3 + mindspore/lite/src/ops/range.cc | 11 ++- .../src/runtime/kernel/arm/fp32/range_fp32.cc | 14 +++- .../parser/onnx/onnx_adder_parser.cc | 47 ++++++++++++ .../converter/parser/onnx/onnx_adder_parser.h | 34 +++++++++ .../parser/onnx/onnx_range_parser.cc | 48 +++++++++++++ .../converter/parser/onnx/onnx_range_parser.h | 34 +++++++++ .../graph/clip_convert_activation_pass.cc | 34 +++++---- 16 files changed, 429 insertions(+), 28 deletions(-) create mode 100644 mindspore/lite/nnacl/adder.c create mode 100644 mindspore/lite/nnacl/adder.h create mode 100644 mindspore/lite/src/ops/adder.cc create mode 100644 mindspore/lite/src/ops/adder.h create mode 100644 mindspore/lite/src/ops/populate/adder_populate.cc create mode 100644 mindspore/lite/tools/converter/parser/onnx/onnx_adder_parser.cc create mode 100644 mindspore/lite/tools/converter/parser/onnx/onnx_adder_parser.h create mode 100644 mindspore/lite/tools/converter/parser/onnx/onnx_range_parser.cc create mode 100644 mindspore/lite/tools/converter/parser/onnx/onnx_range_parser.h diff --git a/mindspore/lite/nnacl/adder.c b/mindspore/lite/nnacl/adder.c new file mode 100644 index 0000000000..9d4db613f3 --- /dev/null +++ b/mindspore/lite/nnacl/adder.c @@ -0,0 +1,18 @@ +/** + * 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 "nnacl/adder.h" +#include "nnacl/errorcode.h" diff --git a/mindspore/lite/nnacl/adder.h b/mindspore/lite/nnacl/adder.h new file mode 100644 index 0000000000..1399510888 --- /dev/null +++ b/mindspore/lite/nnacl/adder.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_NNACL_ADDER_H_ +#define MINDSPORE_LITE_NNACL_ADDER_H_ + +#include +#include "nnacl/op_base.h" +#include "nnacl/quantization/fixed_point.h" + +typedef struct AdderParameter { + OpParameter op_parameter_; +} AdderParameter; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ADDER_H_ diff --git a/mindspore/lite/nnacl/fp32/activation_fp32.c b/mindspore/lite/nnacl/fp32/activation_fp32.c index 56214f82ff..0d36821f35 100644 --- a/mindspore/lite/nnacl/fp32/activation_fp32.c +++ b/mindspore/lite/nnacl/fp32/activation_fp32.c @@ -15,6 +15,7 @@ */ #include "nnacl/fp32/activation_fp32.h" +#include #include "nnacl/errorcode.h" int Fp32Relu(const float *src, int length, float *dst) { @@ -150,14 +151,17 @@ int HardTanh(const float *src, int length, float *dst, float min_val, float max_ return NNACL_ERR; } int i = 0; - for (i = 0; i < length; ++i) { - float in = src[i]; - if (in < min_val) { - dst[i] = min_val; - } else if (in > max_val) { - dst[i] = max_val; - } else { - dst[i] = in; + if (min_val == FLT_MIN) { + for (i = 0; i < length; ++i) { + dst[i] = src[i] > max_val ? max_val : src[i]; + } + } else if (max_val == FLT_MAX) { + for (i = 0; i < length; ++i) { + dst[i] = src[i] < min_val ? min_val : src[i]; + } + } else { + for (i = 0; i < length; ++i) { + dst[i] = src[i] < min_val ? min_val : (src[i] > max_val ? max_val : src[i]); } } return NNACL_OK; diff --git a/mindspore/lite/schema/model.fbs b/mindspore/lite/schema/model.fbs index 9a951da9c0..0cbdf18ff4 100644 --- a/mindspore/lite/schema/model.fbs +++ b/mindspore/lite/schema/model.fbs @@ -251,6 +251,7 @@ union PrimitiveType { TensorListReserve, All, Assert, + Adder, } enum QuantType: int { diff --git a/mindspore/lite/schema/ops.fbs b/mindspore/lite/schema/ops.fbs index 93cb226e8d..ed0225b535 100644 --- a/mindspore/lite/schema/ops.fbs +++ b/mindspore/lite/schema/ops.fbs @@ -674,7 +674,7 @@ table Range { dType: int; start: int; limit: int; - delta: int; + delta: int = 1; } table ExpandDims { @@ -1176,4 +1176,7 @@ table All { table Assert { summarize : int; -} \ No newline at end of file +} + +table Adder { +} diff --git a/mindspore/lite/src/ops/adder.cc b/mindspore/lite/src/ops/adder.cc new file mode 100644 index 0000000000..1a4d1c1a2a --- /dev/null +++ b/mindspore/lite/src/ops/adder.cc @@ -0,0 +1,71 @@ +/** + * 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 "src/ops/adder.h" + +#ifndef PRIMITIVE_WRITEABLE +#include "src/ops/ops_register.h" +#endif + +namespace mindspore { +namespace lite { +#ifdef PRIMITIVE_WRITEABLE + +#else +int Adder::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { + MS_ASSERT(nullptr != primitive); + MS_ASSERT(nullptr != fbb); + auto attr = primitive->value_as_Adder(); + if (attr == nullptr) { + MS_LOG(ERROR) << "value_as_Adder return nullptr"; + return RET_ERROR; + } + auto val_offset = schema::CreateAdder(*fbb); + auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Adder, val_offset.o); + fbb->Finish(prim_offset); + return RET_OK; +} + +PrimitiveC *AdderCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } +Registry AdderRegistry(schema::PrimitiveType_Adder, AdderCreator); +#endif + +int Adder::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive_ != nullptr); + MS_ASSERT(inputs_.size() == 2); + auto input0 = inputs_.front(); + MS_ASSERT(input0 != nullptr); + MS_ASSERT(input0->shape().size() == 2); + auto input1 = inputs_.at(1); + MS_ASSERT(input1 != nullptr); + MS_ASSERT(input1->shape().size() == 2); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + output->set_data_type(input0->data_type()); + output->set_format(input0->format()); + if (!infer_flag()) { + return RET_OK; + } + std::vector in_shape; + in_shape.push_back(input0->shape().at(0)); + in_shape.push_back(input1->shape().at(1)); + output->set_shape(in_shape); + + return RET_OK; +} +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/adder.h b/mindspore/lite/src/ops/adder.h new file mode 100644 index 0000000000..486572e5f2 --- /dev/null +++ b/mindspore/lite/src/ops/adder.h @@ -0,0 +1,43 @@ +/** + * 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 LITE_MINDSPORE_LITE_C_OPS_ADDER_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ADDER_H_ + +#include +#include +#include +#include "src/ops/primitive_c.h" + +namespace mindspore { +namespace lite { +class Adder : public PrimitiveC { + public: + Adder() = default; + ~Adder() = default; +#ifdef PRIMITIVE_WRITEABLE + MS_DECLARE_PARENT(Adder, PrimitiveC); + explicit Adder(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} + +#else + int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace lite +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ADDER_H_ diff --git a/mindspore/lite/src/ops/populate/adder_populate.cc b/mindspore/lite/src/ops/populate/adder_populate.cc new file mode 100644 index 0000000000..aceca8ce11 --- /dev/null +++ b/mindspore/lite/src/ops/populate/adder_populate.cc @@ -0,0 +1,38 @@ +/** + * 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 "src/ops/adder.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/adder.h" + +namespace mindspore { +namespace lite { + +OpParameter *PopulateAdderParameter(const mindspore::lite::PrimitiveC *primitive) { + auto *adder_param = reinterpret_cast(malloc(sizeof(AdderParameter))); + if (adder_param == nullptr) { + MS_LOG(ERROR) << "malloc AdderParameter failed."; + return nullptr; + } + memset(adder_param, 0, sizeof(AdderParameter)); + adder_param->op_parameter_.type_ = primitive->Type(); + return reinterpret_cast(adder_param); +} +Registry AdderParameterRegistry(schema::PrimitiveType_Adder, PopulateAdderParameter); + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/primitive_c.cc b/mindspore/lite/src/ops/primitive_c.cc index 29601793c6..68ac35e3ed 100644 --- a/mindspore/lite/src/ops/primitive_c.cc +++ b/mindspore/lite/src/ops/primitive_c.cc @@ -132,6 +132,7 @@ #include "src/ops/hashtable_lookup.h" #include "src/ops/skip_gram.h" #include "src/ops/clip.h" +#include "src/ops/adder.h" #include "src/ops/custom_predict.h" #include "src/ops/custom_normalize.h" #include "src/ops/custom_extract_features.h" @@ -858,6 +859,8 @@ PrimitiveC *PrimitiveC::Create(mindspore::schema::PrimitiveT *primitive) { return new (std::nothrow) SkipGram(primitive); case schema::PrimitiveType_Clip: return new (std::nothrow) Clip(primitive); + case schema::PrimitiveType_Adder: + return new (std::nothrow) Adder(primitive); case schema::PrimitiveType_CustomPredict: return new (std::nothrow) CustomPredict(primitive); case schema::PrimitiveType_CustomNormalize: diff --git a/mindspore/lite/src/ops/range.cc b/mindspore/lite/src/ops/range.cc index 0f7fd8b4ee..d4b55cbc8a 100644 --- a/mindspore/lite/src/ops/range.cc +++ b/mindspore/lite/src/ops/range.cc @@ -64,14 +64,19 @@ int Range::InferShape(std::vector inputs_, std::vector outpu auto output = outputs_.front(); MS_ASSERT(output != nullptr); - output->set_data_type(input->data_type()); + output->set_data_type(mindspore::kNumberTypeFloat32); output->set_format(input->format()); if (!infer_flag()) { return RET_OK; } - int shape_size = std::ceil(static_cast(GetLimit() - GetStart()) / GetDelta()); - std::vector in_shape(1); + int shape_size = 0; + if (inputs_.size() == 3) { + shape_size = -1; + } else { + shape_size = std::ceil(static_cast(GetLimit() - GetStart()) / GetDelta()); + } + std::vector in_shape; in_shape.push_back(shape_size); output->set_shape(in_shape); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/range_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/range_fp32.cc index 27e7d34baf..4a08dd654c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/range_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/range_fp32.cc @@ -35,7 +35,19 @@ int RangeCPUKernel::Run() { size_t start = (reinterpret_cast(op_parameter_))->start_; size_t limit = (reinterpret_cast(op_parameter_))->limit_; size_t delta = (reinterpret_cast(op_parameter_))->delta_; - auto output_ptr = reinterpret_cast(out_tensors_.at(0)->MutableData()); + if (in_tensors_.size() == 3) { + if ((in_tensors_.at(0)->data_type() == mindspore::kNumberTypeInt32) && + (in_tensors_.at(1)->data_type() == mindspore::kNumberTypeInt32) && + (in_tensors_.at(2)->data_type() == mindspore::kNumberTypeInt32)) { + start = *reinterpret_cast(in_tensors_.at(0)->data_c()); + limit = *reinterpret_cast(in_tensors_.at(1)->data_c()); + delta = *reinterpret_cast(in_tensors_.at(2)->data_c()); + } else { + MS_LOG(ERROR) << "Unsupported parameter type : " << in_tensors_.at(0)->data_type() << "."; + return RET_ERROR; + } + } + auto output_ptr = reinterpret_cast(out_tensors_.at(0)->data_c()); Range(output_ptr, start, limit, delta); return RET_OK; } diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_adder_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_adder_parser.cc new file mode 100644 index 0000000000..eac8e078c6 --- /dev/null +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_adder_parser.cc @@ -0,0 +1,47 @@ +/** + * 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_adder_parser.h" +#include + +namespace mindspore { +namespace lite { +STATUS OnnxAdderParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::NodeProto &onnx_node, + schema::CNodeT *op) { + MS_LOG(DEBUG) << "onnx AdderParser"; + 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; + } + + auto attr = std::make_unique(); + if (attr == nullptr) { + MS_LOG(ERROR) << "new op failed"; + return RET_NULL_PTR; + } + op->primitive->value.type = schema::PrimitiveType_Adder; + op->primitive->value.value = attr.release(); + return RET_OK; +} + +OnnxNodeRegistrar g_onnxAdderParser("adder_f", new OnnxAdderParser()); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_adder_parser.h b/mindspore/lite/tools/converter/parser/onnx/onnx_adder_parser.h new file mode 100644 index 0000000000..0c383ee1de --- /dev/null +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_adder_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_ADDER_PARSER_H +#define MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_ONNX_ADDER_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 OnnxAdderParser : public OnnxNodeParser { + public: + OnnxAdderParser() : OnnxNodeParser("Adder") {} + ~OnnxAdderParser() override = 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_ADDER_PARSER_H diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_range_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_range_parser.cc new file mode 100644 index 0000000000..9bc619a4cb --- /dev/null +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_range_parser.cc @@ -0,0 +1,48 @@ +/** + * 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_range_parser.h" +#include + +namespace mindspore { +namespace lite { +STATUS OnnxRangeParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::NodeProto &onnx_node, + schema::CNodeT *op) { + MS_LOG(DEBUG) << "onnx RangeParser"; + 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; + } + attr->dType = 0; + op->primitive->value.type = schema::PrimitiveType_Range; + op->primitive->value.value = attr.release(); + return RET_OK; +} + +OnnxNodeRegistrar g_onnxRangeParser("Range", new OnnxRangeParser()); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_range_parser.h b/mindspore/lite/tools/converter/parser/onnx/onnx_range_parser.h new file mode 100644 index 0000000000..f565f153a8 --- /dev/null +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_range_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_RANGE_PARSER_H +#define MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_ONNX_RANGE_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 OnnxRangeParser : public OnnxNodeParser { + public: + OnnxRangeParser() : OnnxNodeParser("Range") {} + ~OnnxRangeParser() override = 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_RANGE_PARSER_H diff --git a/mindspore/lite/tools/optimizer/graph/clip_convert_activation_pass.cc b/mindspore/lite/tools/optimizer/graph/clip_convert_activation_pass.cc index 8c3a8a5bec..828622a269 100644 --- a/mindspore/lite/tools/optimizer/graph/clip_convert_activation_pass.cc +++ b/mindspore/lite/tools/optimizer/graph/clip_convert_activation_pass.cc @@ -42,9 +42,7 @@ bool ClipConvertActivationPass::Run(const FuncGraphPtr &graph) { continue; } auto clip_cnode = node->cast(); - MS_ASSERT(clip_cnode->inputs().size() > kClipMinIndex); - MS_ASSERT(clip_cnode->inputs().size() > kClipMaxIndex); - + MS_ASSERT(clip_cnode->size() >= kClipMinIndex); auto primitive_c = GetValueNode>(clip_cnode->input(0)); MS_ASSERT(primitive_c != nullptr); auto primT = primitive_c->primitiveT(); @@ -55,19 +53,27 @@ bool ClipConvertActivationPass::Run(const FuncGraphPtr &graph) { float max = primT->value.AsClip()->max; float min = primT->value.AsClip()->min; if ((min == -1) && (max == -1)) { - if (clip_cnode->size() != 4) { - MS_LOG(ERROR) << "Clip param invalid"; - return false; + if (clip_cnode->size() > kClipMinIndex) { + auto min_param_value = GetLiteParamValue(clip_cnode->input(kClipMinIndex)); + if (min_param_value->tensor_type() != mindspore::kNumberTypeFloat32) { + MS_LOG(ERROR) << "Clip param type invalid"; + return false; + } + min = *reinterpret_cast(min_param_value->tensor_addr()); + } else { + min = FLT_MIN; } - auto min_param_value = GetLiteParamValue(clip_cnode->input(kClipMinIndex)); - auto max_param_value = GetLiteParamValue(clip_cnode->input(kClipMaxIndex)); - if ((min_param_value->tensor_type() != mindspore::kNumberTypeFloat32) || - (max_param_value->tensor_type() != mindspore::kNumberTypeFloat32)) { - MS_LOG(ERROR) << "Clip param type invalid"; - return false; + + if (clip_cnode->size() > kClipMaxIndex) { + auto max_param_value = GetLiteParamValue(clip_cnode->input(kClipMaxIndex)); + if (max_param_value->tensor_type() != mindspore::kNumberTypeFloat32) { + MS_LOG(ERROR) << "Clip param type invalid"; + return false; + } + max = *reinterpret_cast(max_param_value->tensor_addr()); + } else { + max = FLT_MAX; } - min = *reinterpret_cast(min_param_value->tensor_addr()); - max = *reinterpret_cast(max_param_value->tensor_addr()); } auto manager = graph->manager();