Skip to content

Add graph surgery for scalar tensor constants - #2791

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

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

Conversation

@ananyaa-ms

Copy link
Copy Markdown
Contributor

Describe your changes

Add  ReplaceTensorWithConstant , a graph surgery that replaces a named scalar INT64 tensor’s producer with an integer  Constant  and removes unused upstream nodes. Add focused tests for shared outputs and invalid inputs.

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 Outdated
Comment on lines +465 to +466
if output.dtype not in (None, ir.DataType.INT64) or (output.shape is not None and len(output.shape) != 0):
raise ValueError(f"Tensor '{self.tensor_name}' must be scalar INT64.")
Comment thread olive/passes/onnx/graph_surgeries.py Outdated
node.domain = ""
node.op_type = "Constant"
node.attributes.clear()
node.attributes["value_int"] = ir.AttrInt64("value_int", self.value)

@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>

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.

3 participants