mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-08-30 07:06:06 +00:00
fix shard axis through symbolic reshape (#17238)
* fix shard axis through symbolic reshape * bind
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import gc, unittest
|
||||
from tinygrad import Tensor, GlobalCounters, dtypes
|
||||
from tinygrad import Tensor, UOp, GlobalCounters, dtypes
|
||||
from tinygrad.engine.jit import TinyJit
|
||||
from tinygrad.helpers import Context
|
||||
|
||||
@@ -218,5 +218,10 @@ class TestMultiAxis(unittest.TestCase):
|
||||
self.assertEqual(e.uop.axis, 0)
|
||||
self.assertTrue(e.uop.has_buffer_identity())
|
||||
|
||||
def test_symbolic_reshape_shard_axis(self):
|
||||
rows = UOp.variable("rows", 1, 4).bind(3)
|
||||
x = Tensor.empty(4, 2).shard(("NULL:1", "NULL:2"), axis=1)[:rows]
|
||||
self.assertEqual(x.reshape(rows, 1, 2).uop.axis, 2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
+2
-2
@@ -695,9 +695,9 @@ class UOp(RandMixin, metaclass=UOpMetaClass):
|
||||
return src_axis - self.arg[1]
|
||||
if self.op is Ops.RESHAPE:
|
||||
if src_axis is None: return None
|
||||
arg_acc:list[sint] = list(itertools.accumulate(self.marg, operator.mul, initial=1))
|
||||
arg_acc:list[sint] = [ssimplify(x) for x in itertools.accumulate(self.marg, operator.mul, initial=1)]
|
||||
# new_axis is the last one that preserves prod(prior to new_axis) and must not move items between shards
|
||||
target = prod(self.src[0].shape[:src_axis])
|
||||
target = ssimplify(prod(self.src[0].shape[:src_axis]))
|
||||
if target not in arg_acc: raise RuntimeError(f"reshape {self.src[0].shape} -> {self.shape} moved items between shards")
|
||||
new_axis = len(arg_acc) - arg_acc[::-1].index(target) - 1
|
||||
if self.shape[new_axis] % len(self.device) != 0: raise RuntimeError(f"reshape {self.src[0].shape} -> {self.shape} moved items between shards")
|
||||
|
||||
Reference in New Issue
Block a user