Skip to content

Commit 25c9530

Browse files
authored
[spmd_types] fix dsv3 ep + chunk loss typecheck (#3866)
https://github.com/pytorch/torchtitan/actions/runs/28794699622/job/85382003819 - ChunkedLossWrapper(CrossEntropyLoss) wrapper refactor broke typecheck, since now it always goes through base loss class - existing bug for EP + no typecheck
1 parent d04f7b9 commit 25c9530

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

torchtitan/components/loss.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ def __call__(
327327
# TODO(pianpwk): Teach spmd_types that P / scalar preserves P.
328328
with spmd.no_typecheck():
329329
loss = loss / global_valid_tokens
330+
if get_spmd_backend() == "spmd_types":
331+
spmd.assert_type(
332+
loss, {"dp": spmd.P, "cp": spmd.P, "tp": spmd.I}
333+
)
330334
return loss, {}
331335

332336

@@ -354,6 +358,10 @@ def __call__(
354358
# TODO(pianpwk): Teach spmd_types that P / scalar preserves P.
355359
with spmd.no_typecheck():
356360
loss = loss / global_valid_tokens
361+
if get_spmd_backend() == "spmd_types":
362+
spmd.assert_type(
363+
loss, {"dp": spmd.P, "cp": spmd.P, "tp": spmd.I}
364+
)
357365
return loss, {}
358366

359367

@@ -737,9 +745,6 @@ def _chunk(t):
737745
logits, label_chunk, global_valid_tokens, **chunk_inputs
738746
)
739747
metrics = self._combine_chunk_metrics(metrics, chunk_metrics)
740-
if get_spmd_backend() == "spmd_types":
741-
# V -> P reinterpret, after exiting local region.
742-
spmd.assert_type(chunk_loss, {"dp": spmd.P, "cp": spmd.P})
743748
total_loss = total_loss + chunk_loss.detach()
744749

745750
if requires_grad:

torchtitan/models/common/token_dispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def dispatch(
448448
if get_spmd_backend() == "spmd_types"
449449
else self.ep_mesh.get_group()
450450
)
451-
if get_spmd_backend() == "spmd_types":
451+
if get_spmd_backend() == "spmd_types" and spmd.is_type_checking():
452452
num_local_tokens_per_expert_E = spmd.reinterpret_mesh(
453453
num_local_tokens_per_expert_E, spmd.current_mesh()
454454
)

0 commit comments

Comments
 (0)