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#
MODELPath to the input
.onnxmodel file.
Options#
--output OUTPUT/-o OUTPUTWrite the result to OUTPUT instead of overwriting MODEL in place.
When the model stores weights in a separate file (external data), the output
.onnxfile 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.--keepSeed the inference context from any shapes already present in the model’s
graph.value_infoandgraph.output(prefill_with_value_info_output=True). Existing non-conflicting shapes are kept as anchors.--inplace-infoAfter shape inference, compute in-place buffer-reuse opportunities and record them in each eligible node’s
metadata_propsunder the keyonnx_light.inplace_reuse.--release-infoAfter shape inference, compute last-use release hints and record them in each eligible node’s
metadata_propsunder the keysonnx_light.release_afterandonnx_light.not_used_after.--peak-memoryCompute estimated peak scratch memory for nodes with a registered peak-memory function and concrete input shapes. The estimate is stored in
metadata_propsunderonnx_light.peak_memory. Unsupported or dynamic nodes are left unchanged.--shape-tagAfter shape inference, infer semantic
shape/axes/weight/ambiguoustags for every value and node in the graph and record them inmetadata_props(per-value keyonnx_light.value_tagon each ValueInfoProto/initializer, and per-node keyonnx_light.node_tag).--token NAME=LOW:HIGHBind a symbolic dimension token to an inclusive integer range before running shape inference. May be specified multiple times.
--token seq=1:128— treatsseqas having range[1, 128]and uses1(the lower bound) for shape propagation.
Symbolic dims not covered by
--tokenremain symbolic. Providing this option forces the use of theShapesContext-based path.--showPrint 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), level1is used and a short summary is printed. Level2(--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
--outputis used, the output.onnxfile 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#
infer_shapes_model()— the Python function used under the hood.html_theme.sidebar_secondary.remove — how to plug in a callback for custom operators before running shape inference.
How-to Python / C++ — other onnx-light how-to recipes.