-
Notifications
You must be signed in to change notification settings - Fork 53
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 aten_convolution_backward function #1707
Open
xiaowuhu
wants to merge
22
commits into
main
Choose a base branch
from
xiaowu/addConvBackward
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
22 commits
Select commit
Hold shift + click to select a range
b701d9a
Update core.py
xiaowuhu 2b97b2a
Update core.py
xiaowuhu 5ad4268
Update core.py
xiaowuhu cfb0567
Update core.py
xiaowuhu 9e11c20
Update core.py
xiaowuhu fc2beec
Update .gitignore
xiaowuhu cec9700
Merge branch 'main' of https://github.com/microsoft/onnxscript
xiaowuhu a690d73
Update .gitignore
xiaowuhu 8adc6a2
update
xiaowuhu 0969173
Update backward_test.py
xiaowuhu 7c0e7c1
Merge branch 'main' into xiaowu/addConvBackward
xiaowuhu 0d439e1
Merge branch 'main' into xiaowu/addConvBackward
xiaowuhu be11f9d
Merge branch 'xiaowu/addConvBackward' of https://github.com/microsoft…
xiaowuhu 55edf8f
Update backward_test.py
xiaowuhu 3a67a57
Update backward_test.py
xiaowuhu 8689019
Update backward_test.py
xiaowuhu b32ada3
Merge branch 'main' into xiaowu/addConvBackward
xiaowuhu 97cc9d4
move to new folder
xiaowuhu 7d75dc4
Merge branch 'main' into xiaowu/addConvBackward
xiaowuhu 4455dd3
Merge branch 'main' into xiaowu/addConvBackward
xiaowuhu c21f6ac
Update core.py
xiaowuhu 1eb33c3
Merge branch 'main' into xiaowu/addConvBackward
xiaowuhu 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
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,109 @@ | ||||||
# Copyright (c) Microsoft Corporation. | ||||||
# Licensed under the MIT License. | ||||||
# pylint: disable=not-callable | ||||||
|
||||||
import copy | ||||||
import sys | ||||||
import unittest | ||||||
|
||||||
import torch | ||||||
|
||||||
import onnxscript.tools.training_helper | ||||||
import onnxscript.tools.transformers_models | ||||||
import onnxscript.tools.transformers_models.llama | ||||||
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I wonder why ruff doesn't warn the unused imports |
||||||
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") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
@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( # pylint: disable=unbalanced-tuple-unpacking | ||||||
model, *input_tensors | ||||||
) | ||||||
results, gradients, onnx_models = onnxscript.tools.training_helper.train_loop( # pylint: disable=unbalanced-tuple-unpacking | ||||||
compiled_model, | ||||||
*input_tensors, | ||||||
dump_onnx_models=True, | ||||||
dump_prefix="_dump_testbw_working", | ||||||
dump_clean_first=True, | ||||||
) | ||||||
torch.testing.assert_close(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_close(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=(0, 0), # not support padding=1, will do it soon | ||||||
) | ||||||
self.fc1 = torch.nn.Linear(12, 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( # pylint: disable=unbalanced-tuple-unpacking | ||||||
model, *input_tensors | ||||||
) | ||||||
results, gradients, onnx_models = onnxscript.tools.training_helper.train_loop( # pylint: disable=unbalanced-tuple-unpacking | ||||||
compiled_model, | ||||||
*input_tensors, | ||||||
dump_onnx_models=True, | ||||||
dump_prefix="_dump_testbw_conv", | ||||||
dump_clean_first=True, | ||||||
) | ||||||
torch.testing.assert_close(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_close(expected_gradients[0], gradients[0], atol=1e-5, rtol=1e-5) | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
unittest.main(verbosity=2) |
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.
Check notice
Code scanning / CodeQL
Commented-out code Note