
    
9j]                    L   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSK	rSSK
r
SSKrSSKrSSKrSSKrSSKJr  SSKrSqSqSqSqSq SqSSS.r\R2                  R5                  SS5      S	:H  rS(S
 jrS rS rS rS r S r!\R                  RE                  S5      r#\R                  RE                  S5      r$S r%S r&S r'S r(S r)S r*S r+S r,S r-S)S jr.S*S jr/S+S jr0S r1S,S jr2        S-S  jr3S.S! jr4S.S" jr5S# r6\Rn                  S$ 5       r8S% r9S/S& jr:S/S' jr;g)0z6
This file must not depend on any other CuPy modules.
    )annotationsN)Any )ncclcutensorCUPY_DEBUG_LIBRARY_LOAD01c                    [         (       aB  [        R                  R                  SU  S35        [        R                  R	                  5         g g )Nz[CUPY_DEBUG_LIBRARY_LOAD] 
)_debugsysstderrwriteflush)msgs    K/home/wildlama/miniconda3/lib/python3.13/site-packages/cupy/_environment.py_logr   E   s8    v

5cU"=>

     c                 6    [         S:X  a
  [        5       q [         $ Nr   )
_cuda_path_get_cuda_path r   r   get_cuda_pathr   K        R#%
r   c                 6    [         S:X  a
  [        5       q [         $ r   )
_nvcc_path_get_nvcc_pathr   r   r   get_nvcc_pathr    S   r   r   c                 6    [         S:X  a
  [        5       q [         $ r   )
_rocm_path_get_rocm_pathr   r   r   get_rocm_pathr$   [   r   r   c                 6    [         S:X  a
  [        5       q [         $ r   )_hipcc_path_get_hipcc_pathr   r   r   get_hipcc_pathr(   c   s     b%'r   c                 6    [         S:X  a
  [        5       q [         $ r   )	_cub_path_get_cub_pathr   r   r   get_cub_pathr,   k   s     B!O	r   linuxwin32c                    [         R                  R                  S5      n U c  g[        (       ao  [        R
                  " 5       nUS:X  a-  [         R                  R                  S5      (       a  U S3nOSnOU S3n[         R                  R                  U SU5      nO.[        (       a!  [         R                  R                  U S5      nO e[         R                  R                  U5      (       a  U$ S$ )	zThis works since CUDA 12.0+.CONDA_PREFIXNaarch64z/etc/nv_tegra_releasez-linuxz
sbsa-linuxtargetsLibrary)
osenvironget_PLATFORM_LINUXplatformmachinepathexistsjoin_PLATFORM_WIN32)conda_prefixplatarch	cuda_paths       r   _get_conda_cuda_pathrB   w   s    ::>>.1L!9ww~~566v#V6?DGGLLy$?		GGLLy9	uy119;t;r   c                 P   [        5       n U b  U S   S:X  a  [        5       nUb  U$ SSKJnJn   U" S5      n[
        R                  R                  [
        R                  R                  [
        R                  R                  UR                  5      5      5      nUR                  S:X  a$  [        5       nUR                  U5      (       d   eU$ UR                  S:X  aI  [        R                  " SU5      (       a,  [        (       a  [
        R                  R                  U5      nU$ g [        (       aB  [
        R                  R                  U5      S:X  a  [
        R                  R                  U5      nU$ ! U a     Of = f[
        R                  R!                  S	5      (       a  g	g )
N	packagingcondar   load_nvidia_dynamic_libDynamicLibNotFoundErrornvrtczsite-packageszsite-packages.*nvidia.*cu\d{2}binz/usr/local/cuda)get_preload_configrB   cuda.pathfinderrG   rH   r4   r:   dirnamerealpathabs_path	found_via
startswithresearchr=   basenamer;   )configconda_cuda_pathrG   rH   rI   rA   s         r   r   r      sS     !Ff[1W<.0&"":'0 GGOOGGOOBGG,,U^^<=?	??g%24O"--i8888""__/ yy:IFF"? "	 :I   ?rww//	:eC	2I1 # 6 
ww~~'(( s   E6 6E?>E?c                     [         R                  R                  SS 5      n U b  U $ SSKJn  U" S5      n U b  U $ [        5       nUc  g [        R                  " S[         R                  R                  US5      S9$ )NNVCCr   )find_nvidia_binary_utilitynvccrJ   r:   )
r4   r5   r6   rL   rY   r   shutilwhichr:   r<   )	nvcc_pathrY   rA   s      r   r   r      so    

vt,I ;*62I I<<RWW\\)U%CDDr   c                    [         R                  R                  SS5      n [         R                  R	                  U 5      (       a  U $ [
        R                  " S5      nUb<  [         R                  R                  [         R                  R                  U5      5      $ [         R                  R	                  S5      (       a  gg )N	ROCM_HOMEr   hipccz	/opt/rocm)r4   r5   r6   r:   r;   r\   r]   rM   )	rocm_path
hipcc_paths     r   r#   r#      s    

{B/I	ww~~i   g&Jwwrwwz:;; 
ww~~k""r   c                     [        5       n U c  g [        R                  " S[        R                  R                  U S5      S9$ )Nra   rJ   r[   )r$   r\   r]   r4   r:   r<   )rb   s    r   r'   r'      s4     I<<bggll9e&DEEr   c                    SSK Jn   [        R                  R	                  [        R                  R                  [        5      5      nU R                  (       dK  [        R                  R                  [        R                  R                  US5      5      (       a  SnU$ S n U$ [        5       nUbF  [        R                  R                  [        R                  R                  US5      5      (       a  SnU$ S nU$ )Nr   )runtimez_core/include/cupy/_cccl/cubz<bundle>zinclude/hipcubz<ROCm>)cupy_backends.cuda.apirf   r4   r:   rM   abspath__file__is_hipisdirr<   r$   )rf   current_dirr*   rb   s       r   r+   r+      s    .''//"''//(";<K>>77==[*HIK K"I  I  "O	 RWW]]Y(89&; &; !I  Ir   c                    [         R                  R                  S5      (       Ga]  [        5       n U S L=(       a    U S   S:H  n[	        5       nUb+  U(       a  UnO@[
        R                  R                  US5      nOS nU(       d  [        R                  " S5        [        SR                  U5      5        [
        R                  R                  [        5       SSS	5      n[
        R                  R                  U5      (       a  [        S
R                  U5      5        O[        SR                  U5      5        S nS[         R                  ::  a  Ub  [        SR                  U5      5        [
        R                  " U5        [
        R                  R                  US5      n[
        R                  R!                  U5      (       a$  [        SU 35        [
        R                  " U5        Ub1  [        SR                  U5      5        [
        R                  " U5        g g Ubb  [        SR                  U5      5        [
        R"                  R%                  SS5      nU[
        R&                  -   U-   [
        R"                  S'   g g g )Nr.   rD   rE   rJ   zZCUDA path could not be detected. Set CUDA_PATH environment variable if CuPy fails to load.zCUDA_PATH: {}cupy.datalibzWheel shared libraries: {}z%Not wheel distribution ({} not found))      zAdding DLL search path: {}x64z&Adding DLL search path (for CUDA 13): zAdding to PATH: {}PATHr   )r   r8   rQ   rK   r   r4   r:   r<   warningswarnr   formatget_cupy_install_pathrk   version_infoadd_dll_directoryr;   r5   r6   pathsep)rU   is_condarA   cuda_bin_pathwheel_libdircuda_bin_x64_pathr:   s          r   _setup_win32_dll_directoryr   
  s     ||w''#%$&KF;,?7,J "O	  ) "Y > M%& 	_##I./ ww||!#VWe=77==&&-44\BC8?? LS%%%(188GH$$]3$&GGLL$F!77>>"344A-.0 1(():;'188FG$$\2 (
 ')00>?zz~~fb1%1BJJ%>%E

6" ([ (r   c                     [         R                  R                  [         R                  R                  [         R                  R	                  [
        5      S5      5      $ )Nz..)r4   r:   rh   r<   rM   ri   r   r   r   rx   rx   @  s6    77??
RWW__X.57 7r   c                     [         R                  R                  SS5      n U c  [         R                  R	                  S5      $ [         R                  R                  U 5      $ )ab  Returns the directory where CUDA external libraries are installed.

This environment variable only affects wheel installations.

Shared libraries are looked up from
`$CUPY_CUDA_LIB_PATH/$CUDA_VER/$LIB_NAME/$LIB_VER/{lib,lib64,bin}`,
e.g., `~/.cupy/cuda_lib/11.2/nccl/2.8.0/lib64/libnccl.so.2.8.0`.

The default $CUPY_CUDA_LIB_PATH is `~/.cupy/cuda_lib`.
CUPY_CUDA_LIB_PATHNz~/.cupy/cuda_lib)r4   r5   r6   r:   
expanduserrh   )cupy_cuda_lib_paths    r   get_cupy_cuda_lib_pathr   F  sJ     (<dC!ww!!"45577??-..r   c                 2    [         c  [        S5      q [         $ )Nz_wheel.json)_preload_config_get_json_datar   r   r   rK   rK   W  s    (7r   c                   [         R                  R                  [        5       SSU 5      n[         R                  R	                  U5      (       d  g [        U5       n[        R                  " U5      sS S S 5        $ ! , (       d  f       g = f)Nrn   ro   )r4   r:   r<   rx   r;   openjsonload)nameconfig_pathfs      r   r   r   ^  sW    '',,$8K77>>+&&	k	ayy| 
		s   A;;
B	c                    [        5       nUb	  US   S:X  a  [        SU  S35        gU [        ;  a  [        SU  35      eX;  a  [        SU  S35        g[        U    b  [        S	U  35        gg
)z(Returns if the preload can be attempted.rD   rE   zNot preloading z( as this is not a pip wheel installationFzUnknown preload library: zPreload z not configured in wheelzPreload already attempted: T)rK   r   _preload_libsAssertionError)rp   rU   s     r   _can_attempt_preloadr   g  s      !FF;/7: 	se#KLM
-8>??
xu456S%*3%01r   c                   [        SU  35        [        U 5      (       d  g0 [        U '   SSKJnJn   U" U 5      nU[        U    S'   [        U  SU 35        g! U a  n[        U  SU 35         SnAOSnAff = f[        5       nUS   n[        S	R                  U5      5        [        5       n[        S
R                  U5      5        XP   S   nXP   S   n	U	 GH  n
[        SU  SU SU
 S35        S Vs/ s H(  n[        R                  R                  XuS   XUU
5      PM*     Os  snf nnU S:X  a  XP   S   n[        XS   5      U-   nOU S:X  a  XP   S   n[        XS   5      U-   nU H  n[        R                  R                  U5      (       d  [        SR                  U5      5        MC   [        SU 35        [        R                   " U5      [        U    U'   [        S5          GM  ! ["         aM  n[%        U5      R&                  nSU SU SU S3n[        U5        [(        R*                  " U5         SnAM  SnAff = f   [        SR                  U
5      5        [        SU
 S35         [        R                   " U
5      [        U    U
'   [        S5        GM  ! ["         a  n[        SU  SU 35         SnAGM  SnAff = f   g)zPreload dependent shared libraries.

The preload configuration file (cupy/.data/_wheel.json) will be added
during the wheel build process.
z"Preloading triggered for library: Nr   rF   
pathfinderz is loaded by cuda-pathfinder: z) could not be loaded by cuda-pathfinder: cudaz$CuPy wheel package built for CUDA {}zCuPy CUDA library directory: {}version	filenameszLooking for z	 version  ())rp   lib64rJ   r   min_pypi_versionr   z"Rejected candidate (not found): {}zTrying to load Loadedz CuPy failed to preload library (z): zFile {} could not be foundz from default search pathzLibrary z could not be preloaded: )r   r   r   rL   rG   rH   rK   rw   r   r4   r:   r<   _get_cutensor_from_wheel_get_nccl_from_wheelr;   ctypesCDLL	Exceptiontype__name__ru   rv   )rp   rG   rH   	loaded_dlerU   cuda_versionr   r   r   filenamexlibpath_candsr   libpathe_typer   s                    r   _preload_libraryr     s    	-cU	34$$M#:	+C0	 ,5c<(u3I;?@ # C 	u=aSABBC  !F&>L	/	6	6|	DE/1	*	1	12D	EFk)$GK(I|C5	'"XJa@A -	. - GGLL"6NC! -	. .
 *%{+=>()9&>J  F]%{+=>$%5f~F  %G77>>'**9@@IJ#wi01.4kk'.Bc"7+X #a))6wishb1&  S	c""# %& -44X>? ?8*,EFGC/5{{8/Dc"8,X C xu$=aSABBCc sN   A A7A22A74/D$.9G++
I5AH==I1+J
K)KKc                    [         R                  " SU SS9n[        US   5      n[        U5      S:  a  [        US   5      OSn[        U5      S:  a  [        US   5      OSnX#U4$ )Nz[^\d]rq   )maxsplitr         )rR   splitintlen)r   partsmajorminorpatchs        r   _parse_versionr     s^    HHXw3EaME Z1_CaM!E Z1_CaM!Er   c           	     0   UR                  S5      u  p4U  SU 3n [        R                  R                  U5      n[        UR                  5      n[        U5      nUS   US   :H  =(       a/    US   US   :  =(       d    US   US   :H  =(       a    US   US   :  n	U	(       d!  [        U  SU S	U S
UR                   35        g[        U  SU SUR                   35        U$ ! [        R                  R                   a    [        U  SU S35         gf = f)z
Returns the distribution of the given package name and version
installed via pip (e.g., cutensor-cuXX).
If the package is not found or incompatible, returns None.
.z-cuz wheel package (z) not installedNr   r   r   z) incompatible: expected z, found z	) found: )r   	importlibmetadatadistributionPackageNotFoundErrorr   r   r   )
package_prefixr   r   cuda_major_ver_pkgdistactualexpectedis_compatibles
             r   _find_compatible_wheelr     s=    

3NC/
0C!!..s3
 DLL)Fg&Hq	Xa[  	
1I# DAY(1+%B&)x{*B	  /u 5 	$,,9 	:N+C5	$,,	HIK% 22 /uODEs   C# #/DDc                l   [        SX5      nUc  / $ [        R                  S:X  aM  UR                  SU R	                  S5      S    35      UR                  SU R	                  S5      S    35      /nO"UR                  S5      UR                  S5      /nU Vs/ s H  n[        U5      PM     sn$ s  snf )	zl
Returns the list of shared library path candidates for cuTENSOR
installed via Pip (cutensor-cuXX package).
r   r-   zcutensor/lib/libcutensor.so.r   r   zcutensor/lib/libcutensorMg.so.zcutensor\bin\cutensor.dllzcutensor\bin\cutensorMg.dll)r   r   r8   locate_filer   strr   r   r   shared_libsrp   s        r   r   r   	  s    
 "*g<D|	
||w.w}}S/A!/D.EF 0s1CA1F0GH	
 :;<=
 !,,CH,,,s   B1c                    [         R                  S:w  a  / $ [        SX5      nUc  / $ UR                  SU R	                  S5      S    35      /nU Vs/ s H  n[        U5      PM     sn$ s  snf )zk
Returns the list of shared library path candidates for NCCL
installed via Pip (nvidia-nccl-cuXX package).
r-   znvidia-ncclznvidia/nccl/lib/libnccl.so.r   r   )r   r8   r   r   r   r   r   s        r   r   r   "  sw    
 ||w	!-?D|	)'--*<Q*?)@A	
K
 !,,CH,,,s   A+c                   [        5       nUb  X;  a  g US   S:X  a  US   nUR                  S5      S   nU S:X  a&  US   S   n[        U5      S   nSU S	U S
US-    S3nOOU S:X  a&  US   S   n[        U5      S   nSU S	U S
US-    S3nO#[        SU  35      eUS   S:X  a  SU  3nO[        e[        R
                  " SU  S[        U5      R                   S[        U5       SU S3	5        g )NrD   pipr   r   r   r   r   zpip install "cutensor-cuz>=z,<r   "r   zpip install "nvidia-nccl-cuzUnknown library: rE   zconda install -c conda-forge r   z' library could not be loaded.

Reason: r   z')

You can install the library by:
  $ )	rK   r   r   r   ru   rv   r   r   r   )rp   excrU   r   
cuda_majorr   r   cmds           r   _preload_warningr   4  sE   !F~*ke#f~ZZ_Q'
*Z();<G"7+A.E,ZL7)2eAgYaPCF]Vn%78G"7+A.E/
|2gYbq	QRSC #4SE!:;;			'-cU3MM  		c		Bs3xj ) 	E 
 r   c                    / SQn [         R                  " U [         R                  [         R                  SS9nUR                  S:w  a  [        SU S35      e[        R                  " SUR                  [        R                  5      nUc  [        SU  S	35      eUR                  S
5      R                  5       n[        S U 5       5      $ )N)ra   z-xhipz-Ez-vz	/dev/nullT)stdoutr   textr   zSubprocess command 'z	' failed.zA#include <\.\.\.> search starts here:\n(.*?)\nEnd of search list.zECould not find any hipcc default include directories using command: 'zK'. Possible errors are changed hipcc output format, or hipcc was not found.r   c              3  n   #    U  H+  oR                  5       (       d  M  UR                  5       v   M-     g 7f)N)strip).0include_paths     r   	<genexpr>*_get_hipcc_include_dirs.<locals>.<genexpr>l  s.      )L-?-?-A 	)s   55)
subprocessrunDEVNULLPIPE
returncodeRuntimeErrorrR   rS   r   Sgroup
splitlinestuple)r   procmatchesinclude_pathss       r   _get_hipcc_include_dirsr   T  s    
9C>>#j&8&8!+t=D !1$yABBiiL
G  ;<
 	
 MM!$//1M )  r   c                 R   Sn / nU  H4  n [         R                  R                  U5        UR                  U5        M6     S[        U5      :  a5  SR                  [        U5      5      n[        R                  " SU S35        g g ! [         R                  R                   a     M  f = f)N)rn   zcupy-cuda12xzcupy-cuda13xzcupy-rocm-7-0zcupy-rocm-7-1zcupy-cuda11xzcupy-cuda102zcupy-cuda110zcupy-cuda111zcupy-rocm-4-3zcupy-rocm-5-0zcupy-cuda112zcupy-cuda113zcupy-cuda114zcupy-cuda115zcupy-cuda116zcupy-cuda117zcupy-rocm-4-0zcupy-rocm-4-2zamd-cupyr   z, z
--------------------------------------------------------------------------------

  CuPy may not function correctly because multiple CuPy packages are installed
  in your environment:

    a/  

  Follow these steps to resolve this issue:

    1. For all packages listed above, run the following command to remove all
       existing CuPy installations:

         $ pip uninstall <package_name>

      If you previously installed CuPy via conda, also run the following:

         $ conda uninstall cupy

    2. Install the appropriate CuPy package.
       Refer to the Installation Guide for detailed instructions.

         https://docs.cupy.dev/en/stable/install.html

--------------------------------------------------------------------------------
)
r   r   r   appendr   r   r<   sortedru   rv   )knowncupy_installedkcupy_packages_lists       r   _detect_duplicate_installationr   r  s    E< N	++A.!!!$  	3~!YYvn'=>    	  !!66 		s   0BB&%B&c                     Sn [         R                  S:X  a   U [        5       -  n U $ U $ ! [         a)  nU S[	        U5      R
                   SU 3-  n  S nAU $ S nAff = f)Na  Failed to import CuPy.

If you installed CuPy via wheels (cupy-cudaXXX or cupy-rocm-X-X), make sure that the package matches with the version of CUDA or ROCm installed.

On Linux, you may need to set LD_LIBRARY_PATH environment variable depending on how you installed CUDA/ROCm.
On Windows, try setting CUDA_PATH environment variable.

Check the Installation Guide for details:
  https://docs.cupy.dev/en/latest/install.htmlr.   z%

The cause could not be identified: : )r   r8   _diagnose_win32_dll_loadr   r   r   )r   r   s     r   _diagnose_import_errorr     sv    	2C ||w	+--C J3J  	97##$Bqc+C
 J	s   ) 
AAAc                    [        S5      n U c  gSSKJn  [        R                  " S5      nUR                  UR
                  UR                  /UR                  l        UR                  UR                  l	        SSS[        5        3S/n[        R                  " S5      nU S	    Ha  n [        R                  " U5      nUR                  UR                  U[        U5      5        UR                  S
U SUR                   35        Mc     SR)                  U5      $ ! [          a    UR                  S
U S35         M  ["         a9  nUR                  S
U S[%        U5      R&                   SU S35         S nAM  S nAff = f)Nz_depends.jsonr   r   )wintypeskernel32zCUDA Path: zDLL dependencies:i   dependsz  z -> z -> not foundz -> error (r   r   r   )r   r   r   r   HANDLELPWSTRDWORDGetModuleFileNameWargtypesrestyper   create_unicode_buffer_handler   r   valueFileNotFoundErrorr   r   r   r<   )r   r   r   linesfilepathr   dllr   s           r   r   r     sW   _-G{{:&H(..,:H(*2..H' 	

mo&'	E ++E2H	"	I++d#C''Xs8}MLL2dV4'789	 # 99U ! 	3LL2dV=12 	ILL2dV;tAw/?/?.@1#QGHH	Is   )ADE?=	E?.E::E?)r   r   returnNone)r  dict[str, Any] | None)r   r   r  r  )rp   r   r  bool)r   r   r  ztuple[int, int, int])r   r   r   r   r   r   r  z&importlib.metadata.Distribution | None)r   r   r   r   r  z	list[str])r  r   )<__doc__
__future__r   r   	functoolsimportlib.metadatar   r   r4   os.pathr8   rR   r\   r   r   typingr   ru   r   r   r"   r&   r*   r   r   r5   r6   r   r   r   r    r$   r(   r,   rQ   r7   r=   rB   r   r   r#   r'   r+   r   rx   r   rK   r   r   r   r   r   r   r   r   cacher   r   r   r   r   r   r   <module>r     s}   #     	   	   
   


	>  
 
13	73	> ,,))'2,,))'2<.1hE($F.3Fl7/"8ZCz&)14+B-2-$@  :EP2r   