From 8cc2dff4d845f93ae92bd73f721a0cc78e8a21f8 Mon Sep 17 00:00:00 2001 From: chenyu Date: Thu, 22 May 2025 21:02:11 -0400 Subject: [PATCH] only float Tensors have gradient [pr] (#10475) --- test/test_tensor.py | 2 +- test/unit/test_gradient.py | 5 +++++ tinygrad/tensor.py | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/test_tensor.py b/test/test_tensor.py index 8f4403139a..420eefc083 100644 --- a/test/test_tensor.py +++ b/test/test_tensor.py @@ -494,7 +494,7 @@ class TestTinygrad(unittest.TestCase): _a = Tensor([3]) in [Tensor([3]), Tensor([4]), Tensor([5])] def test_repr_with_grad(self): - a = Tensor([1], requires_grad=True) + a = Tensor([1.0], requires_grad=True) b = Tensor([1]) c = (a + b).sum().backward() print(a) diff --git a/test/unit/test_gradient.py b/test/unit/test_gradient.py index 3c3290864d..20980e313a 100644 --- a/test/unit/test_gradient.py +++ b/test/unit/test_gradient.py @@ -104,6 +104,11 @@ class TestTensorGradient(unittest.TestCase): x_casted = x_reshaped.cast(dtypes.float16) x_casted.mean().gradient(x_reshaped) + def test_non_float_tensor_raise(self): + x = Tensor([1, 2, 3]) + with self.assertRaises(RuntimeError): x.sum().gradient(x) + with self.assertRaises(RuntimeError): x.float().sum().gradient(x) + class TestRealizeMeansRealize(unittest.TestCase): def test_randn_realizes(self): x = Tensor.randn(2, 3, 64, 64, requires_grad=True).realize() diff --git a/tinygrad/tensor.py b/tinygrad/tensor.py index 5dc666d792..c25015e7ba 100644 --- a/tinygrad/tensor.py +++ b/tinygrad/tensor.py @@ -907,6 +907,7 @@ class Tensor(MathTrait): ``` """ assert gradient is not None or self.shape == tuple(), "when no gradient is provided, backward must be called on a scalar tensor" + if not (self.is_floating_point() and all(t.is_floating_point() for t in targets)): raise RuntimeError("only float Tensors have gradient") if gradient is None: gradient = Tensor(1.0, dtype=self.dtype, device=self.device, requires_grad=False) rets = [] target_uops = [x.lazydata for x in targets]