
    `i                       d dl Zd dlZd dlZd dl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Z G d	 d
e          Zd Zd Zd Zi dddddddddddddddddddddddfdddddfd dd!dd"d#d$d#d%d#d&d#d#ddd'd'd#d#d#d(d#d)d)d*d+d,Zi dd-dej         dfdd-dd-dd-dd-dd-dd-dd-dd.dd.d d-d!d-d"d-d$d-d%d-d&d-d-d-d-ej         dfej         dfd-d-d-d-d-d.d.ej         dfd/d,Z!d0 Z" ej#                    d1             Z$d2e%fd3Z&dgd6Z'dhd7Z(did9Z)dgd:Z*djd=Z+dkd>Z,dkd?Z-dld@Z.dmdAZ/dhdBZ0dhdCZ1dD Z2dE Z3dndGZ4dH Z5dI Z6dJ Z7dK Z8dL Z9dM Z:dN Z;dO Z<dP Z=dQ Z>dR Z?dS Z@ G dT dUe          ZA G dV dWeA          ZB G dX dYeA          ZC G dZ d[eA          ZD G d\ d]eA          ZEdgd^ZFdod_ZG	 	 dpd`ZHdqdaZIdrdcZJdhddZKdsdeZLdtdfZMdS )u    N)driver)runtime)cusparse)_dtype)device)stream)_utilc                   \    e Zd Zd Zed             Zd Zej        fdZ	d Z
d Zd Zd Zd	S )
MatDescriptorc                     || _         d S N)
descriptor)selfr   s     b/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/cusparse.py__init__zMatDescriptor.__init__   s    $    c                 F    t          j                    }t          |          S r   )	_cusparsecreateMatDescrr   )clsdescrs     r   createzMatDescriptor.create   s    (**U###r   c                     | j         dfS )N )r   )r   s    r   
__reduce__zMatDescriptor.__reduce__   s    {Br   c                 p     |            rd S | j         r"t          j        | j                    d | _         d S d S r   )r   r   destroyMatDescrr   is_shutting_downs     r   __del__zMatDescriptor.__del__   sJ     	F? 	#%do666"DOOO	# 	#r   c                 :    t          j        | j        |           d S r   )r   
setMatTyper   )r   typs     r   set_mat_typezMatDescriptor.set_mat_type%   s    T_c22222r   c                 :    t          j        | j        |           d S r   )r   setMatIndexBaser   )r   bases     r   set_mat_index_basez MatDescriptor.set_mat_index_base(   s    !$/488888r   c                 :    t          j        | j        |           d S r   )r   setMatFillModer   )r   	fill_modes     r   set_mat_fill_modezMatDescriptor.set_mat_fill_mode+        )<<<<<r   c                 :    t          j        | j        |           d S r   )r   setMatDiagTyper   )r   	diag_types     r   set_mat_diag_typezMatDescriptor.set_mat_diag_type.   r-   r   N)__name__
__module____qualname__r   classmethodr   r   r	   r   r    r$   r(   r,   r1   r   r   r   r   r      s        % % % $ $ [$   (-'= # # # #3 3 39 9 9= = == = = = =r   r   c                  v    d | D             }t          j        t          j        |          fd| D             S )Nc                      g | ]}||j         S r   dtype).0xs     r   
<listcomp>z%_cast_common_type.<locals>.<listcomp>3   s    333!Q]ag]]]r   c                 X    g | ]&}| |j         k    r|                              n|'S r   )r9   astype)r:   r;   r9   s     r   r<   z%_cast_common_type.<locals>.<listcomp>5   sG         !}E1A1AAHHUOOOq   r   )
_functoolsreduce_numpypromote_types)xsdtypesr9   s     @r   _cast_common_typerE   2   sV    33r333Ff2F;;E      r   c                 6    | rt           j        S t           j        S r   )r   CUSPARSE_OPERATION_TRANSPOSE CUSPARSE_OPERATION_NON_TRANSPOSE)transs    r   _transpose_flagrJ   9   s     :5599r   c                     |dk    rd}n"|dk    rd}n|dk    rd}n|dk    rd}nt           t          t          || z             } || S )NfsdFcDz)	TypeErrorgetattrr   )namer9   argsprefixrL   s        r   _call_cusparserX   @   se    ||	#	#	#	6D=))A1d8Or   csrmv)@  *  csrmvExcsrmmcsrmm2csrgeamcsrgeam2)<#  Ncsrgemmcsrgemm2)rZ   .  gthrspmv'  r[   )LinuxWindowsspmmi=(  	csr2dense	csc2densecsrsort)rZ   Ncscsortcoosortcoo2csr)rg   N)ra   rd   )i$,  N)i\+  N)iP-  N)csr2coocsr2csccsc2csr
csr2cscEx2
csc2csrEx2	dense2csc	dense2csrcsr2csr_compresscsrsm2csrilu02denseToSparsesparseToDensespgemmspsm)i1  N)  N)iNc                     t          | t                    rCt          j                    }|| vr$d                    |          }t          |          | |         S | S )Nz/No version information specified for the OS: {})
isinstancedict	_platformsystemformat
ValueError)r;   os_namemsgs      r   _get_avail_version_from_specr      s`    !T "$$!CJJ CS//!zHr   c                 R   t           j        st          }t          j                    }nt
          }t          j                    }| |vr$d                    |           }t          |          ||          \  }}t          |          }t          |          }|||k     rdS |||k    rdS dS )Nz1No available version information specified for {}FT)
_runtimeis_hip_available_cusparse_versionr   get_build_version_available_hipsparse_version_driverr   r   r   )rU   available_versionversionr   version_addedversion_removeds         r   check_availabilityr      s    ? .7-//8+--$$$AHHNNoo%6t%<"M?0??M2?CCO W}%<%<u"w/'A'Au4r   returnc                  L    t          j        t          j                              S r   )r   
getVersion_deviceget_cusparse_handler   r   r   r   r      s     ; = =>>>r      Fc                    t          d          st          d          ||j        j        sJ |s| j        n| j        ddd         }|d         t          |          k    rt          d          t          j                    }|\  }}	t          | ||          \  } }}| j
        }
|t          j        ||
          }t          j        ||
          j        }t          j        ||
          j        }t!          d|
|t#          |          | j        d         | j        d         | j        |j        | j        j        | j        j        j        | j        j        j        | j        j        j        |j        j        |j        |j        j                   |S )a\  Matrix-vector product for a CSR-matrix and a dense vector.

    .. math::

       y = \alpha * o_a(A) x + \beta y,

    where :math:`o_a` is a transpose function when ``transa`` is ``True`` and
    is an identity function otherwise.

    Args:
        a (cupyx.cusparse.csr_matrix): Matrix A.
        x (cupy.ndarray): Vector x.
        y (cupy.ndarray or None): Vector y. It must be F-contiguous.
        alpha (float): Coefficient for x.
        beta (float): Coefficient for y.
        transa (bool): If ``True``, transpose of ``A`` is used.

    Returns:
        cupy.ndarray: Calculated ``y``.

    rY   zcsrmv is not available.Nr   dimension mismatchr   )r   RuntimeErrorflagsf_contiguousshapelenr   r   r   rE   r9   _cupyzerosrA   arrayctypesrX   rJ   nnzdata_descrr   ptrindptrindices)ar;   yalphabetatransaa_shapehandlemnr9   s              r   rY   rY      s`   , g&& 645559,99,#6agg2GqzSVV-...(**FDAq1a((GAq!GEyK5!!L&&-E<e$$+D''	
AGAJuz183F	*AIN,>	
DIqvz+ + + Hr   c                     | j         j         j        dz  dk    rdS | j        j         j        dz  dk    rdS | j        j         j        dz  dk    rdS |j         j        dz  dk    rdS ||j         j        dz  dk    rdS dS )a  Check if the pointers of arguments for csrmvEx are aligned or not

    Args:
        a (cupyx.cusparse.csr_matrix): Matrix A.
        x (cupy.ndarray): Vector x.
        y (cupy.ndarray or None): Vector y.

        Check if a, x, y pointers are aligned by 128 bytes as
        required by csrmvEx.

    Returns:
        bool:
        ``True`` if all pointers are aligned.
        ``False`` if otherwise.

       r   FNT)r   r   r   r   )r   r;   r   s      r   csrmvExIsAlignedr      s    $ 	v{!!ux}3!##uy~C1$$uvzC1u}c)Q..u4r   Tc                 $   t          d          st          d          ||j        j        sJ | j        d         t          |          k    rt          d          t          j                    }| j        \  }}t          | ||          \  } }}| j
        }	|t          j        ||	          }t          j        |	          }
|rt          j        nt          j        }t          j        }t'          j        ||	          j        }t'          j        ||	          j        }t-          | ||          sJ t          j        |||| j        d         | j        d         | j        |j        |
| j        j        | j        j        j        |
| j        j        j        | j        j        j        |j        j        |
|j        |
|j        j        |
|
          }t          j        |d          }|j        j        dz  dk    sJ t          j         |||| j        d         | j        d         | j        |j        |
| j        j        | j        j        j        |
| j        j        j        | j        j        j        |j        j        |
|j        |
|j        j        |
|
|j        j                   |S )	a  Matrix-vector product for a CSR-matrix and a dense vector.

    .. math::

       y = \alpha * A x + \beta y,

    Args:
        a (cupyx.cusparse.csr_matrix): Matrix A.
        x (cupy.ndarray): Vector x.
        y (cupy.ndarray or None): Vector y. It must be F-contiguous.
        alpha (float): Coefficient for x.
        beta (float): Coefficient for y.
        merge_path (bool): If ``True``, merge path algorithm is used.

        All pointers must be aligned with 128 bytes.

    Returns:
        cupy.ndarray: Calculated ``y``.

    r\   zcsrmvEx is not available.Nr   r   r   br   )!r   r   r   r   r   r   r   r   r   rE   r9   r   r   r   to_cuda_dtyper   CUSPARSE_ALG_MERGE_PATHCUSPARSE_ALG_NAIVErH   rA   r   r   r   csrmvEx_bufferSizer   r   r   r   r   r   r   emptyr\   )r   r;   r   r   r   
merge_pathr   r   r   r9   datatypealgmodetransa_flag
bufferSizebufs                  r   r\   r\     s?   * i(( 867779,99,wqzSVV-...(**F7DAq1a((GAq!GEyK5!!#E**H5i//!4 <KL&&-E<e$$+DAq!$$$$$-	
AGAJuz8	QV[_h	19>-	
Hdi	
Hh( (J +j#
&
&C8<#""""	
AGAJuz8	QV[_h	19>-	
Hdi	
Hh6 6 6 Hr   c                    t          d          st          d          | j        |j        cxk    rdk    sn J |j        j        sJ ||j        j        sJ |s| j        n| j        ddd         }|d         |j        d         k    rt          d          t          j                    }|\  }}	|j        d         }
t          | ||          \  } }}|t          j        ||
f| j        d	          }|	}|}t          j        || j                  j        }t          j        || j                  j        }t!          d| j        |t#          |          | j        d         |
| j        d         | j        |j        | j        j        | j        j        j        | j        j        j        | j        j        j        |j        j        ||j        |j        j        |           |S )
a|  Matrix-matrix product for a CSR-matrix and a dense matrix.

    .. math::

       C = \alpha o_a(A) B + \beta C,

    where :math:`o_a` is a transpose function when ``transa`` is ``True`` and
    is an identity function otherwise.

    Args:
        a (cupyx.scipy.sparse.csr): Sparse matrix A.
        b (cupy.ndarray): Dense matrix B. It must be F-contiguous.
        c (cupy.ndarray or None): Dense matrix C. It must be F-contiguous.
        alpha (float): Coefficient for AB.
        beta (float): Coefficient for C.
        transa (bool): If ``True``, transpose of A is used.

    Returns:
        cupy.ndarray: Calculated C.

    r]   zcsrmm is not available.   Nr   r   r   r   rO   )r   r   ndimr   r   r   r   r   r   rE   r   r   r9   rA   r   r   rX   rJ   r   r   r   r   r   r   r   )r   r   rP   r   r   r   r   r   r   kr   ldbldcs                r   r]   r]   T  s   , g&& 645556QV    q      79,99,#6agg2GqzQWQZ-...(**FDAq	
A1a((GAq!yKA--
C
CL((/E<ag&&-D''	
Aqwqz15
AH'	19>-	
CAFJ5 5 5 Hr         ?        c                 8   t          d          st          d          | j        |j        cxk    rdk    sn J | j        sJ |j        j        sJ ||j        j        sJ |r|rJ |s| j        n| j        ddd         }|s|j        n|j        ddd         }|d         |d         k    rt          d          t          j	                    }	|\  }
}|d         }t          | ||          \  } }}|t          j        |
|f| j        d	          }|j        d         }|j        d         }t          |          }t          |          }t          j        || j                  j        }t          j        || j                  j        }t%          d| j        |	||| j        d         || j        d         | j        |j        | j        j        | j        j        j        | j        j        j        | j        j        j        |j        j        ||j        |j        j        |           |S )
aV  Matrix-matrix product for a CSR-matrix and a dense matrix.

    .. math::

       C = \alpha o_a(A) o_b(B) + \beta C,

    where :math:`o_a` and :math:`o_b` are transpose functions when ``transa``
    and ``tranb`` are ``True`` respectively. And they are identity functions
    otherwise.
    It is forbidden that both ``transa`` and ``transb`` are ``True`` in
    cuSPARSE specification.

    Args:
        a (cupyx.scipy.sparse.csr): Sparse matrix A.
        b (cupy.ndarray): Dense matrix B. It must be F-contiguous.
        c (cupy.ndarray or None): Dense matrix C. It must be F-contiguous.
        alpha (float): Coefficient for AB.
        beta (float): Coefficient for C.
        transa (bool): If ``True``, transpose of A is used.
        transb (bool): If ``True``, transpose of B is used.

    Returns:
        cupy.ndarray: Calculated C.

    r^   zcsrmm2 is not available.r   Nr   r   r   r   rO   )r   r   r   has_canonical_formatr   r   r   r   r   r   rE   r   r   r9   rJ   rA   r   r   rX   r   r   r   r   r   r   r   )r   r   rP   r   r   r   transbr   b_shaper   r   r   r   r   r   op_aop_bs                    r   r^   r^     s   4 h'' 756666QV    q      !!!!79,99,"6"""#6agg2G#6agg2GqzWQZ-...(**FDAq
A1a((GAq!yKA--
'!*C
'!*C6""D6""DL((/E<ag&&-D!'dAGAJ171:qu
AH'	19>-	
CAFJ5 5 5 Hr   c                 T   t          d          st          d          t          | t          j        j        j                  s/t          d                    t          |                               t          |t          j        j        j                  s/t          d                    t          |                              | j
        sJ |j
        sJ | j        |j        k    rt          d          t          j                    }| j        \  }}t          | |          \  } }t!          j        dd          }t%          j        |t$          j                   t*                                          }t/          j        |dz   d          }	t%          j        |||| j        j        | j        | j        j        j        | j        j        j        |j        j        |j        |j        j        j        |j        j        j        |j        |	j        j        |j         j                   t/          j        tC          |          d          }
t/          j        tC          |          | j"                  }t!          j#        || j"                  j         }t!          j#        || j"                  j         }tI          d| j"        ||||j        | j        j        | j        | j        j        j        | j        j        j        | j        j        j        |j        |j        j        |j        |j        j        j        |j        j        j        |j        j        j        |j        |j        j        |	j        j        |
j        j                   t          j        j                            ||
|	f| j                  }d	|_%        |S )
e  Matrix-matrix addition.

    .. math::
        C = \alpha A + \beta B

    Args:
        a (cupyx.scipy.sparse.csr_matrix): Sparse matrix A.
        b (cupyx.scipy.sparse.csr_matrix): Sparse matrix B.
        alpha (float): Coefficient for A.
        beta (float): Coefficient for B.

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

    r_   zcsrgeam is not available.unsupported type (actual: {})inconsistent shapesr   ir   r   T)&r   r   r   cupyxscipysparse
csr_matrixrS   r   typer   r   r   r   r   rE   rA   r   r   setPointerModeCUSPARSE_POINTER_MODE_HOSTr   r   r   xcsrgeamNnzr   r   r   r   r   r   r   r   intr9   r   rX   _has_canonical_format)r   r   r   r   r   r   r   r   c_descrc_indptr	c_indicesc_datarP   s                r   r_   r_     s     i(( 86777a+677 I7>>tAwwGGHHHa+677 I7>>tAwwGGHHH!!!!!!!!w!'.///(**F7DAqQ""DAq
,r3

C	46 6 6 ""$$G{1q5#&&H1	QUAHM$5qy~7I	QUAHM$5qy~7IHM-sz	@ @ @ CHHc**I[S17++FL((/E<ag&&-D171ej	QUAFKO	19>-ty	QUAFKO	19>-FKOX]->   	%%	H%QW 	& 	6 	6A"AHr   c                 4	   t          d          st          d          t          | t          j        j        j                  s/t          d                    t          |                               t          |t          j        j        j                  s/t          d                    t          |                              | j
        sJ |j
        sJ | j        |j        k    rt          d          t          j                    }| j        \  }}t          | |          \  } }t!          j        dd          }t%          j        |t$          j                   t!          j        || j                  j        }t!          j        || j                  j        }t0                                          }t5          j        |dz   d          }	d}
t7          d	| j        ||||j        | j        j        | j        | j        j        j         | j!        j        j         | j"        j        j         |j        |j        j        |j        |j        j        j         |j!        j        j         |j"        j        j         |j        |
|	j        j         |
          }t5          j        |t           j#                  }t%          j$        |||| j        j        | j        | j!        j        j         | j"        j        j         |j        j        |j        |j!        j        j         |j"        j        j         |j        |	j        j         |j        j        |j        j                    t5          j        tK          |          d          }t5          j        tK          |          | j                  }t7          d| j        ||||j        | j        j        | j        | j        j        j         | j!        j        j         | j"        j        j         |j        |j        j        |j        |j        j        j         |j!        j        j         |j"        j        j         |j        |j        j         |	j        j         |j        j         |j        j                    t          j        j                            |||	f| j        
          }d|_&        |S )r   r`   zcsrgeam2 is not available.r   r   r   r   r   r   csrgeam2_bufferSizeExtr   T)'r   r   r   r   r   r   r   rS   r   r   r   r   r   r   r   rE   rA   r   r   r   r   r   r9   r   r   r   r   rX   r   r   r   r   r   r   r   int8xcsrgeam2Nnzr   r   )r   r   r   r   r   r   r   r   r   r   null_ptr	buff_sizebuffr   r   rP   s                   r   r`   r`     s     j)) 97888a+677 I7>>tAwwGGHHHa+677 I7>>tAwwGGHHH!!!!!!!!w!'.///(**F7DAqQ""DAq
,r3

C	46 6 6 L((/E<ag&&-D""$$G{1q5#&&HH !'1ej!("5quafko	19>-ty!(:M	qv{ 119>3EHhm&7C CI ;y&+..D1ah)15!(-2C		AH/8I		G.0A
	( ( (
 CHHc**I[S17++FAG1ej!("5quafko	19>-ty!(:M	qv{ 119>3EFKOX]->DIM+ + + 	%%	H%QW 	& 	6 	6A"AHr   c                    t          d          st          d          | j        |j        cxk    rdk    sn J | j        sJ |j        sJ |s| j        n| j        ddd         }|s|j        n|j        ddd         }|d         |d         k    rt          d          t          j                    }|\  }}|d         }	t          | |          \  } }| j	        dk    s|j	        dk    r-t          j        j                            ||	f| j        	          S t          |          }
t          |          }t!          j        d
d          }t%          j        |t$          j                   t*                                          }t/          j        |dz   d          }t%          j        ||
|||	|| j        j        | j	        | j        j        j        | j        j        j        |j        j        |j	        |j        j        j        |j        j        j        |j        |j        j        |j        j                   t/          j        tA          |          d          }t/          j        tA          |          | j                  }tC          d| j        ||
|||	|| j        j        | j	        | j        j        j        | j        j        j        | j        j        j        |j        j        |j	        |j        j        j        |j        j        j        |j        j        j        |j        |j        j        |j        j        |j        j                   t          j        j                            |||f||	f          }d|_"        |S )a  Matrix-matrix product for CSR-matrix.

    math::
       C = op(A) op(B),

    Args:
        a (cupyx.scipy.sparse.csr_matrix): Sparse matrix A.
        b (cupyx.scipy.sparse.csr_matrix): Sparse matrix B.
        transa (bool): If ``True``, transpose of A is used.
        transb (bool): If ``True``, transpose of B is used.

    Returns:
        cupyx.scipy.sparse.csr_matrix: Calculated C.

    rb   zcsrgemm is not available.r   Nr   r   r   r   r8   r   r   r   T)#r   r   r   r   r   r   r   r   rE   r   r   r   r   r   r9   rJ   rA   r   r   r   r   r   r   r   xcsrgemmNnzr   r   r   r   r   r   r   r   rX   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rP   s                     r   rb   rb   P  s     i(( 867776QV    q      !!!!!!!!#6agg2G#6agg2GqzWQZ-...(**FDAq
AQ""DAquzzQUaZZ{!,,aV17,CCC6""D6""D
,r3

C	46 6 6 ""$$G{1q5#&&HdAq!QX%8!%	19>-qx/BAE	19>-w/A3:?	, , , CHHc**I[S17++F17dAq!QX%8!%	*AIN,>	QUAFKOQX]5F		FKOX]->   	%%	H%aV 	& 	5 	5A"AHr   c                 |   t          d          st          d          | j        |j        cxk    rdk    sn J t          | t          j        j        j                  s/t          d	                    t          |                               t          |t          j        j        j                  s/t          d	                    t          |                              | j        sJ |j        sJ | j        d         |j        d         k    rt          d          ||j        dk    sJ t          |t          j        j        j                  s/t          d	                    t          |                              |j        sJ | j        d         |j        d         k    s|j        d         |j        d         k    rt          d          t          j        r&t!          j                    d	k     rt          d
          t%          j                    }| j        \  }}|j        \  }}	|t)          | |          \  } }nt)          | ||          \  } }}t+          j                    }
t/          j        || j                  j        }d}|$|}t6                                          }d}|}|}|}ngt/          j        || j                  j        }|j        }|j        }|j        }|j        j        j         }|j!        j        j         }|j"        j        j         }tG          d| j        |||	||j        | j        j$        | j        | j!        j        j         | j"        j        j         |j        j$        |j        |j!        j        j         |j"        j        j         ||j$        ||||
          }tK          j&        |t.          j'                  }t/          j&        dd          }t+          j(        |t*          j)                   t6                                          }tK          j&        |dz   d          }t+          j*        |||	|| j        j$        | j        | j!        j        j         | j"        j        j         |j        j$        |j        |j!        j        j         |j"        j        j         |j$        ||||j$        |j        j         |j        j        |
|j        j                    tK          j&        tW          |          d          }tK          j&        tW          |          | j                  }tG          d| j        |||	||j        | j        j$        | j        | j        j        j         | j!        j        j         | j"        j        j         |j        j$        |j        |j        j        j         |j!        j        j         |j"        j        j         ||j$        |||||j$        |j        j         |j        j         |j        j         |
|j        j                    t          j        j                            |||f||	f          }d|_,        t+          j-        |
           |S )a  Matrix-matrix product for CSR-matrix.

    math::
       C = alpha * A * B + beta * D

    Args:
        a (cupyx.scipy.sparse.csr_matrix): Sparse matrix A.
        b (cupyx.scipy.sparse.csr_matrix): Sparse matrix B.
        d (cupyx.scipy.sparse.csr_matrix or None): Sparse matrix D.
        alpha (scalar): Coefficient
        beta (scalar): Coefficient

    Returns:
        cupyx.scipy.sparse.csr_matrix

    rc   zcsrgemm2 is not available.r   r   r   r   mismatched shapeNr   z'd != None is supported since ROCm 4.2.0csrgemm2_bufferSizeExtr   r   r   T).r   r   r   r   r   r   r   r   rS   r   r   r   r   r   r   r   r   r   r   r   rE   r   createCsrgemm2InforA   r   r9   r   r   r   r   r   r   r   r   r   rX   r   r   r   r   r   r   xcsrgemm2Nnzr   r   destroyCsrgemm2Info)r   r   rN   r   r   r   r   r   _r   infor   	beta_datad_descrd_nnzd_datad_indptr	d_indicesr   r   c_nnzr   r   r   r   rP   s                             r   rc   rc     sO   " j)) 978886QV    q      a+677 I7>>tAwwGGHHHa+677 I7>>tAwwGGHHH!!!!!!!!wqzQWQZ+,,,}v{{{{!U[/:;; 	M;BB477KKLLL%%%%71:##qwqzQWQZ'?'?/000? 	Jw8::S@@HIII(**F7DAq7DAqy A&&11#Aq!,,1a'))DL((/EHy	&&((		|D!'**1I	(8=$IN&	 !'1aQX%8!%	19>-qx/BAE	19>-y':LxD* *I ;y&+..DLS!!EVY%IJJJ""$$G{1q5#&&H1a,aeQX]5F		AH/8I		G.xHM-u|/@$	   CJJ,,I[UQW--FAG1aQX%8!%	*AIN,>	QUAFKOQX]5F		Iw'95&)W/9>-tTY]D D D 	%%	H%aV 	& 	5 	5A"A!$'''Hr   c                    t          d          st          d          | j        }|j        dv sJ |t	          j        | j        |d          }n|j        j        sJ t          j
                    }t          d| j        || j        d         | j        d         | j        j        | j        j        j        | j        j        j        | j        j        j        |j        j        | j        d                    |S )	a!  Converts CSR-matrix to a dense matrix.

    Args:
        x (cupyx.scipy.sparse.csr_matrix): A sparse matrix to convert.
        out (cupy.ndarray or None): A dense metrix to store the result.
            It must be F-contiguous.

    Returns:
        cupy.ndarray: Converted result.

    rk   zcsr2dense is not available.fdFDNrO   r9   orderr   r   )r   r   r9   charr   r   r   r   r   r   r   rX   r   r   r   r   r   r   r;   outr9   r   s       r   rk   rk     s     k** :8999GE:
{k!'c:::y%%%%(**FQW
AGAJ(;	*AIN,>agaj	" " " Jr   c                    t          d          st          d          | j        }|j        dv sJ |t	          j        | j        |d          }n|j        j        sJ t          j
                    }t          d| j        || j        d         | j        d         | j        j        | j        j        j        | j        j        j        | j        j        j        |j        j        | j        d                    |S )	a!  Converts CSC-matrix to a dense matrix.

    Args:
        x (cupyx.scipy.sparse.csc_matrix): A sparse matrix to convert.
        out (cupy.ndarray or None): A dense metrix to store the result.
            It must be F-contiguous.

    Returns:
        cupy.ndarray: Converted result.

    rl   zcsc2dense is not available.r  NrO   r  r   r   )r   r   r9   r  r   r   r   r   r   r   r   rX   r   r   r   r   r   r   r  s       r   rl   rl     s     k** :8999GE:
{k!'c:::y%%%%(**FQW
AGAJ(;	+QX]->agaj	" " " Jr   c                    t          d          st          d          | j        }|dk    rdS t          j                    }| j        \  }}t          j        ||||| j        j	        j
        | j        j	        j
                  }t          j        |d          }t          j        |d          }| j	                                        }t          j        |||j	        j
                   t          j        ||||| j        j        | j        j	        j
        | j        j	        j
        |j	        j
        |j	        j
        	  	         t          d          rJt'          d| j        |||j	        j
        | j	        j	        j
        |j	        j
        t          j                   dS t,                              || j	                  }	t0                              |          }
t          j        ||
j        |	j                   dS )zzSorts indices of CSR-matrix in place.

    Args:
        x (cupyx.scipy.sparse.csr_matrix): A sparse matrix to sort.

    rm   zcsrsort is not available.r   Nr   r   re   )r   r   r   r   r   r   r   xcsrsort_bufferSizeExtr   r   r   r   r   r   copycreateIdentityPermutationxcsrsortr   r   rX   r9   CUSPARSE_INDEX_BASE_ZEROSpVecDescriptorr   DnVecDescriptorgatherdescr;   r   r   r   r   buffer_sizer   P	data_origdesc_xdesc_ys              r   rm   rm   >      i(( 86777
%C
axx(**F7DAq21c18=,		 K +k3
'
'CCAI'QVZ@@@1c18.0A		AFJ6 6 6 &!! ;AGC+QV[_FJ	:	< 	< 	< 	< 	<
 !''1622 ''	22fk:::::r   c                    t          d          st          d          | j        }|dk    rdS t          j                    }| j        \  }}t          j        ||||| j        j	        j
        | j        j	        j
                  }t          j        |d          }t          j        |d          }| j	                                        }t          j        |||j	        j
                   t          j        ||||| j        j        | j        j	        j
        | j        j	        j
        |j	        j
        |j	        j
        	  	         t          d          rJt'          d| j        |||j	        j
        | j	        j	        j
        |j	        j
        t          j                   dS t,                              || j	                  }	t0                              |          }
t          j        ||
j        |	j                   dS )zzSorts indices of CSC-matrix in place.

    Args:
        x (cupyx.scipy.sparse.csc_matrix): A sparse matrix to sort.

    rn   zcscsort is not available.r   Nr   r   re   )r   r   r   r   r   r   r   xcscsort_bufferSizeExtr   r   r   r   r   r   r  r  xcscsortr   r   rX   r9   r  r  r   r  r  r  r  s              r   rn   rn   d  r  r   rc           
         t          d          st          d          | j        }|dk    rdS t          j                    }| j        \  }}t          j        ||||| j        j	        j
        | j        j	        j
                  }t          j        |d          }t          j        |d          }| j	                                        }	t          j        |||j	        j
                   |dk    rNt          j        ||||| j        j	        j
        | j        j	        j
        |j	        j
        |j	        j
                   nc|dk    rNt          j        ||||| j        j	        j
        | j        j	        j
        |j	        j
        |j	        j
                   nt%          d	          | j        j        d
k    rt          d          rIt+          d| j        |||	j	        j
        | j	        j	        j
        |j	        j
        t          j                   nZt.                              || j	                  }
t2                              |	          }t          j        ||j        |
j                   |dk    r	d| _        dS dS )zSorts indices of COO-matrix in place.

    Args:
        x (cupyx.scipy.sparse.coo_matrix): A sparse matrix to sort.
        sort_by (str): Sort the indices by row ('r', default) or column ('c').

    ro   zcoosort is not available.r   Nr   r   r  rP   z!sort_by must be either 'r' or 'c'?re   F)r   r   r   r   r   r   r   xcoosort_bufferSizeExtrowr   r   colr   r   r  r  xcoosortByRowxcoosortByColumnr   r9   r  rX   r  r  r   r  r  r  r   )r;   sort_byr   r   r   r   r  r   r  r  r  r  s               r   ro   ro     s/    i(( 86777
%C
axx(**F7DAq21c15:>15:>; ;K
+k3
'
'CCAI'QVZ@@@#~~Aq#quz~quz~FJ	& 	& 	& 	& 
C"Aq#quz~quz~FJ	& 	& 	& 	& <===w|sf%% 	?Y^/
I>@ @ @ @
 %++Aqv66F$++I66FVV[&+>>>#~~"' ~r   c                    t          j                    }| j        d         }| j        }|dk    rt	          j        |dz   d          }nTt	          j        |dz   d          }t          j        || j	        j
        j        |||j
        j        t          j                   t          j        j                            | j
        | j        |f| j                  S )Nr   r   r   r   )r   r   r   r   r   r   r   r   xcoo2csrr!  r   r   r  r   r   r   r   r"  )r;   r   r   r   r   s        r   rp   rp         (**F	
A
%C
axxQUC((QUC((AEJNCKOY?	A 	A 	A ;((	
qw ) 0 0 0r   c                    t          j                    }| j        d         }| j        }|dk    rt	          j        |dz   d          }nTt	          j        |dz   d          }t          j        || j	        j
        j        |||j
        j        t          j                   t          j        j                            | j
        | j        |f| j                  S )Nr   r   r   r   )r   r   r   r   r   r   r   r   r'  r"  r   r   r  r   r   r   
csc_matrixr!  )r;   r   r   r   r   s        r   coo2cscr+    r(  r   c                    t          d          st          d          t          j                    }| j        d         }| j        }t          j        |d          }t          j	        || j
        j        j        |||j        j        t          j                   t          j        j                            |||ff| j                  S )aD  Converts a CSR-matrix to COO format.

    Args:
        x (cupyx.scipy.sparse.csr_matrix): A matrix to be converted.
        data (cupy.ndarray): A data array for converted data.
        indices (cupy.ndarray): An index array for converted data.

    Returns:
        cupyx.scipy.sparse.coo_matrix: A converted matrix.

    rq   zcsr2coo is not available.r   r   r   )r   r   r   r   r   r   r   r   r   xcsr2coor   r   r   r  r   r   r   
coo_matrix)r;   r   r   r   r   r   r!  s          r   rq   rq     s     i(( 86777(**F	
A
%C
+c3

C!338<*, , , ;((	W~ag ) / / /r   c                    t          d          st          d          t          j                    }| j        \  }}| j        }t          j        || j                  }t          j        |d          }|dk    rt          j	        |dz   d          }nt          j        |dz   d          }t          d| j        ||||| j        j        j        | j        j        j        | j        j        j        |j        j        |j        j        |j        j        t          j        t          j                   t$          j        j                            |||f| j                  S )Nrr   csr2csc is not available.r   r   r   r   )r   r   r   r   r   r   r   r   r9   r   rX   r   r   r   r   r   CUSPARSE_ACTION_NUMERICr  r   r   r   r*  r;   r   r   r   r   r   r   r   s           r   rr   rr     )   i(( 86777(**F7DAq
%C;sAG$$Dk#s##G
axxQUC((QUC((qwAq#qv{HMqy~1IM7<+V[_-.	0 	0 	0 ;((	wqw ) 0 0 0r   c                 .   t          d          st          d          t          j                    }| j        \  }}| j        }t          j        || j                  }t          j        |d          }|dk    rt          j	        |dz   d          }nWt          j        |dz   d          }t          j        | j                  }t          j        }	t          j        }
t          j        }t          j        ||||| j        j        j        | j        j        j        | j        j        j        |j        j        |j        j        |j        j        ||	|
|          }t          j        |t*          j                  }t          j        ||||| j        j        j        | j        j        j        | j        j        j        |j        j        |j        j        |j        j        ||	|
||j        j                   t0          j        j                            |||f| j                  S )Nrt   zcsr2cscEx2 is not available.r   r   r   r   )r   r   r   r   r   r   r   r   r9   r   r   r   r   r1  r  CUSPARSE_CSR2CSC_ALG1csr2cscEx2_bufferSizer   r   r   r   rA   r   rt   r   r   r   r*  r;   r   r   r   r   r   r   r   x_dtypeactionibasealgor  buffers                 r   rt   rt        l++ ;9:::(**F7DAq
%C;sAG$$Dk#s##G
axxQUC((QUC((&qw//22.5Aq#qv{0AIN	v{Lgvud< < [&+66Aq#qv{0AIN	v{LgvudFKO	M 	M 	M ;((	wqw ) 0 0 0r   c                 P   t          j                    }| j        d         }| j        }t	          j        |d          }t          j        || j        j	        j
        |||j	        j
        t          j                   t          j        j                            |||ff| j                  S )aD  Converts a CSC-matrix to COO format.

    Args:
        x (cupyx.scipy.sparse.csc_matrix): A matrix to be converted.
        data (cupy.ndarray): A data array for converted data.
        indices (cupy.ndarray): An index array for converted data.

    Returns:
        cupyx.scipy.sparse.coo_matrix: A converted matrix.

    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"  s          r   csc2coor?  *  s     (**F	
A
%C
+c3

C!338<*, , , ;((	~ag ) / / /r   c                    t          d          st          d          t          j                    }| j        \  }}| j        }t          j        || j                  }t          j        |d          }|dk    rt          j	        |dz   d          }nt          j        |dz   d          }t          d| j        ||||| j        j        j        | j        j        j        | j        j        j        |j        j        |j        j        |j        j        t          j        t          j                   t$          j        j                            |||f| j                  S )Nrs   r0  r   r   r   rr   r   )r   r   r   r   r   r   r   r   r9   r   rX   r   r   r   r   r   r1  r  r   r   r   r   r2  s           r   rs   rs   B  r3  r   c                 .   t          d          st          d          t          j                    }| j        \  }}| j        }t          j        || j                  }t          j        |d          }|dk    rt          j	        |dz   d          }nWt          j        |dz   d          }t          j        | j                  }t          j        }	t          j        }
t          j        }t          j        ||||| j        j        j        | j        j        j        | j        j        j        |j        j        |j        j        |j        j        ||	|
|          }t          j        |t*          j                  }t          j        ||||| j        j        j        | j        j        j        | j        j        j        |j        j        |j        j        |j        j        ||	|
||j        j                   t0          j        j                            |||f| j                  S )Nru   zcsc2csrEx2 is not available.r   r   r   r   )r   r   r   r   r   r   r   r   r9   r   r   r   r   r1  r  r5  r6  r   r   r   r   rA   r   rt   r   r   r   r   r7  s                 r   ru   ru   Z  r=  r   c                    t          d          st          d          | j        dk    sJ t          j        |           } t          j        dd          }t          j                    }| j	        \  }}t                                          }t          j        |d          }t          d| j        |t          j        |||j        | j        j        ||j        j        |j        j                   t)          |          }t          j        || j                  }t          j        |dz   d          }t          j        |d          }	t          d| j        ||||j        | j        j        ||j        j        |j        j        |	j        j        |j        j                   t*          j        j                            ||	|f| j	        	          }
d
|
_        |
S )zConverts a dense matrix in CSC format.

    Args:
        x (cupy.ndarray): A matrix to be converted.

    Returns:
        cupyx.scipy.sparse.csc_matrix: A converted matrix.

    rv   zdense2csc is not available.r   r   r   r8   r   r   r   T)r   r   r   r   asfortranarrayrA   r   r   r   r   r   r   rX   r9   r   CUSPARSE_DIRECTION_COLUMNr   r   r   r   r   r   r   r   r*  r   )r;   r   r   r   r   r   nnz_per_colr   r   r   cscs              r   rv   rv   x  s    k** :89996Q;;;;QA
,r
%
%
%C(**F7DAq  ""E+a%%Kqw	3Q5;K	
A{'+SZ_> > >
 c((C;sAG$$D[Q$$Fk#s##GQW1e&	
A{'+	w|'	: : : +

'
'w(?qw
'
O
OC $CJr   c                    t          d          st          d          | j        dk    sJ t          j        |           } t          j        dd          }t          j                    }| j	        \  }}t                                          }t          j        |d          }t          d| j        |t          j        |||j        | j        j        ||j        j        |j        j                   t)          |          }t*          j        r|dk    rt/          d	          t          j        || j                  }t          j        |d
z   d          }t          j        |d          }	t          d| j        ||||j        | j        j        ||j        j        |j        j        |j        j        |	j        j                   t0          j        j                            ||	|f| j	                  }
d|
_        |
S )zConverts a dense matrix in CSR format.

    Args:
        x (cupy.ndarray): A matrix to be converted.

    Returns:
        cupyx.scipy.sparse.csr_matrix: A converted matrix.

    rw   zdense2csr is not available.r   r   r   r8   r   r   @hipSPARSE currently cannot handle sparse matrices with null ptrsr   r   T)r   r   r   r   rC  rA   r   r   r   r   r   r   rX   r9   r   CUSPARSE_DIRECTION_ROWr   r   r   r   r   r   r   r   r   r   r   r   r   )r;   r   r   r   r   r   nnz_per_rowr   r   r   csrs              r   rw   rw     s    k** :89996Q;;;;QA
,r
%
%
%C(**F7DAq  ""E+a%%Kqw	0!Q8H	
A{'+SZ_> > >
 c((C ?!88 > ? ? ?;sAG$$D[Q$$Fk#s##GQW1e&	
A{'+	v{(8	: : : +

'
'w(?qw
'
O
OC $CJr   c                 T   t          d          st          d          | j        j        dv sJ t	          j                    }| j        \  }}t          j        |d          }t          d| j        ||| j
        j        | j        j        j        | j        j        j        |j        j        |	  	        }t          j        || j                  }t          j        |dz   d          }t          j        |d          }	t          d| j        |||| j
        j        | j        j        j        | j        j        j        | j        j        j        | j        |j        j        |j        j        |	j        j        |j        j        |           t$          j        j                            ||	|f| j                  S )Nrx   z"csr2csr_compress is not available.r  r   nnz_compressr   r   )r   r   r9   r  r   r   r   r   r   rX   r   r   r   r   r   r   r   r   r   r   r   r   )
r;   tolr   r   r   rJ  r   r   r   r   s
             r   rx   rx     st   011 A?@@@7<6!!!!(**F7DAq+a%%K
18&	*K,<,@#G GC ;sAG$$D[Q$$Fk#s##GAG1ah)	+QX]->	{#TY]GL4D   ;((	wqw ) 0 0 0r   c                 |    | dk    rt           j        S | dk    rt           j        S | dk    rt           j        S t          )Nuint16int32int64)r   CUSPARSE_INDEX_16UCUSPARSE_INDEX_32ICUSPARSE_INDEX_64IrS   r8   s    r   _dtype_to_IndexTyperV    sC    ++	'		++	'		++r   c                   0    e Zd ZddZej        fdZd ZdS )BaseDescriptorNc                 0    || _         || _        || _        d S r   )r  getdestroy)r   r   rZ  	destroyers       r   r   zBaseDescriptor.__init__  s    	 r   c                      |            rd S | j         	d | _        d S | j        #|                      | j                   d | _        d S d S r   )r[  r  r   s     r   r    zBaseDescriptor.__del__  sZ     	F<DIIIY"LL###DIII #"r   c                 n    | j         (t          |                      | j                  |          S t          r   )rZ  rT   r  AttributeError)r   rU   s     r   __getattr__zBaseDescriptor.__getattr__  s.    8488DI..555r   )NN)r2   r3   r4   r   r	   r   r    r`  r   r   r   rX  rX    sS        ! ! ! !
 (-'=        r   rX  c                   *    e Zd Zed             Zd ZdS )SpMatDescriptorc                 p   t           j        j                            |          sJ |j        \  }}t
          j        }t          j        |j	                  }|j
        dk    rt          j        |||j        |j        j        j        |j        j        j        |j        j        j        t#          |j        j	                  t#          |j        j	                  ||
  
        }t
          j        }n/|j
        dk    rrt          j        |||j        |j        j        j        |j        j        j        |j        j        j        t#          |j        j	                  ||	  	        }t
          j        }n|j
        dk    rt          j        |||j        |j        j        j        |j        j        j        |j        j        j        t#          |j        j	                  t#          |j        j	                  ||
  
        }d }n't1          d
                    |j
                            t
          j        }t5          |||          S )NrK  coorF  z3csr, csc and coo format are supported (actual: {}).)r   r   r   issparser   r   r  r   r   r9   r   	createCsrr   r   r   r   r   rV  csrGet	createCoor!  r"  cooGet	createCscr   destroySpMatrb  )	r   r   rowscolsidx_base
cuda_dtyper  rZ  r[  s	            r   r   zSpMatDescriptor.create  s   {!**1-----W
d5)!'22
8u&dAE18=#4ain6H!4QX^!D!D#AIO44h
L LD "CCX&dAE15:>15:>!4QU[!A!A*& &D "CCX&dAE18=#4ain6H!4QX^!D!D#AIO44h
L LD CC --3VAH-=-=? ? ?(tS'222r   c                 <    t          j        | j        ||           d S r   )r   spMatSetAttributer  )r   	attributer   s      r   set_attributezSpMatDescriptor.set_attribute-  s    #DIy$?????r   N)r2   r3   r4   r5   r   rs  r   r   r   rb  rb    sA        3 3 [3:@ @ @ @ @r   rb  c                   $    e Zd Zed             ZdS )r  c           	      (   |j         }t          j        |j                  }t	          j        |||j        j        |j        j        t          |j                  t          j	        |          }t          j
        }t          j        }t          |||          S r   )sizer   r   r9   r   createSpVecr   r   rV  r  spVecGetdestroySpVecr  )r   idxr;   r   ro  r  rZ  r[  s           r   r   zSpVecDescriptor.create3  sw    f)!'22
$S#sx|QVZ%8%C%C%.%G%/1 1  (tS'222r   Nr2   r3   r4   r5   r   r   r   r   r  r  1  s-        	3 	3 [	3 	3 	3r   r  c                   $    e Zd Zed             ZdS )r  c                     t          j        |j                  }t          j        |j        |j        j        |          }t          j        }t          j	        }t          |||          S r   )r   r   r9   r   createDnVecrv  r   r   dnVecGetdestroyDnVecr  )r   r;   ro  r  rZ  r[  s         r   r   zDnVecDescriptor.createB  sP    )!'22
$QVQVZDD (tS'222r   Nr{  r   r   r   r  r  @  s-        3 3 [3 3 3r   r  c                   $    e Zd Zed             ZdS )DnMatDescriptorc                 .   |j         dk    sJ |j        j        sJ |j        \  }}|}t	          j        |j                  }t          j        ||||j	        j
        |t          j                  }t          j        }t          j        }t          |||          S )Nr   )r   r   r   r   r   r   r9   r   createDnMatr   r   CUSPARSE_ORDER_COLdnMatGetdestroyDnMatr  )	r   r   rl  rm  ldro  r  rZ  r[  s	            r   r   zDnMatDescriptor.createM  s    v{{{{w####W
d)!'22
$T4QVZ%.%AC C (tS'222r   Nr{  r   r   r   r  r  K  s-        
3 
3 [
3 
3 
3r   r  c                 v   t          d          st          d          t          | t          j        j        j                  ra| j        }t          |t          j        j        j                  s1d	                    t          |                    }t          |          |} | }t          | t          j        j        j        t          j        j        j        f          s/t          d	                    t          |                               |s| j        n| j        ddd         }|d         t          |          k    rt          d          | j        sJ |\  }	}
t#          | ||          \  } }}|t%          j        |	| j                  }n"t          |          |	k    rt          d          | j        d	k    r|                    d	           |S t.                              |           }t2                              |          }t2                              |          }t5          j                    }t9          |          }t;          j        || j                  j        }t;          j        || j                  j        }tA          j!        | j                  }tD          j#        }tE          j$        |||j%        |j&        |j&        |j%        |j&        ||	  	        }t%          j'        |t$          j(                  }tE          j)        |||j%        |j&        |j&        |j%        |j&        |||j%        j*        
  
         |S )
a  Multiplication of sparse matrix and dense vector.

    .. math::

        y = \alpha * op(A) x + \beta * y

    Args:
        a (cupyx.scipy.sparse.csr_matrix, csc_matrix or coo_matrix):
            Sparse matrix A
        x (cupy.ndarray): Dense vector x
        y (cupy.ndarray or None): Dense vector y
        alpha (scalar): Coefficient
        beta (scalar): Coefficient
        transa (bool): If ``True``, op(A) = transpose of A.

    Returns:
        cupy.ndarray
    rf   zspmv is not available."aT must be csr_matrix (actual: {})r   Nr   r   r   r   )+r   r   r   r   r   r   r*  Tr   r   r   rS   r.  r   r   r   r   rE   r   r   r9   r   fillrb  r   r  r   r   rJ   rA   r   r   r   r   r   CUSPARSE_MV_ALG_DEFAULTspMV_bufferSizer   r  r   r   spMVr   )r   r;   r   r   r   r   aTr   r   r   r   desc_ar  r  r   r   ro  algr   r   s                       r   rf   rf   [  s   & f%% 53444!U['233 S"ek0;<< 	!6==d2hhGGCC.. a%+,7+,79 : : I7>>tAwwGGHHH#6agg2GqzSVV-...!!!!DAq1a((GAq!yK17##	Q1-...uzz	q			##A&&F##A&&F##A&&F(**F6""DL((/E<ag&&-D%ag..J

+C)&$
*0+v{DI*0+z3H HI ;y%*--DN64V[&+9fk:sDIMK K K Hr   c                 \   t          d          st          d          | j        |j        cxk    rdk    sn J |j        j        sJ ||j        j        sJ t          | t          j        j        j	                  ra| j
        }t          |t          j        j        j                  s1d                    t          |                    }t          |          |} | }t          | t          j        j        j        t          j        j        j        f          s/t          d                    t          |                               |s| j        n| j        ddd         }	|s|j        n|j        ddd         }
|	d         |
d	         k    rt#          d
          | j        sJ |	\  }}|
\  }}t'          | ||          \  } }}|t)          j        ||f| j        d          }n1|j        d	         |k    s|j        d         |k    rt#          d
          | j        d	k    r|                    d	           |S t2                              |           }t6                              |          }t6                              |          }t9          j                    }t=          |          }t=          |          }t?          j         || j                  j!        }t?          j         || j                  j!        }tE          j#        | j                  }tH          j%        }tI          j&        ||||j'        |j(        |j(        |j'        |j(        ||
  
        }t)          j)        |t(          j*                  }tI          j+        ||||j'        |j(        |j(        |j'        |j(        |||j'        j,                  }|S )a  Multiplication of sparse matrix and dense matrix.

    .. math::

        C = \alpha * op(A) op(B) + \beta * C

    Args:
        a (cupyx.scipy.sparse.csr_matrix, csc_matrix or coo_matrix):
            Sparse matrix A
        b (cupy.ndarray): Dense matrix B
        c (cupy.ndarray or None): Dense matrix C
        alpha (scalar): Coefficient
        beta (scalar): Coefficient
        transa (bool): If ``True``, op(A) = transpose of A.
        transb (bool): If ``True``, op(B) = transpose of B.

    Returns:
        cupy.ndarray
    rj   zspmm is not available.r   Nr  r   r   r   r   r   rO   )-r   r   r   r   r   r   r   r   r   r*  r  r   r   r   rS   r.  r   r   r   rE   r   r   r9   r   r  rb  r   r  r   r   rJ   rA   r   r   r   r   r   CUSPARSE_MM_ALG_DEFAULTspMM_bufferSizer   r  r   r   spMMr   )r   r   rP   r   r   r   r   r  r   r   r   r   r   r   r   r  desc_bdesc_cr   r   r   ro  r  r   r   s                            r   rj   rj     sY   ( f%% 534446QV    q      79,99,!U['233 S"ek0;<< 	!6==d2hhGGCC.. a%+,7+,79 : : I7>>tAwwGGHHH#6agg2G#6agg2GqzWQZ-...!!!!DAqDAq1a((GAq!yKA--	
qAGAJ!OO-...uzz	q			##A&&F##A&&F##A&&F(**F6""D6""DL((/E<ag&&-D%ag..J

+C)&$ej*0+v{DI*0+z3H HI ;y%*--DvtT5:v{%{DIv{)3	? ?I Hr   c                    t          d          st          d          t          j        j                            |           s3t          j        j                            |           st          d          t          |t          j
                  st          d          |j        dvrt          d          | j        d         | j        d         cxk    r|j        d         k    sn t          d	          | j        |j        k    rt          d
          |du rt          j        }n3|du rt          j        }n"t          d                    |                    |du rt          j        }	n3|du rt          j        }	n"t          d                    |                    |du rd}
n)|du rd}
n"t          d                    |                    |du rt          j        }n3|du rt          j        }n"t          d                    |                    | j        }|j        dk    rd}nL|j        dk    rd}n>|j        dk    rd}n0|j        dk    rd}n"t          d                    |                    t1          t          |dz             }t1          t          |dz             }t1          t          |dz             }|du s|dk    rt          j        }nb|du s|dk    rt          j        }nK|dk    r#|j        dv rt          j        }n/t          j        }n"t          d                     |                    t          j        j                            |           rV|t          j        k    rt          d!          | j        } t          j        j                            |           sJ d|z
  }d|z
  }| j        d         }|j        dk    rdn|j        d         }|j        rt          j        }|}n%|j        rt          j        }|}nt          d"          t?          j                     }tC          j"        ||#          }tF          $                                }|%                    t          j&                   |'                    t          j(                   |)                    |           |*                    |	           t          j+                    } |||
||||| j,        |j-        j.        |j/        | j.        j.        j0        | j1        j.        j0        | j2        j.        j0        |j.        j0        |||          }t          j3        |ftB          j4        #          } |||
||||| j,        |j-        j.        |j/        | j.        j.        j0        | j1        j.        j0        | j2        j.        j0        |j.        j0        ||||j.        j0                    |||
||||| j,        |j-        j.        |j/        | j.        j.        j0        | j1        j.        j0        | j2        j.        j0        |j.        j0        ||||j.        j0                   tk          j6                    7                                 t          j8        |           d$S )%aV  Solves a sparse triangular linear system op(a) * x = alpha * b.

    Args:
        a (cupyx.scipy.sparse.csr_matrix or cupyx.scipy.sparse.csc_matrix):
            Sparse matrix with dimension ``(M, M)``.
        b (cupy.ndarray): Dense vector or matrix with dimension ``(M)`` or
            ``(M, K)``.
        alpha (float or complex): Coefficient.
        lower (bool):
            True: ``a`` is lower triangle matrix.
            False: ``a`` is upper triangle matrix.
        unit_diag (bool):
            True: diagonal part of ``a`` has unit elements.
            False: diagonal part of ``a`` has non-unit elements.
        transa (bool or str): True, False, 'N', 'T' or 'H'.
            'N' or False: op(a) == ``a``.
            'T' or True: op(a) == ``a.T``.
            'H': op(a) == ``a.conj().T``.
        blocking (bool):
            True: blocking algorithm is used.
            False: non-blocking algorithm is used.
        level_info (bool):
            True: solves it with level information.
            False: solves it without level information.

    Note: ``b`` will be overwritten.
    ry   zcsrsm2 is not available.z"a must be CSR or CSC sparse matrixzb must be cupy.ndarray)r   r   b.ndim must be 1 or 2r   r   zinvalid shapedtype mismatchTFUnknown lower (actual: {})Unknown unit_diag (actual: {})zUnknown blocking (actual: {})Unknown level_info (actual: {})rL   rM   rN   rO   rP   rQ   rR   Invalid dtype (actual: {})csrsm2_bufferSizeExtcsrsm2_analysiscsrsm2_solveNr  HfdzUnknown transa (actual: {})z@If matrix is CSC format and complex dtype,transa must not be 'H''b must be F-contiguous or C-contiguous.r8   N)9r   r   r   r   r   isspmatrix_csrisspmatrix_cscr   r   r   ndarrayr   r   r9   rS   r   CUSPARSE_FILL_MODE_LOWERCUSPARSE_FILL_MODE_UPPERr   CUSPARSE_DIAG_TYPE_NON_UNITCUSPARSE_DIAG_TYPE_UNITCUSPARSE_SOLVE_POLICY_NO_LEVELCUSPARSE_SOLVE_POLICY_USE_LEVELr  rT   rH   rG   &CUSPARSE_OPERATION_CONJUGATE_TRANSPOSEr  _f_contiguous_c_contiguousr   r   rA   r   r   r   r$   CUSPARSE_MATRIX_TYPE_GENERALr(   r  r,   r1   createCsrsm2Infor   r   r   r   r   r   r   r   r   _streamget_current_streamsynchronizedestroyCsrsm2Info)r   r   r   lower	unit_diagr   blocking
level_infor+   r0   r;  policyr9   thelperanalysissolver   nrhsr   r   r   a_descr   ws_sizewss                             r   ry   ry     s#   : h'' 75666K--a00 ?K--a00?=>>>a'' 31222vV0111GAJ!'!*2222
2222)))w!'()))}}6			%6		5<<UCCDDDE9			d		5		9@@KKLLL5	T		8??IIJJJU9	t		::AA*MMNNNGEzS	s			s			s		4;;EBBCCCY$: :;;Fy!&7"788HIq>122E&C--;	46S==7	3:;FFEFF6==fEEFFF{((++ "YEEE 8 9 9 9C{!0033333V	M		
A!11D D;	
 D7BCCC(**FLe,,,E!!##F
	>???
i@AAA
Y'''
Y'''%''DfVT661dAE\&(916;?X]&	(:AFJ6# #G 
gZv{	3	3	3BHVT661dAE5<;L1BY^S$M M M 
E&$48I

QV[_ahm.?
).
afj#tVRW[J J J
   ,,...%%%%%r   c                    t          d          st          d          t          j        j                            |           st          d          | j        d         | j        d         k    r't          d	                    | j                            |du rt          j        }n3|du rt          j        }n"t          d		                    |                    | j        }|j        d
k    rd}nL|j        dk    rd}n>|j        dk    rd}n0|j        dk    rd}n"t          d	                    |                    t          t          |dz             }t          t          |dz             }t          t          |dz             }t          t          d          }t!          j                    }	| j        d         }
| j        }t&                                          }|                    t          j                   |                    t          j                   t          j                    } ||	|
||j        | j        j        j        | j        j        j        | j        j        j        |          }t?          j         |ftB          j"                  }tC          j         dtB          j#                  } ||	|
||j        | j        j        j        | j        j        j        | j        j        j        |||j        j        
  
         	  ||	||j$        j                   n6# tJ          $ r) t          d	                    |d                             w xY w ||	|
||j        | j        j        j        | j        j        j        | j        j        j        |||j        j        
  
         	  ||	||j$        j                   dS # tJ          $ r) t          d	                    |d                             w xY w)a  Computes incomplete LU decomposition for a sparse square matrix.

    Args:
        a (cupyx.scipy.sparse.csr_matrix):
            Sparse matrix with dimension ``(M, M)``.
        level_info (bool):
            True: solves it with level information.
            False: solves it without level information.

    Note: ``a`` will be overwritten. This function does not support fill-in
        (only ILU(0) is supported) nor pivoting.
    rz   zcsrilu02 is not available.za must be CSR sparse matrixr   r   zinvalid shape (a.shape: {})FTr  rL   rM   rN   rO   rP   rQ   rR   r  csrilu02_bufferSizecsrilu02_analysisxcsrilu02_zeroPivotr8   r   za({0},{0}) is missingzu({0},{0}) is zeroN)&r   r   r   r   r   r  rS   r   r   r   r   r  r  r9   r  rT   r   r   r   r   r   r$   r  r(   r  createCsrilu02Infor   r   r   r   r   r   r   rA   r   rQ  r   	Exception)r   r  r  r9   r  r  r  r  checkr   r   r   r  r   r  r  positions                    r   rz   rz   |  s    j)) 97888;,,Q// 75666wqzQWQZ6==agFFGGGU9	t		::AA*MMNNNGEzS	s			s			s		4;;EBBCCCY$9 9::Fy!&9"9::HIq:~..EI455E(**F	
A
%C!!Di<===I>???'))DfVQT_afkoX]&	(:DB BG	gZv{	3	3	3B|D555HHVQT_afkoX]	 2D&"'+O O OFfdHO01111 F F F077DDEEEF 
E&!S$/16;?
(-
QY^/vrw{L L LCfdHO011111 C C C-44Xa[AABBBCs   >L 3M	N6 63O)rK  c                 @   t          d          st          d          | j        dk    sJ | j        j        dv sJ t          j        |           } t                              |           }|dk    r1t          j
        j                            | j        | j                  }n|dk    r1t          j
        j                            | j        | j                  }nY|dk    r1t          j
        j                            | j        | j                  }n"t!          d	                    |                    t$                              |          }t&          j        }t+          j                    }t'          j        ||j        |j        |          }t          j        |t
          j                  }t'          j        ||j        |j        ||j        j                   t=          j        d
d          }	t=          j        d
d          }
t=          j        d
d          }t'          j         |j        |	j!        j        |
j!        j        |j!        j                   tE          |          }tF          j$        r|d
k    rtK          d          |dk    re|j&        }t          j        |d          }t          j        || j                  }t          j
        j                            |||f| j                  }n|dk    re|j&        }t          j        |d          }t          j        || j                  }t          j
        j                            |||f| j                  }ny|dk    rst          j'        |d          }t          j'        |d          }t          j        || j                  }t          j
        j                            |||ff| j                  }t$                              |          }t'          j(        ||j        |j        ||j        j                   d|_)        |S )a0  Converts a dense matrix into a CSR, CSC or COO format.

    Args:
        x (cupy.ndarray): A matrix to be converted.
        format (str): Format of converted matrix. It must be either 'csr',
            'csc' or 'coo'.

    Returns:
        cupyx.scipy.sparse.spmatrix: A converted sparse matrix.

    r{   zdenseToSparse is not available.r   r  rK  r8   rF  rd  zunsupported format (actual: {})r   rR  rH  r   r   T)*r   r   r   r9   r  r   rC  r  r   r   r   r   r   r   r*  r.  rS   r   rb  r   "CUSPARSE_DENSETOSPARSE_ALG_DEFAULTr   r   denseToSparse_bufferSizer  r   r   denseToSparse_analysisr   r   rA   r   spMatGetSizer   r   r   r   r   r   r   denseToSparse_convertr   )r;   r   r  r   r  r;  r   r   r   num_rows_tmpnum_cols_tmpr   r   r   r   r!  r"  s                    r   r{   r{     s    o.. ><===6Q;;;;7<6!!!!QA##A&&FK))!')AA	5K))!')AA	5K))!')AA9@@HHIII##A&&F7D(**F266;39;F FI;y%*--D$VV[%+[$	G G G<111L<111L
,q
(
(
(C6;(;(@'.3SZ_F F F
c((C ?!88 > ? ? ?+c3''{3((K))4&*A01 * 9 9	5+c3''{3((K))4&*A01 * 9 9	5k#s##k#s## {3((K))4#s*<AG)LL##A&&F#FFK$*Kty}F F F"AHr   c                    t          d          st          d          | j        }|j        dv sJ |t	          j        | j        |d          }n|j        j        sJ |j        |k    sJ t          
                    |           }t          
                    |          }t          j        }t          j                    }t          j        ||j        |j        |          }t	          j        |t          j                  }t(          j        r| j        dk    rt/          d          t          j        ||j        |j        ||j        j                   |S )	a*  Converts sparse matrix to a dense matrix.

    Args:
        x (cupyx.scipy.sparse.spmatrix): A sparse matrix to convert.
        out (cupy.ndarray or None): A dense metrix to store the result.
            It must be F-contiguous.

    Returns:
        cupy.ndarray: A converted dense matrix.

    r|   zsparseToDense is not available.r  NrO   r  r   rH  )r   r   r9   r  r   r   r   r   r   rb  r   r  r   "CUSPARSE_SPARSETODENSE_ALG_DEFAULTr   r   sparseToDense_bufferSizer  r   r   r   r   r   r   r|   r   r   )	r;   r  r9   r  desc_outr;  r   r   r   s	            r   r|   r|     sN    o.. ><===GE:
{k!'c:::y%%%%yE!!!!##A&&F%%c**H7D(**F266;3;=$H HI;y%*--D ?5A:: > ? ? ?FFK$M4@ @ @ Jr   c                 
   t          d          st          d          |du rd}n|du rd}n|dvrt          d| d	          t          j        j                            |           rnt          j        j                            |           rE|dk    r
| j        } d}n1|dk    r
| j        } d}n!|d
k    r| 	                                j        } d}| }n4t          j        j        
                    |           rnt          d          | j        sJ |j        dk    rd}|                    dd          }n|j        dk    rd}nt          d          | j        d         | j        d         cxk    r|j        d         k    sn t          d          | j        }|j        dvr"t#          d                    |                    ||j        k    rt#          d          |du rt&          j        }n3|du rt&          j        }n"t          d                    |                    |du rt&          j        }	n3|du rt&          j        }	n"t          d                    |                    |dk    rt&          j        }
n5|dk    rt&          j        }
n"|j        dv rt&          j        }
nt&          j        }
|j        rt&          j        }nP|j        r:t'          j                    dk     rt          d          |j        }t&          j        }nt          d          | j        \  }}|t&          j        k    r|j        \  }}n
|j        \  }}||f}t=          j        || j        d          }tA          j!                    }tD          #                    |           }tH          #                    |          }tH          #                    |          }t'          j%                    }tM          j'        ||j                  j(        }tS          j*        |j                  }t&          j+        }	 |,                    t&          j-        |           |,                    t&          j.        |	           t'          j/        ||
||j0        |j1        |j1        |j1        |||
  
        }t=          j2        |t<          j3                  }t'          j4        ||
||j0        |j1        |j1        |j1        ||||j0        j5                   t'          j6        ||
||j0        |j1        |j1        |j1        ||||j0        j5                   |r|                    d          }|t'          j7        |           S # t'          j7        |           w xY w)a  Solves a sparse triangular linear system op(a) * x = alpha * op(b).

    Args:
        a (cupyx.scipy.sparse.csr_matrix or cupyx.scipy.sparse.coo_matrix):
            Sparse matrix with dimension ``(M, M)``.
        b (cupy.ndarray): Dense matrix with dimension ``(M, K)``.
        alpha (float or complex): Coefficient.
        lower (bool):
            True: ``a`` is lower triangle matrix.
            False: ``a`` is upper triangle matrix.
        unit_diag (bool):
            True: diagonal part of ``a`` has unit elements.
            False: diagonal part of ``a`` has non-unit elements.
        transa (bool or str): True, False, 'N', 'T' or 'H'.
            'N' or False: op(a) == ``a``.
            'T' or True: op(a) == ``a.T``.
            'H': op(a) == ``a.conj().T``.
    r~   zspsm is not available.Fr  Tr  NTHzUnknown transa (actual: )r  z'a must be CSR, CSC or COO sparse matrixr   r   r   r  r   r   r  r  r  r  r  r  i-  zb must be F-contiguous.r  rL   r  r8   )8r   r   r   r   r   r   r  r  r  conjisspmatrix_coor   r   reshaper   r9   r  rS   r   r   r  r  r  r  rH   rG   r  r  r  r   r   r   r   r   rb  r   r  spSM_createDescrrA   r   r   r   r   CUSPARSE_SPSM_ALG_DEFAULTrs  CUSPARSE_SPMAT_FILL_MODECUSPARSE_SPMAT_DIAG_TYPEspSM_bufferSizer   r  r   r   spSM_analysisr   
spSM_solvespSM_destroyDescr)r   r   r   r  r  r   is_b_vectorr9   r+   r0   r   r   r   r   r   c_shaperP   r   mat_amat_bmat_c
spsm_descrro  r;  r   r   s                             r   r~   r~   3  sq   & f%% 53444 	4	u		=F===>>> {((++ D			*	*1	-	- DS==AFFs]]AFFs]]
AF				*	*1	-	- DBCCC!!!! 	v{{IIb!	
10111 GAJ!'!*2222
2222+,,, GEz4;;EBBCCC())) }}6			%6		5<<UCCDDD E9			d		5		9@@KKLLL }}9	35:9DDCD 	 D9	
 D&((5006777C5BCCC 7DAqy999w11w1dGG17#666A (**F""1%%E""1%%E""1%%E+--JLag...5E%ag..J.D0I>	JJJ 	I>	JJJ -D$
EJ
J
D*6 6	 {9EJ777 	D$
EJ
J
D*dim	E 	E 	E
 	D$
EJ
J
D*dim	E 	E 	E
  			"A 	#J////	#J////s   D!U U$c                 
   t          d          st          d          | j        |j        cxk    rdk    sn J t          | t          j        j        j                  s/t          d	                    t          |                               t          |t          j        j        j                  s/t          d	                    t          |                              | j        sJ |j        sJ | j        d         |j        d         k    rt          d          | j        \  }}|j        \  }}t          | |          \  } }||f}t          j        j                            || j                  }t!          j                    }	t$                              |           }
t$                              |          }t$                              |          }t)          j                    }t(          j        }t(          j        }t/          j        ||j                  j        }t/          j        d|j                  j        }t5          j        |j                  }t(          j        }d}t)          j        |	|||j        |
j        |j        |j        |j        |||d|          }tA          j!        |t@          j"                  }t)          j        |	|||j        |
j        |j        |j        |j        |||||j        j#                   t)          j$        |	|||j        |
j        |j        |j        |j        |||d|          }tA          j!        |t@          j"                  }t)          j$        |	|||j        |
j        |j        |j        |j        |||||j        j#                   t/          j        dd	          }t/          j        dd	          }t/          j        dd	          }t)          j%        |j        |j        j        |j        j        |j        j                   |d         tM          |          k    sJ |d         tM          |          k    sJ tM          |          }|j'        }tA          j!        |d
          }tA          j!        ||j                  }t)          j(        |j        |j        j#        |j        j#        |j        j#                   t)          j)        |	|||j        |
j        |j        |j        |j        |||           t          j        j                            |||f|          }t)          j*        |           |S )a+  Matrix-matrix product for CSR-matrix.

    math::
       C = alpha * A * B

    Args:
        a (cupyx.scipy.sparse.csr_matrix): Sparse matrix A.
        b (cupyx.scipy.sparse.csr_matrix): Sparse matrix B.
        alpha (scalar): Coefficient

    Returns:
        cupyx.scipy.sparse.csr_matrix

    r}   zspgemm is not available.r   r   r   r   r   r8   rR  r   r   )+r   r   r   r   r   r   r   r   rS   r   r   r   r   r   rE   r9   r   r   rb  r   r   spGEMM_createDescrrH   rA   r   r   r   r   CUSPARSE_SPGEMM_DEFAULTspGEMM_workEstimationr   r  r   r   r   r   spGEMM_computer  r   r   csrSetPointersspGEMM_copyspGEMM_destroyDescr)r   r   r   r   r   r   r   r  rP   r   r  r  r  spgemm_descrr   r   r   ro  r;  r   
buff1_sizebuff1
buff2_sizebuff2
c_num_rows
c_num_colsr   r   r   r   s                                 r   r}   r}     s    h'' 756666QV    q      a+677 I7>>tAwwGGHHHa+677 I7>>tAwwGGHHH!!!!!!!!wqzQWQZ+,,,7DAq7DAqQ""DAq!fG%%wqw%??A(**F""1%%E""1%%E""1%%E/11L5D5DLag...5E<)))0D%ag..J,DH 0dEJ
EJ	
JlAxA AJ K
EJ//E#dEJ
EJ	
JlJ
P P P
 )dEJ
EJ	
JlAxA AJ K
EJ//EdEJ
EJ	
JlJ
P P P
 aw///Jaw///JL'***E5:z'8'=%,15<3DF F F1:Z((((1:Z((((JJExHE3''I[((FUZ):IN<N#[_. . . dEJ
EJ	
Jl4 4 4 	%%vy(&C,3 	& 	5 	5A !,///Hr   )Nr   r   Fr   )Nr   r   T)Nr   r   FF)r   r   )FF)Nr   r   )r  )Nr   r   FF)r   TFFTF)F)rK  )r   TFFr  )N	functoolsr?   numpyrA   platformr   cupyr   cupy_backends.cuda.apir   r   r   r   cupy_backends.cuda.libsr   r   
cupy._corer   	cupy.cudar   r   r   r  r	   cupyx.scipy.sparser   objectr   rE   rJ   rX   r   infr   r   memoizer   r   r   rY   r   r\   r]   r^   r_   r`   rb   rc   rk   rl   rm   rn   ro   rp   r+  rq   rr   rt   r?  rs   ru   rv   rw   rx   rV  rX  rb  r  r  r  rf   rj   ry   rz   r{   r|   r~   r}   r   r   r   <module>r
     sQ                   4 4 4 4 4 4 6 6 6 6 6 6 9 9 9 9 9 9       ' ' ' ' ' ' ' ' ' ' ' '          = = = = =F = = =B  : : :  $]$}$ ]$ m	$
 }$ $ }$ $ M$ u//6$ u//6$  !$" #$$ |%$& |'$( |)$* |+$, $""G$ $ $ N"  ["  
D!	" 
 ["  k"  {"  "  {"  "  K"  K"  K"  "  "   {!" " {#" $ {%" & {'" ( :t$:t$#  z4 C"  "  "  J     (?C ? ? ? ?/ / / /d   >A A A AH5 5 5 5p< < < <~= = = =@B B B BJB B B BJf f f fR   @   @#; #; #;L#; #; #;L.( .( .( .(b0 0 00 0 0/ / /60 0 000 0 0</ / /00 0 000 0 0<' ' 'T+ + +\0 0 08      V   ,!@ !@ !@ !@ !@n !@ !@ !@H3 3 3 3 3n 3 3 33 3 3 3 3n 3 3 33 3 3 3 3n 3 3 3 @ @ @ @FI I I IX AF%*O& O& O& O&dDC DC DC DCNE E E EP% % % %P_0 _0 _0 _0DX X X X X Xr   