
    Vh'                        U d dl mZ d dlZd dlmZmZmZmZ d dlZd dl	m
Z
 d dlmZ erd dlmZ dgZdd	d
dddZded<   ddZ G d d      Zy)    )annotationsN)AnyCallableTYPE_CHECKINGUnion)QConfigMapping)_QCONFIG_STYLE_ORDER)
QConfigAnyQConfigMultiMapping
set_globalset_object_typeset_module_name_regexset_module_name!set_module_name_object_type_order)global_qconfigobject_type_qconfigsmodule_name_regex_qconfigsmodule_name_qconfigs&module_name_object_type_order_qconfigszdict[str, str]_QCONFIG_STYLE_TO_METHODc                "   g }t        |       D ]a  \  }}||j                  |        nI| d | D ]?  }t        j                  j                  j                  ||      s.|j                  |        a c |d d d   D ]  }| j                  |        y )N)	enumerateappendtorchaoquantizationqconfig_equalspop)qconfig_list	to_removeindexcur_qconfigchecked_qconfigs        T/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/ns/fx/qconfig_multi_mapping.py_remove_duplicates_and_noner&      s    I'5 {U#+FU3 	Oxx$$33KQ  '		 4R4       c                      e Zd ZdZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ		 	 	 	 	 	 ddZ
	 	 	 	 	 	 	 	 	 	 dd	Zd
 Ze	 	 	 	 dd       Zy)r   a&  
    This class, used with the prepare_n_shadows_model API, stores a list of :class:`torch.ao.quantization.QConfigMapping`s
    so that multiple QConfigs can be specified for each QConfig matching style.

    The user can specify QConfigs using the following methods (in increasing match priority):

        ``set_global`` : sets the global (default) QConfigs

        ``set_object_type`` : sets the QConfigs for a given module type, function, or method name

        ``set_module_name_regex`` : sets the QConfigs for modules matching the given regex string

        ``set_module_name`` : sets the QConfigs for modules matching the given module name

        ``set_module_name_object_type_order`` : sets the QConfigs for modules matching a combination
        of the given module name, object type, and the index at which the module appears

    Note: Usage of set methods is the same as in QConfigMapping except with a passed in list of QConfigs rather than a
    single QConfig.

    Example usage::

        qconfig_mapping = QConfigMultiMapping()
            .set_global([qconfig1, qconfig2])
            .set_object_type(torch.nn.Linear, [qconfig2, qconfig3])
            .set_object_type(torch.nn.ReLU, [qconfig1])
            .set_module_name_regex("foo.*bar.*conv[0-9]+", [qconfig2])
            .set_module_name_regex("foo.*", [qconfig1, qconfig2, qconfig3])
            .set_module_name("module1", [None])
            .set_module_name("module2", [qconfig2])
            .set_module_name_object_type_order("foo.bar", torch.nn.functional.linear, 0, [qconfig3])

    c                $    t               g| _        y N)r   qconfig_mappings_listselfs    r%   __init__zQConfigMultiMapping.__init__K   s    <J<L;M"r'   c                r   t        |      t        | j                        kD  rt               }| j                  D ]5  }t        dd  D ]&  }t	        ||      }t	        ||      }|D ]  }d ||<   	 (  n t        |      t        | j                        kD  rQ| j                  j                  t        j                  |             t        |      t        | j                        kD  rPy y t        |      t        | j                        k  r4|j                  d        t        |      t        | j                        k  r3y y )N   )lenr+   r   r	   getattrr   copydeepcopy)	r-   r    stylenew_qconfig_mappingqconfig_mappingcheck_styleqconfigs_dicttarget_qconfigs_dictkeys	            r%   _handle_list_size_mismatchz.QConfigMultiMapping._handle_list_size_mismatchO   s!     |s4#=#=>>
 #1"2 $(#=#= #7#; 9K$+O[$IM+23F+T(, 948,S199
  l#c$*D*D&EE**11$--@S2TU l#c$*D*D&EE l#c$*D*D&EE##D) l#c$*D*D&EEr'   c                    t        |       | j                  ||       t        |   }t        | j                  |      D ]  \  }}t        ||      } |g ||   y r*   )r&   r<   r   zipr+   r2   )r-   r5   argsr    method_namer7   qconfig
set_methods           r%   _insert_qconfig_listz(QConfigMultiMapping._insert_qconfig_list|   sc     	$L1''e<.u5(+D,F,F(U 	'$OW +>J&&g&	'r'   c                ,    | j                  dg |       | S )zz
        Set global QConfigs
        see :func:`~torch.ao.quantization.QConfigMapping.set_global()` for more info
        r   rC   )r-   global_qconfig_lists     r%   r   zQConfigMultiMapping.set_global   s    
 	!!"2B8KLr'   c                .    | j                  d|g|       | S )z
        Set object type QConfigs
        see :func:`~torch.ao.quantization.QConfigMapping.set_object_type()` for more info
        r   rE   )r-   object_typer    s      r%   r   z#QConfigMultiMapping.set_object_type        	!!"8;-Vr'   c                .    | j                  d|g|       | S )z
        Set module_name_regex QConfigs
        see :func:`~torch.ao.quantization.QConfigMapping.set_module_name_regex()` for more info
        r   rE   )r-   module_name_regexr    s      r%   r   z)QConfigMultiMapping.set_module_name_regex   s$     	!!(+<*=|	
 r'   c                .    | j                  d|g|       | S )z
        Set module_name QConfigs
        see :func:`~torch.ao.quantization.QConfigMapping.set_module_name()` for more info
        r   rE   )r-   module_namer    s      r%   r   z#QConfigMultiMapping.set_module_name   rI   r'   c                2    | j                  d|||g|       | S )z
        Set module_name QConfigs
        see :func:`~torch.ao.quantization.QConfigMapping.set_module_name_object_type_order()` for more info
        r   rE   )r-   rM   rH   r"   r    s        r%   r   z5QConfigMultiMapping.set_module_name_object_type_order   s)     	!!4+u-	

 r'   c                    | j                   j                  dz   dj                  d | j                  D              z   dz   S )Nz [ c              3  D   K   | ]  }d |j                          d  yw)
,N)__repr__).0r7   s     r%   	<genexpr>z/QConfigMultiMapping.__repr__.<locals>.<genexpr>   s+      # _--/02s    z
])	__class____name__joinr+   r,   s    r%   rT   zQConfigMultiMapping.__repr__   sJ    NN##gg '+'A'A  	
r'   c                    |        }t        j                  |      |_        t        dd D ]  }i }|D ]C  }t	        ||      }|j                         D ]"  \  }}||vrg ||<   ||   j                  |       $ E t        |   }	t	        ||	      }
|j                         D ])  \  }}t        |t              r |
g ||  ! |
||       +  |S )zN
        Creates a QConfigMultiMapping from a list of QConfigMappings
        r0   N)
r3   r4   r+   r	   r2   itemsr   r   
isinstancetuple)clsqconfig_mapping_listnew_qconfig_multi_mappingr5   qconfig_dict_listr7   qconfig_dictr;   rA   set_method_namerB   r    s               r%   from_list_qconfig_mappingz-QConfigMultiMapping.from_list_qconfig_mapping   s    %(E!:>-- ;
!7 *!"- 	2E >@#7 ;&>$0$6$6$8 ;LC"3313)#.%c*11':;; 7u=O !:OLJ%6%<%<%> 2!\c5)22\2sL1	2	2( )(r'   N)returnNone)r    list[QConfigAny]r5   strre   rf   )r5   rh   r?   zlist[Union[str, int, Callable]]r    rg   re   rf   )rF   rg   re   r   )rH   zUnion[Callable, str]r    rg   re   r   )rK   rh   r    rg   re   r   )rM   rh   r    rg   re   r   )
rM   rh   rH   r   r"   intr    rg   re   r   )r_   zlist[QConfigMapping]re   r   )rX   
__module____qualname____doc__r.   r<   rC   r   r   r   r   r   rT   classmethodrd    r'   r%   r   r   (   s    DN**,**58**	**Z'' .' '	'
 
'"/?O	
!$
4D
	
.>	  	
 ' 
$	
 &)#7&)	&) &)r'   )r    rg   re   rf   )
__future__r   r3   typingr   r   r   r   r   torch.ao.quantizationr   %torch.ao.quantization.qconfig_mappingr	   torch.ao.quantization.qconfigr
   __all__r   __annotations__r&   r   rn   r'   r%   <module>rv      s[    "  6 6  0 F 8 
! #-"9-.Q, .  Q) Q)r'   