-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add unit test to check backward function for conv, checks there is no graph breaks #1709
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04de6d0
Simple unit test to check backward function for conv
xadupre 77dafc3
complete
xadupre 2b77fd6
Merge branch 'main' into dort3
xiaowuhu 0e4fc65
Merge branch 'main' into dort3
xadupre 67efd9c
fix merge conflict
xadupre a42d94e
lint
xadupre a28d067
lint
xadupre 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ coverage.xml | |
cover/ | ||
test-output.xml | ||
*.sarif | ||
_dump_* | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
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,110 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
|
||
# Licensed under the MIT License. | ||
# pylint: disable=not-callable, unbalanced-tuple-unpacking | ||
|
||
import copy | ||
import sys | ||
import unittest | ||
|
||
import torch | ||
|
||
import onnxscript.tools.training_helper | ||
import onnxscript.tools.transformers_models | ||
import onnxscript.tools.transformers_models.llama | ||
from onnxscript._internal.version_utils import has_transformers, torch_older_than | ||
|
||
|
||
class TestBackward(unittest.TestCase): | ||
@unittest.skipIf(sys.platform == "win32", reason="not supported yet on Windows") | ||
@unittest.skipIf(not has_transformers(), reason="transformers is missing") | ||
@unittest.skipIf(torch_older_than("2.4"), reason="fails to export") | ||
def test_backward_working(self): | ||
class SimpleCNNN(torch.nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
self.fc1 = torch.nn.Linear(14, 10) | ||
|
||
def forward(self, x): | ||
return torch.nn.functional.relu(self.fc1(x)) | ||
|
||
input_tensors = (torch.randn(1, 1, 14, 14),) | ||
model = SimpleCNNN() | ||
local_aot_ort = onnxscript.tools.training_helper.make_aot_ort(dynamic=False) | ||
|
||
compiled_model = torch.compile( | ||
copy.deepcopy(model), | ||
backend=local_aot_ort, | ||
dynamic=False, | ||
fullgraph=True, | ||
) | ||
|
||
expected_results, expected_gradients = onnxscript.tools.training_helper.train_loop( | ||
|
||
model, *input_tensors | ||
) | ||
results, gradients, onnx_models = onnxscript.tools.training_helper.train_loop( | ||
compiled_model, | ||
*input_tensors, | ||
dump_onnx_models=True, | ||
dump_prefix="_dump_testbw_working", | ||
dump_clean_first=True, | ||
) | ||
torch.testing.assert_allclose(expected_results[0], results[0], atol=1e-5, rtol=1e-5) | ||
|
||
# Checking there is only two generated graphs otherwise, it means there are graph breaks. | ||
self.assertEqual(len(onnx_models), 2) | ||
torch.testing.assert_allclose( | ||
expected_gradients[0], gradients[0], atol=1e-5, rtol=1e-5 | ||
) | ||
|
||
@unittest.skipIf(sys.platform == "win32", reason="not supported yet on Windows") | ||
@unittest.skipIf(not has_transformers(), reason="transformers is missing") | ||
@unittest.skipIf(torch_older_than("2.4"), reason="fails to export") | ||
@unittest.skipIf(True, reason="aten.conv_backward not implemented yet.") | ||
def test_backward_conv(self): | ||
class SimpleCNNN(torch.nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
self.conv1 = torch.nn.Conv2d( | ||
in_channels=1, out_channels=2, kernel_size=3, padding=1 | ||
) | ||
self.fc1 = torch.nn.Linear(14, 10) | ||
|
||
def forward(self, x): | ||
y = torch.nn.functional.relu(self.conv1(x)) | ||
z = self.fc1(y) | ||
return z | ||
|
||
input_tensors = (torch.randn(1, 1, 14, 14),) | ||
model = SimpleCNNN() | ||
local_aot_ort = onnxscript.tools.training_helper.make_aot_ort(dynamic=False) | ||
|
||
compiled_model = torch.compile( | ||
copy.deepcopy(model), | ||
backend=local_aot_ort, | ||
dynamic=False, | ||
fullgraph=True, | ||
) | ||
|
||
expected_results, expected_gradients = onnxscript.tools.training_helper.train_loop( | ||
|
||
model, *input_tensors | ||
) | ||
results, gradients, onnx_models = onnxscript.tools.training_helper.train_loop( | ||
compiled_model, | ||
*input_tensors, | ||
dump_onnx_models=True, | ||
dump_prefix="_dump_testbw_conv", | ||
dump_clean_first=True, | ||
) | ||
torch.testing.assert_allclose(expected_results[0], results[0], atol=1e-5, rtol=1e-5) | ||
|
||
# Checking there is only two generated graphs otherwise, it means there are graph breaks. | ||
self.assertEqual(len(onnx_models), 2) | ||
torch.testing.assert_allclose( | ||
expected_gradients[0], gradients[0], atol=1e-5, rtol=1e-5 | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) | ||
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 |
---|---|---|
|
@@ -120,8 +120,6 @@ | |
onnxscript.tools.transformers_models.llama.get_llama_model() | ||
) | ||
input_tensors = input_tensors_many[0] | ||
expected = model(*input_tensors) | ||
|
||
local_aot_ort = onnxscript.tools.training_helper.make_aot_ort(dynamic=False) | ||
|
||
compiled_model = torch.compile( | ||
|
@@ -131,8 +129,17 @@ | |
fullgraph=True, | ||
) | ||
|
||
results = compiled_model(*input_tensors) | ||
torch.testing.assert_close(expected[0], results[0], atol=1e-5, rtol=1e-5) | ||
expected_results, expected_gradients = onnxscript.tools.training_helper.train_loop( # pylint: disable=unbalanced-tuple-unpacking | ||
model, *input_tensors | ||
) | ||
results, gradients, onnx_models = onnxscript.tools.training_helper.train_loop( | ||
Check warning Code scanning / lintrunner RUFF/F841 Warning
Local variable onnx\_models is assigned to but never used.
See https://docs.astral.sh/ruff/rules/unused-variable Check warning Code scanning / lintrunner PYLINT/W0612 Warning
Unused variable 'onnx_models' (unused-variable)
See unused-variable. To disable, use # pylint: disable=unused-variable |
||
compiled_model, | ||
*input_tensors, | ||
dump_onnx_models=True, | ||
dump_prefix="_dump_dort_llama", | ||
dump_clean_first=True, | ||
) | ||
torch.testing.assert_allclose(expected_results[0], results[0], atol=1e-5, rtol=1e-5) | ||
|
||
expected_gradients = onnxscript.tools.training_helper.train_loop(model, *input_tensors) | ||
gradients = onnxscript.tools.training_helper.train_loop(compiled_model, *input_tensors) | ||
|
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
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.
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.
In the OpInfo data structure, I have seen a field that says supports_grad or something which may make it easier for us to generate backward tests. @xiaowuhu do you have some ideas?
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.
This seems be a different scenario than the OpInfo way. Here, we need to go through the aot-compile-training-backward process which is an e2e scenario, although it is not a straight forward way. But this requirement will only benefit not more than 20 backward functions, so I think it is OK.
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.
SG. Thanks!