Skip to content

Commit b223e7d

Browse files
hsharma35facebook-github-bot
authored andcommitted
Fix overflow and stride>1 fallback in cadence::quantized_conv1d HiFi kernels
Summary: Fixes two correctness issues in the HiFi backend kernels for cadence::quantized_conv1d_ncl.out and quantized_conv1d_nlc.out that were surfaced while writing C++ unit tests: 1. out_multiplier32 overflow: the expression `bias_scale * (1. / output_scale) * 2147483648` overflows a 32-bit signed integer when `bias_scale / output_scale` is >= 1.0. Clamp to INT32_MAX in that case to preserve correct numerics for the supported range. 2. uint8 stride>1 fallback: HiFi nnlib `conv1d_std` does not support stride > 1 in addition to groups > 1. Extend the existing depthwise-fallback condition to also cover stride > 1, matching the kernel's actual capabilities. Differential Revision: D102821209
1 parent 8bda9ac commit b223e7d

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

backends/cadence/hifi/operators/op_quantized_conv1d_ncl.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ void xa_opt_quantized_conv1d_ncl_asym8uxsym8u_asym8u(
240240
WORD32 x_stride = stride[0];
241241
WORD32 x_padding = padding[0];
242242
WORD32 input_zero_bias = -in_zero_point;
243-
WORD32 out_multiplier32 = bias_scale * (1. / output_scale) * 2147483648;
243+
const float eff_scale = bias_scale * (1.0f / output_scale);
244+
WORD32 out_multiplier32 = (eff_scale >= 1.0f)
245+
? static_cast<WORD32>(2147483647)
246+
: static_cast<WORD32>(eff_scale * 2147483648.0f);
244247
WORD32 out_shift32 = 0;
245248
WORD32 kernel_zero_bias = -weight_zero_point;
246249

@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ void quantized_conv1d_nlc_per_tensor_out(
298298
out);
299299
}
300300
} 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) {
301+
// HiFi nnlib conv1d_std kernel does not support depthwise (groups > 1)
302+
// or stride > 1. Fall back to generic implementation.
303+
if (groups > 1 || stride[0] > 1) {
304304
impl::generic::native::quantized_conv1d_nlc_per_tensor_out(
305305
ctx,
306306
input,

0 commit comments

Comments
 (0)