
    *`i                         d dl Z d dlmZmZ d dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ  G d de          ZdS )    N)DictTuple)BaseCompressor)QuantizationSchemeQuantizationStatus"initialize_module_for_quantization)register_offload_parameter)get_execution_device)Tensor)	Parameter)linear)Linearc                        e Zd ZdZd fdZe ej                    dede	de
fd                        Zd	edefd
Z xZS )CompressedLinearz
    Wrapper module for running a compressed forward pass of a quantized Linear module.
    The wrapped layer will decompressed on each forward call.

    returnNc                 n     t                      j        |i | t          j        dt                     d S )NzXCompressedLinear should not be initialized directly. Use the from_linear method instead.)super__init__warningswarnUserWarning)selfargskwargs	__class__s      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/compressed_tensors/linear/compressed_linear.pyr   zCompressedLinear.__init__(   sE    $)&)))2	
 	
 	
 	
 	
    modulequantization_schemequantization_formatc                 D   t           |_        t          j        |          |_        t          |          }t          ||d           |j                            |j        j	        |j
                  }t          |d           |                                D ]?\  }\  }}t          t          j        |||          d          }	t!          |||	           @t"          j        |_        t)          |d          r*t           j                            |t                     |_        |S )a  
        :param module: dense linear module to replace
        :param quantization_scheme: quantization config for the module to wrap
        :param quantization_format: compression format module is stored as
        :return: CompressedLinear module wrapping the input module
        F)force_zero_pointweight)devicedtyperequires_grad_old_forward)r   r   r   load_from_registry
compressorr
   r   compression_param_infor#   shapeweightsdelattritemsr   torchemptyr	   r   
COMPRESSEDquantization_statushasattrforward__get__r(   )
clsr   r   r    init_devicecompression_paramsnamer,   r%   params
             r   from_linearzCompressedLinear.from_linear0   s:    ,*=>QRR*622 	+'%	
 	
 	
 	

 06/@/W/WM!4!<0
 0
 	!!! %7$<$<$>$> 	< 	< D.5%E+UCCCSX  E 'vtU;;;; &8%B" 6>** 	"2":"B"B(# #F r   inputc                     | j         t          j        k    rM| j                            |           }t          |d          }t          | d|           t          j        | _         t          || j	        | j
                  S )zM
        Decompresses the weight, then runs the wrapped forward pass
        Fr&   r#   )r3   r   r2   r*   decompress_moduler   r	   FROZENr   r#   bias)r   r=   weight_datar;   s       r   r5   zCompressedLinear.forwardb   sp     #'9'DDD/;;DAAKk???E&tXu==='9'@D$eT[$)444r   )r   N)__name__
__module____qualname____doc__r   classmethodr0   no_gradr   r   strr<   r   r5   __classcell__)r   s   @r   r   r   !   s         
 
 
 
 
 
 U]__.. 0. !	. . . _ [.`5V 5 5 5 5 5 5 5 5 5r   r   )r   typingr   r   r0   #compressed_tensors.compressors.baser   compressed_tensors.quantizationr   r   r   compressed_tensors.utilsr	    compressed_tensors.utils.offloadr
   r   torch.nnr   torch.nn.functionalr   torch.nn.modulesr   r    r   r   <module>rT      s"             > > > > > >         
 @ ? ? ? ? ? A A A A A A             & & & & & & # # # # # #L5 L5 L5 L5 L5v L5 L5 L5 L5 L5r   