§
    Ÿ²ÅjŒz  ã                   ób  — d dl Z d dlZd dlZd dlZd dlZd dlmc mc mZ	 d dl
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZmZmZmZmZmZmZmZ d dlm Z m!Z! d dl"m#Z# dd	l$m%Z%m&Z& g d
¢Z'eZ(ej)        ej*        j)        ej+        ej*        j+        iej*        j)        ej        j)        ej*        j+        ej        j+        idœZ,d„ Z-	 	 	 d#d„Z.d$d„Z/d„ Z0d„ Z1d%d„Z2	 	 	 	 d&d„Z3d„ Z4d„ Z5	 	 	 	 d'd„Z6d„ Z7d„ Z8d(d„Z9dej:        ddfd„Z;d(d„Z<d%d„Z=	 	 	 	 	 	 d)d„Z>	 	 	 	 	 d*d „Z?	 d%d!„Z@d+d"„ZAdS ),é    N)Ú_FusedModule)Ú_is_activation_post_process)Ú_activation_is_memorylessÚ_add_module_to_qconfig_obs_ctrÚdefault_dynamic_qconfigÚfloat16_dynamic_qconfigÚ!float_qparams_weight_only_qconfigÚ&float_qparams_weight_only_qconfig_4bit)Ú_get_special_act_post_processÚ_has_special_act_post_processÚ)get_default_dynamic_quant_module_mappingsÚget_default_qat_module_mappingsÚ$get_default_qconfig_propagation_listÚ(get_default_static_quant_module_mappingsÚ2get_default_static_quant_reference_module_mappingsÚno_observer_set)ÚDeQuantStubÚQuantWrapper)Útype_before_parametrizationsé   )Úget_qparam_dictÚ)has_no_children_ignoring_parametrizations)
Úget_default_custom_config_dictÚpropagate_qconfig_Úadd_quant_dequantÚprepareÚquantizeÚquantize_dynamicÚprepare_qatÚquantize_qatÚconvertÚswap_module)Ú%float_to_observed_custom_module_classÚ)observed_to_quantized_custom_module_classc                  ó   — t           S )z'Defines the default custom config dict.)Ú_DEFAULT_CUSTOM_CONFIG_DICT© ó    úOC:\Projects\Historical\venv\Lib\site-packages\torch/ao/quantization/quantize.pyr   r   B   s   € å&Ð&r(   Ú c                 ó
  — |                      t          | ¦  «        |¦  «        }|                      ||¦  «        }t          | d|¦  «        }t          j        j        j                             || ¦  «         t          || ¦  «        }|| _        |  	                    ¦   «         D ]b\  }}|r|dz   |z   n|}	|�=||                      dg ¦  «        v s7t          |¦  «        |                      dg ¦  «        v st          ||||	¦  «         ŒcdS )aò  This is a helper function for `propagate_qconfig_`

    Args:
        module: input module
        qconfig_dict: dictionary that maps from name of submodule to quantization
                     configuration
        qconfig_parent: quantization config of parent module, we will fallback to
                       this config when there is no specified config for current
                       module
        prefix: corresponding prefix of the current module, used as key in
                qconfig_dict
        prepare_custom_config_dict: dictionary for custom handling of modules
                                    see docs for :func:`~torch.ao.quantization.prepare_fx`

    Return:
        None, module is modified inplace with qconfig attached
    Úqconfigú.NÚnon_traceable_module_nameÚnon_traceable_module_class)Úgetr   ÚgetattrÚtorchÚaoÚquantizationr,   Ú_assert_valid_qconfigr   Únamed_childrenÚtypeÚ_propagate_qconfig_helper)
ÚmoduleÚqconfig_dictÚqconfig_parentÚprefixÚprepare_custom_config_dictÚmodule_qconfigÚqconfig_with_device_checkÚnameÚchildÚmodule_prefixs
             r)   r8   r8   G   s0  € ð2 "×%Ò%Ý$ VÑ,Ô,¨nñô €Nð "×%Ò% f¨nÑ=Ô=€NÝ˜V Y°Ñ?Ô?€Nå	„HÔÔ!×7Ò7¸ÈÑOÔOÐOå >¸~ÈvÑ VÔ VÐØ.€F„Nà×,Ò,Ñ.Ô.ð 
ð 
‰ˆˆeØ/5Ð?˜ ™ tÑ+Ð+¸4ˆà%Ð-ØÐ.×2Ò2Ð3NÐPRÑSÔSÐSÐSÝ�E‰{Œ{Ø)×-Ò-Ð.JÈBÑOÔOðPð Põ &Ø�|Ð%>Àñô ð øð
ð 
r(   c                 ó:   — |€i }|€i }t          | ||¬¦  «         dS )a“  Propagate qconfig through the module hierarchy and assign `qconfig`
    attribute on each leaf module

    Args:
        module: input module
        qconfig_dict: dictionary that maps from name or type of submodule to
            quantization configuration, qconfig applies to all submodules of a
            given module unless qconfig for the submodules are specified (when
            the submodule already has qconfig attribute)
        prepare_custom_config_dict: dictionary for custom handling of modules
            see docs for :func:`~torch.ao.quantization.prepare_fx`

    Return:
        None, module is modified inplace with qconfig attached
    N)r=   )r8   )r9   r:   r=   s      r)   r   r   x   sD   € ð  ÐØˆØ!Ð)Ø%'Ð"ÝØ�Ð9Sðñ ô ð ð ð r(   c                 ó,   — |                       |¦  «        S )z.Forward hook that calls observer on the output©Úactivation_post_process)ÚselfÚinputÚoutputs      r)   Ú_observer_forward_hookrJ   ‘   s   € à×'Ò'¨Ñ/Ô/Ð/r(   c                 ó8   — |                       |d         ¦  «        S )z2Forward pre hook that calls observer on the outputr   rE   )rG   rH   s     r)   Ú_observer_forward_pre_hookrL   –   s   € à×'Ò'¨¨a¬Ñ1Ô1Ð1r(   Fc                 ó²   — t          | d¦  «        s
J d¦   «         ‚|r|                      t          d¬¦  «        }d S |                      t          d¬¦  «        }d S )NrF   zGExpect activation_post_process attribute already attached to the moduleT)Úprepend)ÚhasattrÚregister_forward_pre_hookrL   Úregister_forward_hookrJ   )r9   Úpre_hookÚhandles      r)   Ú&_register_activation_post_process_hookrT   ›   s‚   € ÝØÐ)ñô ð Qð QàPñQô Qð ð ð TØ×1Ò1Ý&°ð 2ñ 
ô 
ˆˆˆð ×-Ò-Õ.DÈdÐ-ÑSÔSˆˆˆr(   c                 ó  ‡‡‡— |€t          ¦   «         }|€i }‰€`t          | ¦  «        }t          |¦  «        dk    sJ d|› �¦   «         ‚t          |¦  «        dk    rt          t	          |¦  «        ¦  «        ndŠdd„Šd„ Šdˆˆˆfd„	}|                      ¦   «         D �]Å\  }}t          |¦  «        t          j        fv rŒ#t          t          |¦  «        t          j        t          j        f¦  «        rM ‰|¦  «        rAt          |d¦  «        sJ d	t          |¦  «        › d
�¦   «         ‚ ‰|j        ‰¦  «        |_        Œ£t!          |t"          ¦  «        r ‰|¦  «        r ||¦  «         ŒÏ|�(t          |¦  «        |v r ‰|¦  «        r ||¦  «         Œùt%          |¦  «        rt'          |¦  «        }	 |||	¦  «         �Œ% ‰|¦  «        r‚t          |¦  «        |v rq|t          |¦  «                 }
|
                     |¦  «        }t+          | ||¦  «         t          |
t-          t/          ¦   «         ¦  «        ¦  «        s ||¦  «         �Œ²t1          |||‰|¦  «         �ŒÇt3          | ¦  «        r;t!          | t4          j        j        ¦  «        st          | ¦  «        |v r || ¦  «         t          | d¦  «        r=t!          | t4          j        j        ¦  «        s t          | ¦  «        |v r || ¦  «         dS dS dS dS )as  Add observer for the leaf child of the module.

    This function insert observer module to all leaf child module that
    has a valid qconfig attribute.

    Args:
        module: input module with qconfig attributes for all the leaf modules that we want to quantize
        qconfig_propagation_list: a list of quantizable modules that will have observers added to them
            if they are leaf nodes
        device: parent device, if any
        non_leaf_module_list: list of non-leaf modules we want to add observer

    Return:
        None, module is modified inplace with added observer modules and forward_hooks
    Nr   zR_add_observer_ only works with cpu or single-device CUDA modules, but got devices r   c                 ót   — |€|                       ¦   «         n	 |¦   «         }|�|                     |¦  «         |S ©N)Ú
activationÚto)r,   ÚdeviceÚspecial_act_post_processrX   s       r)   Úget_activation_post_processz3_add_observer_.<locals>.get_activation_post_processË   sO   € ð (Ð/ð ×ÒÑ Ô Ð à)Ð)Ñ+Ô+ð 	ð
 ÐØ�MŠM˜&Ñ!Ô!Ð!ØÐr(   c                 ó4   — t          | d¦  «        o| j        d uS )Nr,   ©rO   r,   )Úms    r)   Úneeds_observationz)_add_observer_.<locals>.needs_observationÕ   s   € Ý�q˜)Ñ$Ô$Ð>¨¬¸$Ð)>Ð>r(   c                 óâ   •—  ‰| ¦  «        r`t          | t          ¦  «        sM|                      d ‰| j        ‰|¦  «        ¦  «         t	          | t          | j        ¦  «        ¬¦  «         dS dS dS )zmAdds an activation post process module and register
        a pre or post hook that calls the module
        rF   ©rR   N)Ú
isinstancer   Ú
add_moduler,   rT   r   )r_   r[   rZ   r\   r`   s     €€€r)   Úinsert_activation_post_processz6_add_observer_.<locals>.insert_activation_post_processØ   s£   ø€ ð
 Ð˜QÑÔð 	­
°1µkÑ(BÔ(Bð 	à�LŠLØ)Ø+Ð+Ø”I˜vÐ'?ñô ñô ð õ 3ØÕ5°a´iÑ@Ô@ðñ ô ð ð ð ð	ð 	ð 	ð 	r(   rF   zfunctional class z- has no pre-defined `activation_post_process`Úweight_fake_quantrW   )r   Ú_get_unique_devices_ÚlenÚnextÚiterr6   r   ÚnnÚDropoutÚ
issubclassÚnnqÚFloatFunctionalÚQFunctionalrO   r,   rF   rc   r   r   r   Ú
from_floatÚsetattrÚtupler   Ú_add_observer_r   r2   Ú
Sequential)r9   Úqconfig_propagation_listÚnon_leaf_module_listrZ   Úcustom_module_class_mappingÚdevicesre   r@   rA   r[   Úobserved_classÚobserved_childr\   r`   s      `        @@r)   rt   rt   §   sç  øøø€ ð,  Ð'Ý#GÑ#IÔ#IÐ à"Ð*Ø&(Ð#ð €~Ý& vÑ.Ô.ˆå�‰LŒL˜AÒÐÐØiÐ`gÐiÐiñ ÔÐå(+¨G©¬°qÒ(8Ð(8••d˜7‘m”mÑ$Ô$Ð$¸dˆðð ð ð ð?ð ?ð ?ðð ð ð ð ð ð ð ð& ×,Ò,Ñ.Ô.ð /ñ /‰ˆˆeå'¨Ñ.Ô.µ2´:°,Ð>Ð>ØÝÝ(¨Ñ/Ô/µ#Ô2EÅsÄÐ1Wñ
ô 
ð +	ð !Ð  Ñ'Ô'ð ÝØÐ4ñô ð zð zàyÕ'CÀEÑ'JÔ'JÐyÐyÐyñzô zð ð 1LÐ0KØ”M 6ñ1ô 1�Ô-øõ ˜�|Ñ,Ô,ð !	à Ð  Ñ'Ô'ð 6Ø.Ð.¨uÑ5Ô5Ð5øà Ð,Ý,¨UÑ3Ô3Ð7KÐKÐKà Ð  Ñ'Ô'ð 6Ø.Ð.¨uÑ5Ô5Ð5øÝ*¨5Ñ1Ô1ð 	Ý'DÀUÑ'KÔ'KÐ$Ø*Ð*¨5Ð2JÑKÔKÐKÑKàÐ˜eÑ$Ô$ð	å,¨UÑ3Ô3Ð7RÐRÐRà8Ý,¨UÑ3Ô3ôˆNð ,×6Ò6°uÑ=Ô=ˆNÝ�F˜D .Ñ1Ô1Ð1õ ˜n­eµOÑ4EÔ4EÑ.FÔ.FÑGÔGð ?Ø.Ð.¨~Ñ>Ô>Ð>ùåØØ(Ø$ØØ+ñô ð ñ õ 	2°&Ñ9Ô9ð/å˜6¥5¤8Ô#6Ñ7Ô7ð/õ )¨Ñ0Ô0Ð4LÐLÐLà&Ð& vÑ.Ô.Ð.õ 	�Ð+Ñ,Ô,ð/å˜6¥5¤8Ô#6Ñ7Ô7ð/õ )¨Ñ0Ô0Ð4LÐLÐLà&Ð& vÑ.Ô.Ð.Ð.Ð.ð/ð /ð /ð /ð MÐLr(   c                 ó|   — d„ |                       ¦   «         D ¦   «         d„ |                      ¦   «         D ¦   «         z  S )Nc                 ó   — h | ]	}|j         ’Œ
S r'   ©rZ   ©Ú.0Úps     r)   ú	<setcomp>z'_get_unique_devices_.<locals>.<setcomp>1  s   € Ð2Ð2Ð2˜ˆAŒHÐ2Ð2Ð2r(   c                 ó   — h | ]	}|j         ’Œ
S r'   r~   r   s     r)   r‚   z'_get_unique_devices_.<locals>.<setcomp>1  s'   € ð 6ð 6ð 6ØˆŒð6ð 6ð 6r(   )Ú
parametersÚbuffers)r9   s    r)   rg   rg   0  sO   € Ø2Ð2˜f×/Ò/Ñ1Ô1Ð2Ñ2Ô2ð 6ð 6Ø Ÿ.š.Ñ*Ô*ð6ñ 6ô 6ñ ð r(   c                 óÒ   — t          | ¦  «        r&t          | d¦  «        r| j        rt          | ¦  «        S |                      ¦   «         D ]\  }}t          |¦  «        | j        |<   Œ| S )a{  Wrap the leaf child module in QuantWrapper if it has a valid qconfig
    Note that this function will modify the children of module inplace and it
    can return a new module which wraps the input module as well.

    Args:
        module: input module with qconfig attributes for all the leaf modules
        that we want to quantize

    Return:
        Either the inplace modified module with submodules wrapped in
        `QuantWrapper` based on qconfig or a new `QuantWrapper` module which
        wraps the input module, the latter case only happens when the input
        module is a leaf module and we want to quantize it.
    r,   )r   rO   r,   r   r6   r   Ú_modules)r9   r@   rA   s      r)   r   r   6  s}   € õ  	2°&Ñ9Ô9ð$å�F˜IÑ&Ô&ð$ð ŒNð$õ
 ˜FÑ#Ô#Ð#à×,Ò,Ñ.Ô.ð 9ð 9‰ˆˆeÝ 1°%Ñ 8Ô 8ˆŒ˜ÑÐØ€Mr(   c                 ó¦  — t           j                             d¦  «         |€t          ¦   «         }|                     di ¦  «        }|st          j        | ¦  «        } |}|€t          ¦   «         }t          | d¬¦  «         t          d„ |  
                    ¦   «         D ¦   «         ¦  «        st          j        d¦  «         t          | |||¬¦  «         | S )aƒ  Prepares a copy of the model for quantization calibration or quantization-aware training.

    Quantization configuration should be assigned preemptively
    to individual submodules in `.qconfig` attribute.

    The model will be attached with observer or fake quant modules, and qconfig
    will be propagated.

    Args:
        `model`: input model to be modified in-place
        `inplace`: carry out model transformations in-place, the original module is mutated
        `allow_list`: list of quantizable modules
        `observer_non_leaf_module_list`: list of non-leaf modules we want to add observer
        `prepare_custom_config_dict`: customization configuration dictionary for prepare function

    .. code-block:: python

       # Example of prepare_custom_config_dict:
       prepare_custom_config_dict = {
           # user will manually define the corresponding observed
           # module class which has a from_float class method that converts
           # float custom module to observed custom module
           "float_to_observed_custom_module_class": {
               CustomModule: ObservedCustomModule
           }
        }

    z!quantization_api.quantize.prepareNr#   ©r:   c              3   óD   K  — | ]}t          |d ¦  «        o|j        V — ŒdS )r,   Nr^   )r€   r_   s     r)   ú	<genexpr>zprepare.<locals>.<genexpr>…  s3   è è € ÐLÐL°q�w�q˜)Ñ$Ô$Ð2¨¬ÐLÐLÐLÐLÐLÐLr(   z¬None of the submodule got qconfig applied. Make sure you passed correct configuration through `qconfig_dict` or by assigning the `.qconfig` attribute directly on submodules)rx   )r2   Ú_CÚ_log_api_usage_oncer   r0   ÚcopyÚdeepcopyr   r   ÚanyÚmodulesÚwarningsÚwarnrt   )ÚmodelÚinplaceÚ
allow_listÚobserver_non_leaf_module_listr=   rx   rv   s          r)   r   r   Q  sþ   € õF 
„H× Ò Ð!DÑEÔEÐEØ!Ð)Ý%CÑ%EÔ%EÐ"Ø"<×"@Ò"@Ø/°ñ#ô #Ðð ð %Ý”˜eÑ$Ô$ˆð  *ÐØÐÝ#GÑ#IÔ#IÐ Ý�u¨4Ð0Ñ0Ô0Ð0õ ÐLÐL¸E¿MºM¹O¼OÐLÑLÔLÑLÔLð 
ÝŒðKñ	
ô 	
ð 	
õ ØØ Ø%Ø$?ð	ñ ô ð ð €Lr(   c                 ó¬   ‡ — t          ‰ d¦  «        r$t          ‰ j        ¦  «        rt          ‰ d¦  «         dˆ fd„	} |d¬¦  «          |d¬¦  «         d S )NrF   Fc                 óþ   •— | r‰j         n‰j        }| rt          nt          }t	          ¦   «         }|                     ¦   «         D ]\  }}||u r|                     |¦  «         Œ|D ]}|                     |¦  «         Œd S rW   )Ú_forward_pre_hooksÚ_forward_hooksrL   rJ   ÚsetÚitemsÚaddÚpop)rR   Úhook_mapÚobserver_hookÚhandle_ids_to_removeÚ	handle_idÚhook_fnr9   s         €r)   Úremove_hooksz5_remove_activation_post_process.<locals>.remove_hooksž  s¤   ø€ Ø08ÐS�6Ô,Ð,¸fÔ>Sˆà*2ÐNÕ&Ð&Õ8Nð 	õ  #™uœuÐØ"*§.¢.Ñ"2Ô"2ð 	4ð 	4ÑˆI�wØ˜-Ð'Ð'Ø$×(Ò(¨Ñ3Ô3Ð3øØ-ð 	$ð 	$ˆIØ�LŠL˜Ñ#Ô#Ð#Ð#ð	$ð 	$r(   Trb   ©F)rO   r   rF   Údelattr)r9   r¥   s   ` r)   Ú_remove_activation_post_processr¨   •  s�   ø€ õ ˆvÐ0Ñ1Ô1ð 3Õ6QØÔ&ñ7ô 7ð 3õ 	�Ð1Ñ2Ô2Ð2ð
$ð 
$ð 
$ð 
$ð 
$ð 
$ð €L˜$ÐÑÔÐØ€L˜%Ð Ñ Ô Ð Ð Ð r(   c                 ó”   — |                       ¦   «         D ]}t          |¦  «         Œt          | d¦  «        r| `t	          | ¦  «         dS )zŠClean up the qconfig left in the module so that new qconfig can be
    propagated.

    Args:
        module: module to be cleaned up
    r,   N)ÚchildrenÚ_remove_qconfigrO   r,   r¨   )r9   rA   s     r)   r«   r«   ¯  sY   € ð —’Ñ"Ô"ð ð ˆÝ˜ÑÔÐÐåˆv�yÑ!Ô!ð ØˆNå# FÑ+Ô+Ð+Ð+Ð+r(   c                 ó  — t           j                             d¦  «         |€t          ¦   «         }|st	          j        | ¦  «        } |                      ¦   «          t          | d¬¦  «          || g|¢R Ž  t          | |d¬¦  «         | S )aƒ  Quantize the input float model with post training static quantization.

    First it will prepare the model for calibration, then it calls
    `run_fn` which will run the calibration step, after that we will
    convert the model to a quantized model.

    Args:
        model: input float model
        run_fn: a calibration function for calibrating the prepared model
        run_args: positional arguments for `run_fn`
        inplace: carry out model transformations in-place, the original module is mutated
        mapping: correspondence between original module types and quantized counterparts

    Return:
        Quantized model.
    z"quantization_api.quantize.quantizeNT©r•   )	r2   rŒ   r�   r   rŽ   r�   Úevalr   r!   )r”   Úrun_fnÚrun_argsÚmappingr•   s        r)   r   r   ¿  s”   € õ" 
„H× Ò Ð!EÑFÔFÐFØ€Ý:Ñ<Ô<ˆØð %Ý”˜eÑ$Ô$ˆØ	‡J‚J�L„L€LÝˆE˜4Ð Ñ Ô Ð Ø
€Fˆ5Ð�8ÐÐÐÐÝˆE�7 DÐ)Ñ)Ô)Ð)Ø€Lr(   c                 ó4  — t           j                             d¦  «         |�€`|t           j        k    rjt          j        t          t          j        t          t          j        t          t          j	        t          t          j
        t          t          j        t          i}�nŸ|t           j        k    rjt          j        t          t          j        t          t          j        t          t          j	        t          t          j
        t          t          j        t          i}�n%|t           j        k    r%t          j        t           t          j        t           i}nð|t           j        k    rt          j        t&          i}nÌt)          d|› d�¦  «        ‚t+          |t,          ¦  «        r¤|t           j        u rt          }n_|t           j        u rt          }nI|t           j        u rt           }n3|t           j        u rt&          }nt/          dt1          |¦  «        ¦  «        ‚t3          t5          |t7          j        |¦  «        ¦  «        ¦  «        }|€t;          ¦   «         }|st=          j        | ¦  «        } |                       ¦   «          tC          | |¦  «         tE          | |d¬¦  «         | S )av  Converts a float model to dynamic (i.e. weights-only) quantized model.

    Replaces specified modules with dynamic weight-only quantized versions and output the quantized model.

    For simplest usage provide `dtype` argument that can be float16 or qint8. Weight-only quantization
    by default is performed for layers with large weights size - i.e. Linear and RNN variants.

    Fine grained control is possible with `qconfig` and `mapping` that act similarly to `quantize()`.
    If `qconfig` is provided, the `dtype` argument is ignored.

    Args:
        model: input model
        qconfig_spec: Either:

            - A dictionary that maps from name or type of submodule to quantization
              configuration, qconfig applies to all submodules of a given
              module unless qconfig for the submodules are specified (when the
              submodule already has qconfig attribute). Entries in the dictionary
              need to be QConfig instances.

            - A set of types and/or submodule names to apply dynamic quantization to,
              in which case the `dtype` argument is used to specify the bit-width

        inplace: carry out model transformations in-place, the original module is mutated
        mapping: maps type of a submodule to a type of corresponding dynamically quantized version
            with which the submodule needs to be replaced

    z*quantization_api.quantize.quantize_dynamicNz5Don't know how to quantize with default settings for z. Provide full qconfig pleasez.Unknown dtype specified for quantize_dynamic: Tr­   )#r2   rŒ   r�   Úqint8rk   ÚLinearr   ÚLSTMÚGRUÚLSTMCellÚRNNCellÚGRUCellÚfloat16r   Úquint8ÚEmbeddingBagr	   Ú	EmbeddingÚquint4x2r
   Ú
ValueErrorrc   rœ   ÚRuntimeErrorÚstrÚdictÚzipÚ	itertoolsÚrepeatr   rŽ   r�   r®   r   r!   )r”   Úqconfig_specÚdtyper±   r•   Údefault_qconfigs         r)   r   r   Ü  s  € õ> 
„H× Ò Ð!MÑNÔNÐNØÑØ•E”KÒÐå”	Õ2Ý”Õ0Ý”Õ/Ý”Õ4Ý”
Õ3Ý”
Õ3ðˆL‰Lð •e”mÒ#Ð#å”	Õ2Ý”Õ0Ý”Õ/Ý”Õ4Ý”
Õ3Ý”
Õ3ðˆL‰Lð •e”lÒ"Ð"å”Õ!BÝ”Õ?ðˆLˆLð •e”nÒ$Ð$å”Õ!GðˆLˆLõ ØlÈÐlÐlÐlñô ð õ 
�L¥#Ñ	&Ô	&ð RØ•E”KÐÐÝ5ˆOˆOØ•e”mÐ#Ð#Ý5ˆOˆOØ•e”lÐ"Ð"Ý?ˆOˆOØ•e”nÐ$Ð$ÝDˆOˆOåØ@Å#ÀeÁ*Ä*ñô ð õ �C ­iÔ.>¸Ñ.OÔ.OÑPÔPÑQÔQˆà€Ý;Ñ=Ô=ˆàð %Ý”˜eÑ$Ô$ˆØ	‡J‚J�L„L€LÝ�u˜lÑ+Ô+Ð+ÝˆE�7 DÐ)Ñ)Ô)Ð)Ø€Lr(   c                 ó\  — t           j                             d¦  «         | j        s
J d¦   «         ‚|€t	          ¦   «         }|st          j        | ¦  «        } t          | d¬¦  «         t          | |dd¬¦  «         t          | t          |                     ¦   «         ¦  «        d¬¦  «         | S )	a  
    Prepares a copy of the model for quantization calibration or
    quantization-aware training and converts it to quantized version.

    Quantization configuration should be assigned preemptively
    to individual submodules in `.qconfig` attribute.

    Args:
        model: input model to be modified in-place
        mapping: dictionary that maps float modules to quantized modules to be
                 replaced.
        inplace: carry out model transformations in-place, the original module
                 is mutated
    z%quantization_api.quantize.prepare_qatz1prepare_qat only works on models in training modeNr‰   TF)r±   r•   Úremove_qconfig)r—   r•   )r2   rŒ   r�   Útrainingr   rŽ   r�   r   r!   r   rœ   Úvalues)r”   r±   r•   s      r)   r   r   6  s¯   € õ 
„H× Ò Ð!HÑIÔIÐIØŒ>ÐNÐNÐNÑNÔNˆ>Ø€Ý1Ñ3Ô3ˆàð %Ý”˜eÑ$Ô$ˆå�u¨4Ð0Ñ0Ô0Ð0ÝˆE˜7¨DÀÐGÑGÔGÐGÝˆEµ°W·^²^Ñ5EÔ5EÑ1FÔ1FÐPTÐUÑUÔUÐUØ€Lr(   c                 óî   — t           j                             d¦  «         |st          j        | ¦  «        } |                      ¦   «          t          | d¬¦  «          || g|¢R Ž  t          | d¬¦  «         | S )ag  Do quantization aware training and output a quantized model

    Args:
        model: input model
        run_fn: a function for evaluating the prepared model, can be a
                function that simply runs the prepared model or a training
                loop
        run_args: positional arguments for `run_fn`

    Return:
        Quantized model.
    z&quantization_api.quantize.quantize_qatTr­   )r2   rŒ   r�   rŽ   r�   Útrainr   r!   )r”   r¯   r°   r•   s       r)   r    r    S  s‚   € õ 
„H× Ò Ð!IÑJÔJÐJØð %Ý”˜eÑ$Ô$ˆØ	‡K‚K�M„M€MÝ�˜tÐ$Ñ$Ô$Ð$Ø
€Fˆ5Ð�8ÐÐÐÐÝˆE˜4Ð Ñ Ô Ð Ø€Lr(   Tc                 ó¼   — t           j                             d¦  «         |st          j        | ¦  «        } t          | |d|||¬¦  «         |rt          | ¦  «         | S )a¼  Converts submodules in input module to a different module according to `mapping`
    by calling `from_float` method on the target module class. And remove qconfig at the
    end if remove_qconfig is set to True.

    Args:
        `module`: prepared and calibrated module
        `mapping`: a dictionary that maps from source module type to target
                   module type, can be overwritten to allow swapping user defined
                   Modules
        `inplace`: carry out model transformations in-place, the original module
                   is mutated
        `convert_custom_config_dict`: custom configuration dictionary for convert function
        `use_precomputed_fake_quant`: a flag to enable use of precomputed fake quant

    .. code-block:: python

       # Example of convert_custom_config_dict:
       convert_custom_config_dict = {
           # user will manually define the corresponding quantized
           # module class which has a from_observed class method that converts
           # observed custom module to quantized custom module
           "observed_to_quantized_custom_module_class": {
               ObservedCustomModule: QuantizedCustomModule
           }
       }

    z!quantization_api.quantize.convertT)r•   Úis_referenceÚconvert_custom_config_dictÚuse_precomputed_fake_quant)r2   rŒ   r�   rŽ   r�   Ú_convertr«   )r9   r±   r•   rÊ   rÐ   rÑ   rÒ   s          r)   r!   r!   j  sy   € õH 
„H× Ò Ð!DÑEÔEÐEØð 'Ý”˜vÑ&Ô&ˆÝØØØØ!Ø#=Ø#=ðñ ô ð ð ð  Ý˜ÑÔÐØ€Mr(   c           	      óÞ  — |€|rt          ¦   «         nt          ¦   «         }|€t          ¦   «         }|                     di ¦  «        }|st	          j        | ¦  «        } i }|                      ¦   «         D ]U\  }}	t          |	t          ¦  «        s&t          |	¦  «        |vrt          |	|d|||¬¦  «         t          |	|||¦  «        ||<   ŒV|                     ¦   «         D ]\  }
}|| j        |
<   Œ| S )ao  Converts submodules in input module to a different module according to `mapping`
    by calling `from_float` method on the target module class

    Args:
        module: input module
        mapping: a dictionary that maps from source module type to target
                 module type, can be overwritten to allow swapping user defined
                 Modules
        inplace: carry out model transformations in-place, the original module
                 is mutated
        is_reference: a flag to enable quantized reference module
        use_precomputed_fake_quant: a flag to enable use of precomputed fake quant

    Nr$   T©rÒ   )r   r   r   r0   rŽ   r�   r6   rc   r   r   rÓ   r"   r�   r‡   )r9   r±   r•   rÐ   rÑ   rÒ   rx   Úreassignr@   ÚmodÚkeyÚvalues               r)   rÓ   rÓ   ž  s?  € ð, €ð ð<Õ>Ñ@Ô@Ð@å9Ñ;Ô;ð 	ð
 "Ð)Ý%CÑ%EÔ%EÐ"Ø"<×"@Ò"@Ø3°Rñ#ô #Ðð ð 'Ý”˜vÑ&Ô&ˆØ€HØ×*Ò*Ñ,Ô,ð 
ð 
‰	ˆˆcõ ˜3¥Ñ-Ô-ð	å,¨SÑ1Ô1Ð9TÐTÐTåØØØØØ*Ø+Eðñ ô ð õ %Ø�Ð5Ð7Qñ
ô 
ˆ�‰ˆð —n’nÑ&Ô&ð %ð %‰
ˆˆUØ$ˆŒ˜ÑÐà€Mr(   c                 óL  — | }t          | d¦  «        �r| j        ��d}t          | ¦  «        |v r+|t          | ¦  «                                      | ¦  «        }d}næt          | ¦  «        |v rÕ|t          | ¦  «                 }t          |d¦  «        r_|j        rX| j        €J ‚| j                             ¦   «         } || j        ¦  «         t          |¦  «        }|                     | |¦  «        }nOt          j	        |j        ¦  «        }	d|	j
        v r|                     | |¬¦  «        }n|                     | ¦  «        }d}|râ| j                             ¦   «         D ]}
|                     |
¦  «         Œ| j                             ¦   «         D ] }|t          ur|                     |¦  «         Œ!t#          | ¦  «        }t%          |¦  «        dk    sJ d	|› �¦   «         ‚t%          |¦  «        d
k    rt'          t)          |¦  «        ¦  «        nd}|r|                     |¦  «         |S )a	  Swaps the module if it has a quantized counterpart and it has an
    `observer` attached.

    Args:
        mod: input module
        mapping: a dictionary that maps from nn module to nnq module

    Return:
        The corresponding quantized module of `mod`
    r,   NFTÚ_IS_REFERENCErÒ   rÕ   r   zOswap_module only works with cpu or single-device CUDA modules, but got devices r   )rO   r,   r   Úfrom_observedrÛ   Úweightr   rq   ÚinspectÚ	signaturer„   rš   rÌ   rP   r›   rJ   rQ   rg   rh   ri   rj   rY   )r×   r±   rx   rÒ   Únew_modÚswappedÚqmodÚweight_post_processÚweight_qparamsÚsigÚpre_hook_fnr¤   ry   rZ   s                 r)   r"   r"   Ü  sY  € ð €GÝˆs�IÑÔñ *# 3¤;Ñ#:ØˆÝ'¨Ñ,Ô,Ð0KÐKÐKØ1Ý,¨SÑ1Ô1ôçŠm˜CÑ Ô ð ð ˆGˆGÝ)¨#Ñ.Ô.°'Ð9Ð9ØÕ7¸Ñ<Ô<Ô=ˆDÝ�t˜_Ñ-Ô-ð 3°$Ô2Dð 3Ø”{Ð.Ð.Ð.Ø&)¤k×&8Ò&8Ñ&:Ô&:Ð#Ø#Ð# C¤JÑ/Ô/Ð/Ý!0Ð1DÑ!EÔ!E�ØŸ/š/¨#¨~Ñ>Ô>��åÔ'¨¬Ñ8Ô8�Ø/°3´>ÐAÐAØ"ŸošoØÐ8Rð .ñ ô �G�Gð #Ÿošo¨cÑ2Ô2�GØˆGàð 	#à"Ô5×<Ò<Ñ>Ô>ð ?ð ?�Ø×1Ò1°+Ñ>Ô>Ð>Ð>ð Ô-×4Ò4Ñ6Ô6ð ;ð ;�ØÕ"8Ð8Ð8Ø×1Ò1°'Ñ:Ô:Ð:øõ +¨3Ñ/Ô/ˆGå�G‘” Ò!Ð!Ð!ØjÐahÐjÐjñ "Ô!Ð!å,/°©L¬L¸1Ò,<Ð,<•T�$˜w™-œ-Ñ(Ô(Ð(À$ˆFØð #Ø—
’
˜6Ñ"Ô"Ð"Ø€Nr(   c                 óÒ   — d„ }t          | d¦  «        r| j        | ||¦  «        dz   <   |                      ¦   «         D ](\  }}|r ||¦  «        |z   n|}t          |||¦  «         Œ)dS )a,  Traverse the modules and save all observers into dict.
    This is mainly used for quantization accuracy debug
    Args:
        mod: the top module we want to save all observers
        prefix: the prefix for the current module
        target_dict: the dictionary used to save all the observers
    c                 ó   — | dk    r| n| dz   S )Nr*   r-   r'   )r<   s    r)   Ú
get_prefixz&_get_observer_dict.<locals>.get_prefix!  s   € Ø 2š˜ˆvˆv¨6°C©<Ð7r(   rF   N)rO   rF   r6   Ú_get_observer_dict)r×   Útarget_dictr<   ré   r@   rA   rB   s          r)   rê   rê     s©   € ð8ð 8ð 8õ ˆsÐ-Ñ.Ô.ð (ð Ô'ð 	ØˆJ�vÑÔÐ!:Ñ:ñ	
ð ×)Ò)Ñ+Ô+ð >ð >‰ˆˆeØ5;ÐE˜
˜
 6Ñ*Ô*¨TÑ1Ð1ÀˆÝ˜5 +¨}Ñ=Ô=Ð=Ð=ð>ð >r(   )Nr*   N)NNr¦   )NNNN)FNNN)NF)NFTFNF)NFFNF)r*   )BrŽ   rÞ   rÄ   r’   r2   Útorch.ao.nn.quantizedr3   rk   Ú	quantizedrn   Útorch.nnÚtorch.ao.nn.intrinsicr   Útorch.ao.quantization.observerr   Útorch.ao.quantization.qconfigr   r   r   r   r	   r
   Ú+torch.ao.quantization.quantization_mappingsr   r   r   r   r   r   r   r   Útorch.ao.quantization.stubsr   r   Útorch.nn.utils.parametrizer   Úutilsr   r   Ú__all__Úis_activation_post_processrµ   ÚquantizableÚMultiheadAttentionr&   r   r8   r   rJ   rL   rT   rt   rg   r   r   r¨   r«   r   r³   r   r   r    r!   rÓ   r"   rê   r'   r(   r)   ú<module>rú      sá  ðà €€€Ø €€€Ø Ð Ð Ð Ø €€€à €€€Ø #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .Ø FÐ FÐ FÐ FÐ FÐ Fðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð BÐ AÐ AÐ AÐ AÐ AÐ AÐ AØ CÐ CÐ CÐ CÐ CÐ Cà MÐ MÐ MÐ MÐ MÐ MÐ MÐ Mðð ð €ð 9Ð ð
 	Œ�”Ô$Ø
Ô˜rœ~Ô@ð.ð
 	ŒÔ˜Rœ\Ô.Ø
ŒÔ)¨2¬<Ô+Jð2ð	ð 	Ð ð'ð 'ð 'ð ØØ#ð.ð .ð .ð .ðbð ð ð ð20ð 0ð 0ð
2ð 2ð 2ð
	Tð 	Tð 	Tð 	Tð "ØØØ $ðF/ð F/ð F/ð F/ðRð ð ðð ð ð: ØØ"&Ø#ðAð Að Að AðH!ð !ð !ð4,ð ,ð ,ð ð ð ð ð<  E¤K¸ÀuðWð Wð Wð Wðtð ð ð ð:ð ð ð ð2 ØØØØ#Ø$ð1ð 1ð 1ð 1ðl ØØØ#Ø$ð;ð ;ð ;ð ;ð~ KPð9ð 9ð 9ð 9ðx>ð >ð >ð >ð >ð >r(   