
    `ir2                     N   d dl Z d dlZd dlmZmZ d dlZd dlmZ d dlmZ d dlmZ e	e
e
e
f         Ze	eef         Zeeee
e
f         f         Zee	eee
f                  Ze	edf         Ze j         G d d                      Zd	ed
edefdZded	ed
edefdZde	edf         de	e
df         de	edf         fdZde	edf         de	edf         fdZde	e
df         dee
ee	edf                  f         deeef         fdZdddee	e
df         ge	e
df         f         dee	edf         ge	edf         f         ddfdZd'dZd'dZd'dZd'dZ	 d(ddd dd!ed         ddfd"Zd#ee
         d$ee
         d%eee e
                           dee
ee	edf                  f         fd&Z!dS ))    N)CallableOptional)_array)_chunk)_modes.c                   R    e Zd ZU ee         ed<   ee         ed<   ee         ed<   dS )	_Blockingi_partitionsj_partitionsk_partitionsN)__name__
__module____qualname__listint__annotations__     s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/distributed/array/_linalg.pyr	   r	      sD          s)s)s)r   r	   location_map_alocation_map_breturnc                    g }g }g }d }|                                  D ]\  }} |||            |||           |                                 D ]\  }} |||            |||           d }	 |	|          } |	|          } |	|          }d }
|                                  D ]\  }} |
||            |
||           |                                 D ]\  }} |
||            |
||           t          |||          S )Nc                     | \  }}}|dk    rt          d          |                    |           |                    |           d S )N   z"Step other than 1 is not supported)RuntimeErrorappend)indices
partitionsstartstopsteps        r   add_to_partitionsz)_find_blocking.<locals>.add_to_partitions6   sT    #tT199CDDD%   $r   c                     t          |           dk    rt          d          |                                  | d         g}t          | | dd                    D ] \  }}||k    r|                    |           !|S )Nr   zArray has no chunkr   )lenr   sortzipr   )r   resxys       r   to_unique_sortedz(_find_blocking.<locals>.to_unique_sortedG   s    z??a3444!}o
JqrrN33 	 	DAqAvv

1
r   c                     | \  }}}|                     |          dz   |                     |          k    rt          d          d S )Nr   zInconsistent index mapping)indexr   )r   r   r    r!   _s        r   check_indicesz%_find_blocking.<locals>.check_indicesX   sQ     tQE""Q&**:*:4*@*@@@;<<< A@r   )keysr	   )r   r   r
   r   r   r#   	i_indices	k_indices	j_indicesr+   r/   s              r   _find_blockingr4   -   s   
 !L L L      !/ 3 3 5 5 3 3	9)\222)\2222 . 3 3 5 5 3 3	9)\222)\2222   $#L11L##L11L##L11L= = =
 !/ 3 3 5 5 / /	9i...i.... . 3 3 5 5 / /	9i...i....\<>>>r   blockingc           	      *   | j         }| j        }| j        }g }t          ||dd                    D ]}t          ||dd                    D ]}t          ||dd                    D ]}	|dz   |	dz   f}
|	dz   |dz   f}t	          ||
                                                   }t	          ||                                                   }||z  }|r-|                                }|                    |
||f           t          d|
 d|           Ȍ|S )Nr   r   zAThere is no device that can perform multiplication between block z and )	r
   r   r   r'   setr0   popr   r   )r5   r   r   r
   r   r   plani_rangej_rangek_rangeblock_ablock_b	devices_a	devices_bintersectiondevs                   r   _make_execution_planrD   h   s{    (L(L(LD|\!""%566 C C<abb)9:: 	C 	CG|\!""-=>> C C"T>7T>:"T>7T>:w 7 < < > >??	w 7 < < > >??	(94 C '**,,CKK'3 78888&B*1B B8?B BC C CC	C& Kr   slicesshapec                     t          |           t          |          k    sJ t          d t          | |          D                       S )Nc              3   F   K   | ]\  }}|                     |          V  d S N)r   ).0slengths      r   	<genexpr>z%_convert_to_tuples.<locals>.<genexpr>   s2      GGyq&6""GGGGGGr   )r%   tupler'   )rE   rF   s     r   _convert_to_tuplesrO      sH     v;;#e**$$$$GGC4F4FGGGGGGr   tuplesc                 4    t          d | D                       S )Nc              3   (   K   | ]}t          | V  d S rI   slice)rJ   ts     r   rM   z%_convert_to_slices.<locals>.<genexpr>   s&      ++q++++++r   )rN   )rP   s    r   _convert_to_slicesrV      s!     ++F++++++r   	index_mapc                 F   i }|                                 D ]\  }}t          |          D ]t\  }}t          ||           }|d d         |dd          }	}t          j        t
          |	          }	|                    |i           }
|
                    |	i           }|||<   u|S )N)items	enumeraterO   typingcast	_BlockIdx
setdefault)rF   rW   location_mapsrC   idxschunk_iidx
idx_tuples	batch_idx	block_idxlocation_maplocations               r   _group_by_batchri      s     9;M__&& $ $	T%dOO 	$ 	$LGS+C77J#-crc?JrssOyIIy99I(33IrBBL#..y"==H#HSMM	$ r   arr_array.DistributedArrayf_shapef_idxc                    dt           j        dt           j        ffdi }| j                                        D ]\  }}fd|D             ||<    | j                  }t          j        || j        || j        | j	                  S )Nchunkr   c                     | j                              | j         j                            } | j                  }fd| j        D             }t          j        || j        ||| j                  S )Nc                 0    g | ]\  }}| |          fS r   r   )rJ   datarc   rm   s      r   
<listcomp>z>_reshape_array_with.<locals>.reshape_chunk.<locals>.<listcomp>   s*    EEE)$D%%**%EEEr   )	arrayreshaperF   r-   updatesr   _Chunkready
prevent_gc)ro   rr   r-   rv   rm   rl   s       r   reshape_chunkz*_reshape_array_with.<locals>.reshape_chunk   sz    {""775;+<#=#=>>ek""EEEEu}EEE}%+ugu/?A A 	Ar   c                 &    g | ]} |          S r   r   )rJ   ro   rz   s     r   rs   z'_reshape_array_with.<locals>.<listcomp>   s#    DDDE==//DDDr   )
r   rw   _chunks_maprZ   rF   r   DistributedArraydtype_mode_comms)rj   rl   rm   
chunks_maprC   chunksrF   rz   s    ``    @r   _reshape_array_withr      s    
AV] Av} A A A A A A A J,,.. E EVDDDDVDDD
3GCIE"sy*ci= = =r   c                 (    t          | d d           S )Nc                     d| z   S Nr7   r   rF   s    r   <lambda>z'_prepend_one_to_shape.<locals>.<lambda>   s
    dUl r   c                 (    t          d           f| z   S rI   rS   rc   s    r   r   z'_prepend_one_to_shape.<locals>.<lambda>   s    U4[[NS( r   r   rj   s    r   _prepend_one_to_shaper      #    ""((* * *r   c                 (    t          | d d           S )Nc                     | dz   S r   r   r   s    r   r   z&_append_one_to_shape.<locals>.<lambda>   s
    edl r   c                 (    | t          d           fz   S rI   rS   r   s    r   r   z&_append_one_to_shape.<locals>.<lambda>   s    C5;;.( r   r   r   s    r   _append_one_to_shaper      r   r   c                 N    | j         d         dk    sJ t          | d d           S )Nr   c                     | d d         S Nr   r   r   s    r   r   z!_pop_from_shape.<locals>.<lambda>   s    eCRCj r   c                     | d d         S r   r   r   s    r   r   z!_pop_from_shape.<locals>.<lambda>   s    CH r   rF   r   r   s    r   _pop_from_shaper      s:    9R=A    r   c                 N    | j         d         dk    sJ t          | d d           S )Nr   r   c                     | dd          S Nr   r   r   s    r   r   z'_pop_front_from_shape.<locals>.<lambda>   s    eABBi r   c                     | dd          S r   r   r   s    r   r   z'_pop_front_from_shape.<locals>.<lambda>   s    CG r   r   r   s    r   _pop_front_from_shaper      s:    9Q<1  r   aboutc           
         |t          d          dD ]}||v rt          d| d          t          | t          j                  rt          |t          j                  st          d          |                     t
          j                  } |                    t
          j                  }dx}}| j        dk    rd	}t          |           } |j        dk    rd	}t          |          }| j
        d
d         \  }}|j
        d
d         \  }	}
||	k    s | j
        dd
         |j
        dd
         k    rt          d          t          | j
        | j                  }t          |j
        |j                  }|                                |                                k    rt          d          d | j        D             }d}|                                D ]}||         }||         }t!          ||          }t#          |||          }t%          |          }|D ]c\  }}}||         }||         }| j        |         ||                  }|j        |         ||                  }|                    t
          j                   |                    t
          j                   |t+          |d          t+          |d          fz   }|                                5 }|                    |j                   t3          j        j        j        |j        |j        fi |}t=          j        ||                                 |||f          }||         !                    |           |j"        }ddd           n# 1 swxY w Y   e| j
        dd
         ||
fz   } t          j        | ||t
          j#        | j$                  }!|rtK          |!          }!|rtM          |!          }!|!S )ak  Matrix multiplication between distributed arrays.

    The arguments must have compatible :attr:`~DistributedArray.shape` and
    :attr:`~DistributedArray.index_map`.

    This operation converts its operands into the replica mode, and compute
    their product in the sum mode.

    Args:
        a, b: Input distributed arrays.
        out (optional): A location into which the result is stored. This option
            is currently not supported.
    Returns:
        The matrix product of the inputs.

    Example:
        >>> A = distributed_array(
        ...     cupy.arange(6).reshape(2, 3),
        ...     make_2d_index_map([0, 2], [0, 1, 3],
        ...                       [[{0}, {1, 2}]]))
        >>> B = distributed_array(
        ...     cupy.arange(12).reshape(3, 4),
        ...     make_2d_index_map([0, 1, 3], [0, 2, 4],
        ...                       [[{0}, {0}],
        ...                        [{1}, {2}]]))
        >>> C = A @ B
        >>> C.mode
        'sum'
        >>> C.all_chunks()
        {0: [array([[0, 0],
                    [0, 3]]),
             array([[0, 0],
                    [6, 9]])],
         1: [array([[20, 23],
                    [56, 65]])],
         2: [array([[26, 29],
                    [74, 83]])]}
        >>> C
        array([[20, 23, 26, 29],
               [56, 68, 80, 92]])

    .. seealso:: :obj:`numpy.matmul`
    NzArgument `out` is not supported)subokaxesaxisz
Argument `z` is not supportedzHMixing a distributed array with a non-distributed array is not supportedFr   TrY   zShapes are incompatiblezMismatched batch shapesc                     i | ]}|g S r   r   )rJ   rC   s     r   
<dictcomp>zmatmul.<locals>.<dictcomp>,  s    1O1O1Oc#r1O1O1Or   r   )ry   )'r   
isinstancer   r}   _to_op_moder   REPLICAndimr   r   rF   
ValueErrorri   rW   r0   devicesr4   rD   rV   r|   flushrT   on_ready
wait_eventrx   cupylinalg_productmatmulrt   r   rw   recordr   r~   SUMr   r   r   )"r   r   r   kwargsparamone_prependedone_appendednmm2plocation_maps_alocation_maps_br   r~   re   r   r   r5   r:   index_prefixr>   r?   rC   loc_aloc_bchunk_achunk_br-   streamchunk_ab_arraychunk_abrF   r(   s"                                     r   r   r      s0   ^ <===* G GF??EEEEEFFF q&122 a!899  	 	
fn%%A	fn%%A#((MLv{{!!$$v{{ ##7233<DAqGBCCLEBBww!'#2#,!'#2#,..2333%agq{;;O%agq{;;O!5!5!7!77745551O1OQY1O1O1OJE$))++ - -	(3(3!..AA#HnnMM))44%) 	- 	-!GWc"7+E"7+EmC(s4GmC(s4GMM&.)))MM&.))) E71:$6wqz8J#KKE!!## 
-v!!'-000!%!5!<M7="< "<4:"< "< "="FMMOOU '13 3 3 3&&x000&,
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
-	-* GCRCLAq6!E

!uj&*ah8 8C  )#C(( #c""Js   BM55M9<M9r
   r   r   c                    | d         dk    sJ t          t          |                     | k    sJ |d         dk    sJ t          t          |                    |k    sJ i }t          |          t          |           dz
  k    sJ t          t          |                    D ]}t          ||                   t          |          dz
  k    sJ t          t          ||                             D ]}| |         }| |dz            }||         }||dz            }	t	          ||          t	          ||	          f}
||         |         D ]+}|                    |g                               |
           ,|S )a  Create an ``index_map`` for a 2D matrix with a specified blocking.

    Args:
        i_partitions (list of ints): boundaries of blocks on the `i` axis
        j_partitions (list of ints): boundaries of blocks on the `j` axis
        devices (2D list of sets of ints): devices owning each block

    Returns:
        dict from int to array indices: index_map
            Indices for the chunks that devices with designated IDs are going
            to own.

    Example:
        >>> index_map = make_2d_index_map(
        ...     [0, 2, 4], [0, 3, 5],
        ...     [[{0}, {1}],
        ...      [{2}, {0, 1}]])
        >>> pprint(index_map)
        {0: [(slice(0, 2, None), slice(0, 3, None)),
             (slice(2, 4, None), slice(3, 5, None))],
         1: [(slice(0, 2, None), slice(3, 5, None)),
             (slice(2, 4, None), slice(3, 5, None))],
         2: [(slice(2, 4, None), slice(0, 3, None))]}
    r   r   )sortedr8   r%   rangerT   r_   r   )r
   r   r   rW   iji_starti_stopj_startj_stoprc   rC   s               r   make_2d_index_mapr   X  s   : ?a#l##$$4444?a#l##$$444446Iw<<3|,,q000003w<<   : :71:#l"3"3a"77777s71:'' 		: 		:A"1oG!!A#&F"1oG!!A#&F&))5&+A+ABCqz!} : :$$S"--44S9999:		: r   )r   rk   rI   )"dataclassesr\   r   r   r   cupyx.distributed.arrayr   r   r   rN   r   _SliceIndicesr^   dict_BlockLocationMapr   _ExecutionPlan	_BatchIdx	dataclassr	   r4   rD   rT   rO   rV   ri   r   r   r   r   r   r   r8   r   r   r   r   <module>r      s        % % % % % % % %  * * * * * * * * * * * * * * * * * * c3m$ -./	
 DcN23  eIy#567
 -$%	 
 
 
 
 
 
 
 
8?%8?%8? 8? 8? 8? 8?v  %  &  	       FH%*H&+CHoH
=#H H H H,-$%,
5#:, , , ,c?'+CeE3J6G1H,H'I	)&
&'   $=	"=uSCZ()5+<<== uUCZ()5+<<== 	= = = =** * * ** * * *       04x x x%>x	+	,x x x x xv1s)1s)1 $s3x.!1 
#tE%*%&
&'	1 1 1 1 1 1r   