
    iG                         S SK JrJr  S SKJrJr  S SKrS SKJr  S SK	J
r
JrJr  S SKJr  S SKJr  S SKJr  S S	KJrJr  S S
KJrJrJrJrJr  S SKJr   " S S\\5      r " S S\5      r  " S S\5      r!g)    )ABCabstractmethod)OptionalTupleN)get_tensor_constant_from_node)constant_updatemodule_insertionnode_removal)WeightCompressionParameters)do_integer_quantization)Tensor)PTTargetPoint
TargetType)BaseWeightsDecompressor!INT4AsymmetricWeightsDecompressor INT4SymmetricWeightsDecompressor!INT8AsymmetricWeightsDecompressor INT8SymmetricWeightsDecompressor)ObserverBasec                     ^  \ rS rSrSrS\S\R                  SS4U 4S jjrS\R                  S\
\R                  \R                  \\R                     4   4S	 jrS
\R                  S\R                  4S jrS\R                  R                  S\R                  R                   SS4S jr\S\R                  S\\R                     S\R                  S\R                  S\4
S j5       rSrU =r$ )WeightObserverBase+   z
Base implementation of an NNCF observer that defines the rules for compressing layer weights into the OpenVINO representation.
wc_paramdtypereturnNc                 .   > [         TU ]  USS9  Xl        g)zl
:param wc_param: Weight compression parameters container.
:param dtype: target dtype for the quantization.
F)r   
is_dynamicN)super__init__	_wc_param)selfr   r   kwargs	__class__s       o/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/openvino/quantizer/observers.pyr   WeightObserverBase.__init__0   s     	u7!    weightc                     U R                   nUR                  nUR                  n[        [	        U5      X4S9u  pVnUb  UR
                  OSnUR
                  UR
                  U4$ )a  
Calculates quantization parameters: quantized weight, quantization scale and quantization zero point.

:param weight: FP weight to be used for calculating qparams.
:return: A tuple containing the quantized weight, quantization scale and quantization zero point.
)reduction_axesN)r    compression_configr)   r   
NNCFTensordata)r!   r'   r   	wc_configr)   q_weightscalezps           r$   calculate_qparams$WeightObserverBase.calculate_qparams=   sb     >>//	!005v	
 RWWD}}ejj",,r&   xc                     U$ N )r!   r3   s     r$   forwardWeightObserverBase.forwardP   s    r&   modelobserver_nodec           	         UR                   S   n[        X15      nU R                  U5      u  pVnU R                  XgXT5      nUR	                  U5      n	[        XU	SS9  UR                  S   R                  n
SR                  U
R                  SS5      R                  S5      SS 5      nUR                   SU 3n[        UU[        [        R                  U
S9/U5        [!        XS5        g)	a  
Replaces the given observer node from the given model with a quantized
weight and a OpenVINO specific decompression module.

:param model: A `torch.fx.GraphModule` representing the statically traced model
            with observer nodes attached and calibrated.
:param observer_node: The `torch.fx.Node` corresponding to the observer module for
                    the weight that is being transformed into a compressed representation.
r   )input_port_id_.N_weights_decompressor_)target_node_name)argsr   r1   _create_decompressorpack_weightr   all_input_nodesnamejoinreplacesplitquantization_moder	   r   r   OPERATOR_POST_HOOKr
   )r!   r9   r:   weight_nodeoriginal_weightr.   r/   
zero_pointdecompressorpacked_q_weightcompressed_weight_namedecompressor_suffixdecompressor_names                r$   convertWeightObserverBase.convertS   s    $((+7K&*&<&<_&M#00x
 '228< 	oQO!.!>!>q!A!F!F!hh"**34::3?D
  ,==>>TUhTij11%; 
	
 	U1-r&   r/   rN   r.   rM   c                     g)a  
Returns a respective NNCF decompressor for different types of quantization.

:param scale: Calculated scale quantization parameter.
:param zero_point: Calculated zero_point quantization parameter.
:param q_weight: Calculated quantized weight.
:param original_weight: FP weight.
:return: NNCF observer according to the qmode which creates the decompression subgraph supported by OpenVINO.
Nr6   r!   r/   rN   r.   rM   s        r$   rC   'WeightObserverBase._create_decompressor~   s    r&   )r    )__name__
__module____qualname____firstlineno____doc__r   torchr   r   r   r   r   r1   r7   fxGraphModuleNoderT   r   r   rC   __static_attributes____classcell__)r#   s   @r$   r   r   +   s	   "-" {{"
 
"-- 
u||U\\8ELL+AA	B-& %,, ).XX))).:?((--).	).V || U\\* ,,	
  
! r&   r   c            
           \ rS rSrSrS\R                  S\\R                     S\R                  S\R                  S\4
S jr	S	r
g
)INT4WeightObserver   z,
OpenVINO INT4 Weight Compression observer.
r/   rN   r.   rM   r   c                     Uc+  [        XR                  UR                  UR                  5      $ [        UUUR                  UR                  UR                  5      $ r5   )r   shaper   r   rW   s        r$   rC   'INT4WeightObserver._create_decompressor   s\     3~~'<'<o>S>S  1NN!!!!
 	
r&   r6   NrY   rZ   r[   r\   r]   r^   r   r   r   rC   rb   r6   r&   r$   re   re      sQ    
||
 U\\*
 ,,	

 
 
!
r&   re   c            
           \ rS rSrSrS\R                  S\\R                     S\R                  S\R                  S\4
S jr	S	r
g
)INT8WeightObserver   z8
OpenVINO INT8 Weight Compression per channel observer.
r/   rN   r.   rM   r   c                 ^    Uc  [        XR                  5      $ [        XUR                  5      $ r5   )r   r   r   rW   s        r$   rC   'INT8WeightObserver._create_decompressor   s3     3E;P;PQQ044
 	
r&   r6   Nrj   r6   r&   r$   rl   rl      sQ    
||
 U\\*
 ,,	

 
 
!
r&   rl   )"abcr   r   typingr   r   r^   %nncf.experimental.torch.fx.node_utilsr   *nncf.experimental.torch.fx.transformationsr   r	   r
   6nncf.quantization.algorithms.weight_compression.configr   ?nncf.quantization.algorithms.weight_compression.weight_loweringr   nncf.tensor.tensorr   r+   )nncf.torch.graph.transformations.commandsr   r   nncf.torch.quantization.layersr   r   r   r   r   torchao.quantization.pt2er   r   re   rl   r6   r&   r$   <module>rz      sm    $ "  
 4  3cs cL
+ 
2
+ 
r&   