!3951 add Op_BatchNorm and testcase;
Merge pull request !3951 from songhonglei413/testpull/3951/MERGE
commit
6fc6f290a2
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* 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/batchnorm.h"
|
||||
#include <cmath>
|
||||
#include "schema/model_generated.h"
|
||||
#include "src/kernel_registry.h"
|
||||
#include "include/errorcode.h"
|
||||
#include "src/runtime/runtime_api.h"
|
||||
|
||||
using mindspore::kernel::KERNEL_ARCH::kCPU;
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_BatchNorm;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int BatchnormCPUKernel::Init() { return RET_OK; }
|
||||
|
||||
int BatchnormCPUKernel::ReSize() { return RET_OK; }
|
||||
|
||||
int BatchnormCPUKernel::DoExecute(int tid) {
|
||||
int count = MSMIN(thread_unit_, units_ - tid * thread_unit_);
|
||||
if (count <= 0) {
|
||||
return RET_OK;
|
||||
}
|
||||
int offset = tid * thread_unit_ * channel_;
|
||||
BatchNorm(in_addr_ + offset, mean_addr_, var_addr_, count, channel_, batchnorm_param_->epsilon_, out_addr_ + offset);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int BatchNormRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) {
|
||||
auto g_kernel = reinterpret_cast<BatchnormCPUKernel *>(cdata);
|
||||
auto ret = g_kernel->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "BatchnormRun error task_id[" << task_id << "] error_code[" << ret << "]";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int BatchnormCPUKernel::Run() {
|
||||
in_addr_ = reinterpret_cast<float *>(inputs_.at(0)->Data());
|
||||
mean_addr_ = reinterpret_cast<float *>(inputs_.at(1)->Data());
|
||||
var_addr_ = reinterpret_cast<float *>(inputs_.at(2)->Data());
|
||||
out_addr_ = reinterpret_cast<float *>(outputs_.at(0)->Data());
|
||||
auto input_shapes = inputs_[0]->shape();
|
||||
channel_ = input_shapes[3];
|
||||
units_ = 1;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
units_ *= input_shapes[i];
|
||||
}
|
||||
thread_count_ = MSMIN(thread_count_, units_);
|
||||
thread_unit_ = UP_DIV(units_, thread_count_);
|
||||
int ret = LiteBackendParallelLaunch(BatchNormRun, this, thread_count_);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "BatchnormRun error error_code[" << ret << "]";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
kernel::LiteKernel *CpuBatchnormKernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs,
|
||||
OpParameter *opParameter, const lite::Context *ctx,
|
||||
const kernel::KernelKey &desc) {
|
||||
MS_ASSERT(opParameter != nullptr);
|
||||
MS_ASSERT(desc.type == schema::PrimitiveType_BatchNorm);
|
||||
auto *kernel = new (std::nothrow) BatchnormCPUKernel(opParameter, inputs, outputs, ctx);
|
||||
if (kernel == nullptr) {
|
||||
MS_LOG(ERROR) << "new BatchNormCPUKernel fail!";
|
||||
return nullptr;
|
||||
}
|
||||
auto ret = kernel->Init();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: "
|
||||
<< schema::EnumNamePrimitiveType(static_cast<schema::PrimitiveType>(opParameter->type_));
|
||||
delete kernel;
|
||||
return nullptr;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_BatchNorm, CpuBatchnormKernelCreator)
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 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_BATCHNORM_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_BATCHNORM_H_
|
||||
|
||||
#include <vector>
|
||||
#include "src/lite_kernel.h"
|
||||
#include "include/context.h"
|
||||
#include "src/runtime/kernel/arm/opclib/fp32/batchnorm.h"
|
||||
|
||||
using mindspore::lite::Context;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
class BatchnormCPUKernel : public LiteKernel {
|
||||
public:
|
||||
BatchnormCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs, const Context *ctx)
|
||||
: LiteKernel(parameter, inputs, outputs), ctx_(ctx), thread_count_(ctx->thread_num_) {
|
||||
batchnorm_param_ = reinterpret_cast<BatchNormParameter *>(parameter);
|
||||
}
|
||||
~BatchnormCPUKernel() override { delete batchnorm_param_; }
|
||||
|
||||
int Init() override;
|
||||
int ReSize() override;
|
||||
int Run() override;
|
||||
int DoExecute(int tid);
|
||||
|
||||
private:
|
||||
int thread_count_;
|
||||
int thread_unit_;
|
||||
int units_;
|
||||
int channel_;
|
||||
float *in_addr_;
|
||||
float *mean_addr_;
|
||||
float *var_addr_;
|
||||
float *out_addr_;
|
||||
const Context *ctx_;
|
||||
BatchNormParameter *batchnorm_param_;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_BATCHNORM_H_
|
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 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/opclib/fp32/batchnorm.h"
|
||||
|
||||
void BatchNorm(const float *input_ptr, const float *mean_ptr, const float *variance_ptr, int units, int channel,
|
||||
float epsilon, float *output_ptr) {
|
||||
for (int u = 0; u < units; u++) {
|
||||
for (int c = 0; c < channel; c++) {
|
||||
auto variance_sqrt = sqrt(variance_ptr[c] + epsilon);
|
||||
output_ptr[u * channel + c] = (input_ptr[u * channel + c] - mean_ptr[c]) / variance_sqrt;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 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_OPCLIB_FP32_BATCHNORM_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_FP32_BATCHNORM_H_
|
||||
|
||||
#include "src/runtime/kernel/arm/opclib/op_base.h"
|
||||
|
||||
struct BatchNormParameter {
|
||||
OpParameter op_parameter_;
|
||||
float epsilon_;
|
||||
};
|
||||
|
||||
void BatchNorm(const float *input_ptr, const float *mean_ptr, const float *variance_ptr, int count, int channel,
|
||||
float epsilon, float *output_ptr);
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_FUSED_BATCHNORM_H_
|
@ -0,0 +1,100 @@
|
||||
/**
|
||||
* 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 "mindspore/core/utils/log_adapter.h"
|
||||
#include "common/common_test.h"
|
||||
#include "mindspore/lite/src/runtime/kernel/arm/opclib/fp32/batchnorm.h"
|
||||
#include "mindspore/lite/src/runtime/kernel/arm/opclib/fused_batchnorm.h"
|
||||
#include "mindspore/lite/src/kernel_registry.h"
|
||||
#include "mindspore/lite/src/lite_kernel.h"
|
||||
#include "mindspore/lite/src/common/file_utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
class TestBatchnormFp32 : public mindspore::Common {
|
||||
public:
|
||||
TestBatchnormFp32() {}
|
||||
};
|
||||
|
||||
TEST_F(TestBatchnormFp32, BNTest) {
|
||||
std::vector<float> in_data = {0.0669681, 0.959215, 0.252686, 0.613594, 0.811776, 0.139469, 0.322848, 0.118354,
|
||||
0.082978, 0.399467, 0.961267, 0.0247456, 0.0714259, 0.0791484, 0.0648625, 0.561612,
|
||||
0.412069, 0.311492, 0.46109, 0.377125, 0.369283, 0.0332446, 0.696142, 0.715973,
|
||||
0.525524, 0.477265, 0.0336351, 0.751577, 0.377548, 0.964603, 0.0196834, 0.174865};
|
||||
std::vector<float> in_data1 = {0.855446, 0.821765, 0.281008, 0.0798653, 0.22294, 0.793782, 0.963222, 0.17851,
|
||||
0.667549, 0.274381, 0.592842, 0.216552, 0.190274, 0.237873, 0.610063, 0.307559,
|
||||
0.830007, 0.760957, 0.583265, 0.763793, 0.456372, 0.391378, 0.547915, 0.862198,
|
||||
0.510794, 0.826776, 0.515894, 0.30071, 0.404987, 0.184773};
|
||||
std::vector<float> in_data2 = {0.712438, 0.4927, 0.078419, 0.310429, 0.546871, 0.0667141, 0.874321, 0.0265647,
|
||||
0.685165, 0.732586, 0.952889, 0.506402, 0.540784, 0.131119, 0.357713, 0.678992,
|
||||
0.960839, 0.340706, 0.697678, 0.398146, 0.313321, 0.6485, 0.739153, 0.00190134,
|
||||
0.536842, 0.996873, 0.445276, 0.371212, 0.420397, 0.0930115};
|
||||
std::vector<float> in_data3(32, 1);
|
||||
std::vector<float> in_data4(32, 0);
|
||||
std::vector<lite::tensor::Tensor *> inputs_tensor;
|
||||
std::vector<lite::tensor::Tensor *> outputs_tensor;
|
||||
|
||||
BatchNormParameter op_param;
|
||||
op_param.op_parameter_.type_ = schema::PrimitiveType_BatchNorm;
|
||||
op_param.epsilon_ = 0.001f;
|
||||
|
||||
std::vector<int> in_shape = {1, 2, 4, 4};
|
||||
|
||||
lite::tensor::Tensor input0_tensor;
|
||||
lite::tensor::Tensor input1_tensor;
|
||||
lite::tensor::Tensor input2_tensor;
|
||||
inputs_tensor.push_back(&input0_tensor);
|
||||
inputs_tensor.push_back(&input1_tensor);
|
||||
inputs_tensor.push_back(&input2_tensor);
|
||||
input0_tensor.SetData(in_data.data());
|
||||
input1_tensor.SetData(in_data1.data());
|
||||
input2_tensor.SetData(in_data2.data());
|
||||
input0_tensor.set_shape(in_shape);
|
||||
|
||||
std::vector<float> output(32);
|
||||
std::vector<float> corr_out(32);
|
||||
std::vector<int> output_shape = {1, 2, 4, 4};
|
||||
|
||||
lite::tensor::Tensor output0_tensor;
|
||||
outputs_tensor.push_back(&output0_tensor);
|
||||
output0_tensor.SetData(output.data());
|
||||
kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_BatchNorm};
|
||||
auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc);
|
||||
ASSERT_NE(creator, nullptr);
|
||||
lite::Context ctx;
|
||||
ctx.thread_num_ = 7;
|
||||
kernel::LiteKernel *kernel =
|
||||
creator(inputs_tensor, outputs_tensor, reinterpret_cast<OpParameter *>(&op_param), &ctx, desc);
|
||||
ASSERT_NE(kernel, nullptr);
|
||||
auto output_tensor_shape = output0_tensor.shape();
|
||||
kernel->Run();
|
||||
|
||||
FusedBatchNorm(in_data.data(), in_data3.data(), in_data4.data(), in_data1.data(), in_data2.data(), in_shape.data(),
|
||||
0.001f, corr_out.data());
|
||||
|
||||
printf("==================output data=================\n");
|
||||
for (int i = 0; i < 1 * 28; i++) {
|
||||
std::cout << output[i] << " ,";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
CompareOutputData(output.data(), corr_out.data(), 32, 0.00001);
|
||||
|
||||
input0_tensor.SetData(nullptr);
|
||||
input1_tensor.SetData(nullptr);
|
||||
input2_tensor.SetData(nullptr);
|
||||
output0_tensor.SetData(nullptr);
|
||||
}
|
||||
} // namespace mindspore
|
Loading…
Reference in new issue