commit
9cb1ae8ab9
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "nnacl/fp32/exp.h"
|
||||
#include <math.h>
|
||||
#include "nnacl/errorcode.h"
|
||||
|
||||
int Exp(float *input_data, float *output_data, ExpParameter *parameter, int task_id) {
|
||||
if (parameter->scale_ == 1) {
|
||||
for (size_t i = task_id; i < parameter->element_num_; i += parameter->thread_num_) {
|
||||
output_data[i] = expf(input_data[i]);
|
||||
}
|
||||
} else {
|
||||
for (size_t i = task_id; i < parameter->element_num_; i += parameter->thread_num_) {
|
||||
output_data[i] = expf(input_data[i] * parameter->in_scale_);
|
||||
}
|
||||
}
|
||||
if (parameter->out_scale_ != 1) {
|
||||
for (size_t i = task_id; i < parameter->element_num_; i += parameter->thread_num_) {
|
||||
output_data[i] = output_data[i] * parameter->out_scale_;
|
||||
}
|
||||
}
|
||||
return NNACL_OK;
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_NNACL_FP32_EXP_H_
|
||||
#define MINDSPORE_LITE_NNACL_FP32_EXP_H_
|
||||
|
||||
#include "nnacl/op_base.h"
|
||||
|
||||
typedef struct ExpParameter {
|
||||
OpParameter op_parameter_;
|
||||
int thread_num_;
|
||||
float base_;
|
||||
float scale_;
|
||||
float shift_;
|
||||
float in_scale_;
|
||||
float out_scale_;
|
||||
int element_num_;
|
||||
} ExpParameter;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
int Exp(float *input_data, float *output_data, ExpParameter *parameter, int task_id);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // MINDSPORE_LITE_NNACL_FP32_EXP_H_
|
@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "src/runtime/kernel/arm/fp32/exp.h"
|
||||
#include <math.h>
|
||||
#include "include/errorcode.h"
|
||||
#include "src/kernel_registry.h"
|
||||
#include "src/runtime/runtime_api.h"
|
||||
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_Exp;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int ExpCPUKernel::Init() {
|
||||
exp_parameter_ = reinterpret_cast<ExpParameter *>(op_parameter_);
|
||||
exp_parameter_->thread_num_ = thread_count_;
|
||||
|
||||
if (!InferShapeDone()) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
return ReSize();
|
||||
}
|
||||
|
||||
int ExpCPUKernel::ReSize() {
|
||||
exp_parameter_->thread_num_ = thread_count_;
|
||||
float log_ = (exp_parameter_->base_ == -1) ? 1 : logf(exp_parameter_->base_);
|
||||
exp_parameter_->in_scale_ = exp_parameter_->scale_ * log_;
|
||||
if (exp_parameter_->shift_ == 0) {
|
||||
exp_parameter_->out_scale_ = 1;
|
||||
} else {
|
||||
if (log_ == 1) {
|
||||
exp_parameter_->out_scale_ = expf(exp_parameter_->shift_);
|
||||
} else {
|
||||
exp_parameter_->out_scale_ = powf(exp_parameter_->base_, exp_parameter_->shift_);
|
||||
}
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int ExpCPUKernel::DoExcute(int task_id) {
|
||||
Exp(input_addr_, output_addr_, exp_parameter_, task_id);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int ExpRun(void *cdata, int task_id) {
|
||||
auto ExpData = reinterpret_cast<ExpCPUKernel *>(cdata);
|
||||
auto ret = ExpData->DoExcute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "ExpRun error task_id[" << task_id << "] error_code[" << ret << "]";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int ExpCPUKernel::Run() {
|
||||
auto prepare_ret = Prepare();
|
||||
if (prepare_ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Prepare fail!ret: " << prepare_ret;
|
||||
return prepare_ret;
|
||||
}
|
||||
input_addr_ = reinterpret_cast<float *>(in_tensors_.front()->Data());
|
||||
output_addr_ = reinterpret_cast<float *>(out_tensors_.front()->Data());
|
||||
exp_parameter_->element_num_ = in_tensors_.front()->ElementsNum();
|
||||
|
||||
auto ret = ParallelLaunch(THREAD_POOL_DEFAULT, ExpRun, this, exp_parameter_->thread_num_);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Exp error: error_code[" << ret << "]";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
kernel::LiteKernel *CpuExpFp32KernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs, OpParameter *parameter,
|
||||
const lite::Context *ctx, const KernelKey &desc,
|
||||
const mindspore::lite::PrimitiveC *primitive) {
|
||||
if (parameter == nullptr || ctx == nullptr) {
|
||||
MS_LOG(ERROR) << "parameter or ctx is nullptr";
|
||||
return nullptr;
|
||||
}
|
||||
MS_ASSERT(desc.type == PrimitiveType_Exp);
|
||||
auto *kernel = new (std::nothrow) ExpCPUKernel(parameter, inputs, outputs, ctx, primitive);
|
||||
if (kernel == nullptr) {
|
||||
MS_LOG(ERROR) << "Create Kernel failed, name: " << parameter->name_;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto ret = kernel->Init();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Init Kernel failed, name: " << parameter->name_
|
||||
<< ", type: " << schema::EnumNamePrimitiveType(static_cast<schema::PrimitiveType>(parameter->type_));
|
||||
delete kernel;
|
||||
return nullptr;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_Exp, CpuExpFp32KernelCreator)
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_EXP_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_EXP_H_
|
||||
|
||||
#include <vector>
|
||||
#include "src/lite_kernel.h"
|
||||
#include "nnacl/fp32/exp.h"
|
||||
|
||||
namespace mindspore::kernel {
|
||||
class ExpCPUKernel : public LiteKernel {
|
||||
public:
|
||||
explicit ExpCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs, const lite::Context *ctx,
|
||||
const mindspore::lite::PrimitiveC *primitive)
|
||||
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx), thread_count_(ctx->thread_num_) {}
|
||||
~ExpCPUKernel() override{};
|
||||
|
||||
int Init() override;
|
||||
int ReSize() override;
|
||||
int Run() override;
|
||||
int DoExcute(int task_id);
|
||||
|
||||
protected:
|
||||
const lite::Context *ctx_;
|
||||
int thread_count_;
|
||||
ExpParameter *exp_parameter_;
|
||||
|
||||
private:
|
||||
float *input_addr_;
|
||||
float *output_addr_;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_EXP_H_
|
Loading…
Reference in new issue