-
Notifications
You must be signed in to change notification settings - Fork 675
feat: add parallelization filters #4144
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
tedzhouhk
wants to merge
12
commits into
main
Choose a base branch
from
hzhou/parallel-filter
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 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
377d8e2
stage
tedzhouhk 10dd2cf
Merge branch 'main' of https://github.com/ai-dynamo/dynamo into hzhou…
tedzhouhk 55379a8
feat: add parallelization mapping filter
tedzhouhk 524b71d
Apply suggestion from @coderabbitai[bot]
tedzhouhk 5f4fd0a
address coderabbit
tedzhouhk 0fd70e5
Merge branch 'main' of https://github.com/ai-dynamo/dynamo into hzhou…
tedzhouhk 9556137
address PR issues
tedzhouhk 5b5bcca
Merge branch 'main' into hzhou/parallel-filter
tedzhouhk 1620c5a
address pr
tedzhouhk 77949d7
pr comments
tedzhouhk 6ef9464
address pr comment
tedzhouhk b5005e6
Merge branch 'main' into hzhou/parallel-filter
tedzhouhk 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
170 changes: 170 additions & 0 deletions
170
benchmarks/profiler/utils/config_modifiers/parallelization_mapping.py
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,170 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import logging | ||
| from dataclasses import dataclass | ||
|
|
||
| from benchmarks.profiler.utils.model_info import ModelInfo | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| logger.setLevel(logging.INFO) | ||
| console_handler = logging.StreamHandler() | ||
| console_handler.setLevel(logging.INFO) | ||
| formatter = logging.Formatter( | ||
| "%(asctime)s - %(name)s - %(levelname)s - %(message)s", "%Y-%m-%d %H:%M:%S" | ||
| ) | ||
| console_handler.setFormatter(formatter) | ||
| logger.addHandler(console_handler) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ParallelizationMapping: | ||
| """ | ||
| Represents parallelization mapping of configs | ||
| """ | ||
|
|
||
| tp: int | None = None | ||
| tep: int | None = None | ||
| dep: int | None = None | ||
|
|
||
| def label(self, phase: str) -> str: | ||
| if self.tp is not None: | ||
| return f"TP={self.tp}" | ||
| if phase == "prefill" and self.tep is not None: | ||
| return f"TEP={self.tep}" | ||
| if phase == "decode" and self.dep is not None: | ||
| return f"DEP={self.dep}" | ||
| return "default" | ||
|
|
||
|
|
||
| def get_candidate_parallel_mappings( | ||
| num_gpus: int, model_info: ModelInfo, phase: str | ||
| ) -> list[ParallelizationMapping]: | ||
| """ | ||
| Return a list of candidate parallelization mappings for a given GPU count and phase, | ||
| verified against model properties. | ||
| Verification rules: | ||
| - TP and TEP must divide num_kv_heads (if available) | ||
| - TEP and DEP must divide num_experts (if available) | ||
| """ | ||
| is_moe = bool(model_info.is_moe) | ||
| num_kv_heads = model_info.num_kv_heads | ||
| num_experts = model_info.num_experts | ||
| intermediate_size = model_info.intermediate_size | ||
| quant_block = model_info.quantization_block_size | ||
|
|
||
| candidates: list[ParallelizationMapping] = [] | ||
| if is_moe: | ||
| if phase == "prefill": | ||
| candidates = [ParallelizationMapping(tep=num_gpus)] | ||
| else: | ||
| candidates = [ParallelizationMapping(dep=num_gpus)] | ||
| else: | ||
| candidates = [ParallelizationMapping(tp=num_gpus)] | ||
|
|
||
| # now verify if the candidates are valid | ||
| verified: list[ParallelizationMapping] = [] | ||
| for m in candidates: | ||
| # 1) KV heads divisibility checks | ||
| if m.tp is not None: | ||
| if num_kv_heads is None: | ||
| logger.warning( | ||
| f"Skipping KV heads divisibility check for TP={m.tp}: num_kv_heads is unknown" | ||
| ) | ||
| else: | ||
| if int(num_kv_heads) % int(m.tp) != 0: | ||
| logger.warning( | ||
| f"Invalid mapping TP={m.tp}: num_kv_heads={num_kv_heads} not divisible by TP" | ||
| ) | ||
| continue | ||
|
|
||
| if m.tep is not None: | ||
| if num_kv_heads is None: | ||
| logger.warning( | ||
| f"Skipping KV heads divisibility check for TEP={m.tep}: num_kv_heads is unknown" | ||
| ) | ||
| else: | ||
| if int(num_kv_heads) % int(m.tep) != 0: | ||
| logger.warning( | ||
| f"Invalid mapping TEP={m.tep}: num_kv_heads={num_kv_heads} not divisible by TEP" | ||
| ) | ||
| continue | ||
|
|
||
| # 2) Experts divisibility checks (for MoE) | ||
| if m.tep is not None: | ||
| if num_experts is None: | ||
| logger.warning( | ||
| f"Skipping experts divisibility check for TEP={m.tep}: num_experts is unknown" | ||
| ) | ||
| else: | ||
| if int(num_experts) % int(m.tep) != 0: | ||
| logger.warning( | ||
| f"Invalid mapping TEP={m.tep}: num_experts={num_experts} not divisible by TEP" | ||
| ) | ||
| continue | ||
|
|
||
| if m.dep is not None: | ||
| if num_experts is None: | ||
| logger.warning( | ||
| f"Skipping experts divisibility check for DEP={m.dep}: num_experts is unknown" | ||
| ) | ||
| else: | ||
| if int(num_experts) % int(m.dep) != 0: | ||
| logger.warning( | ||
| f"Invalid mapping DEP={m.dep}: num_experts={num_experts} not divisible by DEP" | ||
| ) | ||
| continue | ||
hhzhang16 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # 3) Intermediate size vs quantization block checks | ||
| # Always check: intermediate_size % parallel_size == 0 when intermediate_size is known | ||
| # Additionally (if quant_block known): (intermediate_size // parallel_size) divides quant_block if quant_block is known | ||
| # Applies to TP and TEP only | ||
| if intermediate_size is not None: | ||
| parallel_size = None | ||
| tag = None | ||
| if m.tp is not None: | ||
| parallel_size = int(m.tp) | ||
| tag = "TP" | ||
hhzhang16 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| elif m.tep is not None: | ||
| parallel_size = int(m.tep) | ||
| tag = "TEP" | ||
|
|
||
| if parallel_size is not None and parallel_size > 0: | ||
| intermediate_size = int(intermediate_size) | ||
| if intermediate_size % parallel_size != 0: | ||
| logger.warning( | ||
| f"Invalid mapping {tag}={parallel_size}: intermediate_size={intermediate_size} not divisible by {tag}" | ||
| ) | ||
| continue | ||
| if quant_block is not None: | ||
| per_shard = intermediate_size // parallel_size | ||
| quant_block = int(quant_block) | ||
| if quant_block % per_shard != 0: | ||
| logger.warning( | ||
| f"Invalid mapping {tag}={parallel_size}: (intermediate_size // {tag})={per_shard} does not divide quantization block {quant_block}" | ||
| ) | ||
| continue | ||
|
|
||
| verified.append(m) | ||
|
|
||
| return verified | ||
|
|
||
|
|
||
| def apply_parallel_mapping_to_config( | ||
| base_config: dict, | ||
| mapping: ParallelizationMapping, | ||
| phase: str, | ||
| config_modifier, | ||
| num_gpus_per_node: int | None, | ||
| ) -> dict: | ||
| cfg = base_config | ||
hhzhang16 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if mapping.tp is not None: | ||
| cfg = config_modifier.set_config_tp_size(cfg, mapping.tp) | ||
| elif phase == "prefill" and mapping.tep is not None: | ||
hhzhang16 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cfg = config_modifier.set_config_tep_size(cfg, mapping.tep, num_gpus_per_node) | ||
| elif phase == "decode" and mapping.dep is not None: | ||
| cfg = config_modifier.set_config_dep_size(cfg, mapping.dep, num_gpus_per_node) | ||
| else: | ||
| pass | ||
| return cfg | ||
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,84 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # 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. | ||
|
|
||
| from typing import Literal, Protocol | ||
|
|
||
| from dynamo.planner.defaults import SubComponentType | ||
|
|
||
|
|
||
| class ConfigModifierProtocol(Protocol): | ||
| @classmethod | ||
| def convert_config( | ||
| cls, | ||
| config: dict, | ||
| target: Literal["prefill", "decode"], | ||
hhzhang16 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| is_moe_model: bool = False, | ||
| ) -> dict: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def set_config_tp_size( | ||
| cls, | ||
| config: dict, | ||
| tp_size: int, | ||
| component_type: SubComponentType = SubComponentType.DECODE, | ||
| ) -> dict: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def set_config_tep_size( | ||
| cls, | ||
| config: dict, | ||
| tep_size: int, | ||
| num_gpus_per_node: int, | ||
| component_type: SubComponentType = SubComponentType.DECODE, | ||
| ) -> dict: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def set_config_dep_size( | ||
| cls, | ||
| config: dict, | ||
| dep_size: int, | ||
| num_gpus_per_node: int, | ||
| component_type: SubComponentType = SubComponentType.DECODE, | ||
| ) -> dict: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def get_model_name(cls, config: dict) -> str: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def get_port(cls, config: dict) -> int: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def get_kv_cache_size_from_dynamo_log( | ||
| cls, dynamo_log_fn: str, attention_dp_size: int = 1 | ||
| ) -> int: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def load_default_config(cls) -> dict: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def update_model(cls, config: dict, model_name: str) -> dict: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def update_image(cls, config: dict, image: str) -> dict: | ||
| ... | ||
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
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.