|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Generalized Tensor Parallelism helpers for the standard Bridge runtime.""" |
| 16 | + |
| 17 | +from typing import Any |
| 18 | + |
| 19 | +import torch |
| 20 | +from megatron.core import parallel_state |
| 21 | +from megatron.core.process_groups_config import ProcessGroupCollection |
| 22 | + |
| 23 | + |
| 24 | +def get_transformer_config(model_config: Any) -> Any: |
| 25 | + """Return the MCore transformer config nested in a Bridge model config.""" |
| 26 | + model_fields = getattr(type(model_config), "__dataclass_fields__", {}) |
| 27 | + if "transformer" in model_fields: |
| 28 | + return model_config.transformer |
| 29 | + return model_config |
| 30 | + |
| 31 | + |
| 32 | +def is_gtp_remat_active(model_config: Any) -> bool: |
| 33 | + """Return whether dense or expert GTP weight rematerialization is enabled.""" |
| 34 | + transformer_config = get_transformer_config(model_config) |
| 35 | + dense_size = getattr(transformer_config, "gtp_weight_remat_size", 1) |
| 36 | + expert_size = getattr(transformer_config, "expert_gtp_weight_remat_size", 1) |
| 37 | + return any(isinstance(size, int) and size > 1 for size in (dense_size, expert_size)) |
| 38 | + |
| 39 | + |
| 40 | +def configure_gtp_remat(model_config: Any) -> None: |
| 41 | + """Configure process-global GTP state before constructing model modules.""" |
| 42 | + if not is_gtp_remat_active(model_config): |
| 43 | + return |
| 44 | + |
| 45 | + transformer_config = get_transformer_config(model_config) |
| 46 | + from megatron.core.tensor_parallel import gtp_api |
| 47 | + |
| 48 | + if not gtp_api.HAVE_GTP: |
| 49 | + raise RuntimeError("GTP requires TransformerEngine >= 2.19.") |
| 50 | + |
| 51 | + gtp_api.configure_gtp_remat_from_recipe( |
| 52 | + fp4=transformer_config.fp4 is not None, |
| 53 | + fp8_recipe=transformer_config.fp8_recipe, |
| 54 | + fp8=transformer_config.fp8 is not None, |
| 55 | + calculate_per_token_loss=transformer_config.calculate_per_token_loss, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def classify_gtp_remat_chains(model: list[torch.nn.Module], model_config: Any) -> None: |
| 60 | + """Classify all model chunks after distributed wrapping and before first forward.""" |
| 61 | + if not is_gtp_remat_active(model_config): |
| 62 | + return |
| 63 | + |
| 64 | + transformer_config = get_transformer_config(model_config) |
| 65 | + from megatron.core.tensor_parallel import gtp_api |
| 66 | + |
| 67 | + gtp_api.classify_gtp_remat_chains( |
| 68 | + model, |
| 69 | + cuda_graph_modules=transformer_config.cuda_graph_modules, |
| 70 | + moe_shared_expert_overlap=transformer_config.moe_shared_expert_overlap, |
| 71 | + cuda_graph_impl=transformer_config.cuda_graph_impl, |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def get_data_distribution_group( |
| 76 | + pg_collection: ProcessGroupCollection, |
| 77 | + model_config: Any, |
| 78 | + *, |
| 79 | + with_context_parallel: bool = False, |
| 80 | +) -> torch.distributed.ProcessGroup: |
| 81 | + """Return the group spanning every rank that consumes distinct input data.""" |
| 82 | + if not is_gtp_remat_active(model_config): |
| 83 | + return pg_collection.dp_cp if with_context_parallel else pg_collection.dp |
| 84 | + if with_context_parallel: |
| 85 | + return pg_collection.dp_cp_gtp_remat |
| 86 | + return parallel_state.get_data_parallel_group(with_gtp_remat=True) |
0 commit comments