Add dynamic shape support for TopK#4880
Open
klin2024 wants to merge 27 commits into
Open
Conversation
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #4880 +/- ##
===========================================
+ Coverage 92.86% 92.87% +0.01%
===========================================
Files 585 585
Lines 30152 30213 +61
===========================================
+ Hits 27998 28059 +61
Misses 2154 2154
🚀 New features to boost your workflow:
|
pfultz2
requested changes
May 13, 2026
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends MIGraphX’s TopK support to handle dynamic input shapes and cases where k is not a compile-time constant (by using a placeholder k derived from the input shape), and adjusts dynamic-output handling in evaluation/GPU lowering to better support tuple-shaped outputs like TopK.
Changes:
- Update ONNX TopK parsing to allow non-constant
kby deriving a placeholderkfrom the input shape (static lens or dynamic max lens). - Update
op::topkto support dynamic shapes at shape-inference/eval time viadyn_output, including runtime clamping ofk. - Adjust GPU/lowering + dyn-output plumbing for tuple/dynamic shapes, and add tests for dynamic TopK scenarios.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/verify/test_topk_dynamic.cpp | Adds a verify test exercising TopK with a dynamic input shape and placeholder k. |
| test/ref/topk.cpp | Adds reference tests for k > n with dynamic input and for k == n behavior. |
| src/targets/gpu/lowering.cpp | Switches dynamic-shape detection to any_of_dynamic() to account for tuple outputs. |
| src/targets/gpu/include/migraphx/gpu/hip.hpp | Minor whitespace-only adjustment. |
| src/targets/gpu/compile_ops.cpp | Alters dynamic code-object execution to always compute/reshape runtime output shape and adds a skip-on-empty heuristic. |
| src/rewrite_topk.cpp | Disables large-TopK rewrite when input shape is dynamic. |
| src/program.cpp | Makes tracing more robust when evaluating submodules from dynamic code-object ops. |
| src/onnx/parse_topk.cpp | Removes the “k must be constant” restriction; derives placeholder k from input shape when needed. |
| src/include/migraphx/op/topk.hpp | Adds dynamic-shape support and switches compute to dyn_output, clamping k at runtime. |
| src/include/migraphx/dyn_output.hpp | Uses any_of_dynamic() so tuple outputs with dynamic subshapes get runtime-computed shapes. |
Comment on lines
+62
to
+66
| auto input_shape = args.at(0)->get_shape(); | ||
| auto ndim = input_shape.ndim(); | ||
| auto norm_axis = axis < 0 ? axis + static_cast<int64_t>(ndim) : axis; | ||
| if(input_shape.dynamic()) | ||
| { |
Comment on lines
+61
to
+71
| // k is not constant: use the input dimension along the topk axis | ||
| auto input_shape = args.at(0)->get_shape(); | ||
| auto ndim = input_shape.ndim(); | ||
| auto norm_axis = axis < 0 ? axis + static_cast<int64_t>(ndim) : axis; | ||
| if(input_shape.dynamic()) | ||
| { | ||
| k = input_shape.dyn_dims().at(norm_axis).get_interval().max; | ||
| } | ||
| else | ||
| { | ||
| k = input_shape.lens().at(norm_axis); |
Comment on lines
+163
to
+167
| auto out_shape = pre_op.compute_shape(to_shapes(static_args), module_args); | ||
| static_args[static_args.size() - 1] = output_arg.reshape(out_shape); | ||
| // Skip JIT compilation when dynamic shape resolves to 0 elements at runtime | ||
| if(args.front().get_shape().elements() == 0) | ||
| return static_args.back(); |
Comment on lines
84
to
86
| auto topk_ret = info.add_instruction( | ||
| make_op("topk", {{"k", k}, {"axis", axis}, {"largest", largest}}), args.at(0)); | ||
|
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Hi @pfultz2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The topk operator previously required a constant k and static input shapes, which will block the SSDMobileNetV2 model from running.
AMDMIGraphX/src/onnx/parse_topk.cpp
Line 47 in 93b8849
This PR adds dynamic input support for the topk op.
Affected model: SSDMobileNetV2
Technical Details
.\bin\migraphx-driver.exe perf .\bin\topk_shape_derived_k.onnx can run success.
onnx file
topk_shape_derived_k.zip
Add a
CHANGELOG.mdentry for any option other thanNot Applicable