2626
2727import paddle
2828from paddle import base
29- from paddle .base import core
3029
3130
3231def adamw_step (inputs , attributes ):
@@ -60,8 +59,8 @@ def adamw_step(inputs, attributes):
6059
6160 moment1_out = beta1 * moment1 + (1 - beta1 ) * grad
6261 moment2_out = beta2 * moment2 + (1 - beta2 ) * np .square (grad )
63- lr_t = lr * np .sqrt (1 - beta2_pow ) / ( 1 - beta1_pow )
64- param_out = param - lr_t * (moment1_out / (np . sqrt ( moment2_out ) + epsilon ))
62+ denom = ( np .sqrt (moment2_out ) / np . sqrt ( 1.0 - beta2_pow )) + epsilon
63+ param_out = param + (( moment1_out / denom ) * (- ( lr / (1.0 - beta1_pow )) ))
6564 return param_out , moment1_out , moment2_out
6665
6766
@@ -677,7 +676,7 @@ def _test_adamw_op_dygraph_place_amp_with_maingrad(
677676 param = paddle .randn (shape ).astype (paddle .bfloat16 )
678677 master_weight = param .astype (paddle .float32 )
679678 grad = paddle .randn (shape ).astype (paddle .bfloat16 )
680- main_grad = grad .astype (paddle .bfloat16 )
679+ main_grad = grad .astype (paddle .float32 )
681680 moment1 = paddle .randn (shape ).astype (paddle .float32 )
682681 moment2 = paddle .randn (shape ).astype (paddle .float32 ).abs ()
683682 lr = paddle .zeros ([1 ]).astype (paddle .float32 )
@@ -696,7 +695,7 @@ def _test_adamw_op_dygraph_place_amp_with_maingrad(
696695 # reference code
697696 _ , _ , _ , _ , _ , _ = paddle ._C_ops .adamw_ (
698697 ref_param ,
699- main_grad . astype ( paddle . float32 ) ,
698+ main_grad ,
700699 lr ,
701700 ref_moment_1 ,
702701 ref_moment_2 ,
@@ -780,9 +779,6 @@ def _get_places(self):
780779 return places
781780
782781 def test_main (self ):
783- xpu_version = core .get_xpu_device_version (0 )
784- if xpu_version != core .XPUVersion .XPU3 :
785- return
786782 for _ in range (1 ):
787783 shape = paddle .randint (1 , 1024 , [2 ])
788784 for place in self ._get_places ():
@@ -793,6 +789,61 @@ def test_main(self):
793789 )
794790
795791
792+ class TestAdamWOpMultiPrecison (unittest .TestCase ):
793+ def _test_adamw_op_dygraph_place_amp (self , place , use_amp = False ):
794+ paddle .disable_static ()
795+ paddle .seed (10 )
796+ paddle .set_device (place )
797+
798+ input = paddle .randn ((5 , 5 ))
799+
800+ model = paddle .nn .Linear (5 , 5 )
801+
802+ optimizer = paddle .optimizer .AdamW (
803+ parameters = [
804+ {
805+ 'params' : model .parameters (),
806+ 'weight_decay' : 0.001 ,
807+ 'beta1' : 0.1 ,
808+ 'beta2' : 0.99 ,
809+ }
810+ ],
811+ multi_precision = use_amp ,
812+ )
813+
814+ for idx in range (2 ):
815+ if place == 'xpu' and use_amp :
816+ model = paddle .amp .decorate (models = model , level = 'O2' )
817+ scaler = paddle .amp .GradScaler (init_loss_scaling = 1024 )
818+
819+ if place == 'xpu' and use_amp :
820+ with paddle .amp .auto_cast (level = 'O2' ):
821+ output = model (input )
822+ loss = paddle .mean (output )
823+ scaled = scaler .scale (loss )
824+ scaled .backward ()
825+ scaler .step (optimizer )
826+ optimizer .clear_grad ()
827+ else :
828+ output = model (input )
829+ loss = paddle .mean (output )
830+ loss .backward ()
831+ optimizer .step ()
832+ optimizer .clear_grad ()
833+
834+ def _get_places (self ):
835+ places = ['cpu' ]
836+ if paddle .is_compiled_with_xpu ():
837+ places .append ('xpu' )
838+ return places
839+
840+ def test_main (self ):
841+ for place in self ._get_places ():
842+ use_amp_list = [True , False ]
843+ for use_amp in use_amp_list :
844+ self ._test_adamw_op_dygraph_place_amp (place , use_amp )
845+
846+
796847support_types = get_xpu_op_support_types ('adamw' )
797848for stype in support_types :
798849 create_test_class (globals (), XPUTestAdamwOp1 , stype )
0 commit comments