o
    0 i*                     @   s  d dl Z d dl mZ d dlmZ d dl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 d dlmZ ded deedf dededed f
ddZdeded deedf dededeee
 eee
f f fddZded deedf ddfddZded deedf ddfddZded deedf ddfddZded deedf de fdd Z!dedefd!d"Z"dS )#    N)Sequence)chain)ndarray)Device)Stream)get_current_stream)_array)_chunk)_data_transfer)_index_arith)_modesargsz_array.DistributedArraykwargsdevchunk_ireturnz_data_transfer._PartialUpdatec           	      C   s   g }d}t | | D ]}|j| | j}|r|rd} n|}q|r$|S t | | D ]}t |j D ]}||j q5q+g S NTF)r   values_chunks_mapupdatesfrom_iterableflush_mode)	r   r   r   r   r   Zat_most_one_updateargZupdates_nowchunk r   p/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/cupyx/distributed/array/_elementwise.py_find_updates   s"   
r   streamc                    s@   fdd  fdd|D } fdd|  D }||fS )Nc                    s    | j    }|j |jS N)r   
wait_eventreadyarray)Zd_arrayr   )r   r   r   r   r   access_array7   s   z+_prepare_chunks_array.<locals>.access_arrayc                    s   g | ]} |qS r   r   .0r   r#   r   r   
<listcomp><   s    z)_prepare_chunks_array.<locals>.<listcomp>c                    s   i | ]	\}}| |qS r   r   )r%   keyr   r&   r   r   
<dictcomp>=   s    z)_prepare_chunks_array.<locals>.<dictcomp>)items)r   r   r   r   r   
arg_arrayskwarg_arraysr   )r#   r   r   r   r   _prepare_chunks_array1   s   r-   c                 C   s2   dd | D | d d < | dd | D  d S )Nc                 S   s   g | ]}| tjqS r   _to_op_moder   REPLICAr$   r   r   r   r'   E   s    z/_change_all_to_replica_mode.<locals>.<listcomp>c                 s   s$    | ]\}}|| tjfV  qd S r   r.   )r%   kr   r   r   r   	<genexpr>F   s    
z._change_all_to_replica_mode.<locals>.<genexpr>)updater*   )r   r   r   r   r   _change_all_to_replica_modeB   s   
r4   c              	   C   s  t |}t|| d }i }|p| D ]}|j} | D ]\}}g ||< t| t }	t|D ]\}
}t||||
}t	|	||||
\}}d }t
|| D ]}t|tjrg|d u s]J tj|j|j|}qO|d u r| |i |}|j}tj||	 |||fd}|| | |sq1d gt| }i }|D ]^\}}t|D ]\}}t|tjr|j||< q|| ||< q| D ]\}}t|tjr|j||< q|| ||< q|	|j | |i |}|j}|	 }tj||||fd}||| qq1W d    n	1 sw   Y  qt
| D ]}t|jttjfs tdqd  }}|p,| D ]}|j}|j} |d us=J t !|||t"j#|S )NZ
prevent_gc;Kernels returning other than single array are not supported)$listr4   r   	index_mapr*   r   r   	enumerater   r-   r   
isinstancer	   Z_ArrayPlaceholder_ChunkZcreate_placeholdershapedevicedtyperecordappendlenr"   r    r!   r
   Z
_AsyncDataZ
add_updater   r   RuntimeError_commsr   DistributedArrayr   r0   )kernelr   r   Z	out_dtypeout_chunks_mapr   r8   r   Zidxsr   r   idxr   r+   r,   	out_chunkdata	out_arrayZ
arg_slicesZkwarg_slicesr3   ir1   Zout_update_arrayr!   Z
out_updater   r<   commsr   r   r   _execute_kernelK   s   




<rM   c              
   C   s  t |dksJ t |dkrtd|rtdt|}t|D ]\}}|tj||< t|| j	
 D ]}|tj q6q |\}}t| tjjjro| j|j|jf}|du rktd| j d|jj|jjf |j}	nt| tjjjsyJ | |jj|jjfd\}
}	}
t |	dkrt|	 td	|	d
 }|j}|j}i }t|j	
 D ]}|jjj}| }t !|jj|}t|j	
 D ]i}t"#|j$|j$|}|du rq|jjj}tj%j&'||dkrt()|||j$|j|j}n	tjj*|j| |+|j, t"-|j$||}t"-|j$||}| j.dksJ | t/0t1|j| t/0t1|j| ||  qt2j3||4 |j$|j	d}|5|g 6| W d   n	1 sQw   Y  qt(7|||tj|S )z&Arguments must be in the replica mode.   zsElement-wise operation over more than two distributed arrays is not supported unless they share the same index_map.zLKeyword argument is not supported unless arguments share the same index_map.Nz#Could not guess the return type of z with arguments of type r      r6   r   r5   )8rA   rB   r7   r9   r/   r   r0   r   r   r   r   r   r:   cupyZ_coreZ_kernelZufuncZ_opsZ_guess_routine_from_in_typesr>   nametype	out_typesZElementwiseKernelZ_decide_params_typeprintr<   rC   r"   r=   idZon_ready_creation_basicemptyr   Z_index_intersectionindexcudaZruntimeZdeviceCanAccessPeerr   Z_make_chunk_asyncZ_check_peer_accessr    r!   Z_index_for_subindexZnintypingcastr   r	   r;   r?   
setdefaultr@   rD   )rE   r   r   rK   r   r   aboprS   _r>   r<   rL   rF   Za_chunkZa_devr   rJ   Zb_chunkintersectionZb_devZ	a_new_idxZ	b_new_idxrH   r   r   r   _execute_peer_access   s   





#rb   c                 C   s<   d }t | | D ]}|d u r|j}q	|j|kr dS q	dS r   )r   r   r8   )r   r   r8   r   r   r   r   _is_peer_access_needed  s   
rc   c                 C   sN   t || D ]}t|tjstdqt||}|r!t| ||S t| ||S )NzFMixing a distributed array with a non-distributed one is not supported)	r   r   r:   r   rD   rB   rc   rb   rM   )rE   r   r   r   Zneeds_peer_accessr   r   r   _execute  s   
rd   )#rZ   r   	itertoolsr   rP   Zcupy._creation.basicZ	_creationbasicrV   Zcupy._core.corer   Zcupy.cuda.devicer   Zcupy.cuda.streamr   r   Zcupyx.distributed.arrayr   r	   r
   r   r   dictstrintr7   r   tupler-   r4   rM   rb   boolrc   rd   r   r   r   r   <module>   s    





	

a

X

