o
    0jM                     @   s   d dl Z d dlmZmZ d dlZd dlZd dlmZ ddl	m
Z
 e Zdeeef fddZdd	d
Zdd Ze ddededefddZe ddefddZdS )    N)AnyUnion   )device_guardsizec                 C   s$  t | tr| S |  drt| dd d S |  dr)t| dd d S |  dr:t| dd d S |  d	rVt| dd
 d }| drT|d S |S |  drrt| dd
 d }| drp|d S |S |  drt| dd
 d }| dr|d S |S td)z
    Converts a size expressed as a string with digits an unit (like `"5MB"`) to an integer (in bytes).
    Args:
        size (`int` or `str`): The size to convert. Will be directly returned if an `int`.
    ZGIBNi   @ZMIBi   ZKIBi   GBi ʚ;b   MBi@B ZKBi  zR`size` is not in a valid format. Use an integer followed by the unit, e.g., '5GB'.)
isinstanceintupperendswith
ValueError)r   Zint_size r   y/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/common/transformers/distributed.pyconvert_file_size_to_int   s(   
r   32MiBc                 c   s    | j tjkrt| j}n	tt|  }t	|}| 
dg |t| j  }td||D ]}| |t|||  }||t||| ffV  q.d S )Nr   )dtypepaddleZint8npprodshaper   numelitemr   reshape_dtype_byte_sizerangemin)tensorbuffer_sizer   Z	send_sizexZpart_tensorr   r   r   reduce_tensor7   s   r%   c                 C   sN   | t jkrdS tdt| }|du rtd|  dt| d }|d S )zP
    Returns the size (in bytes) occupied by one parameter of type `dtype`.
    g      ?z[^\d](\d+)$Nz`dtype` is not a valid dtype: .r   r   )r   boolresearchstrr   r   groups)r   Z
bit_searchZbit_sizer   r   r   r   F   s   
r   Fr"   dstreturnc              	      s(  zt ttfrt fddD W S t tr, fdd D W S d } tjdk}|r^rHdd ttj	dD }nfddttj	dD }d	d |D }rj
}d
g tD ]J\}d }tjd krfddttj	dD }tjjjj|rj  n ddd |rtt|D ]}	||	 ||	    qqm| |rtd" g }
|D ]}t|}||}|
| q|
}W d    |W S 1 sw   Y  |W S tjjjj|rj  n ddd |W S  ty   tdw )Nc                 3   s    | ]
}t | V  qd S Ndistributed_gather.0tr,   groupoffloadr   r   	<genexpr>W   s    
z%distributed_gather.<locals>.<genexpr>c                    s    i | ]\}}|t | qS r   r/   r2   kvr4   r   r   
<dictcomp>[   s    z&distributed_gather.<locals>.<dictcomp>r5   c                 S   s   g | ]}g qS r   r   r2   _r   r   r   
<listcomp>d   s    z&distributed_gather.<locals>.<listcomp>c                       g | ]}t  qS r   r   Z
empty_liker=   r"   r   r   r?   h       c                 S   &   g | ]}t |jd kr|n|d qS r   Nlenr   r1   r   r   r   r?   l   s    r   c                    r@   r   rA   r=   slice_tensorr   r   r?   w   rC   TF)r,   r5   Zsync_opZuse_calc_streamcpu(Not currently using distributed training)r   tuplelisttypedictitemsdistributedZget_rankr    get_world_sizer   r   r%   r   ZcommunicationstreamZgatherZranksrG   appendrJ   numpyr   r   ZconcatenateZreshapeAssertionError)r"   r,   r5   r6   output_tensorsZis_dstorigin_shapeindexslice_output_tensorsiZnew_output_tensorsr$   r3   r   )r,   r5   r6   rI   r"   r   r0   S   s   




	





	r0   c           	   
      s  zt ttfrt fddD W S t tr) fdd D W S g }rPtd fddtt	 D }W d   n1 sJw   Y  nfd	dtt	 D }d
d |D }rֈj
}dg |D ]}|dg qrtD ]D\}fddtt	 D }tj| d t||D ]$\}}td | ||d |d < W d   n1 sw   Y  qq| |D ]}|| q|W S t| |W S  ty   tdw )a  nested all gather function with offload

    Args:
        tensor (Any): the desired tensor, list of tensor, dict of tensor to allgather.
        group (_type_, optional): the communication group. Defaults to None.
        offload (bool, optional): If True, we offload the received tensor to cpu/(numpy). Defaults to False.

    Raises:
        AssertionError: Unexpected errors.

    Returns:
        tensor list: list of all gathered tensors
    c                 3   s    | ]	}t | V  qd S r.   distributed_allgatherr1   r5   r6   r   r   r7      s    
z(distributed_allgather.<locals>.<genexpr>c                    s   i | ]\}}|t | qS r   r\   r8   r^   r   r   r;      s    z)distributed_allgather.<locals>.<dictcomp>rJ   c                    r@   r   rA   r=   rB   r   r   r?      rC   z)distributed_allgather.<locals>.<listcomp>Nc                    r@   r   rA   r=   rB   r   r   r?      rC   c                 S   rD   rE   rF   r1   r   r   r   r?      s   & r   c                    r@   r   rA   r=   rH   r   r   r?      rC   r<   r   r   rK   )r   rL   rM   rN   rO   rP   r   r    rQ   rR   r   r   r%   Z
all_gatherziprJ   rV   )	r"   r5   r6   rW   rX   r$   rY   rZ   yr   )r5   r6   rI   r"   r   r]      s\   







r]   )r   )r   NF)NF)r(   typingr   r   rU   r   r   Zpaddle.distributedrQ   utilsr   rR   Z
world_sizer   r*   r   r%   r   Zno_gradr0   r]   r   r   r   r   <module>   s   
N