Skip to content

Add dynamic shape support for TopK#4880

Open
klin2024 wants to merge 27 commits into
developfrom
topk_op_support_dynamic_input_fix
Open

Add dynamic shape support for TopK#4880
klin2024 wants to merge 27 commits into
developfrom
topk_op_support_dynamic_input_fix

Conversation

@klin2024
Copy link
Copy Markdown
Contributor

@klin2024 klin2024 commented May 13, 2026

Motivation

The topk operator previously required a constant k and static input shapes, which will block the SSDMobileNetV2 model from running.

check_arg_empty(arg_k, "PARSE_TopK: k input must be constant");

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.md entry for any option other than Not Applicable

    • Added: New functionality.
    • Changed: Changes to existing functionality.
    • Removed: Functionality or support that has been removed. (Compared to a previous release)
    • Optimized: Component performance that has been optimized or improved.
    • Resolved Issues: Known issues from a previous version that have been resolved.
    • Not Applicable: This PR is not to be included in the changelog.

kazhang2 and others added 12 commits May 11, 2026 20:36
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
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Patch coverage is 84.78261% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/onnx/parse_topk.cpp 57.14% 6 Missing ⚠️
src/program.cpp 66.67% 1 Missing ⚠️
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              
Files with missing lines Coverage Δ
src/include/migraphx/dyn_output.hpp 100.00% <100.00%> (ø)
src/include/migraphx/op/topk.hpp 98.72% <100.00%> (+0.28%) ⬆️
src/rewrite_topk.cpp 100.00% <100.00%> (ø)
src/program.cpp 81.05% <66.67%> (-0.08%) ⬇️
src/onnx/parse_topk.cpp 78.57% <57.14%> (-21.43%) ⬇️

... and 12 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/targets/gpu/include/migraphx/gpu/hip.hpp Outdated
Comment thread src/replace_allocate.cpp Outdated
kazhang2 and others added 12 commits May 13, 2026 23:42
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>
@klin2024 klin2024 changed the title Topk op support dynamic input fix Add dynamic shape support for TopK May 14, 2026
@klin2024 klin2024 marked this pull request as ready for review May 14, 2026 15:45
@klin2024 klin2024 requested a review from causten as a code owner May 14, 2026 15:45
Copilot AI review requested due to automatic review settings May 14, 2026 15:45
Copy link
Copy Markdown
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

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 k by deriving a placeholder k from the input shape (static lens or dynamic max lens).
  • Update op::topk to support dynamic shapes at shape-inference/eval time via dyn_output, including runtime clamping of k.
  • 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 thread src/include/migraphx/op/topk.hpp Outdated
Comment thread src/onnx/parse_topk.cpp
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 thread src/onnx/parse_topk.cpp
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 thread test/ref/topk.cpp Outdated
Comment thread src/onnx/parse_topk.cpp
Comment on lines 84 to 86
auto topk_ret = info.add_instruction(
make_op("topk", {{"k", k}, {"axis", axis}, {"largest", largest}}), args.at(0));

@causten causten requested a review from pfultz2 May 14, 2026 17:31
kazhang2 and others added 2 commits May 16, 2026 09:32
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>
@wendyli-2024
Copy link
Copy Markdown

Hi @pfultz2
Do you have time to check the code change? Thanks

@CharlieL7 CharlieL7 self-requested a review May 26, 2026 18:21
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.

5 participants