﻿# ONNX Reshape: zero-copy dimension markers differ from literal empty dimensions

Interpret ONNX Reshape shape values with allowzero and inferred dimensions explicitly, especially when empty inputs reach a graph exported from nonempty examples.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-onnx-reshape-allowzero-inferred-dimension/
Language: en

## A target shape is not just a list of extents

A graph editor sees a zero in a Reshape target tensor and concludes that the result must be empty. Under the ordinary marker convention, that entry instead copies the corresponding input dimension. Confusing a marker with a literal extent can break batching or sequence handling without changing the number of visible shape entries. Inspect the node attributes and imported schema before interpreting a shape tensor as a fully concrete allocation request.

## allowzero selects the interpretation

The [Reshape specification](https://onnx.ai/onnx/operators/onnx__Reshape.html) describes the default copy behavior and literal-zero behavior selected by `allowzero`. It also restricts combining literal zeros with an inferred dimension marker. A shape containing `0` and `-1` therefore needs interpretation under the actual attribute setting, not a generic arithmetic shortcut. Copying a dimension is a statement about the corresponding input axis; inferring a dimension uses the element-count relationship under the operator’s rules.

## Unknown metadata is another separate case

The [shape inference guide](https://onnx.ai/onnx/repo-docs/ShapeInference.html) distinguishes symbolic or unknown extents from known integer values. An unknown annotation does not imply that the runtime shape tensor contains a zero. Keep type metadata and actual shape data separate in diagnostics. The [tensor descriptor guide](/journal/learn-tensor-memory-descriptor/) explains related distinctions among dimensions, offsets and strides. A change in logical shape should not be casually interpreted as a guarantee about storage aliasing or allocation.

## Validate the graph with representative shape values

The [ONNX checker](https://onnx.ai/onnx/api/checker.html) helps establish structural validity after a rewrite. Behavioral validation must additionally exercise the target shape values that the application actually supplies. Compare a copied nonempty dimension, a supported empty case and an inferred-dimension case where appropriate. If the application forbids empty tensors, document that restriction at its input boundary instead of relying on a graph annotation accidentally excluding an untested value.

## Keep marker semantics visible in adapters

An adapter that serializes shapes should preserve the attribute-controlled distinction rather than replacing all markers with guessed sizes. Explain the source of each target-shape entry and retain the original graph for comparison. The [explicit schema guide](/journal/data-guide-jsonlines-explicit-schema/) offers the same practical principle for input interpretation: a sentinel’s meaning belongs to its contract. Here that contract is the selected Reshape definition and its attributes, not the visual appearance of the integer list alone.
