The surgeon sanitize subtool can be used to fold constants in graphs,
remove unused nodes, and topologically sort nodes. In cases where shapes
are statically known, it can also simplify subgraphs involving shape operations.
In this example, we'll fold constants in a graph that computes output = input + ((a + b) + d),
where a, b, and d are constants:
-
Fold constants with:
polygraphy surgeon sanitize model.onnx \ --fold-constants \ -o folded.onnxThis collapses
a,b, anddinto a constant tensor, and the resulting graph computesoutput = input + e: -
[Optional] You can use
inspect modelto confirm whether it looks correct:polygraphy inspect model folded.onnx --show layers

