
    ȅi7U                        S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
JrJrJr  / SQrSrS	rS
rSrSrSrSrSrSr\ " S S5      5       r " S S5      r " S S5      r " S S5      rg)    )annotations)	dataclass)Any)QConfigMapping)BackendConfig)_get_quant_type_to_str_quant_type_from_str	QuantType)ConvertCustomConfigFuseCustomConfigPrepareCustomConfigStandaloneModuleConfigEntrystandalone_module_namestandalone_module_class%float_to_observed_custom_module_class)observed_to_quantized_custom_module_classnon_traceable_module_namenon_traceable_module_classinput_quantized_idxsoutput_quantized_idxspreserved_attributesc                  >    \ rS rSr% S\S'   S\S'   S\S'   S\S	'   S
rg)r   $   QConfigMapping | Noneqconfig_mappingtuple[Any, ...]example_inputsPrepareCustomConfig | Noneprepare_custom_configBackendConfig | Nonebackend_config N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r"       `/home/james-whalen/.local/lib/python3.13/site-packages/torch/ao/quantization/fx/custom_config.pyr   r   $   s     +*##55((r)   r   c                     \ rS rSrSrSS jrS r            SS jr            SS jr\	R                  4       SS jjr    SS jr    SS	 jrSS
 jrSS jrSS jr\    SS j5       rSS jrSrg)r   .   a  
Custom configuration for :func:`~torch.ao.quantization.quantize_fx.prepare_fx` and
:func:`~torch.ao.quantization.quantize_fx.prepare_qat_fx`.

Example usage::

    prepare_custom_config = PrepareCustomConfig()             .set_standalone_module_name("module1", qconfig_mapping, example_inputs,                 child_prepare_custom_config, backend_config)             .set_standalone_module_class(MyStandaloneModule, qconfig_mapping, example_inputs,                 child_prepare_custom_config, backend_config)             .set_float_to_observed_mapping(FloatCustomModule, ObservedCustomModule)             .set_non_traceable_module_names(["module2", "module3"])             .set_non_traceable_module_classes([NonTraceableModule1, NonTraceableModule2])             .set_input_quantized_indexes([0])             .set_output_quantized_indexes([0])             .set_preserved_attributes(["attr1", "attr2"])
c                t    0 U l         0 U l        0 U l        / U l        / U l        / U l        / U l        / U l        g N)standalone_module_namesstandalone_module_classesfloat_to_observed_mappingnon_traceable_module_namesnon_traceable_module_classesinput_quantized_indexesoutput_quantized_indexesr   selfs    r*   __init__PrepareCustomConfig.__init__B   sB    OQ$RT&LN&57'8:)24$35%/1!r)   c                    U R                   R                  5        VVs0 s H  u  p[        U5      S:  d  M  X_M     nnnSU S3$ s  snnf )Nr   zPrepareCustomConfig()__dict__itemslenr7   kvdict_nonemptys       r*   __repr__PrepareCustomConfig.__repr__L   J    *.--*=*=*?N*?$!3q6A:*?N%m_A66 O
   AAc                8    [        X#XE5      U R                  U'   U $ )aT  
Set the configuration for running a standalone module identified by ``module_name``.

If ``qconfig_mapping`` is None, the parent ``qconfig_mapping`` will be used instead.
If ``prepare_custom_config`` is None, an empty ``PrepareCustomConfig`` will be used.
If ``backend_config`` is None, the parent ``backend_config`` will be used instead.
)r   r/   )r7   module_namer   r   r   r!   s         r*   set_standalone_module_name.PrepareCustomConfig.set_standalone_module_nameP   s'     5P-B5
$$[1 r)   c                8    [        X#XE5      U R                  U'   U $ )aU  
Set the configuration for running a standalone module identified by ``module_class``.

If ``qconfig_mapping`` is None, the parent ``qconfig_mapping`` will be used instead.
If ``prepare_custom_config`` is None, an empty ``PrepareCustomConfig`` will be used.
If ``backend_config`` is None, the parent ``backend_config`` will be used instead.
)r   r0   )r7   module_classr   r   r   r!   s         r*   set_standalone_module_class/PrepareCustomConfig.set_standalone_module_classd   s'     8S-B8
&&|4 r)   c                    U[         R                  :w  a  [        S5      eX0R                  ;  a  0 U R                  U'   X R                  U   U'   U $ )a  
Set the mapping from a custom float module class to a custom observed module class.

The observed module class must have a ``from_float`` class method that converts the float module class
to the observed module class. This is currently only supported for static quantization.
zQset_float_to_observed_mapping is currently only supported for static quantization)r
   STATIC
ValueErrorr1   )r7   float_classobserved_class
quant_types       r*   set_float_to_observed_mapping1PrepareCustomConfig.set_float_to_observed_mappingx   sX     )))c  ;;;9;D**:6BP&&z2;?r)   c                    Xl         U $ )zJ
Set the modules that are not symbolically traceable, identified by name.
)r2   )r7   module_namess     r*   set_non_traceable_module_names2PrepareCustomConfig.set_non_traceable_module_names   s     +7'r)   c                    Xl         U $ )zK
Set the modules that are not symbolically traceable, identified by class.
)r3   )r7   module_classess     r*    set_non_traceable_module_classes4PrepareCustomConfig.set_non_traceable_module_classes   s     -;)r)   c                    Xl         U $ )z
Set the indexes of the inputs of the graph that should be quantized.
Inputs are otherwise assumed to be in fp32 by default instead.
)r4   r7   indexess     r*   set_input_quantized_indexes/PrepareCustomConfig.set_input_quantized_indexes   s    
 (/$r)   c                    Xl         U $ )z
Set the indexes of the outputs of the graph that should be quantized.
Outputs are otherwise assumed to be in fp32 by default instead.
)r5   ra   s     r*   set_output_quantized_indexes0PrepareCustomConfig.set_output_quantized_indexes   s    
 )0%r)   c                    Xl         U $ z
Set the names of the attributes that will persist in the graph module even if they are not used in
the model's ``forward`` method.
r   r7   
attributess     r*   set_preserved_attributes,PrepareCustomConfig.set_preserved_attributes       
 %/!r)   c                   SS jn      SS jnSS jnU " 5       nUR                  [        / 5       HE  u  nnnn	n
U" U[        5      nU" U	[        5      nU" U
[        5      nUR                  UUUUU5        MG     UR                  [        / 5       HE  u  nnnn	n
U" U[        5      nU" U	[        5      nU" U
[        5      nUR	                  UUUUU5        MG     UR                  [
        0 5      R                  5        H>  u  nn[        U5      nUR                  5        H  u  nnUR                  UUU5        M     M@     UR                  UR                  [        / 5      5        UR                  UR                  [        / 5      5        UR                  UR                  [        / 5      5        UR                  UR                  [         / 5      5        UR#                  UR                  [$        / 5      5        U$ )a  
Create a ``PrepareCustomConfig`` from a dictionary with the following items:

    "standalone_module_name": a list of (module_name, qconfig_mapping, example_inputs,
    child_prepare_custom_config, backend_config) tuples

    "standalone_module_class" a list of (module_class, qconfig_mapping, example_inputs,
    child_prepare_custom_config, backend_config) tuples

    "float_to_observed_custom_module_class": a nested dictionary mapping from quantization
    mode to an inner mapping from float module classes to observed module classes, e.g.
    {"static": {FloatCustomModule: ObservedCustomModule}}

    "non_traceable_module_name": a list of modules names that are not symbolically traceable
    "non_traceable_module_class": a list of module classes that are not symbolically traceable
    "input_quantized_idxs": a list of indexes of graph inputs that should be quantized
    "output_quantized_idxs": a list of indexes of graph outputs that should be quantized
    "preserved_attributes": a list of attributes that persist even if they are not used in ``forward``

This function is primarily for backward compatibility and may be removed in the future.
c                    [        U [        5      (       d  U c  U $ [        U [        5      (       a  [        R                  " U 5      $ [	        SU S[        U 5       S35      e)zV
Convert the given object into a QConfigMapping if possible, else throw an exception.
z7Expected QConfigMapping in prepare_custom_config_dict["	"], got '')
isinstancer   dict	from_dictrR   typeobjdict_keys     r*   _get_qconfig_mapping;PrepareCustomConfig.from_dict.<locals>._get_qconfig_mapping   sa     #~..#+
#t$$%//44J8*T^_cdg_h^iijk r)   c                    [        U [        5      (       d  U c  U $ [        U [        5      (       a  [        R                  U 5      $ [	        SU S[        U 5       S35      e)z[
Convert the given object into a PrepareCustomConfig if possible, else throw an exception.
z<Expected PrepareCustomConfig in prepare_custom_config_dict["rr   rs   )rt   r   ru   rv   rR   rw   rx   s     r*   _get_prepare_custom_configAPrepareCustomConfig.from_dict.<locals>._get_prepare_custom_config   sc     #233s{
#t$$*44S99OPXzYcdhildmcnnop r)   c                    [        U [        5      (       d  U c  U $ [        U [        5      (       a  [        R                  " U 5      $ [	        SU S[        U 5       S35      e)zU
Convert the given object into a BackendConfig if possible, else throw an exception.
z6Expected BackendConfig in prepare_custom_config_dict["rr   rs   )rt   r   ru   rv   rR   rw   rx   s     r*   _get_backend_config:PrepareCustomConfig.from_dict.<locals>._get_backend_config   sa     #}--
#t$$$..s33I(S]^bcf^g]hhij r)   )ry   r   rz   strreturnr   )ry   r   rz   r   r   r   )ry   r   rz   r   r   r    )getSTANDALONE_MODULE_NAME_DICT_KEYrJ    STANDALONE_MODULE_CLASS_DICT_KEYrN   FLOAT_TO_OBSERVED_DICT_KEYr>   r	   rV   rZ   "NON_TRACEABLE_MODULE_NAME_DICT_KEYr^   #NON_TRACEABLE_MODULE_CLASS_DICT_KEYrc    INPUT_QUANTIZED_INDEXES_DICT_KEYrf   !OUTPUT_QUANTIZED_INDEXES_DICT_KEYrm   PRESERVED_ATTRIBUTES_DICT_KEY)clsprepare_custom_config_dictr{   r~   r   confrI   qconfig_dictr   _prepare_custom_config_dictbackend_config_dictr   r   r!   rM   quant_type_namecustom_module_mappingrU   rS   rT   s                       r*   rv   PrepareCustomConfig.from_dict   sJ   4
			 #	'	
	 u (++,KRP
'2=O %?+-L%! 1#%DN ++% Q. (++,LbQ
'2>O %?+-M%! 1#%EN ,,% R" 7Q6T6T&7

%'72O2 .o>J/D/J/J/L+^22 0M	7 	++&**+MrR	
 	--&**+NPRS	
 	((&**+KRP	
 	))&**+LbQ	
 	%%&**+H"M	
 r)   c                   SS jn0 nU R                   R                  5        H6  u  p4[        U;  a	  / U[        '   U[           R                  U" X45      5        M8     U R                  R                  5        H6  u  pT[
        U;  a	  / U[
        '   U[
           R                  U" XT5      5        M8     U R                  R                  5        H.  u  nn[        U;  a	  0 U[        '   UU[           [        U5      '   M0     [        U R                  5      S:  a  U R                  U[        '   [        U R                  5      S:  a  U R                  U[        '   [        U R                  5      S:  a  U R                  U[        '   [        U R                   5      S:  a  U R                   U["        '   [        U R$                  5      S:  a  U R$                  U[&        '   U$ )z
Convert this ``PrepareCustomConfig`` to a dictionary with the items described in
:func:`~torch.ao.quantization.fx.custom_config.PrepareCustomConfig.from_dict`.
c                    UR                   (       a  UR                   R                  5       OS nUR                  (       a  UR                  R                  5       OS nU UUR                  UUR                  4$ r.   )r   to_dictr   r   r!   )keyer   r   s       r*   _make_tuple0PrepareCustomConfig.to_dict.<locals>._make_tupleF  sj    :;:K:K1,,446QUL565L5L''//1RV '   *   r)   r   )r   r   r   r   )r/   r>   r   appendr0   r   r1   r   r   r?   r2   r   r3   r   r4   r   r5   r   r   r   )r7   r   drI   sm_config_entryrM   rU   r1   s           r*   r   PrepareCustomConfig.to_dict@  s   	 ,0,H,H,N,N,P(K.a75712-.55K9 -Q .2-K-K-Q-Q-S)L/q86823./66L: .T ++113
%)202,-) ()*@*LM 4 t../!3484S4SA01t001A5595V5VA12t++,q0262N2NA./t,,-1373P3PA/0t(()A-/3/H/HA+,r)   )r1   r4   r3   r2   r5   r   r0   r/   Nr   None)rI   r   r   r   r   r   r   r   r!   r    r   r   )rM   rw   r   r   r   r   r   r   r!   r    r   r   )rS   rw   rT   rw   rU   r
   r   r   )rY   	list[str]r   r   )r]   z
list[type]r   r   )rb   z	list[int]r   r   )rl   r   r   r   )r   dict[str, Any]r   r   r   r   )r#   r$   r%   r&   __doc__r8   rD   rJ   rN   r
   rQ   rV   rZ   r^   rc   rf   rm   classmethodrv   r   r(   r"   r)   r*   r   r   .   s,   &27 / (	
  : - 
( / (	
  : - 
0 !* 0 0	  	
 
*%	(	 E)7E	E EN3r)   r   c                      \ rS rSrSrSS jrS r\R                  4       SS jjr	SS jr
\    SS j5       rSS jrS	rg
)r   iv  a+  
Custom configuration for :func:`~torch.ao.quantization.quantize_fx.convert_fx`.

Example usage::

    convert_custom_config = ConvertCustomConfig()             .set_observed_to_quantized_mapping(ObservedCustomModule, QuantizedCustomModule)             .set_preserved_attributes(["attr1", "attr2"])
c                     0 U l         / U l        g r.   observed_to_quantized_mappingr   r6   s    r*   r8   ConvertCustomConfig.__init__  s    PR*/1!r)   c                    U R                   R                  5        VVs0 s H  u  p[        U5      S:  d  M  X_M     nnnSU S3$ s  snnf )Nr   zConvertCustomConfig(r;   r<   r@   s       r*   rD   ConvertCustomConfig.__repr__  rF   rG   c                d    X0R                   ;  a  0 U R                   U'   X R                   U   U'   U $ )z
Set the mapping from a custom observed module class to a custom quantized module class.

The quantized module class must have a ``from_observed`` class method that converts the observed module class
to the quantized module class.
)r   )r7   rT   quantized_classrU   s       r*   !set_observed_to_quantized_mapping5ConvertCustomConfig.set_observed_to_quantized_mapping  s8     ???=?D..z:IX**:6~Fr)   c                    Xl         U $ ri   rj   rk   s     r*   rm   ,ConvertCustomConfig.set_preserved_attributes  ro   r)   c                $   U " 5       nUR                  [        0 5      R                  5        H;  u  p4[        U5      nUR                  5        H  u  pgUR	                  XgU5        M     M=     UR                  UR                  [        / 5      5        U$ )a  
Create a ``ConvertCustomConfig`` from a dictionary with the following items:

    "observed_to_quantized_custom_module_class": a nested dictionary mapping from quantization
    mode to an inner mapping from observed module classes to quantized module classes, e.g.::
    {
    "static": {FloatCustomModule: ObservedCustomModule},
    "dynamic": {FloatCustomModule: ObservedCustomModule},
    "weight_only": {FloatCustomModule: ObservedCustomModule}
    }
    "preserved_attributes": a list of attributes that persist even if they are not used in ``forward``

This function is primarily for backward compatibility and may be removed in the future.
)r   OBSERVED_TO_QUANTIZED_DICT_KEYr>   r	   r   rm   r   )r   convert_custom_config_dictr   r   r   rU   rT   r   s           r*   rv   ConvertCustomConfig.from_dict  s    $ u6P6T6T*B7

%'72O .o>J3H3N3N3P/66"Z 4Q	7 	%%&**+H"M	
 r)   c                    0 nU R                   R                  5        H.  u  nn[        U;  a	  0 U[        '   UU[           [        U5      '   M0     [	        U R
                  5      S:  a  U R
                  U[        '   U$ )z
Convert this ``ConvertCustomConfig`` to a dictionary with the items described in
:func:`~torch.ao.quantization.fx.custom_config.ConvertCustomConfig.from_dict`.
r   )r   r>   r   r   r?   r   r   )r7   r   rU   r   s       r*   r   ConvertCustomConfig.to_dict  s    
  //557
)-Q64601- ,-.DZ.PQ 8 t(()A-/3/H/HA+,r)   r   Nr   )rT   rw   r   rw   rU   r
   r   r   )rl   r   r   r   )r   r   r   r   r   )r#   r$   r%   r&   r   r8   rD   r
   rQ   r   rm   r   rv   r   r(   r"   r)   r*   r   r   v  su    27 !* 0 0	  	
 
" )7	 >r)   r   c                  P    \ rS rSrSrS
S jrS rSS jr\SS j5       r	SS jr
Srg	)r   i  z
Custom configuration for :func:`~torch.ao.quantization.quantize_fx.fuse_fx`.

Example usage::

    fuse_custom_config = FuseCustomConfig().set_preserved_attributes(
        ["attr1", "attr2"]
    )
c                    / U l         g r.   rj   r6   s    r*   r8   FuseCustomConfig.__init__  s
    /1!r)   c                    U R                   R                  5        VVs0 s H  u  p[        U5      S:  d  M  X_M     nnnSU S3$ s  snnf )Nr   zFuseCustomConfig(r;   r<   r@   s       r*   rD   FuseCustomConfig.__repr__  sJ    *.--*=*=*?N*?$!3q6A:*?N"=/33 OrG   c                    Xl         U $ ri   rj   rk   s     r*   rm   )FuseCustomConfig.set_preserved_attributes  ro   r)   c                ^    U " 5       nUR                  UR                  [        / 5      5        U$ )a  
Create a ``ConvertCustomConfig`` from a dictionary with the following items:

    "preserved_attributes": a list of attributes that persist even if they are not used in ``forward``

This function is primarily for backward compatibility and may be removed in the future.
)rm   r   r   )r   fuse_custom_config_dictr   s      r*   rv   FuseCustomConfig.from_dict  s1     u%%#''(ErJ	
 r)   c                b    0 n[        U R                  5      S:  a  U R                  U[        '   U$ )z
Convert this ``FuseCustomConfig`` to a dictionary with the items described in
:func:`~torch.ao.quantization.fx.custom_config.ConvertCustomConfig.from_dict`.
r   )r?   r   r   )r7   r   s     r*   r   FuseCustomConfig.to_dict  s2    
 t(()A-/3/H/HA+,r)   rj   Nr   )rl   r   r   r   )r   r   r   r   r   )r#   r$   r%   r&   r   r8   rD   rm   r   rv   r   r(   r"   r)   r*   r   r     s/    24  r)   r   N)
__future__r   dataclassesr   typingr   torch.ao.quantizationr   $torch.ao.quantization.backend_configr    torch.ao.quantization.quant_typer   r	   r
   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r)   r*   <module>r      s    " !  0 >  #; #<  D !L %@ "&B ##9  $; ! 6  ) ) )E EP
^ ^B2 2r)   