
    `i,              
          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 Zd Zd Ze                     ed                     e                     ed                     eed<   d	 Zd
 Zd Ze                     ed                     e                     ed                     eed<   d Z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 Zd Zd Zd Zedk    r eej        dd                    dS dS ) zM
CUDA Library Installer

Installs the latest CUDA library supported by CuPy.
    Nc                     d|  d| z   S )NzAhttps://developer.download.nvidia.com/compute/cudnn/redist/cudnn// platformfilenames     o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/tools/install_library.py_make_cudnn_urlr
      s#     	L  h  	!"    c                                          d          d         g d}| t          d|          fd|D             dt          d|          fd|D             dd	d
S )N.r   ) 
_ops_infer
_ops_train
_cnn_infer
_cnn_train
_adv_infer
_adv_trainzlinux-x86_64c                      g | ]
}d | d S )libcudnnz.so.r   ).0suffixpublic_versions     r	   
<listcomp>z'__make_cudnn_record.<locals>.<listcomp>2   s:     9 9 9"( FEE^EE 9 9 9r   url	filenameszwindows-x86_64c                 "    g | ]}d | d dS )cudnn64_z.dllr   )r   r   major_versions     r	   r   z'__make_cudnn_record.<locals>.<listcomp>7   s=     9 9 9"( EfDDDDD 9 9 9r   Linux:x86_64zWindows:x86_64)cudar   assets)splitr
   )cuda_versionr   filename_linuxfilename_windowssuffix_listr!   s    `   @r	   __make_cudnn_recordr+   $   s    "((--a0M/ / /K  '~~FF9 9 9 9,79 9 9  ''79IJJ9 9 9 9,79 9 9 
 
  r   c                     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_majors     r	   _make_cudnn_recordr1   >   si    \'',,Q/00J!!!!gE*EEEDJDDDF F Fr   z12.xz11.xr   c                     dd|  d| z   S )Nz7https://developer.download.nvidia.com/compute/cutensor/zredist/libcutensor/z-x86_64/r   r   s     r	   _make_cutensor_urlr3   M   s%     	B:h::::	;<r   c                     | ||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%   )r3   format)r'   r   r8   r(   r)   s        r	   __make_cutensor_recordr:   T   ss     ", *'>BB188HHI 
 *)5EFF,- 	
 	
	  r   c                 (    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'   s    r	   _make_cutensor_recordr<   h   s#    
 "gw98: : :r   r7   c                     dd|  d| z   S )Nz:https://developer.download.nvidia.com/compute/redist/nccl/vr   r   )r   r   s     r	   _make_nccl_urlr?   x   s%     	E'N''X''	()r   c                     | ||t          ||          d                    |          gdt          ||          d                    |          gdddS )Nzlibnccl.so.{}r   )r#   zLinux:aarch64)r$   ncclr8   r%   )r?   r9   )r'   full_versionr   r8   filename_linux_x86_64filename_linux_aarch64s         r	   _make_nccl_recordrE      s     , &"$9; ;-44\BBC  &"$:< <-44\BBC 
 
	  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.txzrA   c                 (   	 t          j        | |           d S # t           j        $ ri t          d           	 t	          j        |d           t          j        dd| d|gd           Y d S # t          j        $ r d}t          |          w xY ww xY 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msgs      r	   _unpack_archiverW      s    $h44444 	$ 	$ 	$ > 	? 	? 	?	$Kd3333Nhk:$H H H H H H H, 	$ 	$ 	$FCs###	$	$s    B1A,,!BBc           	      
   |dk    r|dv rn|dk    rt          d          d }t          }||         D ]}|d         | k    r n@t          d                    t          d ||         D                                           |t          j                            d          }t          || |||                   }t          j                            |          r"t          d	                    |                    t          j
                     d
| }|d                             |d           }|"t          d                    |                    |dk    r-t          d           t          d           t                       n<|dk    r-t          d           t          d           t                       n	|dk    rnJ t          d                    |||         |d         |                     |d         }	t          d                    |	                     t          j                    5 }
t          t          j                            |
t          j                            |	                    d          5 }t$          j                            |	          5 }|                    |                                           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          d           t          j                            |
d          }t/          |j        |           t	          j        |          }t5          |          dk    sJ |d         }t          d           |dk    rut6          j	        dk    rddgndg}|d d!gz   D ]T}t9          j        t          j                            |||          t          j                            ||                     UnT|dk    r|                     d"          r	| d#k    rd$} n|                     d%          rd&} d!}t9          j        t          j                            ||d           t          j                            |d                      t9          j        t          j                            ||d|           t          j                            |d                     t9          j        t          j                            |||          |           n<|dk    r4t9          j        t          j                            ||          |           nJ t          d'           d d d            n# 1 swxY w Y   t          d(           d S ))NrA   )x86_64aarch64rY   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                     g | ]
}|d          S )r$   r   )r   xs     r	   r   zinstall_lib.<locals>.<listcomp>   s    #L#L#L!AfI#L#L#Lr   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.htmlr7   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!)rT   library_recordsr9   strrO   path
expandusercalculate_destinationexistsr   systemgetrN   tempfileTemporaryDirectoryopenjoinbasenameurllibrequesturlopenwritereadrW   namelistdirlensysrK   move
startswith)r$   prefixlibraryarchrecordlib_recordsdestinationtarget_platformassetr   tmpdirfresponseoutdirsubdirdir_namelibdirsitemlicenses                      r	   install_libr      s?   &T%:::			 N O O 	OF!Kg& P P&>T!!E "  s#L#L{77K#L#L#LMMNNP P 	P ~##$677'gvgOOK	w~~k"" M 88>{8K8KM M 	M "**33T33O8  $77E} //5vo/F/FH H 	H ' L 	M 	M 	MKLLL	J		 O 	P 	P 	PDEEE	F		u	
/
6
6&.+? ? @ @ @ ,C	

$
$S
)
)***		$	&	& % &"',,vrw'7'7'<'<==tDD 	)'',, )((() ) ) ) ) ) ) ) ) ) ) ) ) ) )	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	ofi00'''F##6{{a!9og(+(?(?uenneWG9i"88 5 5GLL488GLLd335 5 5 55 
""u%% $&..'' GKVXy99[)446 6 6 KVXud;;[%002 2 2 KVXw77F F F FKVX66DDDD5K%  %  %  %  %  %  %  %  %  %  %  %  %  %  % L 
'NNNNNs\   1AU!= K((KK(KK(KK(U!(K,	,U!/K,	0I%U!!U%(U%c                 F    t           j                            | |||          S )zqCalculates the installation directory.

    ~/.cupy/cuda_lib/{cuda_version}/{library_name}/{library_version}
    )rO   rj   rs   )r   r$   rc   lib_vers       r	   rl   rl     s    
 7<<c7333r   c                 p   t          j                    }|                    dg ddd           |                    dt          dd           |                    d	d
dgd d           |                    dt          d d           |                    dddgdd           |                    |           }|j        Vt          j                    j        	                                }|dv rd
|_        n |dk    rd|_        nt          d|           |j        )t          j                            |j                  |_        |j        dk    r(t!          |j        |j        |j        |j                   d S |j        dk    r5t'          t)          j        t,          |j                 d                     d S J )Nz	--library)r   r7   rA   TzLibrary to install)choicesrequiredhelpz--cudazCUDA version)typer   r   z--archrY   rZ   z&Target arhitecture (x86_64 or aarch64))r   defaultr   z--prefixzInstall destination)r   r   r   z--actioninstalldumpzAction to perform)rY   amd64zunsupported architecture:    )indent)argparseArgumentParseradd_argumentri   
parse_argsr   r   unamemachinelowerAssertionErrorr   rO   rj   abspathactionr   r$   r   rN   jsondumpsrh   )argsparserparamsr   s       r	   mainr     s   $&&F
 = = =!%1  3 3 3 sT+  - - -
8Y*? $E  G G G 
d2  4 4 4

Y,? )0  2 2 2 t$$F{.""*0022)))"FKK	!!#FKK !Gg!G!GHHH} 66}	!!FKK	! 	! 	! 	! 	!	&	 	 dj8CCCDDDDDur   __main__r`   )__doc__r   r   rO   r   rK   rQ   r}   rp   urllib.requestru   _cudnn_records_cutensor_records_nccl_recordsrh   r
   r+   r1   appendr3   r:   r<   r?   rE   rW   r   rl   r   __name__argvr   r   r	   <module>r      s      				       



       " " "  4F F F   ((00 1 1 1   ((00 1 1 1) < < <  (: : :   ..v66 7 7 7   ..v66 7 7 7/
 ) ) )  .   &&
Hh'(* * + + +   &&
Hh'(* * + + +   &&
Hh'(* * + + + ( $ $ $X X Xv4 4 4% % %P zD!"" r   