提交 af66ec41 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!5053 [MS][LITE][Develop] opencl activation function support 2d data

Merge pull request !5053 from liuzhongkai/2d_data
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
#include <vector> #include <vector>
#include <map>
#include <string> #include <string>
#include <set> #include <set>
...@@ -23,7 +24,6 @@ ...@@ -23,7 +24,6 @@
#include "src/kernel_registry.h" #include "src/kernel_registry.h"
#include "src/runtime/runtime_api.h" #include "src/runtime/runtime_api.h"
#include "include/errorcode.h" #include "include/errorcode.h"
#include "src/runtime/kernel/opencl/cl/activation.cl.inc" #include "src/runtime/kernel/opencl/cl/activation.cl.inc"
using mindspore::kernel::KERNEL_ARCH::kGPU; using mindspore::kernel::KERNEL_ARCH::kGPU;
...@@ -39,61 +39,58 @@ using mindspore::schema::PrimitiveType_Activation; ...@@ -39,61 +39,58 @@ using mindspore::schema::PrimitiveType_Activation;
namespace mindspore::kernel { namespace mindspore::kernel {
int ActivationOpenClKernel::Init() { int ActivationOpenClKernel::Init() {
const int max_shape_dim = 4; in_size_ = in_tensors_[0]->shape().size();
if (in_tensors_[0]->shape().size() != max_shape_dim) { out_size_ = out_tensors_[0]->shape().size();
MS_LOG(ERROR) << "Activate fun only support dim=4, but your dim=" << in_tensors_[0]->shape().size(); if (in_size_ != 2 && in_size_ != 4) {
MS_LOG(ERROR) << "Activate fun only support dim=4 or 2, but your dim=" << in_size_;
return RET_ERROR; return RET_ERROR;
} }
std::string program_name = ""; std::map<int, std::vector<std::string>> Program_Kernel{
std::string kernel_name = ""; {ActivationType_LEAKY_RELU, std::vector<std::string>{"LEAKY_RELU", "ReluScalar"}},
std::string source = activation_source; {ActivationType_RELU, std::vector<std::string>{"RELU", "Relu"}},
if (type_ == ActivationType_RELU) { {ActivationType_SIGMOID, std::vector<std::string>{"SIGMOID", "Sigmoid"}},
program_name = "RELU"; {ActivationType_RELU6, std::vector<std::string>{"RELU6", "Relu6"}}};
kernel_name = "Relu"; if (Program_Kernel.count(type_) == 0) {
} else if (type_ == ActivationType_RELU6) { MS_LOG(ERROR) << "schema::ActivationType:" << type_ << "not found";
program_name = "RELU6";
kernel_name = "Relu6";
} else if (type_ == ActivationType_LEAKY_RELU) {
program_name = "LEAKY_RELU";
kernel_name = "ReluScalar";
} else if (type_ == ActivationType_SIGMOID) {
program_name = "SIGMOID";
kernel_name = "Sigmoid";
} else {
MS_LOG(ERROR) << "Activation type error";
return RET_ERROR; return RET_ERROR;
} }
std::string source = activation_source;
std::set<std::string> build_options; std::set<std::string> build_options;
auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance(); auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance();
ocl_runtime->LoadSource(program_name, source); ocl_runtime->LoadSource(Program_Kernel[type_][0], source);
ocl_runtime->BuildKernel(kernel_, program_name, kernel_name, build_options); ocl_runtime->BuildKernel(kernel_, Program_Kernel[type_][0], Program_Kernel[type_][1], build_options);
std::map<int, schema::Format> format{{4, schema::Format_NHWC4}, {2, schema::Format_NC4}};
if (format.count(out_size_) == 0) {
MS_LOG(ERROR) << "Not found output tensor format";
return RET_ERROR;
}
in_ori_format_ = in_tensors_[0]->GetFormat(); in_ori_format_ = in_tensors_[0]->GetFormat();
in_tensors_[0]->SetFormat(schema::Format_NHWC4);
out_ori_format_ = out_tensors_[0]->GetFormat(); out_ori_format_ = out_tensors_[0]->GetFormat();
out_tensors_[0]->SetFormat(schema::Format_NHWC4); in_tensors_[0]->SetFormat(format[in_size_]);
out_tensors_[0]->SetFormat(format[out_size_]);
if (in_size_ == 2) {
in_ori_format_ = schema::Format_NC4;
out_ori_format_ = schema::Format_NC4;
}
MS_LOG(DEBUG) << op_parameter_->name_ << " init Done!"; MS_LOG(DEBUG) << op_parameter_->name_ << " init Done!";
return RET_OK; return RET_OK;
} }
int ActivationOpenClKernel::Run() { int ActivationOpenClKernel::Run() {
MS_LOG(DEBUG) << op_parameter_->name_ << " begin running!"; MS_LOG(DEBUG) << op_parameter_->name_ << " begin running!";
int N = in_tensors_[0]->shape()[0]; cl_int4 img2d_shape = GetImg2dShape();
int H = in_tensors_[0]->shape()[1];
int W = in_tensors_[0]->shape()[2];
int C = in_tensors_[0]->shape()[3];
cl_int4 input_shape = {N, H, W, C};
auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance(); auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance();
int arg_idx = 0; int arg_idx = 0;
ocl_runtime->SetKernelArg(kernel_, arg_idx++, in_tensors_[0]->Data()); ocl_runtime->SetKernelArg(kernel_, arg_idx++, in_tensors_[0]->Data());
ocl_runtime->SetKernelArg(kernel_, arg_idx++, out_tensors_[0]->Data()); ocl_runtime->SetKernelArg(kernel_, arg_idx++, out_tensors_[0]->Data());
ocl_runtime->SetKernelArg(kernel_, arg_idx++, input_shape); ocl_runtime->SetKernelArg(kernel_, arg_idx++, img2d_shape);
if (type_ == ActivationType_LEAKY_RELU) { if (type_ == ActivationType_LEAKY_RELU) {
ocl_runtime->SetKernelArg(kernel_, arg_idx++, alpha_); ocl_runtime->SetKernelArg(kernel_, arg_idx++, alpha_);
} }
std::vector<size_t> local = {1, 1}; std::vector<size_t> local = {1, 1};
std::vector<size_t> global = {static_cast<size_t>(H), static_cast<size_t>(W)}; std::vector<size_t> global = {static_cast<size_t>(img2d_shape.s[1]), static_cast<size_t>(img2d_shape.s[2])};
std::cout << type_ << " " << std::endl;
auto ret = ocl_runtime->RunKernel(kernel_, global, local, nullptr); auto ret = ocl_runtime->RunKernel(kernel_, global, local, nullptr);
if (ret != RET_OK) { if (ret != RET_OK) {
MS_LOG(ERROR) << "Run kernel:" << op_parameter_->name_ << " fail."; MS_LOG(ERROR) << "Run kernel:" << op_parameter_->name_ << " fail.";
...@@ -102,11 +99,21 @@ int ActivationOpenClKernel::Run() { ...@@ -102,11 +99,21 @@ int ActivationOpenClKernel::Run() {
return RET_OK; return RET_OK;
} }
int ActivationOpenClKernel::GetImageSize(size_t idx, std::vector<size_t> *img_size) { cl_int4 ActivationOpenClKernel::GetImg2dShape() {
int H = in_tensors_[0]->shape()[1]; cl_int4 img2d_shape = {0, 0, 0, 0};
int W = in_tensors_[0]->shape()[2]; for (int i = 0; i < in_size_; ++i) {
int C = in_tensors_[0]->shape()[3]; img2d_shape.s[i + 4 - in_size_] = in_tensors_[0]->shape()[i];
}
if (in_size_ == 2) {
img2d_shape.s[1] = img2d_shape.s[2];
img2d_shape.s[2] = UP_DIV(img2d_shape.s[3], C4NUM);
img2d_shape.s[3] = C4NUM;
}
return img2d_shape;
}
int ActivationOpenClKernel::GetImageSize(size_t idx, std::vector<size_t> *img_size) {
cl_int4 img_shape = GetImg2dShape();
#ifdef ENABLE_FP16 #ifdef ENABLE_FP16
size_t img_dtype = CL_HALF_FLOAT; size_t img_dtype = CL_HALF_FLOAT;
#else #else
...@@ -114,8 +121,8 @@ int ActivationOpenClKernel::GetImageSize(size_t idx, std::vector<size_t> *img_si ...@@ -114,8 +121,8 @@ int ActivationOpenClKernel::GetImageSize(size_t idx, std::vector<size_t> *img_si
#endif #endif
img_size->clear(); img_size->clear();
img_size->push_back(W * UP_DIV(C, C4NUM)); img_size->push_back(img_shape.s[2] * UP_DIV(img_shape.s[3], C4NUM));
img_size->push_back(H); img_size->push_back(img_shape.s[1]);
img_size->push_back(img_dtype); img_size->push_back(img_dtype);
return RET_OK; return RET_OK;
} }
...@@ -125,11 +132,11 @@ kernel::LiteKernel *OpenClActivationFp32KernelCreator(const std::vector<lite::te ...@@ -125,11 +132,11 @@ kernel::LiteKernel *OpenClActivationFp32KernelCreator(const std::vector<lite::te
OpParameter *opParameter, const lite::Context *ctx, OpParameter *opParameter, const lite::Context *ctx,
const kernel::KernelKey &desc, const kernel::KernelKey &desc,
const mindspore::lite::PrimitiveC *primitive) { const mindspore::lite::PrimitiveC *primitive) {
if (inputs.size() == 0) { if (inputs.empty()) {
MS_LOG(ERROR) << "Input data size must be greater than 0, but your size is " << inputs.size(); MS_LOG(ERROR) << "Input data size must be greater than 0, but your size is " << inputs.size();
return nullptr; return nullptr;
} }
if (inputs[0]->shape()[0] > 1) { if (inputs[0]->shape().size() > 2 && inputs[0]->shape()[0] > 1) {
MS_LOG(ERROR) << "Activation kernel:" << opParameter->name_ << " failed: Unsupported multi-batch."; MS_LOG(ERROR) << "Activation kernel:" << opParameter->name_ << " failed: Unsupported multi-batch.";
return nullptr; return nullptr;
} }
......
...@@ -38,11 +38,14 @@ class ActivationOpenClKernel : public OpenCLKernel { ...@@ -38,11 +38,14 @@ class ActivationOpenClKernel : public OpenCLKernel {
int Init() override; int Init() override;
int Run() override; int Run() override;
int GetImageSize(size_t idx, std::vector<size_t> *img_size) override; int GetImageSize(size_t idx, std::vector<size_t> *img_size) override;
cl_int4 GetImg2dShape();
private: private:
cl::Kernel kernel_; cl::Kernel kernel_;
int type_; int type_;
float alpha_; float alpha_;
int in_size_;
int out_size_;
}; };
} // namespace mindspore::kernel } // namespace mindspore::kernel
......
...@@ -73,91 +73,230 @@ void printf_tensor(mindspore::lite::tensor::Tensor *in_data) { ...@@ -73,91 +73,230 @@ void printf_tensor(mindspore::lite::tensor::Tensor *in_data) {
MS_LOG(INFO) << "Print tensor done"; MS_LOG(INFO) << "Print tensor done";
} }
kernel::ActivationOpenClKernel *create_kernel(lite::opencl::OpenCLAllocator *allocator, TEST_F(TestActivationOpenCL, ReluFp32_dim4) {
const std::vector<lite::tensor::Tensor *> &inputs, std::string in_file = "/data/local/tmp/in_data.bin";
const std::vector<lite::tensor::Tensor *> &outputs, std::string test_name, std::string out_file = "/data/local/tmp/relu.bin";
int type, std::string in_file, float alpha = 0.2) { MS_LOG(INFO) << "Relu Begin test!";
auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance();
ocl_runtime->Init();
auto allocator = ocl_runtime->GetAllocator();
MS_LOG(INFO) << "Init tensors.";
std::vector<int> input_shape = {1, 9};
auto data_type = kNumberTypeFloat32;
auto tensor_type = schema::NodeType_ValueNode;
auto *input_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
if (input_tensor == nullptr) {
MS_LOG(ERROR) << "new input tensor error!";
return;
}
auto *output_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
if (output_tensor == nullptr) {
MS_LOG(ERROR) << "new output tensor error!";
delete input_tensor;
return;
}
std::vector<lite::tensor::Tensor *> inputs{input_tensor};
std::vector<lite::tensor::Tensor *> outputs{output_tensor};
inputs[0]->MallocData(allocator);
MS_LOG(INFO) << "Initialize input data";
LoadActivationData(inputs[0]->Data(), inputs[0]->Size(), in_file);
MS_LOG(INFO) << "==================input data================";
printf_tensor(inputs[0]);
auto *param = new (std::nothrow) ActivationParameter(); auto *param = new (std::nothrow) ActivationParameter();
if (param == nullptr) { if (param == nullptr) {
MS_LOG(ERROR) << "New ActivationParameter fail."; MS_LOG(ERROR) << "New ActivationParameter fail.";
return nullptr; delete input_tensor;
delete output_tensor;
return;
} }
memcpy(param->op_parameter_.name_, test_name.c_str(), test_name.size()); param->type_ = ActivationType_RELU;
param->alpha_ = alpha;
param->type_ = type;
auto *kernel = auto *kernel =
new (std::nothrow) kernel::ActivationOpenClKernel(reinterpret_cast<OpParameter *>(param), inputs, outputs); new (std::nothrow) kernel::ActivationOpenClKernel(reinterpret_cast<OpParameter *>(param), inputs, outputs);
if (kernel == nullptr) { if (kernel == nullptr) {
MS_LOG(ERROR) << "Kernel:Relu create fail.";
delete param; delete param;
MS_LOG(ERROR) << "Kernel:" << test_name << " create fail."; delete input_tensor;
delete param; delete output_tensor;
return nullptr; return;
} }
auto ret = kernel->Init(); auto ret = kernel->Init();
if (ret != RET_OK) { if (ret != RET_OK) {
delete param; delete param;
delete kernel; delete kernel;
MS_LOG(ERROR) << "Init " << test_name << " fail."; delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Init relu fail.";
return;
}
MS_LOG(INFO) << "Create kernel SubGraphOpenCLKernel.";
std::vector<kernel::LiteKernel *> kernels{kernel};
auto *sub_graph = new (std::nothrow) kernel::SubGraphOpenCLKernel(inputs, outputs, kernels, kernels, kernels);
if (sub_graph == nullptr) {
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Kernel SubGraphOpenCLKernel create fail.";
return;
}
MS_LOG(INFO) << "Initialize sub_graph.";
ret = sub_graph->Init();
if (ret != RET_OK) {
MS_LOG(ERROR) << "Init sub_graph error.";
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph;
return;
}
MS_LOG(INFO) << "Run SubGraphOpenCLKernel.";
ret = sub_graph->Run();
if (ret != RET_OK) {
delete kernel; delete kernel;
delete param; delete param;
return nullptr; delete input_tensor;
delete output_tensor;
delete sub_graph;
MS_LOG(ERROR) << "Run SubGraphOpenCLKernel error.";
return;
}
MS_LOG(INFO) << "==================output data================";
printf_tensor(outputs[0]);
CompareRes(output_tensor, out_file);
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph;
lite::opencl::OpenCLRuntime::DeleteInstance();
}
TEST_F(TestActivationOpenCL, Relu6Fp32_dim4) {
std::string in_file = "/data/local/tmp/in_data.bin";
std::string out_file = "/data/local/tmp/relu6.bin";
MS_LOG(INFO) << "Relu6 Begin test!";
auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance();
ocl_runtime->Init();
auto allocator = ocl_runtime->GetAllocator();
MS_LOG(INFO) << "Init tensors.";
std::vector<int> input_shape = {1, 9};
auto data_type = kNumberTypeFloat32;
auto tensor_type = schema::NodeType_ValueNode;
auto *input_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
if (input_tensor == nullptr) {
MS_LOG(ERROR) << "new input tensor error!";
return;
} }
auto *output_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
if (output_tensor == nullptr) {
MS_LOG(ERROR) << "new output tensor error!";
delete input_tensor;
return;
}
std::vector<lite::tensor::Tensor *> inputs{input_tensor};
std::vector<lite::tensor::Tensor *> outputs{output_tensor};
inputs[0]->MallocData(allocator);
MS_LOG(INFO) << "Initialize input data"; MS_LOG(INFO) << "Initialize input data";
LoadActivationData(inputs[0]->Data(), inputs[0]->Size(), in_file); LoadActivationData(inputs[0]->Data(), inputs[0]->Size(), in_file);
MS_LOG(INFO) << "==================input data================"; MS_LOG(INFO) << "==================input data================";
printf_tensor(inputs[0]); printf_tensor(inputs[0]);
return kernel;
}
int RunSubGraphOpenCLKernel(const std::vector<lite::tensor::Tensor *> &inputs, auto *param = new (std::nothrow) ActivationParameter();
const std::vector<lite::tensor::Tensor *> &outputs, if (param == nullptr) {
kernel::ActivationOpenClKernel *kernel) { MS_LOG(ERROR) << "New ActivationParameter fail.";
delete input_tensor;
delete output_tensor;
return;
}
param->type_ = ActivationType_RELU6;
auto *kernel =
new (std::nothrow) kernel::ActivationOpenClKernel(reinterpret_cast<OpParameter *>(param), inputs, outputs);
if (kernel == nullptr) {
MS_LOG(ERROR) << "Kernel:Relu6 create fail.";
delete param;
delete input_tensor;
delete output_tensor;
return;
}
auto ret = kernel->Init();
if (ret != RET_OK) {
delete param;
delete kernel;
delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Init relu6 fail.";
return;
}
MS_LOG(INFO) << "Create kernel SubGraphOpenCLKernel."; MS_LOG(INFO) << "Create kernel SubGraphOpenCLKernel.";
std::vector<kernel::LiteKernel *> kernels{kernel}; std::vector<kernel::LiteKernel *> kernels{kernel};
auto *sub_graph = new (std::nothrow) kernel::SubGraphOpenCLKernel(inputs, outputs, kernels, kernels, kernels); auto *sub_graph = new (std::nothrow) kernel::SubGraphOpenCLKernel(inputs, outputs, kernels, kernels, kernels);
if (sub_graph == nullptr) { if (sub_graph == nullptr) {
delete kernel; delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Kernel SubGraphOpenCLKernel create fail."; MS_LOG(ERROR) << "Kernel SubGraphOpenCLKernel create fail.";
return RET_ERROR; return;
} }
MS_LOG(INFO) << "Initialize sub_graph."; MS_LOG(INFO) << "Initialize sub_graph.";
auto ret = sub_graph->Init(); ret = sub_graph->Init();
if (ret != RET_OK) { if (ret != RET_OK) {
MS_LOG(ERROR) << "Init sub_graph error.";
delete kernel; delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph; delete sub_graph;
MS_LOG(ERROR) << "Init sub_graph error."; return;
return RET_ERROR;
} }
MS_LOG(INFO) << "Run SubGraphOpenCLKernel."; MS_LOG(INFO) << "Run SubGraphOpenCLKernel.";
ret = sub_graph->Run(); ret = sub_graph->Run();
if (ret != RET_OK) { if (ret != RET_OK) {
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph; delete sub_graph;
MS_LOG(ERROR) << "Run SubGraphOpenCLKernel error."; MS_LOG(ERROR) << "Run SubGraphOpenCLKernel error.";
return RET_ERROR; return;
} }
MS_LOG(INFO) << "==================output data================";
printf_tensor(outputs[0]);
CompareRes(output_tensor, out_file);
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph; delete sub_graph;
return RET_OK; lite::opencl::OpenCLRuntime::DeleteInstance();
} }
TEST_F(TestActivationOpenCL, ActivationFp32_dim4) { TEST_F(TestActivationOpenCL, SigmoidFp32_dim4) {
MS_LOG(INFO) << "Begin test!"; std::string in_file = "/data/local/tmp/in_data.bin";
std::string out_file = "/data/local/tmp/sigmoid.bin";
MS_LOG(INFO) << "Sigmoid Begin test!";
auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance(); auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance();
ocl_runtime->Init(); ocl_runtime->Init();
auto allocator = ocl_runtime->GetAllocator(); auto allocator = ocl_runtime->GetAllocator();
MS_LOG(INFO) << "Init tensors."; MS_LOG(INFO) << "Init tensors.";
std::vector<int> input_shape = {1, 4, 3, 8}; std::vector<int> input_shape = {1, 9};
auto data_type = kNumberTypeFloat32; auto data_type = kNumberTypeFloat32;
auto tensor_type = schema::NodeType_ValueNode; auto tensor_type = schema::NodeType_ValueNode;
auto *input_tensor = auto *input_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NHWC4, tensor_type);
if (input_tensor == nullptr) { if (input_tensor == nullptr) {
MS_LOG(ERROR) << "new input tensor error!"; MS_LOG(ERROR) << "new input tensor error!";
return; return;
} }
auto *output_tensor = auto *output_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NHWC4, tensor_type);
if (output_tensor == nullptr) { if (output_tensor == nullptr) {
MS_LOG(ERROR) << "new output tensor error!"; MS_LOG(ERROR) << "new output tensor error!";
delete input_tensor; delete input_tensor;
...@@ -166,40 +305,184 @@ TEST_F(TestActivationOpenCL, ActivationFp32_dim4) { ...@@ -166,40 +305,184 @@ TEST_F(TestActivationOpenCL, ActivationFp32_dim4) {
std::vector<lite::tensor::Tensor *> inputs{input_tensor}; std::vector<lite::tensor::Tensor *> inputs{input_tensor};
std::vector<lite::tensor::Tensor *> outputs{output_tensor}; std::vector<lite::tensor::Tensor *> outputs{output_tensor};
inputs[0]->MallocData(allocator); inputs[0]->MallocData(allocator);
MS_LOG(INFO) << "Initialize input data";
LoadActivationData(inputs[0]->Data(), inputs[0]->Size(), in_file);
MS_LOG(INFO) << "==================input data================";
printf_tensor(inputs[0]);
auto *param = new (std::nothrow) ActivationParameter();
if (param == nullptr) {
MS_LOG(ERROR) << "New ActivationParameter fail.";
delete input_tensor;
delete output_tensor;
return;
}
param->type_ = ActivationType_SIGMOID;
auto *kernel =
new (std::nothrow) kernel::ActivationOpenClKernel(reinterpret_cast<OpParameter *>(param), inputs, outputs);
if (kernel == nullptr) {
MS_LOG(ERROR) << "Kernel:Sigmoid create fail.";
delete param;
delete input_tensor;
delete output_tensor;
return;
}
auto ret = kernel->Init();
if (ret != RET_OK) {
delete param;
delete kernel;
delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Init sigmoid fail.";
return;
}
MS_LOG(INFO) << "Create kernel SubGraphOpenCLKernel.";
std::vector<kernel::LiteKernel *> kernels{kernel};
auto *sub_graph = new (std::nothrow) kernel::SubGraphOpenCLKernel(inputs, outputs, kernels, kernels, kernels);
if (sub_graph == nullptr) {
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Kernel SubGraphOpenCLKernel create fail.";
return;
}
MS_LOG(INFO) << "Initialize sub_graph.";
ret = sub_graph->Init();
if (ret != RET_OK) {
MS_LOG(ERROR) << "Init sub_graph error.";
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph;
return;
}
MS_LOG(INFO) << "Run SubGraphOpenCLKernel.";
ret = sub_graph->Run();
if (ret != RET_OK) {
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph;
MS_LOG(ERROR) << "Run SubGraphOpenCLKernel error.";
return;
}
std::map<std::string, int> Test_Activation_Type; MS_LOG(INFO) << "==================output data================";
std::map<std::string, std::string> Test_Res_File; printf_tensor(outputs[0]);
Test_Activation_Type["Relu"] = ActivationType_RELU; CompareRes(output_tensor, out_file);
Test_Activation_Type["Leaky_Relu"] = ActivationType_LEAKY_RELU; delete kernel;
Test_Activation_Type["Relu6"] = ActivationType_RELU6; delete param;
Test_Activation_Type["Sigmoid"] = ActivationType_SIGMOID; delete input_tensor;
Test_Res_File["Leaky_Relu"] = "/data/local/tmp/leaky_relu.bin"; delete output_tensor;
Test_Res_File["Relu"] = "/data/local/tmp/relu.bin"; delete sub_graph;
Test_Res_File["Relu6"] = "/data/local/tmp/relu6.bin"; lite::opencl::OpenCLRuntime::DeleteInstance();
Test_Res_File["Sigmoid"] = "/data/local/tmp/sigmoid.bin"; }
TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) {
std::string in_file = "/data/local/tmp/in_data.bin"; std::string in_file = "/data/local/tmp/in_data.bin";
std::string out_file = "/data/local/tmp/leaky_relu.bin";
MS_LOG(INFO) << "Leaky relu Begin test!";
auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance();
ocl_runtime->Init();
auto allocator = ocl_runtime->GetAllocator();
std::map<std::string, int>::iterator it = Test_Activation_Type.begin(); MS_LOG(INFO) << "Init tensors.";
while (it != Test_Activation_Type.end()) { std::vector<int> input_shape = {1, 9};
auto kernel = create_kernel(allocator, inputs, outputs, it->first, it->second, in_file, 0.3); auto data_type = kNumberTypeFloat32;
if (kernel == nullptr) { auto tensor_type = schema::NodeType_ValueNode;
MS_LOG(ERROR) << "Create kernel:" << it->first << " error."; auto *input_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
return; if (input_tensor == nullptr) {
} MS_LOG(ERROR) << "new input tensor error!";
return;
}
auto *output_tensor = new (std::nothrow) lite::tensor::Tensor(data_type, input_shape, schema::Format_NC, tensor_type);
if (output_tensor == nullptr) {
MS_LOG(ERROR) << "new output tensor error!";
delete input_tensor;
return;
}
std::vector<lite::tensor::Tensor *> inputs{input_tensor};
std::vector<lite::tensor::Tensor *> outputs{output_tensor};
inputs[0]->MallocData(allocator);
MS_LOG(INFO) << "Initialize input data";
LoadActivationData(inputs[0]->Data(), inputs[0]->Size(), in_file);
MS_LOG(INFO) << "==================input data================";
printf_tensor(inputs[0]);
auto ret = RunSubGraphOpenCLKernel(inputs, outputs, kernel); auto *param = new (std::nothrow) ActivationParameter();
if (ret != RET_OK) { if (param == nullptr) {
MS_LOG(ERROR) << it->first << " RunSubGraphOpenCLKernel error."; MS_LOG(ERROR) << "New ActivationParameter fail.";
return; delete input_tensor;
} delete output_tensor;
MS_LOG(INFO) << "==================output data================"; return;
printf_tensor(outputs[0]); }
CompareRes(output_tensor, Test_Res_File[it->first]); param->alpha_ = 0.3;
it++; param->type_ = ActivationType_LEAKY_RELU;
auto *kernel =
new (std::nothrow) kernel::ActivationOpenClKernel(reinterpret_cast<OpParameter *>(param), inputs, outputs);
if (kernel == nullptr) {
MS_LOG(ERROR) << "Kernel:leaky relu create fail.";
delete param;
delete input_tensor;
delete output_tensor;
return;
}
auto ret = kernel->Init();
if (ret != RET_OK) {
delete param;
delete kernel;
delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Init leaky relu fail.";
return;
}
MS_LOG(INFO) << "Create kernel SubGraphOpenCLKernel.";
std::vector<kernel::LiteKernel *> kernels{kernel};
auto *sub_graph = new (std::nothrow) kernel::SubGraphOpenCLKernel(inputs, outputs, kernels, kernels, kernels);
if (sub_graph == nullptr) {
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
MS_LOG(ERROR) << "Kernel SubGraphOpenCLKernel create fail.";
return;
} }
MS_LOG(INFO) << "Initialize sub_graph.";
ret = sub_graph->Init();
if (ret != RET_OK) {
MS_LOG(ERROR) << "Init sub_graph error.";
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph;
return;
}
MS_LOG(INFO) << "Run SubGraphOpenCLKernel.";
ret = sub_graph->Run();
if (ret != RET_OK) {
delete kernel;
delete param;
delete input_tensor;
delete output_tensor;
delete sub_graph;
MS_LOG(ERROR) << "Run SubGraphOpenCLKernel error.";
return;
}
MS_LOG(INFO) << "==================output data================";
printf_tensor(outputs[0]);
CompareRes(output_tensor, out_file);
delete kernel;
delete param;
delete input_tensor; delete input_tensor;
delete output_tensor; delete output_tensor;
delete sub_graph;
lite::opencl::OpenCLRuntime::DeleteInstance(); lite::opencl::OpenCLRuntime::DeleteInstance();
} }
} // namespace mindspore } // namespace mindspore
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册