Skip to content

Commit bcf92ca

Browse files
authored
Fallback to single GPU if tp_group has size 1 (#314)
* fallback to single GPU if tp_group has size 1 Signed-off-by: Hao Wu <skyw@nvidia.com>
1 parent a966f00 commit bcf92ca

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

‎emerging_optimizers/orthogonalized_optimizers/muon_utils.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from absl import logging
2020

2121
from emerging_optimizers import triton_kernels
22+
from emerging_optimizers.utils import get_pg_size
2223

2324

2425
__all__ = ["newton_schulz", "newton_schulz_tp", "NSCoeffT", "get_coefficient_iterator", "CoeffIterMode"]
@@ -306,7 +307,7 @@ def newton_schulz_tp(
306307
307308
This function uses partition_dim to determine along which dimension the input tensor is sharded. Transpose is
308309
set based on the partition_dim. If partition_dim is None, the input tensor is not sharded and the function will
309-
fall back to the non-TP path.
310+
fall back to the non-TP path. It also falls back to the non-TP path if tp_group has size 1.
310311
311312
Warning:
312313
If partition_dim is the smaller dim of the input tensor, `distributed` mode will run Newton-Schulz along the
@@ -333,7 +334,14 @@ def newton_schulz_tp(
333334
takes effect when the fp32 matmul precision is ``"medium"`` (see ``newton_schulz``).
334335
Falls back to GEMM when either matrix dimension is not a multiple of 8.
335336
"""
336-
if partition_dim is None:
337+
if partition_dim is not None:
338+
if tp_mode not in ("duplicated", "distributed"):
339+
raise ValueError(f"Invalid tp_mode: {tp_mode}")
340+
if tp_mode == "distributed" and partition_dim not in (0, 1):
341+
raise ValueError(f"Invalid partition_dim: {partition_dim}")
342+
343+
tp_size = get_pg_size(tp_group)
344+
if partition_dim is None or tp_size == 1:
337345
# Fallback path for non TP params.
338346
return newton_schulz(x, steps, coefficient_type, use_syrk=use_syrk)
339347

@@ -344,22 +352,14 @@ def newton_schulz_tp(
344352
}
345353

346354
if tp_mode == "duplicated":
347-
x_shards = [torch.empty_like(x) for _ in range(tp_group.size())]
355+
x_shards = [torch.empty_like(x) for _ in range(tp_size)]
348356
torch.distributed.all_gather(x_shards, x, tp_group)
349357
global_x = torch.cat(x_shards, dim=partition_dim)
350358

351359
orthogonalized_x = newton_schulz(global_x, tp_group=None, **kwargs)
352-
output = orthogonalized_x.chunk(tp_group.size(), dim=partition_dim)[tp_group.rank()]
353-
elif tp_mode == "distributed":
354-
if partition_dim == 0:
355-
transpose = True
356-
elif partition_dim == 1:
357-
transpose = False
358-
else:
359-
raise ValueError(f"Invalid partition_dim: {partition_dim}")
360-
output = newton_schulz(x, **kwargs, transpose=transpose, tp_group=tp_group)
360+
output = orthogonalized_x.chunk(tp_size, dim=partition_dim)[tp_group.rank()]
361361
else:
362-
raise ValueError(f"Invalid tp_mode: {tp_mode}")
362+
output = newton_schulz(x, **kwargs, transpose=partition_dim == 0, tp_group=tp_group)
363363

364364
return output
365365

0 commit comments

Comments
 (0)