Repository navigation
Add graph surgery for scalar tensor constants - #2791
ananyaa-ms wants to merge 5 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The surgery can replace unresolved intermediate metadata with an incompatible INT64 tensor and emit an invalid Constant for opset-11 models.
2 open issues
What changed in this PR
Adds ReplaceTensorWithConstant to Olive’s ONNX graph surgeries, allowing a named scalar INT64 tensor’s producer to be replaced with a constant while pruning unused upstream nodes.
Changes:
- Adds tensor lookup, producer validation, and signed 64-bit value validation.
- Preserves shared outputs while removing unused computation.
- Adds runtime checks and tests for invalid targets and replacement values.
| File | Description |
|---|---|
test/passes/onnx/test_graph_surgeries.py |
Tests pruning, shared-output preservation, runtime results, and invalid inputs. |
olive/passes/onnx/graph_surgeries.py |
Implements constant replacement and unused-node cleanup. |
Review effort: Balanced
There was a problem hiding this comment.
I feel like this is something that can be generalized. For example, would it make sense if you allow a map as input, so users don't need to run the pass multiple times to replace tensors? Additionally, it seems like we can just allow users to replace some value with some arbitrary dtype matching constant?
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove the hardcoded dtype allowlist and reject lossy integer conversions and out-of-range values while supporting safely representable ONNX constants. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Updated the pass to use a map to replace multiple tensors, and to accept any dtype the installed ONNX libraries can safely encode from JSON |

Describe your changes
Add
ReplaceTensorsWithConstants, which accepts a map of named ONNX tensors to scalar or array values. It checks each target’s concrete shape and supported numeric or BOOL dtype, infers missing metadata when possible, and validates every replacement before changing the graph. It then uses opset-11-compatible tensor-valuedConstantnodes and prunes unused upstream nodes.Tests cover multi-tensor replacements across dtypes and shapes, shape inference, pruning and runtime output, plus invalid inputs, atomic failure, and opset 11 compatibility.
Checklist before requesting a review
lintrunner -a(Optional) Issue link