o
    0 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   g/home/app/PaddleOCR-VL-test/.venv_paddleocr/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   ) Z
_ops_inferZ
_ops_trainZ
_cnn_inferZ
_cnn_trainZ
_adv_inferZ
_adv_trainzlinux-x86_64c                    s   g | ]
}d | d  qS )Z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 )cudnnZ64_z.dllr   r   )major_versionr   r   r   7   s    Linux:x86_64zWindows:x86_64)cudar   assets)splitr   )cuda_versionr   filename_linuxfilename_windowsZ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   Z
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   r(   r   r   r   r   r   __make_cutensor_recordT   s   
r*   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)r*   )r   r   r   r   _make_cutensor_recordh   s
   r+   r'   c                 C   r"   )Nz:https://developer.download.nvidia.com/compute/redist/nccl/vr   r   )r   r   r   r   r   _make_nccl_urlx   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   Zfull_versionr   r(   Zfilename_linux_x86_64Z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.txzr.   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tarZ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   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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 )*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   )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.htmlr'   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.0Z11z12.Z12zCleaning up...zDone!)r<   library_recordsr)   strr7   path
expandusercalculate_destinationexistsr   systemgetr6   tempfileTemporaryDirectoryopenjoinbasenameurllibrequesturlopenwritereadr?   namelistdirlensysr3   move
startswith)r   prefixlibraryarchrecordZlib_recordsdestinationZtarget_platformZassetr   ZtmpdirfresponseZoutdirsubdirdir_namelibdirsitemlicenser   r   r   install_lib   s   





&rp   c                 C   s   t j| |||S )zqCalculates the installation directory.

    ~/.cupy/cuda_lib/{cuda_version}/{library_name}/{library_version}
    )r7   rN   rW   )rd   r   rI   Zlib_verr   r   r   rP     s   rP   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   r'   r.   TzLibrary to install)choicesrequiredhelpz--cudazCUDA version)typerr   rs   z--archr@   rA   z&Target arhitecture (x86_64 or aarch64))rq   defaultrs   z--prefixzInstall destination)rt   ru   rs   z--actioninstalldumpzAction to perform)r@   amd64zunsupported architecture:    )indent)argparseArgumentParseradd_argumentrM   
parse_argsrf   r   unamemachinelowerAssertionErrorrd   r7   rN   abspathactionrp   r   re   r6   jsondumpsrL   )argsparserparamsr   r   r   r   main  sJ   






r   __main__rF   )__doc__r{   r   r7   r   r3   r9   ra   rT   urllib.requestrY   Z_cudnn_recordsZ_cutensor_recordsZ_nccl_recordsrL   r   r   r!   appendr#   r*   r+   r-   r/   r?   rp   rP   r   __name__argvr   r   r   r   <module>   sf   	
[(