Skip to content

Python and Cpp symbolic shape printing - #5205

Open
CharlieL7 wants to merge 6 commits into
developfrom
python_cpp_symbolic_shape_printing
Open

Python and Cpp symbolic shape printing#5205
CharlieL7 wants to merge 6 commits into
developfrom
python_cpp_symbolic_shape_printing

Conversation

@CharlieL7

@CharlieL7 CharlieL7 commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Motivation

  • The cpp and python printers from module.cpp fail when printing dynamic shapes. Calls shape.lens() on a dynamic shape. Add support for printing them.

Technical Details

  • Add shape::from_json() to create a shape from a json value type.

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.

CharlieL7 and others added 5 commits August 26, 2026 15:16
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
CharlieL7 marked this pull request as ready for review August 27, 2026 21:10
@CharlieL7
CharlieL7 requested review from a team and causten as code owners August 27, 2026 21:10
@CharlieL7
CharlieL7 requested review from kahmed10 and shivadbhavsar and a lite review from Copilot August 27, 2026 21:10

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 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 Python migraphx.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 thread src/module.cpp
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))) << ")";
}
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.

2 participants