instancenorm_kernel.cpp 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* 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. */

#ifdef INSTANCENORM_OP

#include "operators/kernel/instancenorm_kernel.h"
#include <cmath>

namespace paddle_mobile {
namespace operators {

template <>
bool InstanceNormKernel<GPU_CL, float>::Init(InstanceNormParam<GPU_CL> *param) {
  this->cl_helper_.AddKernel("instancenorm", "instancenorm_kernel.cl");
  return true;
}

template <>
void InstanceNormKernel<GPU_CL, float>::Compute(
    const InstanceNormParam<GPU_CL> &param) {
  auto kernel = this->cl_helper_.KernelAt(0);
  auto &dims = param.Out()->dims();

  const int n = dims[0];
  const int c_group = (dims[1] + 3) / 4;
  const int h = dims[2];
  const int w = dims[3];
  auto epsilon = param.Epsilon();
  auto input = param.InputX()->GetCLImage();
  auto out = param.Out()->GetCLImage();

  DLOG << "Epsilon: " << epsilon;

  auto local_work_size_info = this->cl_helper_.LocalWorkSizeInfo();

  DLOG << local_work_size_info.max_work_group_size;
  DLOG << local_work_size_info.max_work_item_size0;
  DLOG << local_work_size_info.max_work_item_size1;
  DLOG << local_work_size_info.max_work_item_size2;

52 53 54
  int local_work_size1 =
      std::min(static_cast<int>(local_work_size_info.max_work_item_size1),
               std::min(256, w));
55 56 57 58 59 60 61 62 63 64 65 66
  int local_work_size2 = 1;
  const size_t work_size[3] = {(size_t)(n * c_group), (size_t)local_work_size1,
                               (size_t)local_work_size2};
  const size_t local_work_size[3] = {(size_t)1, (size_t)local_work_size1,
                                     (size_t)local_work_size2};

  DLOG << "work_size" << work_size[0] << " " << work_size[1] << " "
       << work_size[2];
  DLOG << "local_work_size" << local_work_size[0] << " " << local_work_size[1]
       << " " << local_work_size[2];

  cl_int status;
67
  status = clSetKernelArg(kernel, 0, sizeof(cl_int), &w);
68
  CL_CHECK_ERRORS(status);
69
  status = clSetKernelArg(kernel, 1, sizeof(cl_int), &h);
70
  CL_CHECK_ERRORS(status);
71
  status = clSetKernelArg(kernel, 2, sizeof(cl_int), &c_group);
72
  CL_CHECK_ERRORS(status);
73
  status = clSetKernelArg(kernel, 3, sizeof(cl_int), &local_work_size1);
74
  CL_CHECK_ERRORS(status);
75
  status = clSetKernelArg(kernel, 4, sizeof(cl_int), &local_work_size2);
76
  CL_CHECK_ERRORS(status);
77
  status = clSetKernelArg(kernel, 5, sizeof(cl_float), &epsilon);
78
  CL_CHECK_ERRORS(status);
79
  status = clSetKernelArg(kernel, 6, sizeof(cl_mem), &input);
80
  CL_CHECK_ERRORS(status);
81 82 83 84 85
  status = clSetKernelArg(kernel, 7, sizeof(cl_mem), &out);
  CL_CHECK_ERRORS(status);
  status =
      clEnqueueNDRangeKernel(this->cl_helper_.CLCommandQueue(), kernel, 3, NULL,
                             work_size, local_work_size, 0, NULL, NULL);
86 87 88 89 90 91 92 93 94
  CL_CHECK_ERRORS(status);
}

template class InstanceNormKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile

#endif