Python and Cpp symbolic shape printing - #5205
Open
CharlieL7 wants to merge 6 commits into
Open
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>
CharlieL7
marked this pull request as ready for review
August 27, 2026 21:10
CharlieL7
requested review from
kahmed10 and
shivadbhavsar
and
a lite review from Copilot
August 27, 2026 21:10
Contributor
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.
Comment on lines
+1661
to
+1664
| 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))) << ")"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.