Repository navigation
Add graph surgery for scalar tensor constants - #2791
Open
ananyaa-ms wants to merge 2 commits into
Open
ananyaa-ms wants to merge 2 commits into
ananyaa-ms wants to merge 2 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
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
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.") |
| node.domain = "" | ||
| node.op_type = "Constant" | ||
| node.attributes.clear() | ||
| node.attributes["value_int"] = ir.AttrInt64("value_int", self.value) |
Contributor
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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
lintrunner -a(Optional) Issue link