From 10eb27b19f05a01676ad55fb6759f5158b51cb96 Mon Sep 17 00:00:00 2001 From: chenzupeng Date: Fri, 21 Aug 2020 16:53:30 +0800 Subject: [PATCH] add reshape test case --- .../kernel/opencl/kernel/depthwise_conv2d.cc | 4 +- .../kernel/opencl/kernel/depthwise_conv2d.h | 1 - .../runtime/kernel/opencl/kernel/pooling2d.cc | 2 +- .../runtime/kernel/opencl/kernel/pooling2d.h | 1 - .../runtime/kernel/opencl/kernel/reshape.cc | 14 ++- .../src/runtime/kernel/opencl/kernel/slice.cc | 4 +- mindspore/lite/test/CMakeLists.txt | 1 + .../runtime/kernel/opencl/reshape_tests.cc | 111 ++++++++++++++++++ 8 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.cc b/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.cc index d49006f8ca..16a9d5db39 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.cc +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.cc @@ -49,7 +49,7 @@ int DepthwiseConv2dOpenCLKernel::Init() { MS_LOG(ERROR) << "input format(" << in_format << ") " << "format not support!"; } - if (mem_type_ == MEM_TYPE::BUF) { + if (out_mem_type_ == OpenCLMemType::BUF) { kernel_name += "_BUF"; } else { kernel_name += "_IMG"; @@ -73,7 +73,7 @@ int DepthwiseConv2dOpenCLKernel::Init() { ocl_runtime->BuildKernel(kernel_, program_name, kernel_name, build_options); #endif this->InitBuffer(); - MS_LOG(DEBUG) << kernel_name << " Init Done! mem type=" << static_cast(mem_type_); + MS_LOG(DEBUG) << kernel_name << " Init Done! mem type=" << static_cast(out_mem_type_); return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.h b/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.h index adad41f6c8..a137cef78d 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.h +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/depthwise_conv2d.h @@ -49,7 +49,6 @@ class DepthwiseConv2dOpenCLKernel : public OpenCLKernel { FLOAT_t *packed_weight_; FLOAT_t *bias_data_; cl::Kernel kernel_; - enum class MEM_TYPE { BUF, IMG } mem_type_{MEM_TYPE::IMG}; }; } // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.cc b/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.cc index 27b4ff2800..6e181c5ec2 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.cc +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.cc @@ -64,7 +64,7 @@ int PoolingOpenCLKernel::Init() { #ifdef PROGRAM_WITH_IL ocl_runtime->CreateKernelFromIL(kernel_(), kernel_name); #else - if (mem_type_ == MEM_TYPE::BUF) { + if (out_mem_type_ == OpenCLMemType::BUF) { kernel_name += "_BUF"; } else { kernel_name += "_IMG"; diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.h b/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.h index b08b000308..56859a4a95 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.h +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/pooling2d.h @@ -42,7 +42,6 @@ class PoolingOpenCLKernel : public OpenCLKernel { private: std::vector InitGlobalSize() const; - enum class MEM_TYPE { BUF, IMG } mem_type_{MEM_TYPE::IMG}; PoolingParameter *parameter_; cl::Kernel kernel_; }; diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/reshape.cc b/mindspore/lite/src/runtime/kernel/opencl/kernel/reshape.cc index a5153baaf3..cc5055be1a 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/reshape.cc +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/reshape.cc @@ -33,7 +33,17 @@ namespace mindspore::kernel { int ReshapeOpenCLKernel::Init() { std::string kernel_name = "reshape"; auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance(); - + in_ori_format_ = in_tensors_[0]->GetFormat(); + out_ori_format_ = out_tensors_[0]->GetFormat(); + if (in_ori_format_ != schema::Format_NHWC4 && in_ori_format_ != schema::Format_NHWC) { + MS_LOG(ERROR) << "Reshape input format:" << in_ori_format_ << " not support yet."; + return RET_ERROR; + } + if (in_tensors_[0]->shape().back() != out_tensors_[0]->shape().back()) { + MS_LOG(ERROR) << "Reshape input channel " << in_tensors_[0]->shape().back() << " should equal output channel" + << out_tensors_[0]->shape().back(); + return RET_ERROR; + } #ifdef PROGRAM_WITH_IL ocl_runtime->CreateKernelFromIL(kernel_(), kernel_name); #else @@ -43,9 +53,7 @@ int ReshapeOpenCLKernel::Init() { ocl_runtime->LoadSource(program_name, source); ocl_runtime->BuildKernel(kernel_, program_name, kernel_name, build_options); #endif - in_ori_format_ = in_tensors_[0]->GetFormat(); in_tensors_[0]->SetFormat(schema::Format_NHWC4); - out_ori_format_ = out_tensors_[0]->GetFormat(); out_tensors_[0]->SetFormat(schema::Format_NHWC4); if (out_tensors_[0]->shape().size() == 2) { out_ori_format_ = schema::Format_NC; diff --git a/mindspore/lite/src/runtime/kernel/opencl/kernel/slice.cc b/mindspore/lite/src/runtime/kernel/opencl/kernel/slice.cc index ecbefcc558..dfc2038bc2 100644 --- a/mindspore/lite/src/runtime/kernel/opencl/kernel/slice.cc +++ b/mindspore/lite/src/runtime/kernel/opencl/kernel/slice.cc @@ -56,7 +56,9 @@ int SliceOpenCLKernel::Init() { auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance(); ocl_runtime->LoadSource(program_name, source); ocl_runtime->BuildKernel(kernel_, program_name, kernel_name, build_options); - ori_format_ = out_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_tensors_[0]->SetFormat(schema::Format_NHWC4); return RET_OK; diff --git a/mindspore/lite/test/CMakeLists.txt b/mindspore/lite/test/CMakeLists.txt index c492112e0c..d773c2434f 100644 --- a/mindspore/lite/test/CMakeLists.txt +++ b/mindspore/lite/test/CMakeLists.txt @@ -337,6 +337,7 @@ if (SUPPORT_GPU) ${TEST_DIR}/ut/src/runtime/kernel/opencl/to_format_tests.cc ${TEST_DIR}/ut/src/runtime/kernel/opencl/caffe_prelu_tests.cc ${TEST_DIR}/ut/src/runtime/kernel/opencl/prelu_tests.cc + ${TEST_DIR}/ut/src/runtime/kernel/opencl/reshape_tests.cc ) endif() diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc new file mode 100644 index 0000000000..bbed448f85 --- /dev/null +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc @@ -0,0 +1,111 @@ +/** + * 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 +#include +#include "mindspore/core/utils/log_adapter.h" +#include "common/common_test.h" +#include "mindspore/lite/src/common/file_utils.h" +#include "mindspore/lite/src/runtime/opencl/opencl_runtime.h" +#include "mindspore/lite/src/runtime/kernel/opencl/subgraph_opencl_kernel.h" +#include "mindspore/lite/src/runtime/kernel/opencl/kernel/reshape.h" + +namespace mindspore { +class TestReshapeOpenCL : public mindspore::CommonTest { + public: + TestReshapeOpenCL() {} +}; + +TEST_F(TestReshapeOpenCL, ReshapeFp32) { + auto ocl_runtime = lite::opencl::OpenCLRuntime::GetInstance(); + ocl_runtime->Init(); + auto allocator = ocl_runtime->GetAllocator(); + int c = 63; + size_t input_size; + std::string input_path = "./test_data/reshape/reshape_fp32_input.bin"; + auto input_data = reinterpret_cast(mindspore::lite::ReadFile(input_path.c_str(), &input_size)); + if (input_data == nullptr) { + MS_LOG(ERROR) << "input_data load error."; + return; + } + std::vector input_shape = {1, 1, 1, c}; + auto tensor_x_ptr = + std::make_unique(TypeId(kNumberTypeFloat32), input_shape, schema::Format_NHWC); + auto tensor_x = tensor_x_ptr.get(); + if (tensor_x == nullptr) { + MS_LOG(ERROR) << "tensor_x create error."; + return; + } + std::vector out_shape = {1, c}; + auto tensor_out_ptr = + std::make_unique(TypeId(kNumberTypeFloat32), out_shape, schema::Format_NC); + auto tensor_out = tensor_out_ptr.get(); + if (tensor_out == nullptr) { + MS_LOG(ERROR) << "tensor_out create error."; + return; + } + std::vector inputs{tensor_x}; + std::vector outputs{tensor_out}; + auto arith_kernel_ptr = std::make_unique(nullptr, inputs, outputs); + auto arith_kernel = arith_kernel_ptr.get(); + if (arith_kernel == nullptr) { + MS_LOG(ERROR) << "arith_kernel create error."; + return; + } + arith_kernel->Init(); + + inputs[0]->MallocData(allocator); + + std::vector kernels{arith_kernel}; + auto pGraph_ptr = std::make_unique(inputs, outputs, kernels, kernels, kernels); + auto pGraph = pGraph_ptr.get(); + if (pGraph == nullptr) { + MS_LOG(ERROR) << "pGraph create error."; + return; + } + pGraph->Init(); + memcpy(inputs[0]->Data(), input_data, input_size); + pGraph->Run(); + + size_t output_size; + std::string output_path = "./test_data/reshape/reshape_fp32_output.bin"; + auto correct_data = reinterpret_cast(mindspore::lite::ReadFile(output_path.c_str(), &output_size)); + if (correct_data == nullptr) { + MS_LOG(ERROR) << "correct_data create error."; + return; + } + printf("==================output data=================\n"); + float *output_data = reinterpret_cast(tensor_out->Data()); + std::cout << std::endl; + int size_n = c; + size_n = size_n > 100 ? 100 : size_n; + for (int i = 0; i < size_n; i++) { + std::cout << output_data[i] << " "; + if ((i + 1) % c == 0) { + std::cout << std::endl; + } + } + std::cout << std::endl; + + // compare + CompareOutputData(output_data, correct_data, c, 0.00001); + + inputs[0]->SetData(nullptr); + outputs[0]->SetData(nullptr); + lite::opencl::OpenCLRuntime::DeleteInstance(); + + MS_LOG(INFO) << "Test ReshapeFp32 passed"; +} +} // namespace mindspore