﻿# ONNX Loop: distinguish carried state from per-iteration scan outputs

Read ONNX Loop body signatures and final outputs without confusing the last carried value with a concatenated iteration history or overlooking scan shape consistency.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-onnx-loop-carried-scan-output-order/
Language: en

## The final hidden state is mistaken for a history tensor

A recurrent computation exports both the updated state and an observation from every iteration. An adapter interprets the first returned tensor as the observation history because it follows the state’s variable name rather than the loop signature. The result can pass shallow dtype checks while exposing the wrong semantic object. Read the enclosing loop interface together with the body interface: the control value, carried dependencies and accumulated observations have different positions and different roles.

## The body and enclosing outputs have distinct signatures

The [Loop operator specification](https://onnx.ai/onnx/operators/onnx__Loop.html) defines the body inputs and outputs, including the condition followed by carried values and scan values. The enclosing loop returns final carried values followed by accumulated scan outputs. Its control result is not simply another externally returned model value. Track each position through the export, especially after graph editing renames intermediates or changes the number of carried dependencies. Names alone cannot establish the correct mapping.

## Accumulation imposes a consistency requirement

Per-iteration scan values must have compatible dimensions and dtype as required by the selected Loop schema. An observation that changes extent during iteration cannot be assumed to concatenate into the same declared result. The [Scan specification](https://onnx.ai/onnx/operators/onnx__Scan.html) offers related iteration semantics, but is a separate operator with its own interface. The [variable-length reduction guide](/journal/variable-length-loss-reduction/) explains why sequence length and aggregation meaning need explicit treatment rather than inference from a tensor’s name.

## Construct positional interfaces deliberately

The [graph helper API](https://onnx.ai/onnx/api/helper.html) exposes graph and node construction. When generating a loop, create an explicit correspondence table for body state inputs, updated state outputs and observations. Validate a case that executes several iterations and distinguish the final state from the collected history numerically. Also consider the permitted stopping behavior; a single-iteration example can hide an ordering error because final state and one observation may coincidentally look similar.

## Explain the output to downstream consumers

Document which result represents the terminal state, which contains a history and which axis reflects iteration accumulation. Connect that explanation to the exact released graph through [artifact verification](/journal/artifact-signature-verification/). A correct adapter should preserve these meanings when converting outputs into application objects. This review targets a control-flow interface defect, not generic GPU memory tuning: improving kernel speed cannot repair an adapter that returns the wrong logical result of the loop.
