o
    pi                     @   s\   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dZ
		dd	d
ZdS )    N)_C_ops)
check_typecheck_variable_and_dtype)Variablein_dynamic_or_pir_mode)LayerHelperc           	      C   s   t  rt| |\} }| |fS td
i t }|jdd}t| dttfd | D ]}t	|dg dd q(| |d}| |d}|j
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float64Zuint16)XZScale)OutFoundInfinite)typeinputsoutputsN)r   )r   r   Zcheck_finite_and_unscale_r   localsZ"create_variable_for_type_inferencer   tuplelistr   	append_op)	r   scalenameZfloat_status	found_infhelperer   r    r   _/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/static/amp/amp_nn.pyr      s&   

r   Fc                 C   s  t  rt| |||||||||	
 | S t|dddgd t| dttfd | D ])}t|dg dd |jtj	tj
fv rF|jtjksEJ dq'|j|jksPJ dq'tdi t }| ||||d	}| |||d
}||||d}t|	try|	|d< n|	|d< |j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   ZPrevLossScalingZInGoodStepsZ
InBadSteps)r   ZLossScalingZOutGoodStepsZOutBadSteps)incr_every_n_stepsdecr_every_n_nan_or_inf
incr_ratio
decr_ratioZ
StopUpdatestop_update)r   r   r   attrsN)r"   )r   r   Zupdate_loss_scaling_r   r   r   r   r
   paddler   Zbfloat16r   r   r   
isinstancer   r   )r   r   r!   Znum_good_stepsZnum_bad_stepsr#   r$   r%   r&   r'   r   r   r   r   r   r(   r   r   r    r"   B   sv   '	

r"   )NN)FN)r)   r   Zpaddle.base.data_feederr   r   Zpaddle.base.frameworkr   r   Zpaddle.base.layer_helperr   r   r"   r   r   r   r    <module>   s   
3