1212import torch .nn as nn
1313
1414from deepspeed .module_inject .auto_tp import AutoTP , AutoTPConfig , PartitionType , TPLayerSpec
15- from deepspeed .module_inject .layers import LinearLayer
15+ from deepspeed .module_inject .layers import LinearLayer , LmHeadLinearAllreduce
1616
1717
1818class SubAttn (nn .Module ):
@@ -135,7 +135,7 @@ def test_nested_depth_correct():
135135 assert f"layers.{ layer_idx } .self_attn.o_proj" in matched_names
136136
137137
138- def _build_gathered_lm_head_autotp (model ):
138+ def _build_gathered_lm_head_autotp (model , mp_size = 1 ):
139139 config = AutoTPConfig (layer_specs = [
140140 TPLayerSpec (
141141 patterns = [r".*lm_head\.weight$" ],
@@ -152,7 +152,7 @@ def _build_gathered_lm_head_autotp(model):
152152 orig_layer_impl = None ,
153153 partition_config = config ,
154154 )
155- autotp .set_tensor_parallel_config (1 , None )
155+ autotp .set_tensor_parallel_config (mp_size , None )
156156 autotp .update_linear_policies ()
157157 return autotp
158158
@@ -176,5 +176,14 @@ def test_gathered_lm_head_falls_back_for_runtime_parameter_tie():
176176 assert model .lm_head .weight is model .embed_tokens .weight
177177
178178
179+ def test_gathered_lm_head_falls_back_to_legacy_allreduce_when_output_dim_is_uneven ():
180+ model = OutputModel (tied = False )
181+ model .lm_head = nn .Linear (32 , 101 , bias = False )
182+
183+ _build_gathered_lm_head_autotp (model , mp_size = 2 )._replace_module (model )
184+
185+ assert isinstance (model .lm_head , LmHeadLinearAllreduce )
186+
187+
179188if __name__ == "__main__" :
180189 pytest .main ([__file__ , "-v" ])
0 commit comments