o
    oiS                  
   @   s
  d dl Zd dlZd dlZd dlmZ d dlZd dlZd dlm	Z	m
Z
 d dlmZmZ eeZdedefddZG d	d
 d
ZG dd deZdefddZze ZW dS  ey Z z$dZejde dd ej ryed W Y dZ[dS W Y dZ[dS dZ[ww )    N)Path)DYNAMIC_LIBRARY_SUFFIXPACKAGE_DIR)	CUDASpecsget_cuda_specs
cuda_specsreturnc                 C   sX   d| j  t }tjd}|r(tjdd| |dd}td| d| d	 t	| S )
z
    Get the disk path to the CUDA BNB native library specified by the
    given CUDA specs, taking into account the `BNB_CUDA_VERSION` override environment variable.

    The library is not guaranteed to exist at the returned path.
    libbitsandbytes_cudaBNB_CUDA_VERSIONzcuda\d+cuda   )countzWARNING: BNB_CUDA_VERSION=z( environment variable detected; loading a  .
This 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
)
cuda_version_stringr   osenvirongetresubloggerwarningr   )r   library_nameoverride_value r   K/home/ubuntu/.local/lib/python3.10/site-packages/bitsandbytes/cextension.pyget_cuda_bnb_library_path   s   r   c                   @   s<   e Zd ZU ejed< dZdejfddZdd Zdd	 Z	d
S )BNBNativeLibrary_libFlibc                 C   s
   || _ d S N)r   selfr   r   r   r   __init__*   s   
zBNBNativeLibrary.__init__c                 C      t | j|S r   getattrr   r    itemr   r   r   __getattr__-      zBNBNativeLibrary.__getattr__c                 C   r"   r   r#   r%   r   r   r   __getitem__0   r(   zBNBNativeLibrary.__getitem__N)
__name__
__module____qualname__ctCDLL__annotations__compiled_with_cudar!   r'   r)   r   r   r   r   r   &   s   
 
r   c                       s(   e Zd ZdZdejf fddZ  ZS )CudaBNBNativeLibraryTr   c                    s.   t  | tj|j_tj|j_tj|j_d S r   )superr!   r-   c_void_pget_contextrestypeget_cusparsecget_managed_ptrr   	__class__r   r   r!   7   s   

zCudaBNBNativeLibrary.__init__)r*   r+   r,   r0   r-   r.   r!   __classcell__r   r   r8   r   r1   4   s    r1   c                  C   s~   t dt  } t }|rt|}| r|} ntd| td|   tj	
t| }t|dr6t|S td t|S )Nlibbitsandbytes_cpuz1Could not find the bitsandbytes CUDA binary at %rz*Loading bitsandbytes native library from: r4   zThe installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers, 8-bit multiplication, and GPU quantization are unavailable.)r   r   r   r   existsr   r   debugr-   cdllLoadLibrarystrhasattrr1   r   )binary_pathr   cuda_binary_pathdllr   r   r   get_native_library>   s   
rE   z,Could not load bitsandbytes native library: T)exc_infoa  
CUDA Setup failed despite CUDA being available. Please run the following command to get more information:

python -m bitsandbytes

Inspect the output of the command and see if you can locate CUDA libraries. You might need to add them
to your LD_LIBRARY_PATH. If you suspect a bug, please take the information from python -m bitsandbytes
and open an issue at: https://github.com/bitsandbytes-foundation/bitsandbytes/issues
)ctypesr-   loggingr   pathlibr   r   torchbitsandbytes.constsr   r   bitsandbytes.cuda_specsr   r   	getLoggerr*   r   r   r   r1   rE   r   	Exceptioneerrorr   is_availabler   r   r   r   r   <module>   s2    


