
    [Th                         S SK r S SKJr  S SKJr  S SKJrJrJr  S SK	J
r
  / SQrS rS rSS jr\S4S	 jrS
\S4S jrS
\S4S jrS
\S4S jrg)    N)Optional)fuse_conv_bnfuse_conv_bn_reluget_fuser_methodtype_before_parametrizations)fuse_known_modulesfuse_modulesfuse_modules_qatc                 T    UR                  S5      nU nU H  n[        X45      nM     U$ )N.)splitgetattr)modelsubmodule_keytokenscur_modss        Z/var/www/auris/envauris/lib/python3.13/site-packages/torch/ao/quantization/fuse_modules.py_get_moduler      s0      %FG'% N    c                 z    UR                  S5      nUS S nU nU H  n[        XV5      nM     [        XSS   U5        g )Nr   )r   r   setattr)r   r   moduler   
sub_tokensr   r   s          r   _set_moduler   !   sF      %FJG'%  GBZ(r   c                 t   [        S U  5       5      n[        X25      nUc  [        SU 35      eS/[        U 5      -  nU" U/U Q76 nU S   R                  R                  5        H  nUR                  U5        M     U S   R                  R                  5         U S   R                  R                  5        H  nUR                  U5        M     U S   R                  R                  5         XeS'   [        S[        U 5      5       H0  n	[        R                  " 5       n
U S   R                  U
l        XU	'   M2     U$ )az  Return a list of known fuse modules.

Returns a list of modules that fuses the operations specified
 in the input module list.

Fuses only the following sequence of modules:
conv, bn
conv, bn, relu
conv, relu
linear, bn
linear, relu
For these sequences, the first element in the output module list performs
the fused operation. The rest of the elements are set to nn.Identity()
c              3   8   #    U  H  n[        U5      v   M     g 7fNr   ).0ms     r   	<genexpr>%fuse_known_modules.<locals>.<genexpr>:   s     D8a.q118s   NzCannot fuse modules: r   r      )tupler   NotImplementedErrorlen_forward_pre_hooksvaluesregister_forward_pre_hookclear_forward_hooksregister_forward_hookrangennIdentitytraining)mod_listis_qatadditional_fuser_method_mappingtypesfuser_methodnew_modfusedpre_hook_fnhook_fniidentitys              r   r	   r	   +   s!    D8DDE#EKL!$9%"ABB*.#h-)?G+(+E  {55<<>''4 ?QK""((*B<..557##G, 8RL%%'AJ1c(m$;;=$QK00
 %
 Nr   c                     Uc  0 nUR                  S0 5      nU Vs/ s H  n[        X5      PM     nnU" XrU5      n[        U5       H  u  p[        XX   5        M     g s  snf )Nr5   )getr   	enumerater   )
r   modules_to_fuser4   
fuser_funcfuse_custom_config_dictr5   itemr3   new_mod_listr<   s
             r   _fuse_modules_helperrF   S   sv     &"$&=&A&A)2'# 6EE_TE(_HE h0OPL _-E1 . Fs   AFc                     U(       d  [         R                  " U 5      n [        S U 5       5      (       a  [        XX$U5        U $ U H  n[        XX$U5        M     U $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr    )
isinstancestr)r!   module_elements     r   r#    _fuse_modules.<locals>.<genexpr>t   s     
Q~:nc**s   )copydeepcopyallrF   )r   rA   r4   inplacerB   rC   module_lists          r   _fuse_modulesrR   i   s`     e$

Q
QQQF8O	
 L	 +K F8O + Lr   c           	          [        U USUUUS9$ )a  Fuse a list of modules into a single module.

Fuses only the following sequence of modules:
conv, bn
conv, bn, relu
conv, relu
linear, relu
bn, relu
All other sequences are left unchanged.
For these sequences, replaces the first item in the list
with the fused module, replacing the rest of the modules
with identity.

Args:
    model: Model containing the modules to be fused
    modules_to_fuse: list of list of module names to fuse. Can also be a list
                     of strings if there is only a single list of modules to fuse.
    inplace: bool specifying if fusion happens in place on the model, by default
             a new model is returned
    fuser_func: Function that takes in a list of modules and outputs a list of fused modules
                of the same length. For example,
                fuser_func([convModule, BNModule]) returns the list [ConvBNModule, nn.Identity()]
                Defaults to torch.ao.quantization.fuse_known_modules
    `fuse_custom_config_dict`: custom configuration for fusion

.. code-block:: python

   # Example of fuse_custom_config_dict
   fuse_custom_config_dict = {
       # Additional fuser_method mapping
       "additional_fuser_method_mapping": {
           (torch.nn.Conv2d, torch.nn.BatchNorm2d): fuse_conv_bn
       },
   }

Returns:
    model with fused modules. A new copy is created if inplace=True.

Examples::

        >>> # xdoctest: +SKIP
        >>> m = M().eval()
        >>> # m is a module containing the sub-modules below
        >>> modules_to_fuse = [ ['conv1', 'bn1', 'relu1'], ['submodule.conv', 'submodule.relu']]
        >>> fused_m = torch.ao.quantization.fuse_modules(m, modules_to_fuse)
        >>> output = fused_m(input)

        >>> m = M().eval()
        >>> # Alternately provide a single list of modules to fuse
        >>> modules_to_fuse = ['conv1', 'bn1', 'relu1']
        >>> fused_m = torch.ao.quantization.fuse_modules(m, modules_to_fuse)
        >>> output = fused_m(input)

Fr4   rP   rB   rC   rR   r   rA   rP   rB   rC   s        r   r
   r
      s$    z  7 r   c           	          [        U USUUUS9$ )zQAT version for `fuse_modules`.TrT   rU   rV   s        r   r   r      s#      7 r   r    )rM   typingr   torch.nnr0   +torch.ao.quantization.fuser_method_mappingsr   r   r   torch.nn.utils.parametrizer   __all__r   r   r	   rF   rR   r
   r    r   r   <module>r^      st       
 D)%X " 24 ! 8 ! DT ! r   