From 3964eee64fcba9d3ff3c2541ef575b3ef42845e3 Mon Sep 17 00:00:00 2001 From: chenyu Date: Fri, 10 Jul 2026 14:17:03 -0400 Subject: [PATCH] failing tests for Tensor(Variable) (#16967) --- test/backend/test_tensor_variable.py | 49 +++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/test/backend/test_tensor_variable.py b/test/backend/test_tensor_variable.py index 5a8b1fde37..8943200cc8 100644 --- a/test/backend/test_tensor_variable.py +++ b/test/backend/test_tensor_variable.py @@ -1,6 +1,7 @@ import unittest import numpy as np -from tinygrad import Tensor, Variable +from tinygrad import Tensor, Variable, dtypes +from tinygrad.helpers import CHECK_OOB class TestTensorVariable(unittest.TestCase): def test_add_tvar(self): @@ -8,6 +9,52 @@ class TestTensorVariable(unittest.TestCase): ret = (Tensor(vv) + 3).item() assert ret == 4 + def test_variable_mul_tensor(self): + vv = Variable("a", 1, 10).bind(2) + t = Tensor.ones(3, dtype=dtypes.int8) + self.assertListEqual((t * vv).tolist(), [2, 2, 2]) + # TODO: fix + try: + self.assertListEqual((vv * t).tolist(), [2, 2, 2]) + except RuntimeError: pass + + def test_large_range_variable(self): + vv = Variable("b", 0, 2**40).bind(2**35) + # TODO: pm_lower_index_dtype lowers ALU PARAM to int32 unconditionally + try: + self.assertEqual(Tensor(vv).item(), 2**35) + except AssertionError: + pass + + def test_variable_tensor_dtype_arg(self): + vv = Variable("a", 1, 10).bind(2) + # TODO: dtype arg is silently dropped for a symbolic int, should be honored (or rejected) + try: + self.assertEqual(Tensor(vv, dtype=dtypes.float32).dtype, dtypes.float32) + except AssertionError: pass + + def test_unbound_variable_tensor(self): + # TODO: Tensor creation from unbound variable should assert + # with self.assertRaises(AssertionError): Tensor(Variable("u", 1, 10)) + t = Tensor(Variable("u", 1, 10)) + self.assertRaises(KeyError, t.item) # today it builds silently and fails at execution + + def test_shrink_beyond_buffer_variable(self): + # TODO: shrink by a variable whose vmax exceeds the dim should fail at build, today only CHECK_OOB=1 rejects it + t = Tensor.ones(3).contiguous()[:Variable("a", 1, 10).bind(5)] + if CHECK_OOB: self.assertRaises(RuntimeError, t.sum().item) + else: t.sum().item() # silent OOB: reads 2 elements past the buffer, result depends on the allocator + + def test_symbolic_shape_mul_variable_tensor(self): + # NOTE: the buffer dim must cover the variable's vmax + vv = Variable("a", 1, 10).bind(2) + self.assertEqual((Tensor.ones(10).contiguous()[:vv] * Tensor(vv)).sum().item(), 4.0) + v0 = Variable("z", 0, 10).bind(2) + # TODO: broadcasting a vmin=0 symbolic dim fails, max(dim, 1) cannot be proven equal to dim + try: + self.assertEqual((Tensor.ones(10).contiguous()[:v0] * Tensor(v0)).sum().item(), 4.0) + except IndexError: pass + def test_inner_tvar_node(self): vv = Variable("w", 0, 10).bind(2) ret = Tensor(vv * 4).item()