o
    0 i                     @   s*  d dl Z d dlmZ d dlmZmZmZmZ d dl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 d dlmZ d dlmZ G dd dZG dd dZ ddde!e"df de#e"e$e  f de#e"ef de#e"ef ddfddZ%dS )    N)chain)AnyIteratorOptionalUnion)ndarray)Device)Event)Stream)get_current_stream)_modes)_index_arith)_data_transfer)_Communicatorc                   @   sv   e Zd ZU eedf ed< eed< deedf deddfddZdeedf dd fd	d
Zddde	j
defddZdS )_ArrayPlaceholder.shapedevicereturnNc                 C   s   || _ || _d S N)r   r   )selfr   r    r   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/cupyx/distributed/array/_chunk.py__init__   s   
z_ArrayPlaceholder.__init__	new_shapec                 C   s   t || jS r   )r   r   )r   r   r   r   r   reshape    s   z_ArrayPlaceholder.reshapemode_modes.Modedtypec                 C   sh   | j ' |tju rt| j|}n||}t| j||}t	|W  d    S 1 s-w   Y  d S r   )
r   r   REPLICA_creation_basicemptyr   identity_offull_manipulation_dimsZ
atleast_1d)r   r   r   datavaluer   r   r   
to_ndarray#   s   

$z_ArrayPlaceholder.to_ndarray)__name__
__module____qualname__tupleint__annotations__r   r   r   numpyr   r   r&   r   r   r   r   r      s   
 r   c                   @   s  e Zd ZU eeef ed< eed< ee	df ed< e
ej ed< dZeed< 		d%deeef dedee	df dee
ej  ded	dfd
dZe	d&deedf deeef dee	df dee
ej  d	d f
ddZejd	ee fddZdejdee	df d	dfddZd'ddZd(ddZdd dddeedf deeejf deeef d	dfdd Z dee	df deedf d	dfd!d"Z!d)d#d$Z"dS )*_Chunkarrayready.indexupdatesN
prevent_gcr$   r   c                 C   s.   || _ || _|| _|d ur|ng | _|| _d S r   )r/   r0   r1   r2   r3   )r   r$   r0   r1   r2   r3   r   r   r   r   9   s
   
z_Chunk.__init__r   r   c                 C   s`   t |tr	t|}t||}| t }W d    n1 sw   Y  |d u r)g }t||||S r   )
isinstancer+   r   r   r	   r.   )clsr   r   r1   r2   r$   r0   r   r   r   create_placeholderE   s   

z_Chunk.create_placeholderc                 c   sH    | j j t }|| j |V  W d    d S 1 sw   Y  d S r   )r/   r   r   
wait_eventr0   )r   streamr   r   r   on_readyV   s   
"z_Chunk.on_readyupdateidxc                 C   s   | j ||f d S r   )r2   append)r   r:   r;   r   r   r   
add_update]   s   z_Chunk.add_updatec                 C   sp   t | jtr| j}| j}n|  }| j }| }W d    n1 s%w   Y  t||| jt	| j
| jdS )N)r3   )r4   r/   r   r0   r9   copyrecordr.   r1   listr2   r3   )r   r$   r0   r8   r   r   r   r>   b   s   


z_Chunk.copyr   r   c                 C   s   t | jdkr	dS t| jtr | jd d jj}| j||| _|  @}| jD ]#\}}||j	 |t
ju r>|j| j|< q(|| j| |j| j|< q(|| j	 | j| jf| _g | _W d   dS 1 sgw   Y  dS )zApply all updates in-place.r   N)lenr2   r4   r/   r   r   r&   r9   r7   r0   r   r   funcr?   r3   )r   r   r   r8   Zupdate_datar;   r   r   r   flusho   s"   

"z_Chunk.flushtargetcommsstreamsc                 C   sJ  | }|}t |jdksJ t|jtsJ |jjj}|jjj}	|j}
|j}t	|
||}|d u r2d S t
|
||}t
|||}t|j| |j|j}|tjurX|jsX| }t|| || |||	 ||	 |	}||| |tjur|js|jj}| }|||j|< ||j W d    d S 1 sw   Y  d S d S d S )Nr   )rA   r2   r4   r/   r   r   idr1   r   _index_intersection_index_for_subindexr   
_AsyncDatar0   r3   r   r   Z
idempotentr>   Z	_transferr=   r   r9   r!   r?   )r   rD   r   r   rE   rF   	src_chunk	dst_chunkZsrc_devZdst_devZsrc_idxZdst_idxintersectionZsrc_new_idxZdst_new_idxZdata_to_transferr:   r   r8   r   r   r   apply_to   sJ   


"z_Chunk.apply_toc                 C   s   t | jtsJ t| j||}|d u rd S t| j||}|  }|| j|< || j	 W d    d S 1 s9w   Y  d S r   )
r4   r/   r   r   rH   r1   rI   r9   r?   r0   )r   r;   r   identityrM   Zself_new_idxr8   r   r   r   set_identity_on_intersection   s   

"z#_Chunk.set_identity_on_intersectionc                 C   sd   t | jtrd S |  }| jD ]	\}}|| j|< q|| j W d    d S 1 s+w   Y  d S r   )r4   r/   r   r9   r2   r?   r0   )r   rO   r8   _r;   r   r   r   #set_identity_on_overwritten_entries   s   
"z*_Chunk.set_identity_on_overwritten_entries)NNr   )r   r.   )r   r   r   N)r   N)#r'   r(   r)   r   r   r   r,   r	   r*   slicer@   r   Z_PartialUpdater3   r   r   r   classmethodr+   r   r6   
contextlibcontextmanagerr   r
   r9   rJ   r=   r>   rC   dictr   rN   rP   rR   r   r   r   r   r.   0   s   
 












0


r.   op_modez_modes._OpModer   .	chunk_maprE   rF   r   c           
   	   C   s   t t| }tt|D ]$}|| }||  t|d t|D ]}|| }	||	| ||| q#qtt|d ddD ]!}|| }|tj	 t|D ]}|| }	||	tj	||| qNq>d S )N   )
r@   r   from_iterablevaluesrangerA   rC   rN   r   r   )
rX   r   rY   rE   rF   Zchunks_listirK   jrL   r   r   r   _all_reduce_intersections   s$   
ra   )&rU   	itertoolsr   typingr   r   r   r   r-   Zcupy._core.corer   Zcupy._creation.basicZ	_creationbasicr   Zcupy._manipulation.dimsZ_manipulationdimsr#   Zcupy.cuda.devicer   Zcupy.cuda.streamr	   r
   r   Zcupyx.distributed.arrayr   r   r   Z&cupyx.distributed.array._data_transferr   r   r.   r*   r+   rW   r@   ra   r   r   r   r   <module>   s>     


