@@ -449,32 +449,42 @@ def test_gemma3_text_generate(self):
449449
450450class Gemma3TextIntegrationTest (unittest .TestCase ):
451451 base_model_class = Gemma3TextModel
452+ test_dtype = "float32" # "bfloat16"
452453
453454 def test_inference_no_attention (self ):
454455 model = Gemma3TextModel .from_pretrained (
455- "PaddleFormers/tiny-random-gemma3" , download_hub = "aistudio" , convert_from_hf = True
456+ "PaddleFormers/tiny-random-gemma3" , download_hub = "aistudio" , convert_from_hf = True , dtype = self . test_dtype
456457 )
457458 model .eval ()
458459 input_ids = paddle .to_tensor ([[0 , 345 , 232 , 328 , 740 , 140 , 1695 , 69 , 6078 , 1588 , 2 ]])
459- attention_mask = paddle .to_tensor ([[0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]])
460460 with paddle .no_grad ():
461- output = model (input_ids , attention_mask = attention_mask )[0 ]
461+ output = model (input_ids )[0 ]
462462 expected_shape = [1 , 11 , 16 ]
463463 self .assertEqual (output .shape , expected_shape )
464- expected_slice = paddle .to_tensor (
464+ expected_slice_bf16 = paddle .to_tensor (
465465 [
466466 [
467- [- 1.26562500 , - 1.28125000 , 1.30468750 ],
468- [0.39257812 , - 0.23437500 , 0.94921875 ],
469- [0.84765625 , - 0.00598145 , 1.53125000 ],
467+ [- 1.24218750 , - 1.01562500 , 0.68750000 ],
468+ [0.32617188 , - 0.24609375 , 1.25000000 ],
469+ [1.10156250 , 0.29687500 , 0.88671875 ],
470470 ]
471471 ]
472472 )
473+ expected_slice_fp32 = paddle .to_tensor (
474+ [
475+ [
476+ [- 1.25233459 , - 1.01471460 , 0.69251710 ],
477+ [0.32604450 , - 0.25053313 , 1.26085544 ],
478+ [0.98726571 , 0.30734059 , 0.91449308 ],
479+ ]
480+ ]
481+ )
482+ expected_slice = expected_slice_fp32 if self .test_dtype == "float32" else expected_slice_bf16
473483 self .assertTrue (paddle .allclose (output [:, 1 :4 , 1 :4 ].cast (paddle .float32 ), expected_slice , atol = 1e-4 ))
474484
475485 def test_inference_with_attention (self ):
476486 model = Gemma3TextModel .from_pretrained (
477- "PaddleFormers/tiny-random-gemma3" , download_hub = "aistudio" , convert_from_hf = True
487+ "PaddleFormers/tiny-random-gemma3" , download_hub = "aistudio" , convert_from_hf = True , dtype = self . test_dtype
478488 )
479489 model .eval ()
480490 input_ids = paddle .to_tensor ([[0 , 345 , 232 , 328 , 740 , 140 , 1695 , 69 , 6078 , 1588 , 2 ]])
@@ -483,7 +493,7 @@ def test_inference_with_attention(self):
483493 output = model (input_ids , attention_mask = attention_mask )[0 ]
484494 expected_shape = [1 , 11 , 16 ]
485495 self .assertEqual (output .shape , expected_shape )
486- expected_slice = paddle .to_tensor (
496+ expected_slice_bf16 = paddle .to_tensor (
487497 [
488498 [
489499 [- 1.26562500 , - 1.28125000 , 1.30468750 ],
@@ -492,6 +502,16 @@ def test_inference_with_attention(self):
492502 ]
493503 ]
494504 )
505+ expected_slice_fp32 = paddle .to_tensor (
506+ [
507+ [
508+ [- 1.27054501 , - 1.26936519 , 1.29382658 ],
509+ [0.37663761 , - 0.25405365 , 0.95409876 ],
510+ [0.81471157 , - 0.01011910 , 1.53275037 ],
511+ ]
512+ ]
513+ )
514+ expected_slice = expected_slice_fp32 if self .test_dtype == "float32" else expected_slice_bf16
495515 self .assertTrue (paddle .allclose (output [:, 1 :4 , 1 :4 ].cast (paddle .float32 ), expected_slice , atol = 1e-4 ))
496516
497517
0 commit comments