o
    3wi6                     @   s   d dl mZmZ d dlZd dlZd dlmZ d dl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 )    )IterableIteratorN)Path)get_cuda_bnb_library_path)	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-   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/bitsandbytes/diagnostics/cuda.py find_cuda_libraries_in_path_list+   s6   
r/   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)r0   r1   r-   r-   r.   is_relevant_candidate_env_varA   s   
r7   c                   C   s   dd t j D S )Nc                 S   s    i | ]\}}t ||r||qS r-   )r7   ).0r0   r1   r-   r-   r.   
<dictcomp>O   s     z@get_potentially_lib_path_containing_env_vars.<locals>.<dictcomp>)r   environitemsr-   r-   r-   r.   ,get_potentially_lib_path_containing_env_varsN   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<   r5   r/   popr;   )candidate_env_varsenvvar	directoryr0   r1   r-   r-   r.   find_cudart_librariesR   s   
rA   
cuda_specsc                 C   sP   t d| j d| j d t| }| std| d | js&td d S d S )Nz%PyTorch settings found: CUDA_VERSION=z, Highest Compute Capability: .z 
            Library not found: z8. Maybe you need to compile it from source?
            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   has_imma)rB   binary_pathr-   r-   r.   print_cuda_diagnosticsj   s"   
rI   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: )listrA   rD   lenr   torchversioncuda)cudart_pathsr,   r-   r-   r.   print_cuda_runtime_diagnostics   s   
rQ   )r   N)collections.abcr   r   loggingr   pathlibr   rM   bitsandbytes.cextensionr   bitsandbytes.cuda_specsr   bitsandbytes.diagnostics.utilsr   r5   r6   r%   	getLogger__name__r"   strr/   boolr7   dictr<   rA   rI   rQ   r-   r-   r-   r.   <module>   s$    
