conv2d_3x3_kernel.cl 12.4 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
/* 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 <cl_common.h>

__kernel void conv2d_3x3(__private const int global_size_dim0,
                         __private const int global_size_dim1,
                         __private const int global_size_dim2,
                         __read_only image2d_t input_image,
                         __read_only image2d_t filter,
                         __read_only image2d_t bias,
                         __write_only image2d_t output_image,
                         __private const int stride,
                         __private const int offset,
                         __private const int input_c,
                         __private const int dilation,
X
xiebaiyuan 已提交
28 29
                         __private const int input_width,  /* of one block */
                         __private const int input_height, /* of one block */
30 31 32
                         __private const int output_width,
                         __private const int output_height,
                         __private const int output_c,
33
                         __private const int filter_tensor_c,
X
xiebaiyuan 已提交
34 35 36
                         __private const int filter_width,
                         __private const int filter_height,
                         __private const int group,
37
                         __private const int input_tensor_c) {
38

X
xiebaiyuan 已提交
39 40 41
  const int out_c = get_global_id(0);
  const int out_w = get_global_id(1);
  const int out_nh = get_global_id(2);
42

X
xiebaiyuan 已提交
43 44
  const sampler_t sampler =
      CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
45

X
xiebaiyuan 已提交
46
  int2 output_pos = (int2)(out_c * global_size_dim1 + out_w, out_nh);
47

X
xiebaiyuan 已提交
48 49 50 51
  if (out_c >= global_size_dim0 || out_w >= global_size_dim1 ||
      out_nh >= global_size_dim2) {
    return;
  }
52

X
xiebaiyuan 已提交
53 54 55
  int2 stride_xy;
  stride_xy.x = stride;
  stride_xy.y = stride;
56

X
xiebaiyuan 已提交
57 58 59
  int2 ouput_pos_in_one_block;
  ouput_pos_in_one_block.x = out_w;
  ouput_pos_in_one_block.y = out_nh;
60

X
xiebaiyuan 已提交
61 62 63
  int2 in_pos_in_one_block;
  in_pos_in_one_block.x = ouput_pos_in_one_block.x * stride + offset;
  in_pos_in_one_block.y = ouput_pos_in_one_block.y * stride + offset;
64 65

#ifdef BIASE_CH
X
xiebaiyuan 已提交
66 67
  CL_DTYPE4 output =
      READ_IMG_TYPE(CL_DTYPE_CHAR, bias, sampler, (int2)(out_c, 0));
68
#elif defined(BIASE_ELE)
X
xiebaiyuan 已提交
69
  CL_DTYPE4 output = READ_IMG_TYPE(CL_DTYPE_CHAR, bias, sampler, output_pos);
70
#else
X
xiebaiyuan 已提交
71
  CL_DTYPE4 output = (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f);
72
#endif
73
  CL_DTYPE4 zero_dtype4 = (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f);
74

75
  CL_DTYPE4 input0, input1, input2, input3, input4, input5, input6, input7, input8;
X
xiebaiyuan 已提交
76 77
    for (int i = 0; i < 4; i++) {
      int used_input_channel_num =
78 79 80
          (out_c * 4 + i) / (output_c / group) * filter_tensor_c;
      for (int filter_tensor_c_idx = 0; filter_tensor_c_idx < filter_tensor_c; ++filter_tensor_c_idx) {
        int input_c = used_input_channel_num + filter_tensor_c_idx;
X
xiebaiyuan 已提交
81 82 83
        int input_block = input_c / 4;
        int2 pos_in = (int2)(input_block * input_width + in_pos_in_one_block.x,
                             in_pos_in_one_block.y);
84
        input0 = select(
X
xiebaiyuan 已提交
85 86 87
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
88
                          (int2)(pos_in.x - dilation, pos_in.y - dilation)),
89
            zero_dtype4,
X
xiebaiyuan 已提交
90 91 92 93 94
            (ushort4)((in_pos_in_one_block.x - dilation < 0 ||
                       in_pos_in_one_block.y - dilation < 0 ||
                       in_pos_in_one_block.x - dilation >= input_width ||
                       in_pos_in_one_block.y - dilation >= input_height)
                      << 15));
95
        input1 =
X
xiebaiyuan 已提交
96 97 98
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
99
                                 (int2)(pos_in.x, pos_in.y - dilation)),
100
                   zero_dtype4,
X
xiebaiyuan 已提交
101 102 103 104 105
                   (ushort4)((in_pos_in_one_block.x < 0 ||
                              in_pos_in_one_block.y - dilation < 0 ||
                              in_pos_in_one_block.x >= input_width ||
                              in_pos_in_one_block.y - dilation >= input_height)
                             << 15));
106
        input2 = select(
X
xiebaiyuan 已提交
107 108 109
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
110
                          (int2)(pos_in.x + dilation, pos_in.y - dilation)),
111
            zero_dtype4,
X
xiebaiyuan 已提交
112 113 114 115 116
            (ushort4)((in_pos_in_one_block.x + dilation < 0 ||
                       in_pos_in_one_block.y - dilation < 0 ||
                       in_pos_in_one_block.x + dilation >= input_width ||
                       in_pos_in_one_block.y - dilation >= input_height)
                      << 15));
117 118

        input3 =
X
xiebaiyuan 已提交
119 120 121 122
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
                                 (int2)(pos_in.x - dilation, pos_in.y)),
123
                   zero_dtype4,
X
xiebaiyuan 已提交
124 125 126 127 128
                   (ushort4)((in_pos_in_one_block.x - dilation < 0 ||
                              in_pos_in_one_block.y < 0 ||
                              in_pos_in_one_block.x - dilation >= input_width ||
                              in_pos_in_one_block.y >= input_height)
                             << 15));
129 130

        input4 = select(
X
xiebaiyuan 已提交
131 132 133 134
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
                          (int2)(pos_in.x, pos_in.y)),
135
            zero_dtype4,
X
xiebaiyuan 已提交
136 137 138 139
            (ushort4)((in_pos_in_one_block.x < 0 || in_pos_in_one_block.y < 0 ||
                       in_pos_in_one_block.x >= input_width ||
                       in_pos_in_one_block.y >= input_height)
                      << 15));
140
        input5 =
X
xiebaiyuan 已提交
141 142 143 144
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
                                 (int2)(pos_in.x + dilation, pos_in.y)),
145
                   zero_dtype4,
146 147 148 149 150
                   (ushort4)((in_pos_in_one_block.x + dilation < 0 ||
                              in_pos_in_one_block.y < 0 ||
                              in_pos_in_one_block.x + dilation >= input_width ||
                              in_pos_in_one_block.y >= input_height)
                             << 15));
151
        input6 = select(
X
xiebaiyuan 已提交
152 153 154
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
155
                          (int2)(pos_in.x - dilation, pos_in.y + dilation)),
156
            zero_dtype4,
X
xiebaiyuan 已提交
157 158 159 160 161
            (ushort4)((in_pos_in_one_block.x - dilation < 0 ||
                       in_pos_in_one_block.y + dilation < 0 ||
                       in_pos_in_one_block.x - dilation >= input_width ||
                       in_pos_in_one_block.y + dilation >= input_height)
                      << 15));
162
        input7 =
X
xiebaiyuan 已提交
163 164 165
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
166
                                 (int2)(pos_in.x, pos_in.y + dilation)),
167
                   zero_dtype4,
X
xiebaiyuan 已提交
168 169 170 171 172
                   (ushort4)((in_pos_in_one_block.x < 0 ||
                              in_pos_in_one_block.y + dilation < 0 ||
                              in_pos_in_one_block.x >= input_width ||
                              in_pos_in_one_block.y + dilation >= input_height)
                             << 15));
173
        input8 = select(
X
xiebaiyuan 已提交
174 175 176
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
177
                          (int2)(pos_in.x + dilation, pos_in.y + dilation)),
178
            zero_dtype4,
X
xiebaiyuan 已提交
179 180 181 182 183 184 185 186 187
            (ushort4)((in_pos_in_one_block.x + dilation < 0 ||
                       in_pos_in_one_block.y + dilation < 0 ||
                       in_pos_in_one_block.x + dilation >= input_width ||
                       in_pos_in_one_block.y + dilation >= input_height)
                      << 15));

        CL_DTYPE tmp_out = 0;
        for (int j = 0; j < 9; j++) {
          int2 pos_of_weight;
188
          pos_of_weight.x = (filter_tensor_c_idx / 4) * 3 + j % 3;
X
xiebaiyuan 已提交
189 190 191 192
          pos_of_weight.y = out_c * 4 * 3 + i * 3 + j / 3;
          CL_DTYPE4 weight =
              READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);

193 194 195 196 197 198
          int filter_tensor_c_idx_offset = filter_tensor_c_idx % 4;
          CL_DTYPE f_value = 0;
          f_value = (filter_tensor_c_idx_offset == 0) ? weight.x : f_value;
          f_value = (filter_tensor_c_idx_offset == 1) ? weight.y : f_value;
          f_value = (filter_tensor_c_idx_offset == 2) ? weight.z : f_value;
          f_value = (filter_tensor_c_idx_offset == 3) ? weight.w : f_value;
199

X
xiebaiyuan 已提交
200
          int input_c_offset = input_c % 4;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
          CL_DTYPE input_value = 0;
          if (j == 0) {
              input_value = (input_c_offset == 0) ? input0.x : input_value;
              input_value = (input_c_offset == 1) ? input0.y : input_value;
              input_value = (input_c_offset == 2) ? input0.z : input_value;
              input_value = (input_c_offset == 3) ? input0.w : input_value;
	  } else if (j == 1) {
              input_value = (input_c_offset == 0) ? input1.x : input_value;
              input_value = (input_c_offset == 1) ? input1.y : input_value;
              input_value = (input_c_offset == 2) ? input1.z : input_value;
              input_value = (input_c_offset == 3) ? input1.w : input_value;
	  } else if (j == 2) {
              input_value = (input_c_offset == 0) ? input2.x : input_value;
              input_value = (input_c_offset == 1) ? input2.y : input_value;
              input_value = (input_c_offset == 2) ? input2.z : input_value;
              input_value = (input_c_offset == 3) ? input2.w : input_value;
	  } else if (j == 3) {
              input_value = (input_c_offset == 0) ? input3.x : input_value;
              input_value = (input_c_offset == 1) ? input3.y : input_value;
              input_value = (input_c_offset == 2) ? input3.z : input_value;
              input_value = (input_c_offset == 3) ? input3.w : input_value;
	  } else if (j == 4) {
              input_value = (input_c_offset == 0) ? input4.x : input_value;
              input_value = (input_c_offset == 1) ? input4.y : input_value;
              input_value = (input_c_offset == 2) ? input4.z : input_value;
              input_value = (input_c_offset == 3) ? input4.w : input_value;
	  } else if (j == 5) {
              input_value = (input_c_offset == 0) ? input5.x : input_value;
              input_value = (input_c_offset == 1) ? input5.y : input_value;
              input_value = (input_c_offset == 2) ? input5.z : input_value;
              input_value = (input_c_offset == 3) ? input5.w : input_value;
	  } else if (j == 6) {
              input_value = (input_c_offset == 0) ? input6.x : input_value;
              input_value = (input_c_offset == 1) ? input6.y : input_value;
              input_value = (input_c_offset == 2) ? input6.z : input_value;
              input_value = (input_c_offset == 3) ? input6.w : input_value;
	  } else if (j == 7) {
              input_value = (input_c_offset == 0) ? input7.x : input_value;
              input_value = (input_c_offset == 1) ? input7.y : input_value;
              input_value = (input_c_offset == 2) ? input7.z : input_value;
              input_value = (input_c_offset == 3) ? input7.w : input_value;
	  } else if (j == 8) {
              input_value = (input_c_offset == 0) ? input8.x : input_value;
              input_value = (input_c_offset == 1) ? input8.y : input_value;
              input_value = (input_c_offset == 2) ? input8.z : input_value;
              input_value = (input_c_offset == 3) ? input8.w : input_value;
	  }
X
xiebaiyuan 已提交
248

249
          tmp_out += f_value * input_value;
250
        }
251 252 253 254
        output.x = (i == 0) ? output.x + tmp_out : output.x;
        output.y = (i == 1) ? output.y + tmp_out : output.y;
        output.z = (i == 2) ? output.z + tmp_out : output.z;
        output.w = (i == 3) ? output.w + tmp_out : output.w;
255 256
      }
    }
X
xiebaiyuan 已提交
257
  output = activation_type4(output);
258

X
xiebaiyuan 已提交
259
  WRITE_IMG_TYPE(CL_DTYPE_CHAR, output_image, output_pos, output);
260
}