TECHNOLOGY

ONNX If outputs: a shared interface must describe both branch shapes

In this article
THE QUICK TAKE

A conditional output annotation must accommodate either valid branch result; an example from one path is not a universal shape declaration.

Exporting one observed path narrows the interface incorrectly

A conditional graph returns a filtered tensor in one branch and an unfiltered tensor in another. During review, an engineer copies the observed output dimensions from the first example into the enclosing conditional output. The other branch then contradicts that annotation. The issue is not necessarily that conditionals require identical extents: the shared output declaration must represent the alternatives allowed by the selected operator schema. Begin with both branches, rather than promoting one execution trace into an unconditional promise.

Branch correspondence is positional and typed

The ONNX If specification describes corresponding branch outputs and the compatible enclosing shape. Consult the imported version, since the page also includes historical schemas. Match output order and types first, then derive an annotation that covers the possible shapes. A single static shape copied from a branch can be too restrictive even when both branches independently perform valid computations and produce the intended semantic result.

Inference cannot erase a real alternative

The shape inference documentation explains symbolic and incomplete shape information. An unknown extent at the merge can accurately describe uncertainty, rather than indicate a failed export. Do not replace it with a convenient constant merely because the downstream adapter wants fixed buffers. The broadcasting guide illustrates why shape declarations have operational consequences: later tensor operations can interpret dimensions differently even when element types remain unchanged.

Validate the graph and the consumer independently

The checker API supplies model validation, but a valid conditional interface is not a guarantee that every execution provider supports its dynamic behavior efficiently or at all. Test both branch conditions through the real consumer. Confirm that output allocation and subsequent operations accommodate the resulting shape. A model that passes only the frequently selected path has not demonstrated support for the other legal result of the same interface.

Preserve the branch contract in release evidence

Record corresponding branch outputs, the enclosing declaration and the examples that exercise each path. Keep any fixed-shape specialization distinct from the general conditional model. The artifact verification guide connects those reviewed declarations to a deployed graph. This makes a shape mismatch explainable as an overly narrow annotation, unsupported consumer assumption or actual branch defect, instead of treating all conditional export failures as a single generic compiler limitation.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles