o
    X۷iI                     @  s~   d dl mZ d dlZd dlmZ d1d	d
Zd2ddZd3ddZd4ddZd5d d!Zd6d#d$Z	d7d'd(Z
d8d*d+Zd9d/d0ZdS ):    )annotationsN)Anyaintbreturntuple[int, int]c                 C  sN   | |}}d\}}|r#|| }||||  }}||||  }}|s||fS )N)   r    )r   r   cdxurr
   r
   Z/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/distributed/array/_index_arith.py_extgcd   s   
r   a1n1a2n2tuple[int, int] | Nonec                 C  sz   t ||\}}||  | dkrd S |||  }| ||  | | ||  |  }||k r9||| d | d | 7 }||fS )Nr   r	   )r   )r   r   r   r   gm1nr   r
   r
   r   _crt   s    r   slicelengthslice | Nonec                 C  sd   |  |\}}}| |\}}}t||||}	|	d u rd S |	\}
}t||}|
|kr,d S t|
||S N)indicesr   minr   )r   r   r   a_starta_stopa_stepb_startb_stopb_step
crt_resultc_startc_stepc_stopr
   r
   r   _slice_intersection3   s   
r+   subc                 C  sT   |  |\}}}| |\}}}|| | }	|| d | d }
|| }t|	|
|S Nr	   )r   r   )r   r,   r   r!   r"   r#   	sub_startsub_stopsub_stepr(   r*   r)   r
   r
   r   _index_for_subsliceD   s   r1   a_idxtuple[slice, ...]b_idxshapetuple[int, ...]tuple[slice, ...] | Nonec                 C  sN   t | t |ks
J tdd t| ||D }d |v rd S tttdf |S )Nc                 s  "    | ]\}}}t |||V  qd S r   )r+   ).0r   r   r   r
   r
   r   	<genexpr>X       z&_index_intersection.<locals>.<genexpr>.)lentupleziptypingcastr   )r2   r4   r5   resultr
   r
   r   _index_intersectionR   s   
rB   sub_idxc                 C  s.   t | t |ks
J tdd t| ||D S )Nc                 s  r8   r   )r1   )r9   r   r,   r   r
   r
   r   r:   g   r;   z&_index_for_subindex.<locals>.<genexpr>)r<   r=   r>   )r2   rC   r5   r
   r
   r   _index_for_subindexa   s   
rD   outer_shapeidxc                 C  sR   t | }tt|D ]}|| || \}}}|| d | d ||< q
t|S r-   )listranger<   r   r=   )rE   rF   r5   istartstopstepr
   r
   r   _shape_after_indexingk   s
   rM   r   c              	   C  s>  t |ts|f}t| }t||krtd| dt| d|td f|t|   }g }t|D ]h}t || trd|| | | krTtd||  d| d| |  |t|| || d d q2t || tr|| | | \}}}|dkrt	d	||krt	d
| |t||| q2t	d| t|S )Nz%too many indices for array: array is z-dimensional, but z were indexedzIndex z is out of bounds for axis z with size r	   r   zSlice step must be positive.zThe index is empty on axis zInvalid index on axis )

isinstancer=   r<   
IndexErrorr   rH   r   appendr   
ValueError)r5   rF   ndimnew_idxrI   rJ   rK   rL   r
   r
   r   _normalize_indexv   sB   
 rT   	index_mapdict[int, Any]"dict[int, list[tuple[slice, ...]]]c                   sL   i }|  D ]\}}t|ts|g} fdd|D }|  |||< q|S )Nc                   s   g | ]}t  |qS r
   )rT   )r9   rF   r5   r
   r   
<listcomp>   s    z(_normalize_index_map.<locals>.<listcomp>)itemsrN   rG   sort)r5   rU   new_index_mapdevidxsr
   rX   r   _normalize_index_map   s   

r_   )r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r,   r   r   r   r   r   )r2   r3   r4   r3   r5   r6   r   r7   )r2   r3   rC   r3   r5   r6   r   r3   )rE   r6   rF   r3   r   r6   )r5   r6   rF   r   r   r3   )r5   r6   rU   rV   r   rW   )
__future__r   r?   r   r   r   r+   r1   rB   rD   rM   rT   r_   r
   r
   r
   r   <module>   s    








"