o
    h~4                     @   sX  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlZd dlmZ d dlmZ d dlmZ g Zdeded	eeed
f eed
f f fddZde	ej deded	eeef fddZdeed
f deed
f d	eeed
f eeef f fddZedeeeZedejeZe
dedejde d	ee fddZ!e
dedejde d	ee fddZ!dd Z!dMdeded e d	dfd!d"Z"d#ejd$ej#d	dfd%d&Z$d#ejfd'd(Z%ed)Z&ed*eeee'eeeZ(e
d+eejge&f d,ejd	e&fd-d.Z)e
d+eejgef d,e(d	e(fd/d.Z)d0d. Z)deed
f de	eeef  dejde d	eeed
f eeeef d
f f f
d1d2Z*	dNd3ej+d4eej d5e	d6 fd7d8Z,	dMd9ej-d3ej+d:e.d;e.d<ee d=e d	dfd>d?Z/d3ej+d@eej d:e.d;e.d	df
dAdBZ0dCeeef dDedEed	dfdFdGZ1d#ejd	e fdHdIZ2dJeej- d	eej- fdKdLZ3dS )O    N)OrderedDict)	Container)AnyCallableOptionaloverloadTypeVar)nn)PackedSequenceargskwargsreturn.c                  O   sB   g }t | }| D ]\}}|| || q
t|t|fS )a  
    Turn argument list into separate key list and value list (unpack_kwargs does the opposite).

    Inspiration: https://github.com/facebookresearch/fairscale/blob/eeb6684/fairscale/internal/containers.py#L70
    Usage::

        kwarg_keys, flat_args = pack_kwargs(1, 2, a=3, b=4)
        assert kwarg_keys == ("a", "b")
        assert flat_args == (1, 2, 3, 4)
        args, kwargs = unpack_kwargs(kwarg_keys, flat_args)
        assert args == (1, 2)
        assert kwargs == {"a": 3, "b": 4}
    Returns:
        Tuple[Tuple[Any, ...], Tuple[str, ...]]: The first tuple element gives
        gives both positional args and kwarg values, where the positional args
        proceed kwarg values and kwarg values are ordered consistently with the
        kwarg keys. The second tuple element gives the kwarg keys.
        The second tuple element's length is at most the first tuple element's length.
    )listitemsappendtuple)r   r   
kwarg_keys	flat_argskv r   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/distributed/utils.py_pack_kwargs   s   
r   dtypec                    s>    du r||fS dt jdt jf fdd}t||t||fS )z
    Cast floating point tensors in ``args`` and ``kwargs`` to ``input_dtype``.

    This respects the existing ``requires_grad`` on the tensors.
    Nxr   c                    s"   t | r
| j kr| S |  S N)torchis_floating_pointr   to)r   r   r   r   cast_fn;   s   
z%_cast_forward_inputs.<locals>.cast_fn)r   Tensor_apply_to_tensors)r   r   r   r    r   r   r   _cast_forward_inputs.   s   
r#   r   r   c                 C   sv   t |t | ksJ dt | dt |  t |dkr | i fS | dt |  }tt|| t | d }||fS )zSee _pack_kwargs.ztoo many keys z vs. r   N)lendictzip)r   r   r   r   r   r   r   _unpack_kwargsC   s   r'   STinputstarget_device!use_side_stream_for_tensor_copiesc                 C      d S r   r   r*   r+   r,   r   r   r   _recursive_toU      r/   c                 C   r-   r   r   r.   r   r   r   r/   [   r0   c                    s*    fddz	| }W d|S dw )z-Recursively moves input to the target_device.c                    s  t  tjtfrt  tr jjn j}|kr fS s# fS tt|jd }|jdks3|d u r9 fS ddl	m
} |}||  }W d    n1 sXw   Y  |j5 | }|| t  try|j| nt |tjsJ || W d    |fS W d    |fS 1 sw   Y  |fS ddlm} | r fddtt  D S t  trt dkrttt  S t  trt dkrdd tt  D S t  trt dkr fddtt   D S  gS )	Ncpur   )_get_stream_is_namedtuplec                    s   g | ]}t  | qS r   type).0r   objr   r   
<listcomp>       z1_recursive_to.<locals>.to_map.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   r7   ir   r   r   r:      s    c                    s   g | ]}t  |qS r   r5   r<   r8   r   r   r:      r;   )
isinstancer   r!   r
   datadevicer   getattrr6   torch.nn.parallel._functionsr2   streamindexcurrent_streamwait_streamrecord_stream torch.nn.parallel.scatter_gatherr4   r&   mapr   r$   r   r%   r   )r9   r@   
device_modr2   rC   outputrE   r4   r+   to_mapr,   r8   r   rM   d   sL   



 z_recursive_to.<locals>.to_mapNr   )r*   r+   r,   resr   rL   r   r/   a   s   /
Tcondsraise_assertion_errorc                 C   s(   | st | t  |rt|dS dS )zwAlternate to ``assert`` when in the backward context to print the error message ``s`` since otherwise, it is swallowed.N)print	tracebackprint_stackAssertionError)rO   rP   rQ   r   r   r   	_p_assert   s   rV   tensorsizec                 C   s   t  A t jj s5|   | k}|s=|   }t|dkd |  	|  W d   dS W d   dS W d   dS 1 sHw   Y  dS )z
    Allocate storage for ``tensor`` with the given size.

    Returns:
        bool: ``True`` if this method allocated storage and ``False`` if the
        storage was already allocated.
    r   zCTensor storage should have been resized to be 0 but got PLACEHOLDErN)
r   no_graddistributed_functional_collectivesis_torchdynamo_compiling_typed_storage_sizenumelrV   _resize_)rW   rX   already_allocatedtensor_storage_sizer   r   r   _alloc_storage   s   
"rc   c              	   C   s   t  I t jj s=|   dk}|sEt|  dkd|   d|    d| j	  |  
d W d   dS W d   dS W d   dS 1 sPw   Y  dS )z
    Frees the underlying storage of ``tensor``.

    Returns:
        bool: ``True`` if the method freed the storage and ``False`` if the
        storage was already freed.
    r   zVFreeing a tensor's storage is unsafe when it is not the sole occupant
storage offset: z
storage size: z
tensor shape: N)r   rY   rZ   r[   r\   r]   r^   rV   storage_offsetshaper`   )rW   already_freedr   r   r   _free_storage   s(   


"rg   QRfn	containerc                 C   r-   r   r   rj   rk   r   r   r   r"      r0   r"   c                 C   r-   r   r   rl   r   r   r   r"      s   c                    s    fdd  |S )zFRecursively apply to all tensor in different kinds of container types.c                    s   ddl m} t| tjr| S t| dr0t|   fddt D }tj fi |S t| t	rJ| 
 }|  D ]
\}}|||< q=|S t| trV| j | S t| trffdd|  D S || ryfdd| D }t| | S t| tttfrt| fd	d| D S | S )
Nr   r3   __dataclass_fields__c                    s    i | ]}|j  t|j qS r   )namerA   )r7   f)applydcr   r   
<dictcomp>   s    z4_apply_to_tensors.<locals>.apply.<locals>.<dictcomp>c                    s   i | ]	\}}| |qS r   r   )r7   keyvaluerp   r   r   rr      s    c                 3       | ]} |V  qd S r   r   r7   elru   r   r   	<genexpr>       z3_apply_to_tensors.<locals>.apply.<locals>.<genexpr>c                 3   rv   r   r   rw   ru   r   r   ry      rz   )rH   r4   r>   r   r!   hasattrdataclassesreplacefieldsr   	__class__r   r
   r?   r%   r6   r   r   set)r   r4   changesodrs   rt   rN   rp   rj   )rq   r   rp      s2   





z _apply_to_tensors.<locals>.applyr   rl   r   r   r   r"      s   c                 C   s   | rt | ||ng }|rt |||ng }t|t|k r/|dd tt|t|  D  nt|t|k rI|dd tt|t| D  t|t|fS )Nc                 S   s   g | ]}d qS )r   r   r7   _r   r   r   r:         z_to_kwargs.<locals>.<listcomp>c                 S   s   g | ]}i qS r   r   r   r   r   r   r:     r   )r/   r$   extendranger   )r*   r   r+   r,   moved_inputsmoved_kwargsr   r   r   
_to_kwargs   s   &$r   process_grouptensorsloggerzdist.Loggerc                 C   s   t | ||S r   )dist_verify_params_across_processes)r   r   r   r   r   r   $_verify_param_shape_across_processes  s   r   modulebroadcast_bucket_sizesrcparams_and_buffers_to_ignorebroadcast_buffersc           
      C   sj   g }|   D ]\}}||vr||  q|r,|  D ]\}}	||vr+||	  qt|||| dS )ag  
    Sync ``module``'s parameters and buffers state.

    Syncs ``module``'s parameters and buffers state so that all ranks contain
    the same module state across all ranks. Note that this API assumes that all
    parameter shapes are consistent before running the synchronization. This can
    be checked with ``_verify_param_shape_across_processes``.
    N)named_parametersr   detachnamed_buffers_sync_params_and_buffers)
r   r   r   r   r   r   module_statesrn   parambufferr   r   r   _sync_module_states  s   r   r   c                 C   s$   t |dkrt| ||| dS dS )zfSynchronize ``module_states`` (list of tensors) across all processes by broadcasting them from rank 0.r   N)r$   r   _broadcast_coalesced)r   r   r   r   r   r   r   r   :  s
   r   
state_dict
old_prefix
new_prefixc                 C   sX   ||krt dt|  D ]}||sq||t|d  }| | | |< | |= qdS )a  
    Replace all keys that match a given old_prefix with a new_prefix (in-place).

    Usage::

        state_dict = {"layer.xyz": torch.tensor(1)}
        replace_by_prefix_(state_dict, "layer.", "module.layer.")
        assert state_dict == {"module.layer.xyz": torch.tensor(1)}
    z*old_prefix and new_prefix must be distinctN)
ValueErrorr   keys
startswithr$   )r   r   r   rs   new_keyr   r   r   _replace_by_prefixG  s   
r   c                 C   s   |    dkS )Nr   )untyped_storagedata_ptr)rW   r   r   r   _data_ptr_allocated_  s   r   modulesc                 C   s`   g }dd | D }| D ]"}d}|  D ]\}}||uo||v }|r%d} nq|r-|| q|S )z
    Returns the modules in ``modules`` that are root modules (i.e.
    parent-less) with respect to the set ``modules``. In other words, these
    are the modules in ``modules`` that are the not child of any other
    module in ``modules``.
    c                 S   s   i | ]	}|t | qS r   )r   r   )r7   r   r   r   r   rr   k  s    z%_get_root_modules.<locals>.<dictcomp>TF)r   r   )r   root_modulesmodule_to_modulescandidate_moduleis_root_moduler   _modulesis_child_moduler   r   r   _get_root_modulesc  s"   
r   )Tr   )4r|   rS   collectionsr   collections.abcr   typingr   r   r   r   r   r   torch.distributedrZ   r   r	   torch.nn.utils.rnnr
   __all__r   strr   r   r#   r%   r'   r   r(   r!   r)   r@   boolr/   rV   Sizerc   rg   rh   r   ri   r"   r   ProcessGroupr   Moduleintr   r   r   r   r   r   r   r   r   <module>   s   .




9	$#
"





"