forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvPrepack.cpp
281 lines (254 loc) · 8.12 KB
/
ConvPrepack.cpp
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
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
#include <vector>
#include <ATen/native/ConvUtils.h>
#include <ATen/native/mkldnn/Common.h>
#include <ATen/native/mkldnn/ConvPrepack.h>
#include <ATen/native/mkldnn/MKLDNNCommon.h>
#include <ATen/native/mkldnn/OpContext.h>
#include <ATen/native/utils/Factory.h>
#include <ATen/native/utils/ParamUtils.h>
#include <c10/util/irange.h>
#if AT_MKLDNN_ENABLED()
namespace at::native::mkldnn::internal::convolution {
c10::intrusive_ptr<mkldnn::ConvOpContext> createConvPrePackOpContext(
Tensor weight,
std::optional<Tensor> bias,
std::vector<int64_t> stride,
std::vector<int64_t> padding,
std::vector<int64_t> dilation,
int64_t groups,
std::vector<int64_t> input_size,
std::string attr) {
auto it = fusion_attr_map.find(attr);
TORCH_CHECK(it != fusion_attr_map.end(), "Fusion behavior undefined.");
ideep::attr_t op_attr = it->second;
return mkldnn::MkldnnConvOpContext::create_context(
std::move(weight),
std::move(bias),
std::move(padding),
std::move(stride),
std::move(dilation),
groups,
std::move(input_size),
op_attr);
}
ContextConv create(
const Tensor& weight,
const std::optional<Tensor>& bias,
const IntArrayRef padding,
const IntArrayRef stride,
const IntArrayRef dilation,
const int64_t groups,
const IntArrayRef input_size,
const ideep::attr_t& attr) {
auto k = weight.ndimension();
int64_t dim = k - 2;
const auto padding_expanded = expand_param_if_needed(padding, "padding", dim);
const auto stride_expanded = expand_param_if_needed(stride, "stride", dim);
const auto dilation_expanded =
expand_param_if_needed(dilation, "dilation", dim);
const auto input_size_expanded =
expand_param_if_needed(input_size, "input_size", k);
c10::impl::ExcludeDispatchKeyGuard edkg(c10::autograd_dispatch_keyset);
auto w = itensor_view_from_dense(weight);
// TODO: what if input is nhwc but w is nchw
bool is_channels_last =
weight.suggest_memory_format() == at::MemoryFormat::ChannelsLast;
ideep::tensor::desc expected_weight_desc =
ideep::convolution_forward::expected_weights_desc(
w.get_dims(),
w.get_data_type(),
{stride_expanded.begin(), stride_expanded.end()},
{padding_expanded.begin(), padding_expanded.end()},
{padding_expanded.begin(), padding_expanded.end()},
{dilation_expanded.begin(), dilation_expanded.end()},
groups,
ideep::algorithm::convolution_direct,
ideep::prop_kind::forward,
/*x_dtype*/ w.get_data_type(),
{input_size_expanded.begin(), input_size_expanded.end()},
attr,
is_channels_last);
ideep::tensor packed_weight;
packed_weight.init(expected_weight_desc);
packed_weight.feed_from(w);
return ContextConv{
std::move(packed_weight),
bias,
{padding_expanded.begin(), padding_expanded.end()},
{stride_expanded.begin(), stride_expanded.end()},
{dilation_expanded.begin(), dilation_expanded.end()},
groups,
attr};
}
static void _mkldnn_convolution_out(
const ideep::tensor& x,
ideep::tensor& y,
const ideep::tensor& w,
const std::optional<ideep::tensor>& b,
IntArrayRef padding,
IntArrayRef stride,
IntArrayRef dilation,
IntArrayRef output_sizes,
int64_t groups,
const ideep::attr_t& attr = ideep::attr_t()) {
if (b.has_value()) {
ideep::convolution_forward::compute_v2(
x,
w,
b.value(),
{output_sizes.cbegin(), output_sizes.cend()},
y,
{stride.begin(), stride.end()},
{dilation.begin(), dilation.end()},
{padding.begin(), padding.end()},
{padding.begin(), padding.end()},
groups,
ideep::scale_t(),
ideep::scale_t(),
ideep::scale_t(),
ideep::zero_point_t(),
ideep::zero_point_t(),
attr);
} else {
ideep::convolution_forward::compute_v2(
x,
w,
{output_sizes.cbegin(), output_sizes.cend()},
y,
{stride.begin(), stride.end()},
{dilation.begin(), dilation.end()},
{padding.begin(), padding.end()},
{padding.begin(), padding.end()},
groups,
ideep::scale_t(),
ideep::scale_t(),
ideep::scale_t(),
ideep::zero_point_t(),
ideep::zero_point_t(),
attr);
}
}
static void mkldnn_convolution_out(
const Tensor& input,
ideep::tensor& mkldnn_output,
const ideep::tensor& mkldnn_weight,
const std::optional<Tensor>& bias_opt,
IntArrayRef padding,
IntArrayRef stride,
IntArrayRef dilation,
IntArrayRef output_sizes,
int64_t groups,
const ideep::attr_t& attr = ideep::attr_t()) {
c10::MaybeOwned<Tensor> bias_maybe_owned =
at::borrow_from_optional_tensor(bias_opt);
const Tensor& bias = *bias_maybe_owned;
c10::impl::ExcludeDispatchKeyGuard edkg(c10::autograd_dispatch_keyset);
const ideep::tensor mkldnn_input = itensor_from_tensor(input);
std::optional<ideep::tensor> mkldnn_bias{std::nullopt};
if (bias.defined()) {
mkldnn_bias = itensor_from_tensor(bias);
}
_mkldnn_convolution_out(
mkldnn_input,
mkldnn_output,
mkldnn_weight,
mkldnn_bias,
padding,
stride,
dilation,
output_sizes,
groups,
attr);
}
static std::vector<int64_t> get_output_sizes(
ContextConv& context,
const Tensor& input) {
const ideep::tensor& mkldnn_weight = context.weight_packed_;
IntArrayRef padding = context.padding_;
IntArrayRef stride = context.stride_;
IntArrayRef dilation = context.dilation_;
auto kernel_size = mkldnn_weight.get_dims();
std::vector<int64_t> input_size = input.sizes().vec();
return conv_output_size(input_size, kernel_size, padding, stride, dilation);
}
Tensor run(ContextConv& context, const Tensor& input) {
std::vector<int64_t> output_sizes = get_output_sizes(context, input);
auto output = at::empty(
output_sizes,
input.options().memory_format(input.suggest_memory_format()));
bool is_channels_last =
input.suggest_memory_format() == at::MemoryFormat::ChannelsLast;
ideep::tensor y;
c10::impl::ExcludeDispatchKeyGuard edkg(c10::autograd_dispatch_keyset);
ideep::tensor mkldnn_output = itensor_from_tensor(output);
if (is_channels_last) {
mkldnn_convolution_out(
input,
mkldnn_output,
context.weight_packed_,
context.at_bias_,
context.padding_,
context.stride_,
context.dilation_,
output_sizes,
context.groups_,
context.attr_);
} else {
mkldnn_convolution_out(
input,
y,
context.weight_packed_,
context.at_bias_,
context.padding_,
context.stride_,
context.dilation_,
output_sizes,
context.groups_,
context.attr_);
mkldnn_output.feed_from(y);
}
return output;
}
void run(ContextConv& context, const Tensor& input, void* output) {
std::vector<int64_t> output_sizes = get_output_sizes(context, input);
bool is_channels_last =
input.suggest_memory_format() == at::MemoryFormat::ChannelsLast;
ideep::tensor y;
ideep::tag o_tag = is_channels_last ? ideep::tag::nhwc : ideep::tag::nchw;
ideep::tensor::desc o_desc = {
output_sizes, get_mkldnn_dtype(input.scalar_type()), o_tag};
ideep::tensor mkldnn_output = {o_desc, output};
if (is_channels_last) {
mkldnn_convolution_out(
input,
mkldnn_output,
context.weight_packed_,
context.at_bias_,
context.padding_,
context.stride_,
context.dilation_,
output_sizes,
context.groups_,
context.attr_);
} else {
mkldnn_convolution_out(
input,
y,
context.weight_packed_,
context.at_bias_,
context.padding_,
context.stride_,
context.dilation_,
output_sizes,
context.groups_,
context.attr_);
mkldnn_output.feed_from(y);
}
}
Tensor conv_run(
const Tensor& input,
const c10::intrusive_ptr<mkldnn::ConvOpContext>& op_context) {
return op_context->run(input);
}
} // namespace at::native::mkldnn::internal::convolution
#endif // AT_MKLDNN_ENABLED()