
    9io5                        S SK rS SKrS SKrS SKrS SKJr  S SKrS SKJ	r	  S SK
r
S SKJrJr  S SKJrJrJrJr  \R&                  " \5      rS\S\4S jr " S	 S
5      r " S S\5      rS\\   4S jrS\S\4S jr " S S\5      rS\4S jr\" 5       rSr Sr!\
RD                  RF                  (       a  Sr Sr!O?\
RH                  RK                  5       (       a  Sr!O\
RL                  RN                  (       a  Sr! \" 5       r(g! \) a@  r*\!S;   a  \" S5      r( Sr*C*g\" \*5      r+\RY                  S\+ 3SS9  \" \+5      r( Sr*C*gSr*C*ff = f)    N)Path)Optional)DYNAMIC_LIBRARY_SUFFIXPACKAGE_DIR)	CUDASpecsget_cuda_specsget_cuda_version_tupleget_rocm_gpu_arch
cuda_specsreturnc                    [         R                  R                  (       a  SOSnSU U R                   [         3n[
        R                  R                  S5      nU(       ad  [        R                  " SSU 3USS9n[         R                  R                  (       a  [        SU S	35      e[        R                  S
U SU S35        [        U-  $ )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.
rocmcudalibbitsandbytes_BNB_CUDA_VERSIONzcuda\d+   )countzBNB_CUDA_VERSION=zM detected for ROCm!! 
Clear the variable and retry: export BNB_CUDA_VERSION=
zWARNING: BNB_CUDA_VERSION=z( environment variable detected; loading z.
This can be used to load a bitsandbytes version built with a CUDA 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=
)torchversionhipcuda_version_stringr   osenvirongetresubRuntimeErrorloggerwarningr   )r   prefixlibrary_nameoverride_values       g/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/bitsandbytes/cextension.pyget_cuda_bnb_library_pathr$      s     }}((VfF%fXj.L.L-MNdMefLZZ^^$67NvvjD0@*A<WXY==#N#3 4K L  	((88`am`n ov v	
 %%    c                       \ rS rSr% \R
                  \S'   SrS\R
                  4S jr\	R                  S 5       rS rSrg	)
BNBNativeLibrary-   _libFlibc                     Xl         g Nr)   )selfr*   s     r#   __init__BNBNativeLibrary.__init__1   s    	r%   c                 L   ^ [        U R                  TS 5      nUb  U$ U4S jnU$ )Nc                  "   > [        ST S35      e)NzMethod 'zm' not available in CPU-only version of bitsandbytes.
Reinstall with GPU support or use CUDA-enabled hardware.)r   )argskwargsnames     r#   throw_on_call3BNBNativeLibrary.__getattr__.<locals>.throw_on_call;   s#    4& !K K r%   )getattrr)   )r.   r5   fnr6   s    `  r#   __getattr__BNBNativeLibrary.__getattr__4   s,    TYYd+>I	 r%   c                 $    U R                  U5      $ r,   r:   )r.   items     r#   __getitem__BNBNativeLibrary.__getitem__C       %%r%   r-   N)__name__
__module____qualname____firstlineno__ctCDLL__annotations__compiled_with_cudar/   	functoolscacher:   r?   __static_attributes__ r%   r#   r'   r'   -   s>    
''MBGG  __ &r%   r'   c                   H   ^  \ rS rSrSrS\R                  4U 4S jjrSrU =r	$ )CudaBNBNativeLibraryG   Tr*   c                    > [         TU ]  U5        [        R                  UR                  l        [        R                  UR                  l        [        R                  UR                  l        g r,   )superr/   rF   c_void_pget_contextrestypeget_cusparsecget_managed_ptr)r.   r*   	__class__s     r#   r/   CudaBNBNativeLibrary.__init__J   sD    "$++#%;; '){{$r%   rM   )
rB   rC   rD   rE   rI   rF   rG   r/   rL   __classcell__)rX   s   @r#   rO   rO   G   s    3BGG 3 3r%   rO   c                     S[         R                  5        S[         3n / n[        [        5      R
                  R                  U 5       H}  n[         R                  5        S3n[        R                  " X2R                  5      nU(       d  MC  [        UR                  S5      5      nUS-  nUS-  nUR                  U SU 35        M     [        U5      $ )zNGet formatted CUDA versions from existing library files using cuda_specs logicr   *z(\d+)r   
   .)BNB_BACKENDlowerr   r   __file__parentglobr   searchr5   intgroupappendsorted)lib_patternversionsr*   patternmatchver_codemajorminors           r#   "get_available_cuda_binary_versionsrp   Q   s    $[%6%6%8$9;Q:RSKHH~$$))+6!'')*%0		'88,55;;q>*HNErMEOOugQug./ 7 (r%   version_strc                 J    U R                  5       (       a  U SS  SU S    3$ U $ )zWConvert raw version string (e.g. '118' from env var) to formatted version (e.g. '11.8')Nr^   )isdigit)rq   s    r#   parse_cuda_versionru   `   s5    cr"#1[_$566r%   c                   r    \ rS rSrSrS\4S jr  SS\\   S\S\S	\\   S
\4
S jjr	S
\4S jr
S rS rSrg) ErrorHandlerMockBNBNativeLibraryg   a  
Mock library handler that defers errors until native methods are called.

This class serves as a fallback when the native bitsandbytes library fails to load.
It captures the original error and generates detailed troubleshooting guidance.

Key behaviors:
- Allows attribute access and method assignment without immediate errors
- Throws a RuntimeError with diagnostic information only when a native method is called, as otherwise it would error out on import, breaking backward compatibility
- Handles both missing CUDA dependencies and version mismatch scenarios

Error scenarios covered:
1. Missing shared library dependencies (e.g., libcudart.so not in LD_LIBRARY_PATH or through PyTorch CUDA installation)
2. CUDA version mismatch between PyTorch and available pre-compiled binaries
3. Completely missing pre-compiled binaries when CUDA is detected
4. Custom BNB_CUDA_VERSION override but mismatch
5. CPU-only installation attempts when GPU functionality is requested

	error_msgc                    Xl         [        5       U l        [        5       U l        [
        R                  R                  S5      U l        U R                  (       a  [        U R                  5      O3U R                  (       a!  U R                  S    SU R                  S    3OSU l
        SU;   a  U R                  5       U l        g U R                  U R                  U R                  (       a!  U R                  S    SU R                  S    3OSU R                   (       a  SU R                    S3OS	U R                  S
9U l        g )Nr   r   r^   r   unknownzcannot open shared object filezOriginal error: 
 )available_versionsuser_cuda_versionoriginal_errorrequested_version)ry   r	   r   rp   r~   r   r   r   r"   ru   r   _format_dependency_errorformatted_error_format_lib_error_message)r.   ry   s     r#   r/   )ErrorHandlerMockBNBNativeLibrary.__init__|   s"   "!7!9"D"F jjnn-?@ "" t223 %% **1-.a0F0Fq0I/JK 	 ,y8#'#@#@#BD #'#A#A#'#:#:)) &*%;%;A%>$?qAWAWXYAZ@["\HL!1$..1AD\^"&"8"8 $B $D r%   Nr~   r   r   r   r   c                    SnSU;   n[          S3U;   nU(       a  SnONU(       aG  U(       a  SSR                  U5      -   OSnU(       a!  S[          S[          S	U S
[          SU SU S3OSnSn	U(       a
  S[          S3O	S[          S3n
U(       a  S[          S[          S3OSnU(       d  SO[        (       d  SOSnS[         R                  5        S3nU U	 U
 U U U SU 3$ )z:Format detailed error message for library loading failuresr}   z#libbitsandbytes_cpu.so: cannot openz binary not foundu9   
🚨 Failed to load CPU-only bitsandbytes library 🚨

z
  - NONE   
🚨 u!    VERSION MISMATCH 🚨
Requested z version:          z
Detected PyTorch z version:   z"
Available pre-compiled versions: zU

This means:
The version you're trying to use is NOT distributed with this package

u   
🚨 Forgot to compile the bitsandbytes library? 🚨
1. You're not using the package but checked-out the source code
2. You MUST compile from source

zTAttempted to use bitsandbytes native library functionality but it's not available.

z^This typically happens when:
1. bitsandbytes doesn't ship with a pre-compiled binary for your zR version
2. The library wasn't compiled properly during installation from source

zlThis typically happens when you checked the code out from source and your torch installation doesn't detect z on your machine.

zVTo make bitsandbytes work, the compiled library version MUST exactly match the linked z version.
If your zL version doesn't have a pre-compiled binary, you MUST compile from source.

zPCOMPILE FROM SOURCE for CPU-only:
  `cmake -DCOMPUTE_BACKEND=cpu -S . && make`

aN  You have two options:
1. COMPILE FROM SOURCE (required if no binary exists):
   https://huggingface.co/docs/bitsandbytes/main/en/installation#cuda-compile
2. Use BNB_CUDA_VERSION to specify a DIFFERENT CUDA version from the detected one, which is installed on your machine and matching an available pre-compiled version listed above

zYou can COMPILE FROM SOURCE as mentioned here:
   https://huggingface.co/docs/bitsandbytes/main/en/installation?backend=AMD+ROCm#amd-gpu
u   🔍 Run this command for detailed diagnostics:
python -m bitsandbytes

If you've tried everything and still have issues:
1. Include ALL version info (operating system, bitsandbytes, pytorch, , python)
2. Describe what you've tried in detail
3. Open an issue with this information:
   https://github.com/bitsandbytes-foundation/bitsandbytes/issues

r|   )r_   joinHIP_ENVIRONMENTr`   )r.   r~   r   r   r   analysisno_cpu_lib_foundno_cuda_lib_foundversion_list_strbase_msgtroubleshootingnotecompile_instructionsdiagnosticss                 r#   r   :ErrorHandlerMockBNBNativeLibrary._format_lib_error_message   s    @NR*m+<=OUHOax(--8J*KKgm & k] +!!,-@AR@S T((3}LAR@S T88H7I J``6  l !	TT_S` a^_
 @  AL  @M  Mb  c 	 ! iithu v&-'uw  	 % c #?Hn 	"U VaUfUfUhTi jTU 	 H:o%6tf=Q<RSaRbbdepdqrrr%   c                    U R                   R                  S5      n[        U5      S:  a  US   R                  5       OSnSU R                  ;   a  U R                  R                  S5      S   OU R                  nS[
         SU S[
         SU S	[
         SU S
[
         S[
        R                  5        SU S[        (       d  SOS SU R                    S[
        R                  5        S3$ )z1Format error message for missing shared libraries:r   zunknown libraryr^   r   z" SETUP ERROR: Missing dependency: u    🚨

 zp.x runtime libraries were not found in the LD_LIBRARY_PATH.

To fix this, make sure that:
1. You have installed z!.x toolkit on your system
2. The z runtime libraries are in your LD_LIBRARY_PATH

You can add them with (and persist the change by adding the line to your .bashrc):
   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/-z.x/                    lib64r*   z

Original error: u   

🔍 Run this command for detailed diagnostics:
python -m bitsandbytes

If you've tried everything and still have issues:
1. Include ALL version info (operating system, bitsandbytes, pytorch, r   )ry   splitlenstripr   r_   r`   r   )r.   error_partsmissing_libcuda_major_versions       r#   r   9ErrorHandlerMockBNBNativeLibrary._format_dependency_error   s    nn**3/03K0@10Dk!n**,J[474;Q;Q4QD""((-a0W[WmWm 	
 k]"D[MQZm1/0 1%%0M3E2F G!] #CCNCTCTCVBWWXYkXl m$3OW? @#~~. /U VaUfUfUhTi jTU	
r%   c                    ^ ^ UU 4S jnU$ )zPReturn a dummy function that throws when called, rather than on attribute accessc                  :   > [        TR                   ST S35      e)Nz*Native code method attempted to call: lib.z())r   r   )r3   r4   r5   r.   s     r#   r6   CErrorHandlerMockBNBNativeLibrary.__getattr__.<locals>.throw_on_call  s&    $"6"6!77abfaggijkkr%   rM   )r.   r5   r6   s   `` r#   r:   ,ErrorHandlerMockBNBNativeLibrary.__getattr__  s    	l r%   c                 $    U R                  U5      $ r,   r=   )r.   r5   s     r#   r?   ,ErrorHandlerMockBNBNativeLibrary.__getitem__  rA   r%   )r~   ry   r   r"   r   r   )r}   N)rB   rC   rD   rE   __doc__strr/   listr   r   r   r:   r?   rL   rM   r%   r#   rw   rw   g   s{    (# < !+/Os IOs Os 	Os
 $C=Os 
Osb
# 
8&r%   rw   c                     [        5       n [        S[         3-  nU (       a7  [        U 5      nUR	                  5       (       d  [        S[         SU 35      eUn[        R                  R                  (       a  [        S[         3-  n[        R                  SU 35        [        R                  R                  [        U5      5      n[!        US5      (       a  [#        U5      $ [%        U5      $ )zV
Load CUDA library XOR CPU, as the latter contains a subset of symbols of the former.
libbitsandbytes_cpuzConfigured z binary not found at libbitsandbytes_xpuz*Loading bitsandbytes native library from: rT   )r   r   r   r$   existsr   r_   r   _C_has_xpur   debugrF   cdllLoadLibraryr   hasattrrO   r'   )r   binary_pathcuda_binary_pathdlls       r#   get_native_libraryr     s      !J"56L5M NNK4Z@&&(([M9NO_N`abb&xx!&9:P9Q$RR
LL=k]KL ''

c+.
/CsM""#C((C  r%   FCPUTROCmCUDAXPU)r   r   z'XPU/CPU can run without native library.z!bitsandbytes library load error: )exc_info)-ctypesrF   rJ   loggingr   pathlibr   r   typingr   r   bitsandbytes.constsr   r   bitsandbytes.cuda_specsr   r   r	   r
   	getLoggerrB   r   r$   r'   rO   r   r   rp   ru   rw   r   ROCM_GPU_ARCHr   r_   r   r   r   is_availabler   r   r*   	Exceptionery   errorrM   r%   r#   <module>r      sX      	  	   C h h			8	$&) & &8& &43+ 3DI C C e&'7 e&P!, !: "#==OK
ZZK
XXK:

C :n$./XYF	/	{; 	 	
 /y9:s   9D EE#EE