o
    ְiS                  	   @   s0  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dZdd Zdd Zdd Z dd Z!dd Z"dd Z#dd Z$dd Z%dd Z&dd  Z'd!d" Z(d#d$ Z)d%d& Z*d
eeeef  fd'd(Z+d)ed
eeeef  fd*d+Z,d,ed
e-fd-d.Z.d/d0 Z/d1ed
ee0e0e0f fd2d3Z1d4ed1ed5ed
eej2j3 fd6d7Z4d1ed5ed
e5e fd8d9Z6d1ed5ed
e5e fd:d;Z7d<d= Z8d>e0d?e0d
ee fd@dAZ9dBdC Z:d
efdDdEZ;d
efdFdGZ<dS )Hz6
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                 C   s*   t rtjd|  d tj  d S d S )Nz[CUPY_DEBUG_LIBRARY_LOAD] 
)_debugsysstderrwriteflush)r    r   E/home/ubuntu/.local/lib/python3.10/site-packages/cupy/_environment.py_logC   s   r   c                   C      t dkrt a t S Nr   )
_cuda_path_get_cuda_pathr   r   r   r   get_cuda_pathI      r   c                   C   r   r   )
_nvcc_path_get_nvcc_pathr   r   r   r   get_nvcc_pathQ   r   r!   c                   C   r   r   )
_rocm_path_get_rocm_pathr   r   r   r   get_rocm_pathY   r   r$   c                   C   r   r   )_hipcc_path_get_hipcc_pathr   r   r   r   get_hipcc_patha   r   r'   c                   C   r   r   )	_cub_path_get_cub_pathr   r   r   r   get_cub_pathi   r   r*   c                  C   X   t jdd} t j| r| S td}|d ur"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_pathr   r   r   r   q      
r   c                  C   sD   t jdd } | d ur| S t }|d u rd S tjdt j|ddS )NNVCCr-   binr2   )r/   r0   r1   r   r4   r5   r2   join)r8   r7   r   r   r   r       s   r    c                  C   r+   )N	ROCM_HOMEr   hipccz	/opt/rocmr.   )	rocm_path
hipcc_pathr   r   r   r#      r9   r#   c                  C   s*   t  } | d u r	d S tjdtj| ddS )Nr?   r;   r<   )r$   r4   r5   r/   r2   r=   )r@   r   r   r   r&      s   r&   c                  C   s   ddl m}  tjtjt}| js&tjtj	|dr"d}|S d }|S t
 }|d ur<tjtj	|dr<d}|S d }|S )Nr   )runtimez_core/include/cupy/_cccl/cubz<bundle>zinclude/hipcubz<ROCm>)cupy_backends.cuda.apirB   r/   r2   r6   abspath__file__is_hipisdirr=   r$   )rB   current_dirr(   r@   r   r   r   r)      s$   
r)   c                  C   sx  t jdrt } | d uo| d dk}t }|d ur'|r|}ntj|d}n	d }|s0t	d t
d| tjt ddd	}tj|rOt
d
| n	t
d| d }dt jkr|d urt
d| t| tj|d}tj|rt
d|  t| |d urt
d| t| d S d S |d urt
d| tjdd}|tj | tjd< d S d S d S )Nwin32	packagingcondar;   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   r/   r2   r=   warningswarnr   formatget_cupy_install_pathrG   version_infoadd_dll_directoryr3   r0   r1   pathsep)configis_condar7   cuda_bin_pathwheel_libdircuda_bin_x64_pathr2   r   r   r   _setup_win32_dll_directory   sV   



-rb   c                   C   s   t jt jt jtdS )Nz..)r/   r2   rD   r=   r6   rE   r   r   r   r   rY      s   rY   c                  C   s.   t jdd} | du r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)r/   r0   r1   r2   
expanduserrD   )cupy_cuda_lib_pathr   r   r   get_cupy_cuda_lib_path  s   rf   c                   C   s   t d u rtda t S )Nz_wheel.json)_preload_config_get_json_datar   r   r   r   rU     s   rU   namec                 C   sZ   t jt dd| }t j|sd S t|}t|W  d    S 1 s&w   Y  d S )NrL   rM   )r/   r2   r=   rY   r3   openjsonload)ri   config_pathfr   r   r   rh     s   

$rh   rN   c                 C   s   t  }|du s|d dkrtd|  d dS | tvr"td|  | |vr0td|  d	 dS t|  dur?td
|   dS dS )z(Returns if the preload can be attempted.NrJ   rK   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)rU   r   _preload_libsAssertionError)rN   r]   r   r   r   _can_attempt_preload"  s   rq   c           	         s   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 }dkrh  d }t| d | }ndkr{  d }t| d | }|D ]S}tj	
|st d| q}zt d|  t|t |< t d W  nh ty } z t|j}d| d| d
| d}t | t| W Y d}~q}d}~ww t d t d d ztt < t d W q7 ty } zt d d|  W Y d}~q7d}~ww q7d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              
      s&   g | ]}t j d  |qS )rr   )r/   r2   r=   ).0xr]   re   filenamerN   rs   r   r   
<listcomp>Z  s    z$_preload_library.<locals>.<listcomp>)rN   lib64r;   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   rq   ro   rU   rX   rf   _get_cutensor_from_wheel_get_nccl_from_wheelr/   r2   r3   ctypesCDLL	Exceptiontype__name__rV   rW   )	rN   cuda_versionrt   libpath_candsr~   libpathee_typer   r   rz   r   _preload_library>  sx   
 r   rs   c                 C   s^   t jd| dd}t|d }t|dkrt|d nd}t|dkr(t|d nd}|||fS )Nz[^\d]rO   )maxsplitr         )resplitintlen)rs   partsmajorminorpatchr   r   r   _parse_version  s
   
r   package_prefixrr   c           
      C   s   | d\}}|  d| }ztj|}W n tjjy,   t|  d| d Y dS w t|j}t|}|d |d koU|d |d kpU|d |d koU|d |d k}	|	sjt|  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   rs   )
r   rs   rr   cuda_major_ver_pkgdistactualexpectedis_compatibler   r   r   _find_compatible_wheel  s.   
r   c                 C   sR   t d| |}|du rg S tjdkr|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   rS   locate_filer   str)rs   rr   r   
shared_libr   r   r   r     s   


r   c                 C   sP   t jdkrg S td| |}|du rg S |d| dd  g}d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                 S   s   g | ]}t |qS r   )r   )rx   rN   r   r   r   r|     s    z(_get_nccl_from_wheel.<locals>.<listcomp>)r   rS   r   r   r   )rs   rr   r   shared_libsr   r   r   r     s   
r   c                 C   s  t  }|d u s| |vrd S |d dkre|d }|dd }| dkr=|d d }t|d }d| d	| d
|d  d}n6| dkr\|d d }t|d }d| d	| d
|d  d}nd|  d| }n|d dkrqd|  }nttd|  dt|j dt| d| d	 d S )NrJ   piprr   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 rK   zconda install -c conda-forge r   z' library could not be loaded.

Reason: rv   z')

You can install the library by:
  $ )	rU   r   r   rp   rV   rW   r   r   r   )rN   excr]   rr   
cuda_majorrs   r   cmdr   r   r   _preload_warning  s6   r   r   r   c           
      C   s  | dk s| dkr|dk rg S t  }|d urZ|d dkrZtjdrHt }|dkr+d}|s1J d| d	}tjtjd
|dtjtjdgS tjdrXtjtjddgS g S | dv rfd|  }d}nd}d|  }|  d| }t	d| d| d zt
j|}W n t
jjy   t	d g  Y S w |j|ks|j| dr|d| d}	|	 st	d g S t|	gS t	d|j d g S )N   r   rJ   rK   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   ru   z==z.*zThe package could not be foundznvidia/z/includez(The include directory could not be foundzFound incompatible version (rw   )rU   r   rS   rT   machiner/   r2   r=   prefixr   r   r   r   r   rs   r   r3   r   )
r   r   r]   arch
target_dirpkg_namedir_namever_strr   include_dirr   r   r   $_get_include_dir_from_conda_or_wheel  sL   



r   c                  C   sX   h d} dd t j D }| |@ }dt|k r*dt|}t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-0rL   c                 S   s   h | ]	}|j d dqS )NameN)r   r1   )rx   dr   r   r   	<setcomp>O  s    z1_detect_duplicate_installation.<locals>.<setcomp>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   r=   sortedrV   rW   )knowninstalled_namescupy_installedcupy_packages_listr   r   r   _detect_duplicate_installation,  s   !r   c               
   C   sd   d} t jdkr0z| t 7 } W | S  ty/ } z| dt|j d| 7 } W Y d }~| S d }~w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.htmlrI   z%

The cause could not be identified: : )r   rS   _diagnose_win32_dll_loadr   r   r   )r   r   r   r   r   _diagnose_import_errorp  s"   
r   c                  C   s"  t d} | d u r
dS ddlm} td}|j|j|jg|j_|j|j_	dddt
  dg}td}| d	 D ]T}zt|}||j|t| |d
| d|j  W q7 tyh   |d
| d Y q7 ty } z|d
| dt|j d| d W Y d }~q7d }~ww d|S )Nz_depends.jsonr   r   )wintypeskernel32zCUDA Path: zDLL dependencies:i   dependsz  z -> z -> not foundz -> error (r   rw   r   )rh   r   r   r   HANDLELPWSTRDWORDGetModuleFileNameWargtypesrestyper   create_unicode_buffer_handler   appendvalueFileNotFoundErrorr   r   r   r=   )r   r   r   linesfilepathri   dllr   r   r   r   r     s4   




0
r   )=__doc__r   importlib.metadatar   rk   r/   os.pathrS   r   r4   r   typingr   r   r   r   r   rV   r   r   r"   r%   r(   rg   ro   r0   r1   r   r   r   r   r!   r$   r'   r*   r   r    r#   r&   r)   rb   rY   rf   rU   rh   boolrq   r   r   r   r   Distributionr   listr   r   r   r   r   r   r   r   r   r   r   <module>   sv    6	M

! 5D