o
    X۷i                     @  s4  d dl mZ d dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlm  mZ d dlm  mZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ ejrbd dlmZ n	G dd dZ e Zej!G dd dZ"e#e"e#e$df f Z%ejrd%ddZ&d&d!d"Z'dS d%d#dZ&d&d$d"Z'dS )'    )annotationsN)Any)IterableIterator)ndarray)Device)Event)Stream)get_current_stream)nccl)_get_nccl_dtype_and_count)NcclCommunicatorc                   @  s   e Zd ZdS )_MockCommunicatorN)__name__
__module____qualname__ r   r   \/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/distributed/array/_data_transfer.pyr      s    r   c                   @  sD   e Zd ZU ded< ded< dZded< dd	d
ZejdddZdS )
_AsyncDatar   arrayr   readyNr   
prevent_gcreturnc                 C  sT   |   }| j }|| j t|| | jW  d    S 1 s#w   Y  d S N)on_readyr   copyrecordr   r   r   )selfstreamr   r   r   r   r   "   s
   

$z_AsyncData.copyIterator[Stream]c                 c  sH    | j j t }|| j |V  W d    d S 1 sw   Y  d S r   )r   devicer
   
wait_eventr   )r   r   r   r   r   r   )   s   
"z_AsyncData.on_ready)r   r   )r   r   )	r   r   r   __annotations__r   r   
contextlibcontextmanagerr   r   r   r   r   r      s   
 
r   .devicesIterable[int]r   dict[int, _Communicator]c                 C  s   t t| }dd |D S )Nc                 S  s   i | ]}|  |qS r   )	device_id).0commr   r   r   
<dictcomp>:   s    )_create_communicators.<locals>.<dictcomp>)_CommunicatorinitAlllist)r%   
comms_listr   r   r   _create_communicators6   s   r1   src_commr-   
src_streamr	   src_datadst_comm
dst_streamdst_devintc                 C  s  |j jj}||krt|j |jS t|}t|}zt| |  ||j t	
|j }	W d    n1 s9w   Y  t| |  t|	j|	j}
W d    n1 sYw   Y  t|	\}}t  t| | |	jj||| |j W d    n1 sw   Y  t|T ||
jj|||  |j t  t|
| |dW  d    W t| |  W d    n1 sw   Y  t| |  W d    S 1 sw   Y  S 1 sw   Y  W t| |  W d    n1 sw   Y  t| |  W d    d S 1 sw   Y  d S t| |  W d    n	1 s3w   Y  t| |  W d    w 1 sLw   Y  w N)r   )r   r    idr   r   r
   r   user!   _creation_from_dataascontiguousarray_creation_basicemptyshapedtyper   r   
groupStartsenddataptrrank_idrecvgroupEndr   )r2   r3   r4   r5   r6   r7   src_devprev_src_streamprev_dst_stream	src_arraydst_bufrA   countr   r   r   	_transfer<   sl   















$



 rO   c                 C  s   dd | D S )Nc                 S  s   i | ]}|t  qS r   )r-   )r)   devr   r   r   r+   i   s    r,   r   )r%   r   r   r   r1   f   s   c           	   	   C  s   |j jj}||krt|j |jS t|/ t }z%|  ||j |j 	 }t||
 |j dW |  W  d    S |  w 1 sFw   Y  d S r9   )r   r    r:   r   r   r   r
   r;   r!   r   r   )	r2   r3   r4   r5   r6   r7   rI   prev_stream	dst_arrayr   r   r   rO   k   s    




)r%   r&   r   r'   )r2   r-   r3   r	   r4   r   r5   r-   r6   r	   r7   r8   r   r   )(
__future__r   r#   dataclassestypingr   collections.abcr   r   cupy._core.corer   cupy._creation.from_data	_creation	from_datar<   cupy._creation.basicbasicr>   cupy.cuda.devicer   cupy.cuda.streamr   r	   r
   	cupy.cudar   cupyx.distributed._nccl_commr   	availablecupy.cuda.ncclr   r-   r   	dataclassr   tupleslice_PartialUpdater1   rO   r   r   r   r   <module>   s4    

*