im2col.cpp 21.4 KB
Newer Older
Z
zhaojiaying01 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
朔-望's avatar
朔-望 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Z
zhaojiaying01 已提交
15
#include "operators/math/im2col.h"
Z
zhaojiaying01 已提交
16
#include <vector>
E
eclipsess 已提交
17
#ifdef __ARM_NEON
L
liuruilong 已提交
18
#include <arm_neon.h>
E
eclipsess 已提交
19
#endif
朔-望's avatar
朔-望 已提交
20 21
#include "common/types.h"
namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
22 23
namespace operators {
namespace math {
朔-望's avatar
朔-望 已提交
24

朔-望's avatar
朔-望 已提交
25 26 27 28 29 30
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 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
template <>
void Im2ColFunctor<ColFormat::kCFO, CPU, float>::operator()(
    const framework::Tensor &im, const std::vector<int> &dilation,
    const std::vector<int> &stride, const std::vector<int> &padding,
    framework::Tensor *col) {
  int im_channels = im.dims()[0];
  int im_height = im.dims()[1];
  int im_width = im.dims()[2];
  int filter_height = col->dims()[1];
  int filter_width = col->dims()[2];
  int col_height = col->dims()[3];
  int col_width = col->dims()[4];

  int channels_col = im_channels * filter_height * filter_width;
  const float *im_data = im.data<float>();
  float *col_data = col->data<float>();
#if __ARM_NEON
  const int osize = col_height;
  const int isize = im_height;
  bool pad1 = padding[0] > 0;
  bool pad2 =
      (pad1 && padding[1] &&
       (((isize - 2 * padding[0] + filter_height) % stride[0] == 0) ? 1 : 0));
  int fill = isize % 2;
  if (stride[0] == 1 && filter_height == 3 && pad1 && pad2 &&
      dilation[0] == 1 && im_height > 2) {
    for (int c = 0; c < im_channels; ++c) {
      int oosize = osize * osize;
      int nk4 = osize / 4;
      int mk4 = osize % 4;

      float *col0 = col_data + 0 * oosize + 2 * osize + 2;
      float *col1 = col_data + 1 * oosize + 2 * osize + 1;
      float *col2 = col_data + 2 * oosize + 2 * osize;

      float *col3 = col_data + 3 * oosize + osize + 2;
      float *col4 = col_data + 4 * oosize + osize + 1;
      float *col5 = col_data + 5 * oosize + osize;

      float *col6 = col_data + 6 * oosize + 2;
      float *col7 = col_data + 7 * oosize + 1;
      float *col8 = col_data + 8 * oosize;

      float32x4_t im1;
      const float *im_tmp_data = im_data + osize + 1;

      int rrsize = oosize - osize - 1;
      int nr4 = rrsize / 4;
      int mr4 = rrsize % 4;
      for (int i = 0; i < nr4; ++i) {
        im1 = vld1q_f32(im_tmp_data);
        vst1q_f32(col0, im1);
        vst1q_f32(col1, im1);
        vst1q_f32(col2, im1);
        vst1q_f32(col3, im1);
        vst1q_f32(col4, im1);
        vst1q_f32(col5, im1);
        vst1q_f32(col6, im1);
        vst1q_f32(col7, im1);
        vst1q_f32(col8, im1);

        col0 += 4;
        col1 += 4;
        col2 += 4;
        col3 += 4;
        col4 += 4;
        col5 += 4;
        col6 += 4;
        col7 += 4;
        col8 += 4;

        im_tmp_data += 4;
      }
      for (int i = 0; i < mr4; ++i) {
        *col0 = *im_tmp_data;
        *col1 = *im_tmp_data;
        *col2 = *im_tmp_data;
        *col3 = *im_tmp_data;
        *col4 = *im_tmp_data;
        *col5 = *im_tmp_data;
        *col6 = *im_tmp_data;
        *col7 = *im_tmp_data;
        *col8 = *im_tmp_data;

        col0++;
        col1++;
        col2++;
        col3++;
        col4++;
        col5++;
        col6++;
        col7++;
        col8++;

        im_tmp_data++;
      }
朔-望's avatar
朔-望 已提交
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
      im_tmp_data = im_data + 1;
      col0 = col_data + 0 * oosize + osize + 2;
      col1 = col_data + 1 * oosize + osize + 1;
      col2 = col_data + 2 * oosize + osize;

      col3 = col_data + 3 * oosize + 2;
      col4 = col_data + 4 * oosize + 1;
      col5 = col_data + 5 * oosize;

      for (int i = 0; i < nk4; i++) {
        im1 = vld1q_f32(im_tmp_data);
        vst1q_f32(col0, im1);
        vst1q_f32(col1, im1);
        vst1q_f32(col2, im1);
        vst1q_f32(col3, im1);
        vst1q_f32(col4, im1);
        vst1q_f32(col5, im1);

        col0 += 4;
        col1 += 4;
        col2 += 4;
        col3 += 4;
        col4 += 4;
        col5 += 4;
        im_tmp_data += 4;
      }
朔-望's avatar
朔-望 已提交
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 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
      for (int i = 0; i < mk4; i++) {
        *col0 = *im_tmp_data;
        *col1 = *im_tmp_data;
        *col2 = *im_tmp_data;
        *col3 = *im_tmp_data;
        *col4 = *im_tmp_data;
        *col5 = *im_tmp_data;
        col0++;
        col1++;
        col2++;
        col3++;
        col4++;
        col5++;

        im_tmp_data++;
      }

      // fill 0 1 11;
      for (int i = 0; i < osize; ++i) {
        col_data[0 * oosize + i * osize] = 0.0;
        col_data[3 * oosize + i * osize] = 0.0;
        col_data[6 * oosize + i * osize] = 0.0;

        col_data[2 * oosize + osize - 1 + i * osize] = 0.0;
        col_data[5 * oosize + osize - 1 + i * osize] = 0.0;
        col_data[8 * oosize + osize - 1 + i * osize] = 0.0;
      }

      col_data[0 * oosize + osize + 1] = im_data[0];
      col_data[3 * oosize + 1] = im_data[0];
      col_data[6 * oosize + 1] = im_data[osize];

      col_data[1 * oosize + osize] = im_data[0];
      col_data[4 * oosize] = im_data[0];
      col_data[7 * oosize] = im_data[osize];

      float32x4_t zero4;
      zero4 = vdupq_n_f32(0.0);
      auto col_z0 = col_data;
      auto col_z1 = col_data + oosize;
      auto col_z2 = col_data + 2 * oosize;
      auto col_z6 = col_data + 6 * oosize + osize * (osize - 1);
      auto col_z7 = col_data + 7 * oosize + osize * (osize - 1);
      auto col_z8 = col_data + 8 * oosize + osize * (osize - 1);

      for (int i = 0; i < nk4; ++i) {
        vst1q_f32(col_z0, zero4);
        vst1q_f32(col_z1, zero4);
        vst1q_f32(col_z2, zero4);
        vst1q_f32(col_z6, zero4);
        vst1q_f32(col_z7, zero4);
        vst1q_f32(col_z8, zero4);

        col_z0 += 4;
        col_z1 += 4;
        col_z2 += 4;
        col_z6 += 4;
        col_z7 += 4;
        col_z8 += 4;
      }

      for (int i = 0; i < mk4; ++i) {
        col_z0[i] = 0.0;
        col_z1[i] = 0.0;
        col_z2[i] = 0.0;
        col_z6[i] = 0.0;
        col_z7[i] = 0.0;
        col_z8[i] = 0.0;
      }
      col_data += 9 * oosize;
      im_data += isize * isize;
    }
  } else if (stride[0] == 2 && filter_height == 3 && pad1 && dilation[0] == 1 &&
             im_height > 2) {
    for (int c = 0; c < im_channels; ++c) {
      int oosize = osize * osize;
      int nk4 = osize / 4;
      int mk4 = osize % 4;

      // 3 2 3 1 0 1 3 2 3
      float *col0 = col_data + 0 * oosize + osize + 1;
      float *col1 = col_data + 1 * oosize + osize;
      float *col2 = col_data + 2 * oosize + osize;

      float *col3 = col_data + 3 * oosize + 1;
      float *col4 = col_data + 4 * oosize;
      float *col5 = col_data + 5 * oosize;

      float *col6 = col_data + 6 * oosize + 1;
      float *col7 = col_data + 7 * oosize;
      float *col8 = col_data + 8 * oosize;

      float32x4x2_t im01;
      float32x4x2_t im23;
      const float *im_tmp_data0 = im_data;
      const float *im_tmp_data2 = im_data + isize;

      for (int j = 0; j < osize; ++j) {
        for (int i = 0; i < nk4; ++i) {
          im01 = vld2q_f32(im_tmp_data0);
          im23 = vld2q_f32(im_tmp_data2);
          vst1q_f32(col0, im23.val[1]);
          vst1q_f32(col1, im23.val[0]);
          vst1q_f32(col2, im23.val[1]);
          vst1q_f32(col3, im01.val[1]);
          vst1q_f32(col4, im01.val[0]);
          vst1q_f32(col5, im01.val[1]);
          vst1q_f32(col6, im23.val[1]);
          vst1q_f32(col7, im23.val[0]);
          vst1q_f32(col8, im23.val[1]);
E
eclipsess 已提交
265 266 267 268 269 270 271 272 273 274 275

          col0 += 4;
          col1 += 4;
          col2 += 4;
          col3 += 4;
          col4 += 4;
          col5 += 4;
          col6 += 4;
          col7 += 4;
          col8 += 4;

276 277
          im_tmp_data0 += 8;
          im_tmp_data2 += 8;
E
eclipsess 已提交
278
        }
279 280 281 282 283 284 285 286 287 288 289 290
        const float *im_tmp_data1 = im_tmp_data0 + 1;
        const float *im_tmp_data3 = im_tmp_data2 + 1;
        for (int i = 0; i < mk4; ++i) {
          *col0 = *im_tmp_data3;
          *col1 = *im_tmp_data2;
          *col2 = *im_tmp_data3;
          *col3 = *im_tmp_data1;
          *col4 = *im_tmp_data0;
          *col5 = *im_tmp_data1;
          *col6 = *im_tmp_data3;
          *col7 = *im_tmp_data2;
          *col8 = *im_tmp_data3;
E
eclipsess 已提交
291 292 293 294 295 296 297 298 299 300

          col0++;
          col1++;
          col2++;
          col3++;
          col4++;
          col5++;
          col6++;
          col7++;
          col8++;
301 302 303 304
          im_tmp_data0 += 2;
          im_tmp_data1 += 2;
          im_tmp_data2 += 2;
          im_tmp_data3 += 2;
E
eclipsess 已提交
305
        }
306 307 308 309 310 311 312 313
        im_tmp_data0 += (isize - fill);
        im_tmp_data2 += (isize - fill);
      }
      for (int i = 0; i < osize; ++i) {
        col_data[0 * oosize + i * osize] = 0.0;
        col_data[3 * oosize + i * osize] = 0.0;
        col_data[6 * oosize + i * osize] = 0.0;
        if (pad2) {
E
eclipsess 已提交
314 315 316 317
          col_data[2 * oosize + osize - 1 + i * osize] = 0.0;
          col_data[5 * oosize + osize - 1 + i * osize] = 0.0;
          col_data[8 * oosize + osize - 1 + i * osize] = 0.0;
        }
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      }
      float32x4_t zero4;
      zero4 = vdupq_n_f32(0.0);
      auto col_z0 = col_data;
      auto col_z1 = col_data + oosize;
      auto col_z2 = col_data + 2 * oosize;
      auto col_z6 = col_data + 6 * oosize + osize * (osize - 1);
      auto col_z7 = col_data + 7 * oosize + osize * (osize - 1);
      auto col_z8 = col_data + 8 * oosize + osize * (osize - 1);

      for (int i = 0; i < nk4; ++i) {
        vst1q_f32(col_z0, zero4);
        vst1q_f32(col_z1, zero4);
        vst1q_f32(col_z2, zero4);
        if (pad2) {
E
eclipsess 已提交
333 334 335 336
          vst1q_f32(col_z6, zero4);
          vst1q_f32(col_z7, zero4);
          vst1q_f32(col_z8, zero4);
        }
337 338 339 340 341 342 343
        col_z0 += 4;
        col_z1 += 4;
        col_z2 += 4;
        col_z6 += 4;
        col_z7 += 4;
        col_z8 += 4;
      }
E
eclipsess 已提交
344

345 346 347 348 349
      for (int i = 0; i < mk4; ++i) {
        col_z0[i] = 0.0;
        col_z1[i] = 0.0;
        col_z2[i] = 0.0;
        if (pad2) {
E
eclipsess 已提交
350 351 352 353 354 355
          col_z6[i] = 0.0;
          col_z7[i] = 0.0;
          col_z8[i] = 0.0;
        }
      }

356 357 358
      col_data[1 * oosize + osize] = im_data[isize];
      for (int i = 1; i < osize; ++i) {
        col_data[3 * oosize + i] = im_data[(i - 1) * stride[0] + 1];
359
      }
360 361 362 363 364
      col_data[4 * oosize] = im_data[0];
      col_data[7 * oosize] = im_data[isize];

      col_data += 9 * oosize;
      im_data += isize * isize;
朔-望's avatar
朔-望 已提交
365
    }
366
  } else {
E
eclipsess 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379
    for (int c = 0; c < channels_col; ++c) {
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int c_im = c / (filter_width * filter_height);
      for (int h = 0; h < col_height; ++h) {
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
        for (int w = 0; w < col_width; ++w) {
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
          int col_idx = (c * col_height + h) * col_width + w;
          int im_idx = (im_row_idx + c_im * im_height) * im_width + im_col_idx;

          col_data[col_idx] = (im_row_idx < 0 || im_row_idx >= im_height ||
                               im_col_idx < 0 || im_col_idx >= im_width)
380
                                  ? static_cast<float>(0)
E
eclipsess 已提交
381 382 383 384
                                  : im_data[im_idx];
        }
      }
    }
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  }
#else
  for (int c = 0; c < channels_col; ++c) {
    int w_offset = c % filter_width;
    int h_offset = (c / filter_width) % filter_height;
    int c_im = c / (filter_width * filter_height);
    for (int h = 0; h < col_height; ++h) {
      int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
      for (int w = 0; w < col_width; ++w) {
        int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
        int col_idx = (c * col_height + h) * col_width + w;
        int im_idx = (im_row_idx + c_im * im_height) * im_width + im_col_idx;

        col_data[col_idx] = (im_row_idx < 0 || im_row_idx >= im_height ||
                             im_col_idx < 0 || im_col_idx >= im_width)
                                ? static_cast<T>(0)
                                : im_data[im_idx];
      }
    }
  }
E
eclipsess 已提交
405
#endif
406 407 408 409 410 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
}

// TODO(hjchen2)
void ExtractToRows1() {}

void ExtractToRows2() {}

/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
template <>
void Im2ColFunctor<ColFormat::kCFO, CPU, int8_t>::operator()(
    const framework::Tensor &im, const std::vector<int> &dilation,
    const std::vector<int> &stride, const std::vector<int> &padding,
    framework::Tensor *col) {
  int im_channels = im.dims()[0];
  int im_height = im.dims()[1];
  int im_width = im.dims()[2];
  int filter_height = col->dims()[1];
  int filter_width = col->dims()[2];
  int col_height = col->dims()[3];
  int col_width = col->dims()[4];

  int channels_col = im_channels * filter_height * filter_width;
  const int8_t *im_data = im.data<int8_t>();
  int8_t *col_data = col->data<int8_t>();
// #if defined(__ARM_NEON__) || defined(__ARM_NEON)
#if 0
  if (stride[0] == stride[1] && stride[0] == 1 && dilation[0] == 1 &&
      padding[0] == padding[1] && dilation[0] == dilation[1]) {
    // pad 0
    memset(col_data, 0, col->numel() * sizeof(int8_t));
    for (int ic = 0; ic < im_channels; ++ic) {
      for (int oh = 0; oh < padding[0]; ++oh) {
        for (int k = 0; k < filter_height * filter_width; ++k) {
          ExtractToRows1();
          ExtractToRows1();
        }
      }
      for (int oh = padding[0]; oh < col_height - padding[0]; ++oh) {
        for (int k = 0; k < filter_height * filter_width; ++k) {
          ExtractToRows1();
        }
      }
    }
  } else if (stride[0] == stride[1] && stride[0] == 2 && dilation[0] == 1 &&
             padding[0] == padding[1] && dilation[0] == dilation[1]) {
    // pad 0
    memset(col_data, 0, col->numel() * sizeof(int8_t));
    for (int ic = 0; ic < im_channels; ++ic) {
      for (int oh = 0; oh < padding[0]; ++oh) {
        for (int k = 0; k < filter_height * filter_width; ++k) {
          ExtractToRows2();
          ExtractToRows2();
        }
      }
      for (int oh = padding[0]; oh < col_height - padding[0]; ++oh) {
        for (int k = 0; k < filter_height * filter_width; ++k) {
          ExtractToRows2();
        }
      }
    }
  } else {
#endif
  for (int c = 0; c < channels_col; ++c) {
    int w_offset = c % filter_width;
    int h_offset = (c / filter_width) % filter_height;
    int c_im = c / (filter_width * filter_height);
    for (int h = 0; h < col_height; ++h) {
      int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
      for (int w = 0; w < col_width; ++w) {
        int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
        int col_idx = (c * col_height + h) * col_width + w;
        int im_idx = (im_row_idx + c_im * im_height) * im_width + im_col_idx;

        col_data[col_idx] = (im_row_idx < 0 || im_row_idx >= im_height ||
                             im_col_idx < 0 || im_col_idx >= im_width)
                                ? static_cast<int8_t>(0)
                                : im_data[im_idx];
      }
    }
490
  }
491 492 493 494 495
// #if defined(__ARM_NEON__) || defined(__ARM_NEON)
#if 0
  }
#endif
}
朔-望's avatar
朔-望 已提交
496

朔-望's avatar
朔-望 已提交
497 498 499 500 501 502
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
朔-望's avatar
朔-望 已提交
503 504 505
template <class T>
class Col2ImFunctor<ColFormat::kCFO, CPU, T> {
 public:
506 507 508 509 510 511 512 513 514 515 516 517 518
  void operator()(const framework::Tensor &col,
                  const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *im) {
    //    PADDLE_ENFORCE(im->dims().size() == 3);
    //    PADDLE_ENFORCE(col.dims().size() == 5);
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
    int filter_height = col.dims()[1];
    int filter_width = col.dims()[2];
    int col_height = col.dims()[3];
    int col_width = col.dims()[4];
朔-望's avatar
朔-望 已提交
519

520
    int channels_col = im_channels * filter_height * filter_width;
朔-望's avatar
朔-望 已提交
521

522 523
    T *im_data = im->data<T>();
    const T *col_data = col.data<T>();
524
    memset(static_cast<void *>(im_data), 0, sizeof(T) * im->numel());
朔-望's avatar
朔-望 已提交
525

526 527 528 529 530 531 532 533 534 535 536 537 538
    for (int c = 0; c < channels_col; ++c) {
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int c_im = c / (filter_width * filter_height);
      for (int h = 0; h < col_height; ++h) {
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
        for (int w = 0; w < col_width; ++w) {
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
          if ((im_row_idx) >= 0 && (im_row_idx) < im_height &&
              (im_col_idx) >= 0 && (im_col_idx) < im_width) {
            im_data[(im_row_idx + c_im * im_height) * im_width + im_col_idx] +=
                col_data[(c * col_height + h) * col_width + w];
          }
朔-望's avatar
朔-望 已提交
539
        }
540
      }
朔-望's avatar
朔-望 已提交
541
    }
542
  }
朔-望's avatar
朔-望 已提交
543
};
朔-望's avatar
朔-望 已提交
544

朔-望's avatar
朔-望 已提交
545
template class Im2ColFunctor<ColFormat::kCFO, CPU, float>;
546
template class Im2ColFunctor<ColFormat::kCFO, CPU, int8_t>;
朔-望's avatar
朔-望 已提交
547
template class Col2ImFunctor<ColFormat::kCFO, CPU, float>;
548
template class Col2ImFunctor<ColFormat::kCFO, CPU, int8_t>;
朔-望's avatar
朔-望 已提交
549

朔-望's avatar
朔-望 已提交
550 551 552 553 554 555
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
556 557 558
template <class T>
class Im2ColFunctor<ColFormat::kOCF, CPU, T> {
 public:
559 560 561 562 563 564 565 566 567 568
  void operator()(const framework::Tensor &im, const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *col) {
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
    int filter_height = col->dims()[3];
    int filter_width = col->dims()[4];
    int col_height = col->dims()[0];
    int col_width = col->dims()[1];
朔-望's avatar
朔-望 已提交
569

570 571
    const T *im_data = im.data<T>();
    T *col_data = col->data<T>();
朔-望's avatar
朔-望 已提交
572

573 574 575 576 577 578 579 580 581 582 583
    for (int col_row_idx = 0; col_row_idx < col_height; ++col_row_idx) {
      for (int col_col_idx = 0; col_col_idx < col_width; ++col_col_idx) {
        for (int channel = 0; channel < im_channels; ++channel) {
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
朔-望's avatar
朔-望 已提交
584

585 586 587 588 589 590 591
              int col_offset =
                  ((((col_row_idx)*col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;
朔-望's avatar
朔-望 已提交
592

593 594 595 596 597 598 599
              int im_offset = (channel * im_height + im_row_offset) * im_width +
                              im_col_offset;
              col_data[col_offset] =
                  (im_row_offset < 0 || im_row_offset >= im_height ||
                   im_col_offset < 0 || im_col_offset >= im_width)
                      ? static_cast<T>(0)
                      : im_data[im_offset];
朔-望's avatar
朔-望 已提交
600
            }
601
          }
朔-望's avatar
朔-望 已提交
602
        }
603
      }
朔-望's avatar
朔-望 已提交
604
    }
605
  }
朔-望's avatar
朔-望 已提交
606
};
朔-望's avatar
朔-望 已提交
607

朔-望's avatar
朔-望 已提交
608 609 610 611 612 613
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
614 615 616
template <class T>
class Col2ImFunctor<ColFormat::kOCF, CPU, T> {
 public:
617 618 619 620 621 622 623 624 625 626 627
  void operator()(const framework::Tensor &col,
                  const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *im) {
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
    int filter_height = col.dims()[3];
    int filter_width = col.dims()[4];
    int col_height = col.dims()[0];
    int col_width = col.dims()[1];
朔-望's avatar
朔-望 已提交
628

629 630
    T *im_data = im->data<T>();
    const T *col_data = col.data<T>();
朔-望's avatar
朔-望 已提交
631

632 633 634 635 636 637 638 639 640 641 642
    for (int col_row_idx = 0; col_row_idx < col_height; ++col_row_idx) {
      for (int col_col_idx = 0; col_col_idx < col_width; ++col_col_idx) {
        for (int channel = 0; channel < im_channels; ++channel) {
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
朔-望's avatar
朔-望 已提交
643

644 645 646 647 648 649 650
              int col_offset =
                  (((col_row_idx * col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;
朔-望's avatar
朔-望 已提交
651

652 653 654 655 656 657 658
              if (im_row_offset >= 0 && im_row_offset < im_height &&
                  im_col_offset >= 0 && im_col_offset < im_width) {
                int im_offset =
                    (channel * im_height + im_row_offset) * im_width +
                    im_col_offset;
                im_data[im_offset] += col_data[col_offset];
              }
朔-望's avatar
朔-望 已提交
659
            }
660
          }
朔-望's avatar
朔-望 已提交
661
        }
662
      }
朔-望's avatar
朔-望 已提交
663
    }
664
  }
朔-望's avatar
朔-望 已提交
665
};
朔-望's avatar
朔-望 已提交
666

朔-望's avatar
朔-望 已提交
667 668
template class Im2ColFunctor<ColFormat::kOCF, CPU, float>;
template class Col2ImFunctor<ColFormat::kOCF, CPU, float>;
朔-望's avatar
朔-望 已提交
669

朔-望's avatar
朔-望 已提交
670 671 672
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile