Skip to content

Commit b0f4265

Browse files
authored
Fix TIPSv2 bfloat16 forward: text pooling returned float32 (#50175)
Fix TIPSv2 text pooling dtype in bfloat16
1 parent 8f548bb commit b0f4265

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎src/transformers/models/tipsv2/modeling_tipsv2.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ def forward(
919919
last_hidden_state = encoder_outputs.last_hidden_state
920920
last_hidden_state = self.final_layer_norm(last_hidden_state)
921921
if pooling_mask is not None:
922-
pooling_mask = pooling_mask.to(last_hidden_state.device)
922+
pooling_mask = pooling_mask.to(device=last_hidden_state.device, dtype=last_hidden_state.dtype)
923923
masked_hidden_state = last_hidden_state * pooling_mask[..., None]
924924
pooled_output = masked_hidden_state.sum(dim=1) / (
925925
pooling_mask.sum(dim=1, keepdim=True) + self.config.pooling_epsilon

‎src/transformers/models/tipsv2/modular_tipsv2.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def forward(
665665
last_hidden_state = encoder_outputs.last_hidden_state
666666
last_hidden_state = self.final_layer_norm(last_hidden_state)
667667
if pooling_mask is not None:
668-
pooling_mask = pooling_mask.to(last_hidden_state.device)
668+
pooling_mask = pooling_mask.to(device=last_hidden_state.device, dtype=last_hidden_state.dtype)
669669
masked_hidden_state = last_hidden_state * pooling_mask[..., None]
670670
pooled_output = masked_hidden_state.sum(dim=1) / (
671671
pooling_mask.sum(dim=1, keepdim=True) + self.config.pooling_epsilon

‎tests/models/tipsv2/test_modeling_tipsv2.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,17 @@ def test_model(self):
389389
config_and_inputs = self.model_tester.prepare_config_and_inputs()
390390
self.model_tester.create_and_check_model(*config_and_inputs)
391391

392+
def test_model_bfloat16(self):
393+
config, input_ids, attention_mask, pixel_values = self.model_tester.prepare_config_and_inputs()
394+
model = Tipsv2Model(config).to(torch_device, dtype=torch.bfloat16).eval()
395+
with torch.no_grad():
396+
result = model(
397+
input_ids=input_ids, pixel_values=pixel_values.to(torch.bfloat16), attention_mask=attention_mask
398+
)
399+
400+
self.assertEqual(result.text_embeds.dtype, torch.bfloat16)
401+
self.assertEqual(result.image_embeds.dtype, torch.bfloat16)
402+
392403
@unittest.skip(reason="Hidden states are tested in the individual TIPSv2 text and vision tower tests.")
393404
def test_hidden_states_output(self):
394405
pass

0 commit comments

Comments
 (0)