fillshape#

Fills a model’s graph.value_info and graph.output with the shapes inferred by the onnx_shapes shape-inference engine, then writes the result back to disk.

python -m onnx_light fillshape model.onnx

Synopsis:

python -m onnx_light fillshape MODEL [--output OUTPUT] [--keep]
                                     [--inplace-info] [--release-info]
                                     [--peak-memory] [--shape-tag] [--show]
                                     [--token NAME=LOW:HIGH ...]
                                     [--verbose [LEVEL]]

Positional argument#

MODEL

Path to the input .onnx model file.

Options#

--output OUTPUT / -o OUTPUT

Write the result to OUTPUT instead of overwriting MODEL in place.

When the model stores weights in a separate file (external data), the output .onnx file is always placed next to the original model regardless of the directory given to --output, so that the relative weight-file paths encoded in the proto remain valid. The weight file is not written again.

--keep

Seed the inference context from any shapes already present in the model’s graph.value_info and graph.output (prefill_with_value_info_output=True). Existing non-conflicting shapes are kept as anchors.

--inplace-info

After shape inference, compute in-place buffer-reuse opportunities and record them in each eligible node’s metadata_props under the key onnx_light.inplace_reuse.

--release-info

After shape inference, compute last-use release hints and record them in each eligible node’s metadata_props under the keys onnx_light.release_after and onnx_light.not_used_after.

--peak-memory

Compute estimated peak scratch memory for nodes with a registered peak-memory function and concrete input shapes. The estimate is stored in metadata_props under onnx_light.peak_memory. Unsupported or dynamic nodes are left unchanged.

--shape-tag

After shape inference, infer semantic shape/axes/weight/ambiguous tags for every value and node in the graph and record them in metadata_props (per-value key onnx_light.value_tag on each ValueInfoProto/initializer, and per-node key onnx_light.node_tag).

--token NAME=LOW:HIGH

Bind a symbolic dimension token to an inclusive integer range before running shape inference. May be specified multiple times.

  • --token seq=1:128 — treats seq as having range [1, 128] and uses 1 (the lower bound) for shape propagation.

Symbolic dims not covered by --token remain symbolic. Providing this option forces the use of the ShapesContext-based path.

--show

Print the model with inferred shapes to stdout using pretty_onnx(); do not save the model.

--verbose [LEVEL]

Prints shape-inference progress information.

When passed without a level (--verbose), level 1 is used and a short summary is printed. Level 2 (--verbose 2) also prints per-event details.

Examples#

Fill shapes and overwrite the file in place:

python -m onnx_light fillshape model.onnx

Write the result to a separate file:

python -m onnx_light fillshape model.onnx -o model_with_shapes.onnx

Preserve existing symbolic dimensions as anchors:

python -m onnx_light fillshape model.onnx --keep

Bind symbolic dimensions to ranges for shape propagation:

python -m onnx_light fillshape model.onnx --token batch=1:8 --token seq=1:128

Bind a symbolic dimension to a range (lower bound used for inference):

python -m onnx_light fillshape model.onnx --token seq=1:512

Annotate nodes with in-place buffer-reuse information:

python -m onnx_light fillshape model.onnx --inplace-info

Annotate nodes with release hints:

python -m onnx_light fillshape model.onnx --release-info

Annotate supported nodes with estimated peak scratch memory:

python -m onnx_light fillshape model.onnx --peak-memory

Annotate values and nodes with semantic shape/axes/weight/ambiguous tags:

python -m onnx_light fillshape model.onnx --shape-tag

Print inferred shapes without saving:

python -m onnx_light fillshape model.onnx --show

Print shape-inference events:

python -m onnx_light fillshape model.onnx --verbose
python -m onnx_light fillshape model.onnx --verbose 2

External-data models#

When a model stores its weight tensors in a separate .data file, fillshape handles it transparently:

  • The model is loaded without reading the weight bytes (they are not needed for shape inference).

  • When --output is used, the output .onnx file is placed in the same directory as the input model so that the relative paths to the weight file remain correct. No new weight file is created.

# model.onnx references model.onnx.data for its weights
python -m onnx_light fillshape model.onnx -o model_filled.onnx
# model_filled.onnx is written next to model.onnx (not in the cwd)
# model.onnx.data is untouched

See also#