Driver and API updates for using sym shapes#4946
Conversation
There was a problem hiding this comment.
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 viadynamic_dimension(sym_expr)andis_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. |
| 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'; |
| "Parse onnx file", | ||
| py::arg("filename"), | ||
| py::arg("default_dim_value") = 0, |
| [](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, |
| 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) |
| 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 Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
| /** | ||
| * @brief Symbolic expression used to describe symbolic dynamic dimensions. | ||
| */ | ||
| struct sym_expr : MIGRAPHX_HANDLE_BASE(sym_expr) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
how do we set the intervals and optimals that way?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| &migraphx_dynamic_dimension_create_min_max_optimals, min, max, opts.get_handle_ptr()); | ||
| } | ||
|
|
||
| dynamic_dimension(const sym_expr& expr) // NOLINT(google-explicit-constructor) |
There was a problem hiding this comment.
This should be dynamic_dimension(const std::string& expr).
| call(&migraphx_onnx_options_set_use_debug_symbols, this->get_handle_ptr(), value); | ||
| } | ||
|
|
||
| void set_use_symbolic_shapes(bool value = true) |
There was a problem hiding this comment.
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.
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.--enable-symbolicflag; dynamic-dim JSON now accepts a"name"key to make a dim symbolic. Fixed a crash in the single-object--dim-parampath.migraphx_sym_exprtype (var/literal/parse, arithmetic ops,to_string);dynamic_dimension(sym_expr)+is_symbolic();onnx_optionssettersset_use_symbolic_shapes/set_dim_param.migraphx.symsubmodule (expr,var/lit/parse, operators);dynamic_dimension(expr)+is_symbolic();use_symbolic_shapesonparse_onnx/parse_onnx_buffer, plusdim_paramson the buffer variant.test/api/test_symbolic_shape.cppandtest/py/test_symbolic_shape.py.Changelog Category
Add a
CHANGELOG.mdentry for any option other thanNot Applicable