
    {-j                     Z    d dl Z d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 d	dZ
	 	 d
dZdS )    N)_C_ops)
check_typecheck_variable_and_dtype)Variablein_dynamic_or_pir_mode)LayerHelperc                 l   t                      rt          j        | |          \  } }| |fS t          d	i t	                      }|                    d          }t          | dt          t          fd           | D ]}t          |dg dd           | |d}| |d}|
                    d||           | |fS )
ay  
    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.11/site-packages/paddle/static/amp/amp_nn.pyr
   r
      s    "  75AA9)|@@vxx@@F999GGIq#t}&@AAA 
 
 777&		
 	
 	
 	
 u%%F)44G
'     i<    Fc                 v   t                      rt          j        | |||||||||	
  
         | S t          |dddgd           t	          | dt
          t          fd           | D ]o}t          |dg dd           |j        t          j	        t          j
        fv r |j        t          j        k    s
J d            U|j        |j        k    s
J d            pt          di t                      }| ||||d	}| |||d
}||||d}t          |	t                    r|	|d<   n|	|d<   |                    d|||           | S )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77!		
 	
 	
 7v~v777$*fn<<<n =<<< %*ag555S 6555 ;;&((;;F ",%# F (&$	 G 1#:  	 E +x(( +*|*m
"67%     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 H H H H H H H        1 0 0 0 0 0& & & &f 	n n n n n nr'   