
    ^jN                     >    d Z ddlZddlmZ ddlmZ  G d de      Zy)z"CompressedTensors integration file    N)nn   )ConversionOpsc                       e Zd ZdZd Z	 ddeeej                  f   de	e   de	e   dedz  deeej                  f   f
d	Z
edd
       Zy)DecompressExpertsa  
    Dequantize MoE layers when they are in new layout, because they aren't `nn.Module` anymore!

    Takes packed weights and scales from the loaded state dict, creates a dummy Module
    to take advantage of higher-lvl API `decompress_module` and dequantizes all weights.

    Requires MoE conversion to be defined on conversion mapping, so that decompressed weights
    are stacked/merged for all experts.
    c                     || _         y N)hf_quantizer)selfr
   s     w/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/integrations/compressed_tensors.py__init__zDecompressExperts.__init__!   s
    (    N
input_dictsource_patternstarget_patternsfull_layer_namereturnc                    ddl m} ddlm} | j                  j
                  j                  }t        |j                  j                               d   }	|	j                  xs  |t        j                  |	      }
|j                  |
      } G d dt        j                        }d|	j                  j                   z  }i }|j#                         D ]'  \  }}d|vr|}||j%                  dd         }d }t'        t)        ||            D ]  \  }\  }}t+        j,                  |j.                  d   |j.                  d	   |z  g      } ||||      }|	|_        |j3                  |       |at+        j4                  t7        |      g|j8                  j.                  |j8                  j:                  |j8                  j<                  
      }||   j?                  |j8                         ~ ~~|#|||<   * |S )Nr   )BaseCompressor)infer_module_formatc                        e Zd Z fdZ xZS ).DecompressExperts.convert.<locals>.DummyModulec                     t         |           t        j                  |d      | _        t        j                  |d      | _        t        j                  |d      | _        y )NF)requires_grad)superr   r   	Parameterweight_packedweight_scaleweight_shape)r   weightscaleshape	__class__s       r   r   z7DecompressExperts.convert.<locals>.DummyModule.__init__6   sG     "%'\\&%N"$&LLe$L!$&LLe$L!r   )__name__
__module____qualname__r   __classcell__)r#   s   @r   DummyModuler   5   s    M Mr   r(       r   r      )dtypedevice) compressed_tensors.compressorsr   %compressed_tensors.compressors.formatr   r
   
compressorquantization_configlistconfig_groupsvaluesformatr   Linearget_value_from_registryModuleweightsnum_bitsitemsreplace	enumerateziptorchtensorr"   quantization_schemedecompress_moduleemptylenr    r+   r,   copy_)r   r   r   r   r   kwargsr   r   ct_quantization_configr@   r4   r/   r(   pack_factorprocessed_outkeyvalue	quantizedscalesoutputiquantr!   r"   modules                            r   convertzDecompressExperts.convert$   s    	BM!%!2!2!=!=!Q!Q"#9#G#G#N#N#PQRST$++b/B299Na/b#;;FC
	M")) 	M /77@@@ $**, "	,JCc)IO^ LMF
 F%.s9f/E%F !>E5 ekk!nekk!n{6R%ST$UE59-@*,,V4>"[[Y>&--*=*=>$mm11%}}33F
 q	.%( 6! &,c"E"	,H r   c                      y r	    )r   s    r   
reverse_opzDecompressExperts.reverse_opg   s    r   r	   )r   r   )r$   r%   r&   __doc__r   dictstrr>   Tensorr1   rQ   propertyrT   rS   r   r   r   r      s    ) '+Aell*+A cA c	A
 tA 
c5<<	 AF  r   r   )rU   r>   r   core_model_loadingr   r   rS   r   r   <module>r[      s!    %   .S Sr   