o
    oi                     @   s   d dl Z d dlZd dlmZ d dlmZmZm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 dZh d	Zd
Ze eZdedee fddZdededefddZdeeef fddZdee fddZdeddfddZdddZdS )    N)Path)DictIterableIterator)get_cuda_bnb_library_path)NONPYTORCH_DOC_URL)	CUDASpecs)print_dedented)CONDA_PREFIXLD_LIBRARY_PATH>   _PWDHOMEMAILPATHTMUXSHELLOLDPWDSSH_TTYLESSOPEN	LESSCLOSESSH_AUTH_SOCKXDG_DATA_DIRSXDG_RUNTIME_DIRXDG_GREETER_DATA_DIRDBUS_SESSION_BUS_ADDRESSGOOGLE_VM_CONFIG_LOCK_FILE)zcudart64*.dllzlibcudart*.so*znvcuda*.dllpaths_list_candidatereturnc              
   c   s    |  tjD ]K}|sqtj|vrqz5t|}z| s'td|  W W qW n	 ty1   Y nw t	D ]}|
|D ]	}| rD|V  q;q4W q ttfyR   Y qw d S )Nz?The directory listed in your path is found to be non-existent: )splitospathsepsepr   existsloggerwarningOSErrorCUDA_RUNTIME_LIB_PATTERNSglobis_filePermissionError)r   
dir_stringdirlib_patternpth r/   Q/home/ubuntu/.local/lib/python3.10/site-packages/bitsandbytes/diagnostics/cuda.py find_cuda_libraries_in_path_list,   s6   
r1   env_varvaluec                 C   s2   | t v ptj|v o| tvod| vod| vod|vS )NCONDA	BASH_FUNC
)CUDART_PATH_PREFERRED_ENVVARSr    r"   CUDART_PATH_IGNORED_ENVVARS)r2   r3   r/   r/   r0   is_relevant_candidate_env_varB   s   
r9   c                   C   s   dd t j D S )Nc                 S   s    i | ]\}}t ||r||qS r/   )r9   ).0r2   r3   r/   r/   r0   
<dictcomp>P   s     z@get_potentially_lib_path_containing_env_vars.<locals>.<dictcomp>)r    environitemsr/   r/   r/   r0   ,get_potentially_lib_path_containing_env_varsO   s   r>   c                  c   s^    t  } tD ]}|| v r| | }t|E dH  | | q|  D ]\}}t|E dH  q!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
            - don't contain the path separator `/`

    If multiple libraries are found in part 3, we optimistically try one,
    while giving a warning message.
    N)r>   r7   r1   popr=   )candidate_env_varsenvvar	directoryr2   r3   r/   r/   r0   find_cudart_librariesS   s   
rC   
cuda_specsc                 C   sx   t d| j d| j d t| }| std| d | j\}}|dk r*td t dt  | js:td	 d S d S )
Nz%PyTorch settings found: CUDA_VERSION=z, Highest Compute Capability: .z
        Library not found: aZ  . Maybe you need to compile it from source?
        If you compiled from source, try again with `make CUDA_VERSION=DETECTED_CUDA_VERSION`,
        for example, `make CUDA_VERSION=113`.

        The CUDA version for the compile might depend on your conda install, if using conda.
        Inspect CUDA version via `conda list | grep cuda`.
           z
            WARNING: CUDA versions lower than 11 are currently not supported for LLM.int8().
            You will be only to use 8-bit optimizers and quantization routines!
            z:To manually override the PyTorch CUDA version please see: a  
            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-bitsandbytes
            )	printcuda_version_stringhighest_compute_capabilityr   r#   r	   cuda_version_tupler   has_imma)rD   binary_path
cuda_major
cuda_minorr/   r/   r0   print_cuda_diagnosticsk   s.   

rO   c                  C   sZ   t t } | std d S t| dkr)tdtjj d | D ]}td|  qd S d S )NzLCUDA SETUP: WARNING! CUDA runtime files not found in any environmental path.   z
            Found duplicate CUDA runtime files (see below).

            We select the PyTorch default CUDA runtime, which is a]  ,
            but this might mismatch with the CUDA version that is needed for bitsandbytes.
            To override this behavior set the `BNB_CUDA_VERSION=<version string, e.g. 122>` environmental variable.

            For example, if you want to use the CUDA version 122,
                BNB_CUDA_VERSION=122 python ...

            OR set the environmental variable in your .bashrc:
                export BNB_CUDA_VERSION=122

            In the case of a manual override, make sure you set LD_LIBRARY_PATH, e.g.
            export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.2,
            z* Found CUDA runtime at: )listrC   rG   lenr	   torchversioncuda)cudart_pathsr.   r/   r/   r0   print_cuda_runtime_diagnostics   s   
rW   )r   N)loggingr    pathlibr   typingr   r   r   rS   bitsandbytes.cextensionr   bitsandbytes.constsr   bitsandbytes.cuda_specsr   bitsandbytes.diagnostics.utilsr	   r7   r8   r'   	getLogger__name__r$   strr1   boolr9   r>   rC   rO   rW   r/   r/   r/   r0   <module>   s&    
-