Skip to content

[MIGraphX] Fix handling of Loop/If/Scan subgraphs and Loops with non-constant / huge maximum iteration count - #31978

Open
Sebastian Dröge (sdroege) wants to merge 2 commits into
microsoft:mainfrom
sdroege:migraphx-loop-if-scan-subgraphs
Open

[MIGraphX] Fix handling of Loop/If/Scan subgraphs and Loops with non-constant / huge maximum iteration count#31978
Sebastian Dröge (sdroege) wants to merge 2 commits into
microsoft:mainfrom
sdroege:migraphx-loop-if-scan-subgraphs

Conversation

@sdroege

@sdroege Sebastian Dröge (sdroege) commented Aug 11, 2026

Copy link
Copy Markdown

Description

See individual commits. This fixes two separate issues in the MIGraphX execution provider:

  1. Fix handling of Loop with non-constant, dynamic or huge iterations in MIGraphX execution provider

    Consider such cases as unsupported ops, as MIGraphX clamps them to
    65535 iterations, which a) does not work for non-constant iterations, b)
    doesn't give the desired result for higher iteration counts, and c) uses
    huge amounts of memory.

  2. Fix handling of Loop/If/Scan subgraphs in MIGraphX execution provider

    The GraphPartitioner is recursing bottom-up into Loop/If/Scan
    subgraphs, and the EP then creates new MGXKernel_subgraph_* ops for
    them inside the body. Later during compilation, MIGraphX's parser
    recurses into the body, finds the new unknown ops and fails with

    Unknown operator: MGXKernel_subgraph_tf2onnx_...

    To avoid that, early return in these cases and consider them
    unsupported, falling back to the CPU instead for them.

Motivation and Context

This fixes #31713 but the model there still does not load correctly because of what I think is a bug in MIGraphX itself (ROCm/AMDMIGraphX#5126).

The `GraphPartitioner` is recursing bottom-up into Loop/If/Scan
subgraphs, and the EP then creates new `MGXKernel_subgraph_*` ops for
them inside the body. Later during compilation, MIGraphX's parser
recurses into the body, finds the new unknown ops and fails with

  Unknown operator: MGXKernel_subgraph_tf2onnx_...

To avoid that, early return in these cases and consider them
unsupported, falling back to the CPU instead for them.

Fixes microsoft#31713
… MIGraphX execution provider

Consider such cases as unsupported ops, as MIGraphX clamps them to
65535 iterations, which a) does not work for non-constant iterations, b)
doesn't give the desired result for higher iteration counts, and c) uses
huge amounts of memory.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@sdroege

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Centricular"

@sdroege

Copy link
Copy Markdown
Author

Doesn't look like @-ing the bot works.

@sdroege Sebastian Dröge (sdroege) changed the title Migraphx loop if scan subgraphs Fix handling of Loop/If/Scan subgraphs and Loops with non-constant / huge maximum iteration count Aug 11, 2026
@sdroege Sebastian Dröge (sdroege) changed the title Fix handling of Loop/If/Scan subgraphs and Loops with non-constant / huge maximum iteration count Fix handling of Loop/If/Scan subgraphs and Loops with non-constant / huge maximum iteration count in MIGraphX execution provider Aug 11, 2026
@sdroege Sebastian Dröge (sdroege) changed the title Fix handling of Loop/If/Scan subgraphs and Loops with non-constant / huge maximum iteration count in MIGraphX execution provider [MIGraphX] Fix handling of Loop/If/Scan subgraphs and Loops with non-constant / huge maximum iteration count Aug 11, 2026

@tianleiwu Tianlei Wu (tianleiwu) 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.

The control-flow body partitioning and recursive support checks look sound. Requesting changes because the Loop upper bound is not enforced for valid typed-field or external-data initializers, leaving the wrong-result and excessive-memory case reachable. Please also add focused capability tests for supported/unsupported nested bodies and trip counts around 65,535, including both raw_data and int64_data encodings.

if (!canEvalNodeArgument(graph_viewer, node, {0}, m_input_nodes)) {
return true;
}
// constant-foldable but not a readable initializer, accept it as a best effort

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.

Blocking: This best-effort path still accepts large constant trip counts when the initializer is not encoded as exactly eight bytes of raw_data. ONNX can store an INT64 initializer in int64_data or external data; canEvalNodeArgument() returns true for any initializer without decoding its value. For example, int64_data: 100000 reaches this branch, is assigned to MIGraphX, and is then clamped to 65,535, preserving the wrong-result/excessive-memory behavior this change aims to prevent. Please decode all scalar initializer representations (for example via ORT's Initializer or utils::UnpackTensor) and reject the Loop unless the actual value is available and <= 65,535. That also avoids the alignment-unsafe reinterpret_cast<int64_t*> read in the raw-data branch.

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.

[MIGraphX EP] Fails processing models with multiple layers of sub-graphs with "Unknown operator: MGXKernel_subgraph_tf2onnx_9255412890226604894_0"

2 participants