Merge pull request !25011 from zhengyuanhua/model_br1tags/v1.6.0
| @@ -71,6 +71,10 @@ void AclModelOptions::RenameInput(const std::vector<std::string> &input_names) { | |||
| } | |||
| input_shape_ = ""; | |||
| for (size_t i = 0; i < input_shape_map_.size(); i++) { | |||
| if (input_shape_map_.find(i) == input_shape_map_.end()) { | |||
| MS_LOG(WARNING) << "Not find the key: " << i; | |||
| return; | |||
| } | |||
| std::string s; | |||
| for (size_t j = 0; j < input_shape_map_[i].size(); j++) { | |||
| s += std::to_string(input_shape_map_[i][j]) + ","; | |||
| @@ -332,6 +332,7 @@ constexpr const char kNameStridedSliceV2[] = "StridedSliceV2"; | |||
| constexpr const char kNameBNInference[] = "BNInference"; | |||
| constexpr const char kNameDeconvolution[] = "Deconvolution"; | |||
| constexpr const char kNameUpsample[] = "Upsample"; | |||
| constexpr const char kNameConv2DTransposeD[] = "Conv2DTransposeD"; | |||
| class OpAdapterMap { | |||
| public: | |||
| @@ -64,6 +64,21 @@ OUTPUT_MAP(Deconvolution) = {{0, OUTPUT_DESC(y)}}; | |||
| REG_ADPT_DESC(Deconvolution, kNameDeconvolution, ADPT_DESC(Deconvolution)) | |||
| REG_ADPT_DESC(Conv2DTranspose, kConv2DTransposeOpName, ADPT_DESC(Conv2DBackpropInputD)) | |||
| // Conv2DTransposeD for tf onnx inference | |||
| INPUT_MAP(Conv2DTransposeD) = { | |||
| {1, INPUT_DESC(x)}, {2, INPUT_DESC(filter)}, {3, INPUT_DESC(bias)}, {4, INPUT_DESC(offset_w)}}; | |||
| ATTR_MAP(Conv2DTransposeD) = { | |||
| {"input_size", ATTR_DESC(input_size, AnyTraits<std::vector<int64_t>>(), AnyTraits<std::vector<int64_t>>())}, | |||
| {"stride", ATTR_DESC(strides, AnyTraits<std::vector<int64_t>>(), AnyTraits<std::vector<int64_t>>())}, | |||
| {"pad_list", ATTR_DESC(pads, AnyTraits<std::vector<int64_t>>(), AnyTraits<std::vector<int64_t>>())}, | |||
| {"dilation", ATTR_DESC(dilations, AnyTraits<std::vector<int64_t>>(), AnyTraits<std::vector<int64_t>>())}, | |||
| {"group", ATTR_DESC(groups, AnyTraits<int64_t>())}, | |||
| {"format", ATTR_DESC(data_format, AnyTraits<string>())}, | |||
| {"output_paddings", ATTR_DESC(output_padding, AnyTraits<std::vector<int64_t>>(), AnyTraits<std::vector<int64_t>>())}, | |||
| {"offset", ATTR_DESC(offset_x, AnyTraits<int64_t>())}}; | |||
| OUTPUT_MAP(Conv2DTransposeD) = {{0, OUTPUT_DESC(y)}}; | |||
| REG_ADPT_DESC(Conv2DTransposeD, kNameConv2DTransposeD, ADPT_DESC(Conv2DTransposeD)) | |||
| // Conv2DBackpropFilterD | |||
| INPUT_MAP(Conv2DBackpropFilterD) = {{1, INPUT_DESC(out_backprop)}, {2, INPUT_DESC(x)}}; | |||
| INPUT_ATTR_MAP(Conv2DBackpropFilterD) = { | |||
| @@ -72,5 +72,8 @@ DECLARE_OP_USE_OUTPUT(DepthwiseConv2DBackpropInputD) | |||
| DECLARE_OP_ADAPTER(Deconvolution) | |||
| DECLARE_OP_USE_OUTPUT(Deconvolution) | |||
| DECLARE_OP_ADAPTER(Conv2DTransposeD) | |||
| DECLARE_OP_USE_OUTPUT(Conv2DTransposeD) | |||
| } // namespace mindspore::transform | |||
| #endif // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_DECLARE_NN_CALCULATION_OPS_DECLARE_H_ | |||
| @@ -146,11 +146,14 @@ STATUS AclPassImpl::DeparseGraph(const FuncGraphPtr &func_graph, const FuncGraph | |||
| MS_LOG(ERROR) << "Run mapper primitive failed."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| if (lite::AdapteSpatialNode(func_graph, manager) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Adapter spatial node failed."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| if (lite::acl::DelRedundantParameter(func_graph) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Delete redundant parameter failed."; | |||
| return lite::RET_OK; | |||
| } | |||
| return lite::RET_OK; | |||
| } | |||
| @@ -167,6 +170,10 @@ STATUS AclPassImpl::CommonPass(const FuncGraphPtr &func_graph) { | |||
| } | |||
| STATUS AclPassImpl::PreProcGraph(const FuncGraphPtr &func_graph) { | |||
| if (CommonPass(func_graph) != lite::RET_OK) { | |||
| 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; | |||
| @@ -23,6 +23,7 @@ | |||
| #include "abstract/abstract_value.h" | |||
| #include "utils/utils.h" | |||
| #include "src/common/log_util.h" | |||
| #include "ir/func_graph.h" | |||
| namespace mindspore { | |||
| namespace lite { | |||
| @@ -178,6 +179,19 @@ std::string GetCNodeTargetFuncName(const CNodePtr &cnode) { | |||
| } | |||
| return name; | |||
| } | |||
| STATUS DelRedundantParameter(const FuncGraphPtr &func_graph) { | |||
| CHECK_NULL_RETURN(func_graph); | |||
| auto nodes = TopoSort(func_graph->get_return()); | |||
| auto parameters = func_graph->parameters(); | |||
| for (auto ¶meter : parameters) { | |||
| CHECK_NULL_RETURN(parameter); | |||
| if (std::find(nodes.begin(), nodes.end(), parameter) == nodes.end()) { | |||
| func_graph->DropNode(parameter); | |||
| } | |||
| } | |||
| return lite::RET_OK; | |||
| } | |||
| } // namespace acl | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -33,6 +33,8 @@ TypeId GetTypeFromNode(const AnfNodePtr &node); | |||
| std::vector<int> GetIntParameterData(const ParameterPtr ¶m_ptr); | |||
| std::string GetCNodeTargetFuncName(const CNodePtr &cnode); | |||
| STATUS DelRedundantParameter(const FuncGraphPtr &func_graph); | |||
| } // namespace acl | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -19,6 +19,7 @@ | |||
| #include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" | |||
| #include "tools/converter/adapter/acl/mapper/tbe_op_def.h" | |||
| #include "include/registry/parser_context.h" | |||
| #include "src/common/log_util.h" | |||
| namespace mindspore { | |||
| namespace lite { | |||
| @@ -33,29 +34,30 @@ STATUS AvgPoolFusionMapper::Mapper(const CNodePtr &cnode) { | |||
| auto attr_val = src_prim->GetAttr(ops::kFmkType); | |||
| int fmk_type = attr_val != nullptr ? GetValue<int>(attr_val) : converter::kFmkTypeTf; | |||
| PrimitivePtr dst_prim = nullptr; | |||
| CreateTargetPrim(src_prim, &dst_prim, fmk_type); | |||
| CHECK_NULL_RETURN(dst_prim); | |||
| dst_prim->SetAttrs(src_prim->attrs()); | |||
| if (AdjustPoolAttr(fmk_type, kNameAvgPoolFusion, dst_prim) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Adjust pool attr failed."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| value_node->set_value(dst_prim); | |||
| return lite::RET_OK; | |||
| } | |||
| void AvgPoolFusionMapper::CreateTargetPrim(const PrimitivePtr &src_prim, PrimitivePtr *dst_prim, int fmk_type) { | |||
| if (fmk_type == converter::kFmkTypeCaffe) { | |||
| dst_prim = std::make_shared<acl::Pooling>(); | |||
| *dst_prim = std::make_shared<acl::Pooling>(); | |||
| } else if (fmk_type == converter::kFmkTypeOnnx) { | |||
| ValuePtr val_ptr = src_prim->GetAttr(ops::kKernelSize); | |||
| if (val_ptr == nullptr) { | |||
| dst_prim = std::make_shared<acl::GlobalAveragePool>(); | |||
| *dst_prim = std::make_shared<acl::GlobalAveragePool>(); | |||
| } else { | |||
| dst_prim = std::make_shared<acl::AvgPoolV2>(); | |||
| *dst_prim = std::make_shared<acl::AvgPoolV2>(); | |||
| } | |||
| } else { | |||
| dst_prim = std::make_shared<ops::AvgPool>(); | |||
| *dst_prim = std::make_shared<ops::AvgPool>(); | |||
| } | |||
| if (dst_prim == nullptr) { | |||
| MS_LOG(ERROR) << "Get primitive by fmk type failed."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| dst_prim->SetAttrs(src_prim->attrs()); | |||
| if (AdjustPoolAttr(fmk_type, kNameAvgPoolFusion, dst_prim) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Adjust pool attr failed."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| value_node->set_value(dst_prim); | |||
| return lite::RET_OK; | |||
| } | |||
| REGISTER_PRIMITIVE_MAPPER(kNameAvgPoolFusion, AvgPoolFusionMapper) | |||
| @@ -30,6 +30,9 @@ class AvgPoolFusionMapper : public PrimitiveMapper { | |||
| ~AvgPoolFusionMapper() override = default; | |||
| STATUS Mapper(const CNodePtr &cnode) override; | |||
| private: | |||
| void CreateTargetPrim(const PrimitivePtr &src_prim, PrimitivePtr *dst_prim, int fmk_type); | |||
| }; | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -30,7 +30,7 @@ STATUS CastMapper::Mapper(const CNodePtr &cnode) { | |||
| return lite::RET_ERROR; | |||
| } | |||
| if (cnode->size() != kNameCastInputNum) { | |||
| MS_LOG(ERROR) << "Input size of gather must be three."; | |||
| MS_LOG(ERROR) << "Input size of cast must be three, real size: " << cnode->size(); | |||
| return lite::RET_ERROR; | |||
| } | |||
| // convert last parameter to const value node | |||
| @@ -31,29 +31,13 @@ STATUS ConcatMapper::Mapper(const CNodePtr &cnode) { | |||
| MS_LOG(ERROR) << "Concat rename failed."; | |||
| return RET_ERROR; | |||
| } | |||
| if (AddAttrForDynInputPrimitive(cnode) != RET_OK) { | |||
| if (AddAttrForDynInputPrimitive(cnode, kNameInputNums) != RET_OK) { | |||
| MS_LOG(ERROR) << "Concat mapper failed."; | |||
| return RET_ERROR; | |||
| } | |||
| return RET_OK; | |||
| } | |||
| STATUS ConcatMapper::AddAttrForDynInputPrimitive(const CNodePtr &cnode) { | |||
| auto value_node = cnode->input(0)->cast<ValueNodePtr>(); | |||
| MS_ASSERT(value_node != nullptr); | |||
| auto prim = GetValueNode<PrimitivePtr>(value_node); | |||
| if (prim == nullptr) { | |||
| MS_LOG(ERROR) << "Value node is invalid."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| // add attr input num for dynamic input op | |||
| int64_t num = static_cast<int64_t>(cnode->size()); | |||
| if (num > 1) { | |||
| prim->AddAttr(kNameInputNums, MakeValue(num - 1)); | |||
| } | |||
| return lite::RET_OK; | |||
| } | |||
| STATUS ConcatMapper::RenameNode(const CNodePtr &cnode) { | |||
| const std::string kNamePercent = "%"; | |||
| std::string name = cnode->fullname_with_scope(); | |||
| @@ -33,7 +33,6 @@ class ConcatMapper : public PrimitiveMapper { | |||
| STATUS Mapper(const CNodePtr &cnode) override; | |||
| private: | |||
| STATUS AddAttrForDynInputPrimitive(const CNodePtr &cnode); | |||
| STATUS RenameNode(const CNodePtr &cnode); | |||
| }; | |||
| } // namespace lite | |||
| @@ -16,12 +16,17 @@ | |||
| #include "tools/converter/adapter/acl/mapper/conv2d_transpose_fusion_mapper.h" | |||
| #include <memory> | |||
| #include <vector> | |||
| #include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" | |||
| #include "tools/converter/adapter/acl/common/utils.h" | |||
| #include "include/registry/parser_context.h" | |||
| #include "tools/converter/adapter/acl/mapper/tbe_op_def.h" | |||
| namespace mindspore { | |||
| namespace lite { | |||
| namespace { | |||
| constexpr auto kNameOutputPaddingNum = 2; | |||
| } // namespace | |||
| STATUS Conv2dTransposeMapper::Mapper(const CNodePtr &cnode) { | |||
| ValueNodePtr value_node = nullptr; | |||
| PrimitivePtr src_prim = nullptr; | |||
| @@ -36,19 +41,63 @@ STATUS Conv2dTransposeMapper::Mapper(const CNodePtr &cnode) { | |||
| if (fmk_type == converter::kFmkTypeCaffe) { | |||
| dst_prim = std::make_shared<acl::Deconvolution>(); | |||
| } else { | |||
| dst_prim = std::make_shared<ops::Conv2DTranspose>(); | |||
| dst_prim = std::make_shared<acl::Conv2DTransposeD>(); | |||
| } | |||
| MS_ASSERT(dst_prim != nullptr); | |||
| dst_prim->SetAttrs(src_prim->attrs()); | |||
| if (fmk_type != converter::kFmkTypeCaffe) { | |||
| if (AdjustGeAttr(cnode, dst_prim) != RET_OK) { | |||
| MS_LOG(ERROR) << "Adjust ge attr failed."; | |||
| return RET_ERROR; | |||
| } | |||
| } | |||
| auto status = AttrAdjust(dst_prim, ops::kDilation); | |||
| if (status != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "adjust failed."; | |||
| MS_LOG(ERROR) << "Adjust dilation failed."; | |||
| return status; | |||
| } | |||
| value_node->set_value(dst_prim); | |||
| return lite::RET_OK; | |||
| } | |||
| STATUS Conv2dTransposeMapper::AdjustGeAttr(const CNodePtr &cnode, const PrimitivePtr &dst_prim) { | |||
| std::vector<int64_t> shape; | |||
| if (acl::GetShapeVectorFromCNode(cnode, &shape) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Get shape failed from conv2d transpose."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| dst_prim->AddAttr("input_size", MakeValue(shape)); | |||
| dst_prim->AddAttr("format", MakeValue("NCHW")); | |||
| if (AttrAdjust(dst_prim, ops::kStride) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Adjust strides failed."; | |||
| return RET_ERROR; | |||
| } | |||
| if (AdjustOutputPadding(dst_prim) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Adjust output padding failed."; | |||
| return RET_ERROR; | |||
| } | |||
| return RET_OK; | |||
| } | |||
| STATUS Conv2dTransposeMapper::AdjustOutputPadding(const PrimitivePtr &dst_prim) { | |||
| const int kDim3D = 2; | |||
| const int kDim4D = 3; | |||
| std::vector<int64_t> output_padding = {0, 0, 0, 0}; | |||
| auto value_ptr = dst_prim->GetAttr(ops::kOutputPaddings); | |||
| if (value_ptr != nullptr) { | |||
| std::vector<int64_t> val = GetValue<std::vector<int64_t>>(value_ptr); | |||
| if (val.size() != kNameOutputPaddingNum) { | |||
| MS_LOG(ERROR) << "Output padding num is not 2"; | |||
| return RET_ERROR; | |||
| } | |||
| output_padding[kDim3D] = val[0]; | |||
| output_padding[kDim4D] = val[1]; | |||
| } | |||
| dst_prim->set_attr(ops::kOutputPaddings, MakeValue(output_padding)); | |||
| return RET_OK; | |||
| } | |||
| REGISTER_PRIMITIVE_MAPPER(kNameConv2dTransposeFusion, Conv2dTransposeMapper) | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -30,6 +30,10 @@ class Conv2dTransposeMapper : public PrimitiveMapper { | |||
| ~Conv2dTransposeMapper() override = default; | |||
| STATUS Mapper(const CNodePtr &cnode) override; | |||
| private: | |||
| STATUS AdjustGeAttr(const CNodePtr &cnode, const PrimitivePtr &dst_prim); | |||
| STATUS AdjustOutputPadding(const PrimitivePtr &dst_prim); | |||
| }; | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -0,0 +1,35 @@ | |||
| /** | |||
| * 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 <memory> | |||
| #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<ops::Div>(); | |||
| 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 | |||
| @@ -0,0 +1,37 @@ | |||
| /** | |||
| * 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 | |||
| @@ -21,30 +21,13 @@ | |||
| namespace mindspore { | |||
| namespace lite { | |||
| STATUS EltWiseMapper::Mapper(const CNodePtr &cnode) { | |||
| if (AddAttrForDynInputPrimitive(cnode) != RET_OK) { | |||
| if (AddAttrForDynInputPrimitive(cnode, ops::kN) != RET_OK) { | |||
| MS_LOG(ERROR) << "EltWise mapper failed."; | |||
| return RET_ERROR; | |||
| } | |||
| return RET_OK; | |||
| } | |||
| STATUS EltWiseMapper::AddAttrForDynInputPrimitive(const CNodePtr &cnode) { | |||
| MS_ASSERT(cnode != nullptr); | |||
| auto value_node = cnode->input(0)->cast<ValueNodePtr>(); | |||
| MS_ASSERT(value_node != nullptr); | |||
| auto prim = GetValueNode<PrimitivePtr>(value_node); | |||
| if (prim == nullptr) { | |||
| MS_LOG(ERROR) << "Value node is invalid."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| // add attr input num for dynamic input op | |||
| int64_t num = static_cast<int64_t>(cnode->size()); | |||
| if (num > 1) { | |||
| prim->AddAttr(ops::kN, MakeValue(num - 1)); | |||
| } | |||
| return lite::RET_OK; | |||
| } | |||
| REGISTER_PRIMITIVE_MAPPER(kNameEltwise, EltWiseMapper) | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -31,9 +31,6 @@ class EltWiseMapper : public PrimitiveMapper { | |||
| ~EltWiseMapper() override = default; | |||
| STATUS Mapper(const CNodePtr &cnode) override; | |||
| private: | |||
| STATUS AddAttrForDynInputPrimitive(const CNodePtr &cnode); | |||
| }; | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -0,0 +1,35 @@ | |||
| /** | |||
| * 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 <memory> | |||
| #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<ops::Pow>(); | |||
| 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 | |||
| @@ -0,0 +1,37 @@ | |||
| /** | |||
| * 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 | |||
| @@ -196,5 +196,20 @@ STATUS PrimitiveMapper::AddAttrToInput(const FuncGraphPtr &func_graph, const CNo | |||
| cnode->set_inputs(inputs); | |||
| return lite::RET_OK; | |||
| } | |||
| STATUS PrimitiveMapper::AddAttrForDynInputPrimitive(const CNodePtr &cnode, const std::string &attr_name) { | |||
| CHECK_NULL_RETURN(cnode); | |||
| CHECK_NULL_RETURN(cnode->input(0)); | |||
| auto value_node = cnode->input(0)->cast<ValueNodePtr>(); | |||
| CHECK_NULL_RETURN(value_node); | |||
| auto prim = GetValueNode<PrimitivePtr>(value_node); | |||
| CHECK_NULL_RETURN(prim); | |||
| // add attr input num for dynamic input op | |||
| int64_t num = static_cast<int64_t>(cnode->size()); | |||
| if (num > 1) { | |||
| prim->AddAttr(attr_name, MakeValue(num - 1)); | |||
| } | |||
| return lite::RET_OK; | |||
| } | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -45,6 +45,8 @@ class PrimitiveMapper { | |||
| STATUS AddAttrToInput(const FuncGraphPtr &func_graph, const CNodePtr &cnode, const PrimitivePtr &dst_prim, | |||
| const std::string &attr_name, size_t flag); | |||
| STATUS AddAttrForDynInputPrimitive(const CNodePtr &cnode, const std::string &attr_name); | |||
| private: | |||
| void AdjustCaffePoolAttr(const std::string &src_prim_name, const PrimitivePtr &dst_prim); | |||
| @@ -0,0 +1,88 @@ | |||
| /** | |||
| * 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/reduce_fusion_mapper.h" | |||
| #include <memory> | |||
| #include <vector> | |||
| #include <algorithm> | |||
| #include "tools/converter/adapter/acl/mapper/primitive_mapper_register.h" | |||
| #include "tools/converter/adapter/acl/mapper/tbe_op_def.h" | |||
| #include "tools/converter/adapter/acl/common/utils.h" | |||
| #include "src/common/log_util.h" | |||
| #include "ops/op_utils.h" | |||
| #include "ops/reduce_sum.h" | |||
| #include "ops/reduce_mean.h" | |||
| namespace mindspore { | |||
| namespace lite { | |||
| namespace { | |||
| constexpr auto kNameReduceInputNum = 3; | |||
| } // namespace | |||
| STATUS ReduceFusionMapper::Mapper(const CNodePtr &cnode) { | |||
| ValueNodePtr value_node = nullptr; | |||
| PrimitivePtr src_prim = nullptr; | |||
| if (GetValueNodeAndPrimFromCnode(cnode, &value_node, &src_prim) != lite::RET_OK) { | |||
| MS_LOG(ERROR) << "Get value node and primitive from cnode failed."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| auto attr_val = src_prim->GetAttr(ops::kMode); | |||
| CHECK_NULL_RETURN(attr_val); | |||
| int64_t mode = GetValue<int64_t>(attr_val); | |||
| PrimitivePtr dst_prim = nullptr; | |||
| if (mode == static_cast<int64_t>(ReduceMode::Reduce_Sum)) { | |||
| dst_prim = std::make_shared<ops::ReduceSum>(); | |||
| } else if (mode == static_cast<int64_t>(ReduceMode::Reduce_Mean)) { | |||
| dst_prim = std::make_shared<ops::ReduceMean>(); | |||
| } | |||
| CHECK_NULL_RETURN(dst_prim); | |||
| dst_prim->SetAttrs(src_prim->attrs()); | |||
| value_node->set_value(dst_prim); | |||
| if (AdjustInput(cnode) != RET_OK) { | |||
| MS_LOG(ERROR) << "Adjust reduce input failed."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| return RET_OK; | |||
| } | |||
| STATUS ReduceFusionMapper::AdjustInput(const CNodePtr &cnode) { | |||
| if (cnode->size() != kNameReduceInputNum) { | |||
| MS_LOG(ERROR) << "Input size of reduce must be three, real size: " << cnode->size(); | |||
| return lite::RET_ERROR; | |||
| } | |||
| auto axes_input = cnode->input(kNameReduceInputNum - 1); | |||
| CHECK_NULL_RETURN(axes_input); | |||
| if (!utils::isa<ParameterPtr>(axes_input)) { | |||
| MS_LOG(ERROR) << "The reduce node is not parameter."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| ParameterPtr axes_param = axes_input->cast<ParameterPtr>(); | |||
| CHECK_NULL_RETURN(axes_param); | |||
| auto data = acl::GetIntParameterData(axes_param); | |||
| std::vector<int64_t> axes; | |||
| std::transform(data.begin(), data.end(), std::back_inserter(axes), | |||
| [](int32_t n) -> int64_t { return static_cast<int64_t>(n); }); | |||
| ValueNodePtr value_node = NewValueNode<std::vector<int64_t>>(axes); | |||
| CHECK_NULL_RETURN(value_node); | |||
| cnode->set_input(kNameReduceInputNum - 1, value_node); | |||
| return lite::RET_OK; | |||
| } | |||
| REGISTER_PRIMITIVE_MAPPER(kNameReduceFusion, ReduceFusionMapper) | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -0,0 +1,40 @@ | |||
| /** | |||
| * 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_REDUCE_FUSION_MAPPER_H | |||
| #define ACL_MAPPER_PRIMITIVE_REDUCE_FUSION_MAPPER_H | |||
| #include "tools/converter/adapter/acl/mapper/primitive_mapper.h" | |||
| #include "ops/fusion/reduce_fusion.h" | |||
| namespace mindspore { | |||
| namespace lite { | |||
| using mindspore::ops::kNameReduceFusion; | |||
| class ReduceFusionMapper : public PrimitiveMapper { | |||
| public: | |||
| ReduceFusionMapper() : PrimitiveMapper(kNameReduceFusion) {} | |||
| ~ReduceFusionMapper() override = default; | |||
| STATUS Mapper(const CNodePtr &cnode) override; | |||
| private: | |||
| STATUS AdjustInput(const CNodePtr &cnode); | |||
| }; | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| #endif // ACL_MAPPER_PRIMITIVE_REDUCE_FUSION_MAPPER_H | |||
| @@ -24,30 +24,13 @@ constexpr auto kNameNum = "num"; | |||
| } | |||
| STATUS StackMapper::Mapper(const CNodePtr &cnode) { | |||
| if (AddAttrForDynInputPrimitive(cnode) != RET_OK) { | |||
| if (AddAttrForDynInputPrimitive(cnode, kNameNum) != RET_OK) { | |||
| MS_LOG(ERROR) << "Stack mapper failed."; | |||
| return RET_ERROR; | |||
| } | |||
| return RET_OK; | |||
| } | |||
| STATUS StackMapper::AddAttrForDynInputPrimitive(const CNodePtr &cnode) { | |||
| MS_ASSERT(cnode != nullptr); | |||
| auto value_node = cnode->input(0)->cast<ValueNodePtr>(); | |||
| MS_ASSERT(value_node != nullptr); | |||
| auto prim = GetValueNode<PrimitivePtr>(value_node); | |||
| if (prim == nullptr) { | |||
| MS_LOG(ERROR) << "Value node is invalid."; | |||
| return lite::RET_ERROR; | |||
| } | |||
| // add attr input num for dynamic input op | |||
| int64_t num = static_cast<int64_t>(cnode->size()); | |||
| if (num > 1) { | |||
| prim->AddAttr(kNameNum, MakeValue(num - 1)); | |||
| } | |||
| return lite::RET_OK; | |||
| } | |||
| REGISTER_PRIMITIVE_MAPPER(kNameStack, StackMapper) | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -31,9 +31,6 @@ class StackMapper : public PrimitiveMapper { | |||
| ~StackMapper() override = default; | |||
| STATUS Mapper(const CNodePtr &cnode) override; | |||
| private: | |||
| STATUS AddAttrForDynInputPrimitive(const CNodePtr &cnode); | |||
| }; | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -0,0 +1,35 @@ | |||
| /** | |||
| * 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 <memory> | |||
| #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<ops::Sub>(); | |||
| 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 | |||
| @@ -0,0 +1,37 @@ | |||
| /** | |||
| * 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 | |||
| @@ -32,22 +32,16 @@ namespace acl { | |||
| }; | |||
| ADD_CONVERTER_TBE_OP(Pooling) | |||
| ADD_CONVERTER_TBE_OP(AvgPoolV2) | |||
| ADD_CONVERTER_TBE_OP(MaxPoolV3) | |||
| ADD_CONVERTER_TBE_OP(PadV3) | |||
| ADD_CONVERTER_TBE_OP(StridedSliceV2) | |||
| ADD_CONVERTER_TBE_OP(GlobalAveragePool) | |||
| ADD_CONVERTER_TBE_OP(BNInference) | |||
| ADD_CONVERTER_TBE_OP(Deconvolution) | |||
| ADD_CONVERTER_TBE_OP(Upsample) | |||
| ADD_CONVERTER_TBE_OP(Conv2DTransposeD) | |||
| } // namespace acl | |||
| } // namespace lite | |||
| } // namespace mindspore | |||
| @@ -123,7 +123,6 @@ STATUS AclOptionParamParser::ParseInputShapeVector(const std::string &input_shap | |||
| int32_t idx = 0; | |||
| std::map<int32_t, std::vector<int32_t>> input_shape_map; | |||
| for (auto &item : intput_shape_str) { | |||
| idx++; | |||
| if (item.size() < 2 || item[0] != '[' || item[item.size() - 1] != ']') { | |||
| MS_LOG(ERROR) << "Input param valid, item size: " << item.size(); | |||
| return RET_ERROR; | |||
| @@ -138,6 +137,7 @@ STATUS AclOptionParamParser::ParseInputShapeVector(const std::string &input_shap | |||
| } | |||
| } | |||
| input_shape_map[idx] = input_shape_int; | |||
| idx++; | |||
| } | |||
| acl_option_cfg->input_shape_map = input_shape_map; | |||