
    ёi                     \    S SK r S SK Jr  S SKJrJr  S SKJrJr  S SKJ	r	  SS jr
  S	S jrg)
    N)_C_ops)
check_typecheck_variable_and_dtype)Variablein_dynamic_or_pir_mode)LayerHelperc                 2   [        5       (       a  [        R                  " X5      u  pX4$ [        S	0 [	        5       D6nUR                  SS9n[        U S[        [        4S5        U  H  n[        US/ SQS5        M     XS.nXS.nUR                  SXxS9  X4$ )
aM  
Check if input X contains all finite data, if yes, scale it by input Scale.

$$Out = X / scale$$

If any tensor in X contains Inf or Nan, the Out will generate a indicator.
FoundInfinite will be 1 (True), and Out will not be scaled. In this case, the data of
Out should not be used, and its data may not be deterministic.
Otherwise, FoundInfinite will be 0 (False).

Args:
    x(list|tuple): The input tensors of check_finite_and_unscale operator.
    scale: The scale of check_finite_and_unscale operator.
    float_status(Tensor): (Only used on NPU) The float status to check overflow.
check_finite_and_unscalebool)dtypexfloat16float32float64uint16)XScale)OutFoundInfinite)typeinputsoutputs)r
   )r   r   check_finite_and_unscale_r   locals"create_variable_for_type_inferencer   tuplelistr   	append_op)	r   scalenamefloat_status	found_infhelperer   r   s	            X/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/static/amp/amp_nn.pyr
   r
      s    " 77A|@vx@F999GIq#t}&@A 7&		
  %F4G
'   <    c                    [        5       (       a!  [        R                  " U UUUUUUUUU	5
        U $ [        USSS/S5        [	        U S[
        [        4S5        U  H  n[        US/ SQS5        UR                  [        R                  [        R                  4;   a'  UR                  [        R                  :X  d   S5       eMh  UR                  UR                  :X  a  M   S5       e   [        S0 [        5       D6nU UUUUS	.nU UUUS
.nUUUUS.n[        U	[        5      (       a  XS'   OXS'   UR!                  SXUS9  U $ )a  
Update loss scaling according to overall gradients. If all gradients is
finite after incr_every_n_steps, loss scaling will increase by incr_ratio.
Otherwise, loss scaling will decrease by decr_ratio after
decr_every_n_nan_or_inf steps and each step some gradients are infinite.

Args:
    x(list|tuple): The input tensors of update_loss_scaling operator.
    found_inf (Variable): A boolean variable indicates whether
                                 there is any infinite gradient.
    prev_loss_scaling (Variable): Previous loss scaling.
    num_good_steps (Variable): A variable accumulates good steps in which
                               all gradients are finite.
    num_bad_steps (Variable): A variable accumulates bad steps in which
                              some gradients are infinite.
    incr_every_n_steps (int): A variable represents increasing loss
                                   scaling every n consecutive steps with
                                   finite gradients.
    decr_every_n_nan_or_inf (int): A variable represents decreasing
                                        loss scaling every n accumulated
                                        steps with nan or inf gradients.
    incr_ratio(float): The multiplier to use when increasing the loss
                       scaling.
    decr_ratio(float): The less-than-one-multiplier to use when decreasing
                       loss scaling.
prev_loss_scalingr   r   update_loss_scalingr   r   z\The dtype of prev_loss_scaling should be float32 when the dtype of x is float16 or bfloat16.zAThe dtype of prev_loss_scaling should be equal to the dtype of x.)r   r   PrevLossScalingInGoodSteps
InBadSteps)r   LossScalingOutGoodStepsOutBadSteps)incr_every_n_stepsdecr_every_n_nan_or_inf
incr_ratio
decr_ratio
StopUpdatestop_update)r   r   r   attrs)r*   )r   r   update_loss_scaling_r   r   r   r   r   paddler   bfloat16r   r   r   
isinstancer   r   )r   r#   r)   num_good_stepsnum_bad_stepsr1   r2   r3   r4   r6   r!   r%   r$   r   r   r7   s                   r&   r*   r*   B   s   N ###	
 	I	 q#t}&;< 7!		
 77v~~v77$**fnn< n< %**agg5 S5   ;&(;F ",%#F (&$	G 1#:  	E +x((*|*m
"6%   Hr'   )NN)FN)r9   r   paddle.base.data_feederr   r   paddle.base.frameworkr   r   paddle.base.layer_helperr   r
   r*    r'   r&   <module>rB      s.      H 1&f 	nr'   