Skip to content

Fix BinaryBackend.select_and_scatter crash with :same padding (#1675)Fix/select and scatter negative padding#1676

Open
blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
blasphemetheus:fix/select-and-scatter-negative-padding
Open

Fix BinaryBackend.select_and_scatter crash with :same padding (#1675)Fix/select and scatter negative padding#1676
blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
blasphemetheus:fix/select-and-scatter-negative-padding

Conversation

@blasphemetheus
Copy link

Summary

select_and_scatter/8 computes absolute_index in the padded coordinate space but uses it against the unpadded output_shape, producing negative offsets that crash List.duplicate/2.

Fix: subtract low-padding from indices to map back to original coordinates, and filter out indices that land in the padding region.

Changes

nx/lib/nx/binary_backend.ex

  • Subtract per-dimension low-padding from absolute_index after computing it in padded space
  • Filter output_windows to discard indices outside original tensor bounds

nx/test/nx/defn/grad_test.exs

  • Add regression test: grad(sum(window_max(x, {1,3,3,1}, padding: :same, strides: [1,2,2,1]))) on a {1,4,4,1} input — verifies output shape, finiteness, and gradient sum equals number of output elements

Test Results

1170 tests, 0 failures, 1 skipped
1351 doctests, 0 failures

Closes #1675

)

Adjust absolute_index from padded to original coordinates and filter
out-of-bounds indices in BinaryBackend.select_and_scatter/8.
Adds regression test for window_max gradient with :same padding.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

BinaryBackend.select_and_scatter/8 crashes with negative padding during max_pool gradient

1 participant