Python and Cpp symbolic shape printing - #5205
Conversation
The generated-code printers called shape::lens() unconditionally, so any program holding a dynamic shape made them throw "SHAPE: lens() called on a dynamic shape". A range-based dynamic dimension now prints as its bounds. A symbolic one carries an expression no constructor argument can spell, so it prints as the json form of its value representation via the new migraphx::make_json_shape and migraphx.shape.from_json, which round trips the expression, its per-variable bounds and optimals, and any symbolic strides. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes --cpp/--py printer failures when modules/programs contain dynamic shapes by avoiding shape.lens() on dynamic shapes, and introduces a JSON-based round-trip path (shape::from_json) to preserve symbolic dynamic-dimension expressions when printing reconstructible source.
Changes:
- Added
migraphx::shape::from_json(std::string)(and Pythonmigraphx.shape.from_json) to rebuild shapes from their JSON value representation. - Updated module printers to emit readable range-based dynamic dimensions, and to fall back to
from_json(...)when a shape contains symbolic dimensions. - Added C++ and Python tests to ensure printed code round-trips symbolic expressions and dynamic-dimension optimals.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/shape_test.cpp | Adds round-trip tests for shape::from_json across static, dynamic-range, symbolic, and tuple shapes. |
| test/py/test_symbolic_shape.py | Ensures program.to_py() preserves symbolic expressions and dyn-dim optimals via shape.from_json. |
| test/module_test.cpp | Validates module.print_cpp/print_py round-trip behavior for symbolic shapes and keeps range-based dyn shapes readable. |
| src/shape.cpp | Implements shape::from_json using from_json_string + from_value<shape>. |
| src/py/migraphx_py.cpp | Exposes shape.from_json in the Python bindings. |
| src/module.cpp | Updates C++/Python shape printing to handle dynamic and symbolic dims without calling lens() on dynamic shapes. |
| src/include/migraphx/shape.hpp | Declares and documents shape::from_json. |
| docs/reference/MIGraphX-py.rst | Documents the new Python shape.from_json API. |
| CHANGELOG.md | Adds an entry for the new API and the printer crash fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| static void print_json_shape(std::ostream& os, const std::string& factory, const migraphx::shape& s) | ||
| { | ||
| os << factory << "(" << enclose_name(to_json_string(migraphx::to_value(s))) << ")"; | ||
| } |
|
I dont really like dumping the raw json, can we just have |
Would need to add quite a few python bindings. Having AI test it out |
Motivation
Technical Details
Changelog Category
Add a
CHANGELOG.mdentry for any option other thanNot ApplicableFollow the LLVM AI Tool Use Policy for contributions using AI.