
    ^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34==?::    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      48Aq!A3k!&&(--/#./s   24z[rank %d][bwd] %s)join	enumerateloginfor   )gradsnormss     r   _debug_grad_log_hookr'   ?   s7    HH <Ee<L E HH )+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   