-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathop_convolution.cpp
More file actions
284 lines (264 loc) · 8.09 KB
/
op_convolution.cpp
File metadata and controls
284 lines (264 loc) · 8.09 KB
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
282
283
284
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/cadence/fused_quant/op_convolution.h>
#include <executorch/backends/cadence/fused_quant/quant_utils.h>
#include <executorch/runtime/kernel/kernel_includes.h>
namespace cadence {
namespace fused_quant {
namespace native {
using executorch::aten::IntArrayRef;
using executorch::aten::optional;
using executorch::aten::ScalarType;
using executorch::aten::Tensor;
using executorch::runtime::KernelRuntimeContext;
namespace {
void conv2d_kernel(
const float* inp,
const float* weight,
const float* bias,
float* out,
int64_t N,
int64_t C_in,
int64_t H_in,
int64_t W_in,
int64_t C_out,
int64_t kH,
int64_t kW,
int64_t stride_h,
int64_t stride_w,
int64_t pad_h,
int64_t pad_w,
int64_t dil_h,
int64_t dil_w,
int64_t groups,
int64_t H_out,
int64_t W_out) {
int64_t C_in_per_group = C_in / groups;
int64_t C_out_per_group = C_out / groups;
for (int64_t n = 0; n < N; ++n) {
for (int64_t g = 0; g < groups; ++g) {
for (int64_t oc = 0; oc < C_out_per_group; ++oc) {
int64_t oc_global = g * C_out_per_group + oc;
for (int64_t oh = 0; oh < H_out; ++oh) {
for (int64_t ow = 0; ow < W_out; ++ow) {
float sum = bias ? bias[oc_global] : 0.0f;
for (int64_t ic = 0; ic < C_in_per_group; ++ic) {
int64_t ic_global = g * C_in_per_group + ic;
for (int64_t kh = 0; kh < kH; ++kh) {
for (int64_t kw = 0; kw < kW; ++kw) {
int64_t ih = oh * stride_h - pad_h + kh * dil_h;
int64_t iw = ow * stride_w - pad_w + kw * dil_w;
if (ih >= 0 && ih < H_in && iw >= 0 && iw < W_in) {
float inp_val =
inp[((n * C_in + ic_global) * H_in + ih) * W_in + iw];
float w_val = weight
[((oc_global * C_in_per_group + ic) * kH + kh) * kW +
kw];
sum += inp_val * w_val;
}
}
}
}
out[((n * C_out + oc_global) * H_out + oh) * W_out + ow] = sum;
}
}
}
}
}
}
} // namespace
Tensor& convolution_out(
KernelRuntimeContext& ctx,
const Tensor& inp,
const Tensor& weight,
const optional<Tensor>& bias,
// inp qparams
const optional<Tensor>& inp_scale,
const optional<Tensor>& inp_zero_point,
ScalarType inp_dtype,
int64_t inp_quant_min,
int64_t inp_quant_max,
optional<int64_t> inp_axis,
// weight qparams
const optional<Tensor>& weight_scale,
const optional<Tensor>& weight_zero_point,
ScalarType weight_dtype,
int64_t weight_quant_min,
int64_t weight_quant_max,
optional<int64_t> weight_axis,
// bias qparams
const optional<Tensor>& bias_scale,
const optional<Tensor>& bias_zero_point,
ScalarType bias_dtype,
int64_t bias_quant_min,
int64_t bias_quant_max,
optional<int64_t> bias_axis,
// out qparams
const optional<Tensor>& out_scale,
const optional<Tensor>& out_zero_point,
ScalarType out_dtype,
int64_t out_quant_min,
int64_t out_quant_max,
optional<int64_t> out_axis,
// conv params
IntArrayRef stride,
IntArrayRef padding,
IntArrayRef dilation,
int64_t groups,
Tensor& out) {
// Extract dimensions from input tensor [N, C_in, H_in, W_in]
int64_t N = inp.size(0);
int64_t C_in = inp.size(1);
int64_t H_in = inp.size(2);
int64_t W_in = inp.size(3);
// Extract dimensions from weight tensor [C_out, C_in/groups, kH, kW]
int64_t C_out = weight.size(0);
int64_t kH = weight.size(2);
int64_t kW = weight.size(3);
int64_t stride_h = stride[0];
int64_t stride_w = stride[1];
int64_t pad_h = padding[0];
int64_t pad_w = padding[1];
int64_t dil_h = dilation[0];
int64_t dil_w = dilation[1];
int64_t H_out = (H_in + 2 * pad_h - dil_h * (kH - 1) - 1) / stride_h + 1;
int64_t W_out = (W_in + 2 * pad_w - dil_w * (kW - 1) - 1) / stride_w + 1;
int64_t inp_numel = inp.numel();
int64_t weight_numel = weight.numel();
int64_t out_numel = N * C_out * H_out * W_out;
bool inp_quantized = inp_scale.has_value();
bool weight_quantized = weight_scale.has_value();
bool bias_quantized = bias_scale.has_value();
bool out_quantized = out_scale.has_value();
// Dequantize input if quantized
std::vector<float> inp_buf;
const float* const inp_float = [&]() -> const float* {
if (!inp_quantized) {
return inp.const_data_ptr<float>();
}
inp_buf.resize(inp_numel);
QParams qp = extract_qparams(
inp_scale, inp_zero_point, inp_quant_min, inp_quant_max, inp_axis, inp);
FUSED_QUANT_DTYPE_SWITCH(
inp.scalar_type(),
scalar_t,
dequantize_buffer(
inp.const_data_ptr<scalar_t>(), inp_buf.data(), inp_numel, qp);)
return inp_buf.data();
}();
// Dequantize weight if quantized
std::vector<float> weight_buf;
const float* const weight_float = [&]() -> const float* {
if (!weight_quantized) {
return weight.const_data_ptr<float>();
}
weight_buf.resize(weight_numel);
QParams qp = extract_qparams(
weight_scale,
weight_zero_point,
weight_quant_min,
weight_quant_max,
weight_axis,
weight);
FUSED_QUANT_DTYPE_SWITCH(weight.scalar_type(),
scalar_t,
dequantize_buffer(
weight.const_data_ptr<scalar_t>(),
weight_buf.data(),
weight_numel,
qp);)
return weight_buf.data();
}();
// Dequantize bias if present and quantized
std::vector<float> bias_buf;
const float* bias_float = nullptr;
if (bias.has_value()) {
const Tensor& bias_tensor = bias.value();
if (bias_quantized) {
int64_t bias_numel = bias_tensor.numel();
bias_buf.resize(bias_numel);
QParams qp = extract_qparams(
bias_scale,
bias_zero_point,
bias_quant_min,
bias_quant_max,
bias_axis,
bias_tensor);
FUSED_QUANT_DTYPE_SWITCH(bias_tensor.scalar_type(),
scalar_t,
dequantize_buffer(
bias_tensor.const_data_ptr<scalar_t>(),
bias_buf.data(),
bias_numel,
qp);)
bias_float = bias_buf.data();
} else {
bias_float = bias_tensor.const_data_ptr<float>();
}
}
// Run convolution
if (out_quantized) {
std::vector<float> result_float(out_numel);
conv2d_kernel(
inp_float,
weight_float,
bias_float,
result_float.data(),
N,
C_in,
H_in,
W_in,
C_out,
kH,
kW,
stride_h,
stride_w,
pad_h,
pad_w,
dil_h,
dil_w,
groups,
H_out,
W_out);
QParams qp = extract_qparams(
out_scale, out_zero_point, out_quant_min, out_quant_max, out_axis, out);
FUSED_QUANT_DTYPE_SWITCH(out.scalar_type(),
scalar_t,
quantize_buffer(
result_float.data(),
out.mutable_data_ptr<scalar_t>(),
out_numel,
qp);)
} else {
conv2d_kernel(
inp_float,
weight_float,
bias_float,
out.mutable_data_ptr<float>(),
N,
C_in,
H_in,
W_in,
C_out,
kH,
kW,
stride_h,
stride_w,
pad_h,
pad_w,
dil_h,
dil_w,
groups,
H_out,
W_out);
}
return out;
}
} // namespace native
} // namespace fused_quant
} // namespace cadence