o
    X۷i5                     @  s   d dl mZ zd dlZdZW n ey   dZY nw d dlZd dlmZ d dlm	Z	 d dl
Zd dl
mZ d dl
mZ G d	d
 d
ejZdd ZdS )    )annotationsNTF)driver)runtime)_base)_compressedc                   @  s   e Zd ZdZdZd/ddZdd Zdd	 Zd
d Zdd Z	dd Z
d0ddZdd Zd1ddZd/ddZd1ddZdd Zd2ddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. ZdS )3
csc_matrixa  Compressed Sparse Column matrix.

    This can be instantiated in several ways.

    ``csc_matrix(D)``
        ``D`` is a rank-2 :class:`cupy.ndarray`.
    ``csc_matrix(S)``
        ``S`` is another sparse matrix. It is equivalent to ``S.tocsc()``.
    ``csc_matrix((M, N), [dtype])``
        It constructs an empty matrix whose shape is ``(M, N)``. Default dtype
        is float64.
    ``csc_matrix((data, (row, col)))``
        All ``data``, ``row`` and ``col`` are one-dimenaional
        :class:`cupy.ndarray`.
    ``csc_matrix((data, indices, indptr))``
        All ``data``, ``indices`` and ``indptr`` are one-dimenaional
        :class:`cupy.ndarray`.

    Args:
        arg1: Arguments for the initializer.
        shape (tuple): Shape of a matrix. Its length must be two.
        dtype: Data type. It must be an argument of :class:`numpy.dtype`.
        copy (bool): If ``True``, copies of given arrays are always used.

    .. seealso::
        :class:`scipy.sparse.csc_matrix`

    cscNc                 C  sH   t std| j|}| j|}| j|}tjj|||f| j	dS )a  Returns a copy of the array on host memory.

        .. warning::
           You need to install SciPy to use this method.

        Args:
            stream (cupy.cuda.Stream): CUDA stream object. If it is given, the
                copy runs asynchronously. Otherwise, the copy is synchronous.

        Returns:
            scipy.sparse.csc_matrix: Copy of the array on host memory.

        zscipy is not available)shape)
_scipy_availableRuntimeErrordatagetindicesindptrscipysparser   _shape)selfstreamr   r   r    r   M/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/scipy/sparse/_csc.pyr   2   s   zcsc_matrix.getc                 C  s@   ddl m} |dr|j|dd}n||}|j|j|jfS )Nr   cusparsedenseToSparser   )format)cupyxr   check_availabilityr   	dense2cscr   r   r   )r   xr   mr   r   r   _convert_denseH   s
   

zcsc_matrix._convert_densec                 C  s   ||fS Nr   )r   r   yr   r   r   _swapQ   s   zcsc_matrix._swapc                 C  s  ddl m} t|r|   | | j| S t jj	|ra|   |  |
dr9|  }|  |||S |
drLtjsL| j}|j||ddS |
dr_|  }|  |||S tt|r|   |  |
drtjs| j}|j}|j||dddS |
dr|  }| }|  |  |||S |
dr|  }| }|  |  |||S tt jj|r| |  S t|rQ|jdkr|   | | j| S |jd	kr|   |
d
rtjrt dkr|j}n|
dr
tjs
|j}nt|| jt|ddS |jdkrM|   |
dr6tjr2t dkr6|j}n|
dr@|j}nt|| jt|ddS tdt S )Nr   r   spgemmcsrgemmT)transacsrgemm2)r&   transb   csrmvispmv   csrmm2spmmzcould not interpret dimensions)!r   r   cupyisscalarsum_duplicates
_with_datar   r   r   isspmatrix_csrr   tocsrr$   r   is_hipTr%   r'   AssertionErrorisspmatrix_csc
isspmatrixr   isdensendimr   get_build_versionr*   r+   asfortranarrayr-   r.   
ValueErrorNotImplemented)r   otherr   abr*   csrmmr   r   r   __mul__T   s   





zcsc_matrix.__mul__c                 C  s0   | j }|  |j }|j| _|j| _|j| _dS )zRemoves zero entories in place.N)r6   eliminate_zerosr   r   r   )r   tcompressr   r   r   rE      s   zcsc_matrix.eliminate_zerosc                 C  s*   ddl m} | js||  d| _dS dS )zSorts the indices of this matrix *in place*.

        .. warning::
            Calling this function might synchronize the device.

        r   r   TN)r   r   has_sorted_indicescscsort)r   r   r   r   r   sort_indices   s
   

zcsc_matrix.sort_indicesc                 C  s   ddl m} |du rd}| }| jdkrtj| j| j|dS |  }d|_	|
  |drOtjr7|jdkrO||}|dkrB|S |dkrKt|S td	|dkrZ||jjS |dkrc||S td	)
a  Returns a dense matrix representing the same value.

        Args:
            order ({'C', 'F', None}): Whether to store data in C (row-major)
                order or F (column-major) order. Default is C-order.
            out: Not supported.

        Returns:
            cupy.ndarray: Dense array representing the same matrix.

        .. seealso:: :meth:`scipy.sparse.csc_matrix.toarray`

        r   r   NC)r	   dtypeorderFsparseToDenseFzorder not understood)r   r   uppernnzr/   zerosr	   rL   copyhas_canonical_formatr1   r   r   r5   rN   ascontiguousarrayr>   	csr2denser6   	csc2dense)r   rM   outr   r   r"   r   r   r   toarray   s0   





zcsc_matrix.toarrayc                 C  s`   ddl m} |   | j}|  |dr|j}n|dr%|j}nt|| j|||jS )Nr   r   csrgeam2csrgeam)	r   r   r1   tocscr6   r   rZ   r[   NotImplementedError)r   r@   alphabetar   r[   r   r   r   _add_sparse   s   


zcsc_matrix._add_sparseFc                 C  s@   ddl m} |r| j }| j }n| j}| j}|| ||S )zConverts the matrix to COOrdinate format.

        Args:
            copy (bool): If ``False``, it shares data arrays as much as
                possible.

        Returns:
            cupyx.scipy.sparse.coo_matrix: Converted matrix.

        r   r   )r   r   r   rS   r   csc2coo)r   rS   r   r   r   r   r   r   tocoo
  s   
zcsc_matrix.tocooc                 C  s   |r|   S | S )a  Converts the matrix to Compressed Sparse Column format.

        Args:
            copy (bool): If ``False``, the method returns itself.
                Otherwise it makes a copy of the matrix.

        Returns:
            cupyx.scipy.sparse.csc_matrix: Converted matrix.

        rS   )r   rS   r   r   r   r\      s   zcsc_matrix.tocscc                 C  s@   ddl m} |dr|j}|| S |dr|j}|| S t)ax  Converts the matrix to Compressed Sparse Row format.

        Args:
            copy (bool): If ``False``, it shares data arrays as much as
                possible. Actually this option is ignored because all
                arrays in a matrix cannot be shared in csr to csc conversion.

        Returns:
            cupyx.scipy.sparse.csr_matrix: Converted matrix.

        r   r   csc2csr
csc2csrEx2)r   r   r   rd   re   r]   )r   rS   r   rd   r   r   r   r4   0  s   

zcsc_matrix.tocsrc                 C  s   |   S )zInverts the format.
        )r4   )r   r   r   r   _tocsxH  s   zcsc_matrix._tocsxc                 C  sP   |durt d| jd | jd f}tjjj| j| j| jf||d}| j	|_	|S )aT  Returns a transpose matrix.

        Args:
            axes: This option is not supported.
            copy (bool): If ``True``, a returned matrix shares no data.
                Otherwise, it shared data arrays as much as possible.

        Returns:
            cupyx.scipy.sparse.csr_matrix: `self` with the dimensions reversed.

        NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r)   r   )r	   rS   )
r>   r	   r   r   r   
csr_matrixr   r   r   rT   )r   axesrS   r	   transr   r   r   	transposeQ  s   zcsc_matrix.transposec                 C  s   | j t||d dd S )zReturns a copy of row i of the matrix, as a (1 x n)
        CSR matrix (row vector).

        Args:
            i (integer): Row

        Returns:
            cupyx.scipy.sparse.csc_matrix: Sparse matrix with single row
        r)   Trc   )_minor_sliceslicer4   r   ir   r   r   getrowh  s   
zcsc_matrix.getrowc                 C  s   | j t||d ddS )zReturns a copy of column i of the matrix, as a (m x 1)
        CSC matrix (column vector).

        Args:
            i (integer): Column

        Returns:
            cupyx.scipy.sparse.csc_matrix: Sparse matrix with single column
        r)   Trc   )_major_slicerl   rm   r   r   r   getcolt  s   
zcsc_matrix.getcolc                 C  s   t ||d }| ||S Nr)   )rl   _major_index_fancyrk   r   rowcolr   r   r   _get_intXarray     zcsc_matrix._get_intXarrayc                 C  s,   t ||d }|jdv }| |j||dS )Nr)   )r)   Nrc   )rl   steprp   rk   )r   ru   rv   rS   r   r   r   _get_intXslice  s   
zcsc_matrix._get_intXslicec                 C  s"   t ||d }| |j|ddS )Nr)   Trc   )rl   rp   rk   rt   r   r   r   _get_sliceXint  s   zcsc_matrix._get_sliceXintc                 C     |  ||S r!   )rs   rk   rt   r   r   r   _get_sliceXarray     zcsc_matrix._get_sliceXarrayc                 C  s   t ||d }| ||S rr   )rl   rp   _minor_index_fancyrt   r   r   r   _get_arrayXint  rx   zcsc_matrix._get_arrayXintc                 C  r|   r!   )rp   r   rt   r   r   r   _get_arrayXslice  r~   zcsc_matrix._get_arrayXslicer!   )NN)F)NF)__name__
__module____qualname____doc__r   r   r    r#   rD   rE   rJ   rY   r`   rb   r\   r4   rf   rj   ro   rq   rw   rz   r{   r}   r   r   r   r   r   r   r      s0    
	^
-



	r   c                 C  s
   t | tS )zChecks if a given matrix is of CSC format.

    Returns:
        bool: Returns if ``x`` is :class:`cupyx.scipy.sparse.csc_matrix`.

    )
isinstancer   )r   r   r   r   r8     s   
r8   )
__future__r   scipy.sparser   r
   ImportErrorr/   cupy_backends.cuda.apir   r   cupyx.scipy.sparser   r   r   _compressed_sparse_matrixr   r8   r   r   r   r   <module>   s$       
