
    o9iK                         d dl Z d dlZd dlZd dlmZ d Z G d de      ZddZdgddfd	Z	d
e
dee
e
f   fdZdgddfdZ	y)    N)Tuplec           
      r   t        | t        j                  j                        sJ t        j                         }|j                  | j                        }||j                  vrt        | j                        }|j                  j                  |       |j                  j                  |       t        |j                        dkD  rv|j                  d   j                         dkD  r9|j                  d   j                         | j                  j                   d   k  sJ |j                  d   |j                  |<   y |d   j#                  d|d   j                   d         }t        |dd      }t        j$                  |d         dkD  j'                  t)        t+        t        |d   j                         dz
                    }t        j,                  |dkD        d   }t        j.                  ||g      j1                         }||j                  |<   y |j2                  D ]  }|j5                           y )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	            \/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/bitsandbytes/utils.pyoutlier_hookr1      s   fehhoo...'')Fv}}-FV...'6{+f%v!# r"((*Q.r*..06==3F3Fq3IIII060CF%%f- 1X]]2uQx~~b'9:F+F!ANKIIeAh'*//Ds58>>?RST?T9U4V/WD ;;tax03L))[,$?@GGIK0;F%%f-LL 	DKKM	    c                   >    e Zd ZdZd Zd Zd Zd Zd Ze	d        Z
y)r   Nc                     t        d      )NzCall get_instance() instead)RuntimeErrorselfs    r0   __init__zOutlierTracer.__init__*   s    899r2   c                 <   d | _         d | _        g | _        g | _        i | _        d| _        g | _        |j                         D ]X  \  }}t        |t        j                  j                        s+| j                  j                  |j                  t                     Z y )NT)last_wcurrent_outlier_dimsr   r   r   initializedr%   named_modulesr   r   r   r   r   register_forward_pre_hookr1   )r7   modelnms       r0   
initializezOutlierTracer.initialize-   s    $(!"$
'') 	MDAq!UXX__-

!!!"="=l"KL	Mr2   c                     t        | dd      S )Nr<   F)getattrr6   s    r0   is_initializedzOutlierTracer.is_initialized:   s    t]E22r2   c                 R    |j                   j                         j                         S N)datastoragedata_ptr)r7   r   s     r0   r   zOutlierTracer.get_hvalue=   s    {{""$--//r2   c                     | j                         st        d       y | j                  |      }|| j                  v r| j                  |   S y )Nz$Outlier tracer is not initialized...)rE   printr   r   )r7   r   r*   s      r0   get_outlierszOutlierTracer.get_outliers@   sJ    ""$89(T,,,**622r2   c                 ^    | j                   | j                  |       | _         | j                   S rG   )	_instance__new__)clss    r0   r   zOutlierTracer.get_instanceJ   s&    == KK,CM}}r2   )__name__
__module____qualname__rO   r8   rB   rE   r   rM   classmethodr    r2   r0   r   r   '   s5    I:M30  r2   r   Fc                    |r?t        j                  d| j                  d   |f| j                        j	                         S | j                  |      }|j                         }|j                         }||z
  |z  }| j                  |      }	|	j                         }
|	j                         }|	|
z
  |z  }|+t        j                  |	j                         |d      \  }}|S t        j                  ||kD        d   }|S )Nr   r   )sizedevice)kr   )
r   randintr   rY   longmeanstdtopkr   r"   )r   r   r	   r_   rdmrA   mmmstdzmr^   stdmstdstdzstdvalidxs                  r0   r   r   P   s    
}}QQtgfmmTYY[[M"A	
B557D
B$B
**]
#C88:DWWYFHfD::cggi4Q7S J kk$-(+Jr2   lm_headc                 4   | j                         D ]  \  }}t        t        |j                                     dkD  rt	        |||||       t        |t        j                  j                        s`||vse| j                  |   } ||j                  |j                  |j                  du      | j                  |<   |r<|j                  | j                  |   _        |j                  | j                  |   _        |t        ||d      }| ||        | S )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_fun_name (`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   rD   	r?   linear_replacementskip_modulescopy_weightspost_processing_functionnamer'   
old_modulefuncs	            r0   rn   rn   f   s    $ ,,. 1ftFOO%&'!+6#5|\Sklfehhoo.4|3K-J#5""##4'$ENN4 
 .8.?.?t$+,6OOt$)'3f&>Et"DL#1$ Lr2   command_stringreturnc                 2    d fd} ||       \  }}||fS )Nc                 &    t        d | D              S )Nc              3   Z   K   | ]#  }|j                  d       j                          % yw)zUTF-8N)decodestrip).0	to_decodes     r0   	<genexpr>z6execute_and_return.<locals>._decode.<locals>.<genexpr>   s,      
 W%++-
s   )+)tuple)subprocess_err_out_tuples    r0   _decodez#execute_and_return.<locals>._decode   s     
5
 
 	
r2   c                      t        j                  t        j                  |       t         j                  t         j                        j                               S )N)stdoutstderr)
subprocessPopenshlexsplitPIPEcommunicate)r{   r   s    r0   &execute_and_return_decoded_std_streamszBexecute_and_return.<locals>.execute_and_return_decoded_std_streams   sA    N+!! km
 	
r2   rV   )r{   r   std_outstd_errr   s       @r0   execute_and_returnr      s)    

 >nMGWGr2   c                 4   | j                         D ]  \  }}t        t        |j                                     dkD  rt	        |||||       t        |t        j                  j                        s`||vse| j                  |   } ||j                  |j                  |j                  du      | j                  |<   |r<|j                  | j                  |   _        |j                  | j                  |   _        |t        ||d      }| ||        | S )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_fun_name (`str`):
            A function name of the replacement linear class that is called
            after processing.
    r   Nrk   rs   s	            r0   rn   rn      s    " ,,. 1ftFOO%&'!+6#5|\Sklfehhoo.4|3K-J#5""##4'$ENN4 
 .8.?.?t$+,6OOt$)'3f&>Et"DL#1$ Lr2   )r   g      @NF)r   r   r   typingr   r1   objectr   r   rn   strr   rV   r2   r0   <module>r      sk       B'F 'R, =F;UZuy $Ps uS#X * =F;UZuy #r2   