• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/runtime/kernel/arm/int8/tanh_int8.h"
18 
19 using mindspore::lite::RET_ERROR;
20 using mindspore::lite::RET_OK;
21 
22 namespace mindspore::kernel {
Init()23 int TanhInt8CPUKernel::Init() {
24   lite::Tensor *input = in_tensors_.at(0);
25   lite::Tensor *output = out_tensors_.at(0);
26 
27   tanh_quant_.in_scale_ = input->quant_params().front().scale;
28   tanh_quant_.in_zp_ = input->quant_params().front().zeroPoint;
29   tanh_quant_.out_scale_ = output->quant_params().front().scale;
30   tanh_quant_.out_zp_ = output->quant_params().front().zeroPoint;
31 
32   if (!InferShapeDone()) {
33     return RET_OK;
34   }
35   return ReSize();
36 }
37 
ReSize()38 int TanhInt8CPUKernel::ReSize() {
39   element_size_ = in_tensors_.at(0)->ElementsNum();
40   thread_count_ = MSMIN(element_size_, op_parameter_->thread_num_);
41   thread_stride_ = UP_DIV(element_size_, thread_count_);
42   return RET_OK;
43 }
44 
DoActivation(int task_id) const45 int TanhInt8CPUKernel::DoActivation(int task_id) const {
46   int current_size = element_size_ - task_id * thread_stride_;
47   current_size = MSMIN(thread_stride_, current_size);
48   if (current_size <= 0) {
49     return RET_OK;
50   }
51 
52   int8_t *cur_input = in_ptr_ + task_id * thread_stride_;
53   int8_t *cur_output = out_ptr_ + task_id * thread_stride_;
54 
55   TanhInt8(cur_input, cur_output, current_size, &tanh_quant_);
56   return RET_OK;
57 }
58 
TanhInt8Run(void * cdata,int task_id,float lhs_scale,float rhs_scale)59 int TanhInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
60   auto activation_kernel = reinterpret_cast<TanhInt8CPUKernel *>(cdata);
61   auto error_code = activation_kernel->DoActivation(task_id);
62   if (error_code != RET_OK) {
63     MS_LOG(ERROR) << "TanhInt8Run error task_id[" << task_id << "] error_code[" << error_code << "]";
64     return RET_ERROR;
65   }
66   return RET_OK;
67 }
68 
Run()69 int TanhInt8CPUKernel::Run() {
70   in_ptr_ = reinterpret_cast<int8_t *>(in_tensors_.at(0)->data());
71   out_ptr_ = reinterpret_cast<int8_t *>(out_tensors_.at(0)->data());
72 
73   auto ret = ParallelLaunch(this->ms_context_, TanhInt8Run, this, thread_count_);
74   if (ret != RET_OK) {
75     MS_LOG(ERROR) << "TanhInt8 Run failed";
76     return ret;
77   }
78   return RET_OK;
79 }
80 }  // namespace mindspore::kernel
81