§
    �Štj#  ã                   ó®   — 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dS )
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                             ¦   «         sdS dd lm}  |                      ¦   «         r|                      ¦   «         ndS )Nr   )ÚtorchÚdistributedÚis_availableÚtorch.distributedÚis_initializedÚget_rank)Údists    úS/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/utils/debug_log.pyÚ	_get_rankr   !   sS   € ÝÔ×)Ò)Ñ+Ô+ð ØˆqØ$Ð$Ð$Ð$Ð$Ð$à"×1Ò1Ñ3Ô3Ð:ˆ4�=Š=‰?Œ?ˆ?¸Ð:ó    c                  ó   — dS )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	   € ð ˆ4r   c                  ó   — d S )Nr   r   s    r   Ú_debug_grad_log_faker   :   s   € àˆ4r   c                  ó®   — d                      d„ t          | ¦  «        D ¦   «         ¦  «        }t                               dt	          ¦   «         |¦  «         d S )Nú c              3   óv   K  — | ]4\  }}d |› d|                      ¦   «                              ¦   «         d›�V — Œ5dS )Útz_grad_norm=z.4fN)ÚnormÚitem)Ú.0ÚiÚgs      r   ú	<genexpr>z'_debug_grad_log_hook.<locals>.<genexpr>A   s[   è è € ð ð Ù48°A°qÐ/ˆAÐ/Ð/˜!Ÿ&š&™(œ(Ÿ-š-™/œ/Ð/Ð/Ð/ðð ð ð ð ð r   z[rank %d][bwd] %s)ÚjoinÚ	enumerateÚlogÚinfor   )ÚgradsÚnormss     r   Ú_debug_grad_log_hookr'   ?   s\   € à�HŠHð ð Ý<EÀeÑ<LÔ<Lðñ ô ñ ô €Eõ ‡H‚HÐ ¥)¡+¤+¨uÑ5Ô5Ð5Ð5Ð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Ð 2Ð 2Ð 2Ð 2Ð 2ð Ð
€à€gÔ˜Ñ!Ô!€ð;�3ð ;ð ;ð ;ð ;ð ðð ñ „ðð  Ôðð ñ Ôðð Ô(ð6ð 6ñ )Ô(ð6ð 6ð 6r   