Skip to content

Commit 241fa92

Browse files
CopilotIvanYashchuk
andcommitted
Add tests for use_dtensor_execute flag
Co-authored-by: IvanYashchuk <19621411+IvanYashchuk@users.noreply.github.com>
1 parent a353391 commit 241fa92

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

thunder/tests/distributed/test_dtensor.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,47 @@ def test_dtensor_opinfo(self, op: OpInfo, executor):
459459
assert tested_sample_count > 0, f"test_dtensor_opinfo:No samples tested for {op.name} with {executor} executor"
460460

461461

462+
@common_utils.parametrize("executor", tuple(executors_map.keys()))
463+
def test_use_dtensor_execute_flag_with_dtensor(self, executor):
464+
"""Test that use_dtensor_execute flag is set to True for DTensor inputs."""
465+
import thunder.examine as examine
466+
467+
num_devices = self.world_size
468+
mesh = DeviceMesh("cuda", list(range(num_devices)))
469+
dim_size = 16
470+
471+
in_dtensor = distribute_tensor(torch.randn(dim_size, dim_size, requires_grad=True), mesh, [Shard(0)])
472+
w_dtensor = distribute_tensor(torch.randn(dim_size, dim_size, requires_grad=True), mesh, [Shard(0)])
473+
474+
def fn(x, w):
475+
return torch.mul(x, w)
476+
477+
tmodel = thunder.jit(fn, executors=executors_map[executor].executors_list())
478+
result = tmodel(in_dtensor, w_dtensor)
479+
480+
traces = thunder.last_traces(tmodel)
481+
extrace = traces[-1]
482+
fusions = examine.get_fusion_symbols(extrace)
483+
484+
# Verify there is at least one fusion
485+
assert len(fusions) > 0
486+
487+
# Check that use_dtensor_execute is True for DTensor inputs
488+
for fusion_bsym in fusions:
489+
_, call_ctx, _ = fusion_bsym.gather_ctxs()
490+
fusion_name = fusion_bsym.sym.name
491+
fdw = call_ctx.get(fusion_name)
492+
assert fdw is not None, f"FusionDefinitionWrapper not found for {fusion_name}"
493+
assert hasattr(
494+
fdw, "use_dtensor_execute"
495+
), "FusionDefinitionWrapper should have use_dtensor_execute attribute"
496+
assert fdw.use_dtensor_execute is True, "use_dtensor_execute should be True for DTensor inputs"
497+
498+
# Verify the result is correct
499+
expected = torch.mul(in_dtensor, w_dtensor)
500+
torch.testing.assert_close(result, expected)
501+
502+
462503
common_utils.instantiate_parametrized_tests(DTensorTest)
463504

464505
if __name__ == "__main__":

thunder/tests/test_nvfuser.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,3 +1040,40 @@ def test_scatter(executor, device: str, dtype: dtypes.dtype):
10401040
assert len(fusion_bsyms) == 1
10411041
outside_fusion_syms = ["unpack_trivial", "python_return"]
10421042
assert {el.sym.name for el in fw_trace.bound_symbols if not el.sym.is_fusion} == set(outside_fusion_syms)
1043+
1044+
1045+
@instantiate(
1046+
executors=(nvFuserExecutor,),
1047+
dtypes=(thunder.float32,),
1048+
)
1049+
def test_use_dtensor_execute_flag(executor, device: str, dtype: dtypes.dtype):
1050+
"""Test that use_dtensor_execute flag is set correctly at trace construction time."""
1051+
torch_dtype = ltorch.to_torch_dtype(dtype)
1052+
a = make_tensor((2, 2), device=device, dtype=torch_dtype)
1053+
b = make_tensor((2, 2), device=device, dtype=torch_dtype)
1054+
1055+
def foo(a, b):
1056+
return a + b
1057+
1058+
cfoo = thunder.jit(foo)
1059+
result = cfoo(a, b)
1060+
1061+
traces = thunder.last_traces(cfoo)
1062+
extrace = traces[-1]
1063+
fusions = examine.get_fusion_symbols(extrace)
1064+
1065+
# Verify there is at least one fusion
1066+
assert len(fusions) > 0
1067+
1068+
# Check that use_dtensor_execute is False for regular tensors
1069+
for fusion_bsym in fusions:
1070+
_, call_ctx, _ = fusion_bsym.gather_ctxs()
1071+
fusion_name = fusion_bsym.sym.name
1072+
fdw = call_ctx.get(fusion_name)
1073+
assert fdw is not None, f"FusionDefinitionWrapper not found for {fusion_name}"
1074+
assert hasattr(fdw, "use_dtensor_execute"), "FusionDefinitionWrapper should have use_dtensor_execute attribute"
1075+
assert fdw.use_dtensor_execute is False, "use_dtensor_execute should be False for regular tensors"
1076+
1077+
# Verify the result is correct
1078+
expected = a + b
1079+
torch.testing.assert_close(result, expected)

0 commit comments

Comments
 (0)