Skip to content

Add graph surgery for scalar tensor constants - #2791

Open
ananyaa-ms wants to merge 5 commits into
mainfrom
aamancherla/gemma4-onehot-depth-surgery
Open

ananyaa-ms wants to merge 5 commits into
mainfrom
aamancherla/gemma4-onehot-depth-surgery

Conversation

@ananyaa-ms

@ananyaa-ms ananyaa-ms commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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-valued Constant nodes 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

  • Add unit tests for this change.
  • Make sure all tests can pass.
  • Update documents if necessary.
  • Lint and apply fixes to your code by running lintrunner -a
  • Is this a user-facing change? If yes, give a description of this change to be included in the release notes.

(Optional) Issue link

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ananyaa-ms
ananyaa-ms marked this pull request as ready for review November 6, 2026 20:50
Copilot AI balanced review requested due to automatic review settings November 6, 2026 20:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread olive/passes/onnx/graph_surgeries.py Resolved
Comment thread olive/passes/onnx/graph_surgeries.py Resolved

@justinchuby Justin Chu (justinchuby) left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
ananyaa-ms and others added 3 commits November 9, 2026 21:52
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>
@ananyaa-ms

Copy link
Copy Markdown
Contributor Author

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?

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants