
    o9iF                     t   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j+                  d        G d d      Zd Zdedee   fdZdee   dee   fdZdee   dee   fdZdedee   fdZdedee   fdZdee   ddfdZdeedf   fdZd Z d Z!d Z"y)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                   L    e Zd ZdZd Zd Zd Zd Zd Zd
dZ	d Z
ed	        Zy)	CUDASetupNc                     t        d      )NzCall get_instance() instead)RuntimeErrorselfs    f/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/bitsandbytes/cuda_setup/main.py__init__zCUDASetup.__init__+   s    899    c                    t        | dd      ry t        | j                         d| _        | j                  sx| j	                  d       | j	                  d       | j	                  d       | j	                  d       | j	                  d       | j	                  d	       | j	                  d
       y | j
                  | j	                  d       | j	                  d       | j	                  d       | j	                  d       | j	                  d       | j	                  d       | j	                  d       | j	                  d       | j	                  d       y d| j                   }t        | j                        dk  r|dz  }nz| j                  dk(  r|dz  }ne| j                  d d dk(  r!t        | j                  d         dkD  r|dz  }n2| j                  dk(  r#| j	                  d       | j	                  d       y t        | j                        }|s|d z  }| j	                  d!       | j	                  d"       | j	                  d#       | j	                  |       | j	                  d$       y )%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 ~/.bashrczCUDA 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.zCUDA SETUP: Solution 2a): Download CUDA install script: wget https://github.com/TimDettmers/bitsandbytes/blob/main/cuda_install.shzCUDA SETUP: Solution 2b): Install desired CUDA version to desired location. The syntax is bash cuda_install.sh CUDA_VERSION PATH_TO_INSTALL_INTO.zCUDA 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.zCUDA 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_cublaslts      r   generate_instructionszCUDASetup.generate_instructions.   s   4%(&djj
""   T  U   J  Kstyz   o  p   X  Y   g  h#   F  G   X  Y   D  E   o  p   X  Yvw   d  e   s  t   h  i"4#;#;"<=t''(1,&H%%.'H%%bq)T1c$:R:RST:U6VYZ6Z'H%%.jk   v  w .dgg6#HcdVW,-8$45r   c                 X    t        | dd      sd| _        d | _        d| _        d| _        y y )NinitializedF)r   has_printedlibr*   r   r   s    r   
initializezCUDASetup.initialize_   s1    t]E2$DDH$DDJ	 3r   c           	      J   t         j                  j                         rdt        j                  v rrt        t        j                  d         dkD  rRt        dd dt        j                  d    dd d       | j                  d d t        j                  d    dz   | _        y y y y )	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   s    r   manual_overridezCUDASetup.manual_overridef   s    ::""$!RZZ/rzz"456:D )D EGJJOaDbCcvhd, . (,'7'7'<"**M_B`Aaad?e'eD$ ; 0 %r   c                    d| _         g | _        t               \  }}}}|| _        t        j
                  j                         | _        || _        || _	        || _
        | j                          t        t              j                  j                  }|| j                  z  }	 |j                         sk| j!                  d| d       d}| j!                  d| d       ||z  }|j                         rt        j
                  j                         r| j!                  d       | j!                  d       | j!                  d	       | j!                  d
       | j!                  d       | j!                  d       | j!                  d       | j!                  d       | j!                  d       | j!                  d       | j!                  d       | j!                  d       | j#                          t%        d      t&        j(                  j+                  |      | _        y | j!                  d| d       t&        j(                  j+                  |      | _        y # t$        $ r$}| j!                  t/        |             Y d }~y d }~ww xY 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:z1. 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!zCUDA SETUP: If you compiled from source, try again with `make CUDA_VERSION=DETECTED_CUDA_VERSION` for example, `make CUDA_VERSION=113`.zCUDA SETUP: The CUDA version for the compile might depend on your conda install. Inspect CUDA version via `conda list | grep cuda`.r0   zCUDA SETUP: Setup Failed!zCUDA SETUP: Loading binary )r*   cuda_setup_logevaluate_cuda_setupr    r3   r4   r5   r   r%   r!   r8   r9   r   __file__parentexistsr   r(   	ExceptionctcdllLoadLibraryr,   str)	r   r8   r    r%   r!   package_dirbinary_pathlegacy_binary_nameexs	            r   run_cuda_setupzCUDASetup.run_cuda_setupt   sJ    <O<Q9["&9&#jj557#6 &8n++22!D$4$44	(%%'""%UVaUb  cN  $O  P%="""%?@R?SSV#WX),>>"))+uzz/F/F/H&&r*&&'@A&&']^&& (t u&&'EF&&'>?&&'XY&&'lm&&  (q  r&&  (m  n&&v.&&r*..0#$?@@77..{;""%@S#QR77..{; 	(s2w''	(s    E;I 9I 	JI>>Jc                 >    | j                   j                  ||f       y N)r=   appendr   msg
is_warnings      r   r   zCUDASetup.add_log_entry   s    ""C#45r   c                 ^    | j                   D ]  \  }}|rt        |       t        |         y rM   )r=   r   r   rO   s      r   print_log_stackzCUDASetup.print_log_stack   s,    #22 	OCS	c
		r   c                     | j                   0| j                  |       | _         | j                   j                          | j                   S rM   )	_instance__new__r-   )clss    r   get_instancezCUDASetup.get_instance   s6    == KK,CMMM$$&}}r   )F)__name__
__module____qualname__rU   r   r(   r-   r9   rK   r   rS   classmethodrX    r   r   r   r   (   sA    I:/6bf*(X6  r   r   c                     d}| g| j                  d      \  }}t        |      dk  st        |      dk(  r5t        |      dk  r't        j                         j	                  dd       |S d}|S )NF.      zWARNING: 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rQ   )splitr#   r   rX   r   )r%   r'   cc_majorcc_minors       r   r$   r$      s    L	~XXc](x=1X!!3H8I""$22 4Z gk 3 l   Lr   paths_list_candidatereturnc                 b    | j                  d      D ch c]  }|st        |       c}S c c}w )N:)rc   r   )rf   ld_paths     r   extract_candidate_pathsrk      s'    )=)C)CC)HTgGDMTTTs   ,,candidate_pathsc                 V   t               }| D ]$  }	 |j                         r|j                  |       & | |z
  }|r(t        j                         j                  d| d       |S # t        $ r)}|j                  t        j
                  k7  r|Y d }~d }~wt        $ r
}Y d }~d }~ww xY w)NzMThe following directories listed in your path were found to be non-existent: Frb   )
setrA   addOSErrorerrnoENAMETOOLONGPermissionErrorr   rX   r   )rl   existent_directoriespathexcpexnon_existent_directoriess         r   remove_non_existent_dirsry      s    &)e 	{{}$((. +:<P*P .. 0  890;GL 	/ 	N    	yyE...	 / 		s   !A%%	B(.BB(#B(c                     t               }t        D ]1  }| D ]*  }||z  j                         s|j                  ||z         , 3 |S rM   )rn   r
   is_filero   )rl   pathslibnameru   s       r   get_cuda_runtime_lib_pathsr~      sN    EE$ *# 	*Dw'')		$.)	** Lr   c                 *    t        t        |             S )zc
    Searches a given environmental var for the CUDA runtime library,
    i.e. `libcudart.so`.
    )ry   rk   rf   s    r   resolve_paths_listr      s    
 $$;<P$QRRr   c                 *    t        t        |             S rM   )r~   r   r   s    r   find_cuda_lib_inr      s    %/0 r   results_pathsc                     t        |       dkD  r3dt         d|  d}t        j                         j	                  |d       y y )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.2Trb   )r"   r
   r   rX   r   )r   warning_msgs     r   warn_in_case_of_duplicatesr      sS    
=A01- IK K 	 	 ..{t.L r   c                     t               } t               }d| v rut        | d         dz  }t        t	        |            }t        |       |r|j                  |       t        j                         j                  | d    dt         dd       d| v r^t        | d         }|r|j                  |       t        |       t        j                         j                  | d    dt         dd       | j                         D ci c]  \  }}|dvr|| }}}t               }|j                         D ]  \  }}|j                  t        |             ! t        |      d	k(  r=t        j                         j                  d
       |j                  t        d             t        |       t        j                         }|j                  d|        |rt        t        |            S dS c c}}w )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...Trb   LD_LIBRARY_PATH>   r   r   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	   rn   r   r   rF   r   updater   rX   r   r
   itemsr"   nextiter)	candidate_env_varscuda_runtime_libsconda_libs_pathconda_cuda_libslib_ld_cuda_libsenv_varvalueremaining_candidate_env_vars
cuda_setups	            r   determine_cuda_runtime_lib_pathr      s    FG++1.ABUJ*3+?@"?3$$_5 ..2D^2T1UUf !!I0KW[ 	/ 	] ..+,>?P,QR$$%56"#34 ..2DEV2W1XXi !!I0KW[ 	/ 	] .@-E-E-G$)7E== 	$  $
 6<<> :  !1%!89: " ..  0U  	V  !12I!JK01'')JOPaObcd,=4&'(G4G%$s   G)c                      t        t        t        j                  j                  j                  d            \  } }| dk  r#t        j                         j                  d       |  | S )Nr_      zCUDA 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#   r3   versionr4   rc   r   rX   r   )majorminors     r   get_cuda_versionr   6  s[    sEMM..44S9:LE5rz ..  0I  	JWUGr   c                     g } t        t        j                  j                               D ]W  }t        j                  j	                  t        j                  j                  |            \  }}| j                  | d|        Y | S )Nr_   )ranger3   r4   device_countget_device_capabilitydevicerN   )ccsird   re   s       r   get_compute_capabilitiesr   >  so    
C5::**,- -"ZZ==ejj>O>OPQ>RS(

hZq
+,- Jr   c                  N   t         j                         } dt        j                  vst	        t        j                  d         dk(  rE| j                  d       | j                  d       | j                  dd       | j                  d       t        j                  j                         syt               }t               }|j                          |d	   }t               }| j                  d
| d| d       | j                  d       t        |      }|rd| d}n	 d| d}||||fS )NBITSANDBYTES_NOWELCOME0r<   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/issuesr0   )r;   NNNz1CUDA SETUP: PyTorch settings found: CUDA_VERSION=z, Highest Compute Capability: r_   zCUDA SETUP: To manually override the PyTorch CUDA version please see:https://github.com/TimDettmers/bitsandbytes/blob/main/how_to_use_nonpytorch_cuda.mdlibbitsandbytes_cudar2   z_nocublaslt.so)r   rX   r6   r7   rF   r   r3   r4   r5   r   r   sortr   r$   )r   r    r   r%   r!   r'   r8   s          r   r>   r>   G  sG   '')Jrzz1SD\9]5^be5e  $  !?@  "v C	E  (::""$-W13K
"
$CHHJ	RB*,PQdPe  fD  EG  DH  HI  J  K  s t
 *"-L ,-@,AEP,-@,APR)<<<r   )#__doc__ctypesrC   r6   rq   r3   warningsr   	itertoolsr   pathlibr   typingr   r   env_varsr	   r
   list__annotations__backup_pathsrN   r   r$   rF   rk   ry   r~   r   r   r   r   r   r   r>   r]   r   r   <module>r      s  $  	       B U 4 T    9 :G GT	U# U#d) U c$i  CI  (D	 c$i SS SSY S3 3t9 Mc$i MD M7HtTz): 7Hv%=r   