
    Χg                        d dl Z d dlmZmZmZmZmZmZmZ d dl	m
Z
 d dlZd dlmZ d dlmZmZmZ g dZeej        eej                 f         Zd Ze	 	 	 dd
ededededee         dej        fd            Z e
de          	 	 	 dd
ededededee         dej        fd            Ze	 dd
ededee         ddfd            ZdS )    N)castDictIterableListOptionalTupleUnion)
deprecated)Tensor)_device_has_foreach_support"_group_tensors_by_device_and_dtype_has_foreach_support)clip_grad_norm_clip_grad_normclip_grad_value_c                 <      fd}t          j        |            |S )z
    This wrapper is needed to avoid a circular import when using @torch.no_grad on the exposed functions
    clip_grad_norm_ and clip_grad_value_ themselves.
    c                  n    t          j                    5   | i |cd d d            S # 1 swxY w Y   d S N)torchno_grad)argskwargsfuncs     T/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/nn/utils/clip_grad.py_no_grad_wrapperz"_no_grad.<locals>._no_grad_wrapper   s    ]__ 	) 	)4(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   *..)	functoolsupdate_wrapper)r   r   s   ` r   _no_gradr      s8    ) ) ) ) ) -t444           @F
parametersmax_norm	norm_typeerror_if_nonfiniteforeachreturnc                 8   t          | t          j                  r| g} d | D             }t          |          }t                    t	          |          dk    rt          j        d          S |d         j        t          |g          }g }|                                D ]\  \  }}	\  \  }
}	|t          |
|          s|r8t          |          r)|                    t          j        |
                     Y|rt          d|j         d          |                    fd|
D                        t          j                            t          j        fd|D                                 }|rLt          j        |                                |                                          rt          d	 d
          ||dz   z  }t          j        |d          }|                                D ]\  \  }}	\  \  }
}	|t          |
|          s|r8t          |          r)t          j        |
|                    |                     Y|rt          d|j         d          |                    |          }|
D ]}|                    |           |S )a  Clip the gradient norm of an iterable of parameters.

    The norm is computed over the norms of the individual gradients of all parameters,
    as if the norms of the individual gradients were concatenated into a single vector.
    Gradients are modified in-place.

    Args:
        parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a
            single Tensor that will have gradients normalized
        max_norm (float): max norm of the gradients
        norm_type (float): type of the used p-norm. Can be ``'inf'`` for
            infinity norm.
        error_if_nonfinite (bool): if True, an error is thrown if the total
            norm of the gradients from :attr:`parameters` is ``nan``,
            ``inf``, or ``-inf``. Default: False (will switch to True in the future)
        foreach (bool): use the faster foreach-based implementation.
            If ``None``, use the foreach implementation for CUDA and CPU native tensors and silently
            fall back to the slow implementation for other device types.
            Default: ``None``

    Returns:
        Total norm of the parameter gradients (viewed as a single vector).
    c                 *    g | ]}|j         	|j         S r   grad.0ps     r   
<listcomp>z#clip_grad_norm_.<locals>.<listcomp>E   !    >>>16+=QV+=+=+=r   r   g        N:foreach=True was passed, but can't use the foreach API on  tensorsc                 P    g | ]"}t           j                            |          #S  )r   linalgvector_norm)r,   gr#   s     r   r.   z#clip_grad_norm_.<locals>.<listcomp>\   s+    WWWQ%,221i@@WWWr   c                 :    g | ]}|                               S r3   )to)r,   normfirst_devices     r   r.   z#clip_grad_norm_.<locals>.<listcomp>_   s%    ===tTWW\**===r   zThe total norm of order z for gradients from `parameters` is non-finite, so it cannot be clipped. To disable this error and scale the gradients by the non-finite norm anyway, set `error_if_nonfinite=False`gư>g      ?)max)
isinstancer   r   floatlentensordevicer   itemsr   r   extend_foreach_normRuntimeErrortyper4   r5   stack
logical_orisnanisinfclamp_foreach_mul_r8   mul_)r!   r"   r#   r$   r%   gradsgrouped_gradsnormsr@   _device_grads
total_norm	clip_coefclip_coef_clampedclip_coef_clamped_devicer6   r:   s     `             @r   r   r   $   s   > *el++ " \
>>Z>>>EXHi  I
5zzQ|C   8?L 	+		 	  E,9,?,?,A,A 
Y 
Y((n|aO 4\6 J JO 3F;;  LL,\9EEFFFF 	YbV[bbb   LLWWWW,WWWXXXX))====u===>>	 J  
e.z/?/?/A/A:CSCSCUCUVV 
-y - - -
 
 	
 J-.I I3777,9,?,?,A,A 1 1((n|aO 4\6 J JO 3F;;  .?.B.B6.J.JKKKK 	1bV[bbb   (9';';F'C'C$! 1 1/00001 r   z_`torch.nn.utils.clip_grad_norm` is now deprecated in favor of `torch.nn.utils.clip_grad_norm_`.)categoryc                 (    t          | ||||          S )zClip the gradient norm of an iterable of parameters.

    .. warning::
        This method is now deprecated in favor of
        :func:`torch.nn.utils.clip_grad_norm_`.
    )r   )r!   r"   r#   r$   r%   s        r   r   r      s    $ :x<NPWXXXr   
clip_valuec                    t          | t          j                  r| g} t          |          }d | D             }t	          |g          }|                                D ]\  \  }}\  \  }}|/t          t          t          t                   |          |          s|rwt          |          rht          j
        t          t          t                   |          |            t          j        t          t          t                   |          |           |rt          d|j         d          |D ]-}t          t          |                              | |           .dS )a  Clip the gradients of an iterable of parameters at specified value.

    Gradients are modified in-place.

    Args:
        parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a
            single Tensor that will have gradients normalized
        clip_value (float): maximum allowed value of the gradients.
            The gradients are clipped in the range
            :math:`\left[\text{-clip\_value}, \text{clip\_value}\right]`
        foreach (bool): use the faster foreach-based implementation
            If ``None``, use the foreach implementation for CUDA and CPU native tensors and
            silently fall back to the slow implementation for other device types.
            Default: ``None``
    c                 *    g | ]}|j         	|j         S r   r)   r+   s     r   r.   z$clip_grad_value_.<locals>.<listcomp>   r/   r   N)r@   r0   r1   )minr;   )r<   r   r   r=   r   rA   r   r   r   r   _foreach_clamp_min__foreach_clamp_max_rD   rE   clamp_)r!   rX   r%   rM   rN   r@   rP   r*   s           r   r   r      s{   * *el++ " \
z""J>>Z>>>E6w??M%2%8%8%:%: K K!\guqO$T$v,%>%>vNNN  5f==  %d4<&?&?*MMM%d4<&?&?LLLL 	KbV[bbb    K KVT""))zkz)JJJJKK Kr   )r    FNr   )r   typingr   r   r   r   r   r   r	   typing_extensionsr
   r   r   torch.utils._foreach_utilsr   r   r   __all___tensor_or_tensorsr   r=   boolr   FutureWarningr   r   r3   r   r   <module>rf      sV       E E E E E E E E E E E E E E E E E E ( ( ( ( ( (                 D
C
C 5<%,)??@    
 $"W W"WW W 	W
 d^W \W W W 
Wt 4   $"Y Y"YY Y 	Y
 d^Y \Y Y Y 
Y  
 #(K (K"(K(K d^(K 
	(K (K (K 
(K (K (Kr   