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 #ifndef MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_NPU_OP_CONVOLUTION_BASE_NPU_H_ 17 #define MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_NPU_OP_CONVOLUTION_BASE_NPU_H_ 18 19 #include <utility> 20 #include <vector> 21 #include <memory> 22 #include <string> 23 #include "include/graph/op/all_ops.h" 24 #include "src/litert/delegate/npu/op/npu_op.h" 25 namespace mindspore::lite { 26 constexpr int WEIGHT_INDEX = 1; 27 constexpr int BIAS_INDEX = 2; 28 constexpr int CONV_INPUT_SIZE = 3; 29 30 class ConvolutionBaseNPUOp : public NPUOp { 31 public: ConvolutionBaseNPUOp(const schema::Primitive * primitive,const std::vector<mindspore::MSTensor> & in_tensors,const std::vector<mindspore::MSTensor> & out_tensors,std::string name)32 ConvolutionBaseNPUOp(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors, 33 const std::vector<mindspore::MSTensor> &out_tensors, std::string name) 34 : NPUOp(primitive, in_tensors, out_tensors, std::move(name)) {} 35 36 ~ConvolutionBaseNPUOp() override; 37 38 protected: 39 template <typename T> SetQuantParam(T * conv_,const std::vector<mindspore::MSTensor> & in_tensors)40 void SetQuantParam(T *conv_, const std::vector<mindspore::MSTensor> &in_tensors) { 41 conv_->set_attr_x_quant_scale(in_tensors.at(0).QuantParams().front().scale); 42 conv_->set_attr_x_quant_offset(in_tensors.at(0).QuantParams().front().zero_point); 43 conv_->set_attr_x_quant_type(1); 44 45 std::vector<float> filter_scales(in_tensors.at(WEIGHT_INDEX).QuantParams().size()); 46 for (size_t i = 0; i < in_tensors.at(WEIGHT_INDEX).QuantParams().size(); i++) { 47 filter_scales[i] = in_tensors.at(WEIGHT_INDEX).QuantParams().at(i).scale; 48 } 49 conv_->set_attr_filter_quant_scales(filter_scales); 50 conv_->set_attr_filter_quant_type(1); 51 } 52 int InitWeightConst(const std::vector<mindspore::MSTensor> &inputs); 53 int InitBiasConst(const std::vector<mindspore::MSTensor> &inputs); 54 int SetActivation(const ge::Operator *input, schema::ActivationType act_type); 55 void FreeTmpWeight(); 56 hiai::op::Activation *act_ = nullptr; 57 hiai::op::Const *weight_ = nullptr; 58 hiai::op::Const *bias_ = nullptr; 59 float *fp32_weight_ = nullptr; 60 void *nchw_weight_ = nullptr; 61 }; 62 } // namespace mindspore::lite 63 #endif // MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_NPU_OP_CONVOLUTION_BASE_NPU_H_ 64