From: @yeyunpeng2020 Reviewed-by: @hangangqiang,@HilbertDavid Signed-off-by: @HilbertDavidpull/11881/MERGE
| @@ -24,6 +24,9 @@ int ScatterNdInferShape(const TensorC *const *inputs, size_t inputs_size, Tensor | |||
| } | |||
| const TensorC *shape = inputs[0]; | |||
| if (shape->data_ == NULL) { | |||
| return NNACL_INFER_INVALID; | |||
| } | |||
| // const TensorC *indices = inputs[1]; | |||
| const TensorC *update = inputs[2]; | |||
| TensorC *output = outputs[0]; | |||
| @@ -727,8 +727,8 @@ table PowerGrad { | |||
| } | |||
| table PowFusion { | |||
| scale: float; | |||
| shift: float; | |||
| scale: float = 1; | |||
| shift: float = 0; | |||
| } | |||
| table PriorBox { | |||
| @@ -726,8 +726,8 @@ OP_ATTR(shift, float) | |||
| OP_SCHEMA_DEF_END(PowerGrad) | |||
| OP_SCHEMA_DEF(PowFusion) | |||
| OP_ATTR(scale, float) | |||
| OP_ATTR(shift, float) | |||
| OP_ATTR_WITH_VALUE(scale, float, 1) | |||
| OP_ATTR_WITH_VALUE(shift, float, 0) | |||
| OP_SCHEMA_DEF_END(PowFusion) | |||
| OP_SCHEMA_DEF(PriorBox) | |||
| @@ -502,6 +502,10 @@ schema::PrimitiveT *ScaleFusionPrimitiveCreator(const AnfNodePtr &node) { | |||
| auto ms_primc = GetValueNode<std::shared_ptr<mindspore::ops::ScaleFusion>>(node); | |||
| return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; | |||
| } | |||
| schema::PrimitiveT *ScatterNdPrimitiveCreator(const AnfNodePtr &node) { | |||
| auto ms_primc = GetValueNode<std::shared_ptr<mindspore::ops::ScatterNd>>(node); | |||
| return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; | |||
| } | |||
| schema::PrimitiveT *ShapePrimitiveCreator(const AnfNodePtr &node) { | |||
| auto ms_primc = GetValueNode<std::shared_ptr<mindspore::ops::Shape>>(node); | |||
| return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; | |||
| @@ -787,6 +791,7 @@ RegistryMSOps g_rsqrtPrimitiveCreatorRegistry("Rsqrt", RsqrtPrimitiveCreator); | |||
| RegistryMSOps g_quantDTypeCastPrimitiveCreatorRegistry("QuantDTypeCast", QuantDTypeCastPrimitiveCreator); | |||
| RegistryMSOps g_scalePrimitiveCreatorRegistry("Scale", ScaleFusionPrimitiveCreator); | |||
| RegistryMSOps g_scaleFusionPrimitiveCreatorRegistry("ScaleFusion", ScaleFusionPrimitiveCreator); | |||
| RegistryMSOps g_scatterNdPrimitiveCreatorRegistry("ScatterNd", ScatterNdPrimitiveCreator); | |||
| RegistryMSOps g_shapePrimitiveCreatorRegistry("Shape", ShapePrimitiveCreator); | |||
| RegistryMSOps g_sigmoidCrossEntropyWithLogitsPrimitiveCreatorRegistry("SigmoidCrossEntropyWithLogits", | |||
| SigmoidCrossEntropyWithLogitsPrimitiveCreator); | |||
| @@ -49,7 +49,10 @@ static const std::vector<schema::PrimitiveType> nhwcOpList = { | |||
| schema::PrimitiveType_InstanceNorm, | |||
| schema::PrimitiveType_SpaceToDepth, | |||
| schema::PrimitiveType_DepthToSpace, | |||
| schema::PrimitiveType_TopKFusion}; | |||
| schema::PrimitiveType_TopKFusion, | |||
| schema::PrimitiveType_BatchToSpace, | |||
| schema::PrimitiveType_SpaceToBatch, | |||
| schema::PrimitiveType_SpaceToBatchND}; | |||
| static const std::vector<schema::PrimitiveType> nhwcOpAllInputList = { | |||
| #ifdef SUPPORT_TRAIN | |||
| @@ -37,13 +37,6 @@ ops::PrimitiveC *OnnxPadParser::Parse(const onnx::GraphProto &onnx_graph, const | |||
| paddings[i][1] = static_cast<int64_t>(onnx_node_attr.ints(i + size / 2)); | |||
| } | |||
| prim->set_paddings(paddings); | |||
| std::vector<std::vector<int32_t>> pads(size / 2, std::vector<int32_t>(2, 0)); | |||
| for (int i = 0; i < size / 2; i++) { | |||
| pads[i][0] = static_cast<int32_t>(onnx_node_attr.ints(i)); | |||
| pads[i][1] = static_cast<int32_t>(onnx_node_attr.ints(i + size / 2)); | |||
| } | |||
| prim->AddAttr("pads", MakeValue(pads)); | |||
| } else if (attribute_name == "mode") { | |||
| const auto &mode = onnx_node_attr.s(); | |||
| if (mode == "constant") { | |||
| @@ -49,13 +49,6 @@ ops::PrimitiveC *TflitePadParser::Parse(const std::unique_ptr<tflite::OperatorT> | |||
| return nullptr; | |||
| } | |||
| prim->set_paddings(paddings); | |||
| std::vector<std::vector<int32_t>> pads; | |||
| if (TransTfliteDataToVec2D(tflite_op->inputs.at(1), tflite_subgraph->tensors, tflite_model->buffers, pads)) { | |||
| MS_LOG(ERROR) << "get Pad -> paddings failed"; | |||
| return nullptr; | |||
| } | |||
| prim->AddAttr("pads", MakeValue(pads)); | |||
| } else if (tflite_op_type == tflite::BuiltinOperator_MIRROR_PAD) { | |||
| const auto &tflite_attr = tflite_op->builtin_options.AsMirrorPadOptions(); | |||
| if (tflite_attr == nullptr) { | |||
| @@ -159,6 +159,37 @@ std::vector<int> CastToInt(const ValuePtr &value) { | |||
| return cur_value; | |||
| } | |||
| std::vector<std::vector<int>> CastToVec2DInt(const ValuePtr &value) { | |||
| if (value == nullptr) { | |||
| MS_LOG(WARNING) << "valueptr is nullptr."; | |||
| return {}; | |||
| } | |||
| std::vector<std::vector<int>> result_value; | |||
| if (utils::isa<ValueSequeuePtr>(value)) { | |||
| if (value->cast<ValueSequeuePtr>() | |||
| ->value() | |||
| .front() | |||
| ->cast<ValueSequeuePtr>() | |||
| ->value() | |||
| .front() | |||
| ->type() | |||
| ->number_type() == kNumberTypeInt64) { | |||
| auto origin_value = GetValue<std::vector<std::vector<int64_t>>>(value); | |||
| for (size_t i = 0; i < origin_value.size(); ++i) { | |||
| std::vector<int> cur_value; | |||
| for (size_t j = 0; j < origin_value.at(i).size(); ++j) { | |||
| cur_value.push_back(static_cast<int>(origin_value[i][j])); | |||
| } | |||
| result_value.push_back(cur_value); | |||
| } | |||
| } else { | |||
| result_value = GetValue<std::vector<std::vector<int>>>(value); | |||
| } | |||
| } | |||
| return result_value; | |||
| } | |||
| bool CheckPrimitiveType(const AnfNodePtr &node, const PrimitivePtr &primitive_type) { | |||
| if (node == nullptr) { | |||
| lite::ReturnCode::GetSingleReturnCode()->UpdateReturnCode(lite::RET_NULL_PTR); | |||
| @@ -37,6 +37,8 @@ namespace mindspore { | |||
| namespace opt { | |||
| std::vector<int> CastToInt(const ValuePtr &value); | |||
| std::vector<std::vector<int>> CastToVec2DInt(const ValuePtr &value); | |||
| bool CheckPrimitiveType(const AnfNodePtr &node, const PrimitivePtr &primitive_type); | |||
| bool IsRealCNodeKernel(const AnfNodePtr &node); | |||
| @@ -44,21 +44,21 @@ STATUS InputAdjustPass::AddAttrToInput(const FuncGraphPtr &func_graph, const CNo | |||
| if (value_ptr != nullptr) { | |||
| switch (flag) { | |||
| case 1: { | |||
| auto value_data = GetValue<int32_t>(value_ptr); | |||
| auto value_data = CastToInt(value_ptr).front(); | |||
| auto param_node = | |||
| BuildIntValueParameterNode(func_graph, value_data, cnode->fullname_with_scope() + "_" + attr_name); | |||
| inputs.push_back(param_node); | |||
| break; | |||
| } | |||
| case 2: { | |||
| auto value_data = GetValue<std::vector<int32_t>>(value_ptr); | |||
| auto value_data = CastToInt(value_ptr); | |||
| auto param_node = | |||
| BuildIntVecParameterNode(func_graph, value_data, cnode->fullname_with_scope() + "_" + attr_name); | |||
| inputs.push_back(param_node); | |||
| break; | |||
| } | |||
| case 3: { | |||
| auto value_data = GetValue<std::vector<std::vector<int32_t>>>(value_ptr); | |||
| auto value_data = CastToVec2DInt(value_ptr); | |||
| auto param_node = | |||
| BuildIntVec2DParameterNode(func_graph, value_data, cnode->fullname_with_scope() + "_" + attr_name); | |||
| inputs.push_back(param_node); | |||
| @@ -123,7 +123,7 @@ bool InputAdjustPass::Run(const FuncGraphPtr &func_graph) { | |||
| status = AddAttrToInput(func_graph, cnode, 2, "axes", 2); | |||
| } else if (CheckPrimitiveType(node, prim::kPrimPadFusion)) { | |||
| MS_LOG(INFO) << "Adjust PadFusion"; | |||
| status = AddAttrToInput(func_graph, cnode, 2, "pads", 3); | |||
| status = AddAttrToInput(func_graph, cnode, 2, "paddings", 3); | |||
| } else if (CheckPrimitiveType(node, prim::kPrimPowFusion)) { | |||
| MS_LOG(INFO) << "Adjust PowFuison"; | |||
| status = AddAttrToInput(func_graph, cnode, 2, "power", 4); | |||
| @@ -41,9 +41,11 @@ | |||
| #include "ops/fusion/max_pool_fusion.h" | |||
| #include "ops/fusion/mul_fusion.h" | |||
| #include "ops/fusion/pad_fusion.h" | |||
| #include "ops/fusion/pow_fusion.h" | |||
| #include "ops/fusion/prelu_fusion.h" | |||
| #include "ops/fusion/reduce_fusion.h" | |||
| #include "ops/fusion/scale_fusion.h" | |||
| #include "ops/fusion/slice_fusion.h" | |||
| #include "ops/fusion/sub_fusion.h" | |||
| #include "ops/fusion/tile_fusion.h" | |||
| #include "ops/fusion/topk_fusion.h" | |||
| @@ -65,6 +67,7 @@ | |||
| #include "ops/resize_bilinear.h" | |||
| #include "ops/resize_nearest_neighbor.h" | |||
| #include "ops/sigmoid.h" | |||
| #include "ops/stack.h" | |||
| #include "ops/tanh.h" | |||
| using mindspore::ops::kNameAbs; | |||
| @@ -89,6 +92,7 @@ using mindspore::ops::kNameLeakyRelu; | |||
| using mindspore::ops::kNameMaxPool; | |||
| using mindspore::ops::kNameMul; | |||
| using mindspore::ops::kNamePad; | |||
| using mindspore::ops::kNamePow; | |||
| using mindspore::ops::kNamePReLU; | |||
| using mindspore::ops::kNameReduceAll; | |||
| using mindspore::ops::kNameReduceASum; | |||
| @@ -115,6 +119,7 @@ namespace { | |||
| constexpr auto kNameArgMaxWithValue = "ArgMaxWithValue"; | |||
| constexpr auto kNameArgMinWithValue = "ArgMinWithValue"; | |||
| constexpr auto kNameBatchMatMul = "BatchMatMul"; | |||
| constexpr auto kNameSlice = "Slice"; | |||
| constexpr auto kNameGatherV2 = "Gather"; | |||
| constexpr auto kNameTensorAdd = "TensorAdd"; | |||
| std::map<std::string, mindspore::ActivationType> activation_map = { | |||
| @@ -418,6 +423,30 @@ int MoveAttrMapResize(const CNodePtr &cnode) { | |||
| } | |||
| } // namespace | |||
| int MoveAttrSlice(const CNodePtr &cnode) { | |||
| MS_ASSERT(value_node != nullptr); | |||
| auto value_node = cnode->input(0)->cast<ValueNodePtr>(); | |||
| MS_ASSERT(value_node != nullptr); | |||
| auto src_prim = GetValueNode<PrimitivePtr>(value_node); | |||
| if (src_prim == nullptr) { | |||
| MS_LOG(ERROR) << "value node is invalid."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| auto dst_prim = std::make_shared<ops::SliceFusion>(); | |||
| MS_ASSERT(dst_prim != nullptr); | |||
| auto begin = GetValueNode<ValuePtr>(cnode->input(2)); | |||
| auto begin_value = GetValue<std::vector<int64_t>>(begin); | |||
| std::vector<int64_t> axes(begin_value.size()); | |||
| for (size_t i = 0; i < begin_value.size(); i++) { | |||
| axes[i] = i; | |||
| } | |||
| dst_prim->set_axes(axes); | |||
| dst_prim->SetAttrs(src_prim->attrs()); | |||
| value_node->set_value(dst_prim); | |||
| return lite::RET_OK; | |||
| } | |||
| bool PrimitiveAdjustPass::Run(const FuncGraphPtr &func_graph) { | |||
| if (this->fmk_type_ != lite::converter::FmkType_MS) { | |||
| MS_LOG(INFO) << "The framework type of model should be mindir."; | |||
| @@ -480,6 +509,7 @@ REGIST_PRIMITIVE_ADJUST(kNameLeakyRelu, MoveAttrMapActivation) | |||
| REGIST_PRIMITIVE_ADJUST(kNameMaxPool, MoveAttrPool) | |||
| REGIST_PRIMITIVE_ADJUST(kNameMul, MoveAttrMapCommon<ops::MulFusion>) | |||
| REGIST_PRIMITIVE_ADJUST(kNamePad, MoveAttrMapCommon<ops::PadFusion>) | |||
| REGIST_PRIMITIVE_ADJUST(kNamePow, MoveAttrMapCommon<ops::PowFusion>) | |||
| REGIST_PRIMITIVE_ADJUST(kNamePReLU, MoveAttrMapCommon<ops::PReLUFusion>) | |||
| REGIST_PRIMITIVE_ADJUST(kNameReduceAll, MoveAttrMapReduce) | |||
| REGIST_PRIMITIVE_ADJUST(kNameReduceASum, MoveAttrMapReduce) | |||
| @@ -495,6 +525,7 @@ REGIST_PRIMITIVE_ADJUST(kNameResizeBilinear, MoveAttrMapResize) | |||
| REGIST_PRIMITIVE_ADJUST(kNameResizeNearestNeighbor, MoveAttrMapResize) | |||
| REGIST_PRIMITIVE_ADJUST(kNameScale, MoveAttrMapCommon<ops::ScaleFusion>) | |||
| REGIST_PRIMITIVE_ADJUST(kNameSigmoid, MoveAttrMapActivation) | |||
| REGIST_PRIMITIVE_ADJUST(kNameSlice, MoveAttrSlice) | |||
| REGIST_PRIMITIVE_ADJUST(kNameSub, MoveAttrMapCommon<ops::SubFusion>) | |||
| REGIST_PRIMITIVE_ADJUST(kNameTanh, MoveAttrMapActivation) | |||
| REGIST_PRIMITIVE_ADJUST(kNameTensorAdd, MoveAttrMapCommon<ops::AddFusion>) | |||