-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support nvfuser fd.add_output(output, alias_input) (#50)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ivan Yashchuk <IvanYashchuk@users.noreply.github.com>
- Loading branch information
1 parent
393828f
commit 3a91b01
Showing
7 changed files
with
165 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
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,89 @@ | ||
from functools import partial | ||
|
||
import torch | ||
from torch.testing import assert_close, make_tensor | ||
|
||
import thunder | ||
import thunder.core.dtypes as datatypes | ||
import thunder.torch as ttorch | ||
from thunder.tests.framework import instantiate | ||
|
||
|
||
@instantiate() | ||
def test_prim_inplace_copy_fwd(executor, device, dtype): | ||
def torch_foo(x, y): | ||
z = x * y | ||
z = z + z | ||
z = x + z | ||
o = x.copy_(z) | ||
return o | ||
|
||
def foo(x, y): | ||
z = x * y | ||
z = z + z | ||
z = x + z | ||
# NOTE: nvfuserex doesn't support `return z`, i.e. the copy_from argument | ||
o = thunder.core.prims.copy_(z, x) | ||
return o | ||
|
||
traced_nvfuser_foo = executor.make_callable(foo) | ||
|
||
tdtype = ttorch.to_torch_dtype(dtype) | ||
a = make_tensor((4, 4), device=device, dtype=tdtype, requires_grad=False) | ||
b = make_tensor((4, 4), device=device, dtype=tdtype, requires_grad=False) | ||
a1 = a.detach().clone() | ||
b1 = b.detach().clone() | ||
thunder_result = traced_nvfuser_foo(a, b) | ||
torch_result = torch_foo(a1, b1) | ||
|
||
custom_comparator = ( | ||
partial(assert_close, atol=1e-2, rtol=1e-2) | ||
if dtype in (datatypes.bfloat16, datatypes.float16) | ||
else assert_close | ||
) | ||
custom_comparator(thunder_result, torch_result) | ||
custom_comparator(a, a1) | ||
|
||
|
||
@instantiate(dtypes=(datatypes.floating,)) | ||
def test_prim_inplace_copy_bwd(executor, device, dtype): | ||
def torch_foo(x, y): | ||
z = x * y | ||
z = z * x | ||
o = x.copy_(z) | ||
p = y * y | ||
return p | ||
|
||
def foo(x, y): | ||
z = x * y | ||
z = z * x | ||
o = thunder.core.prims.copy_(z, x) | ||
p = y * y | ||
return p | ||
|
||
traced_nvfuser_foo = executor.make_callable(foo) | ||
|
||
tdtype = ttorch.to_torch_dtype(dtype) | ||
a = make_tensor((4, 4), device=device, dtype=tdtype, requires_grad=False) | ||
b = make_tensor((4, 4), device=device, dtype=tdtype, requires_grad=True) | ||
a1 = a.detach().clone() | ||
b1 = b.detach().clone() | ||
b1.requires_grad_() | ||
|
||
thunder_result = traced_nvfuser_foo(a, b) | ||
torch_result = torch_foo(a1, b1) | ||
assert_close(thunder_result, torch_result) | ||
custom_comparator = ( | ||
partial(assert_close, atol=1e-2, rtol=1e-2) | ||
if dtype in (datatypes.bfloat16, datatypes.float16) | ||
else assert_close | ||
) | ||
custom_comparator(a, a1) | ||
|
||
g = torch.ones_like(thunder_result) | ||
thunder_result.backward(g) | ||
|
||
g1 = torch.ones_like(torch_result) | ||
torch_result.backward(g1) | ||
assert_close(g, g1) | ||
assert_close(b.grad, b1.grad) |