Ë
    Q/æjÿ  ã                   ó‚  — 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y)é    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                  óh   •— t        j                  «       5   ‰| i |¤Žcd d d «       S # 1 sw Y   y xY w©N)ÚtorchÚno_grad)ÚargsÚkwargsÚfuncs     €ú[/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/nn/utils/clip_grad.pyÚ_no_grad_wrapperz"_no_grad.<locals>._no_grad_wrapper   s.   ø€ Ü�]‰]‹_ñ 	)Ù˜Ð( Ñ(÷	)÷ 	)ò 	)ús   –(¨1)Ú	functoolsÚupdate_wrapper)r   r   s   ` r   Ú_no_gradr      s!   ø€ ô)ô ×ÑÐ-¨tÔ4ØÐó    Ú
parametersÚmax_normÚ	norm_typeÚerror_if_nonfiniteÚforeachÚreturnc           
      óv  — t        | t        j                  «      r| g} | D �cg c]  }|j                  €Œ|j                  ‘Œ }}t	        |«      }t	        |«      }t        |«      dk(  rt        j                  d«      S |d   j                  }t        |g«      }g }	|j                  «       D ]¦  \  \  }
}\  \  }}|€t        ||
«      s|r1t        |
«      r&|	j                  t        j                  ||«      «       ŒO|rt        d|
j                  › d�«      ‚|	j                  |D �cg c]"  }t        j                   j#                  ||«      ‘Œ$ c}«       Œ¨ t        j                   j#                  t        j$                  |	D �cg c]  }|j'                  |«      ‘Œ c}«      |«      }|rAt        j(                  |j+                  «       |j-                  «       «      rt        d|› d�«      ‚||dz   z  }t        j.                  |d¬	«      }|j                  «       D ]’  \  \  }
}\  \  }}|€t        ||
«      s|r1t        |
«      r&t        j0                  ||j'                  |
«      «       ŒO|rt        d|
j                  › d�«      ‚|j'                  |
«      }|D ]  }|j3                  |«       Œ Œ” |S c c}w c c}w c c}w )
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).
    r   g        ú:foreach=True was passed, but can't use the foreach API on ú tensorsz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   ÚgradÚfloatÚlenÚtensorÚdevicer   Úitemsr   r   ÚextendÚ_foreach_normÚRuntimeErrorÚtypeÚlinalgÚvector_normÚstackÚtoÚ
logical_orÚisnanÚisinfÚclampÚ_foreach_mul_Úmul_)r    r!   r"   r#   r$   ÚpÚgradsÚfirst_deviceÚgrouped_gradsÚnormsr/   Ú_Údevice_gradsÚgÚnormÚ
total_normÚ	clip_coefÚclip_coef_clampedÚclip_coef_clamped_devices                      r   r   r   $   s�  € ô> �*œeŸl™lÔ+Ø �\ˆ
Ø'Ö>˜¨1¯6©6Ñ+=ˆQ�V‹VÐ>€EÐ>Ü�X‹€HÜ�iÓ €IÜ
ˆ5ƒz�Q‚Ü�|‰|˜CÓ Ð Ø˜‘8—?‘?€Lô 	+Ø	ˆó	ð ð €EØ,9×,?Ñ,?Ó,Aò 
YÑ(‰ˆ�Ñ(‘n�| aØˆOÔ 4°\À6Ô JÙÔ3°FÔ;à�L‰Lœ×,Ñ,¨\¸9ÓEÕFÙÜØLÈVÏ[É[ÈMÐYaÐbóð ð �L‰LÈ,ÖWÀQœ%Ÿ,™,×2Ñ2°1°iÕ@ÒWÕXð
Yô —‘×)Ñ)Ü�‰°uÖ=¨t�T—W‘W˜\Õ*Ò=Ó>À	ó€Jñ œe×.Ñ.¨z×/?Ñ/?Ó/AÀ:×CSÑCSÓCUÔVÜØ& y kð 2-ð -ó
ð 	
ð ˜J¨Ñ-Ñ.€Iô Ÿ™ I°3Ô7ÐØ,9×,?Ñ,?Ó,Aò 1Ñ(‰ˆ�Ñ(‘n�| aØˆOÔ 4°\À6Ô JÙÔ3°FÔ;ä×Ñ Ð.?×.BÑ.BÀ6Ó.JÕKÙÜØLÈVÏ[É[ÈMÐYaÐbóð ð (9×';Ñ';¸FÓ'CÐ$Ø!ò 1�Ø—‘Ð/Õ0ñ1ð1ð Ðùòo ?ùò. Xùò >s   ¢J,´J,Ä#'J1
Å?J6z_`torch.nn.utils.clip_grad_norm` is now deprecated in favor of `torch.nn.utils.clip_grad_norm_`.)Úcategoryc                 ó    — t        | ||||«      S )z­Clip 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°Ð<NÐPWÓXÐXr   Ú
clip_valuec                 óœ  — t        | t        j                  «      r| g} t        |«      }| D �cg c]  }|j                  €Œ|j                  ‘Œ }}t        |g«      }|j                  «       D ]Ú  \  \  }}\  \  }}|€"t        t        t        t           |«      |¬«      s|rct        |«      rXt        j                  t        t        t           |«      | «       t        j                  t        t        t           |«      |«       Œ—|rt        d|j                  › d�«      ‚|D ]$  }t        t        |«      j                  | |¬«       Œ& ŒÜ yc c}w )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``
    N)r/   r'   r(   )Úminr)   )r*   r   r   r,   r+   r   r0   r   r   r   r   Ú_foreach_clamp_min_Ú_foreach_clamp_max_r3   r4   Úclamp_)	r    rN   r$   r?   r@   rB   r/   rD   r+   s	            r   r   r   ”   s*  € ô* �*œeŸl™lÔ+Ø �\ˆ
Ü�zÓ"€Jà'Ö>˜¨1¯6©6Ñ+=ˆQ�V‹VÐ>€EÐ>Ü6¸°wÓ?€Mà%2×%8Ñ%8Ó%:ò KÑ!‰ˆ�‘\‘g�u˜qàˆOÜ$¤T¬$¬v©,¸Ó%>ÀvÕNÙÔ5°fÔ=Ü×%Ñ%¤d¬4´©<¸Ó&?À*ÀÔMÜ×%Ñ%¤d¬4´©<¸Ó&?ÀÕLÙÜØLÈVÏ[É[ÈMÐYaÐbóð ð ò K�Ü”V˜TÓ"×)Ñ)¨z¨k¸zÐ)ÕJñKñKùò ?s
   ­E	¿E	)g       @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   © r   r   ú<module>r\      su  ðó ß E× EÑ EÝ (ã Ý ÷ñ ò D€ð ˜5Ÿ<™<¨°%·,±,Ñ)?Ð?Ñ@Ð òð 
ð Ø$Ø"ñWØ"ðWàðWð ðWð ð	Wð
 �d‰^ðWð ‡\�\òWó 
ðWñt ð4àôð Ø$Ø"ñYØ"ðYàðYð ðYð ð	Yð
 �d‰^ðYð ‡\�\òYóð
Yð  
ð #ñ(KØ"ð(Kàð(Kð �d‰^ð(Kð 
ò	(Kó 
ñ(Kr   