§
    ­¸jð#  ã                  ó–   — d Z ddlmZ ddlZddlZddlmZ ddlZddlm	Z
 ddlmZ dd„Zd d
„Zd d„Zdd„Zdd„Zd!d„Zd"d#d„Zd$d%d„ZdS )&zIDistributed-training helpers (world-size, rank, all_gather, reduce_dict).é    )ÚannotationsN)ÚAny)ÚTensorÚreturnÚboolc                 óZ   — t          j        ¦   «         sdS t          j        ¦   «         sdS dS )zGReturn True if torch.distributed is available and has been initialised.FT)ÚdistÚis_availableÚis_initialized© ó    úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/utilities/distributed.pyÚis_dist_avail_and_initializedr      s3   € åÔÑÔð ØˆuÝÔÑ Ô ð ØˆuØˆ4r   Úintc                 óH   — t          ¦   «         sdS t          j        ¦   «         S )z@Return the number of processes in the current distributed group.é   )r   r	   Úget_world_sizer   r   r   r   r   #   s$   € å(Ñ*Ô*ð ØˆqÝÔÑ Ô Ð r   c                 óH   — t          ¦   «         sdS t          j        ¦   «         S )z@Return the rank of the current process in the distributed group.r   )r   r	   Úget_rankr   r   r   r   r   *   s!   € å(Ñ*Ô*ð ØˆqÝŒ=‰?Œ?Ðr   c                 ó&   — t          ¦   «         dk    S )zÐReturn True if the current process is rank 0.

    See Also:
        Before the process group is up, use :func:`_is_launcher_main_process`; :func:`get_rank` reports 0 in every
        process until then.
    r   )r   r   r   r   Úis_main_processr   1   s   € õ ‰:Œ:˜Š?Ðr   c                 óD  — ddl m}  | j        dk    o�t          j                             dd¦  «        dk    okt          j                             dd¦  «        dk    oGt          j                             dd¦  «        dk    o#t          j                             dd¦  «        dk    S )a§	  Return True if the process launcher designates this process to write files shared across a run.

    The counterpart to :func:`is_main_process` for code that runs *before* ``torch.distributed`` is initialized,
    where :func:`get_rank` reports 0 in every process and so cannot tell them apart. Prefer
    :func:`is_main_process` wherever the process group is already up: it reads the real global rank instead of
    inferring one from the environment.

    The answer is an environment-based heuristic. It starts from Lightning's own ``rank_zero_only.rank`` -- resolved
    once at import from ``RANK``, ``LOCAL_RANK``, ``SLURM_PROCID`` or ``JSM_NAMESPACE_RANK`` -- and adds checks that
    resolution does not make, each of which marks the process as not the main one when set to anything but ``"0"``:

    * ``NODE_RANK``, because the Lightning rank is node-local when it comes from ``LOCAL_RANK``, which Lightning's
      own subprocess launcher sets to 0 for the process it launches from on every node of a multi-node run, so
      without it one process per node would pass.
    * ``LOCAL_RANK``, because a ``RANK=0`` inherited from the parent environment wins Lightning's resolution in
      every subprocess child (the launcher overrides only ``LOCAL_RANK``), so without it every local rank would
      pass; no supported launcher gives global rank 0 a ``LOCAL_RANK`` other than ``"0"``.
    * ``OMPI_COMM_WORLD_RANK`` and ``PMI_RANK``, the global rank under ``mpirun``/PMI launchers. Lightning reads it
      only through ``mpi4py`` (its ``MPIEnvironment``), never at import, so without them every MPI worker would pass.

    The answer is per launch, not per training: because ``SLURM_PROCID`` feeds the Lightning rank, an ``srun`` sweep
    of independent ``devices=1`` trainings, one per task, sees ``SLURM_PROCID`` other than 0 on tasks 1..N-1 and the
    guard returns False there, so those tasks skip the pre-fit writes it protects. The post-fit rewrite guarded by
    :func:`is_main_process` is unaffected, as each such task has no process group and reports rank 0.

    Returns:
        Whether the launcher's environment identifies this process as rank 0 of node 0.

    Examples:
        The answer is read from the launcher environment this process was started in, so it is only meaningful
        against a known one; ``tests/utilities/test_distributed.py`` pins each case.

        >>> isinstance(_is_launcher_main_process(), bool)
        True
    r   ©Úrank_zero_onlyÚ	NODE_RANKÚ0Ú
LOCAL_RANKÚOMPI_COMM_WORLD_RANKÚPMI_RANK)Ú%pytorch_lightning.utilities.rank_zeror   ÚrankÚosÚenvironÚgetr   s    r   Ú_is_launcher_main_processr%   ;   s£   € ðV EÐDÐDÐDÐDÐDð 	Ô˜qÒ ð 	3ÝŒJ�NŠN˜;¨Ñ,Ô,°Ò3ð	3åŒJ�NŠN˜<¨Ñ-Ô-°Ò4ð	3õ ŒJ�NŠNÐ1°3Ñ7Ô7¸3Ò>ð	3õ ŒJ�NŠN˜: sÑ+Ô+¨sÒ2ðr   Úobjr   ÚfÚargsÚkwargsÚNonec                óR   — t          ¦   «         rt          j        | |g|¢R i |¤Ž dS dS )a<  Save *obj* to *f* only on the main process (rank 0).

    Args:
        obj: Object to save.
        f: File path or file-like object passed to ``torch.save``.
        *args: Additional positional arguments forwarded to ``torch.save``. **kwargs: Additional keyword arguments
        forwarded to ``torch.save``.
    N)r   ÚtorchÚsave)r&   r'   r(   r)   s       r   Úsave_on_masterr.   q   sD   € õ ÑÔð ,ÝŒ
�3˜Ð+˜DÐ+Ð+Ð+ FÐ+Ð+Ð+Ð+Ð+ð,ð ,r   ÚdataÚdeviceútorch.device | Noneú	list[Any]c                óN  ‡— t          ¦   «         }|dk    r| gS ‰€?t          ¦   «         rt          j        ¦   «         nd}t	          j        |dk    rdnd¦  «        Št          j        | ¦  «        }t	          j        t          |¦  «        t          j
        ‰¬¦  «        }|                     ¦   «         }t	          j        |g‰¬¦  «        }ˆfd„t          |¦  «        D ¦   «         }t          j        ||¦  «         d	„ |D ¦   «         }	t          |	¦  «        }
g }|	D ]7}|                     t	          j        |
ft          j
        ‰¬¦  «        ¦  «         Œ8||
k    r=t	          j        |
|z
  ft          j
        ‰¬
¦  «        }t	          j        ||fd¬¦  «        }t          j        ||¦  «         g }t%          |	|¦  «        D ]l\  }}|                     ¦   «                              ¦   «                              ¦   «         d|…         }|                     t          j        |¦  «        ¦  «         Œm|S )u&  Run all_gather on arbitrary picklable data (not necessarily tensors).

    Args:
        data: Any picklable object.
        device: Device for the intermediate byte tensors. If ``None``, derived from the process group
            backend (``cuda`` for NCCL, ``cpu`` otherwise). XLA callers must pass their local XLA device
            explicitly â€” XLA has no dedicated ``dist`` backend name to probe, and defaulting to CPU would
            place gather buffers on the wrong device.

    Returns:
        List of data gathered from each rank.
    r   NÚcpuÚncclÚcuda)Údtyper0   ©r0   c                ó>   •— g | ]}t          j        d g‰¬¦  «        ‘ŒS )r   r8   )r,   Útensor)Ú.0Ú_r0   s     €r   ú
<listcomp>zall_gather.<locals>.<listcomp>™   s*   ø€ ÐTÐTÐT¸Q�œ a S°Ð8Ñ8Ô8ÐTÐTÐTr   c                óP   — g | ]#}t          |                     ¦   «         ¦  «        ‘Œ$S r   )r   Úitem)r;   Úsizes     r   r=   zall_gather.<locals>.<listcomp>›   s(   € Ð?Ð?Ð? d•�T—Y’Y‘[”[Ñ!Ô!Ð?Ð?Ð?r   )r@   r7   r0   r   ©Údim)r   r   r	   Úget_backendr,   r0   ÚpickleÚdumpsr:   Ú	bytearrayÚuint8ÚnumelÚrangeÚ
all_gatherÚmaxÚappendÚemptyÚcatÚzipr4   ÚnumpyÚtobytesÚloads)r/   r0   Ú
world_sizeÚbackendÚbufferr:   Ú
local_sizeÚlocal_size_tensorÚsize_tensor_listÚ	size_listÚmax_sizeÚtensor_listr<   ÚpaddingÚ	data_listr@   s    `              r   rJ   rJ   ~   s  ø€ õ  Ñ!Ô!€JØ�Q‚€Øˆvˆð €~Ý(EÑ(GÔ(GÐR•$Ô"Ñ$Ô$Ð$ÈUˆÝ”¨°6Ò(9Ð(9˜f˜f¸uÑEÔEˆÝŒ\˜$ÑÔ€FÝŒ\�) FÑ+Ô+µ5´;ÀvÐNÑNÔN€Fð —’‘”€JÝœ j \¸&ÐAÑAÔAÐØTÐTÐTÐTÅ%È
ÑBSÔBSÐTÑTÔTÐÝ„OÐ$Ð&7Ñ8Ô8Ð8Ø?Ð?Ð.>Ð?Ñ?Ô?€IÝ�9‰~Œ~€Hð
 €KØð Wð WˆØ×Ò�5œ;¨ {½%¼+ÈfÐUÑUÔUÑVÔVÐVÐVØ�XÒÐÝ”+ H¨zÑ$9Ð#;Å5Ä;ÐW]Ð^Ñ^Ô^ˆÝ”˜F GÐ,°!Ð4Ñ4Ô4ˆÝ„O�K Ñ(Ô(Ð(à€IÝ˜I {Ñ3Ô3ð /ð /‰ˆˆfØ—’‘”×#Ò#Ñ%Ô%×-Ò-Ñ/Ô/°°°Ô6ˆØ×Ò�œ fÑ-Ô-Ñ.Ô.Ð.Ð.àÐr   TÚ
input_dictúdict[str, Tensor]Úaveragec                óÎ  — t          ¦   «         }|dk     r| S t          j        ¦   «         5  g }g }t          |                      ¦   «         ¦  «        D ]2}|                     |¦  «         |                     | |         ¦  «         Œ3t          j        |d¬¦  «        }t          j        |¦  «         |r||z  }d„ t          ||¦  «        D ¦   «         }ddd¦  «         n# 1 swxY w Y   |S )a7  Reduce values in *input_dict* across all processes.

    Args:
        input_dict: Dict whose values will be reduced.
        average: If True, compute the mean across ranks; otherwise compute the sum.

    Returns:
        Dict with the same keys as *input_dict*, with values averaged/summed across ranks.
    é   r   rA   c                ó   — i | ]\  }}||“Œ	S r   r   )r;   ÚkÚvs      r   ú
<dictcomp>zreduce_dict.<locals>.<dictcomp>É   s   € Ð<Ð<Ð<¡  A˜˜1Ð<Ð<Ð<r   N)
r   r,   Úno_gradÚsortedÚkeysrL   Ústackr	   Ú
all_reducerO   )r^   r`   rS   ÚnamesÚ
value_listrd   ÚvaluesÚreduced_dicts           r   Úreduce_dictrp   ±   s/  € õ  Ñ!Ô!€JØ�A‚~€~ØÐÝ	Œ‰Œð =ð =ØˆØˆ
å˜
ŸšÑ)Ô)Ñ*Ô*ð 	-ð 	-ˆAØ�LŠL˜‰OŒOˆOØ×Ò˜j¨œmÑ,Ô,Ð,Ð,Ý”˜Z¨QÐ/Ñ/Ô/ˆÝŒ˜ÑÔÐØð 	!Ø�jÑ ˆFØ<Ð<­¨U°FÑ);Ô);Ð<Ñ<Ô<ˆð=ð =ð =ñ =ô =ð =ð =ð =ð =ð =ð =øøøð =ð =ð =ð =ð Ðs   ªB$CÃCÃ!C)r   r   )r   r   )
r&   r   r'   r   r(   r   r)   r   r   r*   )N)r/   r   r0   r1   r   r2   )T)r^   r_   r`   r   r   r_   )Ú__doc__Ú
__future__r   r"   rD   Útypingr   r,   Útorch.distributedÚdistributedr	   r   r   r   r   r   r%   r.   rJ   rp   r   r   r   ú<module>rv      s'  ðð PÐ Oà "Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø €€€Ø Ð Ð Ð Ð Ð à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð ðð ð ð ð!ð !ð !ð !ðð ð ð ðð ð ð ð3ð 3ð 3ð 3ðl
,ð 
,ð 
,ð 
,ð0ð 0ð 0ð 0ð 0ðfð ð ð ð ð ð r   