
    ȅiJ                        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                   :w  d  UR                   UR                   :w  a  [        S5      eU (       a  [        SXU4 35      e[        R                  [
        R                  0nUR                  [        U5      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: zCannot fuse eval modules: )trainingAssertionError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/home/james-whalen/.local/lib/python3.13/site-packages/torch/ao/quantization/backend_config/onednn.py_fuse_linear_bn_leaky_relur1   L   s     "++%
8K8K)KT
 	
 !)6z*B)CD
 	

 IIs**$
  033DLA#88??>>vJLl7BI%,f*-E,FG     c                 .    [         R                  " X!5      $ Nr!   	ConvAdd2d)r(   addconv_s       r0   _fuse_conv_add_leftr:          ==##r2   c                     U u  pnU$ r4    )patternr9   r8   s      r0   _conv_add_root_node_getter_leftr?      s    JAQKr2   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>   r9   _convextra_inputs       r0   "_conv_add_extra_inputs_getter_leftrD      s     $Ak=r2   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!   r6   )r(   r7   bn_convr9   r*   r8   
fused_convs          r0   _fuse_conv_bn_add_leftrK      K    HB!$?3?P"QRRXX__66t@
}}Z--r2   c                     U u  pnUu  p4U$ r4   r=   )add_patternr9   rI   _bnr8   s        r0   "_conv_bn_add_root_node_getter_leftrP      s    MAICKr2   c                     U u  pnU/$ rA   r=   )rN   r9   _bn_convrC   s       r0   %_conv_bn_add_extra_inputs_getter_leftrS      s      +A=r2   Fc                 .    [         R                  " X15      $ r4   r5   )r(   r7   r9   r8   s       r0   _fuse_conv_add_rightrU      r;   r2   c                     U u  pnU$ r4   r=   )r>   _addr9   r8   s       r0    _conv_add_root_node_getter_rightrX      s    MDTKr2   c                     U u  pnU/$ rA   r=   )r>   r9   rC   rB   s       r0   #_conv_add_extra_inputs_getter_rightrZ      s     $AE=r2   c                     Uu  pEU (       a  [        SXTU4 35      e[        R                  R                  R	                  XT5      n[
        R                  " Xa5      $ rF   rG   )r(   r7   r9   rI   r*   r8   rJ   s          r0   _fuse_conv_bn_add_rightr\      rL   r2   c                     U u  pnUu  pEU$ r4   r=   )r>   rW   r9   rI   rO   r8   s         r0   #_conv_bn_add_root_node_getter_rightr^      s    DWICKr2   c                     U u  pnU/$ rA   r=   )r>   r9   rC   rR   s       r0   &_conv_bn_add_extra_inputs_getter_rightr`     s      'AH=r2   c                 :    Uu  p4n[         R                  " XCU5      $ r4   r!   ConvAddReLU2d)r(   relurN   r7   r8   r9   s         r0   _fuse_conv_add_relu_leftre   9  s    LCqT--r2   c                     U u  pUu  p4nU$ r4   r=   )r>   _relurN   r9   r8   s        r0   $_conv_add_relu_root_node_getter_leftrh   >  s     EJAQKr2   c                     U u  pUu  p4nU/$ rA   r=   )r>   rg   rN   r9   rB   rC   s         r0   '_conv_add_relu_extra_inputs_getter_leftrj   D  s     !E'Ak=r2   c                     Uu  p4nUu  pgU (       a  [        SXvX14 35      e[        R                  R                  R	                  Xv5      n[
        R                  " XU5      $ rF   r   r   r%   r&   rH   r!   rc   )	r(   rd   rN   r7   rI   r9   r*   r8   rJ   s	            r0   _fuse_conv_bn_add_relu_leftrm   V  sY    !OC!HB!$?3@U?V"WXXXX__66t@
  $77r2   c                      U u  pUu  p4nUu  pVU$ r4   r=   r>   rg   rN   r9   rI   rO   r8   s          r0   '_conv_bn_add_relu_root_node_getter_leftrp   `  s     EMAICKr2   c                     U u  pUu  p4nU/$ rA   r=   )r>   rg   rN   r9   rR   rC   s         r0   *_conv_bn_add_relu_extra_inputs_getter_leftrr   g  s     !E*A=r2   c                 :    Uu  p4n[         R                  " XSU5      $ r4   rb   )r(   rd   rN   r7   r9   r8   s         r0   _fuse_conv_add_relu_rightrt     s    LCDT--r2   c                     U u  pUu  p4nU$ r4   r=   )r>   rg   rN   r9   _extra_inputr8   s         r0   %_conv_add_relu_root_node_getter_rightrw     s     E'ATKr2   c                     U u  pUu  p4nU/$ rA   r=   )r>   rg   rN   r9   rC   rB   s         r0   (_conv_add_relu_extra_inputs_getter_rightry     s     !E'AE=r2   c                     Uu  p4nUu  pgU (       a  [        SXvX14 35      e[        R                  R                  R	                  Xv5      n[
        R                  " XU5      $ rF   rl   )	r(   rd   rN   r7   r9   rI   r*   r8   rJ   s	            r0   _fuse_conv_bn_add_relu_rightr{     sY    !OCGHB!$?3@U?V"WXXXX__66t@
  $77r2   c                      U u  pUu    p4Uu  pVU$ r4   r=   ro   s          r0   (_conv_bn_add_relu_root_node_getter_rightr}     s     EMAqICKr2   c                     U u  pUu  p4nU/$ rA   r=   )r>   rg   rN   r9   rC   rR   s         r0   +_conv_bn_add_relu_extra_inputs_getter_rightr     s     !E*AH=r2   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 r4   )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
             r0   _add_eltwise_fusion_configsr     s    NNk78		=	)		,	'		,	'	 NNk34		=	)		,	'		,	'	 NN\*		.	/		=	)		%	'	'(8	9 NNg34		.	/		=	)
 NNg/0		.	/		=	)r2   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=   r2   r0   get_onednn_backend_configr   e  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

!r2   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_configr1   &OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUTr   conv_dtype_configsr   r:   r?   rD   rK   rP   rS   productr7   conv_add_left_optioinswith_bnadd_opr   _set_pattern_complex_formatBatchNorm2dConv2dr   r   r   _set_root_node_getter_set_extra_inputs_getterr   r6   rU   rX   rZ   r\   r^   r`   conv_add_optioinsr   r   re   rh   rj   rm   rp   rr   conv_add_relu_left_optioinsReLUrc   rt   rw   ry   r{   r}   r   linear_dtype_configsr   r   r    r)   	LeakyReLUr+   r"   BatchNorm1dTanhtanh
LinearTanhr   r   r   r   r   r   r   r   __all__r=   r2   r0   <module>r      s      # # . .    L 4     (3{{	( $ !,! 
 $/{{$   )4) % 0;{{	0 ,$T #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  r2   