
    VhB                        d dl Z d dlZd dlmc mc mZ d dlmc mc mc m	Z
 d dlmZ d dlmc mZ d dlmZmZmZ ddlmZ ddlmZmZmZmZmZ ddlmZmZ dgZ eej>                  ej>                  ej@                  ejB                        Z" eej>                  ej>                  	      Z# eej>                  ejB                  ej@                  ejB                  d
      Z$ eej@                  d      Z% eej@                  ddd      Z& ee%ejB                  e&ejB                  d
      Z' eejP                  ejB                  ejP                  ejB                  d
      Z) eejB                  ejB                  ej>                        Z*de+e   fdZ,de+e   fdZ-de+e   fdZ.de+e   fdZ/de+e   fdZ0de+e   fdZ1de+e   fdZ2defdZ3y)    N)_sequential_wrapper2fuse_conv_bnfuse_conv_bn_relu   )_Conv2dMetadata)BackendConfigBackendPatternConfigDTypeConfigDTypeWithConstraintsObservationType)/qnnpack_default_op_qint8_symmetric_dtype_config0qnnpack_weighted_op_qint8_symmetric_dtype_configget_executorch_backend_config)input_dtypeoutput_dtypeweight_dtype
bias_dtype)r   r   T)r   r   r   r   
is_dynamicg      0?)dtypescale_min_lower_boundi   )r   quant_min_lower_boundquant_max_upper_boundr   )r   r   r   returnc                     t         j                  } t        t        t        t
        t        g}g }|j                  t        t        j                  j                        j                  |       j                  |      j                  t        j                  j                        j                  t         j                        j#                  t$        j                               |j                  t        t$        j                        j                  |       j                  |      j                  t        j                  j                        j                  t         j                               |j                  t        t        j                  j&                  j(                        j                  |       j                  |      j+                  ddd             |S )z?
    Return all configs related to linear modules and ops.
    r      weightbias)r   &OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUTr   (executorch_weighted_op_int8_dtype_config.executorch_default_dynamic_quint8_dtype_config-executorch_default_dynamic_qint8_dtype_config/executorch_default_dynamic_float16_dtype_configappendr	   torchnnLinearset_observation_typeset_dtype_configsset_root_moduleset_reference_quantized_modulennqrset_qat_modulennqat
functionallinear_set_input_type_to_index)observation_typedtype_configslinear_configss      _/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/quantization/backend_config/executorch.py_get_linear_configsr7   d   s<    'MM80657M 24NUXX__-		.	/		=	)		)	'	'	4		% U\\*		.	/		=	)		)	'	'	4 UXX00778		.	/		=	)	!	!Q":	;	     c                     t         j                  } t        t        g}g }t        fD ]  }|j                  t        |j                        j                  |       j                  |      j                  |j                        j                  |j                        j                  |j                               |j                  t        |j                        j                  |       j                  |      j                  |j                        j                  |j                               |j                  t        |j                        j                  |       j                  |      j!                  ddd             |j                  t        |j                  t"        j$                  f      j                  |      j'                  t)        |j*                              j-                  |j*                               |j                  t        |j                  t.        j0                  f      j                  |      j'                  t)        |j*                              j-                  |j*                               |j                  t        |j*                        j                  |       j                  |      j                  |j                        j                  |j                        j                  |j2                               |j                  t        |j2                        j                  |       j                  |      j                  |j                        j                  |j                               |j                  t        |j                  t"        j$                  f      j                  |       j                  |             |j                  t        |j                  t.        j0                  f      j                  |       j                  |             |j                  t        |j*                        j                  |      j                  |j2                               |j                  t        |j2                        j                  |      j                  |j                        j                  |j                               |j                  t        |j                  |j4                  f      j                  |      j'                  t6              j-                  |j8                               |j                  t        |j                  |j4                  t"        j$                  f      j                  |      j'                  t:              j-                  |j<                               |j                  t        |j                  |j4                  t.        j0                  f      j                  |      j                  |j                        j'                  t:              j-                  |j<                               |j                  t        |j8                        j                  |      j                  |j>                               |j                  t        |j<                        j                  |      j                  |j@                               |j                  t        |j>                        j                  |       j                  |      j                  |j                        j                  |j                               |j                  t        |j@                        j                  |       j                  |      j                  |j                        j                  |j                                |S )z=
    Return all configs related to conv modules and ops.
    r   r   r   )!r   r    r   r!   r   r%   r	   rootr)   r*   r+   r,   	referencer.   qatfuncr2   r'   ReLUset_fuser_methodr   fused_conv_reluset_fused_moduleFrelurelu_qatbnr   fused_conv_bnr   fused_conv_bn_relubn_qatbn_relu_qat)r3   r4   conv_configsconvss       r6   _get_conv_configsrL      s    'MM80M L!" N
 	 ,!!"23}-_UZZ(++EOO<^EII&	
 	 +!!"23}-_UZZ(++EOO<	
 	 ,!!"23}-%%A&>?		
 	 %**bgg!67}-253H3HIJe334		
 	 %**aff!56}-253H3HIJe334		
 	 !6!67!!"23}-_UZZ(++EOO<^ENN+	
 	 0!!"23}-_UZZ(++EOO<	
 	 %**bgg!67!!"23}-	
 	 %**aff!56!!"23}-	
 	 !6!67}-^ENN+	
 	 0}-_UZZ(++EOO<		
 	 %**ehh!78}-l+e112		
 	 %**ehh!@A}-/0e667		
 	 %**ehh!?@}-_UZZ(/0e667	
 	 !4!45}-^ELL)	
 	 !9!9:}-^E--.	
 	 .!!"23}-_UZZ(++EOO<	
 	 !2!23!!"23}-_UZZ(++EOO<	
QN
^ r8   c                  &   t         t        gt        j                  t        j                  t        j                  dg } t
        j                  t        j                  t
        j                  t        j                  t
        j                  t        j                  fD ]q  }|t        j                  j                  f|t        j                  j                  j                  f|t        j                  f|g}| j                  fd|D               s | S )z3
    Return all configs related to binary ops.
    )r   r   r   c              3   p   K   | ]-  }t        |      j                        j                         / y wN)r	   r*   (_set_num_tensor_args_to_observation_type).0bop_patternr4   +num_tensor_args_to_observation_type_mappings     r6   	<genexpr>z*_get_binary_ops_configs.<locals>.<genexpr>G  s>      !
  !-}-55;!
s   36)r   r!   r   r     OUTPUT_SHARE_OBSERVER_WITH_INPUToperatoraddr&   submulr'   r>   r0   rC   extend)binary_op_configsopbop_patternsr4   rS   s      @@r6   _get_binary_ops_configsr^   (  s    
 	80M AA;;AA3/ 57						 
 $$))*	
 	   !
  ,!
 	

, r8   c                  \   t         j                  } t        t        g}g t        j
                  j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                   t        j
                  j"                  t        j
                  j$                  t        j
                  j&                  t        j
                  j(                  t        j
                  j*                  t        j
                  j,                  t        j
                  j.                  t        j0                  t        j2                  t        j4                  t        j6                  t        j8                  t        j:                  ddddddddd	}|D cg c]+  }t=        |      j?                  |       jA                  |      - }}|S c c}w )
ar  
    Return the operator configs for the operators that works for both float and quantized
    input if input is quantized, the output Tensor shares the same quantization parameter
    with input.

    Example operator: avgpool2d, reshape, transpose, maxpool2d
    Example observed operator:
    observer_0 - avgpool2d - observer_0 (same observer instance as input)
    clampmeanpermutereshaperC   relu_squeezesqueeze_
leaky_relu)!r   rU   r   )executorch_default_op_quint8_dtype_configr&   r'   FlattenrB   adaptive_avg_pool2deluhardtanh
max_pool2dpadrC   relu6rg   leaky_relu_AdaptiveAvgPool2dConstantPad2dELU	MaxPool2dReLU6Hardtanh	LeakyReLUr`   flattenra   rb   permute_copyre   r	   r)   r*   )r3   r4   share_qparams_opsr\   share_qparams_op_configss        r6   _get_share_qparams_ops_configsr|   R  s@    'GG71M!!	! 	
! 	


	!
 	
! 	
! 	
! 	
! 	
! 	
! 	""! 	! 	! 	! 	!  	!!" 	#!$ 	%!& 	'!( 	

)!* 	+!, 	-!. 	/!0 	1!2 	3!4 	5!6 	7!8 	9!: 	;!< 	=!> 	?!@ 	A!L $	<  	R 		.	/		=	)	*< < $#<s   50H)c                      t         j                  } t        t        g}g }|j	                  t        t        j                        j                  |       j                  |             |S )z2
    Return all configs related to batchnorm.
    )
r   r    r   rh   r%   r	   r'   BatchNorm2dr)   r*   )r3   r4   
bn_configss      r6   _get_bn_configsr     s]     'MM71M JR^^,		.	/		=	)
 r8   c                     t         t        g} g }|j                  t        t        j
                        j                  t        j                        j                  |              |j                  t        t        j                        j                  t        j                        j                  |              |j                  t        t        j                        j                  t        j                        j                  |              |S rO   )r   rh   r%   r	   r&   catr)   r   rU   r*   concatconcatenate)r4   cat_configss     r6   _get_cat_configsr     s    71M KUYY'		oNN	O		=	)
 U\\*		oNN	O		=	)
 U../		oNN	O		=	)
 r8   c                     t         g} g }t        j                  t        j                  t        j                  ft        j
                  t        j
                  t        j
                  ffD ]W  \  }}}|j                  t        |      j                  t        j                        j                  |       j                  |      j                  |      j                  |             |j                  t        |      j                  t        j                        j                  |       j                  |      j                  |             |j                  t        t        j                  j                   j"                        j                  t        j                        j                  |       j%                  ddi             Z |S )Nr   r   )*executorch_weight_only_quint8_dtype_configr'   	Embeddingr/   r-   EmbeddingBagr%   r	   r)   r   r    r*   r.   r+   r,   r&   r0   	embeddingr2   )r4   embedding_op_configsembedding_opqat_embedding_opref_embedding_ops        r6   _get_embedding_op_configsr     si   2M 	u7	%,,d.?.?@= !
8&(8 	## .!!FF }-^,-_\*++,<=		
 	## !12!!FF }-_\*++,<=	
 	## !4!4!>!>?!!FF }-%%xm4	
5!
D  r8   c                  Z   t        d      j                  t                     j                  t                     j                  t	                     j                  t                     j                  t                     j                  t                     j                  t                     S )za
    Return the `BackendConfig` for backends PyTorch lowers to through the Executorch stack.
    
executorch)	r   set_backend_pattern_configsr7   rL   r^   r|   r   r   r    r8   r6   r   r     sy    
 	l#	$	$%8%:	;	$	$%6%8	9	$	$%<%>	?	$	$%C%E	F	$	$_%6	7	$	$%5%7	8	$	$%>%@	A	r8   )4rV   r&   torch.ao.nn.qataor'   r<   r/   torch.ao.nn.quantized.reference	quantizedr;   r-   torch.nntorch.nn.functionalr0   rB   +torch.ao.quantization.fuser_method_mappingsr   r   r   _common_operator_config_utilsr   backend_configr   r	   r
   r   r   qnnpackr   r   __all__quint8qint8floatr!   rh   r"   'executorch_act_qint8_scale_min_2_neg_129executorch_weight_qint8_neg_127_to_127_scale_min_2_neg_12r#   float16r$   r   listr7   rL   r^   r|   r   r   r   r   r   r8   r6   <module>r      s       . .     ;  $ ,7{{	, ( -8- )
 2={{2 . +?
++ + '
 =Q
++ 	= 9 1<7J{{1 - 3>{{3 / .9. *%T"67 %PY4 45 Yx'&:!; 'T7$-A(B 7$t23 $$34 0' 4(<#= ' ^} r8   