
    %Vji(                        U d dl Z d dlmZmZmZmZ d dlmc mc m	Z
 d dlmZ d dlmZmZmZ g dZd Zd Zd Zd Zd	 Zi ej        ej        feej        ej        ej        feej        ej        feej        ej        ej        feej        ej        feej        ej        ej        feej        ej        f ee
j                  ej        ej        f ee
j                  ej        ej        f ee
j                  ej         ej        feej         ej        f ee
j!                  ej        ej        f ee
j"                  ej        ej        f ee
j#                  ej$        ej        feej%        ej        feej&        ej        feZ'e(e)eej*        ef         f         e+d
<   ddZ,d Z-d Z.d Z/dede(eeej*        ef         f         fdZ0dS )    N)AnyCallableOptionalUnion)get_combined_dictMatchAllNodePattern)fuse_conv_bnfuse_conv_bn_relufuse_linear_bnfuse_convtranspose_bnget_fuser_methodget_fuser_method_newc                    |j         |j         k    s
J d            t          j        t          j        t          j        t          j        t          j        t          j        i}| r|j	        |j
        k    s
J d            |j        s
J d            |j        s
J d            |                    t          |          d          }| |||          S t          d||f           t          j                            ||          S )a  Return the fused the conv and bn modules.
    Given the conv and bn 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
        conv: Module instance of type conv2d/conv3d
        bn: Spatial BN instance that needs to be fused with the conv

    Examples::

        >>> m1 = nn.Conv2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_conv_bn(m1, b1)
    :Conv and BN both must be in the same mode (train or eval).z?Output channel of Conv2d must match num_features of BatchNorm2dz7Only support fusing BatchNorm2d with affine set to TruezGOnly support fusing BatchNorm2d with tracking_running_stats set to TrueNCannot fuse train modules: )trainingnnConv1dnniConvBn1dConv2dConvBn2dConv3dConvBn3dnum_featuresout_channelsaffinetrack_running_statsgettypeNotImplementedErrorutilsfuse_conv_bn_eval)is_qatconvbnfused_module_class_mapfused_module_classs        e/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/ao/quantization/fuser_method_mappings.pyr
   r
      s   " =BK'''D (''
 		3<
	3<
	3<  4$"3333M 433 ySSSSSy% 	
 	
U	
 	
% 477ddKK)%%dB///%&PT2J&P&PQQQx))$333    c                 F   |j         |j         cxk    r|j         k    sn J d            d}| rt          j        t          j        t          j        t          j        t          j        t          j        i}|j	        |j
        k    s
J d            |j        s
J d            |j        s
J d            |                    t          |          d          }| ||||          S t          d|||f           t          j        t          j        t          j        t          j        t          j        t          j        i}|                    t          |          d          }|1t          j        j                            ||          } |||          S t          d|||f           )aJ  Return the fused conv and bv modules.

    Given the conv and bn 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
        conv: Module instance of type conv2d/conv3d
        bn: Spatial BN instance that needs to be fused with the conv

    Examples::

        >>> m1 = nn.Conv2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> r1 = nn.ReLU(inplace=False)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_conv_bn_relu(m1, b1, r1)
    r   Nz;Output channel of Conv must match num_features of BatchNormz5Only support fusing BatchNorm with affine set to TruezEOnly support fusing BatchNorm with tracking_running_stats set to Truer   zCannot fuse eval modules: )r   r   r   r   ConvBnReLU1dr   ConvBnReLU2dr   ConvBnReLU3dr   r   r   r   r    r!   r"   
ConvReLU1d
ConvReLU2d
ConvReLU3dr#   fusionr$   )r%   r&   r'   relufused_modulemap_to_fused_module_trainmap_to_fused_module_eval
fused_convs           r*   r   r   @   s   & =BK88884=88888D 988 37L WIs'Is'Is'%
!
 $"3333I 433 yQQQQQy% 	
 	
S	
 	
% 144T$ZZFF#<b$///%&VT2tDT&V&VWWW Is~Is~Is~$
 
 033DJJEE#::4DDJ<
D111%&UD"dCS&U&UVVVr+   c                 &   |j         |j         k    s
J d            | rQ|j        |j        k    s
J d            |j        s
J d            |j        s
J d            t          j        ||          S t          j        j	        
                    ||          S )a  Return the fused linear and bn modules.
    Given the linear and bn 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

    Examples::

        >>> m1 = nn.Linear(20, 10)
        >>> b1 = nn.BatchNorm1d(10)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_linear_bn(m1, b1)
    z<Linear and BN both must be in the same mode (train or eval).z@Output features of Linear must match num_features of BatchNorm1dz7Only support fusing BatchNorm1d with affine set to TruezGOnly support fusing BatchNorm1d with tracking_running_stats set to True)r   r   out_featuresr   r   r   
LinearBn1dr   r#   r3   fuse_linear_bn_eval)r%   linearr'   s      r*   r   r   w   s    " ?bk)))F *))  
?&"5555N 655 ySSSSSy% 	
 	
U	
 	
% ~fb)))x2262>>>r+   c                     |j         |j         k    s
J d            | rt          d          t          j        j                            ||d          S )a  Return the fused ConvTranspose and bn modules.
    Given ConvTranspose and bn modules, fuses them and returns the fused module

    Args:
        convt: Module instance of type ConvTransposeNd
        bn: BatchNormNd instance that needs to be fused with the linear layer.
            batch norm N should match the ConvTranspose N

    Examples::

        >>> m1 = nn.ConvTranspose2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_convtranspose_bn(m1, b1)
    zCConvTranspose and BN both must be in the same mode (train or eval).z8Fusing ConvTranspose+BatchNorm not yet supported in QAT.T)	transpose)r   	Exceptionr   r#   r3   r$   )r%   convtr'   s      r*   r   r      se      >R[(((M )((  LF
 
 	
 x00d0KKKr+   c                       fd}|S )a!  Return a sequential wrapped that for is_qat and two modules.
    Given a sequential class for two modules, return a function that takes
    is_qat, and then two modules as argument, that ignores the is_qat flag
    and always returns the sequential that combines the two input modules
    c                      ||          S N )r%   m1m2
sequentials      r*   fuser_methodz*_sequential_wrapper2.<locals>.fuser_method   s    z"b!!!r+   rE   )rH   rI   s   ` r*   _sequential_wrapper2rJ      s$    " " " " " r+    _DEFAULT_OP_LIST_TO_FUSER_METHODc                     |i }t          t          |          }|                    | d          }|J d|  d            |S )zGet fuser method for the given list of module types.

    Get fuser method for the given list of module types,
    return None if fuser method does not exist
    Ndid not find fuser method for:  )r   rK   r    )op_listadditional_fuser_method_mappingall_mappingsrI   s       r*   r   r      sb     '.*,'$(*I L  ##GT22L##%Qw%Q%Q%Q###r+   c                       fd}|S )Nc                      | ||          S rD   rE   )r%   xyfs      r*   reversedz_reverse2.<locals>.reversed   s    qAr+   rE   rV   rW   s   ` r*   	_reverse2rY      s#         Or+   c                       fd}|S )Nc                 *    |\  }} | |||          S rD   rE   )r%   rT   wrU   zrV   s        r*   rW   z_reverse3.<locals>.reversed   s"    1qAq!!!r+   rE   rX   s   ` r*   	_reverse3r^      s#    " " " " " Or+   c                     t          | t          t          f          r(d | D             }t          t          j        |           }n	| t
          g}|S )aQ  Return a list of valid patterns generated from the op_pattern.

    Returns a list of valid patterns generated from the op_pattern,
    since MatchAllNode can match all types of nodes,
    e.g. pattern (torch.nn.Conv2d, torch.add) should also be able to match keys like
    (MatchAllNode, torch.add) and (torch.nn.Conv2d, MatchAllNode)

    Example Input:
    (torch.add, (torch.nn.ReLU, torch.nn.Conv2d))

    Example Output:
    [(torch.add, (torch.nn.ReLU, torch.nn.Conv2d)),
     (torch.add, (torch.nn.ReLU, MatchAllNode)),
     (torch.add, (MatchAllNode, torch.nn.Conv2d)),
     (torch.add, (MatchAllNode, MatchAllNode)),
     (MatchAllNode, (torch.nn.ReLU, torch.nn.Conv2d)),
     (MatchAllNode, (torch.nn.ReLU, MatchAllNode)),
     (MatchAllNode, (MatchAllNode, torch.nn.Conv2d)),
     (MatchAllNode, (MatchAllNode, MatchAllNode)),
    ]
    c                 ,    g | ]}t          |          S rE   )_get_valid_patterns).0sub_patterns     r*   
<listcomp>z'_get_valid_patterns.<locals>.<listcomp>  s!    TTT+(55TTTr+   )
isinstancetuplelist	itertoolsproductr   )
op_pattern	sub_combsresults      r*   ra   ra      sS    . *udm,, ,TTTTT	i'344l+Mr+   rj   fuser_method_mappingc                     t          |           }d}|D ]} |                    | d          }| n|J d|  d            |S )zGet fuser method.

    This will be made default after we deprecate the get_fuser_method
    Would like to implement this first and have a separate PR for deprecation
    NrM   rN   )ra   r    )rj   rm   op_patternsrI   s       r*   r   r     sp     &j11KL!  
+//
DAA#E $##%Tz%T%T%T###r+   rD   )1rh   typingr   r   r   r   torch.ao.nn.intrinsicaor   	intrinsicr   torch.nntorch.ao.quantization.utilsr   r   r	   __all__r
   r   r   r   rJ   r   BatchNorm1dReLUr   BatchNorm2dr   BatchNorm3dr0   r1   r2   Linear
LinearReLUBNReLU2dBNReLU3dConvTranspose1dConvTranspose2dConvTranspose3drK   dictrf   
Sequential__annotations__r   rY   r^   ra   r   rE   r+   r*   <module>r      sk        1 1 1 1 1 1 1 1 1 1 1 1 # # # # # # # # # # # #       P P P P P P P P P P  )4 )4 )4X4W 4W 4Wn? ? ?DL L L8
 
 
QYQY(*;Q YQ Y(*;	Q
 YQ Y(*;Q Y..s~>>Q Y..s~>>Q Y..s~>>Q YQ Y..s~>>Q ^RW33CLAAQ ^RW33CLAAQ (*?Q (*?Q  (*?!Q  $ueBM84K.L'L"M   (          >wbmX.E(FFG     r+   