o
    X۷i:I                     @  sj   d dl mZ d dlZd dlZd dlmZ d dl	m
Z dd Zdd Zd	d
 Zdd Zdd ZdddZdS )    )annotationsN)cublas)devicec                 C  s0  ddl m} | jdkrtd| j|jdvr td|j| jd | jd kr.td| jd |jd krBtd	| j|j| j|jkrRtd
| j|j| j}|dkr\d}n|dkrcd}n|dkrjd}n|dkrqd}ntd| jt||d }t||d }t||d }|jd }|jdkr|jd nd}	| j	rt
j}
n| jrt
j}
ntd|j	stdt }tj|tjd}tjdtjd}||||| jj|}tj|| jd}||||| jj||jj|jj|jj tjj|| |||
||	| jj||jj|jj||jj
 tjj|| dS )a  Solve a linear matrix equation using cusolverDn<t>getr[fs]().

    Computes the solution to a system of linear equation ``ax = b``.

    Args:
        a (cupy.ndarray): The matrix with dimension ``(M, M)``.
        b (cupy.ndarray): The matrix with dimension ``(M)`` or ``(M, K)``.

    Returns:
        cupy.ndarray:
            The matrix with dimension ``(M)`` or ``(M, K)``.

    Note: ``a`` and ``b`` will be overwritten.
    r   cusolver   a.ndim must be 2 (actual: {}))   r   "b.ndim must be 1 or 2 (actual: {})r	   za must be a square matrix.shape mismatch (a: {}, b: {}).zdtype mismatch (a: {}, b: {})fsdFcDzzunsupported dtype (actual:{})getrf_bufferSizegetrfgetrsz'a must be F-contiguous or C-contiguous.zb must be F-contiguous.dtypeN)cupy_backends.cuda.libsr   ndim
ValueErrorformatshaper   	TypeErrorgetattr_f_contiguous_cublasCUBLAS_OP_N_c_contiguousCUBLAS_OP_T_deviceget_cusolver_handle_cupyempty_numpyint32dataptrlinalg_util3_check_cusolver_dev_info_if_synchronization_allowed)ab	_cusolverr   thelperr   r   nnrhstranshandledipivdinfolworkdwork r<   B/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/lapack.pygesv
   sl   


r>   c                 C  s\  ddl m} | jdkrtd| j|jdkrd}n|jdkr&|jd }ntd|j| jd |jd krBtd| j|j| j|jkrRtd| j|j| j}|d	kr\d
}n|dkrcd}n|dkrjd}n|dkrqd}ntd|t||d }t||d }tt|d }|dv rt||d }	t||d }
nt||d }	t||d }
tj	}|j
dv rtj}ntj}| j\}}t||}tjdtjd}tj||d}t }t }tjd|d}||kr| jdd} |jdd}||||| jj|}tj||d}||||| jj||jj|jj||jj	 tjj|| |	|tj||||| jj||jj|jj|}tj||d}|
|tj||||| jj||jj|jj||jj||jj tjj|
| ||tjtj|tj|||jj| jj||jj| |d| S |   j!jdd} |}|jdkr|fn||f}tj"||dd}||d|< ||||| jj|}tj||d}||||| jj||jj|jj||jj	 tjj|| ||tjtj|tj|||jj| jj||jj| |	|tj||||| jj||jj|jj|}tj||d}|
|tj||||| jj||jj|jj||jj||jj tjj|
| |S )a  Solves over/well/under-determined linear systems.

    Computes least-square solution to equation ``ax = b` by QR factorization
    using cusolverDn<t>geqrf().

    Args:
        a (cupy.ndarray): The matrix with dimension ``(M, N)``.
        b (cupy.ndarray): The matrix with dimension ``(M)`` or ``(M, K)``.

    Returns:
        cupy.ndarray:
            The matrix with dimension ``(N)`` or ``(N, K)``.
    r   r   r   r   r	   r
   r   zdtype mismatch (a: {}, b: {}).r   r   r   r   r   r   r   zunsupported dtype (actual: {})geqrf_bufferSizegeqrftrsmsdormqr_bufferSizeormqrunmqr_bufferSizeunmqrfdr   g      ?)orderNr   rH   )#r   r   r   r   r   r   r   r   r    r!   charr#   CUBLAS_OP_Cminr&   r'   r(   r)   r$   r%   get_cublas_handlearraycopyr*   r+   r,   r-   r.   CUBLAS_SIDE_LEFTCUBLAS_FILL_MODE_UPPERCUBLAS_DIAG_NON_UNITctypesconjTzeros)r/   r0   r1   r5   r   r2   geqrf_helperr@   rA   ormqr_helperrD   no_transr6   mr4   mn_mindev_infotaucusolver_handlecublas_handleonews_size	workspacebb	out_shaper<   r<   r=   gelsR   s   






re   c                 C  s  ddl m} dd l}|jdstdt| j|j}t|d}|dkr-|j}|j	}n+|dkr8|j
}|j}n |dkrC|j}|j}n|dkrN|j}|j}n
d	| j}t|| j|d
dd} tj| }| jdd  \}	}
tt| jd d }t }tj}tj|tjd}||||
|jj |	|jj | tj!j"#|| |j}|$ %||
dj|d
dd}tj|}|jdd  \}}tjdtjd}||||
||jj |	|jj ||jj |
 tj!j"#|| |$ %|S )Nr   r   potrsBatchedpotrsBatched is not availabler   r   r   r   Ddtype must be float32, float64, complex64 or complex128 (actual: {})CTrH   rO   r   r	   )&r   r   cupyx.cusolvercheck_availabilityRuntimeErrorr(   promote_typesr   spotrfBatchedspotrsBatcheddpotrfBatcheddpotrsBatchedcpotrfBatchedcpotrsBatchedzpotrfBatchedzpotrsBatchedr   r   astyper&   _core	_mat_ptrsr   intprodr$   r%   r    CUBLAS_FILL_MODE_LOWERr'   r)   r*   r+   r,   r-   r.   rT   reshape)r/   r0   r1   cupyxr   potrfBatchedrf   msgapldar4   
batch_sizer7   uplor\   b_shapebpldbr5   r<   r<   r=   _batched_posv   s^   r   c              
   C  s  ddl m} tjj}|| | ||  ||  | jdkr$t	| |S t
| j|j}t
|d}|dkr@|j}|j}|j}n4|dkrN|j}|j}|j}n&|dkr\|j}|j}|j}n|dkrj|j}|j}|j}n
d| j}t|| j|dd	d
} | j\}	}
t }tj }tj!dt
j"d}||||
| j#j$|	}tj!||d}||||
| j#j$|	|j#j$||j#j$ tjj%|| |j}|&|
dj|dd	d
}|j\}}||||
|| j#j$|	|j#j$||j#j$	 tjj%|| t'|&|S )aP  Solve the linear equations A x = b via Cholesky factorization of A,
    where A is a real symmetric or complex Hermitian positive-definite matrix.

    If matrix ``A`` is not positive definite, Cholesky factorization fails
    and it raises an error.

    Note: For batch input, NRHS > 1 is not currently supported.

    Args:
        a (cupy.ndarray): Array of real symmetric or complex hermitian
            matrices with dimension (..., N, N).
        b (cupy.ndarray): right-hand side (..., N) or (..., N, NRHS).
    Returns:
        x (cupy.ndarray): The solution (shape matches b).
    r   r   r   r   r   r   r   rh   Trj   r	   r   rl   )(r   r   r&   r,   r-   _assert_cupy_array_assert_stacked_2d_assert_stacked_squarer   r   r(   rp   r   spotrfspotrf_bufferSizespotrsdpotrfdpotrf_bufferSizedpotrscpotrfcpotrf_bufferSizecpotrszpotrfzpotrf_bufferSizezpotrsr   r   ry   r   r$   r%   r    r~   r'   r)   r*   r+   r.   r   ascontiguousarray)r/   r0   r1   r-   r   potrfpotrf_bufferSizepotrsr   r   r4   r7   r   r\   worksizerb   r   r   r5   r<   r<   r=   posv  sh   





r   c              
   C  s&  ddl m} tjj}|| | ||  ||  | jdkr%t	| ||S | j
d }|j
}|jdkr:|dddf }|jdksCJ d|j
d |ksNJ dt| j|j}t|d	}| j|d
d|j|d
d} }| jjsx| jjsxt| } | jjr| }|j| jk r| }n|  } |j|ks|jjst|}|d	kr|j}n"|dkr|j}n|dkr|j}n|dkr|j}n
d| j}	t|	t }
tjdtjd}||
|rtj ntj!| j
d |j
d | j"j#| j
d |j"j#|j
d |j"j#	 tjj$|| | jjr|j| jk r| }|%|S )ak   Implements lapack XPOTRS through cusolver.potrs. Solves linear system
    A x = b given the cholesky decomposition of A, namely L. Supports also
    batches of linear systems and more than one right-hand side (NRHS > 1).

    Args:
        L (cupy.ndarray): Array of Cholesky decomposition of real symmetric or
            complex hermitian matrices with dimension (..., N, N).
        b (cupy.ndarray): right-hand side (..., N) or (..., N, NRHS). Note that
            this array may be modified in place, as usually done in LAPACK.
        lower (bool): If True, L is lower triangular. If False, L is upper
            triangular.

    Returns:
        cupy.ndarray: The solution to the linear system. Note this may point to
            the same memory as b, since b may be modified in place.

    .. warning::
        This function calls one or more cuSOLVER routine(s) which may yield
        invalid results if input conditions are not met.
        To detect these invalid results, you can set the `linalg`
        configuration to a value that is not `ignore` in
        :func:`cupyx.errstate` or :func:`cupyx.seterr`.

    r   r   r   rl   r	   Nzb is not a vector or a matrixz.length of arrays in b does not match size of Lr   F)rO   r   r   r   rh   r   )&r   r   r&   r,   r-   r   r   r   r   _batched_potrsr   r(   rp   r   ry   flagsf_contiguousc_contiguousasfortranarraysizerT   r   r   r   r   r   r   r$   r%   r'   r)   r    r~   rQ   r*   r+   r.   r   )Lr0   lowerr1   r-   r4   r   r   r   r   r7   r\   r<   r<   r=   r   a  sr   









r   r   boolc                 C  sf  ddl m} dd l}|jdstd|j| jd ks J d|j}|j| jk r-|d }|jd d | jd d ks?J d	t| j	|j	}t|d
}| j
|ddd} |j
|ddd}| jjre|jjsiJ d| }|d
krt|j}n"|dkr||j}n|dkr|j}n|dkr|j}n
d| j	}t|t }	tjdtjd}
t| jd d }| jd }| }tj| }|jd }|dkr|d }ntj|jd d |j	dd}tj|d }t|jd D ]G}|dkr|d|f |d< ||	|rtjntj|d|j j!| jd |j j!|jd |
j j!|
 tj"j#$||
 |dkr)| |d|f< q| }q|%|S )Nr   r   rf   rg   r	   z8Batch dimension of b is different than that         of L).Nrk   z'Batch dimension of L and b do not matchr   ri   Frj   z"Unexpected non C-contiguous arraysr   r   r   rh   r   rl   ).r   rI   .)&r   r   rm   rn   ro   r   r   r(   rp   r   ry   r   r   rr   rt   rv   rx   r   r   r$   r%   r&   r'   r)   r}   rT   rz   r{   ranger    r~   rQ   r*   r+   r,   r-   r.   r   )r   r0   r   r1   r   r   r   rf   r   r7   r\   r   r4   L_pr5   b_tmpb_tmp_pir<   r<   r=   r     s   






r   )r   r   )
__future__r   numpyr(   cupyr&   r   r   r    	cupy.cudar   r$   r>   re   r   r   r   r   r<   r<   r<   r=   <module>   s    H ;Ob