o
    X۷i<(                  	   @  s.  d Z ddlm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Zg Zg Zi Zdd Zdd Zd	d
 Zeed eed eed< dd Zdd Zeedddddd eedddddd eed< dd Zdd Zdd Zd d! Zed"kree	jd#d  dS dS )$zM
CUDA Library Installer

Installs the latest CUDA library supported by CuPy.
    )annotationsNa  
******************************************************************************

The "cupyx.tools.install_library" tool is deprecated and will be removed in
a future CuPy release. To install NCCL/cuTENSOR libraries, please install them
via package managers (pip/conda) that are used to install CuPy.

You can use the following command to see if NCCL/cuTENSOR is already installed.
If you see the version number displayed for "NCCL Runtime Version" or "cuTENSOR
Version", they are available and enabled in your CuPy installation.

  $ python -c 'import cupy; cupy.show_config(_full=True)'

If you see "None" instead of the version number, and you installed CuPy via pip
or conda, you can get the instructions to install these libraries by running
the following commands:

  $ python -c 'import cupy.cuda.nccl'
  $ python -c 'import cupy.cuda.cutensor'

******************************************************************************
c                 C     dd|  d|  S )Nz7https://developer.download.nvidia.com/compute/cutensor/zredist/libcutensor/z-x86_64/ )platformfilenamer   r   Q/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/tools/install_library.py_make_cutensor_url5      r   c                 C  s@   | ||t d|d|d|gdt d|ddgddd	S )
Nlinuxzlibcutensor.so.{}zlibcutensorMg.so.{}url	filenameswindowszcutensor.dllzcutensorMg.dll)Linux:x86_64zWindows:x86_64)cudacutensormin_pypi_versionassets)r   format)cuda_versionpublic_versionr   filename_linuxfilename_windowsr   r   r   __make_cutensor_record<   s   r   c                 C  s.   |  dd }t| ddd| dd| dS )	N.r   z2.4.1z2.3.0z%libcutensor-linux-x86_64-2.4.1.4_cudaz-archive.tar.xzz'libcutensor-windows-x86_64-2.4.1.4_cudaz-archive.zip)splitr   )r   
cuda_majorr   r   r   _make_cutensor_recordS   s   

r   z12.xz13.xr   c                 C  r   )Nz:https://developer.download.nvidia.com/compute/redist/nccl/v/r   )r   r   r   r   r   _make_nccl_urle   r	   r    c                 C  s<   | ||t ||d|gdt ||d|gdddS )Nzlibnccl.so.{}r   )r   zLinux:aarch64)r   ncclr   r   )r    r   )r   full_versionr   r   filename_linux_x86_64filename_linux_aarch64r   r   r   _make_nccl_recordl   s   

r%   z2.27.7z!nccl_2.27.7-1+cuda13.0_x86_64.txzz"nccl_2.27.7-1+cuda13.0_aarch64.txzz2.25.1z2.18.1z!nccl_2.25.1-1+cuda12.8_x86_64.txzz"nccl_2.25.1-1+cuda12.8_aarch64.txzr!   c                 C  s   ddi}t jdk s| dri }ztj| |fi | W d S  tjyO   td ztj|dd t	j
dd	| d
|gdd W Y d S  t	jyN   d}t|w w )Nfilterdata)      z.zipz`The archive format is not supported in your Python environment. Falling back to "tar" command...T)exist_oktarxfz-C)checkz2Failed to extract the archive using "tar" command.)sysversion_infoendswithshutilunpack_archive	ReadErrorprintosmakedirs
subprocessrunCalledProcessErrorRuntimeError)r   extract_dirkwargsmsgr   r   r   _unpack_archive   s"   r>   c              
   C  s  |dkr	|dv r	n|dkrt dd }t}|| D ]
}|d | kr# nqt dtdd || D |d u r>tjd	}t|| ||| }tj|rTt d
|t	
  d| }|d |d }|d u rpt d||dkrtd td t  n|dkrnJ td||| |d | |d }	td|	 t }
ttj|
tj|	d%}tj|	}||  W d    n1 sw   Y  W d    n1 sw   Y  td tj|
d}t|j| t|}t|dksJ |d }td |dkrTd}ttj||dtj|d t	
 dkr6ttj||dtj|d ttj||dtj|d ttj|||| n|dkrettj||| nJ td  W d    n	1 svw   Y  td! d S )"Nr!   )x86_64aarch64r?   zK
Currently this tool only supports x86_64 or aarch64 architecture for NCCL.r   zB
The CUDA version specified is not supported.
Should be one of {}.c                 S  s   g | ]}|d  qS )r   r   ).0xr   r   r   
<listcomp>   s    zinstall_lib.<locals>.<listcomp>z~/.cupy/cuda_libzb
The destination directory {} already exists.
Remove the directory first if you want to reinstall.:r   z,
The current platform ({}) is not supported.r   zyBy downloading and using cuTENSOR, you accept the terms and conditions of the NVIDIA cuTENSOR Software License Agreement:z4  https://docs.nvidia.com/cuda/cutensor/license.htmlFz#Installing {} {} for CUDA {} to: {}r   zDownloading {}...wbzExtracting...extract   r   zInstalling...LICENSEincludeWindowsbinlibzCleaning up...zDone!)r:   library_recordsr   strr5   path
expandusercalculate_destinationexistsr   systemgetr4   tempfileTemporaryDirectoryopenjoinbasenameurllibrequesturlopenwritereadr>   namelistdirlenr1   move)r   prefixlibraryarchrecordlib_recordsdestinationtarget_platformassetr   tmpdirfresponseoutdirsubdirdir_namelicenser   r   r   install_lib   s   




 rr   c                 C  s   t j| |||S )zqCalculates the installation directory.

    ~/.cupy/cuda_lib/{cuda_version}/{library_name}/{library_version}
    )r5   rO   rX   )rc   r   rL   lib_verr   r   r   rQ      s   rQ   c                 C  s.  t  }|jdddgddd |jdtddd	 |jd
ddgd dd |jdtd dd |jdddgddd || }|jd u r_t j	 }|dv rPd|_n|dkrXd|_nt
d| |jd urltj|j|_|jdkrtt t|j|j|j|j d S |jdkrttjt|j dd d S J )Nz	--libraryr   r!   TzLibrary to install)choicesrequiredhelpz--cudazCUDA version)typeru   rv   z--archr?   r@   z'Target architecture (x86_64 or aarch64))rt   defaultrv   z--prefixzInstall destination)rw   rx   rv   z--actioninstalldumpzAction to perform)r?   amd64zunsupported architecture:    )indent)argparseArgumentParseradd_argumentrN   
parse_argsre   r   unamemachinelowerAssertionErrorrc   r5   rO   abspathactionr4   _deprecation_messagerr   r   rd   jsondumpsrM   )argsparserparamsr   r   r   r   main   sL   






r   __main__rG   )__doc__
__future__r   r~   r   r5   r   r1   r7   r.   rU   urllib.requestrZ   r   _cutensor_records_nccl_recordsrM   r   r   r   appendr    r%   r>   rr   rQ   r   __name__argvr   r   r   r   <module>   sR   P*