From 891591444ec0ce4e552716f6793d41e16d3d8ff1 Mon Sep 17 00:00:00 2001 From: zhengyuanhua Date: Wed, 27 Oct 2021 11:40:15 +0800 Subject: [PATCH] support ocr model --- mindspore/ccsrc/transform/graph_ir/convert.cc | 64 ++++++++++++++- mindspore/ccsrc/transform/graph_ir/convert.h | 2 + .../converter/adapter/acl/acl_pass_impl.cc | 67 +++++++++++++-- .../adapter/acl/mapper/add_fusion_mapper.cc | 34 -------- .../adapter/acl/mapper/add_fusion_mapper.h | 37 --------- .../adapter/acl/mapper/arithmetic_mapper.cc | 74 +++++++++++++++++ .../adapter/acl/mapper/arithmetic_mapper.h | 82 +++++++++++++++++++ .../acl/mapper/conv2d_fusion_mapper.cc | 53 +++++++++++- .../adapter/acl/mapper/conv2d_fusion_mapper.h | 3 + .../adapter/acl/mapper/div_fusion_mapper.cc | 35 -------- .../adapter/acl/mapper/div_fusion_mapper.h | 37 --------- .../adapter/acl/mapper/mul_fusion_mapper.cc | 34 -------- .../adapter/acl/mapper/mul_fusion_mapper.h | 37 --------- .../adapter/acl/mapper/pow_fusion_mapper.cc | 35 -------- .../adapter/acl/mapper/pow_fusion_mapper.h | 37 --------- .../adapter/acl/mapper/primitive_mapper.cc | 14 ++-- .../acl/mapper/spatial_node_adapter.cc | 6 +- .../adapter/acl/mapper/sub_fusion_mapper.cc | 35 -------- .../adapter/acl/mapper/sub_fusion_mapper.h | 37 --------- .../converter/adapter/acl/mapper/tbe_op_def.h | 2 +- 20 files changed, 346 insertions(+), 379 deletions(-) delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.cc delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.h create mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.cc create mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.h delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.cc delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.h delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.cc delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.h delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.cc delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.h delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.cc delete mode 100644 mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.h diff --git a/mindspore/ccsrc/transform/graph_ir/convert.cc b/mindspore/ccsrc/transform/graph_ir/convert.cc index df288839c9f..7816db786ed 100644 --- a/mindspore/ccsrc/transform/graph_ir/convert.cc +++ b/mindspore/ccsrc/transform/graph_ir/convert.cc @@ -910,6 +910,7 @@ DfGraphConvertor &DfGraphConvertor::BuildGraph() { } else if (vars_[name] != nullptr) { MS_LOG(INFO) << "add var input " << it->ToString(); auto op = Convert(it); + UpdateConstOpDesc(it, vars_[name]); MS_EXCEPTION_IF_NULL(op); inputs.push_back(*op); } @@ -942,6 +943,40 @@ DfGraphConvertor &DfGraphConvertor::BuildGraph() { return *this; } +void DfGraphConvertor::UpdateConstOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const { + if (!it->isa()) { + MS_LOG(DEBUG) << "It is not parameter, name: " << it->DebugString(); + return; + } + auto para = it->cast(); + MS_EXCEPTION_IF_NULL(para); + std::string format = kOpFormat_NCHW; + std::string param_debug_info = para->DebugString(); + auto param_format = param_format_.find(param_debug_info); + if (param_format != param_format_.end()) { + format = param_format->second; + MS_LOG(DEBUG) << "Parameter debug info: " << param_debug_info << ", format is " << format; + } + if (format == kOpFormat_NCHW) { + MS_LOG(DEBUG) << "Format is not changed, no need to update op desc, name: " << param_debug_info; + return; + } + if (!para->has_default()) { + MS_LOG(DEBUG) << "Parameter has no default, no need to update op desc, name: " << param_debug_info; + return; + } + auto value = para->default_param(); + MS_EXCEPTION_IF_NULL(value); + auto tensor = value->cast>(); + MS_EXCEPTION_IF_NULL(tensor); + auto const_op_desc = TransformUtil::GetGeTensorDesc(tensor->shape_c(), tensor->data_type(), format); + if (const_op_desc == nullptr) { + MS_LOG(WARNING) << "Create parameter " << para->name() << " output descriptor failed!"; + return; + } + (void)std::static_pointer_cast(op)->update_output_desc_y(*const_op_desc); +} + void DfGraphConvertor::UpdateDataOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const { auto node = std::static_pointer_cast(it); if (node == nullptr) { @@ -1543,6 +1578,27 @@ void DfGraphConvertor::ConvertReshape(const CNodePtr node) { op_cache_[node.get()] = op; } +void DfGraphConvertor::ConvertConv2D(const CNodePtr node) { + MS_EXCEPTION_IF_NULL(node); + OpAdapterPtr adpt = FindAdapter(node, training_); + if (adpt == nullptr) { + return; + } + auto op = adpt->generate(node); + MS_EXCEPTION_IF_NULL(op); + auto value_node = node->input(0)->cast(); + MS_EXCEPTION_IF_NULL(value_node); + MS_EXCEPTION_IF_NULL(value_node->value()); + auto primitive = value_node->value()->cast(); + MS_EXCEPTION_IF_NULL(primitive); + auto value = primitive->GetAttr("padding"); + if (value != nullptr) { + std::string pad_mode = GetValue(value); + (void)op->SetAttr("padding", pad_mode); + } + op_cache_[node.get()] = op; +} + AnfNodePtr DfGraphConvertor::TraceTupleGetItem(const CNodePtr &node, uint64_t *index) { const int TUPLE_GET_ITEM_INDEX = 2; if (node->inputs().size() < 3) { // "tuple_getitem" primitive must have 3 inputs @@ -1735,6 +1791,12 @@ bool DfGraphConvertor::CheckCNode(const std::string &name, const CNodePtr node) return true; } + // Add attr pad mode to Conv2D + if (name == prim::kPrimConv2D->name() || name == prim::kPrimDepthwiseConv2dNative->name()) { + ConvertConv2D(node); + return true; + } + // make_tuple is used for a dynamic_input, convert it to a vector of OutHandlers if (name == prim::kPrimMakeTuple->name()) { ConvertMakeTuple(node); @@ -1813,7 +1875,7 @@ void DfGraphConvertor::SaveParamFormat(const CNodePtr node) { CheckAndConvertUtils::ConvertAttrValueToString(prim->name(), "format", &attr.second); } std::string format = attr.second->ToString(); - if (format != "NCDHW") { + if (format != "NCDHW" && format != "NHWC") { break; } for (size_t i = 1; i < node->size(); i++) { diff --git a/mindspore/ccsrc/transform/graph_ir/convert.h b/mindspore/ccsrc/transform/graph_ir/convert.h index b2db30bc400..10a065df776 100644 --- a/mindspore/ccsrc/transform/graph_ir/convert.h +++ b/mindspore/ccsrc/transform/graph_ir/convert.h @@ -164,6 +164,7 @@ class DfGraphConvertor { void ConvertMakeTuple(const CNodePtr node); void ConvertTopK(const CNodePtr node); void ConvertReshape(const CNodePtr node); + void ConvertConv2D(const CNodePtr node); std::vector CastToInt(const ValuePtr &value); bool CheckCNode(const std::string &name, const CNodePtr node); void TraceOutput(AnfNodePtr node); @@ -177,6 +178,7 @@ class DfGraphConvertor { void BuildSaveCheckpointGraph(); void DrawCNode(const CNodePtr node, const OpAdapterPtr adpt); void UpdateDataOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const; + void UpdateConstOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const; void AddGraphConstInput(const OperatorPtr &op); OperatorPtr ToOperatorPtr(const AnfNodePtr &node); bool IsSourceEdgeNode(const AnfNodePtr &node); diff --git a/mindspore/lite/tools/converter/adapter/acl/acl_pass_impl.cc b/mindspore/lite/tools/converter/adapter/acl/acl_pass_impl.cc index 33acc619feb..93a71d0000d 100644 --- a/mindspore/lite/tools/converter/adapter/acl/acl_pass_impl.cc +++ b/mindspore/lite/tools/converter/adapter/acl/acl_pass_impl.cc @@ -36,6 +36,7 @@ namespace mindspore { namespace opt { static const std::set kDevice = {"Ascend310", "Ascend710"}; +static const std::map kEnumFormatToStrMap = {{Format::NCHW, "NCHW"}, {Format::NHWC, "NHWC"}}; namespace { constexpr auto kMakeTuple = "MakeTuple"; constexpr auto kOutputNames = "outputs_names"; @@ -51,6 +52,50 @@ constexpr auto kDelRedundantTranspose = "DeleteRedundantTranspose"; constexpr size_t kDependInputNum = 3; constexpr size_t kDependFirstInputIdx = 1; constexpr size_t kTupleGetItemFirstInputIdx = 1; + +STATUS PreProcForMindIr(const FuncGraphPtr &func_graph) { return lite::RET_OK; } + +STATUS PreProcForTF(const FuncGraphPtr &func_graph) { + if (!lite::RunOptimizerPass(func_graph, {kInferShapePass})) { + MS_LOG(ERROR) << "Infer shape pass failed."; + return lite::RET_ERROR; + } + auto node_list = TopoSort(func_graph->get_return()); + for (auto &node : node_list) { + CHECK_NULL_RETURN(node); + if (!utils::isa(node)) { + continue; + } + auto cnode = node->cast(); + CHECK_NULL_RETURN(cnode); + auto prim = GetValueNode(cnode->input(0)); + CHECK_NULL_RETURN(prim); + if (prim->GetAttr(ops::kFormat) != nullptr) { + auto node_format = GetValue(prim->GetAttr(ops::kFormat)); + if (kEnumFormatToStrMap.find(node_format) != kEnumFormatToStrMap.end()) { + std::string format = kEnumFormatToStrMap.at(node_format); + prim->AddAttr("io_format", MakeValue(format)); + } + } + } + return lite::RET_OK; +} + +STATUS PreProcForCaffe(const FuncGraphPtr &func_graph) { + if (!lite::RunOptimizerPass(func_graph, {kInferShapePass, kToNCHWFormatPass, kDelRedundantTranspose})) { + MS_LOG(ERROR) << "To nchw format failed."; + return lite::RET_ERROR; + } + return lite::RET_OK; +} + +STATUS PreProcForOnnx(const FuncGraphPtr &func_graph) { + if (!lite::RunOptimizerPass(func_graph, {kInferShapePass, kToNCHWFormatPass, kDelRedundantTranspose})) { + MS_LOG(ERROR) << "To nchw format failed."; + return lite::RET_ERROR; + } + return lite::RET_OK; +} } // namespace AclPassImpl::AclPassImpl(const converter::Flags &config) @@ -174,14 +219,20 @@ STATUS AclPassImpl::PreProcGraph(const FuncGraphPtr &func_graph) { MS_LOG(ERROR) << "Common pass failed."; return lite::RET_ERROR; } - if (fmk_type_ == converter::kFmkTypeMs) { - MS_LOG(DEBUG) << "MindIr no need to change format."; - return lite::RET_OK; - } - // The format of nodes (cnode, parameter, val) must be nchw due to interface of convert om - if (!lite::RunOptimizerPass(func_graph, {kInferShapePass, kToNCHWFormatPass, kDelRedundantTranspose})) { - MS_LOG(ERROR) << "To nchw format failed."; - return lite::RET_ERROR; + std::map> fmk_proc_func = { + {converter::kFmkTypeMs, PreProcForMindIr}, + {converter::kFmkTypeTf, PreProcForTF}, + {converter::kFmkTypeCaffe, PreProcForCaffe}, + {converter::kFmkTypeOnnx, PreProcForOnnx}, + }; + if (fmk_proc_func.find(fmk_type_) != fmk_proc_func.end()) { + auto func = fmk_proc_func.at(fmk_type_); + if (func(func_graph) != lite::RET_OK) { + MS_LOG(ERROR) << "Pre proc failed, fmk " << fmk_type_; + return lite::RET_ERROR; + } + } else { + MS_LOG(WARNING) << "Not support fmk type " << fmk_type_; } MS_LOG(DEBUG) << "Pre proc graph success."; return lite::RET_OK; diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.cc deleted file mode 100644 index ceff2d2862c..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2021 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/adapter/acl/mapper/add_fusion_mapper.h" -#include -#include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" - -namespace mindspore { -namespace lite { -STATUS AddFusionMapper::Mapper(const CNodePtr &cnode) { - auto dst_prim = std::make_shared(); - if (MoveAttrMap(cnode, dst_prim) != RET_OK) { - MS_LOG(ERROR) << "AddFusion mapper failed."; - return RET_ERROR; - } - return RET_OK; -} - -REGISTER_PRIMITIVE_MAPPER(kNameAddFusion, AddFusionMapper) -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.h b/mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.h deleted file mode 100644 index 2a4ef457f79..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/add_fusion_mapper.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2021 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 ACL_MAPPER_PRIMITIVE_ADDFUSION_MAPPER_H -#define ACL_MAPPER_PRIMITIVE_ADDFUSION_MAPPER_H - -#include "tools/converter/adapter/acl/mapper/primitive_mapper.h" -#include "ops/fusion/add_fusion.h" - -using mindspore::ops::kNameAddFusion; - -namespace mindspore { -namespace lite { -class AddFusionMapper : public PrimitiveMapper { - public: - AddFusionMapper() : PrimitiveMapper(kNameAddFusion) {} - - ~AddFusionMapper() override = default; - - STATUS Mapper(const CNodePtr &cnode) override; -}; -} // namespace lite -} // namespace mindspore -#endif // ACL_MAPPER_PRIMITIVE_ADDFUSION_MAPPER_H diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.cc new file mode 100644 index 00000000000..2057d8188e3 --- /dev/null +++ b/mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.cc @@ -0,0 +1,74 @@ +/** + * Copyright 2021 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/adapter/acl/mapper/arithmetic_mapper.h" +#include +#include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" + +namespace mindspore { +namespace lite { +STATUS AddFusionMapper::Mapper(const CNodePtr &cnode) { + auto dst_prim = std::make_shared(); + if (MoveAttrMap(cnode, dst_prim) != RET_OK) { + MS_LOG(ERROR) << "AddFusion mapper failed."; + return RET_ERROR; + } + return RET_OK; +} + +STATUS DivFusionMapper::Mapper(const CNodePtr &cnode) { + auto dst_prim = std::make_shared(); + if (MoveAttrMap(cnode, dst_prim) != RET_OK) { + MS_LOG(ERROR) << "DivFusion mapper failed."; + return RET_ERROR; + } + return RET_OK; +} + +STATUS MulFusionMapper::Mapper(const CNodePtr &cnode) { + auto dst_prim = std::make_shared(); + if (MoveAttrMap(cnode, dst_prim) != RET_OK) { + MS_LOG(ERROR) << "MulFusion mapper failed."; + return RET_ERROR; + } + return RET_OK; +} + +STATUS PowFusionMapper::Mapper(const CNodePtr &cnode) { + auto dst_prim = std::make_shared(); + if (MoveAttrMap(cnode, dst_prim) != RET_OK) { + MS_LOG(ERROR) << "PowFusion mapper failed."; + return RET_ERROR; + } + return RET_OK; +} + +STATUS SubFusionMapper::Mapper(const CNodePtr &cnode) { + auto dst_prim = std::make_shared(); + if (MoveAttrMap(cnode, dst_prim) != RET_OK) { + MS_LOG(ERROR) << "SubFusion mapper failed."; + return RET_ERROR; + } + return RET_OK; +} + +REGISTER_PRIMITIVE_MAPPER(kNameAddFusion, AddFusionMapper) +REGISTER_PRIMITIVE_MAPPER(kNameDivFusion, DivFusionMapper) +REGISTER_PRIMITIVE_MAPPER(kNameMulFusion, MulFusionMapper) +REGISTER_PRIMITIVE_MAPPER(kNamePowFusion, PowFusionMapper) +REGISTER_PRIMITIVE_MAPPER(kNameSubFusion, SubFusionMapper) +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.h b/mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.h new file mode 100644 index 00000000000..241c7adf76c --- /dev/null +++ b/mindspore/lite/tools/converter/adapter/acl/mapper/arithmetic_mapper.h @@ -0,0 +1,82 @@ +/** + * Copyright 2021 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 ACL_MAPPER_PRIMITIVE_ARITHMETIC_MAPPER_H +#define ACL_MAPPER_PRIMITIVE_ARITHMETIC_MAPPER_H + +#include "tools/converter/adapter/acl/mapper/primitive_mapper.h" +#include "ops/fusion/add_fusion.h" +#include "ops/fusion/div_fusion.h" +#include "ops/fusion/mul_fusion.h" +#include "ops/fusion/pow_fusion.h" +#include "ops/fusion/sub_fusion.h" + +namespace mindspore { +namespace lite { +using mindspore::ops::kNameAddFusion; +using mindspore::ops::kNameDivFusion; +using mindspore::ops::kNameMulFusion; +using mindspore::ops::kNamePowFusion; +using mindspore::ops::kNameSubFusion; + +class AddFusionMapper : public PrimitiveMapper { + public: + AddFusionMapper() : PrimitiveMapper(kNameAddFusion) {} + + ~AddFusionMapper() override = default; + + STATUS Mapper(const CNodePtr &cnode) override; +}; + +class DivFusionMapper : public PrimitiveMapper { + public: + DivFusionMapper() : PrimitiveMapper(kNameDivFusion) {} + + ~DivFusionMapper() override = default; + + STATUS Mapper(const CNodePtr &cnode) override; +}; + +class MulFusionMapper : public PrimitiveMapper { + public: + MulFusionMapper() : PrimitiveMapper(kNameMulFusion) {} + + ~MulFusionMapper() override = default; + + STATUS Mapper(const CNodePtr &cnode) override; +}; + +class PowFusionMapper : public PrimitiveMapper { + public: + PowFusionMapper() : PrimitiveMapper(kNamePowFusion) {} + + ~PowFusionMapper() override = default; + + STATUS Mapper(const CNodePtr &cnode) override; +}; + +class SubFusionMapper : public PrimitiveMapper { + public: + SubFusionMapper() : PrimitiveMapper(kNameSubFusion) {} + + ~SubFusionMapper() override = default; + + STATUS Mapper(const CNodePtr &cnode) override; +}; +} // namespace lite +} // namespace mindspore + +#endif // ACL_MAPPER_PRIMITIVE_ARITHMETIC_MAPPER_H diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.cc index cd4bb2ee13d..58c2c190693 100644 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.cc +++ b/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.cc @@ -15,11 +15,25 @@ */ #include "tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.h" +#include +#include +#include #include "memory" #include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" +#include "tools/converter/adapter/acl/mapper/tbe_op_def.h" +#include "src/common/log_util.h" namespace mindspore { namespace lite { +static const std::map kPadModToStrMap = { + {PadMode::PAD, "CALCULATED"}, + {PadMode::SAME, "SAME"}, + {PadMode::VALID, "VALID"}, +}; +namespace { +constexpr auto kNamePaddingMode = "padding"; +} // namespace + STATUS Conv2DFusionMapper::Mapper(const CNodePtr &cnode) { ValueNodePtr value_node = nullptr; PrimitivePtr src_prim = nullptr; @@ -27,10 +41,19 @@ STATUS Conv2DFusionMapper::Mapper(const CNodePtr &cnode) { MS_LOG(ERROR) << "Get primitive from cnode failed."; return lite::RET_ERROR; } - auto dst_prim = std::make_shared(); - MS_ASSERT(dst_prim != nullptr); + bool is_depth_wise = false; + auto depth_wise_ptr = src_prim->GetAttr(ops::kIsDepthWise); + if (depth_wise_ptr != nullptr) { + is_depth_wise = GetValue(depth_wise_ptr); + } + PrimitivePtr dst_prim = nullptr; + if (!is_depth_wise) { + dst_prim = std::make_shared(); + } else { + dst_prim = std::make_shared(); + } + CHECK_NULL_RETURN(dst_prim); dst_prim->SetAttrs(src_prim->attrs()); - auto status = AttrAdjust(dst_prim, ops::kStride); if (status != lite::RET_OK) { MS_LOG(ERROR) << "adjust stride failed."; @@ -41,10 +64,34 @@ STATUS Conv2DFusionMapper::Mapper(const CNodePtr &cnode) { MS_LOG(ERROR) << "adjust dilation failed."; return status; } + status = AdjustAttrPad(dst_prim); + if (status != lite::RET_OK) { + MS_LOG(ERROR) << "adjust pad failed."; + return status; + } value_node->set_value(dst_prim); return lite::RET_OK; } +STATUS Conv2DFusionMapper::AdjustAttrPad(const PrimitivePtr &prim) { + // attr pad val + auto pad_ptr = prim->GetAttr(ops::kPadList); + if (pad_ptr == nullptr) { + std::vector pad_list = {0, 0, 0, 0}; + prim->AddAttr(ops::kPadList, MakeValue(pad_list)); + } + // attr pad mode + auto pad_mode_val = prim->GetAttr(ops::kPadMode); + if (pad_mode_val != nullptr) { + auto pad_mode = GetValue(pad_mode_val); + if (kPadModToStrMap.find(pad_mode) != kPadModToStrMap.end()) { + std::string padding_mode = kPadModToStrMap.at(pad_mode); + prim->AddAttr(kNamePaddingMode, MakeValue(padding_mode)); + } + } + return lite::RET_OK; +} + REGISTER_PRIMITIVE_MAPPER(kNameConv2DFusion, Conv2DFusionMapper) } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.h b/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.h index ba5d298b06d..a33727cd2fb 100644 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.h +++ b/mindspore/lite/tools/converter/adapter/acl/mapper/conv2d_fusion_mapper.h @@ -30,6 +30,9 @@ class Conv2DFusionMapper : public PrimitiveMapper { ~Conv2DFusionMapper() override = default; STATUS Mapper(const CNodePtr &cnode) override; + + private: + STATUS AdjustAttrPad(const PrimitivePtr &prim); }; } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.cc deleted file mode 100644 index 6962808614a..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.cc +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2021 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/adapter/acl/mapper/div_fusion_mapper.h" -#include -#include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" -#include "src/common/log_util.h" - -namespace mindspore { -namespace lite { -STATUS DivFusionMapper::Mapper(const CNodePtr &cnode) { - auto dst_prim = std::make_shared(); - if (MoveAttrMap(cnode, dst_prim) != RET_OK) { - MS_LOG(ERROR) << "DivFusion mapper failed."; - return RET_ERROR; - } - return RET_OK; -} - -REGISTER_PRIMITIVE_MAPPER(kNameDivFusion, DivFusionMapper) -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.h b/mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.h deleted file mode 100644 index a020913a88e..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/div_fusion_mapper.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2021 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 ACL_MAPPER_PRIMITIVE_DIV_FUSION_MAPPER_H -#define ACL_MAPPER_PRIMITIVE_DIV_FUSION_MAPPER_H - -#include "tools/converter/adapter/acl/mapper/primitive_mapper.h" -#include "ops/fusion/div_fusion.h" - -namespace mindspore { -namespace lite { -using mindspore::ops::kNameDivFusion; - -class DivFusionMapper : public PrimitiveMapper { - public: - DivFusionMapper() : PrimitiveMapper(kNameDivFusion) {} - - ~DivFusionMapper() override = default; - - STATUS Mapper(const CNodePtr &cnode) override; -}; -} // namespace lite -} // namespace mindspore -#endif // ACL_MAPPER_PRIMITIVE_DIV_FUSION_MAPPER_H diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.cc deleted file mode 100644 index 451c32deb7b..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2021 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/adapter/acl/mapper/mul_fusion_mapper.h" -#include -#include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" - -namespace mindspore { -namespace lite { -STATUS MulFusionMapper::Mapper(const CNodePtr &cnode) { - auto dst_prim = std::make_shared(); - if (MoveAttrMap(cnode, dst_prim) != RET_OK) { - MS_LOG(ERROR) << "MulFusion mapper failed."; - return RET_ERROR; - } - return RET_OK; -} - -REGISTER_PRIMITIVE_MAPPER(kNameMulFusion, MulFusionMapper) -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.h b/mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.h deleted file mode 100644 index d11b988000d..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/mul_fusion_mapper.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2021 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 ACL_MAPPER_PRIMITIVE_MULFUSION_MAPPER_H -#define ACL_MAPPER_PRIMITIVE_MULFUSION_MAPPER_H - -#include "tools/converter/adapter/acl/mapper/primitive_mapper.h" -#include "ops/fusion/mul_fusion.h" - -using mindspore::ops::kNameMulFusion; - -namespace mindspore { -namespace lite { -class MulFusionMapper : public PrimitiveMapper { - public: - MulFusionMapper() : PrimitiveMapper(kNameMulFusion) {} - - ~MulFusionMapper() override = default; - - STATUS Mapper(const CNodePtr &cnode) override; -}; -} // namespace lite -} // namespace mindspore -#endif // ACL_MAPPER_PRIMITIVE_MULFUSION_MAPPER_H diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.cc deleted file mode 100644 index ac270656bb8..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.cc +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2021 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/adapter/acl/mapper/pow_fusion_mapper.h" -#include -#include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" -#include "src/common/log_util.h" - -namespace mindspore { -namespace lite { -STATUS PowFusionMapper::Mapper(const CNodePtr &cnode) { - auto dst_prim = std::make_shared(); - if (MoveAttrMap(cnode, dst_prim) != RET_OK) { - MS_LOG(ERROR) << "PowFusion mapper failed."; - return RET_ERROR; - } - return RET_OK; -} - -REGISTER_PRIMITIVE_MAPPER(kNamePowFusion, PowFusionMapper) -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.h b/mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.h deleted file mode 100644 index 1da325ee934..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/pow_fusion_mapper.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2021 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 ACL_MAPPER_PRIMITIVE_POW_FUSION_MAPPER_H -#define ACL_MAPPER_PRIMITIVE_POW_FUSION_MAPPER_H - -#include "tools/converter/adapter/acl/mapper/primitive_mapper.h" -#include "ops/fusion/pow_fusion.h" - -namespace mindspore { -namespace lite { -using mindspore::ops::kNamePowFusion; - -class PowFusionMapper : public PrimitiveMapper { - public: - PowFusionMapper() : PrimitiveMapper(kNamePowFusion) {} - - ~PowFusionMapper() override = default; - - STATUS Mapper(const CNodePtr &cnode) override; -}; -} // namespace lite -} // namespace mindspore -#endif // ACL_MAPPER_PRIMITIVE_POW_FUSION_MAPPER_H diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/primitive_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/primitive_mapper.cc index 2c70856a747..7e84ab120f3 100644 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/primitive_mapper.cc +++ b/mindspore/lite/tools/converter/adapter/acl/mapper/primitive_mapper.cc @@ -89,11 +89,15 @@ STATUS PrimitiveMapper::AttrAdjust(const PrimitivePtr &prim, const std::string & MS_LOG(ERROR) << name << " Value num must be two."; return lite::RET_ERROR; } - std::vector new_value; - new_value.push_back(1); - new_value.push_back(1); - new_value.push_back(static_cast(origin_value[0])); - new_value.push_back(static_cast(origin_value[1])); + int64_t format = Format::NCHW; + if (prim->GetAttr(ops::kFormat) != nullptr) { + format = GetValue(prim->GetAttr(ops::kFormat)); + } + std::vector new_value = {1, 1, static_cast(origin_value[0]), static_cast(origin_value[1])}; + if (format == Format::NHWC) { + std::vector tmp = {1, static_cast(origin_value[0]), static_cast(origin_value[1]), 1}; + new_value.swap(tmp); + } prim->AddAttr(name, MakeValue(new_value)); return lite::RET_OK; } diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/spatial_node_adapter.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/spatial_node_adapter.cc index 4a9f5644b67..1b15787b304 100644 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/spatial_node_adapter.cc +++ b/mindspore/lite/tools/converter/adapter/acl/mapper/spatial_node_adapter.cc @@ -87,14 +87,14 @@ static STATUS AdapteNodeWithMultiOutputs(const FuncGraphPtr &func_graph, const C auto input_cnode = input->cast(); std::string input_func_name = GetCNodeFuncName(input_cnode); if (kCNodeWithMultiOutputs.find(input_func_name) != kCNodeWithMultiOutputs.end()) { - MS_LOG(INFO) << "Adapter cnode with multioutputs: " << cnode_func_name; + MS_LOG(INFO) << "Input " << input_func_name << " of cnode " << cnode_func_name << " has multioutputs"; CNodePtr get_item_cnode = CreateTupleGetItemNode(func_graph, input_cnode); if (get_item_cnode == nullptr) { - MS_LOG(ERROR) << "Create tuple item for " << cnode_func_name << " failed."; + MS_LOG(ERROR) << "Create tuple item for " << input_func_name << " of " << cnode_func_name << " failed."; return lite::RET_ERROR; } if (!manager->Replace(input_cnode, get_item_cnode)) { - MS_LOG(ERROR) << "Replace " << cnode_func_name << " failed."; + MS_LOG(ERROR) << "Replace " << input_func_name << " of " << cnode_func_name << " failed."; return lite::RET_ERROR; } } diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.cc b/mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.cc deleted file mode 100644 index 53d91d18d7a..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.cc +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2021 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/adapter/acl/mapper/sub_fusion_mapper.h" -#include -#include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" -#include "src/common/log_util.h" - -namespace mindspore { -namespace lite { -STATUS SubFusionMapper::Mapper(const CNodePtr &cnode) { - auto dst_prim = std::make_shared(); - if (MoveAttrMap(cnode, dst_prim) != RET_OK) { - MS_LOG(ERROR) << "SubFusion mapper failed."; - return RET_ERROR; - } - return RET_OK; -} - -REGISTER_PRIMITIVE_MAPPER(kNameSubFusion, SubFusionMapper) -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.h b/mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.h deleted file mode 100644 index 47abdd51cda..00000000000 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/sub_fusion_mapper.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2021 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 ACL_MAPPER_PRIMITIVE_SUB_FUSION_MAPPER_H -#define ACL_MAPPER_PRIMITIVE_SUB_FUSION_MAPPER_H - -#include "tools/converter/adapter/acl/mapper/primitive_mapper.h" -#include "ops/fusion/sub_fusion.h" - -namespace mindspore { -namespace lite { -using mindspore::ops::kNameSubFusion; - -class SubFusionMapper : public PrimitiveMapper { - public: - SubFusionMapper() : PrimitiveMapper(kNameSubFusion) {} - - ~SubFusionMapper() override = default; - - STATUS Mapper(const CNodePtr &cnode) override; -}; -} // namespace lite -} // namespace mindspore -#endif // ACL_MAPPER_PRIMITIVE_SUB_FUSION_MAPPER_H diff --git a/mindspore/lite/tools/converter/adapter/acl/mapper/tbe_op_def.h b/mindspore/lite/tools/converter/adapter/acl/mapper/tbe_op_def.h index 195b024db72..1b3d92e2458 100644 --- a/mindspore/lite/tools/converter/adapter/acl/mapper/tbe_op_def.h +++ b/mindspore/lite/tools/converter/adapter/acl/mapper/tbe_op_def.h @@ -41,7 +41,7 @@ ADD_CONVERTER_TBE_OP(BNInference) ADD_CONVERTER_TBE_OP(Deconvolution) ADD_CONVERTER_TBE_OP(Upsample) ADD_CONVERTER_TBE_OP(Conv2DTransposeD) - +ADD_CONVERTER_TBE_OP(DepthwiseConv2dNative) } // namespace acl } // namespace lite } // namespace mindspore