Skip to content

Add GPU JIT implementation for gridsample operation - #5139

Open
Imeguras wants to merge 3 commits into
ROCm:developfrom
Imeguras:gridsample-gpu-kernel
Open

Add GPU JIT implementation for gridsample operation#5139
Imeguras wants to merge 3 commits into
ROCm:developfrom
Imeguras:gridsample-gpu-kernel

Conversation

@Imeguras

@Imeguras Imeguras commented Aug 16, 2026

Copy link
Copy Markdown

Motivation

MIGraphX's ONNX parser lowers bilinear GridSample into a concat+gathernd decomposition that materializes explicit index tensors for every output element rather than computing sample positions inline.

For a typical Mask R-CNN mask-paste (100 instances, 28×28→640×640), this produces ~327 MB of baked index literals and takes 78.0 ms on an RX 7800 XT — 51% in concat, 27% in gathernd.

In practice this makes something like InternImage(the smaller models) run at less than 1 fps even with a rocm/hip and other amd dedicated frameworks, which for my project(being an alternative to deepstream is just horrible).

Technical Details

parse_gridsample.cpp's linear_sampler::sample() lowers bilinear GridSample into a concat+gathernd decomposition. For each output element it builds explicit {x0,y0}/{x1,y0}/{x0,y1}/{x1,y1} corner index tensors as ONNX-parser-time literals, then does four gathernd fetches + weighted add. It werks, but ends up baking a lot of memory for index literals into the compiled program.

Fix: a new gridsample operator with a dedicated GPU JIT kernel, following the existing pattern used by nonmaxsuppression/resize/roialign (src/targets/gpu/jit/.cpp + src/targets/gpu/kernels/include/migraphx/kernels/.hpp):

  • src/include/migraphx/op/gridsample.hpp — op definition + CPU reference compute(), used by the ref target and as the correctness baseline.
  • src/targets/gpu/kernels/include/migraphx/kernels/gridsample.hpp — the fused device kernel. One thread per output element; unnormalizes its own (h,w) into a source coordinate and computes all four bilinear taps directly — no index tensor, no intermediate buffer. align_corners/padding_mode are template/constexpr params so the padding branch is compiled out per-instance.
  • src/targets/gpu/jit/gridsample.cpp — hiprtc JIT compiler wiring, mirrors jit/roialign.cpp.
  • src/onnx/parse_gridsample.cpp — emits gridsample for the bilinear case; nearest/bicubic/mixed-dtype inputs still fall through to the existing decomposition unchanged. MIGRAPHX_DISABLE_GRIDSAMPLE_OP=1 forces the old path for A/B comparison(im gonna review this once again im still learning migraphx and how it works).
  • src/CMakeLists.txt, src/include/migraphx/operators.hpp — op registration.
  • test/onnx/parse/gridsample{,_channel,_512x512}_test.cpp — updated to assert the single-instruction lowering instead of the decomposition.

Changelog Category

    • [*] Optimized: Component performance that has been optimized or improved.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution! Since this is an external pull request, a maintainer must review PR and add the "ok-to-test" label if it is approved for testing.

@Imeguras
Imeguras marked this pull request as ready for review August 18, 2026 21:36
@Imeguras
Imeguras requested review from a team and causten as code owners August 18, 2026 21:36
@Imeguras

Imeguras commented Aug 18, 2026

Copy link
Copy Markdown
Author

Hello! Im new to the contributing to such a big project.

I noticed that one of the tests was building the gridsample by hand is it intentional? I look into others but didnt get my answer.

@causten
causten requested a review from pfultz2 August 18, 2026 21:50
@causten
causten requested a review from CharlieL7 August 18, 2026 21:52
@Imeguras

Copy link
Copy Markdown
Author

... Found a bug while testing out the performance increases on InternImage
Debugging it right now then will review my code when im not tired from work. should have it by tomorrow

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.

2 participants