forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathim2col_shape_check.h
226 lines (206 loc) · 6.57 KB
/
im2col_shape_check.h
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
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
#include <ATen/core/Tensor.h>
#include <ATen/TensorUtils.h>
namespace at {
namespace native {
static inline void col2im_shape_check(
const Tensor& input,
const Tensor& grad_output,
int64_t output_height,
int64_t output_width,
int64_t kernel_height,
int64_t kernel_width,
int64_t dilation_height,
int64_t dilation_width,
int64_t pad_height,
int64_t pad_width,
int64_t stride_height,
int64_t stride_width) {
TORCH_CHECK(
kernel_width > 0 && kernel_height > 0,
"kernel size should be greater than zero, but got kernel_height: ",
kernel_height,
" kernel_width: ",
kernel_width);
TORCH_CHECK(
stride_width > 0 && stride_height > 0,
"stride should be greater than zero, but got stride_height: ",
stride_height,
" stride_width: ",
stride_width);
TORCH_CHECK(
dilation_width > 0 && dilation_height > 0,
"dilation should be greater than zero, but got dilation_height: ",
dilation_height,
" dilation_width: ",
dilation_width);
int64_t ndim = input.ndimension();
// allow dim=0 only the batch dimension.
TORCH_CHECK(
(ndim == 2 && input.size(0) != 0 && input.size(1) != 0) ||
(ndim == 3 && input.size(1) != 0 && input.size(2) != 0),
"Expected 2D or 3D (batch mode) tensor for input with possibly 0 batch size and non-zero dimensions for input, but got: ",
input.sizes());
int64_t batch_dim = (ndim == 3) ? 0 : -1;
int64_t n_input_plane = input.size(batch_dim + 1);
if (n_input_plane % (kernel_width * kernel_height) != 0) {
AT_ERROR(
"Expected size of input's dimension 1 to be divisible by the "
"product of kernel_size, but got input.size(1)=",
n_input_plane,
" and kernel_size=(",
kernel_height,
", ",
kernel_width,
").");
}
int64_t input_length = input.size(batch_dim + 2);
int64_t n_blocks_height =
div_rtn<int64_t>(
output_height + 2 * pad_height -
dilation_height * (kernel_height - 1) - 1,
stride_height) +
1;
int64_t n_blocks_width = div_rtn<int64_t>(
output_width + 2 * pad_width -
dilation_width * (kernel_width - 1) - 1,
stride_width) +
1;
if (input_length != (n_blocks_height * n_blocks_width)) {
AT_ERROR(
"Given output_size=(",
output_height,
", ",
output_width,
"), kernel_size=(",
kernel_height,
", ",
kernel_width,
"), dilation=(",
dilation_height,
", ",
dilation_width,
"), padding=(",
pad_height,
", ",
pad_width,
"), stride=(",
stride_height,
", ",
stride_width,
"), expected size of input's dimension 2 to match the calculated number of ",
"sliding blocks ",
n_blocks_height,
" * ",
n_blocks_width,
" = ",
(n_blocks_height * n_blocks_width),
", but got input.size(2)=",
input_length,
".");
}
TORCH_CHECK(
n_blocks_height >= 1 && n_blocks_width >= 1,
"Given output_size=(", output_height, ", ", output_width, "), ",
"kernel_size=(", kernel_height, ", ", kernel_width, "), ",
"dilation=(", dilation_height, ", ", dilation_width, "), ",
"padding=(", pad_height, ", ", pad_width, "), ",
"stride=(", stride_height, ", ", stride_width, "), ",
"calculated shape of the array of sliding blocks as ",
"(", n_blocks_height, ", ", n_blocks_width, "), ",
"which is too small (non-positive)");
if (output_width < 1 || output_height < 1) {
AT_ERROR(
"Expected output spatial size to be positive, but got: output_size=(",
output_height,
", ",
output_width,
").");
}
}
static inline void im2col_shape_check(
const Tensor& input,
const Tensor& grad_output,
int64_t kernel_height,
int64_t kernel_width,
int64_t dilation_height,
int64_t dilation_width,
int64_t pad_height,
int64_t pad_width,
int64_t stride_height,
int64_t stride_width) {
TORCH_CHECK(
kernel_width > 0 && kernel_height > 0,
"kernel size should be greater than zero, but got kernel_height: ",
kernel_height,
" kernel_width: ",
kernel_width);
TORCH_CHECK(
dilation_width > 0 && dilation_height > 0,
"dilation should be greater than zero, but got dilation_height: ",
dilation_height,
" dilation_width: ",
dilation_width);
TORCH_CHECK(
pad_width >= 0 && pad_height >= 0,
"padding should be non-negative, but got pad_height: ",
pad_height,
" pad_width: ",
pad_width);
TORCH_CHECK(
stride_width > 0 && stride_height > 0,
"stride should be greater than zero, but got stride_height: ",
stride_height,
" stride_width: ",
stride_width);
int64_t ndim = input.ndimension();
// allow dim=0 only the batch dimension.
bool valid_dims = input.size(1) != 0 && input.size(2) != 0;
TORCH_CHECK(
(ndim == 3 && input.size(0) && valid_dims) ||
(ndim == 4 && valid_dims && input.size(3) != 0),
"Expected 3D or 4D (batch mode) tensor with possibly 0 batch size and other non-zero dimensions for input, but got: ",
input.sizes());
int64_t dim_batch = 0;
if (ndim == 3) {
dim_batch = -1;
}
int64_t input_height = input.size(dim_batch + 2);
int64_t input_width = input.size(dim_batch + 3);
int64_t output_height = div_rtn<int64_t>(
input_height + 2 * pad_height -
(dilation_height * (kernel_height - 1) + 1),
stride_height) +
1;
int64_t output_width = div_rtn<int64_t>(
input_width + 2 * pad_width -
(dilation_width * (kernel_width - 1) + 1),
stride_width) +
1;
if (output_height < 1 || output_width < 1) {
AT_ERROR(
"Given input with spatial size (",
input_height,
", ",
input_height,
"), kernel_size=(",
kernel_height,
", ",
kernel_width,
"), dilation=(",
dilation_height,
", ",
dilation_width,
"), padding=(",
pad_height,
", ",
pad_width,
"), calculated shape of the array of sliding blocks as (",
output_height,
", ",
output_width,
"), which is too small (non-positive).");
}
}
} // namespace native
} // namespace at