Skip to content

onnx: control flow — If (constant condition), Loop (static trip count, unrolled) - #91

Merged
sbryngelson merged 1 commit into
mainfrom
onnx/control-flow
Jul 22, 2026
Merged

onnx: control flow — If (constant condition), Loop (static trip count, unrolled)#91
sbryngelson merged 1 commit into
mainfrom
onnx/control-flow

Conversation

@sbryngelson

Copy link
Copy Markdown
Owner

The control-flow tier of the coverage plan. One new piece of infrastructure, two handlers.

Infrastructure

_run_subgraph imports an ONNX subgraph (If branch / Loop body) with proper ONNX scoping: a scope stack makes enclosing-graph names visible inside the subgraph, subgraph initializers and formal inputs bind on top, and the node walk is the same _run_nodes the top-level importer now uses.

Handlers

  • If — constant condition only; the taken branch imports inline and the untaken branch is never built. A tensor condition raises: a compiled program is one static graph.
  • Loop — constant trip count (≤1024) with an absent/constant-true condition that the body keeps true. The body unrolls into the single fused program: iteration numbers bind as int64 constants (so iteration-dependent shape arithmetic folds through the existing const machinery), loop-carried values thread across iterations, and scan outputs stack along a new leading axis per the spec (expand_dims + concat for tensors, np.stack for constants). While-style loops raise with a message saying why.

docs/onnx.md gains a Control flow table row and a scope note.

Verification (on-device, M5)

pytest tests/test_onnx.py: 159 passed — If imports the correct branch for both condition values with the branch reading x from the outer scope; the Loop accumulator (3 unrolled iterations of acc += x) and the [3,1,4] stacked scan output both match onnxruntime; tensor-condition If and while-style Loop reject. ruff, 2-space pylint, pyright (0 errors) clean.

… static trip count

New subgraph importer (_run_subgraph): outer-scope names resolve per ONNX
scoping via a scope stack, subgraph initializers and formal inputs bind on
top, and the node walk is shared with the top-level importer (_run_nodes).

If requires a constant condition and imports only the taken branch. Loop
requires a constant trip count (capped at 1024) and a constant-true
condition that the body keeps true; the body unrolls into the single
program, iteration numbers bind as int64 constants (so iter-dependent
shape arithmetic folds), carried values thread through, and scan outputs
stack along a new leading axis per the spec. Data-dependent branching or
termination raises: a compiled program is one static graph, and
docs/onnx.md now says so.

159 tests pass on-device (M5): both If branches with outer-scope reads
from inside the branch, the Loop accumulator and stacked scan output
against onnxruntime, and the while-style/tensor-condition reject paths.
@sbryngelson
sbryngelson merged commit 4c616c2 into main Jul 22, 2026
13 checks passed
@sbryngelson
sbryngelson deleted the onnx/control-flow branch July 22, 2026 22:37
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.

1 participant