
    8hJ                        S SK r S SKrS SKrS SKJs  Js  Jr  S SKJs  Js  J	s  J
r  S SKJr  S SKJs  Jr  S SKJr  S SKJr  SSKJrJrJrJrJrJrJrJrJrJrJr  SSK J!r!J"r"J#r#J$r$  \#" \RJ                  \RJ                  \RL                  \RN                  S9r(\#" \RJ                  \RJ                  S9r)\#" \RJ                  \RN                  \RL                  \RN                  S	S
9r*\#" \RN                  \RN                  \RL                  S9r+\#" \RJ                  \RJ                  \RN                  \RN                  S9r,S r-\$R\                  r/\(/r0\" \05      r1S r2S r3S r4S r5S r6S r7\ Rp                  " S	S/\Rr                  \Rr                  /5      r:\: GHH  u  r;r<\;(       a  \1R{                  \"" 5       R}                  \<\R~                  \R                  4\45      R                  \/5      R                  \05      R                  \55      R                  \65      R                  \75      R                  \R                  5      5        M  \1R{                  \"" 5       R}                  \<\R                  \45      R                  \/5      R                  \05      R                  \25      R                  \35      R                  \45      R                  \R                  5      5        GMK     S rHS rIS rJS rKS rLS rM\ Rp                  " S	S/\Rr                  \Rr                  /5      rN\N GHH  u  r;r<\;(       a  \1R{                  \"" 5       R}                  \<\\R~                  \R                  445      R                  \/5      R                  \05      R                  \K5      R                  \L5      R                  \M5      R                  \R                  5      5        M  \1R{                  \"" 5       R}                  \<\\R                  45      R                  \/5      R                  \05      R                  \H5      R                  \I5      R                  \J5      R                  \R                  5      5        GMK     \1R{                  \"" \R                  5      R                  \/5      R                  \05      R                  \R                  5      R                  \R                  5      5        S rQS rRS rSS rTS rUS rV\ Rp                  " S	S/\Rr                  \Rr                  /5      rW\W GH`  u  r;r<\;(       a  \1R{                  \"" 5       R}                  \R                  \<\R~                  \R                  4\445      R                  \/5      R                  \05      R                  \T5      R                  \U5      R                  \V5      R                  \R                  5      5        M  \1R{                  \"" 5       R}                  \R                  \<\R                  \445      R                  \/5      R                  \05      R                  \Q5      R                  \R5      R                  \S5      R                  \R                  5      5        GMc     S  rZS! r[S" r\S# r]S$ r^S% r_\ Rp                  " S	S/\Rr                  \Rr                  /5      rW\W GH`  u  r;r<\;(       a  \1R{                  \"" 5       R}                  \R                  \<\\R~                  \R                  4445      R                  \/5      R                  \05      R                  \]5      R                  \^5      R                  \_5      R                  \R                  5      5        M  \1R{                  \"" 5       R}                  \R                  \<\\R                  445      R                  \/5      R                  \05      R                  \Z5      R                  \[5      R                  \\5      R                  \R                  5      5        GMc     \1R{                  \"" \R                  5      R                  \/5      R                  \05      R                  \R                  5      R                  \R                  5      5        \(\*/r`\" \`5      raS& rb\b" \a\R                  \R                  \R                  \R                  \`\" \R                  5      \R                  \/\R                  5
        \aR{                  \"" \R                  \R                  \R                  45      R                  \`5      R                  \-5      R                  \R                  5      5        \b" \a\R                  \R                  \R                  \R                  \`\" \R                  5      \R                  \/\R                  5
        \)/rl\)/rm\)/rn\)/ro\*/rp\+/rq\,/rrS'\!4S( jrsS)/rtg)*    N)_sequential_wrapper2)MatchAllNode   )_get_binary_op_configs_get_bn_configs_get_cat_config_get_conv_configs_get_default_op_configs_get_embedding_op_configs_get_fixed_qparams_op_configs_get_linear_configs_get_ln_configs_get_rnn_op_configs_get_share_qparams_op_configs)BackendConfigBackendPatternConfigDTypeConfigObservationType)input_dtypeoutput_dtypeweight_dtype
bias_dtype)r   r   T)r   r   r   r   
is_dynamic)r   r   r   c                    UR                   UR                   :X  a  UR                   UR                   :X  d   S5       eU (       a  [        SXU4 35      e[        R                  [        R
                  0nUR                  [        U5      S5      nUb3  [        R                  R                  R                  X5      nU" Xc5      nU$ [        SXU4 35      e)aa  Given the linear, bn and leaky_relu modules, fuses them and returns the fused module
Args:
    is_qat: a flag for whether we are using quantization aware training fusion
            or post training quantization fusion
    linear: Module instance of type Linear
    bn: BatchNorm1d instance that needs to be fused with the linear layer
    leaky_relu: LeakyReLU instance that needs to be fused with the linear layer
Examples::
    >>> # xdoctest: +SKIP(failing)
    >>> m1 = nn.Linear(20, 10)
    >>> b1 = nn.BatchNorm1d(10)
    >>> lr = nn.LeakyReLU(0.01)
    >>> m2 = _fuse_linear_bn_leaky_relu(m1, b1, lr)
zFLinear, BN and LeakyReLU all must be in the same mode (train or eval).Cannot fuse train modules: NzCannot fuse eval modules: )trainingNotImplementedErrornnLinearnniLinearLeakyReLUgettypeutilsfusionfuse_linear_bn_eval)is_qatlinearbn
leaky_relumap_to_fused_module_evalfused_modulefused_linearfms           e/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/ao/quantization/backend_config/onednn.py_fuse_linear_bn_leaky_relur0   L   s     ??bkk)bkkZ=P=P.P PP !)6z*B)CD
 	

 IIs**$
  033DL$G#88??>>vJLl7BI%,f*-E,FG     c                 .    [         R                  " X!5      $ Nr    	ConvAdd2d)r'   addconv_s       r/   _fuse_conv_add_leftr9          ==##r1   c                     U u  pnU$ r3    )patternr8   r7   s      r/   _conv_add_root_node_getter_leftr>      s    JAQKr1   c                     U u  pnU/$ zyget inputs pattern for extra inputs, inputs for root node
are assumed to be copied over from root node to the fused node
r<   )r=   r8   _convextra_inputs       r/   "_conv_add_extra_inputs_getter_leftrC      s     $Ak=r1   c                     Uu  pEU (       a  [        SXTU4 35      e[        R                  R                  R	                  XT5      n[
        R                  " Xa5      $ Nr   r   r   r$   r%   fuse_conv_bn_evalr    r5   )r'   r6   bn_convr8   r)   r7   
fused_convs          r/   _fuse_conv_bn_add_leftrJ      K    HB!$?3?P"QRRXX__66t@
}}Z--r1   c                     U u  pnUu  p4U$ r3   r<   )add_patternr8   rH   _bnr7   s        r/   "_conv_bn_add_root_node_getter_leftrO      s    MAICKr1   c                     U u  pnU/$ r@   r<   )rM   r8   _bn_convrB   s       r/   %_conv_bn_add_extra_inputs_getter_leftrR      s      +A=r1   Fc                 .    [         R                  " X15      $ r3   r4   )r'   r6   r8   r7   s       r/   _fuse_conv_add_rightrT      r:   r1   c                     U u  pnU$ r3   r<   )r=   _addr8   r7   s       r/    _conv_add_root_node_getter_rightrW      s    MDTKr1   c                     U u  pnU/$ r@   r<   )r=   r8   rB   rA   s       r/   #_conv_add_extra_inputs_getter_rightrY      s     $AE=r1   c                     Uu  pEU (       a  [        SXTU4 35      e[        R                  R                  R	                  XT5      n[
        R                  " Xa5      $ rE   rF   )r'   r6   r8   rH   r)   r7   rI   s          r/   _fuse_conv_bn_add_rightr[      rK   r1   c                     U u  pnUu  pEU$ r3   r<   )r=   rV   r8   rH   rN   r7   s         r/   #_conv_bn_add_root_node_getter_rightr]      s    DWICKr1   c                     U u  pnU/$ r@   r<   )r=   r8   rB   rQ   s       r/   &_conv_bn_add_extra_inputs_getter_rightr_      s      'AH=r1   c                 :    Uu  p4n[         R                  " XCU5      $ r3   r    ConvAddReLU2d)r'   relurM   r6   r7   r8   s         r/   _fuse_conv_add_relu_leftrd   8  s    LCqT--r1   c                     U u  pUu  p4nU$ r3   r<   )r=   _relurM   r8   r7   s        r/   $_conv_add_relu_root_node_getter_leftrg   =  s     EJAQKr1   c                     U u  pUu  p4nU/$ r@   r<   )r=   rf   rM   r8   rA   rB   s         r/   '_conv_add_relu_extra_inputs_getter_leftri   C  s     !E'Ak=r1   c                     Uu  p4nUu  pgU (       a  [        SXvX14 35      e[        R                  R                  R	                  Xv5      n[
        R                  " XU5      $ rE   r   r   r$   r%   rG   r    rb   )	r'   rc   rM   r6   rH   r8   r)   r7   rI   s	            r/   _fuse_conv_bn_add_relu_leftrl   U  sY    !OC!HB!$?3@U?V"WXXXX__66t@
  $77r1   c                      U u  pUu  p4nUu  pVU$ r3   r<   r=   rf   rM   r8   rH   rN   r7   s          r/   '_conv_bn_add_relu_root_node_getter_leftro   _  s     EMAICKr1   c                     U u  pUu  p4nU/$ r@   r<   )r=   rf   rM   r8   rQ   rB   s         r/   *_conv_bn_add_relu_extra_inputs_getter_leftrq   f  s     !E*A=r1   c                 :    Uu  p4n[         R                  " XSU5      $ r3   ra   )r'   rc   rM   r6   r8   r7   s         r/   _fuse_conv_add_relu_rightrs     s    LCDT--r1   c                     U u  pUu  p4nU$ r3   r<   )r=   rf   rM   r8   _extra_inputr7   s         r/   %_conv_add_relu_root_node_getter_rightrv     s     E'ATKr1   c                     U u  pUu  p4nU/$ r@   r<   )r=   rf   rM   r8   rB   rA   s         r/   (_conv_add_relu_extra_inputs_getter_rightrx     s     !E'AE=r1   c                     Uu  p4nUu  pgU (       a  [        SXvX14 35      e[        R                  R                  R	                  Xv5      n[
        R                  " XU5      $ rE   rk   )	r'   rc   rM   r6   r8   rH   r)   r7   rI   s	            r/   _fuse_conv_bn_add_relu_rightrz     sY    !OCGHB!$?3@U?V"WXXXX__66t@
  $77r1   c                      U u  pUu    p4Uu  pVU$ r3   r<   rn   s          r/   (_conv_bn_add_relu_root_node_getter_rightr|     s     EMAqICKr1   c                     U u  pUu  p4nU/$ r@   r<   )r=   rf   rM   r8   rB   rQ   s         r/   +_conv_bn_add_relu_extra_inputs_getter_rightr~     s     !E*AH=r1   c
                    U R                  [        X45      R                  U5      R                  U5      R	                  U5      5        U R                  [        X45      R                  U5      R                  U5      R	                  U5      5        U R                  [        U5      R                  U5      R                  U5      R                  U5      R                  U	5      5        U R                  [        X#45      R                  U5      R                  U5      5        U R                  [        X$45      R                  U5      R                  U5      5        g r3   )appendr   set_dtype_configsset_fuser_methodset_fused_moduleset_observation_typeset_root_moduleset_reference_quantized_module)
configsroot_moduleroot_oppost_modulepost_opdtype_configsfuser_methodr,   observation_typeref_quant_modules
             r/   _add_eltwise_fusion_configsr     s    NNk78		=	)		,	'		,	'	 NNk34		=	)		,	'		,	'	 NN\*		.	/		=	)		%	'	'(8	9 NNg34		.	/		=	)
 NNg/0		.	/		=	)r1   returnc                  \   [        S5      R                  [        5      R                  [        5      R                  [	        [
        5      5      R                  [        [        5      5      R                  [        [        5      5      R                  [        [        5      5      R                  [        [        5      5      R                  [        [        5      5      R                  [        [         5      5      R                  [#        [$        5      5      R                  ['        [(        5      5      $ )zA
Return the `BackendConfig` for PyTorch's native ONEDNN backend.
onednn)r   set_backend_pattern_configsconv_configslinear_configsr   binary_op_dtype_configsset_backend_pattern_configr   default_op_dtype_configsr
   r   fixed_qparams_op_dtype_configsr   share_qparams_op_dtype_configsr   r   layer_norm_op_dtype_configsr   rnn_op_dtype_configsr   embedding_op_dtype_configsr<   r1   r/   get_onednn_backend_configr   d  s    
 	h	$	$\	2	$	$^	4	$	$%;<S%T	U	#	#O4L$M	N	$	$%<=U%V	W	$	$)*HI

 
%	$)*HI

 
%	$_5M%N	O	$	$_5P%Q	R	$	$%89M%N	O	$	$%&@A

!r1   r   )u	itertoolsoperatortorchtorch.ao.nn.intrinsicaor   	intrinsicr    torch.ao.nn.quantized.reference	quantized	referencennqrtorch.nntorch.nn.functional
functionalF+torch.ao.quantization.fuser_method_mappingsr   torch.ao.quantization.utilsr   _common_operator_config_utilsr   r   r   r	   r
   r   r   r   r   r   r   backend_configr   r   r   r   quint8qint8float$onednn_weighted_op_int8_dtype_configonednn_op_quint8_dtype_config onednn_dynamic_int8_dtype_config%onednn_weight_only_qint8_dtype_config,onednn_input_output_only_quint8_dtype_configr0   &OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUTr   conv_dtype_configsr   r9   r>   rC   rJ   rO   rR   productr6   conv_add_left_optioinswith_bnadd_opr   _set_pattern_complex_formatBatchNorm2dConv2dr   r   r   _set_root_node_getter_set_extra_inputs_getterr   r5   rT   rW   rY   r[   r]   r_   conv_add_optioinsr   r   rd   rg   ri   rl   ro   rq   conv_add_relu_left_optioinsReLUrb   rs   rv   rx   rz   r|   r~   linear_dtype_configsr   r   r   r(   	LeakyReLUr*   r!   BatchNorm1dTanhtanh
LinearTanhr   r   r   r   r   r   r   r   __all__r<   r1   r/   <module>r      s      # # . .    L 4     (3{{	( $ !,! 
 $/{{$   )4) % 0;{{	0 ,#R #II :;  !34$
. #**	5M
YY 
 .OGV "((".."))4lC "!"231245""#EF%%&KLcmm,	
 	 "((&"))\)JK!!"231212""#BC%%&HIcmm,		
 .>$
. %%	5M
YY 
 )OGV "((		'BC "!"231256""#FG%%&LMcmm,	
 	 "((&,		)JK!!"231223""#CD%%&IJcmm,		
 )4   '*+)*_RYY##DKK0".
$8 (//	5M
YY 
 3OGV "((6BNNBII#>MN "!"23129:""#JK%%&PQc//0	
 	 "(("''FBII|3T)UV!!"231267""#GH%%&MNc//0		
 3B.
$8 (//	5M
YY 
 3OGV "((6<".."))1LMN "!"2312:;""#KL%%&QRc//0	
 	 "(("''FL"))3T)UV!!"231278""#HI%%&NOc//0		
 34   **+*+)*_RYY##DKK0 )$  %%9:.d IIHHLLLL,,-KK   "))R^^R\\BC+,01c))*	 IIHHGG	JJ(NNKK" 99 9: "?!@ "?!@ 89 CD KL = 6  r1   