forked from tinygrad/tinygrad
only float Tensors have gradient [pr] (#10475)
This commit is contained in:
+1
-1
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user