Browse Source

!11674 [lite]adjust some pass for mindir

From: @xu_anyue
Reviewed-by: @hangangqiang,@zhanghaibo5
Signed-off-by: @hangangqiang
tags/v1.1.1
mindspore-ci-bot Gitee 5 years ago
parent
commit
b09ac3a9be
6 changed files with 88 additions and 19 deletions
  1. +1
    -0
      mindspore/core/ops/op_utils.h
  2. +4
    -1
      mindspore/lite/tools/converter/converter.h
  3. +3
    -0
      mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc
  4. +69
    -12
      mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.cc
  5. +2
    -2
      mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.h
  6. +9
    -4
      mindspore/lite/tools/optimizer/graph/weight_format_hardcode_pass.cc

+ 1
- 0
mindspore/core/ops/op_utils.h View File

@@ -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<TypeId> common_valid_types = {
kNumberTypeInt8, kNumberTypeInt16, kNumberTypeInt32, kNumberTypeInt64, kNumberTypeUInt8, kNumberTypeUInt16,


+ 4
- 1
mindspore/lite/tools/converter/converter.h View File

@@ -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<int>(converter::FmkType_MS)));
return func_graph;
}
};



+ 3
- 0
mindspore/lite/tools/optimizer/fusion/conv_transform_fusion.cc View File

@@ -42,6 +42,9 @@ int Get_Kenrnel_nums(const CNodePtr &conv_node) {
MS_ASSERT(utils::isa<std::shared_ptr<mindspore::ops::Conv2DFusion>>(primitive));
auto primc = utils::cast<std::shared_ptr<mindspore::ops::Conv2DFusion>>(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();


+ 69
- 12
mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.cc View File

@@ -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<std::string, mindspore::ActivationType> 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<ParameterPtr>();
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<tensor::TensorPtr>();
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<bool>(true));
prim->AddAttr(ops::kIsDepthWiseNative, MakeValue<bool>(true));
return lite::RET_OK;
}

template <typename T>
int MoveAttrMapCommon(const ValueNodePtr &value_node) {
int MoveAttrMapCommon(const CNodePtr &cnode) {
MS_ASSERT(cnode != 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) {
@@ -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<ValueNodePtr>();
MS_ASSERT(value_node != nullptr);
auto src_prim = GetValueNode<PrimitivePtr>(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<ValueNodePtr>();
MS_ASSERT(value_node != nullptr);
auto src_prim = GetValueNode<PrimitivePtr>(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<ValueNodePtr>();
MS_ASSERT(value_node != nullptr);
auto src_prim = GetValueNode<PrimitivePtr>(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<bool>(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<ValueNodePtr>();
MS_ASSERT(value_node != nullptr);
auto src_prim = GetValueNode<PrimitivePtr>(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<ValueNodePtr>();
MS_ASSERT(value_node != nullptr);
auto src_prim = GetValueNode<PrimitivePtr>(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<ValueNodePtr>();
MS_ASSERT(value_node != nullptr);
auto src_prim = GetValueNode<PrimitivePtr>(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<ValueNodePtr>();
MS_ASSERT(value_node != nullptr);
auto src_prim = GetValueNode<PrimitivePtr>(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<ops::AddFusion>)
REGIST_PRIMITIVE_ADJUST(kNameTile, MoveAttrMapCommon<ops::TileFusion>)
REGIST_PRIMITIVE_ADJUST(kNameTopK, MoveAttrMapCommon<ops::TopKFusion>)

} // namespace opt
} // namespace mindspore

+ 2
- 2
mindspore/lite/tools/optimizer/graph/primitive_adjust_pass.h View File

@@ -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:


+ 9
- 4
mindspore/lite/tools/optimizer/graph/weight_format_hardcode_pass.cc View File

@@ -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<bool>(prim->GetAttr(ops::kIsDepthWise));
bool is_depth_wise_native =
prim->GetAttr(ops::kIsDepthWiseNative) != nullptr && GetValue<bool>(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);


Loading…
Cancel
Save