r""" This class is almost same with the mindspore's AdamWeightDecay implements, the only difference is the optimizer's state will be always initialized with float32, where the original AdamWeightDecay will initialize the optimizer's state with float16, if the paramete
| 38 | |
| 39 | |
| 40 | class FP32StateAdamWeightDecay(AdamWeightDecay): |
| 41 | r""" |
| 42 | This class is almost same with the mindspore's AdamWeightDecay implements, the |
| 43 | only difference is the optimizer's state will be always initialized with float32, |
| 44 | where the original AdamWeightDecay will initialize the optimizer's state with float16, |
| 45 | if the parameters are initialized with fp16. |
| 46 | This setting will avoid overflow in training PanGu-Alpha model using fp16. |
| 47 | """ |
| 48 | |
| 49 | def __init__(self, params, learning_rate=1e-3, beta1=0.9, beta2=0.999, eps=1e-6, weight_decay=0.0): |
| 50 | super(FP32StateAdamWeightDecay, self).__init__(params, learning_rate=learning_rate, |
| 51 | beta1=beta1, |
| 52 | beta2=beta2, |
| 53 | eps=eps, |
| 54 | weight_decay=weight_decay) |
| 55 | |
| 56 | self.moments1 = self.clone_state(self.parameters, prefix='adam_m', init='zeros') |
| 57 | self.moments2 = self.clone_state(self.parameters, prefix='adam_v', init='zeros') |
| 58 | |
| 59 | def clone_state(self, parameter_tuple, prefix, init): |
| 60 | r""" |
| 61 | parameter_tuple: ParameterTuple. The parameters of the network |
| 62 | prefix: str. The prefix name of the parameters |
| 63 | init: str. The initialization method |
| 64 | """ |
| 65 | new = [] |
| 66 | for old_param in parameter_tuple: |
| 67 | new_state = Parameter(initializer(init, shape=old_param.shape, dtype=mstype.float32)) |
| 68 | new_state.param_info = old_param.param_info.clone() |
| 69 | new_state.is_init = False |
| 70 | new_state.set_data(initializer(init, shape=old_param.shape, dtype=mstype.float32)) |
| 71 | new_state.name = prefix + '.' + new_state.name |
| 72 | new.append(new_state) |
| 73 | return ParameterTuple(new) |
| 74 | |
| 75 | |
| 76 | get_square_sum = C.MultitypeFuncGraph("get_square_sum") |