o
    i,                  	   @   sv  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
g 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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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.
    Nc                 C   s   d|  d|  S )NzAhttps://developer.download.nvidia.com/compute/cudnn/redist/cudnn// platformfilenamer   r   X/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/cupyx/tools/install_library.py_make_cudnn_url   s   r   c                    s\    dd  g d}| td|fdd|D dtd| fd	d|D dd
dS )N.r   ) 
_ops_infer
_ops_train
_cnn_infer
_cnn_train
_adv_infer
_adv_trainzlinux-x86_64c                    s   g | ]
}d | d  qS )libcudnnz.so.r   .0suffix)public_versionr   r   
<listcomp>2   s    z'__make_cudnn_record.<locals>.<listcomp>url	filenameszwindows-x86_64c                    s   g | ]}d | d  dqS )cudnn64_z.dllr   r   )major_versionr   r   r   7   s    Linux:x86_64zWindows:x86_64)cudar   assets)splitr   )cuda_versionr   filename_linuxfilename_windowssuffix_listr   )r   r   r   __make_cudnn_record$   s    

r&   c                 C   s<   t | dd }|dv sJ t| dd| dd| dS )	Nr	   r   )      z8.8.1zcudnn-linux-x86_64-8.8.1.3_cudaz-archive.tar.xzz!cudnn-windows-x86_64-8.8.1.3_cudaz-archive.zip)intr!   r&   )r"   
cuda_majorr   r   r   _make_cudnn_record>   s   

r+   z12.xz11.xr   c                 C      dd|  d|  S )Nz7https://developer.download.nvidia.com/compute/cutensor/zredist/libcutensor/z-x86_64/r   r   r   r   r   _make_cutensor_urlM      r-   c                 C   s6   | ||t d|d|gdt d|dgdddS )Nlinuxzlibcutensor.so.{}r   windowszcutensor.dllr   )r   cutensormin_pypi_versionr    )r-   format)r"   r   r2   r#   r$   r   r   r   __make_cutensor_recordT   s   
r4   c                 C   s   t | ddddS )Nz2.1.0z2.0.0z/libcutensor-linux-x86_64-2.1.0.9-archive.tar.xzz.libcutensor-windows-x86_64-2.1.0.9-archive.zip)r4   )r"   r   r   r   _make_cutensor_recordh   s
   r5   r1   c                 C   r,   )Nz:https://developer.download.nvidia.com/compute/redist/nccl/vr   r   )r   r   r   r   r   _make_nccl_urlx   r.   r7   c                 C   s<   | ||t ||d|gdt ||d|gdddS )Nzlibnccl.so.{}r   )r   zLinux:aarch64)r   ncclr2   r    )r7   r3   )r"   full_versionr   r2   filename_linux_x86_64filename_linux_aarch64r   r   r   _make_nccl_record   s   

r<   z13.xz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.16.5z!nccl_2.25.1-1+cuda12.8_x86_64.txzz"nccl_2.25.1-1+cuda12.8_aarch64.txzz!nccl_2.16.5-1+cuda11.8_x86_64.txzz"nccl_2.16.5-1+cuda11.8_aarch64.txzr8   c                 C   sx   z	t | | W d S  t jy;   td ztj|dd tjdd| d|gdd W Y d S  tjy:   d}t	|w w )	Nz`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.)
shutilunpack_archive	ReadErrorprintosmakedirs
subprocessrunCalledProcessErrorRuntimeError)r   extract_dirmsgr   r   r   _unpack_archive   s   rM   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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sJ |d }td |dkrEtj	dkr&dd gnd g}|d!d"g D ]}ttj|||tj|| q/n`|dkr| d#rX| d$krXd%} n| d&r`d'} d"}ttj||d!tj|d! ttj||d | tj|d  ttj|||| n|dkrttj||| nJ td( W d    n	1 sw   Y  td) d S )*Nr8   )x86_64aarch64rN   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   )r   xr   r   r   r      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   zsBy downloading and using cuDNN, you accept the terms and conditions of the NVIDIA cuDNN Software License Agreement:z;  https://docs.nvidia.com/deeplearning/cudnn/sla/index.htmlr1   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...win32binlibincludeLICENSEz11.z11.011z12.12zCleaning up...zDone!)rJ   library_recordsr3   strrE   path
expandusercalculate_destinationexistsr   systemgetrD   tempfileTemporaryDirectoryopenjoinbasenameurllibrequesturlopenwritereadrM   namelistdirlensysrA   move
startswith)r   prefixlibraryarchrecordlib_recordsdestinationtarget_platformassetr   tmpdirfresponseoutdirsubdirdir_namelibdirsitemlicenser   r   r   install_lib   s   





&r   c                 C   s   t j| |||S )zqCalculates the installation directory.

    ~/.cupy/cuda_lib/{cuda_version}/{library_name}/{library_version}
    )rE   r^   rg   )rt   r   rW   lib_verr   r   r   r`     s   r`   c                 C   s&  t  }|jdg d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r~t|j|j|j|j d S |jdkrttjt|j dd d S J )Nz	--library)r   r1   r8   TzLibrary to install)choicesrequiredhelpz--cudazCUDA version)typer   r   z--archrN   rO   z&Target arhitecture (x86_64 or aarch64))r   defaultr   z--prefixzInstall destination)r   r   r   z--actioninstalldumpzAction to perform)rN   amd64zunsupported architecture:    )indent)argparseArgumentParseradd_argumentr]   
parse_argsrv   r   unamemachinelowerAssertionErrorrt   rE   r^   abspathactionr   r   ru   rD   jsondumpsr\   )argsparserparamsr   r   r   r   main  sJ   






r   __main__rT   )__doc__r   r   rE   r   rA   rG   rq   rd   urllib.requestri   _cudnn_records_cutensor_records_nccl_recordsr\   r   r&   r+   appendr-   r4   r5   r7   r<   rM   r   r`   r   __name__argvr   r   r   r   <module>   sf   	
[(