-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Allow tensor parallelism and context parallelism in one ParallelConfig #14725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
whn09
wants to merge
3
commits into
huggingface:main
Choose a base branch
from
whn09:tp-cp-compose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1669,15 +1669,33 @@ def enable_parallelism( | |
| break | ||
|
|
||
| mesh = None | ||
| if config.context_parallel_config is not None: | ||
| cp_config = config.context_parallel_config | ||
| cp_config, tp_config = config.context_parallel_config, config.tensor_parallel_config | ||
| if cp_config is not None and tp_config is not None: | ||
| # A single mesh spanning both parallelisms, with the context parallel dimensions varying fastest: a | ||
| # CP group is then a contiguous run of ranks and a TP group takes one rank out of each run. | ||
| # | ||
| # That order is a default, not a universal optimum. Some accelerator runtimes only accept contiguous | ||
| # replica groups for all-to-all -- which Ulysses needs -- while tolerating strided groups for | ||
| # all-reduce, which is all TP needs; since only one axis of a 2-D mesh can be contiguous, Ulysses | ||
| # gets it. On multi-node CUDA the opposite order is usually preferable, because TP is the most | ||
| # bandwidth-hungry collective and wants to stay within one NVLink domain. Pass `mesh=` on either | ||
| # config to choose the layout yourself. | ||
| mesh = ( | ||
| cp_config.mesh | ||
| or tp_config.mesh | ||
| or torch.distributed.device_mesh.init_device_mesh( | ||
| device_type=device_type, | ||
| mesh_shape=(tp_config.tp_degree, *cp_config.mesh_shape), | ||
| mesh_dim_names=("tp", *cp_config.mesh_dim_names), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sayakpaul what's your take on the default order of the mesh? If Neuron runtime requests Ulysses mesh to be contiguous maybe we should set default value according to the device? |
||
| ) | ||
| ) | ||
| elif cp_config is not None: | ||
| mesh = cp_config.mesh or torch.distributed.device_mesh.init_device_mesh( | ||
| device_type=device_type, | ||
| mesh_shape=cp_config.mesh_shape, | ||
| mesh_dim_names=cp_config.mesh_dim_names, | ||
| ) | ||
| elif config.tensor_parallel_config is not None: | ||
| tp_config = config.tensor_parallel_config | ||
| elif tp_config is not None: | ||
| mesh = tp_config.mesh or torch.distributed.device_mesh.init_device_mesh( | ||
| device_type=device_type, | ||
| mesh_shape=(tp_config.tp_degree,), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # coding=utf-8 | ||
| # Copyright 2026 HuggingFace Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Generic torchrun worker: assert a model's Neuron tensor-parallel x context-parallel output matches its reference. | ||
|
|
||
| The counterpart of `_neuron_tp_worker.py` for the two parallelisms composed in a single `ParallelConfig`. Same | ||
| contract: the model under test is supplied as a `module:function` spec reference on the command line, and the | ||
| referenced factory returns `(model_class, init_dict, inputs)` with CPU tensors. | ||
|
|
||
| torchrun --nproc_per_node=8 _neuron_hybrid_worker.py \\ | ||
| tests.models.transformers.test_models_transformer_flux:make_neuron_hybrid_spec | ||
|
|
||
| `tp_degree` and `ulysses_degree` are read from `TP_DEGREE` / `ULYSSES_DEGREE` (defaults 2 and 4, whose product is | ||
| the launched world size). `ulysses_degree` cannot be 2 on Neuron: its all-to-all only accepts group sizes of 4, 8, | ||
| 16 or multiples of 32. | ||
|
|
||
| No mesh is passed, so this also exercises the default mesh layout that `enable_parallelism` builds for the combined | ||
| case -- which matters on Neuron, where the all-to-all Ulysses depends on rejects strided replica groups and so the | ||
| context-parallel dimensions have to vary fastest. | ||
|
|
||
| Exit code 0 means the composed path is numerically equivalent to the unsharded model; non-zero means failure. | ||
| """ | ||
|
|
||
| import argparse | ||
| import importlib | ||
| import os | ||
| import sys | ||
| import traceback | ||
|
|
||
|
|
||
| # Make the in-repo `diffusers` and `tests` packages importable when run via torchrun from an arbitrary CWD. | ||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "src")) | ||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..")) | ||
|
|
||
| import torch | ||
| import torch.distributed as dist | ||
| import torch_neuronx # noqa: F401 — registers torch.neuron | ||
|
|
||
| from diffusers import ContextParallelConfig, ParallelConfig, TensorParallelConfig | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="Neuron tensor-parallel x context-parallel correctness worker.") | ||
| parser.add_argument( | ||
| "spec", | ||
| help="`module:function` reference returning (model_class, init_dict, cpu_inputs) for the model under test.", | ||
| ) | ||
| args = parser.parse_args() | ||
| module_name, _, fn_name = args.spec.partition(":") | ||
| model_class, init_dict, inputs = getattr(importlib.import_module(module_name), fn_name)() | ||
|
|
||
| tp_degree = int(os.environ.get("TP_DEGREE", "2")) | ||
| ulysses_degree = int(os.environ.get("ULYSSES_DEGREE", "4")) | ||
|
|
||
| dist.init_process_group(backend="neuron") | ||
| rank = dist.get_rank() | ||
| world_size = dist.get_world_size() | ||
| device = torch.neuron.current_device() | ||
|
|
||
| if tp_degree * ulysses_degree != world_size: | ||
| raise ValueError( | ||
| f"tp_degree ({tp_degree}) x ulysses_degree ({ulysses_degree}) must equal the world size ({world_size})." | ||
| ) | ||
|
|
||
| # Identical weights on every rank (same seed), kept on CPU as the Neuron pre-shard backend requires. | ||
| torch.manual_seed(0) | ||
| model = model_class(**init_dict).eval() | ||
|
|
||
| # Single-device (unsharded) reference on CPU, computed before the shard plan mutates the weights in place. | ||
| with torch.no_grad(): | ||
| ref_output = model(**inputs, return_dict=False)[0].float().cpu() | ||
|
|
||
| model.enable_parallelism( | ||
| config=ParallelConfig( | ||
| tensor_parallel_config=TensorParallelConfig(tp_degree=tp_degree), | ||
| context_parallel_config=ContextParallelConfig(ulysses_degree=ulysses_degree), | ||
| ) | ||
| ) | ||
| model = model.to(device) | ||
| torch.neuron.synchronize() | ||
|
|
||
| inputs_on_device = {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in inputs.items()} | ||
| with torch.no_grad(): | ||
| output = model(**inputs_on_device, return_dict=False)[0] | ||
| torch.neuron.synchronize() | ||
| output = output.float().cpu() | ||
|
|
||
| if rank == 0: | ||
| assert output.shape == ref_output.shape, f"shape mismatch: {output.shape} vs {ref_output.shape}" | ||
| assert torch.isfinite(output).all(), "output contains non-finite values" | ||
| max_abs = (output - ref_output).abs().max().item() | ||
| denom = ref_output.abs().max().item() + 1e-6 | ||
| print( | ||
| f"[rank0] tp_degree={tp_degree} ulysses_degree={ulysses_degree} " | ||
| f"output_shape={tuple(output.shape)} max_abs_diff={max_abs:.4e} max_rel_diff={max_abs / denom:.4e}" | ||
| ) | ||
| # Neuron runs matmuls in bf16 internally, so compare with a bf16-level tolerance, as `_neuron_tp_worker` | ||
| # does. A wrong shard plan or a mis-ordered mesh produces grossly different output and is caught well | ||
| # inside this bound. | ||
| torch.testing.assert_close(output, ref_output, atol=2e-2, rtol=2e-2) | ||
| print("[rank0] PASS: Neuron hybrid-parallel output matches single-device reference.") | ||
|
|
||
| dist.barrier() | ||
| dist.destroy_process_group() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| main() | ||
| except Exception: | ||
| traceback.print_exc() | ||
| # Ensure a non-zero exit so the launching pytest sees the failure. | ||
| os._exit(1) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put these lines under
TensorParallelConfig.setup?