From f578636eaa9ad1722437fd8779c63550c5c0525f Mon Sep 17 00:00:00 2001 From: Andrew Grebenisan Date: Mon, 1 Dec 2025 09:30:15 -0800 Subject: [PATCH] Fix index out of bounds error for 1d convs (#15995) Summary: Unfortunately, we are in a state where conv2d can run either 1d or 2d convs, and the padding/stride/dilation logic is pretty bad. Essentially, if we have a conv1d, we can produce a conv2d where stride becomes [1, value_provided] in the fusion_pass (see get_conv_args). That is why in ops_registrations, we have been indexing at 1. However, if we somehow construct a conv2d_nchw/nchw without going through that fusion_pass, this canonicalization is not done. Ideally, we have conv1d implementations where these arguments are scalars, not lists. But, this is a larger effort, since even the C++ kernels follow this convoluted logic. Temporary fix here is to index at -1, and we'll have to come back to cleaning up conv kernels. Reviewed By: hsharma35 Differential Revision: D87943382 --- backends/cadence/aot/ops_registrations.py | 96 ++++++++++----------- backends/cadence/aot/ref_implementations.py | 6 +- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/backends/cadence/aot/ops_registrations.py b/backends/cadence/aot/ops_registrations.py index c1fba3b110b..00417e3ef97 100644 --- a/backends/cadence/aot/ops_registrations.py +++ b/backends/cadence/aot/ops_registrations.py @@ -1030,9 +1030,9 @@ def quantized_conv2d_nhwc_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) @@ -1074,9 +1074,9 @@ def quantized_conv2d_nchw_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1118,9 +1118,9 @@ def quantized_conv2d_nchw_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1162,9 +1162,9 @@ def quantized_conv2d_nhwc_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) @@ -1211,9 +1211,9 @@ def quantized_conv2d_nchw_asym8sxsym8s_asym8s_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1260,9 +1260,9 @@ def quantized_conv2d_nchw_asym8uxsym8u_asym8u_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1309,9 +1309,9 @@ def quantized_conv2d_nhwc_asym8sxsym8s_asym8s_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) @@ -1358,9 +1358,9 @@ def quantized_conv2d_nhwc_asym8uxsym8u_asym8u_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) @@ -1407,9 +1407,9 @@ def quantized_conv2d_nchw_dilated_asym8sxsym8s_asym8s_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1456,9 +1456,9 @@ def quantized_conv2d_nchw_dilated_asym8uxsym8u_asym8u_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1505,9 +1505,9 @@ def quantized_conv2d_nhwc_dilated_asym8sxsym8s_asym8s_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) @@ -1554,9 +1554,9 @@ def quantized_conv2d_nhwc_dilated_asym8uxsym8u_asym8u_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) @@ -1605,9 +1605,9 @@ def quantized_conv2d_nchw_depthwise_asym8sxsym8s_asym8s_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1656,9 +1656,9 @@ def quantized_conv2d_nchw_depthwise_asym8uxsym8u_asym8u_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], False, ) @@ -1707,9 +1707,9 @@ def quantized_conv2d_nhwc_depthwise_asym8sxsym8s_asym8s_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) @@ -1758,9 +1758,9 @@ def quantized_conv2d_nhwc_depthwise_asym8uxsym8u_asym8u_per_tensor_meta( get_conv1d_output_size( in_size, out_channels, - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], kernel_size[0], True, ) diff --git a/backends/cadence/aot/ref_implementations.py b/backends/cadence/aot/ref_implementations.py index bc589325025..3a5d773ef85 100644 --- a/backends/cadence/aot/ref_implementations.py +++ b/backends/cadence/aot/ref_implementations.py @@ -788,9 +788,9 @@ def quantized_conv_per_tensor( (input_tensor - in_zero_point).float(), (weight - weight_zero_point).float(), (bias * bias_scale).float(), - stride[1], - padding[1], - dilation[1], + stride[-1], + padding[-1], + dilation[-1], groups, )