• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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 "nnacl/kernel/exp.h"
18 #include <math.h>
19 #include "nnacl/exp_parameter.h"
20 #include "nnacl/op_base.h"
21 #include "nnacl/fp32/exp_fp32.h"
22 #include "nnacl/kernel/default_kernel_base.h"
23 #include "nnacl/tensor_c_utils.h"
24 #ifdef ENABLE_FP16
25 #include "nnacl/fp16/exp_fp16.h"
26 #endif
27 
ExpRunImpl(void * cdata,int task_id,float l,float r)28 int ExpRunImpl(void *cdata, int task_id, float l, float r) {
29   ExpStruct *exp = (ExpStruct *)cdata;
30   return exp->Exp(exp->base_.in_[FIRST_INPUT]->data_, exp->base_.out_[OUTPUT_INDEX]->data_, exp, task_id);
31 }
32 
ExpResize(struct KernelBase * self)33 int ExpResize(struct KernelBase *self) {
34   ExpStruct *exp = (ExpStruct *)self;
35   NNACL_CHECK_NULL_RETURN_ERR(exp);
36   ExpParameter *param = (ExpParameter *)exp->base_.param_;
37   NNACL_CHECK_NULL_RETURN_ERR(param);
38   exp->element_num_ = GetElementNum(exp->base_.in_[FIRST_INPUT]);
39   return NNACL_OK;
40 }
41 
ExpPrepare(struct KernelBase * self)42 int ExpPrepare(struct KernelBase *self) {
43   ExpStruct *exp = (ExpStruct *)self;
44   NNACL_CHECK_NULL_RETURN_ERR(exp);
45   ExpParameter *param = (ExpParameter *)exp->base_.param_;
46   NNACL_CHECK_NULL_RETURN_ERR(param);
47   NNACL_CHECK_FALSE(self->in_size_ < 1 || self->out_size_ < 1, NNACL_TENSOR_SIZE_INVALID);
48 
49   float log_base = (param->base_ == -1) ? 1 : logf(param->base_);
50   exp->in_scale_ = param->scale_ * log_base;
51   if (param->shift_ == 0) {
52     exp->out_scale_ = 1;
53   } else {
54     if (log_base == 1) {
55       exp->out_scale_ = expf(param->shift_);
56     } else {
57       exp->out_scale_ = powf(param->base_, param->shift_);
58     }
59   }
60 
61   return NNACL_OK;
62 }
63 
ExpCompute(struct KernelBase * self)64 int ExpCompute(struct KernelBase *self) {
65   return self->env_->ParallelLaunch(self->env_->thread_pool_, ExpRunImpl, self, self->thread_nr_);
66 }
67 
CreateExp(OpParameter * param,int data_type)68 KernelBase *CreateExp(OpParameter *param, int data_type) {
69   ExpStruct *exp = (ExpStruct *)malloc(sizeof(ExpStruct));
70   NNACL_MALLOC_CHECK_NULL_RETURN_NULL(exp);
71   exp->base_.Prepare = ExpPrepare;
72   exp->base_.Resize = ExpResize;
73   exp->base_.Release = DefaultRelease;
74   exp->base_.Compute = ExpCompute;
75   exp->Exp = ExpFusionFp32;
76 #ifdef ENABLE_FP16
77   if (data_type == kNumberTypeFloat16) {
78     exp->Exp = ExpFusionFp16;
79   }
80 #endif
81   return (KernelBase *)exp;
82 }
83 
84 REG_KERNEL_CREATOR(PrimType_ExpFusion, kNumberTypeFloat32, CreateExp)
85 REG_KERNEL_CREATOR(PrimType_ExpFusion, kNumberTypeFloat16, CreateExp)
86