Skip to content

Driver and API updates for using sym shapes#4946

Open
shivadbhavsar wants to merge 8 commits into
sym_onnx_parsefrom
sym_driver
Open

Driver and API updates for using sym shapes#4946
shivadbhavsar wants to merge 8 commits into
sym_onnx_parsefrom
sym_driver

Conversation

@shivadbhavsar

@shivadbhavsar shivadbhavsar commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Motivation

Update the driver flags and C/Python APIs to properly allow symbolic shape inputs.

Technical Details

Exposes symbolic dynamic dimensions (sym::expr) through the driver, C API, and Python API.

  • Driver: --enable-symbolic flag; dynamic-dim JSON now accepts a "name" key to make a dim symbolic. Fixed a crash in the single-object --dim-param path.
  • C API: new migraphx_sym_expr type (var/literal/parse, arithmetic ops, to_string); dynamic_dimension(sym_expr) + is_symbolic(); onnx_options setters set_use_symbolic_shapes / set_dim_param.
  • Python: migraphx.sym submodule (expr, var/lit/parse, operators); dynamic_dimension(expr) + is_symbolic(); use_symbolic_shapes on parse_onnx/parse_onnx_buffer, plus dim_params on the buffer variant.
  • Tests: test/api/test_symbolic_shape.cpp and test/py/test_symbolic_shape.py.

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.

@shivadbhavsar shivadbhavsar requested a review from causten as a code owner June 5, 2026 22:23
Copilot AI review requested due to automatic review settings June 5, 2026 22:23
@shivadbhavsar shivadbhavsar changed the base branch from develop to sym_onnx_parse June 5, 2026 22:23
@shivadbhavsar shivadbhavsar self-assigned this Jun 5, 2026

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 extends MIGraphX’s symbolic dynamic-shape support end-to-end by exposing sym::expr through the driver, the C/C++ API surface, and Python bindings, and adds API/Python tests to validate symbolic dimension construction and ONNX parsing behavior.

Changes:

  • Add sym::expr / sym_expr (C API + C++ handle) and enable symbolic dynamic dimensions via dynamic_dimension(sym_expr) and is_symbolic().
  • Extend ONNX options and front-ends (driver + Python bindings) to enable symbolic shapes and pass dim_params.
  • Add new C++ and Python tests for symbolic dynamic dimensions and ONNX parsing.

Reviewed changes

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

Show a summary per file
File Description
src/driver/main.cpp Adds --enable-symbolic and supports "name" in dynamic-dim JSON to create symbolic dims.
src/api/include/migraphx/migraphx.h Introduces C API migraphx_sym_expr_t and related functions; adds symbolic dynamic-dimension APIs.
src/api/include/migraphx/migraphx.hpp Adds C++ sym_expr handle wrapper and dynamic_dimension(sym_expr) / is_symbolic() accessors.
src/api/api.cpp Implements the new C API for sym_expr, symbolic dynamic dimensions, and ONNX option setters.
tools/api/api.cpp Adds helper(s) used by generated bindings for creating symbolic variables and ONNX option setters.
src/api/migraphx.py Exposes the new migraphx_sym_expr handle + operators and ONNX option setters in the generated API.
src/py/migraphx_py.cpp Adds migraphx.sym submodule, symbolic dynamic_dimension ctor, and ONNX parse flags/params in Python.
test/api/test_symbolic_shape.cpp Adds C++ API tests for symbolic dynamic dimensions and ONNX parsing behavior.
test/api/CMakeLists.txt Registers the new C++ API test.
test/py/test_symbolic_shape.py Adds Python tests for symbolic expressions/dimensions and ONNX parsing behavior.
test/py/CMakeLists.txt Registers the new Python test.

Comment thread src/api/api.cpp Outdated
Comment on lines +1081 to +1088
auto api_error_result = migraphx::try_([&] {
if(out == nullptr)
MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter out: Null pointer");
if(sym_expr == nullptr)
MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter sym_expr: Null pointer");
auto&& api_result = (sym_expr->object).to_string();
auto* it = std::copy_n(api_result.begin(), std::min(api_result.size(), out_size - 1), out);
*it = '\0';
Comment thread src/py/migraphx_py.cpp
Comment on lines 860 to 862
"Parse onnx file",
py::arg("filename"),
py::arg("default_dim_value") = 0,
Comment thread src/py/migraphx_py.cpp
Comment on lines 835 to 841
[](const std::string& onnx_buffer,
unsigned int default_dim_value,
migraphx::shape::dynamic_dimension default_dyn_dim_value,
std::unordered_map<std::string, migraphx::shape::dynamic_dimension> dim_params,
std::unordered_map<std::string, std::vector<std::size_t>> map_input_dims,
std::unordered_map<std::string, std::vector<migraphx::shape::dynamic_dimension>>
map_dyn_input_dims,
Comment thread src/api/api.cpp Outdated
return sym::var(name, {static_cast<int64_t>(min), static_cast<int64_t>(max)});
}

static sym::expr make_sym_var(const char* name, size_t min, size_t max, std::set<size_t> optimals)
Comment thread tools/api/api.cpp Outdated
return sym::var(name, {static_cast<int64_t>(min), static_cast<int64_t>(max)});
}

static sym::expr make_sym_var(const char* name, size_t min, size_t max, std::set<size_t> optimals)
@codecov

codecov Bot commented Jun 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.07407% with 21 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/api/api.cpp 67.21% 20 Missing ⚠️
src/api/include/migraphx/migraphx.hpp 95.00% 1 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                 @@
##           sym_onnx_parse    #4946      +/-   ##
==================================================
- Coverage           92.72%   92.69%   -0.03%     
==================================================
  Files                 592      589       -3     
  Lines               31355    31344      -11     
==================================================
- Hits                29072    29053      -19     
- Misses               2283     2291       +8     
Files with missing lines Coverage Δ
src/api/include/migraphx/migraphx.hpp 98.91% <95.00%> (-0.14%) ⬇️
src/api/api.cpp 74.31% <67.21%> (+0.43%) ⬆️

... and 5 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/api/include/migraphx/migraphx.hpp Outdated
/**
* @brief Symbolic expression used to describe symbolic dynamic dimensions.
*/
struct sym_expr : MIGRAPHX_HANDLE_BASE(sym_expr)

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.

We should not expose the sym_expr to the API. The symbolic symbols should be passed as a string and then we use sym::parse to convert it to a symbolic expression.

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.

how do we set the intervals and optimals that way?

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.

I assume thats done with the dynamic_dimension class, but now that I think about it more, it seems very clunky since the user will need to add a dynamic_dimension for the same dim_param they are setting. It seems like it would be better if the user sets a dynamic_dimension with just the interval and optimals and then we apply the symbols to it based on the onnx parsing.

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.

this was there to allow a user to create a symbolic dynamic dimension via the API like we allow creating a range based dynamic shape currently. ONNX parsing already applies interval and optimals if you use default_dyn_dim/dim_params. This is to allow them to set the shape using map_dyn_input_dims

Comment thread src/api/include/migraphx/migraphx.hpp Outdated
&migraphx_dynamic_dimension_create_min_max_optimals, min, max, opts.get_handle_ptr());
}

dynamic_dimension(const sym_expr& expr) // NOLINT(google-explicit-constructor)

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 should be dynamic_dimension(const std::string& expr).

Comment thread src/api/include/migraphx/migraphx.hpp Outdated
call(&migraphx_onnx_options_set_use_debug_symbols, this->get_handle_ptr(), value);
}

void set_use_symbolic_shapes(bool value = true)

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.

I dont really like exposing this to the API like this since it should become the default and then this API does nothing and should be ignored.

@shivadbhavsar shivadbhavsar requested a review from pfultz2 June 11, 2026 00:43
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.

3 participants