-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[Neuron] Add tensor parallel support for Neuron backend #13718
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
JingyaHuang
wants to merge
54
commits into
huggingface:main
Choose a base branch
from
JingyaHuang:support-neuron-tp
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.
+942
−52
Open
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
98f6c8c
draft:add neuron as a legit backend
JingyaHuang c58b8b8
Merge branch 'huggingface:main' into add-neuron-backend
JingyaHuang 3367409
Merge branch 'huggingface:main' into add-neuron-backend
JingyaHuang 0c51734
Merge branch 'main' into add-neuron-backend
JingyaHuang a76953c
feat: neuron-specific changes in the pipeline
JingyaHuang 2480388
tests: eager tests
JingyaHuang 1469c04
draft: start with tp for flux2
JingyaHuang 929ab72
fix: style
JingyaHuang 52cac76
Merge branch 'huggingface:main' into add-neuron-backend
JingyaHuang 30cb353
Merge branch 'huggingface:main' into support-neuron-tp
JingyaHuang 28a5086
Merge branch 'add-neuron-backend' of github.com:JingyaHuang/diffusers…
JingyaHuang 7fab0c4
Merge branch 'huggingface:main' into support-neuron-tp
JingyaHuang 68689e5
Merge branch 'huggingface:main' into add-neuron-backend
JingyaHuang da79308
Merge branch 'main' into add-neuron-backend
JingyaHuang 3bb9c7c
fix:apr_02 beta
JingyaHuang c4facab
Merge branch 'add-neuron-backend' of github.com:JingyaHuang/diffusers…
JingyaHuang dff1f32
feat:add wan
JingyaHuang 1c930c4
Merge branch 'huggingface:main' into support-neuron-tp
JingyaHuang 1eb5ff9
Merge branch 'huggingface:main' into add-neuron-backend
JingyaHuang cbe8f28
fix:pixart
JingyaHuang 16b9606
fix: rewrite flux swiglu activation to avoid gather op in neuron IR
JingyaHuang 7f13f68
test: pixart compile mode on neuron
JingyaHuang a46cb19
Merge branch 'main' into neuron-torch-comppile
JingyaHuang a354b88
cleanup & fix style
JingyaHuang 931bb85
Merge branch 'neuron-torch-comppile' into support-neuron-tp
JingyaHuang 9ab6dc3
Merge branch 'main' into support-neuron-tp
JingyaHuang 48fb75b
Merge branch 'main' into support-neuron-tp
JingyaHuang c350f7b
merge: another change
JingyaHuang 644477a
Merge branch 'main' into support-neuron-tp
JingyaHuang 03cb725
review: cleanup+suggestions
JingyaHuang 9da93ed
Merge branch 'support-neuron-tp' of github.com:JingyaHuang/diffusers …
JingyaHuang d44f772
fix: CIs style
JingyaHuang 3fc043e
Merge branch 'main' into support-neuron-tp
JingyaHuang e6d20d8
tests: add test units for tp
JingyaHuang e76a2fc
Merge branch 'support-neuron-tp' of github.com:JingyaHuang/diffusers …
JingyaHuang 034ba9e
fix: in case of text-encoder(s) on CPU
JingyaHuang 4907524
review:cleanup+add test
JingyaHuang af2aed7
Merge branch 'support-neuron-tp' of github.com:JingyaHuang/diffusers …
JingyaHuang b9b048b
Merge branch 'main' into support-neuron-tp
JingyaHuang 915eeb1
fix: style
JingyaHuang 720dad2
Merge branch 'support-neuron-tp' of github.com:JingyaHuang/diffusers …
JingyaHuang 89cf8b6
doc: remove it for now
JingyaHuang 29cd9c3
Add from_single_file support for SkyReelsV2 and ChronoEdit transforme…
HaozheZhang6 eaab299
multi-GPU VAE Fix for Cosmos 3 (#13924)
atharvajoshi10 30a43d5
docs: fix repeated word typo in set_timesteps docstring (#13876)
ramkumar27072006 155802c
clean some stuff to simplify code.
sayakpaul f133732
Merge branch 'main' into JingyaHuang-support-neuron-tp
sayakpaul b3d8130
clean more to remove permutation related shenanigans.
sayakpaul 7ea75f7
revert: put torch.chunk back
JingyaHuang c73cf09
Merge branch 'main' into support-neuron-tp
JingyaHuang eb58402
Merge branch 'main' into support-neuron-tp
JingyaHuang c3e123c
Update docs/source/en/training/distributed_inference.md
JingyaHuang dc33e26
Merge Sayak's TP simplification (PR #1): generic Packed{Col,Row}wiseP…
JingyaHuang 491c537
Merge branch 'support-neuron-tp' of github.com:JingyaHuang/diffusers …
JingyaHuang 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
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,225 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # 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. | ||
|
|
||
| import torch | ||
|
|
||
| from ..models._modeling_parallel import TensorParallelConfig | ||
| from ..utils import get_logger | ||
|
|
||
|
|
||
| logger = get_logger(__name__) # pylint: disable=invalid-name | ||
|
|
||
|
|
||
| class PackedColwiseParallel: | ||
| """Column-wise sharding for fused projections with heterogeneous block structure. | ||
|
|
||
| ``blocks`` is a list of proportional integers whose sum divides the weight's row count. For example, ``[1, 1]`` for | ||
| a SwiGLU gate+linear projection (two equal halves) or ``[1, 1, 1, 3, 3]`` for a Q+K+V+gate+linear projection with | ||
| ``mlp_ratio=3``. If ``blocks`` is ``None``, the Linear module must carry a ``_tp_packed_col_blocks`` attribute set | ||
| during model ``__init__``. | ||
| """ | ||
|
|
||
| def __init__(self, blocks: "list[int] | None" = None): | ||
| self.blocks = blocks | ||
|
|
||
|
|
||
| class PackedRowwiseParallel: | ||
| """Row-wise sharding for fused projections with heterogeneous block structure. | ||
|
|
||
| ``blocks`` describes the input-column partition of the fused Linear (e.g. ``[1, 3]`` when the input concatenates an | ||
| attention projection and an MLP projection with ``mlp_ratio=3``). If ``blocks`` is ``None``, the module must carry | ||
| a ``_tp_packed_row_blocks`` attribute. | ||
| """ | ||
|
|
||
| def __init__(self, blocks: "list[int] | None" = None): | ||
| self.blocks = blocks | ||
|
|
||
|
|
||
| def _blocks_to_block_sizes(total_size: int, blocks: "list[int]") -> "list[int]": | ||
| """Convert proportional block counts to absolute sizes. | ||
|
|
||
| ``blocks`` is a list of positive integers interpreted as proportional weights. Their sum must divide ``total_size`` | ||
| evenly. Returns a list of absolute sizes that sum to ``total_size``. | ||
| """ | ||
| total = sum(blocks) | ||
| if total_size % total != 0: | ||
| raise ValueError( | ||
| f"Cannot split {total_size} into proportional blocks {blocks}: " | ||
| f"sum({blocks})={total} does not divide {total_size}." | ||
| ) | ||
| unit = total_size // total | ||
| return [b * unit for b in blocks] | ||
|
|
||
|
|
||
| def _resolve_tp_plan(model: torch.nn.Module, tp_plan: dict) -> list: | ||
| """Group a flat ``_tp_plan`` into per-block ``(submodule, {relative_path: style})`` plans. | ||
|
|
||
| Each glob is split at its single ``*``; the prefix must resolve to a ``ModuleList`` and the suffix is the | ||
| per-element key. Grouping by block lets the caller issue one ``parallelize_module`` call per block, which | ||
| ``RowwiseParallel`` needs to attach its input redistribution at the block boundary. | ||
| """ | ||
|
JingyaHuang marked this conversation as resolved.
|
||
| grouped: dict[int, tuple] = {} | ||
| order: list[int] = [] | ||
|
|
||
| for pattern, style in tp_plan.items(): | ||
| if pattern.count("*") > 1: | ||
| raise ValueError(f"Wildcard '*' can only be used once in a `_tp_plan` key, got '{pattern}'.") | ||
|
|
||
| if "*" in pattern: | ||
| prefix, _, suffix = pattern.partition("*") | ||
| container = model | ||
| for atom in prefix.strip(".").split("."): | ||
| container = getattr(container, atom) | ||
| if not isinstance(container, torch.nn.ModuleList): | ||
| raise ValueError( | ||
| f"`_tp_plan` wildcard '{pattern}' must expand over a `ModuleList`, but " | ||
| f"'{prefix.strip('.')}' resolved to '{container.__class__.__name__}'." | ||
| ) | ||
| relative, blocks = suffix.strip("."), list(container) | ||
| else: | ||
| relative, blocks = pattern, [model] | ||
|
|
||
| for block in blocks: | ||
| key = id(block) | ||
| if key not in grouped: | ||
| grouped[key] = (block, {}) | ||
| order.append(key) | ||
| grouped[key][1][relative] = style | ||
|
|
||
| return [grouped[key] for key in order] | ||
|
|
||
|
|
||
| def _styles(relative_plan: dict) -> dict: | ||
| """Map a ``{relative_path: style}`` plan to ``parallelize_module`` style instances. | ||
|
|
||
| Values may be plain strings (``"colwise"`` / ``"rowwise"``) or ``PackedColwiseParallel`` / | ||
| ``PackedRowwiseParallel`` marker instances. | ||
| """ | ||
| import torch.nn as nn | ||
| from torch.distributed.tensor import DTensor, Replicate, Shard, distribute_tensor | ||
| from torch.distributed.tensor.parallel import ColwiseParallel, RowwiseParallel | ||
|
|
||
| def _make_packed_col(marker: PackedColwiseParallel) -> ColwiseParallel: | ||
| _blocks = marker.blocks | ||
|
|
||
| class _PackedColwiseImpl(ColwiseParallel): | ||
| def _partition_linear_fn(self, name, module, device_mesh): | ||
| blocks = _blocks if _blocks is not None else getattr(module, "_tp_packed_col_blocks") | ||
| rank = device_mesh.get_local_rank() | ||
| tp_size = device_mesh.size() | ||
| for param_name, param in module.named_parameters(): | ||
| if param_name == "weight": | ||
| full = distribute_tensor( | ||
| param, device_mesh, [Replicate()], src_data_rank=self.src_data_rank | ||
| ).to_local() | ||
| block_sizes = _blocks_to_block_sizes(full.shape[0], blocks) | ||
| parts, offset = [], 0 | ||
| for bs in block_sizes: | ||
| chunk = bs // tp_size | ||
| parts.append(full[offset + rank * chunk : offset + (rank + 1) * chunk].contiguous()) | ||
| offset += bs | ||
| local = torch.cat(parts, dim=0) | ||
| dist_param = nn.Parameter( | ||
| DTensor.from_local(local, device_mesh, [Shard(0)], run_check=False), | ||
| requires_grad=param.requires_grad, | ||
| ) | ||
| else: | ||
| dist_param = nn.Parameter( | ||
| distribute_tensor(param, device_mesh, [Shard(0)], src_data_rank=self.src_data_rank), | ||
| requires_grad=param.requires_grad, | ||
| ) | ||
| module.register_parameter(param_name, dist_param) | ||
|
|
||
| return _PackedColwiseImpl() | ||
|
|
||
| def _make_packed_row(marker: PackedRowwiseParallel) -> RowwiseParallel: | ||
| _blocks = marker.blocks | ||
|
|
||
| class _PackedRowwiseImpl(RowwiseParallel): | ||
| def _partition_linear_fn(self, name, module, device_mesh): | ||
| blocks = _blocks if _blocks is not None else getattr(module, "_tp_packed_row_blocks") | ||
| rank = device_mesh.get_local_rank() | ||
| tp_size = device_mesh.size() | ||
| for param_name, param in module.named_parameters(): | ||
| if param_name == "weight": | ||
| full = distribute_tensor( | ||
| param, device_mesh, [Replicate()], src_data_rank=self.src_data_rank | ||
| ).to_local() | ||
| block_sizes = _blocks_to_block_sizes(full.shape[1], blocks) | ||
| parts, offset = [], 0 | ||
| for bs in block_sizes: | ||
| chunk = bs // tp_size | ||
| parts.append(full[:, offset + rank * chunk : offset + (rank + 1) * chunk].contiguous()) | ||
| offset += bs | ||
| local = torch.cat(parts, dim=1) | ||
| dist_param = nn.Parameter( | ||
| DTensor.from_local(local, device_mesh, [Shard(1)], run_check=False), | ||
| requires_grad=param.requires_grad, | ||
| ) | ||
| else: | ||
| dist_param = nn.Parameter( | ||
| distribute_tensor(param, device_mesh, [Replicate()], src_data_rank=self.src_data_rank), | ||
| requires_grad=param.requires_grad, | ||
| ) | ||
| module.register_parameter(param_name, dist_param) | ||
|
|
||
| return _PackedRowwiseImpl() | ||
|
|
||
| resolved = {} | ||
| for path, style in relative_plan.items(): | ||
| if style == "colwise": | ||
| resolved[path] = ColwiseParallel() | ||
| elif style == "rowwise": | ||
| resolved[path] = RowwiseParallel() | ||
| elif isinstance(style, PackedColwiseParallel): | ||
| resolved[path] = _make_packed_col(style) | ||
| elif isinstance(style, PackedRowwiseParallel): | ||
| resolved[path] = _make_packed_row(style) | ||
| else: | ||
| raise ValueError( | ||
| f"Unsupported tensor-parallel style '{style}' for '{path}'. " | ||
| f"Expected 'colwise', 'rowwise', PackedColwiseParallel, or PackedRowwiseParallel." | ||
| ) | ||
| return resolved | ||
|
|
||
|
|
||
| def apply_tensor_parallel( | ||
| model: torch.nn.Module, | ||
| config: TensorParallelConfig, | ||
| tp_plan: dict, | ||
| *, | ||
| backend: str = "default", | ||
|
Member
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. Can this not be derived from |
||
| ) -> None: | ||
| """Apply tensor parallel on a model from its flat ``_tp_plan``. | ||
|
|
||
| ``backend="neuron"`` routes to the Neuron pre-shard path (works around the NRT consecutive-reduce-scatter bug and | ||
| applies the Flux2 fused-weight permutations); ``"default"`` uses ``parallelize_module`` directly. | ||
| """ | ||
| tp_mesh = config._mesh | ||
| if tp_mesh is None: | ||
| raise ValueError("`config._mesh` is None. Call `config.setup(rank, world_size, device)` before applying TP.") | ||
|
|
||
| groups = _resolve_tp_plan(model, tp_plan) | ||
| logger.debug(f"Applying tensor parallel (backend={backend}) over {len(groups)} module group(s) on mesh {tp_mesh}.") | ||
|
|
||
| if backend == "neuron": | ||
| from .tensor_parallel_neuron import _apply_tp_neuron | ||
|
|
||
| _apply_tp_neuron(model, tp_mesh, groups) | ||
| return | ||
|
|
||
| from torch.distributed.tensor.parallel import parallelize_module | ||
|
|
||
| for submodule, relative_plan in groups: | ||
| parallelize_module(submodule, tp_mesh, _styles(relative_plan)) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.