onnx: control flow — If (constant condition), Loop (static trip count, unrolled) - #91
Merged
Conversation
… 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.
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.
The control-flow tier of the coverage plan. One new piece of infrastructure, two handlers.
Infrastructure
_run_subgraphimports 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_nodesthe 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.stackfor constants). While-style loops raise with a message saying why.docs/onnx.mdgains 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 readingxfrom the outer scope; the Loop accumulator (3 unrolled iterations ofacc += 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.