commit
76b0be5248
@ -0,0 +1,87 @@
|
|||||||
|
/**
|
||||||
|
* 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/elu.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_Elu;
|
||||||
|
|
||||||
|
namespace mindspore::kernel {
|
||||||
|
int EluCPUKernel::Init() {
|
||||||
|
elu_parameter_ = reinterpret_cast<EluParameter *>(opParameter);
|
||||||
|
elu_parameter_->thread_num_ = thread_count_;
|
||||||
|
elu_parameter_->in_size_ = inputs_.front()->ElementsNum();
|
||||||
|
return RET_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int EluCPUKernel::ReSize() { return RET_OK; }
|
||||||
|
|
||||||
|
int EluCPUKernel::DoExcute(int task_id) { Elu(input_addr, output_addr, elu_parameter_, task_id); }
|
||||||
|
|
||||||
|
int EluRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) {
|
||||||
|
auto EluData = reinterpret_cast<EluCPUKernel *>(cdata);
|
||||||
|
auto ret = EluData->DoExcute(task_id);
|
||||||
|
if (ret != RET_OK) {
|
||||||
|
MS_LOG(ERROR) << "EluRun error task_id[" << task_id << "] error_code[" << ret << "]";
|
||||||
|
return RET_ERROR;
|
||||||
|
}
|
||||||
|
return RET_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int EluCPUKernel::Run() {
|
||||||
|
input_addr = reinterpret_cast<float *>(inputs_.front()->Data());
|
||||||
|
output_addr = reinterpret_cast<float *>(outputs_.front()->Data());
|
||||||
|
|
||||||
|
auto ret = LiteBackendParallelLaunch(EluRun, this, elu_parameter_->thread_num_);
|
||||||
|
if (ret != RET_OK) {
|
||||||
|
MS_LOG(ERROR) << "Elu error: error_code[" << ret << "]";
|
||||||
|
return RET_ERROR;
|
||||||
|
}
|
||||||
|
return RET_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
kernel::LiteKernel *CpuEluFp32KernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,
|
||||||
|
const std::vector<lite::tensor::Tensor *> &outputs, OpParameter *parameter,
|
||||||
|
const lite::Context *ctx, const KernelKey &desc,
|
||||||
|
const lite::Primitive *primitive) {
|
||||||
|
if (parameter == nullptr || ctx == nullptr) {
|
||||||
|
MS_LOG(ERROR) << "parameter or ctx is nullptr";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
MS_ASSERT(desc.type == PrimitiveType_Elu);
|
||||||
|
auto *kernel = new (std::nothrow) EluCPUKernel(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_Elu, CpuEluFp32KernelCreator)
|
||||||
|
} // 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_ELU_H_
|
||||||
|
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_ELU_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "src/lite_kernel.h"
|
||||||
|
#include "src/runtime/kernel/arm/nnacl/fp32/elu.h"
|
||||||
|
|
||||||
|
namespace mindspore::kernel {
|
||||||
|
class EluCPUKernel : public LiteKernel {
|
||||||
|
public:
|
||||||
|
explicit EluCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
|
||||||
|
const std::vector<lite::tensor::Tensor *> &outputs, const lite::Context *ctx,
|
||||||
|
const lite::Primitive *primitive)
|
||||||
|
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx), thread_count_(ctx->thread_num_) {}
|
||||||
|
~EluCPUKernel() override{};
|
||||||
|
|
||||||
|
int Init() override;
|
||||||
|
int ReSize() override;
|
||||||
|
int Run() override;
|
||||||
|
int DoExcute(int task_id);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int thread_count_;
|
||||||
|
const lite::Context *ctx_;
|
||||||
|
EluParameter *elu_parameter_;
|
||||||
|
|
||||||
|
private:
|
||||||
|
float *input_addr;
|
||||||
|
float *output_addr;
|
||||||
|
};
|
||||||
|
} // namespace mindspore::kernel
|
||||||
|
|
||||||
|
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_ELU_H
|
@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* 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/nnacl/fp32/elu.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "include/errorcode.h"
|
||||||
|
#include "src/runtime/kernel/arm/nnacl/errorcode.h"
|
||||||
|
#include "mindspore/core/utils/log_adapter.h"
|
||||||
|
|
||||||
|
void Calculate_Data(float *input_data, float *output_data, int num, EluParameter *parameter) {
|
||||||
|
output_data[num] = input_data[num] < 0 ? parameter->alpha_ * expm1(input_data[num]) : input_data[num];
|
||||||
|
}
|
||||||
|
|
||||||
|
int Elu(float *input_data, float *output_data, EluParameter *parameter, int task_id) {
|
||||||
|
for (size_t i = task_id; i < parameter->in_size_; i += parameter->thread_num_) {
|
||||||
|
Calculate_Data(input_data, output_data, i, parameter);
|
||||||
|
}
|
||||||
|
return NNACL_OK;
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* 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_NNACL_FP32_ELU_H_
|
||||||
|
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_NNACL_FP32_ELU_H_
|
||||||
|
|
||||||
|
#include "src/runtime/kernel/arm/nnacl/op_base.h"
|
||||||
|
|
||||||
|
struct EluParameter {
|
||||||
|
OpParameter op_parameter_;
|
||||||
|
float alpha_;
|
||||||
|
int thread_num_;
|
||||||
|
int in_size_;
|
||||||
|
};
|
||||||
|
|
||||||
|
int Elu(float *input_data, float *output_data, EluParameter *parameter, int task_id);
|
||||||
|
|
||||||
|
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_NNACL_FP32_ELU_H_
|
@ -0,0 +1,72 @@
|
|||||||
|
/**
|
||||||
|
* 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 <iostream>
|
||||||
|
#include "src/runtime/kernel/arm/fp32/elu.h"
|
||||||
|
#include "src/runtime/kernel/arm/nnacl/fp32/elu.h"
|
||||||
|
#include "src/common/file_utils.h"
|
||||||
|
#include "common/common_test.h"
|
||||||
|
#include "utils/log_adapter.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
using mindspore::lite::tensor::Tensor;
|
||||||
|
|
||||||
|
class TestEluFp32 : public mindspore::Common {
|
||||||
|
public:
|
||||||
|
TestEluFp32() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
void EluTestInit(std::vector<Tensor *> *inputs_, std::vector<Tensor *> *outputs_, EluParameter *elu_param) {
|
||||||
|
Tensor *in_t_first = new Tensor(kNumberTypeFloat32, {6, 2}, schema::Format_NHWC, static_cast<schema::NodeType>(1));
|
||||||
|
in_t_first->MallocData();
|
||||||
|
float in_first[] = {-1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 0};
|
||||||
|
memcpy(in_t_first->Data(), in_first, sizeof(float) * in_t_first->ElementsNum());
|
||||||
|
inputs_->push_back(in_t_first);
|
||||||
|
|
||||||
|
Tensor *outputs_t = new Tensor(kNumberTypeFloat32, {6, 2}, schema::Format_NHWC, static_cast<schema::NodeType>(1));
|
||||||
|
outputs_t->MallocData();
|
||||||
|
outputs_->push_back(outputs_t);
|
||||||
|
|
||||||
|
elu_param->alpha_ = 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(TestEluFp32, EluTest) {
|
||||||
|
std::vector<Tensor *> inputs_;
|
||||||
|
std::vector<Tensor *> outputs_;
|
||||||
|
auto elu_param_ = new EluParameter();
|
||||||
|
EluTestInit(&inputs_, &outputs_, elu_param_);
|
||||||
|
|
||||||
|
lite::Context *ctx = new lite::Context;
|
||||||
|
ctx->thread_num_ = 2;
|
||||||
|
kernel::EluCPUKernel *elu =
|
||||||
|
new kernel::EluCPUKernel(reinterpret_cast<OpParameter *>(elu_param_), inputs_, outputs_, ctx, nullptr);
|
||||||
|
|
||||||
|
elu->Init();
|
||||||
|
elu->Run();
|
||||||
|
|
||||||
|
std::cout << "output shape:" << std::endl;
|
||||||
|
for (int i = 0; i < outputs_.front()->shape().size(); ++i) {
|
||||||
|
std::cout << outputs_.front()->shape()[i] << ' ';
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
float *out = reinterpret_cast<float *>(outputs_.front()->Data());
|
||||||
|
for (int i = 0; i < outputs_.front()->ElementsNum(); ++i) {
|
||||||
|
std::cout << out[i] << ' ';
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // namespace mindspore
|
Loading…
Reference in new issue