§
    ŠŠtj¤z  ã                   óv  — d dl Z 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'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„Z0d$d„Z1d„ Z2d„ Z3d%d„Z4	 	 	 	 d&d„Z5d„ Z6d„ Z7 ej8        e&¦  «        	 	 	 	 d'd„¦   «         Z9d„ Z:d„ Z; ej8        e&¦  «        d(d„¦   «         Z< ej8        e&¦  «        dej=        ddfd„¦   «         Z> ej8        e&¦  «        d(d„¦   «         Z? ej8        e&¦  «        d%d„¦   «         Z@ ej8        e&¦  «        	 	 	 	 	 	 d)d„¦   «         ZA	 	 	 	 	 d*d „ZB	 d%d!„ZCd+d"„ZDd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é   )ÚDEPRECATION_WARNINGÚ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© ó    ú\/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/ao/quantization/quantize.pyr   r   G   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*   r9   r9   L   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>   )r9   )r:   r;   r>   s      r*   r   r   }   sD   € ð  ÐØˆØ!Ð)Ø%'Ð"ÝØ�Ð9Sðñ ô ð ð ð r)   c                 ó,   — |                       |¦  «        S )z.Forward hook that calls observer on the output©Úactivation_post_process)ÚselfÚinputÚoutputs      r*   Ú_observer_forward_hookrK   –   s   € à×'Ò'¨Ñ/Ô/Ð/r)   c                 ó8   — |                       |d         ¦  «        S )z2Forward pre hook that calls observer on the outputr   rF   )rH   rI   s     r*   Ú_observer_forward_pre_hookrM   ›   s   € à×'Ò'¨¨a¬Ñ1Ô1Ð1r)   Fc                 ó¼   — t          | d¦  «        st          d¦  «        ‚|r|                      t          d¬¦  «         d S |                      t
          d¬¦  «         d S )NrG   zGExpect activation_post_process attribute already attached to the moduleT)Úprepend)ÚhasattrÚAssertionErrorÚregister_forward_pre_hookrM   Úregister_forward_hookrK   )r:   Úpre_hooks     r*   Ú&_register_activation_post_process_hookrU       su   € Ý�6Ð4Ñ5Ô5ð 
ÝØUñ
ô 
ð 	
ð ð KØ×(Ò(Õ)CÈTÐ(ÑRÔRÐRÐRÐRà×$Ò$Õ%;ÀTÐ$ÑJÔJÐJÐJÐJr)   c                 ó  ‡‡‡— |€t          ¦   «         }|€i }‰€et          | ¦  «        }t          |¦  «        dk    rt          d|› �¦  «        ‚t          |¦  «        dk    rt	          t          |¦  «        ¦  «        ndŠdd„Šd„ Šdˆˆˆfd„	}|                      ¦   «         D �]É\  }}t          |¦  «        t          j	        u rŒ"t          t          |¦  «        t          j        t          j        f¦  «        rR ‰|¦  «        rFt          |d¦  «        s t          d	t          |¦  «        › d
�¦  «        ‚ ‰|j        ‰¦  «        |_        Œ§t#          |t$          ¦  «        r ‰|¦  «        r ||¦  «         ŒÓ|�(t          |¦  «        |v r ‰|¦  «        r ||¦  «         Œýt'          |¦  «        rt)          |¦  «        }	 |||	¦  «         �Œ) ‰|¦  «        r‚t          |¦  «        |v rq|t          |¦  «                 }
|
                     |¦  «        }t-          | ||¦  «         t          |
t/          t1          ¦   «         ¦  «        ¦  «        s ||¦  «         �Œ¶t3          |||‰|¦  «         �ŒËt5          | ¦  «        r;t#          | t6          j        j        ¦  «        st          | ¦  «        |v r || ¦  «         t          | d¦  «        r=t#          | t6          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_processrY   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-   ©rP   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
        rG   ©rT   N)Ú
isinstancer   Ú
add_moduler-   rU   r   )r`   r\   r[   r]   ra   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)   rG   zfunctional class z- has no pre-defined `activation_post_process`Úweight_fake_quantrX   )r   Ú_get_unique_devices_ÚlenrQ   ÚnextÚiterr7   r   ÚnnÚDropoutÚ
issubclassÚnnqÚFloatFunctionalÚQFunctionalrP   r-   rG   rd   r   r   r   Ú
from_floatÚsetattrÚtupler   Ú_add_observer_r   r3   Ú
Sequential)r:   Úqconfig_propagation_listÚnon_leaf_module_listr[   Úcustom_module_class_mappingÚdevicesrf   rA   rB   r\   Úobserved_classÚobserved_childr]   ra   s      `        @@r*   ru   ru   «   sà  øøø€ ð,  Ð'Ý#GÑ#IÔ#IÐ à"Ð*Ø&(Ð#ð €~Ý& vÑ.Ô.ˆÝˆw‰<Œ<˜!ÒÐÝ ØnÐelÐnÐnñô ð õ ),¨G©¬°qÒ(8Ð(8••d˜7‘m”mÑ$Ô$Ð$¸dˆðð ð ð ð?ð ?ð ?ðð ð ð ð ð ð ð ð& ×,Ò,Ñ.Ô.ð 0ñ 0‰ˆˆeå'¨Ñ.Ô.µ"´*Ð<Ð<ØÝÝ(¨Ñ/Ô/µ#Ô2EÅsÄÐ1Wñ
ô 
ð ,	ð !Ð  Ñ'Ô'ð Ý˜uÐ&?Ñ@Ô@ð Ý(Ø~Õ,HÈÑ,OÔ,OÐ~Ð~Ð~ñô ð ð 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         j        d k    ¯|j         ’ŒS ©Úmeta©r[   r8   ©Ú.0Úps     r*   ú	<setcomp>z'_get_unique_devices_.<locals>.<setcomp>7  s(   € ÐMÐMÐM˜°Q´X´]ÀfÒ5LÐ5LˆAŒHÐ5LÐ5LÐ5Lr)   c                 ó<   — h | ]}|j         j        d k    ¯|j         ’ŒS r   r�   r‚   s     r*   r…   z'_get_unique_devices_.<locals>.<setcomp>7  s2   € ð Qð Qð QØ¨a¬h¬m¸vÒ.EÐ.EˆŒÐ.EÐ.EÐ.Er)   )Ú
parametersÚbuffers)r:   s    r*   rh   rh   6  sT   € ØMÐM˜f×/Ò/Ñ1Ô1ÐMÑMÔMð Qð QØ Ÿ.š.Ñ*Ô*ðQñ Qô Qñ ð 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   rP   r-   r   r7   r   Ú_modules)r:   rA   rB   s      r*   r   r   <  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d¬¦  «         t          | |||¬	¦  «         | S )
af  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é   )Ú
stacklevel)ry   )r3   Ú_CÚ_log_api_usage_oncer   r1   ÚcopyÚdeepcopyr   r   ÚanyÚmodulesÚwarningsÚwarnru   )ÚmodelÚinplaceÚ
allow_listÚobserver_non_leaf_module_listr>   ry   rw   s          r*   r   r   W  s  € õD 
„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 )NrG   Fc                 óþ   •— | r‰j         n‰j        }| rt          nt          }t	          ¦   «         }|                     ¦   «         D ]\  }}||u r|                     |¦  «         Œ|D ]}|                     |¦  «         Œd S rX   )Ú_forward_pre_hooksÚ_forward_hooksrM   rK   ÚsetÚitemsÚaddÚpop)rT   Úhook_mapÚobserver_hookÚhandle_ids_to_removeÚ	handle_idÚhook_fnr:   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)   Trc   ©F)rP   r   rG   Údelattr)r:   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_qconfigrP   r-   r­   )r:   rB   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š   )	r3   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²   )#r3   r‘   r’   Úqint8rl   ÚLinearr   ÚLSTMÚGRUÚLSTMCellÚRNNCellÚGRUCellÚfloat16r   Úquint8ÚEmbeddingBagr	   Ú	EmbeddingÚquint4x2r
   Ú
ValueErrorrd   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                 óf  — t           j                             d¦  «         | j        st	          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š   )r3   r‘   r’   ÚtrainingrQ   r   r“   r”   r   r"   r   r¡   Úvalues)r™   r¶   rš   s      r*   r    r    >  s³   € õ  
„H× Ò Ð!HÑIÔIÐIØŒ>ð RÝÐPÑQÔQÐQØ€Ý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²   )r3   r‘   r’   r“   r”   Útrainr    r"   )r™   r´   rµ   rš   s       r*   r!   r!   ]  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)r3   r‘   r’   r“   r”   Ú_convertr°   )r:   r¶   rš   rÏ   rÕ   rÖ   r×   s          r*   r"   r"   u  sy   € õJ 
„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   r1   r“   r”   r7   rd   r   r   rØ   r#   r¢   rŠ   )r:   r¶   rš   rÕ   rÖ   r×   ry   ÚreassignrA   Ú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                 óÄ  — | }t          | d¦  «        �rL| j        ��Dd}t          | ¦  «        |v r+|t          | ¦  «                                      | ¦  «        }d}nót          | ¦  «        |v râ|t          | ¦  «                 }t          |d¦  «        rl|j        re| j        €t          d¦  «        ‚| 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;t'          |¦  «        d
k    rt)          j        d¦  «        |v st          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_REFERENCEzAmodule qconfig must not be None when swapping to reference moduler×   rÚ   r   r�   r€   zOswap_module only works with cpu or single-device CUDA modules, but got devices r   )rP   r-   r   Úfrom_observedrà   rQ   Úweightr   rr   ÚinspectÚ	signaturer‡   rŸ   rÑ   rR   r    rK   rS   rh   ri   r3   r[   rj   rk   rZ   )rÜ   r¶   ry   r×   Únew_modÚswappedÚqmodÚweight_post_processÚweight_qparamsÚsigÚpre_hook_fnr©   rz   r[   s                 r*   r#   r#   è  s•  € ð €GÝˆs�IÑÔñ 1# 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‘” Ò!Ð!Ý˜‘L”L AÒ%Ð%­%¬,°vÑ*>Ô*>À'Ð*IÐ*Iå$ØoÐfmÐoÐoñô ð õ -0°©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_prefix4  s   € Ø 2š˜ˆvˆv¨6°C©<Ð7r)   rG   N)rP   rG   r7   Ú_get_observer_dict)rÜ   Útarget_dictr=   rî   rA   rB   rC   s          r*   rï   rï   +  s¤   € ð8ð 8ð 8õ ˆsÐ-Ñ.Ô.ð 
àÔ'ð 	�J�J˜vÑ&Ô&Ð)BÑBÑCð ×)Ò)Ñ+Ô+ð >ð >‰ˆˆeØ5;ÐE˜
˜
 6Ñ*Ô*¨TÑ1Ð1ÀˆÝ˜5 +¨}Ñ=Ô=Ð=Ð=ð>ð >r)   )Nr+   N)NNr«   )NNNN)FNNN)NF)NFTFNF)NFFNF)r+   )Er“   rã   rÉ   Útyping_extensionsr—   r3   Útorch.ao.nn.quantizedr4   rl   Ú	quantizedro   Ú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   r   Ú__all__Úis_activation_post_processrº   ÚquantizableÚMultiheadAttentionr'   r   r9   r   rK   rM   rU   ru   rh   r   Ú
deprecatedr   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ðð ð ð ð ð ð ð ð ð ðð ð €ð 9Ð ð
 	Œ�”Ô$Ø
Ô˜rœ~Ô@ð.ð
 	ŒÔ˜Rœ\Ô.Ø
ŒÔ)¨2¬<Ô+Jð2ð	ð 	Ð ð'ð 'ð 'ð ØØ#ð.ð .ð .ð .ðbð ð ð ð20ð 0ð 0ð
2ð 2ð 2ð
Kð Kð Kð Kð "ØØØ $ðH/ð H/ð H/ð H/ðVð ð ðð ð ð6 ÐÔÐ1Ñ2Ô2ð ØØ"&Ø#ð@ð @ð @ñ 3Ô2ð@ðF!ð !ð !ð4,ð ,ð ,ð  ÐÔÐ1Ñ2Ô2ðð ð ñ 3Ô2ðð: ÐÔÐ1Ñ2Ô2à E¤K¸ÀuðWð Wð Wñ 3Ô2ðWðt ÐÔÐ1Ñ2Ô2ðð ð ñ 3Ô2ðð< ÐÔÐ1Ñ2Ô2ðð ð ñ 3Ô2ðð. ÐÔÐ1Ñ2Ô2ð ØØØØ#Ø$ð1ð 1ð 1ñ 3Ô2ð1ðl ØØØ#Ø$ð;ð ;ð ;ð ;ð~ KPð@ð @ð @ð @ðF>ð >ð >ð >ð >ð >r)   