[diffusion] feat: allow T5's TP Group to reuse the transformer's SP Group#17818
[diffusion] feat: allow T5's TP Group to reuse the transformer's SP Group#17818mickqian merged 10 commits intosgl-project:mainfrom
Conversation
Summary of ChangesHello @nono-Sang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant optimization for T5 text encoders in distributed environments. It enables the text encoder's tensor parallelism to leverage existing sequence, Ulysses, or ring parallel groups from the main transformer model. This "parallel folding" can lead to more efficient resource utilization, particularly memory, by preventing redundant model copies on GPUs when other parallelism strategies are already in use. The changes involve modifying T5 configuration, refactoring distributed communication primitives, and updating parallel linear and embedding layers to support dynamic process group assignment. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new 'parallel folding' feature for T5 text encoders, allowing them to utilize existing sequence parallelism groups (SP, Ulysses, or Ring Group) as their tensor parallelism (TP) group. This is achieved by adding parallel_folding and parallel_folding_mode configurations to T5Config. The changes involve refactoring distributed communication operations to accept an optional tp_group argument, which defaults to the global TP group if not specified. This refactoring is consistently applied across various linear and embedding layers to enhance flexibility in group assignment. Additionally, new utility functions get_group_size and get_group_rank are introduced for abstracting process group properties. A notable change in wanvideo.py updates proj_out to use ColumnParallelLinear, enabling its parallelization.
| self.n_heads = config.num_heads // tp_world_size | ||
| self.tp_group = _get_folding_tp_group(config) | ||
| self.tp_world_size = get_group_size(self.tp_group) | ||
| assert config.num_heads % self.tp_world_size == 0 |
There was a problem hiding this comment.
| self.proj_out = ColumnParallelLinear( | ||
| inner_dim, | ||
| config.out_channels * math.prod(config.patch_size), | ||
| bias=True, | ||
| gather_output=True, |
There was a problem hiding this comment.
| def tensor_model_parallel_all_reduce( | ||
| input_: torch.Tensor, tp_group: dist.ProcessGroup = None |
There was a problem hiding this comment.
The addition of tp_group: dist.ProcessGroup = None as an optional argument and defaulting to get_tp_group() significantly enhances the flexibility of these communication operations. This is crucial for supporting the new parallel folding feature, allowing specific process groups to be used for tensor parallelism.
| tp_group: dist.ProcessGroup = None, | ||
| ): | ||
| # Divide the weight matrix along the last dimension. | ||
| self.tp_size = get_tp_world_size() | ||
| self.tp_group = tp_group or get_tp_group() | ||
| self.tp_size = get_group_size(self.tp_group) | ||
| self.tp_rank = get_group_rank(self.tp_group) |
| self.output_sizes = output_sizes | ||
| assert all(output_size % self.tp_size == 0 for output_size in output_sizes) |
| def get_group_size(group) -> int: | ||
| if hasattr(group, "world_size"): | ||
| return group.world_size # GroupCoordinator | ||
| elif hasattr(group, "size") and callable(getattr(group, "size", None)): | ||
| return group.size() # ProcessGroup | ||
| else: | ||
| raise ValueError(f"Unsupported group type: {type(group)}") | ||
|
|
||
|
|
||
| def get_group_rank(group) -> int: | ||
| if hasattr(group, "rank_in_group"): | ||
| return group.rank_in_group # GroupCoordinator | ||
| elif hasattr(group, "rank") and callable(getattr(group, "rank", None)): | ||
| return group.rank() # ProcessGroup | ||
| else: | ||
| raise ValueError(f"Unsupported group type: {type(group)}") |
There was a problem hiding this comment.
| def _get_folding_tp_group(config: T5Config) -> dist.ProcessGroup | None: | ||
| if config.parallel_folding: | ||
| if config.parallel_folding_mode == "sp": | ||
| return get_sp_group() | ||
| elif config.parallel_folding_mode == "ulysses": | ||
| return get_sp_group().ulysses_group | ||
| elif config.parallel_folding_mode == "ring": | ||
| return get_sp_group().ring_group | ||
| return get_tp_group() |
There was a problem hiding this comment.
|
@mickqian Any advices? |
| attn_bias: torch.Tensor | ||
|
|
||
|
|
||
| def _get_folding_tp_group(config: T5Config) -> dist.ProcessGroup | None: |
There was a problem hiding this comment.
could we have a @lru_cache(maxsize=1) here?
| attn_bias: torch.Tensor | ||
|
|
||
|
|
||
| def _get_folding_tp_group(config: T5Config) -> dist.ProcessGroup | None: |
There was a problem hiding this comment.
consider moving to somewhere like distributed/util.py
mickqian
left a comment
There was a problem hiding this comment.
brilliant. we should document this change
|
cc @nono-Sang |
6db6152 to
429f0f9
Compare
|
|
||
|
|
||
| _seen_keys = set() # 用集合记录已经出现过的 key | ||
| _seen_keys = set() |
There was a problem hiding this comment.
could you also clean this? seems redundant 😂
e34df17 to
2af63e2
Compare
|
/tag-and-rerun-ci |
…roup (sgl-project#17818) Co-authored-by: Mick <mickjagger19@icloud.com>
…roup (sgl-project#17818) Co-authored-by: Mick <mickjagger19@icloud.com>
Motivation
Currently, the
TP Groupof thetext_encoderis consistent with that of thetransformer. I have introduced an optional feature that allows thetext_encoderto adopt theSP or Ulysses or Ring Groupof thetransformeras its ownTP Group(referred to as "parallel folding").Use case: For instance, if I run inference on eight GPUs with
ulysses=8 and tp=1, each GPU will store a complete copy of thetext_encoder. By enabling parallel folding, thetext_encodercan utilize tp8 instead.Modifications
Accuracy Tests
Benchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci