-
Notifications
You must be signed in to change notification settings - Fork 469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
burn-import: add some tests for ConstantNode #2623
Open
jameshiew
wants to merge
17
commits into
tracel-ai:main
Choose a base branch
from
jameshiew:onnx-constant-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6126714
i64 f32 bool [f32]
jameshiew e8f5c0d
Test all types
jameshiew 5e680f9
Add test for 3D tensor
jameshiew 34ff5c4
Rework tensor tests
jameshiew b6d6be4
Renames
jameshiew 330b376
Don't use const pi
jameshiew 42d26a5
cargo clippy --fix
jameshiew f01d569
Don't use approx pi in example to avoid intellij warning
jameshiew 7c775d1
Add shape_to_tokens docstring
jameshiew 3bf1a94
Update shape_to_tokens docstring
jameshiew ac47449
Fix test_codegen_constant_tensor_3d shape
jameshiew 27f1ae0
Add test ONNX
jameshiew 8dad35f
Test constant scalar types apart from bool
jameshiew 7e82c37
WIP constant tensor
jameshiew a07d099
Remove tensor stuff
jameshiew 5efd749
cargo fmt
jameshiew 756fd3c
Rename tests
jameshiew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
CONST_VALUE = 2 | ||
|
||
|
||
class ConstantModel(nn.Module): | ||
def __init__(self, const_dtype: torch.dtype): | ||
super().__init__() | ||
self.const = torch.tensor(CONST_VALUE).to(const_dtype) | ||
|
||
def forward(self, x): | ||
return x + self.const | ||
|
||
|
||
def export_model(model: ConstantModel, dummy_input: torch.Tensor, file_name: str): | ||
model.eval() | ||
torch.onnx.export( | ||
model, | ||
dummy_input, | ||
file_name, | ||
verbose=False, | ||
opset_version=16, | ||
do_constant_folding=False, | ||
) | ||
print(f"Finished exporting model to {file_name}") | ||
|
||
# Output some test data for demonstration | ||
test_input = dummy_input.clone() | ||
print(dummy_input.dtype, "test input:", test_input) | ||
output = model.forward(test_input) | ||
print(dummy_input.dtype, "test output:", output) | ||
print("") | ||
|
||
|
||
def main(): | ||
device = torch.device("cpu") | ||
shape = (2, 3, 4) | ||
|
||
model_f32 = ConstantModel(torch.float32) | ||
f32_input = torch.randn(shape, dtype=torch.float32, device=device) | ||
export_model(model_f32, f32_input, "constant_f32.onnx") | ||
|
||
model_f64 = ConstantModel(torch.float64) | ||
f64_input = torch.randn(shape, dtype=torch.float64, device=device) | ||
export_model(model_f64, f64_input, "constant_f64.onnx") | ||
|
||
model_i32 = ConstantModel(torch.int32) | ||
i32_input = torch.randint( | ||
low=-10, high=10, size=shape, device=device, dtype=torch.int32 | ||
) | ||
export_model(model_i32, i32_input, "constant_i32.onnx") | ||
|
||
model_i64 = ConstantModel(torch.int64) | ||
i64_input = torch.randint( | ||
low=-10, high=10, size=shape, device=device, dtype=torch.int64 | ||
) | ||
export_model(model_i64, i64_input, "constant_i64.onnx") | ||
|
||
if __name__ == "__main__": | ||
main() |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if the addition is coercing f64 -> f32 somewhere (and i32 -> i64 below). I wasn't sure how to get PyTorch to just forward the constant by itself so these tests are adding the constant to the input
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe by having the output return the constant only? But a simple constant addition works too.
In case you're curious, you could also manually define the onnx graph like the ConstOfShape script. PyTorch tends doesn't always have a 1-to-1 correspondence for ops, so in such cases it could be easier to define the graph manually.
The floating point and integer data types are defined by the backend used. A model is not as statically defined like an ONNX graph. If you look at the other tests, the input(s) and output(s) are created using the
Tensor
methods, not fromTensorData
.