!4129 add arm cpu op: elu

Merge pull request !4129 from 陶云浩/elu
pull/4129/MERGE
mindspore-ci-bot 5 years ago committed by Gitee
commit 76b0be5248

@ -218,6 +218,8 @@ lite::Primitive *ModelImpl::CopyPrimitive(const schema::Primitive *srcPrim) {
return new lite::QuantDTypeCast(const_cast<schema::Primitive *>(srcPrim));
case schema::PrimitiveType_EmbeddingLookup:
return new lite::EmbeddingLookup(const_cast<schema::Primitive *>(srcPrim));
case schema::PrimitiveType_Elu:
return new lite::Elu(const_cast<schema::Primitive *>(srcPrim));
default:
break;
}

@ -782,6 +782,12 @@ class Lstm : public Primitive {
int InferShape(std::vector<tensor::Tensor *> inputs, std::vector<tensor::Tensor *> outputs) override;
};
class Elu : public Primitive {
public:
explicit Elu(schema::Primitive *primitive) : Primitive(primitive) {}
const schema::Elu *GetAttribute() const { return this->primitive->value_as_Elu(); }
};
class EmbeddingLookup : public Primitive {
public:
explicit EmbeddingLookup(schema::Primitive *primitive) : Primitive(primitive) {}

@ -70,6 +70,7 @@
#include "src/runtime/kernel/arm/nnacl/int8/quant_dtype_cast.h"
#include "src/runtime/kernel/arm/nnacl/fp32/lstm.h"
#include "src/runtime/kernel/arm/nnacl/fp32/embedding_lookup.h"
#include "src/runtime/kernel/arm/nnacl/fp32/elu.h"
namespace mindspore::kernel {
OpParameter *PopulateBatchNorm(const lite::Primitive *primitive) {
@ -1239,6 +1240,18 @@ OpParameter *PopulateBiasAddParameter(const lite::Primitive *primitive) {
return reinterpret_cast<OpParameter *>(arithmetic_param);
}
OpParameter *PopulateEluParameter(const lite::Primitive *primitive) {
EluParameter *elu_parameter = new (std::nothrow) EluParameter();
if (elu_parameter == nullptr) {
MS_LOG(ERROR) << "new EluParameter failed";
return nullptr;
}
elu_parameter->op_parameter_.type_ = primitive->Type();
auto param = primitive->Value()->value_as_Elu();
elu_parameter->alpha_ = param->alpha();
return reinterpret_cast<OpParameter *>(elu_parameter);
}
PopulateParameterRegistry::PopulateParameterRegistry() {
populate_parameter_funcs_[schema::PrimitiveType_SoftMax] = PopulateSoftmaxParameter;
populate_parameter_funcs_[schema::PrimitiveType_Activation] = PopulateActivationParameter;
@ -1328,6 +1341,7 @@ PopulateParameterRegistry::PopulateParameterRegistry() {
populate_parameter_funcs_[schema::PrimitiveType_QuantDTypeCast] = PopulateQuantDTypeCastParameter;
populate_parameter_funcs_[schema::PrimitiveType_Lstm] = PopulateLstmParameter;
populate_parameter_funcs_[schema::PrimitiveType_EmbeddingLookup] = PopulateEmbeddingLookupParameter;
populate_parameter_funcs_[schema::PrimitiveType_Elu] = PopulateEluParameter;
}
PopulateParameterRegistry *PopulateParameterRegistry::GetInstance() {

@ -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…
Cancel
Save