-
Notifications
You must be signed in to change notification settings - Fork 4
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
130 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from unittest import skipUnless | ||
|
||
import pytest | ||
import torch | ||
from hbutils.testing import vpip | ||
|
||
import treetensor.torch as ttorch | ||
from treetensor.torch import Size | ||
|
||
|
||
@pytest.fixture() | ||
def treetensor_x(): | ||
return ttorch.randn({ | ||
'a': (2, 5, 7), | ||
'b': { | ||
'x': (3, 4, 6), | ||
} | ||
}) | ||
|
||
|
||
@pytest.fixture() | ||
def treetensor_y(): | ||
return ttorch.randn({ | ||
'a': (2, 5, 7), | ||
'b': { | ||
'x': (3, 4, 6), | ||
} | ||
}) | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestTorchTensorWrapper: | ||
@skipUnless(vpip('torch') >= '2', 'Torch 2 required.') | ||
def test_vmap(self, treetensor_x, treetensor_y): | ||
f = lambda x, y: (x.sum() + y.mean() * 2) | ||
n_pow = torch.vmap(f) | ||
batched_pow = ttorch.vmap(f) | ||
r = batched_pow(treetensor_x, treetensor_y) | ||
|
||
assert r.shape == Size({ | ||
'a': (2,), | ||
'b': { | ||
'x': (3,) | ||
}, | ||
}) | ||
assert ttorch.isclose( | ||
r, | ||
ttorch.tensor({ | ||
'a': n_pow(treetensor_x.a, treetensor_y.a), | ||
'b': { | ||
'x': n_pow(treetensor_x.b.x, treetensor_y.b.x), | ||
} | ||
}) | ||
).all() | ||
|
||
@skipUnless(vpip('torch') >= '2', 'Torch 2 required.') | ||
def test_vmap_in_dims(self, treetensor_x, treetensor_y): | ||
f = lambda x, y: (x.sum() + y.mean() * 2) | ||
n_pow = torch.vmap(f, in_dims=1) | ||
batched_pow = ttorch.vmap(f, in_dims=1) | ||
r = batched_pow(treetensor_x, treetensor_y) | ||
|
||
assert r.shape == Size({ | ||
'a': (5,), | ||
'b': { | ||
'x': (4,) | ||
}, | ||
}) | ||
assert ttorch.isclose( | ||
r, | ||
ttorch.tensor({ | ||
'a': n_pow(treetensor_x.a, treetensor_y.a), | ||
'b': { | ||
'x': n_pow(treetensor_x.b.x, treetensor_y.b.x), | ||
} | ||
}) | ||
).all() | ||
|
||
@skipUnless(vpip('torch') >= '2', 'Torch 2 required.') | ||
def test_vmap_nested(self, treetensor_x, treetensor_y): | ||
f = lambda x, y: (x.sum() + y.mean() * 2) | ||
n_pow = torch.vmap(torch.vmap(f)) | ||
batched_pow = ttorch.vmap(ttorch.vmap(f)) | ||
r = batched_pow(treetensor_x, treetensor_y) | ||
|
||
assert r.shape == Size({ | ||
'a': (2, 5), | ||
'b': { | ||
'x': (3, 4) | ||
}, | ||
}) | ||
assert ttorch.isclose( | ||
r, | ||
ttorch.tensor({ | ||
'a': n_pow(treetensor_x.a, treetensor_y.a), | ||
'b': { | ||
'x': n_pow(treetensor_x.b.x, treetensor_y.b.x), | ||
} | ||
}) | ||
).all() |
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,13 @@ | ||
import torch | ||
|
||
from .base import doc_from_base, wrap_for_treelize | ||
|
||
__all__ = [ | ||
'vmap', | ||
] | ||
|
||
|
||
@doc_from_base() | ||
@wrap_for_treelize() | ||
def vmap(func, *args, **kwargs): | ||
return torch.vmap(func, *args, **kwargs) |