add min support
MODIFY
test/test_ops.py
test/test_ops.py
@@ -115,6 +115,10 @@ class TestOps(unittest.TestCase):
helper_test_op([(3,4,5,6)], lambda x: x.sum(axis=(0,2)), lambda x: Tensor.sum(x, axis=(0,2)))
helper_test_op([(3,4,5,6)], lambda x: x.sum(axis=(1,2)), lambda x: Tensor.sum(x, axis=(1,2)))
helper_test_op([(3,4,5,6)], lambda x: x.sum(axis=1), lambda x: Tensor.sum(x, axis=1))
+ def test_min(self):
+ helper_test_op([(3,3)], lambda x: x.min(), Tensor.min)
+ helper_test_op([(45,3)], lambda x: x.min(), Tensor.min)
+ helper_test_op([(45,3)], lambda x: x.min().mul(0.5), lambda x: Tensor.min(x).mul(0.5))
def test_max(self):
helper_test_op([(45,3)], lambda x: x.max(), Tensor.max)
helper_test_op([(45,3)], lambda x: x.max().mul(0.5), lambda x: Tensor.max(x).mul(0.5))

MODIFY
tinygrad/tensor.py
tinygrad/tensor.py
@@ -205,11 +205,12 @@ class Tensor:
axis = [axis]
axis = tuple([x if x >= 0 else x+len(self.shape) for x in axis])
shape = [self.shape[i] for i in range(len(self.shape)) if i not in axis]
- ret = fxn(axis=axis)
+ ret = fxn(self, axis=axis)
return ret if keepdim else ret.reshape(shape=[1] if shape == [] else shape)
- def sum(self, axis=None, keepdim=False): return self._reduce(self._sum, axis, keepdim)
- def max(self, axis=None, keepdim=False): return self._reduce(self._max, axis, keepdim)
+ def sum(self, axis=None, keepdim=False): return self._reduce(Tensor._sum, axis, keepdim)
+ def max(self, axis=None, keepdim=False): return self._reduce(Tensor._max, axis, keepdim)
+ def min(self, axis=None, keepdim=False): return -((-self)._reduce(Tensor._max, axis, keepdim))
def mean(self, axis=None, keepdim=False):
out = self.sum(axis=axis, keepdim=keepdim)

add min support