-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[None][feat] Add SM-level disaggregation support #9020
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
base: main
Are you sure you want to change the base?
Changes from all commits
3a29fb7
2cf7cdf
731524a
b24849a
6e83433
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. 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 cuda.bindings import driver | ||
|
|
||
| from tensorrt_llm.runtime.generation import CUASSERT | ||
|
|
||
|
|
||
| def green_ctx_create_streams(res_list, device): | ||
| streams = [] | ||
| for res in res_list: | ||
| desc = CUASSERT(driver.cuDevResourceGenerateDesc([res], 1))[0] | ||
| green_ctx = CUASSERT( | ||
| driver.cuGreenCtxCreate( | ||
| desc, device, driver.CUgreenCtxCreate_flags.CU_GREEN_CTX_DEFAULT_STREAM | ||
| ) | ||
| )[0] | ||
| stream = CUASSERT( | ||
| driver.cuGreenCtxStreamCreate( | ||
| green_ctx, driver.CUstream_flags.CU_STREAM_NON_BLOCKING, 0 | ||
| ) | ||
| )[0] | ||
| stream = torch.cuda.get_stream_from_external(stream, device) | ||
| streams.append(stream) | ||
| return streams | ||
|
|
||
|
|
||
| def green_ctx_split_percent(sm_percent: float, device_id: int = 0): | ||
| device = CUASSERT(driver.cuDeviceGet(device_id))[0] | ||
|
|
||
| res = CUASSERT( | ||
| driver.cuDeviceGetDevResource(device, driver.CUdevResourceType.CU_DEV_RESOURCE_TYPE_SM) | ||
| )[0] | ||
| sm_count = res.sm.smCount | ||
|
|
||
| major = CUASSERT( | ||
| driver.cuDeviceGetAttribute( | ||
| driver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device | ||
| ) | ||
| )[0] | ||
| if major >= 9: | ||
| sm_min = 8 | ||
| sm_align = 8 | ||
| else: | ||
| sm_min = 4 if major == 8 else 2 | ||
| sm_align = 2 | ||
|
|
||
| def green_ctx_split_aligned(sm_g1): | ||
| sm_g1 = round(sm_g1 / sm_align) * sm_align | ||
| sm_g1 = min(max(sm_g1, sm_min), sm_count - sm_min) | ||
| result = CUASSERT( | ||
| driver.cuDevSmResourceSplitByCount( | ||
| 1, # nbGroups | ||
| res, | ||
| 0, # useFlags | ||
| sm_g1, | ||
| ) | ||
| ) | ||
| res_split = (result[0][0], result[2]) | ||
| streams = green_ctx_create_streams(res_split, device) | ||
| return streams, res_split | ||
|
|
||
| sm_g1 = round(sm_count * sm_percent) | ||
| sm_g2 = sm_count - sm_g1 | ||
| # Choose the split closer to sm_percent when sm_count is not divisible by sm_align | ||
| sm_g1_dist = min(sm_g1 % sm_align, sm_align - (sm_g1 % sm_align)) | ||
| sm_g2_dist = min(sm_g2 % sm_align, sm_align - (sm_g2 % sm_align)) | ||
| if sm_g1_dist <= sm_g2_dist: | ||
| (stream_g1, stream_g2), (res_g1, res_g2) = green_ctx_split_aligned(sm_g1) | ||
| else: | ||
| (stream_g2, stream_g1), (res_g2, res_g1) = green_ctx_split_aligned(sm_g2) | ||
| return (stream_g1, stream_g2), (res_g1, res_g2) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -191,7 +191,8 @@ def __init__(self, | |||||||||||||||||||||||
| sparse_attention_config: Optional["SparseAttentionConfig"], | ||||||||||||||||||||||||
| max_num_tokens: int, | ||||||||||||||||||||||||
| max_seq_len: Optional[int], | ||||||||||||||||||||||||
| lora_config: Optional[LoraConfig] = None): | ||||||||||||||||||||||||
| lora_config: Optional[LoraConfig] = None, | ||||||||||||||||||||||||
| weight_sharing_model: Optional[torch.nn.Module] = None): | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| Initializes the ModelLoader. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -210,6 +211,7 @@ def __init__(self, | |||||||||||||||||||||||
| self.max_num_tokens = max_num_tokens | ||||||||||||||||||||||||
| self.max_seq_len = max_seq_len | ||||||||||||||||||||||||
| self.lora_config = lora_config | ||||||||||||||||||||||||
| self.weight_sharing_model = weight_sharing_model | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def load( | ||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||
|
|
@@ -307,6 +309,12 @@ def init_meta_tensor(t: torch.Tensor): | |||||||||||||||||||||||
| moe_load_balancer.finalize_model() | ||||||||||||||||||||||||
| logger.info("moe_load_balancer finalize model done") | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if self.weight_sharing_model is not None: | ||||||||||||||||||||||||
| model.load_state_dict(self.weight_sharing_model.state_dict(), | ||||||||||||||||||||||||
| assign=True) | ||||||||||||||||||||||||
| # Free up duplicate model weights allocated before weight sharing | ||||||||||||||||||||||||
| torch.cuda.empty_cache() | ||||||||||||||||||||||||
|
Comment on lines
+312
to
+316
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. Keep shared weights on-device when assigning
- model.load_state_dict(self.weight_sharing_model.state_dict(),
- assign=True)
+ shared_state = self.weight_sharing_model.state_dict(
+ keep_vars=True)
+ model.load_state_dict(shared_state, assign=True)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| torch.cuda.current_stream().synchronize() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return model, moe_load_balancer | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
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.
🛠️ Refactor suggestion | 🟠 Major
Constructor additions look good; assert config presence when ctx-phase is enabled.
Prevent AttributeError if is_sm_disagg_ctx_phase=True but sm_disagg_config is None.
spec_config: Optional["DecodingBaseConfig"] = None, is_sm_disagg_ctx_phase: bool = False, is_draft_model: bool = False, @@ ) = llm_args.get_runtime_sizes() - if is_sm_disagg_ctx_phase: - max_num_tokens = llm_args.sm_disagg_config.context_max_num_tokens - max_batch_size = llm_args.sm_disagg_config.context_max_batch_size + if is_sm_disagg_ctx_phase: + if llm_args.sm_disagg_config is None: + raise ValueError( + "is_sm_disagg_ctx_phase=True requires sm_disagg_config" + ) + max_num_tokens = llm_args.sm_disagg_config.context_max_num_tokens + max_batch_size = llm_args.sm_disagg_config.context_max_batch_size🤖 Prompt for AI Agents