Skip to content

[Fix][Relax] Preserve PyTorch grid_sample grid layout - #20986

Open
DiadkoShmek wants to merge 1 commit into
apache:mainfrom
DiadkoShmek:DiadkoShmek/fix-exported-grid-sample-layout
Open

DiadkoShmek wants to merge 1 commit into
apache:mainfrom
DiadkoShmek:DiadkoShmek/fix-exported-grid-sample-layout

Conversation

@DiadkoShmek

Copy link
Copy Markdown

Fixes #20882.

PyTorch exports the grid in [N, H_out, W_out, 2] order. With layout="NCHW", Relax reads it as [N, 2, H_out, W_out]. Passing it through directly makes the output shape (1, 1, 5, 2) for a grid whose expected output is (1, 1, 3, 5). A 2×2 grid happens to yield the expected shape but can still sample the wrong coordinates.

Move the coordinate axis before emitting the Relax operator. Use a non-square grid in the IR test and compare compiled LLVM results against PyTorch eager for both grid shapes.

Local validation on 23a6dbe, Linux x86-64, PyTorch 2.10.0, and LLVM from TIRx 0.1.0.

  • On the base, the IR test and both numerical cases fail. The square case differs in three of four values.
  • With the patch, the grid_sample and adjacent affine_grid subset passes 5 tests. Both end-to-end outputs match PyTorch eager.
  • Ruff lint and formatting checks pass for the changed files.

Codex assisted with the patch and regression tests.

This branch has not been deployed

No deployments
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.

[Bug][Relax][PyTorch] grid_sample returns the wrong shape and values after from_exported_program

1 participant