Skip to content

[AIMIGRAPHX-1215] add runtime symbol resolution op - #5085

Open
shivadbhavsar wants to merge 17 commits into
developfrom
sym_resolve_op
Open

[AIMIGRAPHX-1215] add runtime symbol resolution op#5085
shivadbhavsar wants to merge 17 commits into
developfrom
sym_resolve_op

Conversation

@shivadbhavsar

@shivadbhavsar shivadbhavsar commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Motivation

For dynamic shapes, the runtime needs to be able to evaluate symbols using concrete input dimensions to be able to compute dynamic ops (eg. slice)

Technical Details

Attributes (std::vector<sym::expr>, reflected directly):

  • expressions — symbolic expressions to evaluate.
    Inputs: one tensor whose original compile-time shape contains the referenced root symbols as direct dimensions.
    At runtime, each direct symbolic dimension is bound to the corresponding concrete input lens:
compile-time: {N, 3, H, W}
runtime:      {7, 3, 10, 12}
bindings:     N=7, H=10, W=12

Output: one packed int64 vector of shape {expressions.size()}. Element i is eval(expressions[i]), unclamped.
Implementation:

  • compute_shape requires one input, verifies every referenced root symbol appears as a direct input dimension, and returns int64_type, {expressions.size()}.
  • compute reads the original input shape from dyn_output::input_shapes and the concrete dimensions from the runtime argument shape.
  • It maps each direct root symbol to its runtime lens, verifies repeated symbols resolve consistently, then evaluates every expression with eval_uint.
    Restriction: compound input dimensions are not inverted. For example, an input dimension N/2 cannot be used to recover N; N must appear directly on an input axis.
    Example: expressions=[N, H/2, W/2], compile-time input {N,3,H,W}, runtime input {7,3,10,12}[7,5,6].

Usage

x = @param:x -> float_type, {N[1..16], 3, H[1..32], W[1..32]}, {3*H*W, H*W, W, 1}
@1 = eval_expr[expressions={N, H/2, W/2}](x) -> int64_type, {3}, {1}
@2 = slice[axes={0, 2, 3},starts={0, 0, 0}](x,@1) -> dynamic, float_type, {[0..16], 3, [0..32], [0..32]}
@3 = @return(@2)

Changelog Category

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.

Follow the LLVM AI Tool Use Policy for contributions using AI.

@shivadbhavsar
shivadbhavsar requested a review from CharlieL7 July 21, 2026 22:15
@shivadbhavsar shivadbhavsar self-assigned this Jul 21, 2026
@shivadbhavsar
shivadbhavsar requested a review from causten as a code owner July 21, 2026 22:15
Copilot AI review requested due to automatic review settings July 21, 2026 22:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 adds a new resolve_sym_expr runtime operator to evaluate symbolic dimension expressions from concrete runtime dimension values, enabling dynamic-shape lowering patterns (e.g., feeding symbolic slice bounds as runtime inputs).

Changes:

  • Added op::resolve_sym_expr (reflected attributes exprs/symbols) that returns a tuple of evaluated int64 scalars.
  • Added reference and shape tests for resolve_sym_expr, plus an integration-style ref test demonstrating use with dynamic multi-input slice.
  • Registered the new operator in the build system.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/include/migraphx/op/resolve_sym_expr.hpp New operator implementation for runtime symbol expression evaluation.
src/CMakeLists.txt Registers resolve_sym_expr in register_migraphx_ops().
test/ref/resolve_sym_expr.cpp New ref tests covering single/multi-symbol evaluation behavior.
test/op_shape_test.cpp New shape tests (valid shape + bad input arity) for the operator.
test/ref/slice.cpp New ref test demonstrating resolve_sym_expr feeding a runtime slice bound.

Comment thread src/include/migraphx/op/resolve_sym_expr.hpp Outdated
Comment thread src/include/migraphx/op/resolve_sym_expr.hpp Outdated
Comment thread test/ref/resolve_sym_expr.cpp Outdated
@gh-app-migraphx-bot-pr-write

gh-app-migraphx-bot-pr-write Bot commented Jul 22, 2026

Copy link
Copy Markdown
Test Batch New Rate (195aa1) Old Rate (3a503c)* Diff Status
torchvision-resnet50 64 3,119.73 3,264.92 -4.45%
torchvision-resnet50_fp16 64 4,762.93 7,548.67 -36.90% 🔴
torchvision-densenet121 32 2,474.65 2,483.99 -0.38%
torchvision-densenet121_fp16 32 4,786.22 5,004.24 -4.36%
torchvision-inceptionv3 32 2,017.53 2,058.51 -1.99%
torchvision-inceptionv3_fp16 32 4,497.75 4,416.99 1.83%
cadene-inceptionv4 16 462.67 820.61 -43.62% 🔴
cadene-resnext64x4 16 782.73 782.78 -0.01%
slim-mobilenet 64 8,374.55 8,386.36 -0.14%
slim-nasnetalarge 64 71.12 228.86 -68.92% 🔴
slim-resnet50v2 64 3,235.23 3,180.91 1.71%
bert-mrpc-onnx 8 1,167.78 1,168.84 -0.09%
bert-mrpc-tf 1 491.64 498.63 -1.40%
pytorch-examples-wlang-gru 1 483.29 473.35 2.10%
pytorch-examples-wlang-lstm 1 383.33 384.83 -0.39%
torchvision-resnet50_1 1 1,054.59 1,046.63 0.76%
cadene-dpn92_1 1 440.34 437.32 0.69%
cadene-resnext101_1 1 365.82 365.89 -0.02%
onnx-taau-downsample 1 846.83 844.09 0.33%
dlrm-criteoterabyte 1 0.75 32.42 -97.69% 🔴
dlrm-criteoterabyte_fp16 1 51.79 51.80 -0.02%
agentmodel 1 12,002.81 9,209.12 30.34% 🔆
unet_fp16 2 53.60 58.80 -8.85% 🔴
resnet50v1_fp16 1 1,364.69 1,366.11 -0.10%
resnet50v1_int8 1 1,702.69 1,883.96 -9.62% 🔴
bert_base_cased_fp16 64 1,097.85 1,098.16 -0.03%
bert_large_uncased_fp16 32 230.96 345.59 -33.17% 🔴
bert_large_fp16 1 206.53 206.59 -0.03%
distilgpt2_fp16 16 2,091.12 2,092.89 -0.08%
yolov5s 1 553.91 558.33 -0.79%
tinyllama 1 45.82 45.83 -0.02%
vicuna-fastchat 1 44.13 44.20 -0.15%
whisper-tiny-encoder 1 389.45 411.87 -5.44% 🔴
whisper-tiny-decoder 1 407.27 408.48 -0.30%
llama2_7b 1 20.83 20.84 -0.04%
qwen1.5-7b 1 23.58 23.58 0.02%
phi3-3.8b 1 11.33 26.72 -57.60% 🔴
llama3-8b 1 21.61 21.80 -0.89%
whisper-large-encoder 1 10.18 10.18 -0.01%
whisper-large-decoder 1 106.40 105.30 1.05%
mistral-7b 1 23.73 23.78 -0.17%
FLUX.1-schnell 1 770.78 755.22 2.06%

Regressions detected 🔴

* No develop baseline was found for this PR's branch point; compared against the latest available develop run instead.

@gh-app-migraphx-bot-pr-write

gh-app-migraphx-bot-pr-write Bot commented Jul 22, 2026

Copy link
Copy Markdown
Test Status Result
bert-mrpc-onnx PASSED: MIGraphX meets tolerance
bert-mrpc-tf PASSED: MIGraphX meets tolerance
pytorch-examples-wlang-gru PASSED: MIGraphX meets tolerance
pytorch-examples-wlang-lstm PASSED: MIGraphX meets tolerance
dlrm-criteoterabyte PASSED: MIGraphX meets tolerance
agentmodel PASSED: MIGraphX meets tolerance
unet PASSED: MIGraphX meets tolerance
resnet50v1 PASSED: MIGraphX meets tolerance
bert_base_cased_fp16 PASSED: MIGraphX meets tolerance
bert_large_uncased_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
bert_large PASSED: MIGraphX meets tolerance
yolov5s PASSED: MIGraphX meets tolerance
tinyllama PASSED: MIGraphX meets tolerance
vicuna-fastchat PASSED: MIGraphX meets tolerance
whisper-tiny-encoder PASSED: MIGraphX meets tolerance
whisper-tiny-decoder PASSED: MIGraphX meets tolerance
distilgpt2_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
llama2_7b PASSED: MIGraphX meets tolerance
qwen1.5-7b PASSED: MIGraphX meets tolerance
phi3-3.8b PASSED: MIGraphX meets tolerance
llama3-8b PASSED: MIGraphX meets tolerance
whisper-large-encoder PASSED: MIGraphX meets tolerance
whisper-large-decoder PASSED: MIGraphX meets tolerance
mistral-7b PASSED: MIGraphX meets tolerance
FLUX.1-schnell PASSED: MIGraphX meets tolerance

Comment thread src/include/migraphx/op/resolve_sym_expr.hpp Outdated
@shivadbhavsar
shivadbhavsar requested a review from pfultz2 July 23, 2026 22:25
@shivadbhavsar shivadbhavsar changed the title add runtime symbol resolution op [AIMIGRAPHX-1215] add runtime symbol resolution op Jul 24, 2026
@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.83051% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/include/migraphx/op/eval_expr_from_shape.hpp 88.64% 5 Missing ⚠️
src/sym.cpp 91.67% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5085      +/-   ##
===========================================
- Coverage    93.26%   93.25%   -0.01%     
===========================================
  Files          623      624       +1     
  Lines        32969    33027      +58     
===========================================
+ Hits         30747    30799      +52     
- Misses        2222     2228       +6     
Files with missing lines Coverage Δ
src/include/migraphx/sym.hpp 97.87% <ø> (ø)
src/instruction.cpp 90.56% <100.00%> (+0.02%) ⬆️
src/targets/ref/lowering.cpp 95.74% <100.00%> (+0.02%) ⬆️
src/sym.cpp 96.70% <91.67%> (-0.05%) ⬇️
src/include/migraphx/op/eval_expr_from_shape.hpp 88.64% <88.64%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@CharlieL7 CharlieL7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Design looks good. I have questions about some things.

Comment thread src/targets/cpu/lowering.cpp Outdated
Comment thread test/ref/slice.cpp
{{"expressions",
migraphx::value::array{migraphx::to_value(n - migraphx::sym::lit(1))}}}),
x);
mm->add_instruction(migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}}), x, end_vals);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is using the range-based compute_shape() of slice. Where the compute_shape() of slice would output the {0, max_interval} shape. To use the symbolic version you would need to add {"ends", {expr(n - 1)}} to the attributes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thats true, but I need your PR to go in before I can do that I think

Comment thread test/instruction.cpp
#include "test.hpp"
#include "rob.hpp"

struct can_eval_finalize_passthrough

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What's the reason for this logic?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just a test to make sure const-folding doesnt try and fold an on op that requires finalization. This is just a dummy op testing that logic.
For the eval expr op, calling eval before finalize is problematic because the input_shape attribute does not exist yet

MIGRAPHX_THROW("EVAL_EXPR_FROM_SHAPE: input shapes not captured; op was not finalized");

std::unordered_map<sym::expr, std::size_t> values;
for(std::size_t i = 0; i < input_shapes.size(); ++i)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you use for(const auto&[input_shape, arg]:views::zip(input_shapes, args)) instead?

if(not input_shape.symbolic())
continue;
const auto& dims = input_shape.dyn_dims();
for(std::size_t axis = 0; axis < dims.size(); ++axis)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same here, you can use for(const auto&[dim, len]:views::zip(dims, lens))

Comment thread src/sym.cpp
{
auto s = as_symbol(x);
if(seen_variables.insert(s).second)
result.push_back(std::move(s));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you need to push_back here? You could copy it to the vector after the traversal is finished: return {seen_variables.bgin(), seen_variables.end()}

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.

4 participants