Skip to content

Fix affine() IndexError with boolean mask indexing#1547

Merged
alinpahontu2912 merged 1 commit intodotnet:mainfrom
alinpahontu2912:fix/affine-boolean-mask-indexing
Mar 3, 2026
Merged

Fix affine() IndexError with boolean mask indexing#1547
alinpahontu2912 merged 1 commit intodotnet:mainfrom
alinpahontu2912:fix/affine-boolean-mask-indexing

Conversation

@alinpahontu2912
Copy link
Member

@alinpahontu2912 alinpahontu2912 commented Feb 27, 2026

Fixes #1502

Replace img[mask] = fill_img[mask] with torch::where(mask, fill_img, img) in THSVision_ApplyGridTransform's Nearest-mode fill logic. The operator[] boolean mask indexing is not supported in recent LibTorch versions.

This fixes torchvision.transforms.functional.affine() and rotate() when called with a fill value and Nearest interpolation mode.

Replace img[mask] = fill_img[mask] with torch::where(mask, fill_img, img)
in THSVision_ApplyGridTransform's Nearest-mode fill logic. The operator[]
boolean mask indexing is not supported in recent LibTorch versions.

This fixes torchvision.transforms.functional.affine() and rotate() when
called with a fill value and Nearest interpolation mode.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alinpahontu2912 alinpahontu2912 changed the title Fix affine() IndexError with boolean mask indexing (#1502) Fix affine() IndexError with boolean mask indexing Feb 27, 2026
@alinpahontu2912 alinpahontu2912 requested a review from Copilot March 2, 2026 14:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression in TorchVision-style affine/rotate transforms with fill by removing unsupported boolean-mask operator[] indexing in the native grid-transform path.

Changes:

  • Replace img[mask] = fill_img[mask] with torch::where(mask, fill_img, img) in THSVision_ApplyGridTransform for Nearest-mode fill handling.
  • Add regression tests covering affine transforms on 3D/4D inputs (including batched 4D) and rotate with fill.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Native/LibTorchSharp/THSVision.cpp Avoids unsupported boolean mask indexing by using torch::where to apply fill under Nearest sampling.
test/TorchSharpTest/TestTorchVision.cs Adds tests to ensure affine/rotate with fill execute successfully for common input shapes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@alinpahontu2912 alinpahontu2912 merged commit 5782540 into dotnet:main Mar 3, 2026
6 checks passed
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.

torchvision.transforms.functional.affine throws IndexError

2 participants