Command-line interface#
onnx-light ships a small command-line interface that can be invoked as a
Python module:
python -m onnx_light <subcommand> [options]
fillshape#
Fills a model’s graph.value_info and graph.output with the shapes
inferred by the onnx_optim 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]
[--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 keyonnx_light.release_after.--shape-tagAfter shape inference, infer semantic
shape/axes/weight/ambiguoustags for every value and node in the graph and record them inmetadata_props(keysonnx_light.value_tagsandonnx_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 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.
show#
Loads an ONNX model and renders it as plain text, a Mermaid flowchart, an SVG image or Graphviz DOT source. The result is written to stdout by default.
python -m onnx_light show model.onnx
Synopsis:
python -m onnx_light show MODEL [--format {pretty,mermaid,svg,dot}]
[--output OUTPUT]
[--shape-inference]
[--no-shapes]
[--include-attributes]
[--include-inplace]
[--include-node-tags]
[--no-initializers]
[--direction DIRECTION]
[--graphviz GRAPHVIZ_FORMAT]
Positional argument#
MODELPath to the input
.onnxmodel file.
Options#
--format {pretty,mermaid,svg,dot}/-f {pretty,mermaid,svg,dot}Output format (default:
pretty).pretty— compact text listing produced bypretty_onnx().mermaid— Mermaid flowchart source that can be embedded in Markdown or rendered with the Mermaid CLI.svg— SVG image written to stdout (or to the file given by--output).dot— Graphviz DOT source produced byto_dot().
--output OUTPUT/-o OUTPUTWrite the rendered output to OUTPUT instead of printing to stdout.
--shape-inferenceRun onnx-light shape inference on the model before rendering.
--no-shapesSuppress shape annotations in the rendered output (applies to
mermaid,svganddotformats).--include-attributesInclude node attributes in the rendered output.
--include-inplaceShow in-place buffer-reuse annotations (
onnx_light.inplace_reusemetadata).--include-node-tagsShow semantic shape/axes/weight/ambiguous node-tag annotations (
onnx_light.node_tagmetadata). Only used by theprettyformat.--no-initializersExclude initializer nodes from the rendered graph (applies to
mermaid,svganddotformats).--direction DIRECTIONFlowchart direction for
mermaid,svganddotformats. One ofTB(default),LR,TDorBT(Mermaid only).--graphviz GRAPHVIZ_FORMATInvoke the Graphviz
dotexecutable on the generated DOT source and write the rendered image in GRAPHVIZ_FORMAT (e.g.png,svg,pdf). Only used when--format dotis given. Requires Graphviz to be installed anddotto be available onPATH.
Examples#
Print a compact text summary to stdout:
python -m onnx_light show model.onnx
Run shape inference first, then show the model:
python -m onnx_light show model.onnx --shape-inference
Render a Mermaid flowchart and save it to a file:
python -m onnx_light show model.onnx --format mermaid -o model.mmd
Generate an SVG with a left-to-right layout:
python -m onnx_light show model.onnx --format svg --direction LR -o model.svg
Show the model without initializer nodes and without shape annotations:
python -m onnx_light show model.onnx --format mermaid --no-initializers --no-shapes
Dump the Graphviz DOT source to a file:
python -m onnx_light show model.onnx --format dot -o model.dot
Render a PNG image via Graphviz (requires dot on PATH):
python -m onnx_light show model.onnx --format dot --graphviz png -o model.png
See also#
pretty_onnx()— the Python function used by theprettyformat.to_dot()— the Python function used by thedotformat.How-to Python / C++ — other onnx-light how-to recipes.
run#
Generates random inputs for every graph input using the onnx-light deterministic pseudo-random generators and runs the model through the onnx-light runtime.
python -m onnx_light run model.onnx
Synopsis:
python -m onnx_light run MODEL [--dim NAME=VALUE ...]
[--seed SEED]
[--verbose [LEVEL]]
[--dump PATH]
Positional argument#
MODELPath to the input
.onnxmodel file.
Options#
--dim NAME=VALUEOverride a symbolic (dynamic) dimension by name. May be given multiple times (e.g.
--dim batch=4 --dim seq=16). Dimensions that have no concrete value and are not covered by--dimdefault to1.--seed SEEDInteger seed for the random input generator (default:
0).--verbose [LEVEL]/-v [LEVEL]Print run progress. When passed without a level (
--verbose), level1is used.Level
1prints loading progress, input shapes and output shapes.Level
2also prints per-node execution details.
--dump PATHWrite all inputs and outputs to PATH as an ONNX model whose
graph.initializercontains oneTensorProtoper tensor. Non-array outputs (e.g. sequences) are skipped.
Examples#
Run a model with random inputs:
python -m onnx_light run model.onnx
Set a specific batch size and sequence length for dynamic models:
python -m onnx_light run model.onnx --dim batch=4 --dim seq=128
Use a fixed random seed for reproducible results:
python -m onnx_light run model.onnx --seed 42
Print input/output shapes and execution progress:
python -m onnx_light run model.onnx --verbose
python -m onnx_light run model.onnx --verbose 2
Save the inputs and outputs to a file for later inspection:
python -m onnx_light run model.onnx --dump io_tensors.onnx
See also#
ReferenceEvaluator— the Python evaluator used under the hood.make_random_input()— the public helper used to synthesize each input tensor.How-to Python / C++ — other onnx-light how-to recipes.