@@ -491,6 +491,15 @@ def _get_tp_model_class(self):
491491 return self .model_tester .causal_lm_class
492492 return self .all_model_classes [0 ]
493493
494+ def _get_tp_config (self ):
495+ """Tiny config with `vocab_size` rounded up to a multiple of the world size, as sharded dims (typically `lm_head`) have to be split across ranks."""
496+ config = self .model_tester .get_config ()
497+ text_config = config .get_text_config ()
498+ remainder = text_config .vocab_size % self .tensor_parallel_size
499+ if remainder :
500+ text_config .vocab_size += self .tensor_parallel_size - remainder
501+ return config
502+
494503 def _skip_if_not_supported (self , expert_parallel : bool = False ):
495504 """Check and skip the test if tensor/expert parallel is not supported for this model/environment."""
496505 parallelism = "Expert" if expert_parallel else "Tensor"
@@ -539,7 +548,7 @@ def _skip_if_not_supported(self, expert_parallel: bool = False):
539548 def test_tp_forward (self ):
540549 self ._skip_if_not_supported ()
541550
542- config = self .model_tester . get_config ()
551+ config = self ._get_tp_config ()
543552 model_class = self ._get_tp_model_class ()
544553 atol = self .tensor_parallel_atol
545554 rtol = self .tensor_parallel_rtol
@@ -555,7 +564,7 @@ def test_tp_forward(self):
555564 def test_tp_backward (self ):
556565 self ._skip_if_not_supported ()
557566
558- config = self .model_tester . get_config ()
567+ config = self ._get_tp_config ()
559568 model_class = self ._get_tp_model_class ()
560569 atol = self .tensor_parallel_atol
561570 rtol = self .tensor_parallel_rtol
@@ -572,7 +581,7 @@ def test_tp_generation(self):
572581 # Test TP generation: unfused checkpoint → conversion mapping (if needed) → TP sharding → model → generate
573582 self ._skip_if_not_supported ()
574583
575- config = self .model_tester . get_config ()
584+ config = self ._get_tp_config ()
576585
577586 model_class = self ._get_tp_model_class ()
578587 atol = self .tensor_parallel_atol
@@ -594,7 +603,7 @@ def test_tp_generation_quantized(self):
594603 if not is_torchao_available ():
595604 self .skipTest ("Test requires torchao" )
596605
597- config = self .model_tester . get_config ()
606+ config = self ._get_tp_config ()
598607 model_class = self ._get_tp_model_class ()
599608 max_new_tokens = 25
600609
@@ -611,7 +620,7 @@ def test_tp_generation_quantized(self):
611620 def test_ep_forward (self ):
612621 self ._skip_if_not_supported (expert_parallel = True )
613622
614- config = self .model_tester . get_config ()
623+ config = self ._get_tp_config ()
615624 model_class = self ._get_tp_model_class ()
616625 atol = self .tensor_parallel_atol
617626 rtol = self .tensor_parallel_rtol
@@ -627,7 +636,7 @@ def test_ep_forward(self):
627636 def test_ep_backward (self ):
628637 self ._skip_if_not_supported (expert_parallel = True )
629638
630- config = self .model_tester . get_config ()
639+ config = self ._get_tp_config ()
631640 model_class = self ._get_tp_model_class ()
632641 atol = self .tensor_parallel_atol
633642 rtol = self .tensor_parallel_rtol
0 commit comments