Skip to content

Commit 45ff0bd

Browse files
authored
Fix overflow and stride>1 fallback in cadence::quantized_conv1d HiFi kernels (pytorch#19193)
Differential Revision: D102821209 Pull Request resolved: pytorch#19193
1 parent e84a418 commit 45ff0bd

2 files changed

Lines changed: 30 additions & 24 deletions

File tree

‎backends/cadence/hifi/operators/op_quantized_conv1d_ncl.cpp‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace native {
2424

2525
namespace {
2626

27-
// Optimized NCHW 1D convolution for int8 x int8 -> int8
2827
void xa_opt_quantized_conv1d_ncl_asym8sxsym8s_asym8s(
2928
KernelRuntimeContext& ctx,
3029
const Tensor& input,
@@ -66,7 +65,6 @@ void xa_opt_quantized_conv1d_ncl_asym8sxsym8s_asym8s(
6665
WORD32 dilation_height = 1;
6766
WORD32 dilation_width = 1;
6867
WORD32 input_zero_bias = -in_zero_point;
69-
WORD32 kernel_zero_bias = -weight_zero_point;
7068

7169
WORD32 out_zero_bias = output_zero_point;
7270

@@ -131,13 +129,17 @@ void xa_opt_quantized_conv1d_ncl_asym8sxsym8s_asym8s(
131129
kNnlibMaxDim,
132130
kNnlibMaxDim);
133131

134-
WORD32 p_out_multiplier32[out_channels];
135-
WORD32 p_out_shift32[out_channels];
132+
WORD32* p_out_multiplier32 = (WORD32*)kernels::allocate_temp_memory(
133+
ctx, out_channels * sizeof(WORD32));
134+
WORD32* p_out_shift32 = (WORD32*)kernels::allocate_temp_memory(
135+
ctx, out_channels * sizeof(WORD32));
136136

137-
float out_scale = 1. / output_scale;
137+
const float eff_scale = bias_scale * (1.0f / output_scale);
138138

139139
for (int i = 0; i < out_channels; i++) {
140-
p_out_multiplier32[i] = bias_scale * out_scale * 2147483648;
140+
p_out_multiplier32[i] = (eff_scale >= 1.0f)
141+
? static_cast<WORD32>(2147483647)
142+
: static_cast<WORD32>(eff_scale * 2147483648.0f);
141143
p_out_shift32[i] = 0;
142144
}
143145

@@ -202,7 +204,6 @@ void xa_opt_quantized_conv1d_ncl_asym8sxsym8s_asym8s(
202204
}
203205
}
204206

205-
// Optimized NCHW 1D convolution for uint8 x uint8 -> uint8
206207
void xa_opt_quantized_conv1d_ncl_asym8uxsym8u_asym8u(
207208
KernelRuntimeContext& ctx,
208209
const Tensor& input,
@@ -240,7 +241,10 @@ void xa_opt_quantized_conv1d_ncl_asym8uxsym8u_asym8u(
240241
WORD32 x_stride = stride[0];
241242
WORD32 x_padding = padding[0];
242243
WORD32 input_zero_bias = -in_zero_point;
243-
WORD32 out_multiplier32 = bias_scale * (1. / output_scale) * 2147483648;
244+
const float eff_scale = bias_scale * (1.0f / output_scale);
245+
WORD32 out_multiplier32 = (eff_scale >= 1.0f)
246+
? static_cast<WORD32>(2147483647)
247+
: static_cast<WORD32>(eff_scale * 2147483648.0f);
244248
WORD32 out_shift32 = 0;
245249
WORD32 kernel_zero_bias = -weight_zero_point;
246250

@@ -358,7 +362,6 @@ void quantized_conv1d_ncl_per_tensor_out(
358362
Tensor& out) {
359363
// HiFi nnlib kernels only support dilation=1.
360364
// Fall back to generic implementation for dilation > 1.
361-
// Note: For 1D convolution, dilation is a single-element array.
362365
if (dilation[0] != 1) {
363366
impl::generic::native::quantized_conv1d_ncl_per_tensor_out(
364367
ctx,
@@ -419,9 +422,9 @@ void quantized_conv1d_ncl_per_tensor_out(
419422
out);
420423
}
421424
} else if (dtype == ScalarType::Byte) {
422-
// HiFi nnlib conv1d_std kernel does not support depthwise (groups > 1).
423-
// Fall back to generic implementation.
424-
if (groups > 1) {
425+
// HiFi nnlib conv1d_std kernel does not support depthwise (groups > 1)
426+
// or stride > 1. Fall back to generic implementation.
427+
if (groups > 1 || stride[0] > 1) {
425428
impl::generic::native::quantized_conv1d_ncl_per_tensor_out(
426429
ctx,
427430
input,

‎backends/cadence/hifi/operators/op_quantized_conv1d_nlc.cpp‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace native {
2424

2525
namespace {
2626

27-
// Optimized NLC 1D convolution for int8 x int8 -> int8
2827
void xa_opt_quantized_conv1d_nlc_asym8sxsym8s_asym8s(
2928
KernelRuntimeContext& ctx,
3029
const Tensor& input,
@@ -64,7 +63,6 @@ void xa_opt_quantized_conv1d_nlc_asym8sxsym8s_asym8s(
6463
WORD32 dilation_height = 1;
6564
WORD32 dilation_width = 1;
6665
WORD32 input_zero_bias = -in_zero_point;
67-
WORD32 kernel_zero_bias = -weight_zero_point;
6866

6967
WORD32 input_precision = 8;
7068
WORD32 kernel_precision = 8;
@@ -73,13 +71,17 @@ void xa_opt_quantized_conv1d_nlc_asym8sxsym8s_asym8s(
7371

7472
WORD32 out_data_format = 0;
7573

76-
WORD32 p_out_multiplier32[out_channels];
77-
WORD32 p_out_shift32[out_channels];
74+
WORD32* p_out_multiplier32 = (WORD32*)kernels::allocate_temp_memory(
75+
ctx, out_channels * sizeof(WORD32));
76+
WORD32* p_out_shift32 = (WORD32*)kernels::allocate_temp_memory(
77+
ctx, out_channels * sizeof(WORD32));
7878

79-
float out_scale = 1. / output_scale;
79+
const float eff_scale = bias_scale * (1.0f / output_scale);
8080

8181
for (int i = 0; i < out_channels; i++) {
82-
p_out_multiplier32[i] = bias_scale * out_scale * 2147483648;
82+
p_out_multiplier32[i] = (eff_scale >= 1.0f)
83+
? static_cast<WORD32>(2147483647)
84+
: static_cast<WORD32>(eff_scale * 2147483648.0f);
8385
p_out_shift32[i] = 0;
8486
}
8587

@@ -144,7 +146,6 @@ void xa_opt_quantized_conv1d_nlc_asym8sxsym8s_asym8s(
144146
}
145147
}
146148

147-
// Optimized NLC 1D convolution for uint8 x uint8 -> uint8
148149
void xa_opt_quantized_conv1d_nlc_asym8uxsym8u_asym8u(
149150
KernelRuntimeContext& ctx,
150151
const Tensor& input,
@@ -176,7 +177,10 @@ void xa_opt_quantized_conv1d_nlc_asym8uxsym8u_asym8u(
176177
WORD32 x_stride = stride[stride.size() - 1];
177178
WORD32 x_padding = padding[padding.size() - 1];
178179
WORD32 input_zero_bias = -in_zero_point;
179-
WORD32 out_multiplier32 = bias_scale * (1. / output_scale) * 2147483648;
180+
const float eff_scale = bias_scale * (1.0f / output_scale);
181+
WORD32 out_multiplier32 = (eff_scale >= 1.0f)
182+
? static_cast<WORD32>(2147483647)
183+
: static_cast<WORD32>(eff_scale * 2147483648.0f);
180184
WORD32 out_shift32 = 0;
181185
WORD32 kernel_zero_bias = -weight_zero_point;
182186

@@ -237,7 +241,6 @@ void quantized_conv1d_nlc_per_tensor_out(
237241
Tensor& out) {
238242
// HiFi nnlib kernels only support dilation=1.
239243
// Fall back to generic implementation for dilation > 1.
240-
// Note: For 1D convolution, dilation is a single-element array.
241244
if (dilation[dilation.size() - 1] != 1) {
242245
impl::generic::native::quantized_conv1d_nlc_per_tensor_out(
243246
ctx,
@@ -298,9 +301,9 @@ void quantized_conv1d_nlc_per_tensor_out(
298301
out);
299302
}
300303
} else if (dtype == ScalarType::Byte) {
301-
// HiFi nnlib conv1d_std kernel does not support depthwise (groups > 1).
302-
// Fall back to generic implementation.
303-
if (groups > 1) {
304+
// HiFi nnlib conv1d_std kernel does not support depthwise (groups > 1)
305+
// or stride > 1. Fall back to generic implementation.
306+
if (groups > 1 || stride[stride.size() - 1] > 1) {
304307
impl::generic::native::quantized_conv1d_nlc_per_tensor_out(
305308
ctx,
306309
input,

0 commit comments

Comments
 (0)