Skip to content

Commit 6845f33

Browse files
committed
refactor: share the CPU pre-shard path between Neuron and TPU tensor parallelism
1 parent 986ea9a commit 6845f33

2 files changed

Lines changed: 7 additions & 60 deletions

File tree

‎src/diffusers/hooks/tensor_parallel.py‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,8 @@ def apply_tensor_parallel(
565565
f"or from the active accelerator when the mesh is built from `tp_degree`."
566566
)
567567

568-
backend = tp_mesh.device_type if tp_mesh.device_type in ("neuron", "tpu") else "default"
569568
groups = _resolve_tp_plan(model, tp_plan)
570-
logger.debug(f"Applying tensor parallel (backend={backend}) over {len(groups)} module group(s) on mesh {tp_mesh}.")
569+
logger.debug(f"Applying tensor parallel over {len(groups)} module group(s) on mesh {tp_mesh}.")
571570

572571
from torch.distributed.tensor.parallel import parallelize_module
573572

@@ -579,16 +578,12 @@ def apply_tensor_parallel(
579578
# Also validates every planned shard, so an uneven split raises before any module is sharded.
580579
specs = resolve_tp_shard_specs(model, tp_plan, tp_mesh.size())
581580

582-
if backend == "neuron":
583-
from .tensor_parallel_neuron import _apply_tp_neuron
584-
585-
_apply_tp_neuron(model, tp_mesh, groups, specs)
586-
return
587-
588-
if backend == "tpu":
589-
# `parallelize_module` would materialize every full weight on each chip before scattering it, which runs out
590-
# of HBM on models larger than one chip. Pre-sharding on CPU keeps only this rank's slice on the device.
591-
_pre_shard_and_parallelize(model, tp_mesh, groups, specs, config._device)
581+
if tp_mesh.device_type in ("neuron", "tpu"):
582+
# Pre-shard on CPU instead of letting `parallelize_module` distribute the weights. On Neuron, consecutive
583+
# `reduce_scatter`s of large weights (>= 5120x5120) in one `parallelize_module` call can fail in the runtime. On
584+
# TPU, it would materialize every full weight on each chip first, which runs out of HBM on large models.
585+
device = torch.neuron.current_device() if tp_mesh.device_type == "neuron" else config._device
586+
_pre_shard_and_parallelize(model, tp_mesh, groups, specs, device)
592587
return
593588

594589
for submodule, relative_plan in groups:

‎src/diffusers/hooks/tensor_parallel_neuron.py‎

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)