
    ^j              
           d Z ddlZddlmZ ddlZddlmZ defdZ	de
fdZde
fdZdefdZd	ed
edededdf
dZdedee   fdZddeeej&                  f   dedeeej&                  f   fdZy)zIDistributed-training helpers (world-size, rank, all_gather, reduce_dict).    N)Anyreturnc                  X    t        j                         syt        j                         syy)zGReturn True if torch.distributed is available and has been initialised.FT)distis_availableis_initialized     g/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/utilities/distributed.pyis_dist_avail_and_initializedr      s#     r
   c                  @    t               syt        j                         S )z@Return the number of processes in the current distributed group.   )r   r   get_world_sizer	   r
   r   r   r      s    (*  r
   c                  @    t               sy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    (*==?r
   c                      t               dk(  S )z-Return True if the current process is rank 0.r   )r   r	   r
   r   is_main_processr   -   s    :?r
   objfargskwargsc                 N    t               rt        j                  | |g|i | yy)a  Save *obj* to *f* only on the main process (rank 0).

    Safely saves objects, removing any callbacks that cannot be pickled.

    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   2   s'     

3+D+F+ r
   datac                 v   t               }|dk(  r| gS t        j                  t        j                  j	                         rdnd      }t        j                  |       }t        j                  t        |      t        j                  |      }|j                         }t        j                  |g|      }t        |      D cg c]  }t        j                  dg|       }}t        j                  ||       |D 	cg c]  }	t        |	j                                }}	t!        |      }
g }|D ]8  }|j#                  t        j$                  |
ft        j                  |             : ||
k7  rCt        j$                  |
|z
  ft        j                  |      }t        j&                  ||fd      }t        j                  ||       g }t)        ||      D ]X  \  }	}|j+                         j-                         j/                         d	|	 }|j#                  t        j0                  |             Z |S c c}w c c}	w )
zRun all_gather on arbitrary picklable data (not necessarily tensors).

    Args:
        data: Any picklable object.

    Returns:
        List of data gathered from each rank.
    r   cudacpu)dtypedevice)r!   r   )sizer    r!   dimN)r   r   r!   r   r   pickledumpstensor	bytearrayuint8numelranger   
all_gatherintitemmaxappendemptycatzipr   numpytobytesloads)r   
world_sizer!   bufferr'   
local_sizelocal_size_tensor_	size_listr"   max_sizetensor_listpadding	data_lists                 r   r,   r,   A   s     !JQv \\EJJ$;$;$=&5IF\\$F\\)F+5;;vNF Jj\&A;@;LMaqc&1MIMOOI01.78dTYY[!8I89~H
 K W5;;{%++fUVWX++Hz$9#;5;;W]^FG,!4OOK(II{3 /f##%--/6f-./ + N8s   H1< H6
input_dictaveragec                    t               }|dk  r| S t        j                         5  g }g }t        | j	                               D ]'  }|j                  |       |j                  | |          ) t        j                  |d      }t        j                  |       |r||z  }t        ||      D ci c]  \  }}||
 }}}ddd       |S c c}}w # 1 sw Y   S xY w)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   r#   N)
r   r   no_gradsortedkeysr0   stackr   
all_reducer3   )rA   rB   r7   namesvalueskvreduced_dicts           r   reduce_dictrO   n   s      !JA~	 =
)* 	)ALLOMM*Q-(	) V+j F),UF);<A1<<=  == s   B
C0C
=C
CC)T)__doc__r%   typingr   r   torch.distributeddistributedr   boolr   r-   r   r   r   r   listr,   dictstrTensorrO   r	   r
   r   <module>rY      s    P     t ! !#  
, , ,C ,3 ,4 ,*S *T#Y *ZDell!23 d dSVX]XdXdSdNe r
   