1 #ifndef THCUNN_IM2COL_H 2 #define THCUNN_IM2COL_H 4 #include <THCUNN/common.h> 5 #include <THC/THCNumerics.cuh> 6 #include <c10/macros/Macros.h> 10 template <
typename Dtype>
11 C10_LAUNCH_BOUNDS_1(CUDA_NUM_THREADS)
12 __global__
void im2col_kernel(const int64_t n, const Dtype* data_im,
13 const int64_t height, const int64_t width,
14 const int64_t ksize_h, const int64_t ksize_w,
15 const int64_t pad_h, const int64_t pad_w,
16 const int64_t stride_h, const int64_t stride_w,
17 const int64_t dilation_h, const int64_t dilation_w,
18 const int64_t height_col, const int64_t width_col,
20 CUDA_KERNEL_LOOP(index, n) {
21 int64_t w_out = index % width_col;
23 int64_t h_out = index % height_col;
24 int64_t channel_in = index / height_col;
25 int64_t channel_out = channel_in * ksize_h * ksize_w;
26 int64_t h_in = h_out * stride_h - pad_h;
27 int64_t w_in = w_out * stride_w - pad_w;
28 data_col += (channel_out * height_col + h_out) * width_col + w_out;
29 data_im += (channel_in * height + h_in) * width + w_in;
30 for (int64_t i = 0; i < ksize_h; ++i) {
31 for (int64_t j = 0; j < ksize_w; ++j) {
32 int64_t h = h_in + i * dilation_h;
33 int64_t w = w_in + j * dilation_w;
34 *data_col = (h >= 0 && w >= 0 && h < height && w < width) ?
35 data_im[i * dilation_h * width + j * dilation_w] : ScalarConvert<int, Dtype>::to(0);
36 data_col += height_col * width_col;
42 template <
typename Dtype>
43 void im2col(cudaStream_t stream,
const Dtype* data_im,
const int64_t channels,
44 const int64_t height,
const int64_t width,
45 const int64_t height_col,
const int64_t width_col,
46 const int64_t ksize_h,
const int64_t ksize_w,
const int64_t pad_h,
47 const int64_t pad_w,
const int64_t stride_h,
const int64_t stride_w,
48 const int64_t dilation_h,
const int64_t dilation_w, Dtype* data_col) {
51 int64_t num_kernels = channels * height_col * width_col;
53 im2col_kernel <<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS, 0, stream>>> (
54 num_kernels, data_im, height, width, ksize_h, ksize_w,
55 pad_h, pad_w, stride_h, stride_w,
56 dilation_h, dilation_w,
57 height_col, width_col, data_col
59 THCudaCheck(cudaGetLastError());
62 template <
typename Dtype,
typename Acctype>
63 C10_LAUNCH_BOUNDS_1(CUDA_NUM_THREADS)
64 __global__
void col2im_kernel(const int64_t n, const Dtype* data_col,
65 const int64_t height, const int64_t width, const int64_t channels,
66 const int64_t kernel_h, const int64_t kernel_w,
67 const int64_t pad_h, const int64_t pad_w,
68 const int64_t stride_h, const int64_t stride_w,
69 const int64_t dilation_h, const int64_t dilation_w,
70 const int64_t height_col, const int64_t width_col,
72 CUDA_KERNEL_LOOP(index, n) {
73 Acctype val = Acctype(0);
74 const int64_t w_im = index % width + pad_w;
75 const int64_t h_im = (index / width) % height + pad_h;
76 const int64_t c_im = index / (width * height);
77 int64_t kernel_extent_w = (kernel_w - 1) * dilation_w + 1;
78 int64_t kernel_extent_h = (kernel_h - 1) * dilation_h + 1;
80 const int64_t w_col_start =
81 (w_im < kernel_extent_w) ? 0 : (w_im - kernel_extent_w) / stride_w + 1;
82 const int64_t w_col_end = min(w_im / stride_w + 1, width_col);
83 const int64_t h_col_start =
84 (h_im < kernel_extent_h) ? 0 : (h_im - kernel_extent_h) / stride_h + 1;
85 const int64_t h_col_end = min(h_im / stride_h + 1, height_col);
87 for (int64_t h_col = h_col_start; h_col < h_col_end; h_col += 1) {
88 for (int64_t w_col = w_col_start; w_col < w_col_end; w_col += 1) {
89 int64_t h_k = (h_im - h_col * stride_h);
90 int64_t w_k = (w_im - w_col * stride_w);
91 if (h_k % dilation_h == 0 && w_k % dilation_w == 0) {
94 int64_t data_col_index = (((c_im * kernel_h + h_k) * kernel_w + w_k) *
95 height_col + h_col) * width_col + w_col;
96 val += data_col[data_col_index];
100 data_im[index] = ScalarConvert<Acctype, Dtype>::to(val);
105 template <
typename Dtype,
typename Acctype>
106 void col2im(cudaStream_t stream,
const Dtype* data_col,
const int64_t channels,
107 const int64_t height,
const int64_t width,
108 const int64_t output_height,
const int64_t output_width,
109 const int64_t patch_h,
const int64_t patch_w,
const int64_t pad_h,
110 const int64_t pad_w,
const int64_t stride_h,
const int64_t stride_w,
111 const int64_t dilation_h,
const int64_t dilation_w, Dtype* data_im) {
112 int64_t num_kernels = channels * height * width;
115 col2im_kernel<Dtype, Acctype> <<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS, 0, stream>>> (
116 num_kernels, data_col, height, width, channels,
117 patch_h, patch_w, pad_h, pad_w, stride_h, stride_w,
118 dilation_h, dilation_w,
119 output_height, output_width, data_im
121 THCudaCheck(cudaGetLastError());