
    `i0                     F    d dl Zd dlZd dlmZ d dlmZ	 d Z
d Zd Zd ZdS )    N)cublas)devicec                    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    r-t          d	                    | j        |j                            | j        |j        k    r-t          d
                    | j        |j                            | j        }|dk    rd}nB|dk    rd}n9|dk    rd}n0|dk    rd}n't          d                    | j                            t          ||dz             }t          ||dz             }t          ||dz             }|j        d         }|j        dk    r|j        d         nd}	| j	        rt          j        }
n#| j        rt          j        }
nt          d          |j	        st          d          t          j                    }t!          j        |t$          j                  }t!          j        dt$          j                  } ||||| j        j        |          }t!          j        || j                  } ||||| 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dworks                   `/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/lapack.pygesvr>      sH    >=====v{{8??GGHHHvV=DDQVLLMMMwqzQWQZ5666wqzQWQZ9113 3 	3w!'7qw002 2 	2GE||	#	#	#7>>qwGGHHHY$6 677FIq7{++EIq7{++E	
A1171::!D D#	
 D#BCCC? 42333(**FK...EK...EF61aQ//EKQW---E	E&!Q
Auz~uz~
*.  	LJJu   
E&%D!&*a
*.!&*a9 9 9	LJJu        c                 6   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    r-t          d                    | j        |j                            | j        |j        k    r-t          d                    | j        |j                            | j        }|d	k    rd
}n=|dk    rd}n4|dk    rd}n+|dk    rd}n"t          d                    |                    t          ||dz             }t          ||dz             }t          t          |dz             }|dv r't          ||dz             }	t          ||dz             }
n&t          ||dz             }	t          ||dz             }
t          j	        }|j
        dv rt          j        }nt          j        }| j        \  }}t          ||          }t          j        dt           j                  }t          j        ||          }t%          j                    }t%          j                    }t!          j        d|          }||k    r|                     d          } |                    d          } ||||| j        j        |          }t          j        ||          } ||||| j        j        ||j        j        |j        j        ||j        j        	  	         t          j        j                            ||            |	|t          j        ||||| j        j        ||j        j        |j        j        |          }t          j        ||          } |
|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!                            d          } |}|j        dk    r|fn||f}t          j"        ||d          }||d|<    ||||| j        j        |          }t          j        ||          } ||||| 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        ||          } |
|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   rJ   )#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)r0   r1   r2   r6   r   r3   geqrf_helperrB   rC   ormqr_helperrF   no_transr7   mr5   mn_mindev_infotaucusolver_handlecublas_handleonews_size	workspacebb	out_shapes                            r=   gelsrf   P   s]    >=====v{{8??GGHHHv{{	
1wqz=DDQVLLMMMwqzQWQZ9113 3 	3w!'9113 3 	3 GE||	#	#	#9@@GGHHH9a*<&<==LIq7{++E7AJ''DDyyy!.@*@AA	1w;//y!.@*@AA	1w;//"HzT##7DAqAYYF{1FL111H
+fE
*
*
*C133O-//M
,s%
(
(
(CAvvFFFFFF ,1afj!DDKu555	oq!QVZCHLn '8=+<	> 	> 	>NN8	 	 	 ,W5uavFJ38<Q8 8 Ku555	ow74afj!SX\16:qn '8=+<	> 	> 	> 	NN8	 	 	 	]G4+X)64Z_afj!QVZ	< 	< 	<
 !u FFHHJOO#O&&FaKKQDDaY	K	c:::"1" ,1afj!DDKu555	oq!QVZCHLn '8=+<	> 	> 	>NN8	 	 	 	]G4+U)1dZ_afj!QVZ	< 	< 	< ,W5xDAFJ38<Q@ @ Ku555	ow71dafj!SX\16:qn '8=+<	> 	> 	> 	NN8	 	 	 r?   c                    ddl m} dd l}|j                            d          st	          d          t          j        | j        |j                  }t          j        |d          }|dk    r|j        }|j	        }nh|dk    r|j
        }|j        }nS|dk    r|j        }|j        }n>|dk    r|j        }|j        }n)d	                    | j                  }t#          |          |                     |d
d          } t&          j                            |           }| j        dd          \  }	}
t/          t          j        | j        d d                             }t3          j                    }t6          j        }t'          j        |t
          j                  } ||||
|j        j         |	|j        j         |           t&          j!        j"        #                    ||           |j        }|$                                %                    ||
d                              |d
d          }t&          j                            |          }|j        dd          \  }}t'          j        dt
          j                  } ||||
||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rJ   rP   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/   rU   reshape)r0   r1   r2   cupyxr   potrfBatchedrh   msgapldar5   
batch_sizer8   uplor]   b_shapebpldbr6   s                      r=   _batched_posvr      s   ======>,,^<< <:;;; !'22E ,,E|| . .	# . .	# . .	# . .%vag 	oo	c--A			q	!	!BWRSS\FCV["..//J(**F)D{:V\:::H Lq"'+sHM4E  	LJJh      gG	Q++225$2OOA			q	!	!BIC{1FL111H Lq$S"'+s"J0 0 0	LJJh      6688G$$$r?   c                    ddl m} t          j        j        }|                    | |           |                    |            |                    |            | j        dk    rt          | |          S t          j        | j        |j                  }t          j        |d          }|dk    r|j        }|j        }|j        }n}|dk    r|j        }|j        }|j        }na|dk    r|j        }|j        }|j        }nE|dk    r|j        }|j        }|j        }n)d                    | j                  }t5          |          |                     |dd	
          } | j        \  }	}
t;          j                    }t>          j         }t          j!        dt          j"                  } ||||
| j#        j$        |	          }t          j!        ||          } ||||
| j#        j$        |	|j#        j$        ||j#        j$                   t          j        j        %                    ||           |j        }|&                    |
d                              |dd	
          }|j        \  }} ||||
|| j#        j$        |	|j#        j$        ||j#        j$        	  	         t          j        j        %                    ||           t          j'        |&                    |                    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   ri   Trk   r
   r   rm   )(r   r   r'   r-   r.   _assert_cupy_array_assert_stacked_2d_assert_stacked_squarer   r   r)   rq   r   spotrfspotrf_bufferSizespotrsdpotrfdpotrf_bufferSizedpotrscpotrfcpotrf_bufferSizecpotrszpotrfzpotrf_bufferSizezpotrsr   r   rz   r   r%   r&   r!   r   r(   r*   r+   r,   r/   r   ascontiguousarray)r0   r1   r2   r.   r   potrfpotrf_bufferSizepotrsr   r   r5   r8   r   r]   worksizerc   r   r   r6   s                      r=   posvr     s     >=====LE	Q"""	Q	  ###vzzQ""" !'22E ,,E|| $6 	# $6 	# $6 	# $6 %vag 	oo	c--AWFC(**F)D{1FL111HaSAAHHE222I 
E&$16:sIN,>
HM%' ' '	LJJx   gG			!RSt<<AIC 
E&$4S!&*c
-
  	LJJx   "199W#5#5666r?   )numpyr)   cupyr'   r   r   r!   	cupy.cudar   r%   r>   rf   r   r    r?   r=   <module>r      s            5 5 5 5 5 5 ' ' ' ' ' 'E E EPB B BJ8% 8% 8%vL7 L7 L7 L7 L7r?   