Ë
    ÌÓ^j#  ã                   ó²   — d Z ddlZddlZddlmZ dgZ ej                  e«      Zde	fd„Z
ed„ «       Zej                  d„ «       Zej                  d	„ «       Zy)
aQ  Compile-safe backward gradient logging for multiple tensors.

``debug_grad_log`` logs gradient norms during backward for one or more tensors.
It is a leaf function with a ``register_multi_grad_hook`` that fires exactly
once when all requires_grad tensor inputs have their gradients computed.

Example::

    import torch
    from torch.utils.debug_log import debug_grad_log

    x = torch.randn(4, requires_grad=True)
    y = torch.randn(4, requires_grad=True)
    z = x * 2 + y * 3

    debug_grad_log(x, y)

    z.sum().backward()
    # Logs: [rank 0][bwd] t0_grad_norm=... t1_grad_norm=...
é    N)Úleaf_functionÚdebug_grad_logÚreturnc                  ó�   — t         j                  j                  «       sydd lm}  | j	                  «       r| j                  «       S dS )Nr   )ÚtorchÚdistributedÚis_availableÚtorch.distributedÚis_initializedÚget_rank)Údists    ú`/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/utils/debug_log.pyÚ	_get_rankr   !   s6   € Ü×Ñ×)Ñ)Ô+ØÝ$à"×1Ñ1Ô3ˆ4�=‰=‹?Ð:¸Ð:ó    c                   ó   — y)a©  Log gradient norms of multiple tensors during backward.

    This is a no-op in the forward pass. During backward, the hook fires
    exactly once when all requires_grad tensor inputs have their gradients
    computed, and logs ``[rank R][bwd] t0_grad_norm=... t1_grad_norm=...``.

    Args:
        *tensors: One or more tensors to monitor.

    Returns:
        None. Call without assignment: ``debug_grad_log(x, y)``.
    N© ©Útensorss    r   r   r   )   s   € ð r   c                   ó   — y )Nr   r   s    r   Ú_debug_grad_log_faker   :   s   € àr   c                  ó„   — dj                  d„ t        | «      D «       «      }t        j                  dt	        «       |«       y )Nú c              3   ól   K  — | ],  \  }}d |› d|j                  «       j                  «       d›�–— Œ. y­w)Útz_grad_norm=z.4fN)ÚnormÚitem)Ú.0ÚiÚgs      r   ú	<genexpr>z'_debug_grad_log_hook.<locals>.<genexpr>A   s5   è ø€ ò Ù48°A°qˆ!ˆAˆ3ˆk˜!Ÿ&™&›(Ÿ-™-›/¨#Ð.Ô/ñùs   ‚24z[rank %d][bwd] %s)ÚjoinÚ	enumerateÚlogÚinfor   )ÚgradsÚnormss     r   Ú_debug_grad_log_hookr'   ?   s7   € à�H‰Hñ Ü<EÀeÓ<Lôó €Eô ‡H�HÐ ¤)£+¨uÕ5r   )Ú__doc__Úloggingr   Útorch._dynamo.decoratorsr   Ú__all__Ú	getLoggerÚ__name__r#   Úintr   r   Úregister_faker   Úregister_multi_grad_hookr'   r   r   r   ú<module>r1      s…   ðñó* ã Ý 2ð Ð
€à€g×Ñ˜Ó!€ð;�3ó ;ð ñó ðð  ×Ññó ðð ×(Ñ(ñ6ó )ñ6r   