diff --git a/mindspore/core/ops/op_utils.h b/mindspore/core/ops/op_utils.h index f479c684e4..8b58579204 100644 --- a/mindspore/core/ops/op_utils.h +++ b/mindspore/core/ops/op_utils.h @@ -215,6 +215,7 @@ constexpr auto kNearestMode = "nearest_mode"; constexpr auto kReduceToEnd = "reduce_to_end"; constexpr auto kCoeff = "coeff"; constexpr auto kIsDepthWise = "is_depth_wise"; +constexpr auto kIsDepthWiseNative = "is_depth_wise_native"; const std::set common_valid_types = { kNumberTypeInt8, kNumberTypeInt16, kNumberTypeInt32, kNumberTypeInt64, kNumberTypeUInt8, kNumberTypeUInt16, diff --git a/mindspore/lite/tools/converter/converter.h b/mindspore/lite/tools/converter/converter.h index dea11d9cf1..9015b5ee96 100644 --- a/mindspore/lite/tools/converter/converter.h +++ b/mindspore/lite/tools/converter/converter.h @@ -55,7 +55,10 @@ class MindsporeImporter : public Converter { FuncGraphPtr BuildFuncGraph(const std::string &model_file, const std::string &weight_file, schema::QuantType quant_type) override { - return LoadMindIR(model_file); + auto func_graph = LoadMindIR(model_file); + func_graph->set_attr("graph_name", MakeValue("main_graph")); + func_graph->set_attr("fmk", MakeValue(static_cast(converter::FmkType_MS))); + return func_graph; } }; diff --git a/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc b/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc index c7d7453b60..833e794451 100644 --- a/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc +++ b/mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc @@ -42,6 +42,9 @@ int Get_Kenrnel_nums(const CNodePtr &conv_node) { MS_ASSERT(utils::isa>(primitive)); auto primc = utils::cast>(primitive); MS_ASSERT(primc != nullptr); + if (primc->GetAttr(ops::kOutChannel) == nullptr) { + return 0; + } return primc->get_out_channel(); } else { MS_LOG(ERROR) << "Unsupported opType, " << primitive->name(); diff --git a/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.cc b/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.cc index ce8829466d..ff4651d113 100644 --- a/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.cc +++ b/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.cc @@ -63,6 +63,7 @@ #include "ops/relu6.h" #include "ops/resize.h" #include "ops/resize_bilinear.h" +#include "ops/resize_nearest_neighbor.h" #include "ops/sigmoid.h" #include "ops/tanh.h" @@ -100,6 +101,7 @@ using mindspore::ops::kNameReduceSumSquare; using mindspore::ops::kNameReLU; using mindspore::ops::kNameReLU6; using mindspore::ops::kNameResizeBilinear; +using mindspore::ops::kNameResizeNearestNeighbor; using mindspore::ops::kNameScale; using mindspore::ops::kNameSigmoid; using mindspore::ops::kNameSub; @@ -114,7 +116,6 @@ constexpr auto kNameArgMaxWithValue = "ArgMaxWithValue"; constexpr auto kNameArgMinWithValue = "ArgMinWithValue"; constexpr auto kNameBatchMatMul = "BatchMatMul"; constexpr auto kNameGatherV2 = "GatherV2"; -constexpr auto kNameResizeNearestNeighbor = "ResizeNearestNeighbor"; constexpr auto kNameTensorAdd = "TensorAdd"; std::map activation_map = { {ops::kNameAbs, mindspore::ABS}, {ops::kNameElu, mindspore::ELU}, @@ -160,8 +161,44 @@ int AttrAdjust(const PrimitivePtr &prim, const std::string &name, const std::vec return lite::RET_OK; } +int64_t ComputeGroupForDepthWiseConv2D(const AnfNodePtr &anf_node) { + auto weight_node = anf_node->cast(); + if (weight_node == nullptr) { + MS_LOG(INFO) << "weight node is not parameter node."; + return 1; + } + if (!weight_node->has_default() || weight_node->default_param() == nullptr) { + MS_LOG(ERROR) << "weight not is not a const tensor."; + return lite::RET_ERROR; + } + auto weight = weight_node->default_param()->cast(); + MS_ASSERT(weight != nullptr); + auto shape = weight->shape_c(); + MS_ASSERT(shape.size() == 4); + return shape[1]; +} + +int SetAttrForDepthWiseConv2D(const CNodePtr &cnode, const PrimitivePtr &prim, int64_t *group) { + MS_ASSERT(cnode != nullptr && prim != nullptr); + if (*group == 1) { + *group = ComputeGroupForDepthWiseConv2D(cnode->input(2)); + } + if (*group < 0) { + MS_LOG(ERROR) << "fail to compute group."; + return lite::RET_ERROR; + } + if (prim->GetAttr(ops::kOutChannel) == nullptr) { + prim->AddAttr(ops::kOutChannel, MakeValue(*group)); + } + prim->AddAttr(ops::kIsDepthWise, MakeValue(true)); + prim->AddAttr(ops::kIsDepthWiseNative, MakeValue(true)); + return lite::RET_OK; +} + template -int MoveAttrMapCommon(const ValueNodePtr &value_node) { +int MoveAttrMapCommon(const CNodePtr &cnode) { + MS_ASSERT(cnode != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -175,7 +212,9 @@ int MoveAttrMapCommon(const ValueNodePtr &value_node) { return lite::RET_OK; } -int MoveAttrMapActivation(const ValueNodePtr &value_node) { +int MoveAttrMapActivation(const CNodePtr &cnode) { + MS_ASSERT(value_node != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -195,7 +234,9 @@ int MoveAttrMapActivation(const ValueNodePtr &value_node) { return lite::RET_OK; } -int MoveAttrMapReduce(const ValueNodePtr &value_node) { +int MoveAttrMapReduce(const CNodePtr &cnode) { + MS_ASSERT(value_node != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -216,7 +257,9 @@ int MoveAttrMapReduce(const ValueNodePtr &value_node) { return lite::RET_OK; } -int MoveAttrMapConv2D(const ValueNodePtr &value_node) { +int MoveAttrMapConv2D(const CNodePtr &cnode) { + MS_ASSERT(value_node != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -248,12 +291,21 @@ int MoveAttrMapConv2D(const ValueNodePtr &value_node) { if (group > 1) { dst_prim->AddAttr(ops::kIsDepthWise, MakeValue(true)); } + if (src_prim->name() == kNameDepthWiseConv2D) { + status = SetAttrForDepthWiseConv2D(cnode, dst_prim, &group); + if (status != lite::RET_OK) { + MS_LOG(ERROR) << "set attr for depthwiseconv2D native failed."; + return lite::RET_ERROR; + } + } dst_prim->set_group(group); value_node->set_value(dst_prim); return lite::RET_OK; } -int MoveAttrPool(const ValueNodePtr &value_node) { +int MoveAttrPool(const CNodePtr &cnode) { + MS_ASSERT(value_node != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -288,7 +340,9 @@ int MoveAttrPool(const ValueNodePtr &value_node) { return lite::RET_OK; } -int MoveAttrMapAdder(const ValueNodePtr &value_node) { +int MoveAttrMapAdder(const CNodePtr &cnode) { + MS_ASSERT(value_node != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -317,7 +371,9 @@ int MoveAttrMapAdder(const ValueNodePtr &value_node) { return lite::RET_OK; } -int MoveAttrMapLayerNorm(const ValueNodePtr &value_node) { +int MoveAttrMapLayerNorm(const CNodePtr &cnode) { + MS_ASSERT(value_node != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -335,7 +391,9 @@ int MoveAttrMapLayerNorm(const ValueNodePtr &value_node) { return lite::RET_OK; } -int MoveAttrMapResize(const ValueNodePtr &value_node) { +int MoveAttrMapResize(const CNodePtr &cnode) { + MS_ASSERT(value_node != nullptr); + auto value_node = cnode->input(0)->cast(); MS_ASSERT(value_node != nullptr); auto src_prim = GetValueNode(value_node); if (src_prim == nullptr) { @@ -351,7 +409,7 @@ int MoveAttrMapResize(const ValueNodePtr &value_node) { } if (src_prim->name() == kNameResizeBilinear) { dst_prim->set_method(ResizeMethod::LINEAR); - } else if (src_prim->name() == "ResizeNearestNeighbor") { + } else if (src_prim->name() == kNameResizeNearestNeighbor) { dst_prim->set_method(ResizeMethod::NEAREST); } value_node->set_value(dst_prim); @@ -386,7 +444,7 @@ bool PrimitiveAdjustPass::Run(const FuncGraphPtr &func_graph) { MS_LOG(DEBUG) << "dont't need to adjust."; continue; } - status = adjust_func(value_node); + status = adjust_func(cnode); if (status != lite::RET_OK) { MS_LOG(ERROR) << "convert primitive failed."; return false; @@ -441,6 +499,5 @@ REGIST_PRIMITIVE_ADJUST(kNameTanh, MoveAttrMapActivation) REGIST_PRIMITIVE_ADJUST(kNameTensorAdd, MoveAttrMapCommon) REGIST_PRIMITIVE_ADJUST(kNameTile, MoveAttrMapCommon) REGIST_PRIMITIVE_ADJUST(kNameTopK, MoveAttrMapCommon) - } // namespace opt } // namespace mindspore diff --git a/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.h b/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.h index 14abae0c66..729757d12f 100644 --- a/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.h +++ b/mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.h @@ -27,7 +27,7 @@ using mindspore::lite::converter::FmkType; namespace mindspore { namespace opt { -typedef int (*PrimitiveAdjustCreator)(const ValueNodePtr &value_node); +typedef int (*PrimitiveAdjustCreator)(const CNodePtr &value_node); class PrimitiveAdjustRegistry { public: static PrimitiveAdjustRegistry *GetInstance() { @@ -60,7 +60,7 @@ class RegistryPrimitiveAdjust { }; #define REGIST_PRIMITIVE_ADJUST(type, primitive_adjust_func) \ - RegistryPrimitiveAdjust g_##type##_primitive_adjust(type, primitive_adjust_func); // todo + RegistryPrimitiveAdjust g_##type##_primitive_adjust(type, primitive_adjust_func); class PrimitiveAdjustPass : public Pass { public: diff --git a/mindspore/lite/tools/optimizer/graph/weight_format_hardcode_pass.cc b/mindspore/lite/tools/optimizer/graph/weight_format_hardcode_pass.cc index 62faa2505a..891ab6a263 100644 --- a/mindspore/lite/tools/optimizer/graph/weight_format_hardcode_pass.cc +++ b/mindspore/lite/tools/optimizer/graph/weight_format_hardcode_pass.cc @@ -115,12 +115,13 @@ lite::STATUS WeightFormatHardCodePass::HardCodeMS(const CNodePtr &conv_node, MS_LOG(ERROR) << "Invalid anfnode, which don't have primitive."; return lite::RET_ERROR; } - bool is_depth_wise = prim->GetAttr(ops::kIsDepthWise) != nullptr && GetValue(prim->GetAttr(ops::kIsDepthWise)); + bool is_depth_wise_native = + prim->GetAttr(ops::kIsDepthWiseNative) != nullptr && GetValue(prim->GetAttr(ops::kIsDepthWiseNative)); auto weight_node = conv_node->input(kConvWeightIndex); switch (this->quant_type) { case QuantType_AwareTraining: { if (CheckPrimitiveType(conv_node, prim::kPrimConv2DFusion)) { - if (!is_depth_wise) { + if (!is_depth_wise_native) { param_value->set_format(schema::Format::Format_KCHW); } else { param_value->set_format(schema::Format::Format_CKHW); @@ -134,9 +135,13 @@ lite::STATUS WeightFormatHardCodePass::HardCodeMS(const CNodePtr &conv_node, case QuantType_QUANT_NONE: { // sum up from current ms quant models if (CheckPrimitiveType(conv_node, prim::kPrimConv2DFusion)) { - param_value->set_format(schema::Format::Format_KCHW); + if (!is_depth_wise_native) { + param_value->set_format(schema::Format::Format_KCHW); + } else { + param_value->set_format(schema::Format::Format_CKHW); + } } else if (CheckPrimitiveType(conv_node, prim::kPrimConv2dTransposeFusion)) { - if (is_depth_wise) { + if (is_depth_wise_native) { param_value->set_format(schema::Format::Format_CKHW); } else { param_value->set_format(schema::Format::Format_KCHW);