Skip to content

Conversation

@hariprasadravi
Copy link
Contributor

@hariprasadravi hariprasadravi commented Nov 7, 2025

The Bug

The torch-to-linalg lowering for aten.convolution (with transposed=true) incorrectly handles cases where the effective padding is negative. The logic for this is contained in createTransposedInputPadding.

The original implementation had two critical flaws:

Incorrect Math: The logic block for negative padding (if (anyDimensionPaddingIsNegative)) attempted to "pre-crop" the input tensor before un-striding. The math used to calculate these slice offsets and sizes was incorrect, resulting in tensor.extract_slice operations with out-of-bounds offsets and negative sizes, causing the compiler to fail.

Failed "Mixed-Mode" Logic: The code was built on an "all-or-nothing" assumption. It failed to handle "mixed-mode" padding, where one spatial dimension required padding (positive offset) while another required cropping (negative offset). It would enter the negative padding path and apply cropping logic to all dimensions, leading to out-of-bounds errors when it tried to crop a dimension that should have been padded.

The Fix

This patch refactors the logic into two clean, robust paths:

All-Padding Path (else block):

Trigger: All spatial dimensions have an effective padding offset >= 0.

Action: Retains the original, efficient "fast path." It uses a single tensor.insert_slice to perform both un-striding (with strides) and padding (with offsets) in one operation.

Safe Path (if (anyDimensionPaddingIsNegative) block):

Trigger: At least one spatial dimension has a negative effective padding offset.

Action: This path is now a unified, robust 3-step process that correctly handles both all-crop and mixed-mode scenarios:

Create "Super-Tensor": It computes a maxSizes tensor, which is the "union" of the padded and un-strided sizes (i.e., max(innerSize, outerSize) for each dimension).

Pad & Un-stride: It performs a single tensor.insert_slice of the original input into this maxSizes tensor. This one operation correctly applies all positive padding (via insertSliceOffsets) and un-striding (via strideIndexValues).

Crop: It performs a final tensor.extract_slice to crop the maxSizes tensor down to the final outerSizes. This correctly applies all negative padding (via extractSliceOffsets).

This new logic resolves all known failure cases and is validated by the new TransposedConv{1,2,3}dNegativePadding test cases, which specifically target this functionality.

@hariprasadravi hariprasadravi changed the title [LINALG] Fix transposed conv2d with stride bug [LINALG] Fix out-of-bounds indexing issue with transposed convolution with negative padding Nov 7, 2025
@hariprasadravi hariprasadravi changed the title [LINALG] Fix out-of-bounds indexing issue with transposed convolution with negative padding [LINALG] Fix: Incorrect linalg lowering for aten.convolution_transpose with negative effective padding Nov 8, 2025
@hariprasadravi hariprasadravi marked this pull request as ready for review November 8, 2025 00:01
@hariprasadravi
Copy link
Contributor Author

hariprasadravi commented Nov 8, 2025

@sahas3 and @ivangarcia44 Please help review and add additional reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant