(
self,
config: TransformerConfig,
submodules: MambaStackSubmodules,
mamba_ssm_ngroups: int = 8,
residual_in_fp32=False,
pre_process: bool = True,
hybrid_attention_ratio: float = 0.0,
hybrid_mlp_ratio: float = 0.0,
hybrid_override_pattern: str = None,
post_layer_norm: bool = True,
post_process: bool = True,
device=None,
dtype=None,
)
| 72 | |
| 73 | class MambaStack(MegatronModule): |
| 74 | def __init__( |
| 75 | self, |
| 76 | config: TransformerConfig, |
| 77 | submodules: MambaStackSubmodules, |
| 78 | mamba_ssm_ngroups: int = 8, |
| 79 | residual_in_fp32=False, |
| 80 | pre_process: bool = True, |
| 81 | hybrid_attention_ratio: float = 0.0, |
| 82 | hybrid_mlp_ratio: float = 0.0, |
| 83 | hybrid_override_pattern: str = None, |
| 84 | post_layer_norm: bool = True, |
| 85 | post_process: bool = True, |
| 86 | device=None, |
| 87 | dtype=None, |
| 88 | ) -> None: |
| 89 | super().__init__(config=config) |
| 90 | self.residual_in_fp32 = residual_in_fp32 |
| 91 | self.pre_process = pre_process |
| 92 | self.post_layer_norm = post_layer_norm |
| 93 | self.post_process = post_process |
| 94 | |
| 95 | # Required for pipeline parallel schedules |
| 96 | self.input_tensor = None |
| 97 | |
| 98 | self.hybrid_attention_ratio = hybrid_attention_ratio |
| 99 | self.hybrid_mlp_ratio = hybrid_mlp_ratio |
| 100 | self.hybrid_override_pattern = hybrid_override_pattern |
| 101 | |
| 102 | layer_type_list = allocate_layers( |
| 103 | self.config.num_layers, |
| 104 | self.hybrid_attention_ratio, |
| 105 | self.hybrid_mlp_ratio, |
| 106 | self.hybrid_override_pattern, |
| 107 | ) |
| 108 | print("Mamba stack layers:", layer_type_list) |
| 109 | |
| 110 | pp_layer_offset = 0 |
| 111 | if parallel_state.get_pipeline_model_parallel_world_size() > 1: |
| 112 | pp_layer_offset, layer_type_list = self._select_layers_for_pipeline_parallel( |
| 113 | layer_type_list |
| 114 | ) |
| 115 | |
| 116 | self.layers = nn.ModuleList() |
| 117 | if self.config.megatron_hybrid_mamba_method: |
| 118 | for i, layer_type in enumerate(layer_type_list): |
| 119 | if layer_type == LayerSymbols.MAMBA: |
| 120 | layer = build_module( |
| 121 | submodules.mamba_layer, |
| 122 | config=self.config, |
| 123 | mamba_ssm_ngroups=mamba_ssm_ngroups, |
| 124 | residual_in_fp32=residual_in_fp32, |
| 125 | layer_number=i + 1 + pp_layer_offset, |
| 126 | ) |
| 127 | elif layer_type == LayerSymbols.ATTENTION: |
| 128 | # Transformer layers apply their own pp_layer_offset |
| 129 | layer = build_module( |
| 130 | submodules.attention_layer, config=self.config, layer_number=i + 1 |
| 131 | ) |
nothing calls this directly
no test coverage detected