o
    0 i0                     @   sL   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S )    N)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:{})Z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getattrZ_f_contiguous_cublasCUBLAS_OP_NZ_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handleZdipivZdinfoZlworkZdwork r4   X/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/cupyx/lapack.pygesv   sl   


r6   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: {})Zgeqrf_bufferSizegeqrftrsmsdZormqr_bufferSizeormqrZunmqr_bufferSizeZunmqrfdr   g      ?)orderN)r   r<   )#r   r   r   r   r   r   r   r   r   r   charr   ZCUBLAS_OP_Cminr"   r#   r$   r%   r    r!   Zget_cublas_handlearraycopyr&   r'   r(   r)   r*   ZCUBLAS_SIDE_LEFTZCUBLAS_FILL_MODE_UPPERZCUBLAS_DIAG_NON_UNITctypesconjTZzeros)r+   r,   r-   r1   r   r.   Zgeqrf_helperr7   r8   Zormqr_helperr:   Zno_transr2   mr0   Zmn_mindev_infotauZcusolver_handleZcublas_handleoneZws_size	workspaceZbbZ	out_shaper4   r4   r5   gelsP   s   






rI   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   potrsBatchedzpotrsBatched is not availabler   r   r   r   Ddtype must be float32, float64, complex64 or complex128 (actual: {})CTr<   r@   r   r   )&r   r   Zcupyx.cusolverZcheck_availabilityRuntimeErrorr$   promote_typesr   ZspotrfBatchedZspotrsBatchedZdpotrfBatchedZdpotrsBatchedZcpotrfBatchedZcpotrsBatchedZzpotrfBatchedZzpotrsBatchedr   r   astyper"   Z_coreZ	_mat_ptrsr   intprodr    r!   r   CUBLAS_FILL_MODE_LOWERr#   r%   r&   r'   r(   r)   r*   rB   reshape)r+   r,   r-   Zcupyxr   ZpotrfBatchedrJ   msgZapldar0   Z
batch_sizer3   uplorE   b_shapebpldbr1   r4   r4   r5   _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   rK   TrM   r   r   rO   )(r   r   r"   r(   r)   Z_assert_cupy_arrayZ_assert_stacked_2dZ_assert_stacked_squarer   r]   r$   rQ   r   ZspotrfZspotrf_bufferSizeZspotrsZdpotrfZdpotrf_bufferSizeZdpotrsZcpotrfZcpotrf_bufferSizeZcpotrsZzpotrfZzpotrf_bufferSizeZzpotrsr   r   rR   r   r    r!   r   rU   r#   r%   r&   r'   r*   rV   Zascontiguousarray)r+   r,   r-   r)   r   ZpotrfZpotrf_bufferSizeZpotrsrW   rX   r0   r3   rY   rE   ZworksizerH   rZ   r\   r1   r4   r4   r5   posv  sh   





r^   )numpyr$   Zcupyr"   r   r   r   Z	cupy.cudar   r    r6   rI   r]   r^   r4   r4   r4   r5   <module>   s    H ;