|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import torch.nn as nn |
| 8 | +from torchtitan.config.job_config import FaultTolerance as FTConfig |
| 9 | +from torchtitan.distributed.pipeline import generate_llm_fqn_per_model_part |
| 10 | + |
| 11 | + |
| 12 | +def module_split( |
| 13 | + model: nn.Module, |
| 14 | + module_fqns_per_model_fragment: list[list[str]], |
| 15 | +) -> list[nn.Module]: |
| 16 | + """ |
| 17 | + This API creates fragments based on specified module names for each fragment. |
| 18 | + This method updates the model in place. |
| 19 | +
|
| 20 | + Args: |
| 21 | + model: The complete model to be split |
| 22 | + module_fqns_per_model_fragment: List of lists, where each inner list contains the module names |
| 23 | + that should be included in that fragment. Module names should be |
| 24 | + dot-separated paths. Examples: |
| 25 | + - "tok_embeddings" for token embeddings |
| 26 | + - "layers.0", "layers.1" for specific transformer layers |
| 27 | + - "norm" for the final normalization layer |
| 28 | + - "output" for the output projection layer |
| 29 | +
|
| 30 | + Returns: |
| 31 | + List of model fragments |
| 32 | +
|
| 33 | + Example usage: |
| 34 | + module_fqns_per_model_fragment = [ |
| 35 | + ["tok_embeddings", "layers.0"], # fragment 0: embeddings + first layer |
| 36 | + ["layers.1", "layers.2"], # fragment 1: middle layers |
| 37 | + ["norm", "output"] # fragment 2: final norm + output |
| 38 | + ] |
| 39 | + """ |
| 40 | + |
| 41 | + def _build_fragment_from_modules( |
| 42 | + fragment_idx: int, module_names: list[str] |
| 43 | + ) -> nn.Module: |
| 44 | + fragment_model = nn.Module() |
| 45 | + # Create a set of modules to keep for faster lookup |
| 46 | + modules_to_keep = set(module_names) |
| 47 | + print(f"fragment {fragment_idx}: Modules to keep: {modules_to_keep}") |
| 48 | + for module_name, module_value in model.named_children(): |
| 49 | + # Handle layer-like structures (e.g., "layers.0", "layers.1") |
| 50 | + if isinstance(module_value, (nn.ModuleDict, nn.ModuleList)): |
| 51 | + layers_to_keep = { |
| 52 | + name.split(".", 1)[1] |
| 53 | + for name in modules_to_keep |
| 54 | + if name.startswith(f"{module_name}.") |
| 55 | + } |
| 56 | + |
| 57 | + if not layers_to_keep: |
| 58 | + continue |
| 59 | + |
| 60 | + # Keep only specified layers |
| 61 | + if isinstance(module_value, nn.ModuleDict): |
| 62 | + for layer_name in list(module_value.keys()): |
| 63 | + if layer_name in layers_to_keep: |
| 64 | + setattr( |
| 65 | + fragment_model, |
| 66 | + f"{module_name}.{layer_name}", |
| 67 | + module_value[layer_name], |
| 68 | + ) |
| 69 | + else: |
| 70 | + indices_to_keep = { |
| 71 | + int(idx) for idx in layers_to_keep if idx.isdigit() |
| 72 | + } |
| 73 | + new_layers = nn.ModuleList( |
| 74 | + [ |
| 75 | + layer |
| 76 | + for i, layer in enumerate(module_value) |
| 77 | + if i in indices_to_keep |
| 78 | + ] |
| 79 | + ) |
| 80 | + setattr(fragment_model, module_name, new_layers) |
| 81 | + |
| 82 | + continue |
| 83 | + |
| 84 | + # Handle simple module attributes (e.g., "linear", "norm") |
| 85 | + if module_name not in modules_to_keep: |
| 86 | + continue |
| 87 | + |
| 88 | + setattr(fragment_model, module_name, module_value) |
| 89 | + |
| 90 | + return fragment_model |
| 91 | + |
| 92 | + num_fragments = len(module_fqns_per_model_fragment) |
| 93 | + model_fragments = [] |
| 94 | + |
| 95 | + for fragment_idx in range(num_fragments): |
| 96 | + module_names = module_fqns_per_model_fragment[fragment_idx] |
| 97 | + model_fragment = _build_fragment_from_modules( |
| 98 | + fragment_idx, |
| 99 | + module_names, |
| 100 | + ) |
| 101 | + print(f"building fragment_idx {fragment_idx} " f"with modules {module_names}") |
| 102 | + model_fragments.append(model_fragment) |
| 103 | + |
| 104 | + return model_fragments |
| 105 | + |
| 106 | + |
| 107 | +def fragment_llm( |
| 108 | + model: nn.Module, |
| 109 | + ft_config: FTConfig, |
| 110 | + n_layers: int, |
| 111 | +) -> list[nn.Module]: |
| 112 | + assert ft_config.num_fragments > 0 |
| 113 | + |
| 114 | + module_fqns_per_model_fragment = ft_config.module_fqns_per_model_fragment |
| 115 | + |
| 116 | + input_weight = 1 # Weight for tok_embeddings |
| 117 | + output_weight = 1 # Weight for norm + output layers |
| 118 | + |
| 119 | + if module_fqns_per_model_fragment == []: |
| 120 | + if ft_config.num_fragments == 1: |
| 121 | + return [model] |
| 122 | + |
| 123 | + module_fqns_per_model_fragment = generate_llm_fqn_per_model_part( |
| 124 | + ft_config.num_fragments, n_layers, input_weight, output_weight |
| 125 | + ) |
| 126 | + |
| 127 | + model_fragments = module_split(model, module_fqns_per_model_fragment) |
| 128 | + print(f"Created {len(model_fragments)} model fragments") |
| 129 | + |
| 130 | + return model_fragments |
0 commit comments