
    PhO(                        U d dl mZ d dlZd dlmZmZmZmZmZ d dl	Z	d dl
mZ d dlmZ 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DictList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        oC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/ao/ns/fx/qconfig_multi_mapping.py_remove_duplicates_and_noner'      s    I'5{U#+FU3Oxx$$33KQ  '  4	 6 4R4 !    c                      e Zd 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
 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__E   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_mismatchI   s     |s4#=#=>>
 #1"2 $(#=#= $8#;K$+O[$IM+23F+T(,48,S1  - $<
  $> 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,   r3   )r.   r6   argsr!   method_namer8   qconfig
set_methods           r&   _insert_qconfig_listz(QConfigMultiMapping._insert_qconfig_listw   s_     	$L1''e<.u5(+D,F,F(U$OW +>J&&g& )Vr(   c                ,    | j                  dg |       | S )zz
        Set global QConfigs
        see :func:`~torch.ao.quantization.QConfigMapping.set_global()` for more info
        r   rD   )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   rF   )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   rF   )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   rF   )r.   module_namer!   s      r&   r   z#QConfigMultiMapping.set_module_name   rJ   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   rF   )r.   rN   rI   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__).0r8   s     r&   	<genexpr>z/QConfigMultiMapping.__repr__.<locals>.<genexpr>   s&     iNh?b1134A6Nhs    z
])	__class____name__joinr,   r-   s    r&   rU   zQConfigMultiMapping.__repr__   sD    NN##GGidNhNhiij 	
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
        r1   N)
r4   r5   r,   r
   r3   itemsr   r   
isinstancetuple)clsqconfig_mapping_listnew_qconfig_multi_mappingr6   qconfig_dict_listr8   qconfig_dictr<   rB   set_method_namerC   r!   s               r&   from_list_qconfig_mappingz-QConfigMultiMapping.from_list_qconfig_mapping   s     %(E!:>-- ;
!7 *!"-E >@#7&>$0$6$6$8LC"3313)#.%c*11': %9 $8 7u=O !:OLJ%6%<%<%>!\c5)22\2sL1	 &? .* )(r(   N)r!   List[QConfigAny]r6   strreturnNone)r6   rg   r@   zList[Union[str, int, Callable]]r!   rf   rh   ri   )rG   rf   rh   r   )rI   zUnion[Callable, str]r!   rf   rh   r   )rL   rg   r!   rf   rh   r   )rN   rg   r!   rf   rh   r   )
rN   rg   rI   r   r#   intr!   rf   rh   r   )r`   zList[QConfigMapping]rh   r   )rY   
__module____qualname____doc__r/   r=   rD   r   r   r   r   r   rU   classmethodre    r(   r&   r   r   "   s    DN+*,+*58+*	+*\'' .' '	'
 
'$/?O	
!$
4D
	
.>	  	
 ' 
$
 ')#7')	') ')r(   )r!   rf   rh   ri   )
__future__r   r4   typingr   r   r   r   r   r   torch.ao.quantizationr	   %torch.ao.quantization.qconfig_mappingr
   torch.ao.quantization.qconfigr   __all__r   __annotations__r'   r   ro   r(   r&   <module>rw      sV    "  3 3  0 F 4 
! #-"9-.Q, .  Q) Q)r(   