
    ;i                        S SK r S SKrS SKrS SKrS r " S S5      rSS jrS\S\\\4   4S jr	   SS	 jr
S
 rS rS SSSS.r\R                  5        V Vs0 s H  u  pX_M	     snn rS\R                   4S jrgs  snn f )    Nc           
      ~   [        U [        R                  R                  5      (       d   e[        R                  5       nUR                  U R                  5      nX2R                  ;  Ga  [        U R                  5      nUR                  R                  U5        UR                  R                  U5        [        UR                  5      S:  ax  UR                  S   R                  5       S:  a:  UR                  S   R                  5       U R                  R                   S   :  d   eUR                  S   UR                  U'   g US   R#                  SUS   R                   S   5      n[        USSS9n[        R$                  " US   5      S:  R'                  [)        [+        [        US   R                   5      S-
  5      5      S9n[        R,                  " US:  5      S   n[        R.                  " XG/5      R1                  5       nXBR                  U'   g UR2                   H  nUR5                  5         M     g )N   r      )reduction_dimzscore   )dim)
isinstancetorchnnLinearOutlierTracerget_instance
get_hvalueweighthvalue2outlier_idxfind_outlier_dimsoutliersappendhvalueslennumelmaxshapeviewabssumlistrangewherecatuniquehooksremove)	moduleinputtracerhvalueoutlier_idxmergeddimsoutlier_idx2hooks	            b/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/bitsandbytes/utils.pyoutlier_hookr0      s   fehhoo....'')Fv}}-F...'6{+f%v!# r"((*Q.r*..06==3F3Fq3IIII060CF%%f- 1X]]2uQx~~b'9:F+F!ANKIIeAh'!+00T%E!HNN@SVW@W:X5Y0ZD ;;tax03L))[$?@GGIK0;%%f-LLDKKM !    c                   F    \ rS rSrSrS rS rS rS rS r	\
S 5       rS	rg)
r   )   Nc                     [        S5      e)NzCall get_instance() instead)RuntimeErrorselfs    r/   __init__OutlierTracer.__init__,   s    899r1   c                 J   S U l         S U l        / U l        / U l        0 U l        SU l        / U l        UR                  5        H^  u  p#[        U[        R                  R                  5      (       d  M0  U R                  R                  UR                  [        5      5        M`     g )NT)last_wcurrent_outlier_dimsr   r   r   initializedr$   named_modulesr   r   r   r   r   register_forward_pre_hookr0   )r7   modelnms       r/   
initializeOutlierTracer.initialize/   s}    $(!"$
'')DA!UXX__--

!!!"="=l"KL *r1   c                     [        U SS5      $ )Nr=   F)getattrr6   s    r/   is_initializedOutlierTracer.is_initialized<   s    t]E22r1   c                 R    UR                   R                  5       R                  5       $ N)datastoragedata_ptr)r7   r   s     r/   r   OutlierTracer.get_hvalue?   s    {{""$--//r1   c                     U R                  5       (       d  [        S5        g U R                  U5      nX R                  ;   a  U R                  U   $ g )Nz$Outlier tracer is not initialized...)rG   printr   r   )r7   r   r)   s      r/   get_outliersOutlierTracer.get_outliersB   sK    ""$$89(,,,**622r1   c                 `    U R                   c  U R                  U 5      U l         U R                   $ rJ   )	_instance__new__)clss    r/   r   OutlierTracer.get_instanceL   s&    == KK,CM}}r1   )r<   r$   r   r   r=   r;   r   )__name__
__module____qualname____firstlineno__rT   r8   rC   rG   r   rQ   classmethodr   __static_attributes__ r1   r/   r   r   )   s5    I:M30  r1   r   c                    U(       a=  [         R                  " SU R                  S   U4U R                  S9R	                  5       $ U R                  U5      nUR                  5       nUR                  5       nXV-
  U-  nUb(  [         R                  " UR                  5       USS9u  pU
$ [         R                  " X:  5      S   n
U
$ )Nr   r   )sizedevice)kr
   )
r   randintr   ra   longstdmeantopkr   r!   )r   r   r   rg   rdmre   stdmstdstdzstdvalidxs              r/   r   r   S   s    
}}QQtgfmmTYY[[
**]
#C88:DWWYFJ& D::cggi4Q7 J kk$-(+Jr1   command_stringreturnc                 0   ^ S mU4S jnU" U 5      u  p#X#4$ )Nc                 &    [        S U  5       5      $ )Nc              3   ^   #    U  H#  oR                  S 5      R                  5       v   M%     g7f)zUTF-8N)decodestrip).0	to_decodes     r/   	<genexpr>6execute_and_return.<locals>._decode.<locals>.<genexpr>g   s'     aH`9%%g.4466H`s   +-)tuple)subprocess_err_out_tuples    r/   _decode#execute_and_return.<locals>._decodef   s    aH`aaar1   c                    > T" [         R                  " [        R                  " U 5      [         R                  [         R                  S9R                  5       5      $ )N)stdoutstderr)
subprocessPopenshlexsplitPIPEcommunicate)rn   r{   s    r/   &execute_and_return_decoded_std_streamsBexecute_and_return.<locals>.execute_and_return_decoded_std_streamsi   sA    N+!! km
 	
r1   r^   )rn   r   std_outstd_errr{   s       @r/   execute_and_returnr   e   s&    b
 >nMGr1   c                 V   U R                  5        GH  u  pV[        [        UR                  5       5      5      S:  a  [	        XaX#U5        [        U[        R                  R                  5      (       d  Md  XR;  d  Mk  U R                  U   nU" UR                  UR                  UR                  SL5      U R                  U'   U(       a<  UR                  U R                  U   l        UR                  U R                  U   l        Uc  M  [        XdS5      nUc  GM  U" U5        GM     U $ )a  
Replace linear modules with a new Linear module.
Parameters:
    model (`torch.nn.Module`):
        Input model or `torch.nn.Module` as the function is run recursively.
    linear_replacement (`torch.nn.Module`):
        The linear module that replaces the old one. Only expects standard arguments.
        If other arguments need to be passed, use a lambda.
    skip_modules (`List[str]`, *optional*, defaults to `lm_head`):
        List of modules names not to convert. Defaults to `lm_head`.
    copy_weights (`bool`):
        Copy the weights from the old linear module to the new one
    post_processing_function (`str`):
        A function name of the replacement linear class that is called
        after processing.
r   N)named_childrenr   r   childrenreplace_linearr   r   r   r   _modulesin_featuresout_featuresbiasr   rF   )	r@   linear_replacementskip_modulescopy_weightspost_processing_functionnamer&   
old_modulefuncs	            r/   r   r   v   s    . ,,.tFOO%&'!+6|Sklfehhoo..43K-J#5""##4'$ENN4 
 .8.?.?t$+,6OOt$)'3vF#L% /& Lr1   c                     [         R                  " U 5      nUR                  S5      n[        R                  " [        U5      [        R                  S9nU$ )z
Pack a dictionary into a torch tensor for storing quant_state items in state_dict.

Parameters:
- source_dict: The dictionary to be packed.

Returns:
A torch tensor containing the packed data.
utf-8)dtype)jsondumpsencoder   tensorr   uint8)source_dictjson_str
json_bytestensor_datas       r/   pack_dict_to_tensorr      s?     zz+&H)J,,tJ/u{{CKr1   c                     [        U R                  5       R                  5       5      nUR                  S5      n[        R
                  " U5      nU$ )z
Unpack a torch tensor into a Python dictionary.

Parameters:
- tensor_data: The torch tensor containing the packed data.

Returns:
A Python dictionary containing the unpacked data.
r   )bytescpunumpyrs   r   loads)r   r   r   unpacked_dicts       r/   unpack_tensor_to_dictr      sB     {(..01J  )HJJx(Mr1   r      r   )rowcol32
col_turing
col_amperetc                     U R                   R                  S:X  a  [        R                  R	                  5         g U R                   R                  S:X  a  [        R
                  R	                  5         g g )Ncudaxpu)ra   typer   r   synchronizer   )r   s    r/   sync_gpur      sG    xx}}

 	
%			 
 r1   )r   g      @NF))lm_headFN)r   r   r   r   r0   r   r   strry   r   r   r   r   "LINEAR_8BIT_WEIGHTS_FORMAT_MAPPINGitems*INVERSE_LINEAR_8BIT_WEIGHTS_FORMAT_MAPPINGTensorr   )r   rl   s   00r/   <module>r      s       B' 'T$s uS#X ( !*Z"" ./![\%] "KmKsKsKu-vKuKTciKu-v *    .ws   A=