Skip to content
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

Register grad for torch.Tensor.item #1481

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions thunder/tests/test_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,21 @@ def test_vjp_correctness_zeta_manual(op, device, dtype, executor, comp):
comp(grad_rhs, expected_grad[0], equal_nan=True)


@ops((get_opinfo("item"),), supported_dtypes=(dtypes.float64,))
def test_vjp_correctness_torch_item_manual(op, device, dtype, executor, comp):
from thunder.torch import item

for sample in op.sample_inputs(device, dtype, requires_grad=True, no_rhs_numbers=True):
out = op.torch_reference(*sample.args, **sample.kwargs)
flat_op, flat_args, spec = flatten_func(item, sample.args, sample.kwargs)
initial_trace = thunder.trace()(vjp(flat_op), flat_args, (None,))
actual_out, (grad_in,) = executor.make_callable(initial_trace.python_callable(), disable_torch_autograd=True)(
flat_args, (None,)
)
assert grad_in is None, "grad_in should be None"
comp(actual_out, out, equal_nan=True)


@ops((get_opinfo("nll_loss"),), supported_dtypes=(dtypes.float64,))
def test_vjp_correctness_nll_loss_manual(op, device, dtype, executor, comp):
for sample in op.sample_inputs(device, dtype, requires_grad=True, no_rhs_numbers=True):
Expand Down
4 changes: 4 additions & 0 deletions thunder/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4878,6 +4878,10 @@ def item(a: TensorLike) -> Number:
return prims.item(a)


# PyTorch does not support backward for torch.item
register_grad(item.id, item)


# TODO Move this to nn.functional
@torchsymbol(torch.nn.functional.linear)
def linear(a: TensorLike, w: TensorLike, /, bias: None | TensorLike = None) -> TensorLike:
Expand Down
Loading