conv2d_3x3_kernel.cl 22.2 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 33
                         __private const int output_width,
                         __private const int output_height,
                         __private const int output_c,
                         __private const int filter_channel,
X
xiebaiyuan 已提交
34 35 36 37
                         __private const int filter_width,
                         __private const int filter_height,
                         __private const int group,
                         __private const int input_tensor_c
38

X
xiebaiyuan 已提交
39
) {
40

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

X
xiebaiyuan 已提交
45 46
  const sampler_t sampler =
      CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
47

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

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

X
xiebaiyuan 已提交
55 56 57
  int2 stride_xy;
  stride_xy.x = stride;
  stride_xy.y = stride;
58

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

X
xiebaiyuan 已提交
63 64 65
  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;
66 67

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

X
xiebaiyuan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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
  CL_DTYPE4 input[9];  // 3x3 region of input
  if (group == 1) {
    for (int i = 0; i < input_c; ++i) {  // each run for 3x3
      int2 pos_in = (int2)(i * input_width + in_pos_in_one_block.x,
                           in_pos_in_one_block.y);

      input[0] = select(
          READ_IMG_TYPE(CL_DTYPE_CHAR,
                        input_image,
                        sampler,
                        (int2)(pos_in.x - dilation, pos_in.y - dilation)),
          (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
          (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));

      input[1] =
          select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                               input_image,
                               sampler,
                               (int2)(pos_in.x, pos_in.y - dilation)),
                 (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
                 (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));

      input[2] = select(
          READ_IMG_TYPE(CL_DTYPE_CHAR,
                        input_image,
                        sampler,
                        (int2)(pos_in.x + dilation, pos_in.y - dilation)),
          (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
          (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));

      input[3] =
          select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                               input_image,
                               sampler,
                               (int2)(pos_in.x - dilation, pos_in.y)),
                 (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
                 (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));

      input[4] = select(
          READ_IMG_TYPE(
              CL_DTYPE_CHAR, input_image, sampler, (int2)(pos_in.x, pos_in.y)),
          (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
          (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));

      input[5] =
          select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                               input_image,
                               sampler,
                               (int2)(pos_in.x + dilation, pos_in.y)),
                 (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
                 (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));

      input[6] = select(
          READ_IMG_TYPE(CL_DTYPE_CHAR,
                        input_image,
                        sampler,
                        (int2)(pos_in.x - dilation, pos_in.y + dilation)),
          (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
          (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));

      input[7] =
          select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                               input_image,
                               sampler,
                               (int2)(pos_in.x, pos_in.y + dilation)),
                 (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
                 (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));

      input[8] = select(
          READ_IMG_TYPE(CL_DTYPE_CHAR,
                        input_image,
                        sampler,
                        (int2)(pos_in.x + dilation, pos_in.y + dilation)),
          (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
          (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));

      if (i == input_c - 1) {
        int c_shr = input_tensor_c % 4;
        if (c_shr == 1) {
          for (int k = 0; k < 9; k++) {
            input[k].y = (half)0.f;
            input[k].z = (half)0.f;
            input[k].w = (half)0.f;
          }
        } else if (c_shr == 2) {
          for (int k = 0; k < 9; k++) {
            input[k].z = (half)0.f;
            input[k].w = (half)0.f;
          }
        } else if (c_shr == 3) {
          for (int k = 0; k < 9; k++) {
            input[k].w = (half)0.f;
          }
        } else if (c_shr == 0) {
205
        }
X
xiebaiyuan 已提交
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
      }

      int j = 0;
      int2 pos_of_weight;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      CL_DTYPE4 weight_x =
          READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y += 3;
      CL_DTYPE4 weight_y =
          READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y += 3;
      CL_DTYPE4 weight_z =
          READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y += 3;
      CL_DTYPE4 weight_w =
          READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 1;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 2;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 3;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 4;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 5;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 6;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 7;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);

      j = 8;
      pos_of_weight.x = i * 3 + j % 3;
      pos_of_weight.y = out_c * 4 * 3 + 0 * 3 + j / 3;
      weight_x = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.x += dot(input[j], weight_x);

      pos_of_weight.y = out_c * 4 * 3 + 1 * 3 + j / 3;
      weight_y = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.y += dot(input[j], weight_y);

      pos_of_weight.y = out_c * 4 * 3 + 2 * 3 + j / 3;
      weight_z = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.z += dot(input[j], weight_z);

      pos_of_weight.y = out_c * 4 * 3 + 3 * 3 + j / 3;
      weight_w = READ_IMG_TYPE(CL_DTYPE_CHAR, filter, sampler, pos_of_weight);
      output.w += dot(input[j], weight_w);
    }
  } else {  // group != 1
    for (int i = 0; i < 4; i++) {
      int used_input_channel_num =
378
          (out_c * 4 + i) / (output_c / group) * filter_channel;
X
xiebaiyuan 已提交
379 380 381 382 383 384 385 386 387
      for (int f_c = 0; f_c < filter_channel; ++f_c) {
        int input_c = used_input_channel_num + f_c;
        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);
        input[0] = select(
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
388
                          (int2)(pos_in.x - dilation, pos_in.y - dilation)),
X
xiebaiyuan 已提交
389 390 391 392 393 394 395 396 397 398
            (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
            (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));
        input[1] =
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
399
                                 (int2)(pos_in.x, pos_in.y - dilation)),
X
xiebaiyuan 已提交
400 401 402 403 404 405 406 407 408 409
                   (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
                   (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));
        input[2] = select(
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
410
                          (int2)(pos_in.x + dilation, pos_in.y - dilation)),
X
xiebaiyuan 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
            (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
            (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));
        input[3] =
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
                                 (int2)(pos_in.x - dilation, pos_in.y)),
                   (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
                   (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));
        input[4] = select(
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
                          (int2)(pos_in.x, pos_in.y)),
            (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
            (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));
        input[5] =
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
                                 (int2)(pos_in.x + dilation, pos_in.y)),
                   (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
444 445 446 447 448
                   (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));
X
xiebaiyuan 已提交
449 450 451 452
        input[6] = select(
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
453
                          (int2)(pos_in.x - dilation, pos_in.y + dilation)),
X
xiebaiyuan 已提交
454 455 456 457 458 459 460 461 462 463
            (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
            (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));
        input[7] =
            select(READ_IMG_TYPE(CL_DTYPE_CHAR,
                                 input_image,
                                 sampler,
464
                                 (int2)(pos_in.x, pos_in.y + dilation)),
X
xiebaiyuan 已提交
465 466 467 468 469 470 471 472 473 474
                   (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
                   (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));
        input[8] = select(
            READ_IMG_TYPE(CL_DTYPE_CHAR,
                          input_image,
                          sampler,
475
                          (int2)(pos_in.x + dilation, pos_in.y + dilation)),
X
xiebaiyuan 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
            (CL_DTYPE4)(0.0f, 0.0f, 0.0f, 0.0f),
            (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;
          pos_of_weight.x = (f_c / 4) * 3 + j % 3;
          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);

          int f_c_offset = f_c % 4;
          CL_DTYPE f_value;
          if (f_c_offset == 0) {
            f_value = weight.x;
          } else if (f_c_offset == 1) {
            f_value = weight.y;
          } else if (f_c_offset == 2) {
            f_value = weight.z;
          } else if (f_c_offset == 3) {
            f_value = weight.w;
501 502
          }

X
xiebaiyuan 已提交
503 504 505 506 507 508 509 510 511 512
          int input_c_offset = input_c % 4;
          CL_DTYPE input_value;
          if (input_c_offset == 0) {
            input_value = input[j].x;
          } else if (input_c_offset == 1) {
            input_value = input[j].y;
          } else if (input_c_offset == 2) {
            input_value = input[j].z;
          } else if (input_c_offset == 3) {
            input_value = input[j].w;
513
          }
X
xiebaiyuan 已提交
514 515 516 517 518 519 520 521 522 523 524
          tmp_out += f_value * input_value;
        }

        if (i == 0) {
          output.x += tmp_out;
        } else if (i == 1) {
          output.y += tmp_out;
        } else if (i == 2) {
          output.z += tmp_out;
        } else if (i == 3) {
          output.w += tmp_out;
525 526 527
        }
      }
    }
X
xiebaiyuan 已提交
528
  }
529

X
xiebaiyuan 已提交
530
  output = activation_type4(output);
531

X
xiebaiyuan 已提交
532
  WRITE_IMG_TYPE(CL_DTYPE_CHAR, output_image, output_pos, output);
533
}