
    `iS              	          d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZmZmZmZ ddlZdadadadada	 daddddZej                            dd          dk    Zd	ed
dfdZd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d
eeeef                  fdZ+ded
eeeef                  fdZ,ded
e-fdZ.d Z/ded
ee0e0e0f         fd Z1d!eded"ed
eej2        j3                 fd#Z4ded"ed
e5e         fd$Z6ded"ed
e5e         fd%Z7d& Z8d'e0d(e0d
ee         fd)Z9d* Z:d
efd+Z;d
efd,Z<dS )-z6
This file must not depend on any other CuPy modules.
    N)AnyDictListOptionalTuple )cudnnncclcutensorCUPY_DEBUG_LIBRARY_LOAD01msgreturnc                     t           rCt          j                            d|  d           t          j                                         d S d S )Nz[CUPY_DEBUG_LIBRARY_LOAD] 
)_debugsysstderrwriteflush)r   s    e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/_environment.py_logr   C   sP     
=c===>>>
     c                  B    t           dk    rt                      a t           S Nr   )
_cuda_path_get_cuda_path r   r   get_cuda_pathr    I         R#%%
r   c                  B    t           dk    rt                      a t           S r   )
_nvcc_path_get_nvcc_pathr   r   r   get_nvcc_pathr%   Q   r!   r   c                  B    t           dk    rt                      a t           S r   )
_rocm_path_get_rocm_pathr   r   r   get_rocm_pathr)   Y   r!   r   c                  B    t           dk    rt                      a t           S r   )_hipcc_path_get_hipcc_pathr   r   r   get_hipcc_pathr-   a   s      b%''r   c                  B    t           dk    rt                      a t           S r   )	_cub_path_get_cub_pathr   r   r   get_cub_pathr1   i   s     B!OO	r   c                  n   t           j                            dd          } t           j                            |           r| S t          j        d          }|<t           j                            t           j                            |                    S t           j                            d          rdS d S )N	CUDA_PATHr   nvccz/usr/local/cudaosenvirongetpathexistsshutilwhichdirname)	cuda_path	nvcc_paths     r   r   r   q   s    
{B//I	w~~i    V$$Iwrwy99::: 
w~~'(( !  4r   c                      t           j                            dd           } | | S t                      }|d S t	          j        dt           j                            |d                    S )NNVCCr4   binr9   )r6   r7   r8   r    r;   r<   r9   join)r?   r>   s     r   r$   r$      s^    
vt,,I It<RW\\)U%C%CDDDDr   c                  n   t           j                            dd          } t           j                            |           r| S t          j        d          }|<t           j                            t           j                            |                    S t           j                            d          rdS d S )N	ROCM_HOMEr   hipccz	/opt/rocmr5   )	rocm_path
hipcc_paths     r   r(   r(      s    
{B//I	w~~i    g&&Jwrwz::;;; 
w~~k"" {4r   c                      t                      } | d S t          j        dt          j                            | d                    S )NrG   rB   rC   )r)   r;   r<   r6   r9   rD   )rH   s    r   r,   r,      s=     It<bgll9e&D&DEEEEr   c                     ddl m}  t          j                            t          j                            t                              }| j        sCt          j                            t          j        	                    |d                    rd}nUd }nRt                      }|@t          j                            t          j        	                    |d                    rd}nd }|S )Nr   )runtimez_core/include/cupy/_cccl/cubz<bundle>zinclude/hipcubz<ROCm>)cupy_backends.cuda.apirL   r6   r9   r=   abspath__file__is_hipisdirrD   r)   )rL   current_dirr/   rH   s       r   r0   r0      s    ......'//"'//(";";<<K> 7==[*HIIK K 	"IIII "OO	 RW]]Y(899&; &;  !IIIr   c                  *   t           j                            d          rpt                      } | d uo| d         dk    }t	                      }|&|r|}n9t
          j                            |d          }nd }|st          j	        d           t          d                    |                     t
          j                            t                      ddd	          }t
          j                            |          r#t          d
                    |                     n$t          d                    |                     d }dt           j        k    r|t          d                    |                     t          j        |           t
          j                            |d          }t
          j                            |          r&t          d|            t          j        |           |8t          d                    |                     t          j        |           d S d S |et          d                    |                     t
          j                            dd          }|t
          j        z   |z   t
          j        d<   d S d S d S )Nwin32	packagingcondarB   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   platform
startswithget_preload_configr    r6   r9   rD   warningswarnr   formatget_cupy_install_pathrQ   version_infoadd_dll_directoryr:   r7   r8   pathsep)configis_condar>   cuda_bin_pathwheel_libdircuda_bin_x64_pathr9   s          r   _setup_win32_dll_directoryrm      s    |w'' 0F#%%$&KF;,?7,J "OO	  ? ) "Y > > M &%& & & 	_##I../// w||!##VWe= =7==&& 	 -44\BBCCCC8??    LS%%%(188GGHHH$]333$&GLL$F$F!7>>"344 < 0-0 0 1 1 1():;;;'188FFGGG$\22222 ('
 ')00>>???z~~fb11%1BJ%>%E
6"""a0F 0FZ ('r   c                      t           j                            t           j                            t           j                            t
                    d                    S )Nz..)r6   r9   rN   rD   r=   rO   r   r   r   rd   rd      s;    7??
RW__X..557 7 7r   c                      t           j                            dd          } | t           j                            d          S t           j                            |           S )a|  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/cudnn/8.1.1/lib64/libcudnn.so.8.1.1`.

    The default $CUPY_CUDA_LIB_PATH is `~/.cupy/cuda_lib`.
    CUPY_CUDA_LIB_PATHNz~/.cupy/cuda_lib)r6   r7   r8   r9   
expanduserrN   )cupy_cuda_lib_paths    r   get_cupy_cuda_lib_pathrs     sM     (<dCC!w!!"45557??-...r   c                  <    t           t          d          a t           S )Nz_wheel.json)_preload_config_get_json_datar   r   r   r`   r`     s    (77r   namec                    t           j                            t                      dd|           }t           j                            |          sd S t          |          5 }t          j        |          cd d d            S # 1 swxY w Y   d S )NrW   rX   )r6   r9   rD   rd   r:   openjsonload)rw   config_pathfs      r   rv   rv     s    ',,$8 8K7>>+&& t	k		 ay||                 s   B  BBrY   c                    t                      }||d         dk    rt          d|  d           dS | t          vrt          d|            | |vrt          d|  d	           dS t          |          t          d
|             dS dS )z(Returns if the preload can be attempted.NrU   rV   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)r`   r   _preload_libsAssertionError)rY   rh   s     r   _can_attempt_preloadr   "  s      !!FF;/7:: 	LsLLLMMMu
->>>???
&5555666uS%0300111u4r   c           
         	
 t          d             t                     sdS i t           <   t                      		d         }t          d                    |                     t                      
t          d                    
                     	          d         	          d         }|D ]t          d  d	 d
 d           	
 fddD             } dk    r(	          d         }t          |	d                   |z   }n- dk    r'	          d         }t          |	d                   |z   }|D ]}t          j	        
                    |          s#t          d                    |                     D	 t          d|            t          j        |          t                    |<   t          d            n# t          $ rM}t          |          j        }d| d| d
| d}t          |           t!          j        |           Y d}~d}~ww xY wt          d                                         t          d d           	 t          j                  t                    <   t          d           # t          $ r }t          d  d|            Y d}~d}~ww xY wdS )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: Ncudaz$CuPy wheel package built for CUDA {}zCuPy CUDA library directory: {}version	filenamesLooking for z	 version  ()c           
      d    g | ],}t           j                            d          |          -S )r   )r6   r9   rD   ).0xrh   rr   filenamerY   r   s     r   
<listcomp>z$_preload_library.<locals>.<listcomp>Z  sM     . . .  GLL"F6NC! . . .r   )rY   lib64rB   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   r`   rc   rs   _get_cutensor_from_wheel_get_nccl_from_wheelr6   r9   r:   ctypesCDLL	Exceptiontype__name__ra   rb   )rY   cuda_versionr   libpath_candsr   libpathee_typer   rh   rr   r   r   s   `        @@@@r   _preload_libraryr   >  s    		3c	3	3444$$ M#!!F&>L	/	6	6|	D	DEEE/11	*	1	12D	E	EFFFSk)$GsK(I 4C 4C@C@@'@@X@@@AAA
. . . . . . . . -	. . .
 *%c{+=>()96&>JJ M F]]%c{+=>$%5vf~FF  % 	C 	CG7>>'** 9@@IIJJJ#0w00111.4k'.B.Bc"7+X # # #a)&w & && &!"& & &  S			c""""""""# -44X>>??? F8FFFGGGC/5{8/D/Dc"8,X C C C AAAaAABBBBBBBBC# A4C 4Cs2   	AG
H%AH  H%1J
J;J66J;r   c                    t          j        d| d          }t          |d                   }t          |          dk    rt          |d                   nd}t          |          dk    rt          |d                   nd}|||fS )Nz[^\d]rZ   )maxsplitr         )resplitintlen)r   partsmajorminorpatchs        r   _parse_versionr     sv    HXw333EaMME ZZ1__CaMMM!E ZZ1__CaMMM!E%r   package_prefixr   c           	      :   |                     d          \  }}|  d| }	 t          j                            |          }n0# t          j        j        $ r t          |  d| d           Y dS w xY wt          |j                  }t          |          }|d         |d         k    o5|d         |d         k    p#|d         |d         k    o|d         |d         k    }	|	s!t          |  d| d	| d
|j                    dS t          |  d| d|j                    |S )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   )
r   r   r   cuda_major_ver_pkgdistactualexpectedis_compatibles
             r   _find_compatible_wheelr     s    

3NA
0
0
0
0C!..s332   DDDDDEEEtt DL))Fg&&Hq	Xa[  	
1I# DAY(1+%B&)x{*B	    9 9 9 9 9 9*.,9 9 	: 	: 	:tN	H	HC	H	H$,	H	HIIIKs   A )A.-A.c                     t          d| |          }|g S t          j        dk    r2|                    d|                     d          d                    }n|                    d          }t          |          gS )zx
    Returns the list of shared library path candidates for cuTENSOR
    installed via Pip (cutensor-cuXX package).
    r   Nlinuxzcutensor/lib/libcutensor.so.r   r   zcutensor\bin\cutensor.dll)r   r   r^   locate_filer   str)r   r   r   
shared_libs       r   r   r     s    
 "*gt<<D|	
|w%%B7==+=+=a+@BB
 


 %%&CDD

OOr   c                     t           j        dk    rg S t          d| |          }|g S |                    d|                     d          d                    g}d |D             S )zw
    Returns the list of shared library path candidates for NCCL
    installed via Pip (nvidia-nccl-cuXX package).
    r   znvidia-ncclNznvidia/nccl/lib/libnccl.so.r   r   c                 ,    g | ]}t          |          S r   )r   )r   rY   s     r   r   z(_get_nccl_from_wheel.<locals>.<listcomp>  s    ,,,CHH,,,r   )r   r^   r   r   r   )r   r   r   shared_libss       r   r   r     s}    
 |w	!-$??D|	A'--*<*<Q*?AA	
 	
K
 -,,,,,r   c                 8   t                      }|| |vrd S |d         dk    r|d         }|                    d          d         }| dk    r3|d         d         }t          |          d         }d| d	| d
|dz    d}n[| dk    r3|d         d         }t          |          d         }d| d	| d
|dz    d}n"d|  d| }n|d         dk    rd|  }nt          t	          j        d|  dt          |          j         dt          |           d| d	           d S )NrU   pipr   r   r   r   r   zpip install "cutensor-cuz>=z,<r   "r
   zpip install "nvidia-nccl-cuz0python -m cupyx.tools.install_library --library z --cuda rV   zconda install -c conda-forge r   z' library could not be loaded.

Reason: r   z')

You can install the library by:
  $ )	r`   r   r   r   ra   rb   r   r   r   )rY   excrh   r   
cuda_majorr   r   cmds           r   _preload_warningr     s   !!F~F**ke##f~ZZ__Q'
*Z();<G"7++A.EPZPP7PPeAgPPPCCF]]Vn%78G"7++A.ES
SSgSSqSSSCCXSXXRVXXCC			'	'3c33M   
c	   #3xx  	       r   r   r   c                 V   | dk     s| dk    r|dk     rg S t                      }||d         dk    rt          j                            d          rt          j                    }|dk    rd}|s
J d            | d	}t
          j                            t          j        d
|d          t
          j                            t          j        d          gS t          j                            d          r,t
          j                            t          j        dd          gS g S | dv rd|  }d}nd}d|  }|  d| }t          d| d| d           	 t          j                            |          }n+# t          j        j        $ r t          d           g cY S w xY w|j        |k    s|j                            | d          rN|                    d| d          }	|	                                st          d           g S t#          |	          gS t          d|j         d           g S )N   r   rU   rV   r   aarch64sbsaz+platform.machine() returned an empty stringz-linuxtargetsincludewinLibrary)   r   znvidia-cuda-runtime-cucuda_runtimeznvidia-cuda-runtimecur   r   z==z.*zThe package could not be foundznvidia/z/includez(The include directory could not be foundzFound incompatible version (r   )r`   r   r^   r_   machiner6   r9   rD   prefixr   r   r   r   r   r   r   r:   r   )
r   r   rh   arch
target_dirpkg_namedir_namever_strr   include_dirs
             r   $_get_include_dir_from_conda_or_wheelr     sr    rzzerkkeaii	!!Ff[1W<<<""7++ 	#%%Dy  FFFFF4 JSZJ	JJSZ33  \$$U++ 	SZI>> 
 I 3E33! )<<    G	/	/	/G	/	/	/000!..x882   -...			 |w$,"9"9W---"H"H&&'C'C'C'CDD!!## 	;<<<IK  !!;DL;;;<<<	s   E0 0%FFc                     h d} d t           j                                        D             }| |z  }dt          |          k     r<d                    t          |                    }t          j        d| d           d S d S )N>   cupy-cuda80cupy-cuda90cupy-cuda91cupy-cuda92cupy-cuda100cupy-cuda101cupy-cuda102cupy-cuda110cupy-cuda111cupy-cuda112cupy-cuda113cupy-cuda114cupy-cuda115cupy-cuda116cupy-cuda117cupy-cuda118cupy-cuda11xcupy-cuda12xcupy-cuda13xcupy-rocm-4-0cupy-rocm-4-1cupy-rocm-4-2cupy-rocm-4-3cupy-rocm-5-0rW   c                 D    h | ]}|j                             d d          S )NameN)r   r8   )r   ds     r   	<setcomp>z1_detect_duplicate_installation.<locals>.<setcomp>O  s>     D D D z~~fd33 D D Dr   r   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   distributionsr   rD   sortedra   rb   )knowninstalled_namescupy_installedcupy_packages_lists       r   _detect_duplicate_installationr   ,  s      EBD D ) 2 @ @ B BD D DO_,N3~!YYvn'='=>>     	 	 	 	 	 r   c                      d} t           j        dk    rG	 | t                      z  } n4# t          $ r'}| dt	          |          j         d| z  } Y d }~nd }~ww xY w| S )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.htmlrT   z%

The cause could not be identified: : )r   r^   _diagnose_win32_dll_loadr   r   r   )r   r   s     r   _diagnose_import_errorr  p  s    	2C |w	+---CC 	 	 	+77#+ +'(+ +CCCCCC	 Js   & 
AAAc                     t          d          } | dS ddlm} t          j        d          }|j        |j        |j        g|j        _        |j        |j        _	        dddt                       dg}t          j        d          }| d	         D ]}	 t          j        |          }|                    |j        |t          |                     |                    d
| d|j                    a# t           $ r |                    d
| d           Y t"          $ r;}|                    d
| dt%          |          j         d| d           Y d }~d }~ww xY wd                    |          S )Nz_depends.jsonr   r   )wintypeskernel32zCUDA Path: zDLL dependencies:i   dependsz  z -> z -> not foundz -> error (r  r   r   )rv   r   r  r   HANDLELPWSTRDWORDGetModuleFileNameWargtypesrestyper    create_unicode_buffer_handler   appendvalueFileNotFoundErrorr   r   r   rD   )r  r  r  linesfilepathrw   dllr   s           r   r  r    s   _--Gr{:&&H(.,:H(*2.H' 	
'moo''	E +E22H	" I I	I+d##C''Xs8}}MMMLL8d88889999  	3 	3 	3LL1d11122222 	I 	I 	ILLGdGGtAww/?GG1GGGHHHHHHHH	I 99Us   AC//#E	E1EE)=__doc__r   importlib.metadatar   rz   r6   os.pathr^   r   r;   r   typingr   r   r   r   r   ra   r   r#   r'   r+   r/   ru   r   r7   r8   r   r   r   r    r%   r)   r-   r1   r   r$   r(   r,   r0   rm   rd   rs   r`   rv   boolr   r   r   r   r   Distributionr   listr   r   r   r   r   r  r  r   r   r   <module>r     s          				   				  



 3 3 3 3 3 3 3 3 3 3 3 3 3 3  


	>    
13	7	73	>c d                $E E E  $F F F  .3F 3F 3Fl7 7 7/ / /"HT#s(^4     $sCx.!9    c d    8JC JC JCZC E#sC-$8    &)14i -.   Bc  c    "-# -S -T#Y - - - -$  @2 2C 2DI 2 2 2 2jA A AH    2#      r   