-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from ndif-team/dev
Dev
- Loading branch information
Showing
7 changed files
with
94 additions
and
15 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
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
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,55 @@ | ||
from collections import OrderedDict | ||
|
||
import pytest | ||
import torch | ||
|
||
import nnsight | ||
from nnsight import NNsight | ||
|
||
input_size = 5 | ||
hidden_dims = 10 | ||
output_size = 2 | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def tiny_model(device: str): | ||
|
||
net = torch.nn.Sequential( | ||
OrderedDict( | ||
[ | ||
("layer1", torch.nn.Linear(input_size, hidden_dims)), | ||
("layer2", torch.nn.Linear(hidden_dims, output_size)), | ||
] | ||
) | ||
) | ||
|
||
return NNsight(net).to(device) | ||
|
||
|
||
@pytest.fixture | ||
def tiny_input(): | ||
return torch.rand((1, input_size)) | ||
|
||
|
||
@torch.no_grad() | ||
def test_tiny(tiny_model: NNsight, tiny_input: torch.Tensor): | ||
|
||
with tiny_model.trace(tiny_input): | ||
|
||
hs = tiny_model.layer2.output.save() | ||
|
||
assert isinstance(hs.value, torch.Tensor) | ||
|
||
|
||
def test_grad_setting(tiny_model: NNsight, tiny_input: torch.Tensor): | ||
with tiny_model.trace(tiny_input): | ||
l1_grad = tiny_model.layer1.output.grad.clone().save() | ||
|
||
tiny_model.layer1.output.grad = tiny_model.layer1.output.grad.clone() * 2 | ||
|
||
l1_grad_double = tiny_model.layer1.output.grad.save() | ||
|
||
loss = tiny_model.output.sum() | ||
loss.backward() | ||
|
||
assert torch.equal(l1_grad.value * 2, l1_grad_double.value) |