Add LazyTensor class to implement ir.TensorProtocol#2232
Merged
justinchuby merged 14 commits intomainfrom Apr 28, 2025
Merged
Conversation
--- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/microsoft/onnxscript?shareId=XXXX-XXXX-XXXX-XXXX).
❌ 3 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a LazyTensor class that implements the TensorProtocol and defers expensive tensor transformations until methods like numpy() or tobytes() are called. Key changes include:
- Adding the LazyTensor class and its implementation details in onnxscript/ir/_core.py.
- Implementing unit tests for LazyTensor behavior in onnxscript/ir/_core_test.py.
- Exporting LazyTensor in onnxscript/ir/init.py to make it available as part of the public API.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| onnxscript/ir/_core_test.py | Adds tests to validate LazyTensor initialization, numpy conversion, and byte serialization. |
| onnxscript/ir/_core.py | Implements the LazyTensor class with support for lazy evaluation and caching. |
| onnxscript/ir/init.py | Exports LazyTensor for public use. |
Comments suppressed due to low confidence (1)
onnxscript/ir/_core_test.py:1343
- The expected byte literal in the tobytes test is hard-coded and might be brittle on platforms with different endianness or integer representations. Consider generating the expected bytes dynamically using lazy_tensor.numpy().tobytes() to improve test resilience.
lazy_tensor.tobytes(),
b"\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00"
titaiwangms
approved these changes
Apr 28, 2025
Contributor
titaiwangms
left a comment
There was a problem hiding this comment.
Will there be a follow up that actually uses this in any implementation?
Collaborator
Author
|
Yes! See microsoft/onnxruntime-genai#1427 |
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.
I used copilot to help implement #2231. The lazy tensor class allows users to delay transformations to the tensors until serialization time, which helps with memory usage and avoids the need to cache of unload intermediate tensor data to disk.
Example
Fixes #2231
For more details, open the Copilot Workspace session.