Files
TorchNPU/test/distributed/_tensor/test_view_ops.py
T
kisnwang 46afd2da77 refactor dtensor rules
Co-authored-by: kisnwang<wangkaisheng2@huawei.com>



# message auto-generated for no-merge-commit merge:
!34096 merge master into master

refactor dtensor rules

Created-by: kisnwang
Commit-by: kisnwang
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> <font color="red">**如有社区issue,请关联issue链接**</font>\
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

- [ ] 需求
- [ ] 问题单
- [ ] issue/工单
- [x] 重构优化  [#1743](https://gitcode.com/Ascend/pytorch/issues/1743) 
- [ ] 资料更新

# 【修改方案】
自定义算子注册与社区对齐,删除utils/dtensor.py

# 【资料变更】
不涉及

# 【接口变更】
不涉及

# 【功能验证】
适配UT测试用例看护

# 【CheckList】
> PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

- [ ] 代码注释完备,正确记录错误日志
- [ ] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [ ] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!34096
2026-04-28 20:23:06 +08:00

483 lines
16 KiB
Python

# Copyright (c) Meta Platforms, Inc. and affiliates
# Owner(s): ["oncall: distributed"]
import itertools
from typing import cast, List
import torch
import torch.distributed as dist
from torch import rand, randn, Tensor
from torch.distributed.tensor import DeviceMesh, distribute_tensor, Replicate, Shard
from torch.distributed.tensor.debug import CommDebugMode
from torch.distributed.tensor._ops._view_ops import (
Broadcast,
dim_maps,
Flatten,
InputDim,
Repeat,
Singleton,
Split,
view_groups,
)
from torch.distributed.tensor.placement_types import Placement
from torch.testing._internal.common_utils import run_tests
from torch.testing._internal.distributed._tensor.common_dtensor import DTensorTestBase
from torch.utils import _pytree as pytree
import torch_npu
from torch_npu.testing.common_distributed import with_comms, skipIfUnsupportMultiNPU
npu = torch.ops.npu
class TestViewOps(DTensorTestBase):
@skipIfUnsupportMultiNPU(4)
def test_view_groups(self):
self.assertEqual(
view_groups([2, 3], [3, 2]),
(
Split(Flatten((InputDim(0), InputDim(1))), (3, 2), 0),
Split(Flatten((InputDim(0), InputDim(1))), (3, 2), 1),
),
)
self.assertEqual(
view_groups([3, 4, 5], [12, 5]),
(Flatten((InputDim(0), InputDim(1))), InputDim(2)),
)
self.assertEqual(
view_groups([2, 3, 4, 5, 7], [12, 70]),
(
Split(
Flatten(
(
InputDim(0),
InputDim(1),
InputDim(2),
InputDim(3),
InputDim(4),
)
),
(12, 70),
0,
),
Split(
Flatten(
(
InputDim(0),
InputDim(1),
InputDim(2),
InputDim(3),
InputDim(4),
)
),
(12, 70),
1,
),
),
)
self.assertEqual(
view_groups([2, 3, 4, 5, 7], [3, 8, 7, 5]),
(
Split(Flatten((InputDim(0), InputDim(1), InputDim(2))), (3, 8), 0),
Split(Flatten((InputDim(0), InputDim(1), InputDim(2))), (3, 8), 1),
Split(Flatten((InputDim(3), InputDim(4))), (7, 5), 0),
Split(Flatten((InputDim(3), InputDim(4))), (7, 5), 1),
),
)
self.assertEqual(
view_groups([3, 4, 8, 3], [12, 4, 2, 3]),
(
Flatten((InputDim(0), InputDim(1))),
Split(InputDim(2), (4, 2), 0),
Split(InputDim(2), (4, 2), 1),
InputDim(3),
),
)
self.assertEqual(
view_groups([3, 24], [1, 3, 2, 4, 1, 3, 1]),
(
Singleton(),
InputDim(0),
Split(InputDim(1), (2, 4, 3), 0),
Split(InputDim(1), (2, 4, 3), 1),
Singleton(),
Split(InputDim(1), (2, 4, 3), 2),
Singleton(),
),
)
self.assertEqual(
view_groups([1, 1, 3, 2, 1, 1], [6, 1, 1, 1]),
(
Flatten((InputDim(2), InputDim(3))),
Singleton(),
Singleton(),
Singleton(),
),
)
self.assertEqual(
view_groups([1, 1, 12, 1, 1, 1, 2, 5, 1], [3, 4, 1, 10]),
(
Split(InputDim(2), (3, 4), 0),
Split(InputDim(2), (3, 4), 1),
Singleton(),
Flatten((InputDim(6), InputDim(7))),
),
)
self.assertEqual(
view_groups([2, 3, 4], [2, -1, 4]),
(InputDim(0), InputDim(1), InputDim(2)),
)
@property
def world_size(self) -> int:
return 4
def call_dt_test(self, op, args, kwargs, device_mesh: DeviceMesh):
dim_map = dim_maps[op]
rules = dim_map(*args, **kwargs)
outputs = op(*args, **kwargs)
flat_args = pytree.arg_tree_leaves(*args)
in_shape = flat_args[0].shape
no_shard_dims = set()
for rule in rules:
if isinstance(rule, Repeat):
if isinstance(rule.input_dim, InputDim):
no_shard_dims.add(rule.input_dim.input_dim)
elif isinstance(rule, Flatten):
for dim in rule.input_dims[1:]:
if isinstance(dim, InputDim):
no_shard_dims.add(dim.input_dim)
elif isinstance(rule, Split):
if isinstance(rule.input_dim, Flatten):
for dim in rule.input_dim.input_dims[1:]:
if isinstance(dim, InputDim):
no_shard_dims.add(dim.input_dim)
if op == torch.unbind:
no_shard_dims.add(kwargs.get("dim", 0))
sharding_choices = cast(List[Placement], [Replicate()]) + [
Shard(i) for i, s in enumerate(in_shape) if s > 1 and i not in no_shard_dims
]
all_sharding_choices = itertools.product(
*(device_mesh.ndim * [sharding_choices])
)
for in_shard in all_sharding_choices:
in_dt = distribute_tensor(args[0], device_mesh, in_shard)
comm_mode = CommDebugMode()
with comm_mode:
out_dt = op(in_dt, *args[1:], **kwargs)
self.assertEqual(
comm_mode.get_total_counts(), 0, "Expected no redistribution."
)
full_out = out_dt.full_tensor()
if dist.get_rank() == 0:
self.assertEqual(outputs.npu(), full_out)
def dimmap_test(self, op, args, expected_rule_output):
rules = dim_maps[op](*args)
self.assertEqual(rules, expected_rule_output)
self.call_dt_test(op, args, {}, self.device_mesh)
@skipIfUnsupportMultiNPU(4)
@with_comms
def test_view_ops_p1(self):
self.device_mesh = DeviceMesh(
self.device_type, torch.arange(dist.get_world_size()).view(-1, 2)
)
self.dimmap_test(torch.atleast_1d, (randn(()),), (Singleton(),))
self.dimmap_test(torch.atleast_1d, (randn(24),), (InputDim(0),))
self.dimmap_test(torch.atleast_1d, (randn(24, 36),), (InputDim(0), InputDim(1)))
self.dimmap_test(torch.atleast_2d, (randn(()),), (Singleton(), Singleton()))
self.dimmap_test(torch.atleast_2d, (randn(24),), (Singleton(), InputDim(0)))
self.dimmap_test(torch.atleast_2d, (randn(24, 36),), (InputDim(0), InputDim(1)))
self.dimmap_test(
torch.atleast_2d,
(randn(24, 36, 48),),
(InputDim(0), InputDim(1), InputDim(2)),
)
self.dimmap_test(
torch.atleast_3d,
(randn(()),),
(Singleton(), Singleton(), Singleton()),
)
self.dimmap_test(
torch.atleast_3d,
(randn(24),),
(Singleton(), InputDim(0), Singleton()),
)
self.dimmap_test(
torch.atleast_3d,
(randn(24, 36),),
(InputDim(0), InputDim(1), Singleton()),
)
self.dimmap_test(
torch.atleast_3d,
(randn(24, 36, 28),),
(InputDim(0), InputDim(1), InputDim(2)),
)
self.dimmap_test(
torch.atleast_3d,
(randn(24, 36, 28, 24),),
(InputDim(0), InputDim(1), InputDim(2), InputDim(3)),
)
@skipIfUnsupportMultiNPU(4)
@with_comms
def test_view_ops_p2(self):
self.device_mesh = DeviceMesh(
self.device_type, torch.arange(dist.get_world_size()).view(-1, 2)
)
with self.assertRaises(AssertionError):
dim_maps[torch.broadcast_to](randn(24, 36), (1, 2, 4))
self.dimmap_test(
torch.broadcast_to,
(rand(24, 36), (1, 24, 36)),
(Singleton(), InputDim(0), InputDim(1)),
)
self.dimmap_test(
torch.broadcast_to,
(rand(24, 36), (28, 24, 36)),
(Broadcast(Singleton(), 28), InputDim(0), InputDim(1)),
)
self.dimmap_test(
torch.broadcast_to,
(rand(24, 1, 36), (12, 24, 24, 36)),
(
Broadcast(Singleton(), 12),
InputDim(0),
Broadcast(InputDim(1), 24),
InputDim(2),
),
)
self.dimmap_test(
torch.broadcast_to,
(rand(24, 36), (-1, 36)),
(InputDim(0), InputDim(1)),
)
self.dimmap_test(
torch.broadcast_to,
(rand(24, 1, 36), (-1, 1, 36)),
(InputDim(0), InputDim(1), InputDim(2)),
)
self.dimmap_test(
torch.broadcast_to,
(randn(36, 1, 24), (12, 36, 28, 24)),
(
Broadcast(Singleton(), 12),
InputDim(0),
Broadcast(InputDim(1), 28),
InputDim(2),
),
)
self.dimmap_test(
Tensor.expand,
(randn(24, 1, 36, 1), 36, 24, 28, -1, 24),
(
Broadcast(Singleton(), 36),
InputDim(0),
Broadcast(InputDim(1), 28),
InputDim(2),
Broadcast(InputDim(3), 24),
),
)
self.dimmap_test(
Tensor.expand,
(randn(24, 1, 36, 1), (36, 24, 28, -1, 24)),
(
Broadcast(Singleton(), 36),
InputDim(0),
Broadcast(InputDim(1), 28),
InputDim(2),
Broadcast(InputDim(3), 24),
),
)
self.dimmap_test(
torch.flatten,
(randn(24, 36),),
(Flatten((InputDim(0), InputDim(1))),),
)
self.dimmap_test(torch.flatten, (randn(28),), (InputDim(0),))
self.dimmap_test(torch.flatten, (randn(()),), (Singleton(),))
self.dimmap_test(
torch.movedim,
(randn(12, 24, 48, 96), 1, 2),
(InputDim(0), InputDim(2), InputDim(1), InputDim(3)),
)
self.dimmap_test(
torch.movedim,
(randn(4, 12, 24), 1, 0),
(InputDim(1), InputDim(0), InputDim(2)),
)
self.dimmap_test(
torch.movedim,
(randn(24, 12, 4), (1, 2), (0, 1)),
(InputDim(1), InputDim(2), InputDim(0)),
)
self.dimmap_test(
torch.movedim,
(randn(24, 4, 12), (0, 2, 1), (2, 1, 0)),
(InputDim(1), InputDim(2), InputDim(0)),
)
self.dimmap_test(
torch.movedim,
(randn(24, 12), (1, 0), (0, 1)),
(InputDim(1), InputDim(0)),
)
@skipIfUnsupportMultiNPU(4)
@with_comms
def test_view_ops_p3(self):
self.device_mesh = DeviceMesh(
self.device_type, torch.arange(dist.get_world_size()).view(-1, 2)
)
self.dimmap_test(
torch.movedim,
(randn(36, 24, 12), (1, 2), (0, 1)),
(InputDim(1), InputDim(2), InputDim(0)),
)
self.dimmap_test(
torch.movedim,
(randn(36, 24, 12), (1, 2), (-3, -2)),
(InputDim(1), InputDim(2), InputDim(0)),
)
self.dimmap_test(
torch.permute,
(randn(24, 36, 28), (2, 0, 1)),
(InputDim(2), InputDim(0), InputDim(1)),
)
self.dimmap_test(
torch.permute,
(randn(24, 36, 28), (-1, -3, -2)),
(InputDim(2), InputDim(0), InputDim(1)),
)
self.dimmap_test(
npu.npu_transpose,
(randn(24, 36, 28), (2, 0, 1)),
(InputDim(2), InputDim(0), InputDim(1)),
)
self.dimmap_test(
npu.npu_transpose,
(randn(24, 36, 28), (-1, -3, -2)),
(InputDim(2), InputDim(0), InputDim(1)),
)
self.dimmap_test(
torch.ravel,
(randn(24, 36),),
(Flatten((InputDim(0), InputDim(1))),),
)
self.dimmap_test(torch.ravel, (randn(28),), (InputDim(0),))
self.dimmap_test(torch.ravel, (randn(()),), (Singleton(),))
self.dimmap_test(
Tensor.repeat,
(randn(24, 36), 1, 2, 1, 1, 2),
(
Singleton(),
Broadcast(Singleton(), 2),
Singleton(),
InputDim(0),
Repeat(InputDim(1), 2),
),
)
@skipIfUnsupportMultiNPU(4)
@with_comms
def test_view_ops_p4(self):
self.device_mesh = DeviceMesh(
self.device_type, torch.arange(dist.get_world_size()).view(-1, 2)
)
self.dimmap_test(
torch.reshape,
(randn(4, 12, 24), (48, 24)),
(Flatten((InputDim(0), InputDim(1))), InputDim(2)),
)
self.dimmap_test(
torch.tile,
(randn(24, 36), (1, 2, 1, 1, 2)),
(
Singleton(),
Broadcast(Singleton(), 2),
Singleton(),
InputDim(0),
Repeat(InputDim(1), 2),
),
)
self.dimmap_test(
torch.tile,
(randn(28, 24, 36), (1, 3)),
(InputDim(0), InputDim(1), Repeat(InputDim(2), 3)),
)
self.dimmap_test(
torch.transpose,
(randn(24, 60, 28, 60), 2, 0),
(InputDim(2), InputDim(1), InputDim(0), InputDim(3)),
)
self.dimmap_test(
torch.transpose,
(randn(24, 60, 28, 60), -1, 0),
(InputDim(3), InputDim(1), InputDim(2), InputDim(0)),
)
self.dimmap_test(
torch.unsqueeze,
(randn(28, 24, 36), 1),
(InputDim(0), Singleton(), InputDim(1), InputDim(2)),
)
self.dimmap_test(
Tensor.view,
(randn(4, 12, 24), 48, 24),
(Flatten((InputDim(0), InputDim(1))), InputDim(2)),
)
self.dimmap_test(Tensor.view, (randn(1, 1, 12), -1), (InputDim(2),))
self.dimmap_test(
Tensor.view,
(randn(1, 1, 28, 24), -1),
(Flatten((InputDim(2), InputDim(3))),),
)
self.dimmap_test(
Tensor.view,
(randn(1, 1, 28, 1, 24, 1), -1),
(Flatten((InputDim(2), InputDim(4))),),
)
self.dimmap_test(
Tensor.view,
(randn(48, 35, 26), (24, 4, 35, 13)),
(
Split(
Flatten(input_dims=(InputDim(0), InputDim(1), InputDim(2))),
group_shape=(24, 4, 35, 13),
split_id=0,
),
Split(
Flatten(input_dims=(InputDim(0), InputDim(1), InputDim(2))),
group_shape=(24, 4, 35, 13),
split_id=1,
),
Split(
Flatten(input_dims=(InputDim(0), InputDim(1), InputDim(2))),
group_shape=(24, 4, 35, 13),
split_id=2,
),
Split(
Flatten(input_dims=(InputDim(0), InputDim(1), InputDim(2))),
group_shape=(24, 4, 35, 13),
split_id=3,
),
),
)
if __name__ == "__main__":
run_tests()