-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
11 deletions.
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,14 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class SimpleNN(nn.Module): | ||
def __init__(self): | ||
super(SimpleNN, self).__init__() | ||
self.fc1 = nn.Linear(10, 5) | ||
self.fc2 = nn.Linear(5, 1) | ||
|
||
def forward(self, x): | ||
x = torch.relu(self.fc1(x)) | ||
x = self.fc2(x) | ||
return x |
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,36 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
|
||
from tests.module_pool.simple_nn import SimpleNN | ||
|
||
|
||
def test_torch_compile_forward(): | ||
model = SimpleNN() | ||
compiled_model = torch.compile(model, backend="aot_eager") | ||
|
||
input_data = torch.randn(1, 10) | ||
expected_output = model(input_data) | ||
actual_output = compiled_model(input_data) | ||
assert torch.allclose(expected_output, actual_output), "Output mismatch between compiled and original model" | ||
|
||
|
||
def test_torch_compile_backward(): | ||
model = SimpleNN() | ||
compiled_model = torch.compile(model, backend="aot_eager") | ||
|
||
criterion = nn.MSELoss() | ||
optimizer = optim.SGD(compiled_model.parameters(), lr=0.01) | ||
|
||
input_data = torch.randn(1, 10) | ||
target = torch.tensor([1.0]) | ||
|
||
output = compiled_model(input_data) | ||
loss = criterion(output, target) | ||
|
||
optimizer.zero_grad() | ||
loss.backward() | ||
optimizer.step() | ||
|
||
for param in compiled_model.parameters(): | ||
assert param.grad is not None, "Gradient not computed for parameter in compiled model" |