cl_functions_test.cc 10.9 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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 <gtest/gtest.h>
#include <algorithm>
#include <memory>
#include <random>
#include <vector>
20 21 22 23 24
#include "lite/backends/opencl/cl_caller.h"
#include "lite/backends/opencl/cl_context.h"
#include "lite/backends/opencl/cl_image.h"
#include "lite/backends/opencl/cl_runtime.h"
#include "lite/backends/opencl/target_wrapper.h"
Y
Yan Chunwei 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
#include "lite/core/tensor.h"
#include "lite/utils/cp_logging.h"

namespace paddle {
namespace lite {

TEST(cl_test, runtime_test) {
  auto *runtime = CLRuntime::Global();
  CHECK(runtime->IsInitSuccess());
  runtime->platform();
  runtime->device();
  runtime->command_queue();
  auto &context = runtime->context();
38 39
  auto program =
      runtime->CreateProgram(context, "buffer/elementwise_add_kernel.cl");
Y
Yan Chunwei 已提交
40
  auto event = runtime->CreateEvent(context);
41 42
  const std::string build_option("-DCL_DTYPE_float");
  CHECK(runtime->BuildProgram(program.get(), build_option));
Y
Yan Chunwei 已提交
43 44 45 46 47 48
}

TEST(cl_test, context_test) {
  auto *runtime = CLRuntime::Global();
  CHECK(runtime->IsInitSuccess());
  CLContext context;
49
  context.AddKernel("pool_max", "image/pool_kernel.cl", "-DCL_DTYPE_half");
50
  context.AddKernel(
51
      "elementwise_add", "image/elementwise_add_kernel.cl", "-DCL_DTYPE_half");
52
  context.AddKernel(
53
      "elementwise_add", "image/elementwise_add_kernel.cl", "-DCL_DTYPE_half");
Y
Yan Chunwei 已提交
54 55 56 57 58 59
}

TEST(cl_test, kernel_test) {
  auto *runtime = CLRuntime::Global();
  CHECK(runtime->IsInitSuccess());
  std::unique_ptr<CLContext> context(new CLContext);
60
  context->AddKernel(
61 62
      "elementwise_add", "image/elementwise_add_kernel.cl", "-DCL_DTYPE_half");
  context->AddKernel("pool_max", "image/pool_kernel.cl", "-DCL_DTYPE_half");
63
  context->AddKernel(
64
      "elementwise_add", "image/elementwise_add_kernel.cl", "-DCL_DTYPE_half");
Y
Yan Chunwei 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  auto kernel = context->GetKernel(2);

  std::unique_ptr<float[]> in_data(new float[4 * 3 * 256 * 512]);
  for (int i = 0; i < 4 * 3 * 256 * 512; i++) {
    in_data[i] = 1.f;
  }
  const DDim in_dim = DDim(std::vector<DDim::value_type>{4, 3, 256, 512});
  CLImage in_image;
  in_image.set_tensor_data(in_data.get(), in_dim);
  in_image.InitNormalCLImage(context->GetContext());
  LOG(INFO) << in_image;

  std::unique_ptr<float[]> bias_data(new float[4 * 3 * 256 * 512]);
  for (int i = 0; i < 4 * 3 * 256 * 512; i++) {
    bias_data[i] = 2.f;
  }
  const DDim bias_dim = DDim(std::vector<DDim::value_type>{4, 3, 256, 512});
  CLImage bias_image;
  bias_image.set_tensor_data(bias_data.get(), bias_dim);
  bias_image.InitNormalCLImage(context->GetContext());
  LOG(INFO) << bias_image;

  CLImage out_image;
  const DDim out_dim = DDim(std::vector<DDim::value_type>{4, 3, 256, 512});
  out_image.InitEmptyImage(context->GetContext(), out_dim);
  LOG(INFO) << out_image;

  cl_int status;
93
  status = kernel->setArg(0, *in_image.cl_image());
Y
Yan Chunwei 已提交
94
  CL_CHECK_FATAL(status);
95
  status = kernel->setArg(1, *bias_image.cl_image());
Y
Yan Chunwei 已提交
96
  CL_CHECK_FATAL(status);
97
  status = kernel->setArg(2, *out_image.cl_image());
Y
Yan Chunwei 已提交
98 99 100 101 102 103
  CL_CHECK_FATAL(status);

  size_t width = in_image.ImageWidth();
  size_t height = in_image.ImageHeight();
  auto global_work_size = cl::NDRange{width, height};
  cl::Event event;
104 105 106 107 108 109
  status = context->GetCommandQueue().enqueueNDRangeKernel(*kernel.get(),
                                                           cl::NullRange,
                                                           global_work_size,
                                                           cl::NullRange,
                                                           nullptr,
                                                           &event);
Y
Yan Chunwei 已提交
110 111 112 113 114 115 116 117 118 119 120
  CL_CHECK_FATAL(status);
  status = context->GetCommandQueue().finish();
  CL_CHECK_FATAL(status);
  double start_nanos = event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
  double stop_nanos = event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
  double elapsed_micros = (stop_nanos - start_nanos) / 1000.0;
  LOG(INFO) << "Kernel Run Cost Time: " << elapsed_micros << " us.";
  LOG(INFO) << out_image;
}

TEST(cl_test, target_wrapper_buffer_test) {
121
  bool inited = InitOpenCLRuntime();
Y
Yan Chunwei 已提交
122 123 124
  CHECK(inited) << "Fail to initialize OpenCL runtime.";
  std::unique_ptr<CLContext> context(new CLContext);
  std::string kernel_name = "elementwise_add";
125
  std::string build_options = "-DCL_DTYPE_half";
Y
Yan Chunwei 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  context->AddKernel(
      kernel_name, "buffer/elementwise_add_kernel.cl", build_options);
  std::vector<float> h_a;
  std::vector<float> h_b;
  std::vector<float> h_out;
  std::vector<float> h_ref;
  for (int i = 0; i < 10; i++) {
    h_a.push_back(3.14f * i);
    h_b.push_back(6.28f * i);
    h_out.push_back(0);
    h_ref.push_back((3.14f + 6.28f) * i);
  }
  auto *d_a = static_cast<cl::Buffer *>(
      TargetWrapperCL::Malloc(sizeof(float) * h_a.size()));
  auto *d_b = static_cast<cl::Buffer *>(
      TargetWrapperCL::Malloc(sizeof(float) * h_b.size()));
  auto *d_out =
      static_cast<cl::Buffer *>(TargetWrapperCL::Malloc(sizeof(float) * 10));
  auto *d_copy =
      static_cast<cl::Buffer *>(TargetWrapperCL::Malloc(sizeof(float) * 10));
  TargetWrapperCL::MemcpySync(
      d_a, h_a.data(), sizeof(float) * h_a.size(), IoDirection::HtoD);
  TargetWrapperCL::MemcpySync(
      d_b, h_b.data(), sizeof(float) * h_b.size(), IoDirection::HtoD);
  // x + y: x[n=1, c=10, h=1, w=1], y[c=10]
  auto kernel = context->GetKernel(kernel_name + build_options);
152
  cl_int status = kernel->setArg(0, *d_a);
Y
Yan Chunwei 已提交
153
  CL_CHECK_FATAL(status);
154
  status = kernel->setArg(1, *d_b);
Y
Yan Chunwei 已提交
155
  CL_CHECK_FATAL(status);
156
  status = kernel->setArg(2, *d_out);
Y
Yan Chunwei 已提交
157
  CL_CHECK_FATAL(status);
158
  status = kernel->setArg(3, 1);
Y
Yan Chunwei 已提交
159
  CL_CHECK_FATAL(status);
160
  status = kernel->setArg(4, 10);
Y
Yan Chunwei 已提交
161
  CL_CHECK_FATAL(status);
162
  status = kernel->setArg(5, 1);
Y
Yan Chunwei 已提交
163 164
  CL_CHECK_FATAL(status);
  auto global_work_size = cl::NDRange{10, 1};
165 166 167 168 169 170
  status = context->GetCommandQueue().enqueueNDRangeKernel(*kernel.get(),
                                                           cl::NullRange,
                                                           global_work_size,
                                                           cl::NullRange,
                                                           nullptr,
                                                           nullptr);
Y
Yan Chunwei 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  CL_CHECK_FATAL(status);
  status = context->GetCommandQueue().finish();
  CL_CHECK_FATAL(status);
  TargetWrapperCL::MemcpySync(
      h_out.data(), d_out, sizeof(float) * 10, IoDirection::DtoH);

  for (int i = 0; i < 10; i++) {
    std::cout << h_out[i] << " ";
  }
  std::cout << std::endl;

  for (int i = 0; i < 10; i++) {
    EXPECT_NEAR(h_out[i], h_ref[i], 1e-5);
  }

  TargetWrapperCL::MemcpySync(
      d_copy, d_out, sizeof(float) * 10, IoDirection::DtoD);
  std::fill(h_out.begin(), h_out.end(), 0);
  for (int i = 0; i < 10; i++) {
    EXPECT_NEAR(h_out[i], 0, 1e-5);
  }
  TargetWrapperCL::MemcpySync(
      h_out.data(), d_copy, sizeof(float) * 10, IoDirection::DtoH);
  for (int i = 0; i < 10; i++) {
    EXPECT_NEAR(h_out[i], h_ref[i], 1e-5);
  }

  auto *mapped_ptr =
      static_cast<float *>(TargetWrapperCL::Map(d_copy, 0, sizeof(float) * 10));
  for (int i = 0; i < 10; i++) {
    EXPECT_NEAR(mapped_ptr[i], h_ref[i], 1e-5);
  }
  TargetWrapperCL::Unmap(d_copy, mapped_ptr);

  TargetWrapperCL::Free(d_copy);
  TargetWrapperCL::Free(d_out);
  TargetWrapperCL::Free(d_b);
  TargetWrapperCL::Free(d_a);
}

TEST(cl_test, target_wrapper_image_test) {
212 213
  const size_t cl_image2d_width = 28;
  const size_t cl_image2d_height = 32;
214 215
  const size_t cl_image2d_elem_size =
      cl_image2d_width * cl_image2d_height * 4;  // 4 for RGBA channels
216 217
  const size_t cl_image2d_row_pitch{0};
  const size_t cl_image2d_slice_pitch{0};
218
  auto *d_image = static_cast<cl::Image2D *>(
219
      TargetWrapperCL::MallocImage<float>(cl_image2d_width, cl_image2d_height));
220

Y
Yan Chunwei 已提交
221
  // Map/Unmap test
222 223 224 225 226 227
  auto *h_image =
      static_cast<float *>(TargetWrapperCL::MapImage(d_image,
                                                     cl_image2d_width,
                                                     cl_image2d_height,
                                                     cl_image2d_row_pitch,
                                                     cl_image2d_slice_pitch));
228
  CHECK_EQ(cl_image2d_slice_pitch, 0);
229 230
  LOG(INFO) << "cl_image2d_row_pitch = " << cl_image2d_row_pitch
            << ", cl_image2d_slice_pitch " << cl_image2d_slice_pitch;
Y
Yan Chunwei 已提交
231

232
  for (int i = 0; i < cl_image2d_elem_size; i++) {
Y
Yan Chunwei 已提交
233 234 235 236
    h_image[i] = 3.14f * i;
  }
  TargetWrapperCL::Unmap(d_image, h_image);

237 238 239 240 241 242
  auto *h_ptr =
      static_cast<float *>(TargetWrapperCL::MapImage(d_image,
                                                     cl_image2d_width,
                                                     cl_image2d_height,
                                                     cl_image2d_row_pitch,
                                                     cl_image2d_slice_pitch));
243
  for (int i = 0; i < cl_image2d_elem_size; i++) {
Y
Yan Chunwei 已提交
244 245 246 247 248
    EXPECT_NEAR(h_ptr[i], 3.14f * i, 1e-6);
  }
  TargetWrapperCL::Unmap(d_image, h_ptr);

  // Imagecpy test
249 250
  std::vector<float> h_image_cpy(cl_image2d_elem_size);
  for (int i = 0; i < cl_image2d_elem_size; i++) {
Y
Yan Chunwei 已提交
251 252
    h_image_cpy[i] = 3.14f;
  }
253 254 255 256 257 258 259
  TargetWrapperCL::ImgcpySync(d_image,
                              h_image_cpy.data(),
                              cl_image2d_width,
                              cl_image2d_height,
                              cl_image2d_row_pitch,
                              cl_image2d_slice_pitch,
                              IoDirection::HtoD);
Y
Yan Chunwei 已提交
260
  auto *d_image_cpy = static_cast<cl::Image2D *>(
261
      TargetWrapperCL::MallocImage<float>(cl_image2d_width, cl_image2d_height));
262 263

  // device to device
264 265 266 267 268 269 270
  TargetWrapperCL::ImgcpySync(d_image_cpy,
                              d_image,
                              cl_image2d_width,
                              cl_image2d_height,
                              cl_image2d_row_pitch,
                              cl_image2d_slice_pitch,
                              IoDirection::DtoD);
Y
Yan Chunwei 已提交
271
  std::fill(h_image_cpy.begin(), h_image_cpy.end(), 0);
272 273

  // host to device
Y
Yan Chunwei 已提交
274 275
  TargetWrapperCL::ImgcpySync(h_image_cpy.data(),
                              d_image_cpy,
276 277 278 279
                              cl_image2d_width,
                              cl_image2d_height,
                              cl_image2d_row_pitch,
                              cl_image2d_slice_pitch,
Y
Yan Chunwei 已提交
280
                              IoDirection::DtoH);
281
  for (int i = 0; i < cl_image2d_elem_size; i++) {
Y
Yan Chunwei 已提交
282 283 284 285 286 287 288 289 290
    EXPECT_NEAR(h_image_cpy[i], 3.14f, 1e-6);
  }

  TargetWrapperCL::FreeImage(d_image_cpy);
  TargetWrapperCL::FreeImage(d_image);
}

}  // namespace lite
}  // namespace paddle