1 /** 2 * Copyright 2020 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 #ifndef MINDSPORE_LITE_SRC_BACKEND_ARM_INT8_RELUX_INT8_H_ 18 #define MINDSPORE_LITE_SRC_BACKEND_ARM_INT8_RELUX_INT8_H_ 19 20 #include <vector> 21 #include "src/inner_kernel.h" 22 #include "nnacl/fp32/activation_fp32.h" 23 #include "nnacl/int8/relux_int8.h" 24 25 namespace mindspore::kernel { 26 class ReluXInt8CPUKernel : public InnerKernel { 27 public: ReluXInt8CPUKernel(OpParameter * parameter,const std::vector<lite::Tensor * > & inputs,const std::vector<lite::Tensor * > & outputs,const lite::InnerContext * ctx)28 ReluXInt8CPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs, 29 const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx) 30 : InnerKernel(parameter, inputs, outputs, ctx) { 31 type_ = (reinterpret_cast<ActivationParameter *>(parameter))->type_; 32 } 33 ~ReluXInt8CPUKernel() override = default; 34 35 int Init() override; 36 int ReSize() override; 37 int Run() override; 38 int DoActivation(int task_id); 39 40 ReluXQuantArg quant_arg_ = {}; 41 42 private: 43 int type_{0}; 44 }; 45 46 class ReluInt8CPUKernel : public ReluXInt8CPUKernel { 47 public: ReluInt8CPUKernel(OpParameter * parameter,const std::vector<lite::Tensor * > & inputs,const std::vector<lite::Tensor * > & outputs,const lite::InnerContext * ctx)48 ReluInt8CPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs, 49 const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx) 50 : ReluXInt8CPUKernel(parameter, inputs, outputs, ctx) {} 51 52 ~ReluInt8CPUKernel() override = default; 53 Init()54 int Init() override { 55 auto ret = ReluXInt8CPUKernel::Init(); 56 quant_arg_.quantized_output_min = quant_arg_.output_arg.zp_; 57 quant_arg_.quantized_output_max = CHAR_MAX; 58 return ret; 59 }; 60 }; 61 62 class Relu6Int8CPUKernel : public ReluXInt8CPUKernel { 63 public: Relu6Int8CPUKernel(OpParameter * parameter,const std::vector<lite::Tensor * > & inputs,const std::vector<lite::Tensor * > & outputs,const lite::InnerContext * ctx)64 Relu6Int8CPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs, 65 const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx) 66 : ReluXInt8CPUKernel(parameter, inputs, outputs, ctx) {} 67 68 ~Relu6Int8CPUKernel() override = default; 69 Init()70 int Init() override { 71 auto ret = ReluXInt8CPUKernel::Init(); 72 quant_arg_.quantized_output_min = QuantizeToInt8(0, quant_arg_.output_arg.scale_, quant_arg_.output_arg.zp_); 73 quant_arg_.quantized_output_max = QuantizeToInt8(6, quant_arg_.output_arg.scale_, quant_arg_.output_arg.zp_); 74 return ret; 75 }; 76 }; 77 } // namespace mindspore::kernel 78 79 #endif // MINDSPORE_LITE_SRC_BACKEND_ARM_INT8_RELUX_INT8_H_ 80