Skip to content

Add getDimName to DimAnalysis to get the name of a dynamic dimension - #3638

Open
tungld wants to merge 3 commits into
onnx:mainfrom
tungld:get_dim_name
Open

Add getDimName to DimAnalysis to get the name of a dynamic dimension#3638
tungld wants to merge 3 commits into
onnx:mainfrom
tungld:get_dim_name

Conversation

@tungld

@tungld tungld commented Sep 4, 2026

Copy link
Copy Markdown
Member

The name of a dynamic dimension comes from the onnx.dim_params attribute of a function argument/result when the model provides one, e.g. "batch_size". Otherwise a name is synthesized from the argument itself, using its onnx.name attribute or its position, e.g. "X_1" or "arg0_0".

Since all the dynamic dimensions of a group are equal at runtime, one name is elected per group, and a dimension of an intermediate tensor is named as soon as the analysis proves it equal to a named one. Groups are named at the end of analyze(), where the sets are final, because merging moves dimensions between sets and makes set ids disappear. Dimensions are indexed by their set at the same time so that looking up a name does not traverse the sets.

The --onnx-dim-analysis pass exposes the elected name as the new optional group_name attribute of onnx.DimGroup, which is how the LIT tests check this, and dump() prints the name of each set.

Example to show how to call getDimName:

DimAnalysis dimAnalysis(moduleOp);
dimAnalysis.analyze();

Value res : op->getResults();
int64_t axis = -1; // Last dimension.
std::optional<std::string> name = dimAnalysis.getDimName(res, axis);

The name of a dynamic dimension comes from the onnx.dim_params attribute
of a function argument/result when the model provides one, e.g.
"batch_size". Otherwise a name is synthesized from the argument itself,
using its onnx.name attribute or its position, e.g. "X_1" or "arg0_0".
Function results are never named that way, so a name always traces back
to a value the caller supplies.

Since all the dynamic dimensions of a group are equal at runtime, one
name is elected per group, and a dimension of an intermediate tensor is
named as soon as the analysis proves it equal to a named one. Groups are
named at the end of analyze(), where the sets are final, because merging
moves dimensions between sets and makes set ids disappear. Dimensions
are indexed by their set at the same time so that looking up a name does
not traverse the sets.

The --onnx-dim-analysis pass exposes the elected name as the new
optional group_name attribute of onnx.DimGroup, which is how the LIT
tests check this, and dump() prints the name of each set.

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

@AlexandreEichenberger AlexandreEichenberger left a comment

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.

LGTM, it should be helpful to decisions for parallel.

I already "propagated" the names in utils/AnalyzeShape.py. If you have the time, maybe try to just run that script once to see if it needs fixing because of the new info.

: name(std::move(name)), fromDimParam(fromDimParam), argPos(argPos),
dimPos(dimPos) {}

// Returns true if this name is a better candidate than `other`.

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.

just curious how this works? if you have a name "arg0_2" already but the new name is "batch_size", then it would keep the "batch_size" name?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A name from dim_params has the highest priority, so "batch_size" is used.

Signed-off-by: Tung D. Le <tungld@gmail.com>
Signed-off-by: Tung D. Le <tung@jp.ibm.com>
@tungld

tungld commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

I already "propagated" the names in utils/AnalyzeShape.py. If you have the time, maybe try to just run that script once to see if it needs fixing because of the new info.

Yes, changed the script to use the new info (group_namein DimGroup) instead of "propagate" again.

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