o
    œÜÓhF  ã                   @   sX  U d Z ddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZ g d	¢Zeed
< g Ze d¡ G dd„ dƒZdd„ Zdedee fdd„Zdee dee fdd„Zdee dee fdd„Zdedee fdd„Zdedee fdd„Zdee ddfdd„Zdeedf fd d!„Zd"d#„ Z d$d%„ Z!d&d'„ Z"dS )(a  
extract factors the build is dependent on:
[X] compute capability
    [ ] TODO: Q - What if we have multiple GPUs of different makes?
- CUDA version
- Software:
    - CPU-only: only CPU quantization functions (no optimizer, no matrix multipl)
    - CuBLAS-LT: full-build 8-bit optimizer
    - no CuBLAS-LT: no 8-bit matrix multiplication (`nomatmul`)

evaluation:
    - if paths faulty, return meaningful error
    - else:
        - determine CUDA version
        - determine capabilities
        - based on that set the default path
é    N)Úwarn)Úproduct©ÚPath)ÚSetÚUnioné   )Ú,get_potentially_lib_path_containing_env_vars)zlibcudart.sozlibcudart.so.11.0zlibcudart.so.12.0ÚCUDA_RUNTIME_LIBSz#$CONDA_PREFIX/lib/libcudart.so.11.0c                   @   sV   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dd„ Z
edd„ ƒZdS )Ú	CUDASetupNc                 C   s   t dƒ‚)NzCall get_instance() instead)ÚRuntimeError©Úself© r   úR/var/www/html/ai/venv/lib/python3.10/site-packages/bitsandbytes/cuda_setup/main.pyÚ__init__+   s   zCUDASetup.__init__c                 C   s¬  t | ddƒrd S t| jƒ d| _| js8|  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d	¡ |  d
¡ d S | jd u rl|  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ d S d| j› }t| jƒdk r~|d7 }n2| jdkrˆ|d7 }n(| jd d… dkrŸt| jd ƒdkrŸ|d7 }n| jdkr°|  d¡ |  d¡ d S t	| j
ƒ}|s»|d 7 }|  d!¡ |  d"¡ |  d#¡ |  |¡ |  d$¡ d S )%NÚerrorFTzrCUDA SETUP: Problem: The main issue seems to be that the main CUDA library was not detected or CUDA not installed.zhCUDA SETUP: Solution 1): Your paths are probably not up-to-date. You can update them via: sudo ldconfig.zRCUDA SETUP: Solution 2): If you do not have sudo rights, you can do the following:zXCUDA SETUP: Solution 2a): Find the cuda library via: find / -name libcuda.so 2>/dev/nullzCUDA SETUP: Solution 2b): Once the library is found add it to the LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:FOUND_PATH_FROM_2azvCUDA SETUP: Solution 2c): For a permanent solution add the export from 2b into your .bashrc file, located at ~/.bashrcz…CUDA SETUP: Solution 3): For a missing CUDA runtime library (libcudart.so), use `find / -name libcudart.so* and follow with step (2b)zdCUDA SETUP: Problem: The main issue seems to be that the main CUDA runtime library was not detected.zvCUDA SETUP: Solution 1: To solve the issue the libcudart.so location needs to be added to the LD_LIBRARY_PATH variablezbCUDA SETUP: Solution 1a): Find the cuda runtime library via: find / -name libcudart.so 2>/dev/nullzCUDA SETUP: Solution 1b): Once the library is found add it to the LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:FOUND_PATH_FROM_1azvCUDA SETUP: Solution 1c): For a permanent solution add the export from 1b into your .bashrc file, located at ~/.bashrczUCUDA SETUP: Solution 2: If no library was found in step 1a) you need to install CUDA.z‚CUDA SETUP: Solution 2a): Download CUDA install script: wget https://github.com/TimDettmers/bitsandbytes/blob/main/cuda_install.shz‘CUDA SETUP: Solution 2b): Install desired CUDA version to desired location. The syntax is bash cuda_install.sh CUDA_VERSION PATH_TO_INSTALL_INTO.z†CUDA SETUP: Solution 2b): For example, "bash cuda_install.sh 113 ~/local/" will download CUDA 11.3 and install into the folder ~/localzCUDA_VERSION=é   z make cuda92Ú110z make cuda110é   Ú11r   z make cuda11xÚ100zICUDA SETUP: CUDA 10.0 not supported. Please use a different CUDA version.z”CUDA SETUP: Before you try again running bitsandbytes, make sure old CUDA 10.0 versions are uninstalled and removed from $LD_LIBRARY_PATH variables.Ú	_nomatmulzFCUDA SETUP: Something unexpected happened. Please compile from source:z9git clone https://github.com/TimDettmers/bitsandbytes.gitzcd bitsandbyteszpython setup.py install)ÚgetattrÚprintr   Úcuda_availableÚadd_log_entryÚcudart_pathÚcuda_version_stringÚlenÚintÚis_cublasLt_compatibleÚcc)r   Úmake_cmdÚhas_cublasltr   r   r   Úgenerate_instructions.   sT   




















$








zCUDASetup.generate_instructionsc                 C   s,   t | ddƒsd| _d | _d| _d| _d S d S )NÚinitializedF)r   Úhas_printedÚlibr&   r   r   r   r   r   Ú
initialize_   s   
üzCUDASetup.initializec                 C   sx   t j ¡ r6dtjv r8ttjd ƒdkr:tdd› dtjd › dd› dƒ | jd d… tjd › d | _d S d S d S d S )	NÚBNB_CUDA_VERSIONr   z

úP================================================================================a  
WARNING: Manual override via BNB_CUDA_VERSION env variable detected!
BNB_CUDA_VERSION=XXX can be used to load a bitsandbytes version that is different from the PyTorch CUDA version.
If this was unintended set the BNB_CUDA_VERSION variable to an empty string: export BNB_CUDA_VERSION=
If you use the manual override make sure the right libcudart.so is in your LD_LIBRARY_PATH
For example by adding the following to your .bashrc: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_cuda_dir/lib64
Loading CUDA version: BNB_CUDA_VERSION=Ú
iúÿÿÿú.so)ÚtorchÚcudaÚis_availableÚosÚenvironr   r   Úbinary_namer   r   r   r   Úmanual_overridef   s   


ú
ù$õzCUDASetup.manual_overridec           	   
   C   sž  d| _ g | _tƒ \}}}}|| _tj ¡ | _|| _|| _	|| _
|  ¡  ttƒjj}|| j
 }z†| ¡ s¢|  d|› d¡ d}|  d|› d¡ || }| ¡ rTtj ¡ r˜|  d¡ |  d¡ |  d	¡ |  d
¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  ¡  tdƒ‚tj |¡| _W d S |  d|› d¡ tj |¡| _W d S  tyÎ } z|  t|ƒ¡ W Y d }~d S d }~ww )NTz0CUDA SETUP: Required library version not found: z+. Maybe you need to compile it from source?úlibbitsandbytes_cpu.sozCUDA SETUP: Defaulting to z...Ú zZ================================================ERROR=====================================z4CUDA SETUP: CUDA detection failed! Possible reasons:z›1. You need to manually override the PyTorch CUDA version. Please see: "https://github.com/TimDettmers/bitsandbytes/blob/main/how_to_use_nonpytorch_cuda.mdz2. CUDA driver not installedz3. CUDA not installedz/4. You have multiple conflicting CUDA librarieszC5. Required library not pre-compiled for this bitsandbytes release!z‡CUDA SETUP: If you compiled from source, try again with `make CUDA_VERSION=DETECTED_CUDA_VERSION` for example, `make CUDA_VERSION=113`.zƒCUDA SETUP: The CUDA version for the compile might depend on your conda install. Inspect CUDA version via `conda list | grep cuda`.r+   zCUDA SETUP: Setup Failed!zCUDA SETUP: Loading binary )r&   Úcuda_setup_logÚevaluate_cuda_setupr   r.   r/   r0   r   r"   r   r3   r4   r   Ú__file__ÚparentÚexistsr   r%   Ú	ExceptionÚctÚcdllÚLoadLibraryr(   Ústr)	r   r3   r   r"   r   Úpackage_dirÚbinary_pathÚlegacy_binary_nameÚexr   r   r   Úrun_cuda_setupt   sN   












€ÿzCUDASetup.run_cuda_setupFc                 C   s   | j  ||f¡ d S ©N)r7   Úappend©r   ÚmsgÚ
is_warningr   r   r   r       ó   zCUDASetup.add_log_entryc                 C   s*   | j D ]\}}|rt|ƒ qt|ƒ qd S rF   )r7   r   r   rH   r   r   r   Úprint_log_stack£   s
   

üzCUDASetup.print_log_stackc                 C   s&   | j d u r|  | ¡| _ | j  ¡  | j S rF   )Ú	_instanceÚ__new__r)   )Úclsr   r   r   Úget_instanceª   s   

zCUDASetup.get_instance)F)Ú__name__Ú
__module__Ú__qualname__rM   r   r%   r)   r4   rE   r   rL   ÚclassmethodrP   r   r   r   r   r   (   s    1
,r   c                 C   s\   d}| d ur,|   d¡\}}t|ƒdk st|ƒdkr*t|ƒdk r*t ¡ jddd |S d}|S )NFÚ.é   é   zöWARNING: Compute capability < 7.5 detected! Only slow 8-bit matmul is supported for your GPU!                     If you run into issues with 8-bit matmul, you can try 4-bit quantization: https://huggingface.co/blog/4bit-transformers-bitsandbytesT©rJ   )Úsplitr    r   rP   r   )r"   r$   Úcc_majorÚcc_minorr   r   r   r!   ²   s   $
ÿÿr!   Úpaths_list_candidateÚreturnc                 C   s   dd„ |   d¡D ƒS )Nc                 S   s   h | ]}|rt |ƒ’qS r   r   )Ú.0Úld_pathr   r   r   Ú	<setcomp>¾   s    z*extract_candidate_paths.<locals>.<setcomp>ú:)rY   ©r\   r   r   r   Úextract_candidate_paths½   rK   rc   Úcandidate_pathsc                 C   s¤   t ƒ }| D ]8}z| ¡ r| |¡ W q ty, } z|jtjkr"|‚W Y d }~qd }~w ty= } zW Y d }~qd }~ww | | }|rPt ¡ j	d|› dd |S )NzMThe following directories listed in your path were found to be non-existent: FrX   )
Úsetr;   ÚaddÚOSErrorÚerrnoÚENAMETOOLONGÚPermissionErrorr   rP   r   )rd   Úexistent_directoriesÚpathÚexcÚpexÚnon_existent_directoriesr   r   r   Úremove_non_existent_dirsÁ   s.   
€ÿ€€ÿ
ÿÿrp   c                 C   s8   t ƒ }tD ]}| D ]}||  ¡ r| || ¡ q	q|S rF   )re   r
   Úis_filerf   )rd   ÚpathsÚlibnamerl   r   r   r   Úget_cuda_runtime_lib_pathsÕ   s   €þrt   c                 C   ó   t t| ƒƒS )zc
    Searches a given environmental var for the CUDA runtime library,
    i.e. `libcudart.so`.
    )rp   rc   rb   r   r   r   Úresolve_paths_listÞ   s   rv   c                 C   ru   rF   )rt   rv   rb   r   r   r   Úfind_cuda_lib_inæ   s   ÿrw   Úresults_pathsc                 C   s8   t | ƒdkrdt› d| › d}t ¡ j|dd d S d S )Nr   zFound duplicate z files: a*  .. We select the PyTorch default libcudart.so, which is {torch.version.cuda},but this might missmatch with the CUDA version that is needed for bitsandbytes.To override this behavior set the BNB_CUDA_VERSION=<version string, e.g. 122> environmental variableFor example, if you want to use the CUDA version 122BNB_CUDA_VERSION=122 python ...OR set the environmental variable in your .bashrc: export BNB_CUDA_VERSION=122In the case of a manual override, make sure you set the LD_LIBRARY_PATH, e.g.export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.2TrX   )r   r
   r   rP   r   )rx   Úwarning_msgr   r   r   Úwarn_in_case_of_duplicatesì   s
   ÿ
õrz   c            	      C   sH  t ƒ } tƒ }d| v r4t| d ƒd }tt|ƒƒ}t|ƒ |r#| |¡ t ¡ j	| d › dt
› ddd d| v rZt| d ƒ}|rE| |¡ t|ƒ t ¡ j	| d › dt
› ddd dd	„ |  ¡ D ƒ}tƒ }| ¡ D ]\}}| t|ƒ¡ qjt|ƒd
krŠt ¡  	d¡ | tdƒ¡ t|ƒ t ¡ }| 	d|› ¡ |r¢tt|ƒƒS dS )aØ  
        Searches for a cuda installations, in the following order of priority:
            1. active conda env
            2. LD_LIBRARY_PATH
            3. any other env vars, while ignoring those that
                - are known to be unrelated (see `bnb.cuda_setup.env_vars.to_be_ignored`)
                - don't contain the path separator `/`

        If multiple libraries are found in part 3, we optimistically try one,
        while giving a warning message.
    ÚCONDA_PREFIXr(   z did not contain z( as expected! Searching further paths...TrX   ÚLD_LIBRARY_PATHc                 S   s   i | ]\}}|d vr||“qS )>   r{   r|   r   )r^   Úenv_varÚvaluer   r   r   Ú
<dictcomp>   s
    ÿz3determine_cuda_runtime_lib_path.<locals>.<dictcomp>r   zcCUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching in backup paths...z/usr/local/cuda/lib64z0DEBUG: Possible options found for libcudart.so: N)r	   re   r   rw   r@   rz   Úupdater   rP   r   r
   Úitemsr   ÚnextÚiter)	Úcandidate_env_varsÚcuda_runtime_libsÚconda_libs_pathÚconda_cuda_libsÚlib_ld_cuda_libsÚremaining_candidate_env_varsr}   r~   Ú
cuda_setupr   r   r   Údetermine_cuda_runtime_lib_pathû   sH   
ÿÿ
ÿÿÿr‹   c                  C   s:   t ttjj d¡ƒ\} }| dk rt ¡  d¡ | › |› S )NrU   é   z—CUDA SETUP: CUDA version lower than 11 are currently not supported for LLM.int8(). You will be only to use 8-bit optimizers and quantization routines!!)	Úmapr    r.   Úversionr/   rY   r   rP   r   )ÚmajorÚminorr   r   r   Úget_cuda_version6  s   r‘   c                  C   sH   g } t tj ¡ ƒD ]}tj tj |¡¡\}}|  |› d|› ¡ q	| S )NrU   )Úranger.   r/   Údevice_countÚget_device_capabilityÚdevicerG   )ÚccsÚirZ   r[   r   r   r   Úget_compute_capabilities>  s
   r˜   c                  C   sÔ   t  ¡ } dtjvsttjd ƒdkr'|  d¡ |  d¡ |  dd¡ |  d¡ tj ¡ s.dS t	ƒ }t
ƒ }| ¡  |d	 }tƒ }|  d
|› d|› d¡ |  d¡ t|ƒ}|r]d|› d}n	 d|› d}||||fS )NÚBITSANDBYTES_NOWELCOMEÚ0r6   zP===================================BUG REPORT===================================zNWelcome to bitsandbytes. For bug reports, please run

python -m bitsandbytes

zqand submit this information together with your error trace to: https://github.com/TimDettmers/bitsandbytes/issuesr+   )r5   NNNéÿÿÿÿz1CUDA SETUP: PyTorch settings found: CUDA_VERSION=z, Highest Compute Capability: rU   z˜CUDA SETUP: To manually override the PyTorch CUDA version please see:https://github.com/TimDettmers/bitsandbytes/blob/main/how_to_use_nonpytorch_cuda.mdÚlibbitsandbytes_cudar-   z_nocublaslt.so)r   rP   r1   r2   r@   r   r.   r/   r0   r‹   r˜   Úsortr‘   r!   )rŠ   r   r–   r"   r   r$   r3   r   r   r   r8   G  s,   

ÿ

	r8   )#Ú__doc__Úctypesr=   r1   rh   r.   Úwarningsr   Ú	itertoolsr   Úpathlibr   Útypingr   r   Úenv_varsr	   r
   ÚlistÚ__annotations__Úbackup_pathsrG   r   r!   r@   rc   rp   rt   rv   rw   rz   r‹   r‘   r˜   r8   r   r   r   r   Ú<module>   s4    
 	;	