1 /**
2 * Copyright 2020-2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/delegate/npu/op/convolution_depthwise_npu.h"
18 #include "src/delegate/npu/npu_converter_utils.h"
19 namespace mindspore {
SetConvDwParam(const schema::Conv2DFusion * conv_prim)20 int ConvolutionDepthwiseNPUOp::SetConvDwParam(const schema::Conv2DFusion *conv_prim) {
21 auto stride_h = static_cast<int>(*(conv_prim->stride()->begin()));
22 auto stride_w = static_cast<int>(*(conv_prim->stride()->begin() + 1));
23 auto dilation_h = static_cast<int>(*(conv_prim->dilation()->begin()));
24 auto dilation_w = static_cast<int>(*(conv_prim->dilation()->begin() + 1));
25 conv_dw_->set_attr_strides(ge::AttrValue::LIST_INT({stride_h, stride_w}));
26 conv_dw_->set_attr_dilations(ge::AttrValue::LIST_INT({dilation_h, dilation_w}));
27
28 if (conv_prim->pad_mode() == schema::PadMode_SAME) {
29 conv_dw_->set_attr_pad_mode(ge::AttrValue::STR{"SAME"});
30 conv_dw_->set_attr_pads(ge::AttrValue::LIST_INT({0, 0, 0, 0}));
31 } else if (conv_prim->pad_mode() == schema::PadMode_VALID) {
32 conv_dw_->set_attr_pad_mode(ge::AttrValue::STR{"VALID"});
33 conv_dw_->set_attr_pads(ge::AttrValue::LIST_INT({0, 0, 0, 0}));
34 } else {
35 conv_dw_->set_attr_pad_mode(ge::AttrValue::STR{"VALID"});
36 auto pad_u = static_cast<int>(*(conv_prim->pad_list()->begin() + PAD_UP));
37 auto pad_d = static_cast<int>(*(conv_prim->pad_list()->begin() + PAD_DOWN));
38 auto pad_l = static_cast<int>(*(conv_prim->pad_list()->begin() + PAD_LEFT));
39 auto pad_r = static_cast<int>(*(conv_prim->pad_list()->begin() + PAD_RIGHT));
40 conv_dw_->set_attr_pads(ge::AttrValue::LIST_INT({pad_u, pad_d, pad_l, pad_r}));
41 }
42 return RET_OK;
43 }
44
Init(const schema::Primitive * primitive,const std::vector<mindspore::MSTensor> & in_tensors,const std::vector<mindspore::MSTensor> & out_tensors)45 int ConvolutionDepthwiseNPUOp::Init(const schema::Primitive *primitive,
46 const std::vector<mindspore::MSTensor> &in_tensors,
47 const std::vector<mindspore::MSTensor> &out_tensors) {
48 conv_dw_ = new (std::nothrow) hiai::op::ConvolutionDepthwise(name_ + "_conv_depthwise");
49 if (conv_dw_ == nullptr) {
50 MS_LOG(ERROR) << "New convolution depthwise operator for op " << name_ << " failed.";
51 return RET_ERROR;
52 }
53 auto conv_prim = primitive->value_as_Conv2DFusion();
54 if (conv_prim == nullptr) {
55 MS_LOG(ERROR) << "Get null primitive value for op ." << name_;
56 return RET_ERROR;
57 }
58 auto ret = SetConvDwParam(conv_prim);
59 if (ret != RET_OK) {
60 MS_LOG(ERROR) << "Set npu op parameter for convolution depthwise op " << name_ << " failed.";
61 return RET_ERROR;
62 }
63 act_type_ = conv_prim->activation_type();
64 if (act_type_ != schema::ActivationType_NO_ACTIVATION) {
65 ret = SetActivation(conv_dw_, conv_prim->activation_type());
66 if (ret != RET_OK) {
67 MS_LOG(ERROR) << "New activation npu operator for op " << name_ << " failed.";
68 return RET_ERROR;
69 }
70 }
71 return RET_OK;
72 }
73
SetNPUInputs(const std::vector<mindspore::MSTensor> & in_tensors,const std::vector<mindspore::MSTensor> & out_tensors,const std::vector<ge::Operator * > & npu_inputs)74 int ConvolutionDepthwiseNPUOp::SetNPUInputs(const std::vector<mindspore::MSTensor> &in_tensors,
75 const std::vector<mindspore::MSTensor> &out_tensors,
76 const std::vector<ge::Operator *> &npu_inputs) {
77 auto ret = InitWeightConst(in_tensors);
78 if (ret != RET_OK) {
79 MS_LOG(ERROR) << "Set weight and bias for convolution depthwise op " << name_ << " failed when running npu";
80 return RET_ERROR;
81 }
82 conv_dw_->set_input_filter(*weight_);
83
84 if (in_tensors.size() == CONV_INPUT_SIZE) {
85 ret = InitBiasConst(in_tensors);
86 if (ret != RET_OK) {
87 MS_LOG(ERROR) << "Set bias for convolution depthwise op " << name_ << " failed when running npu";
88 return RET_ERROR;
89 }
90 conv_dw_->set_input_bias(*bias_);
91 }
92 conv_dw_->set_input_x(*npu_inputs[0]);
93 return RET_OK;
94 }
95
GetNPUOp()96 ge::Operator *ConvolutionDepthwiseNPUOp::GetNPUOp() {
97 if (act_type_ == schema::ActivationType_NO_ACTIVATION) {
98 return conv_dw_;
99 } else {
100 return act_;
101 }
102 }
103
~ConvolutionDepthwiseNPUOp()104 ConvolutionDepthwiseNPUOp::~ConvolutionDepthwiseNPUOp() {
105 if (conv_dw_ != nullptr) {
106 delete conv_dw_;
107 conv_dw_ = nullptr;
108 }
109 }
110 } // namespace mindspore
111