§
    Qznian  ã                   óB  — d dl Z d dlZd dlZd dlZd dlmZ d dlmc mc mZ	 d dl
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mZ d dlmZmZmZmZm Z m!Z! d dl"m#Z# d d	l$m%Z% d d	l$m%Z& g d
¢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„ Z/d„ Z0d%d„Z1d&d„Z2d„ Z3d„ Z4	 	 	 d'd„Z5d„ Z6d„ Z7d(d„Z8dej9        ddfd„Z:d(d„Z;d%d„Z<	 	 d)d„Z=	 	 d*d „Z>d!„ Z?d+d"„Z@dS ),é    N)Ú_FusedModule)Ú)get_default_dynamic_quant_module_mappingsÚ(get_default_static_quant_module_mappingsÚ2get_default_static_quant_reference_module_mappingsÚget_default_qat_module_mappingsÚ$get_default_qconfig_propagation_listÚno_observer_setÚ_has_special_act_post_processÚ_get_special_act_post_processé   )Úget_qparam_dictÚ)has_no_children_ignoring_parametrizations)ÚDeQuantStubÚQuantWrapper)Ú_add_module_to_qconfig_obs_ctrÚdefault_dynamic_qconfigÚfloat16_dynamic_qconfigÚ!float_qparams_weight_only_qconfigÚ&float_qparams_weight_only_qconfig_4bitÚ_activation_is_memoryless)Útype_before_parametrizations)Ú_is_activation_post_process)
Ú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/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/torch/ao/quantization/quantize.pyr   r   =   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   B   s#  € ð( "×%Ò%Õ&BÀ6Ñ&JÔ&JÈ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‰{Œ{Ð8×<Ò<Ð=YÐ[]Ñ^Ô^Ð^Ð^å%Ø�|Ð%>Àñô ð øð	ð 	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   j   s7   € ð  ÐØˆØ!Ð)Ø%'Ð"Ý˜f lÐOiÐjÑjÔjÐjÐjÐjr(   c                 ó,   — |                       |¦  «        S )z3Forward hook that calls observer on the output
    ©Úactivation_post_process)ÚselfÚinputÚoutputs      r)   Ú_observer_forward_hookrJ   €   s   € ð ×'Ò'¨Ñ/Ô/Ð/r(   c                 ó8   — |                       |d         ¦  «        S )z7Forward pre hook that calls observer on the output
    r   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~   € Ý�6Ð4Ñ5Ô5ð Rð RØQñRô RÐ5àð 
Ø×1Ò1Ý&°ð 2ñ 
ô 
ˆˆˆð ×-Ò-Ý"¨Dð .ñ 
ô 
ˆˆˆ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          j
        t          j        fv r" ‰|¦  «        r ‰|j        ‰¦  «        |_        Œlt          |t          ¦  «        r ‰|¦  «        r ||¦  «         Œ˜|�(t          |¦  «        |v r ‰|¦  «        r ||¦  «         ŒÂt!          |¦  «        rt#          |¦  «        }	 |||	¦  «         Œí ‰|¦  «        rzt          |¦  «        |v ri|t          |¦  «                                      |¦  «        }
t'          | ||
¦  «         |t          |¦  «                 t)          ¦   «         vr ||
¦  «         �Œrt+          |||‰|¦  «         �Œ‡t-          | ¦  «        r=t          | t.          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µ   sE   € Ø-EÐ-M�W×'Ò'Ñ)Ô)Ð)ÐSkÐSkÑSmÔSmˆ
ØÐØ�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 )zn Adds 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ÑÔð 	e­
°1µkÑ(BÔ(Bð 	eà�LŠLÐ2Ð4OÐ4OØ”	˜6Ð#;ñ5=ô 5=ñ >ô >ð >õ 3°1Õ?XÐYZÔYbÑ?cÔ?cÐdÑdÔdÐdÐdÐdð	eð 	eð 	eð 	er(   rW   )r   Ú_get_unique_devices_ÚlenÚnextÚiterr6   r   ÚnnÚDropoutÚnnqÚFloatFunctionalÚQFunctionalr,   rF   rc   r   r
   r   Ú
from_floatÚsetattrr	   Ú_add_observer_r   r2   Ú
Sequential)r9   Úqconfig_propagation_listÚnon_leaf_module_listrZ   Úcustom_module_class_mappingÚdevicesre   r@   rA   r[   Úobserved_childr\   r`   s      `       @@r)   rq   rq   —   s2  øøø€ ð   Ð'Ý#GÑ#IÔ#IÐ à"Ð*Ø&(Ð#ð €~Ý& vÑ.Ô.ˆÝ�7‰|Œ|˜qÒ Ð Ð ØjÐahÐjÐjñ !Ô Ð õ ),¨G©¬°qÒ(8Ð(8••d˜7‘m”mÑ$Ô$Ð$¸dˆðð ð ð ð?ð ?ð ?ðeð eð eð eð eð eð eð eð ×,Ò,Ñ.Ô.ð wñ w‰ˆˆeå'¨Ñ.Ô.µ2´:°,Ð>Ð>ØÝ)¨%Ñ0Ô0µSÔ5HÍ#Ì/Ð4ZÐZÐZØ Ð  Ñ'Ô'ð cØ0KÐ0KÈEÌMÐ[aÑ0bÔ0b�Ô-øÝ˜�|Ñ,Ô,ð 	wà Ð  Ñ'Ô'ð 6Ø.Ð.¨uÑ5Ô5Ð5øØ!Ð-Õ2NÈuÑ2UÔ2UÐYmÐ2mÐ2mØ Ð  Ñ'Ô'ð 6Ø.Ð.¨uÑ5Ô5Ð5øÝ*¨5Ñ1Ô1ð 	wÝ'DÀUÑ'KÔ'KÐ$Ø*Ð*¨5Ð2JÑKÔKÐKÐKØÐ˜uÑ%Ô%ð 	wÕ*FÀuÑ*MÔ*MÐQlÐ*lÐ*lØ8Õ9UÐV[Ñ9\Ô9\Ô]×hÒhÐinÑoÔoˆNÝ�F˜D .Ñ1Ô1Ð1ð +Õ+GÈÑ+NÔ+NÔOÕWfÑWhÔWhÐhÐhØ.Ð.¨~Ñ>Ô>Ð>ùå˜5Ð":Ð<PÐRXÐZuÑvÔvÐvÑvõ 1°Ñ8Ô8ð /ÅÈFÕTYÔT\ÔTgÑAhÔAhð /Ý'¨Ñ/Ô/Ð3KÐKÐKØ&Ð& vÑ.Ô.Ð.Ð.Ð.ð/ð /ð /ð /ØKÐKr(   c                 ó|   — d„ |                       ¦   «         D ¦   «         d„ |                      ¦   «         D ¦   «         z  S )Nc                 ó   — h | ]	}|j         ’Œ
S r'   ©rZ   ©Ú.0Úps     r)   ú	<setcomp>z'_get_unique_devices_.<locals>.<setcomp>í   s   € Ð2Ð2Ð2˜ˆAŒHÐ2Ð2Ð2r(   c                 ó   — h | ]	}|j         ’Œ
S r'   rz   r{   s     r)   r~   z'_get_unique_devices_.<locals>.<setcomp>î   s   € Ð,Ð,Ð,�aˆŒÐ,Ð,Ð,r(   )Ú
parametersÚbuffers)r9   s    r)   rf   rf   ì   sD   € Ø2Ð2˜f×/Ò/Ñ1Ô1Ð2Ñ2Ô2Ø,Ð,˜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   ð   sy   € õ 1°Ñ8Ô8ð $½WÀVÈYÑ=WÔ=Wð $Ð\bÔ\jð $Ý˜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>4  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)ru   )r2   Ú_CÚ_log_api_usage_oncer   r0   ÚcopyÚdeepcopyr   r   ÚanyÚmodulesÚwarningsÚwarnrq   )ÚmodelÚinplaceÚ
allow_listÚobserver_non_leaf_module_listr=   ru   rs   s          r)   r   r     s   € õ> 
„H× Ò Ð!DÑEÔEÐEØ!Ð)Ý%CÑ%EÔ%EÐ"Ø"<×"@Ò"@ÐAhÐjlÑ"mÔ"mÐàð %Ý”˜eÑ$Ô$ˆð  *ÐØÐÝ#GÑ#IÔ#IÐ Ý�u¨4Ð0Ñ0Ô0Ð0õ ÐLÐL¸E¿MºM¹O¼OÐLÑLÔLÑLÔLð VÝŒð Uñ 	Vô 	Vð 	Võ ØÐ'Ð)FØ$?ðAñ Aô Að Að €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_hooksF  sŸ   ø€ Ø08ÐS�6Ô,Ð,¸fÔ>SˆØ6>ÐZÕ2Ð2ÕDZˆÝ"™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Ý" 6Ô#AÑBÔBð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§   T  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   c  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‰   Úqint8rj   Ú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ÒÐå”	Õ3Ý”Õ1Ý”Õ0Ý”Õ5Ý”
Õ4Ý”
Õ4ðˆL‰Lð •e”mÒ#Ð#å”	Õ3Ý”Õ1Ý”Õ0Ý”Õ5Ý”
Õ4Ý”
Õ4ðˆL‰Lð •e”lÒ"Ð"å”Õ"CÝ”Õ@ðˆLˆLð •e”nÒ$Ð$å”Õ"HðˆLˆLõ ØlÈÐlÐlÐlñnô nð nå	�L¥#Ñ	&Ô	&ð RØ•E”KÐÐÝ5ˆOˆOØ•e”mÐ#Ð#Ý5ˆOˆOØ•e”lÐ"Ð"Ý?ˆOˆOØ•e”nÐ$Ð$ÝDˆOˆOåÐOÕQTÐUZÑQ[ÔQ[Ñ\Ô\Ð\Ý�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   Ô  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‚   € õ 
„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 )ag  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

    .. 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)r2   rˆ   r‰   rŠ   r‹   Ú_convertr§   )r9   r­   r‘   rÆ   rÌ   rÍ   s         r)   r!   r!     sr   € õ: 
„H× Ò Ð!DÑEÔEÐEØð 'Ý”˜vÑ&Ô&ˆÝØ� °LØ#=ð?ñ ?ô ?ð ?ð ð  Ý˜ÑÔÐØ€Mr(   c                 óØ  — |€|rt          ¦   «         nt          ¦   «         }|€t          ¦   «         }|                     di ¦  «        }|st	          j        | ¦  «        } i }|                      ¦   «         D ]R\  }}t          |t          ¦  «        s$t          |¦  «        |vrt          ||d||¦  «         t          |||¦  «        ||<   ŒS|                     ¦   «         D ]\  }	}
|
| j        |	<   Œ| S )a  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

    Nr$   T)r   r   r   r0   rŠ   r‹   r6   rc   r   r   rÎ   r"   r™   rƒ   )r9   r­   r‘   rÌ   rÍ   ru   Úreassignr@   ÚmodÚkeyÚvalues              r)   rÎ   rÎ   -  s!  € ð  €ØJVð <ÕDÑFÔFÐFÝ9Ñ;Ô;ð 	à!Ð)Ý%CÑ%EÔ%EÐ"Ø"<×"@Ò"@ÐAlÐnpÑ"qÔ"qÐàð 'Ý”˜vÑ&Ô&ˆØ€HØ×*Ò*Ñ,Ô,ð Pð P‰	ˆˆcõ ˜#�|Ñ,Ô,ð 	?Ý'¨Ñ,Ô,Ð4OÐOÐOÝ�S˜' 4Ø!Ð#=ñ?ô ?ð ?å$ S¨'Ð3NÑOÔOˆ�‰ˆà—n’nÑ&Ô&ð %ð %‰
ˆˆUØ$ˆŒ˜ÑÐà€Mr(   c                 óØ  — | }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          |¦  «        }|                     | |¦  «        }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   zOswap_module only works with cpu or single-device CUDA modules, but got devices r   )rO   r,   r   Úfrom_observedrÕ   Úweightr   ro   r–   rÈ   rP   r—   rJ   rQ   rf   rg   rh   ri   rY   )rÑ   r­   ru   Únew_modÚswappedÚqmodÚweight_post_processÚweight_qparamsÚpre_hook_fnr    rv   rZ   s               r)   r"   r"   U  s  € ð €GÝˆs�IÑÔñ "# 3¤;Ñ#:ØˆÝ'¨Ñ,Ô,Ð0KÐKÐKØ1Õ2NÈsÑ2SÔ2SÔT×bÒbÐcfÑgÔgˆGØˆGˆGÝ)¨#Ñ.Ô.°'Ð9Ð9ØÕ7¸Ñ<Ô<Ô=ˆDÝ�t˜_Ñ-Ô-ð /°$Ô2Dð /Ø”{Ð.Ð.Ð.Ø&)¤k×&8Ò&8Ñ&:Ô&:Ð#Ø#Ð# C¤JÑ/Ô/Ð/Ý!0Ð1DÑ!EÔ!E�ØŸ/š/¨#¨~Ñ>Ô>��àŸ/š/¨#Ñ.Ô.�ØˆGàð 	#à"Ô5×<Ò<Ñ>Ô>ð ?ð ?�Ø×1Ò1°+Ñ>Ô>Ð>Ð>ð Ô-×4Ò4Ñ6Ô6ð ;ð ;�ØÕ"8Ð8Ð8Ø×1Ò1°'Ñ:Ô:Ð:øõ +¨3Ñ/Ô/ˆGÝ�w‘<”< 1Ò$Ð$Ð$ØkÐbiÐkÐkñ %Ô$Ð$õ -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_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Ð-Ñ.Ô.ð bØFIÔFaˆ�J�J˜vÑ&Ô&Ð)BÑBÑCØ×)Ò)Ñ+Ô+ð >ð >‰ˆˆeØ5;ÐE˜
˜
 6Ñ*Ô*¨TÑ1Ð1ÀˆÝ˜5 +¨}Ñ=Ô=Ð=Ð=ð>ð >r(   )Nr*   N)NNr¢   )NNNN)FNNN)NF)NFTFN)NFFN)r*   )ArŠ   rÀ   rŽ   r2   Útorch.nnrj   Útorch.ao.nn.quantizedr3   Ú	quantizedrl   Útorch.ao.nn.intrinsicr   Ú+torch.ao.quantization.quantization_mappingsr   r   r   r   r   r	   r
   r   Úutilsr   r   Útorch.ao.quantization.stubsr   r   Útorch.ao.quantization.qconfigr   r   r   r   r   r   Útorch.nn.utils.parametrizer   Útorch.ao.quantization.observerr   Úis_activation_post_processÚ__all__r±   ÚquantizableÚMultiheadAttentionr&   r   r8   r   rJ   rL   rT   rq   rf   r   r   r¤   r§   r   r¯   r   r   r    r!   rÎ   r"   rá   r'   r(   r)   ú<module>rñ      s¹  ðØ €€€Ø Ð Ð Ð Ø €€€à €€€Ø Ð Ð Ð Ð Ð Ø #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ø .Ð .Ð .Ð .Ð .Ð .ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð NÐ MÐ MÐ MÐ MÐ MÐ MÐ MØ AÐ AÐ AÐ AÐ AÐ AÐ AÐ Aðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð DÐ CÐ CÐ CÐ CÐ CØ FÐ FÐ FÐ FÐ FÐ Fðð ð ð ð ð ðð ð €ð 	Œ�”Ô$Ø
Ô˜rœ~Ô@ð.ð
 	ŒÔ˜Rœ\Ô.Ø
ŒÔ)¨2¬<Ô+Jð2ð	ð 	Ð ð'ð 'ð 'ð Z^ð&ð &ð &ð &ðPkð kð kð kð,0ð 0ð 0ð
2ð 2ð 2ð


ð 

ð 

ð 

ðS/ð S/ð S/ð S/ðj-ð -ð -ðð ð ð, .2Ø*.Ø'+ð6ð 6ð 6ð 6ðp!ð !ð !ð,,ð ,ð ,ðð ð ð ð8 *.°U´[Ø!¨5ðSð Sð Sð Sðjð ð ð ð8ð ð ð ð. =AØ7;ð%ð %ð %ð %ðP ',Ø7;ð&ð &ð &ð &ðP/ð /ð /ðb>ð >ð >ð >ð >ð >r(   