ó
    Eñi¤z  ã                   ó  • S SK r S SKrS SKrS SKrS SKrS SKrS SKJs  Js  J	r
  S SKJr  S SKJr  S SKJr  S SKJrJrJrJrJrJr  S SKJrJrJrJrJrJrJrJr  S SK J!r!J"r"  S SK#J$r$  SS	K%J&r&J'r'J(r(  / S
Qr)\r*\RV                  \RX                  RV                  \RZ                  \RX                  RZ                  0\RX                  RV                  \R                  RV                  \RX                  RZ                  \R                  RZ                  0S.r.S r/   S!S jr0S"S jr1S r2S r3S#S jr4    S$S jr5S r6S r7\Rp                  " \&5          S%S j5       r9S r:S r;\Rp                  " \&5      S&S j5       r<\Rp                  " \&5      S\Rz                  SS4S j5       r>\Rp                  " \&5      S&S j5       r?\Rp                  " \&5      S#S j5       r@\Rp                  " \&5            S'S j5       rA     S(S jrB S#S jrCS)S  jrDg)*é    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                  ó   • [         $ )z'Defines the default custom config dict.)Ú_DEFAULT_CUSTOM_CONFIG_DICT© ó    Ú[/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/ao/quantization/quantize.pyr   r   G   s   € ä&Ð&r)   c                 óà  • UR                  [        U 5      U5      nUR                  X55      n[        U SU5      n[        R                  R
                  R                  R                  XP5        [        XP5      nX`l        U R                  5        H]  u  pxU(       a  US-   U-   OUn	Ub8  XtR                  S/ 5      ;   a  M0  [        U5      UR                  S/ 5      ;   a  MQ  [        X�Xi5        M_     g)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   L   sÜ   € ð2 "×%Ñ%Ü$ VÓ,¨nó€Nð "×%Ñ% fÓ=€NÜ˜V Y°Ó?€Nä	‡H�H×Ñ×!Ñ!×7Ñ7¸ÔOä >¸~Ó VÐØ.„Nà×,Ñ,Ö.‰ˆÞ/5˜ ™ tÒ+¸4ˆà%Ñ-Ø×2Ñ2Ð3NÐPRÓSÕSÜ�E‹{Ø)×-Ñ-Ð.JÈBÓOõPô &ØÐ%>öò /r)   c                 ó,   • Uc  0 nUc  0 n[        XUS9  g)ac  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   }   s)   € ð  ÑØˆØ!Ñ)Ø%'Ð"ÜØÐ9Sór)   c                 ó$   • U R                  U5      $ )z.Forward hook that calls observer on the output©Úactivation_post_process)ÚselfÚinputÚoutputs      r*   Ú_observer_forward_hookrJ   –   s   € à×'Ñ'¨Ó/Ð/r)   c                 ó*   • U R                  US   5      $ )z2Forward pre hook that calls observer on the outputr   rE   )rG   rH   s     r*   Ú_observer_forward_pre_hookrL   ›   s   € à×'Ñ'¨¨a©Ó1Ð1r)   Fc                 óœ   • [        U S5      (       d  [        S5      eU(       a  U R                  [        SS9  g U R	                  [
        SS9  g )NrF   zGExpect activation_post_process attribute already attached to the moduleT)Úprepend)ÚhasattrÚAssertionErrorÚregister_forward_pre_hookrL   Úregister_forward_hookrJ   )r9   Úpre_hooks     r*   Ú&_register_activation_post_process_hookrT       sM   € Ü�6Ð4×5Ñ5ÜØUó
ð 	
ö Ø×(Ñ(Ô)CÈTÐ(ÒRà×$Ñ$Ô%;ÀTÐ$ÒJr)   c                 óÎ  ^^^• Uc
  [        5       nUc  0 nTcM  [        U 5      n[        U5      S:”  a  [        SU 35      e[        U5      S:”  a  [	        [        U5      5      OSmSS jmS mSUUU4S jjnU R                  5        GHµ  u  px[        U5      [        R                  L a  M$  [        [        U5      [        R                  [        R                  45      (       aR  T" U5      (       aC  [        US5      (       d  [        S	[        U5       S
35      eT" UR                  T5      Ul        M¬  M®  [#        U[$        5      (       a  T" U5      (       a
  U" U5        MÚ  MÜ  Ub*  [        U5      U;   a  T" U5      (       a  U" U5        GM  GM	  ['        U5      (       a  [)        U5      n	U" X‰5        GM/  T" U5      (       aj  [        U5      U;   a[  U[        U5         n
U
R+                  U5      n[-        XU5        [        U
[/        [1        5       5      5      (       d  U" U5        GM£  GM¦  [3        UUUTU5        GM¸     [5        U 5      (       a@  [#        U [6        R                  R8                  5      (       d  [        U 5      U;   a  U" U 5        [        U S5      (       aC  [#        U [6        R                  R8                  5      (       d  [        U 5      U;   a	  U" U 5        gggg)aG  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                 ób   • Uc  U R                  5       OU" 5       nUb  UR                  U5        U$ ©N)Ú
activationÚto)r,   ÚdeviceÚspecial_act_post_processrX   s       r*   Úget_activation_post_processÚ3_add_observer_.<locals>.get_activation_post_processÐ   s=   € ð (Ñ/ð ×ÑÔ á)Ó+ð 	ð
 ÑØ�M‰M˜&Ô!ØÐr)   c                 óD   • [        U S5      =(       a    U R                  S L$ )Nr,   ©rO   r,   )Úms    r*   Úneeds_observationÚ)_add_observer_.<locals>.needs_observationÚ   s   € Ü�q˜)Ó$×>¨¯©¸$Ð)>Ð>r)   c                 óÐ   >• T" U 5      (       aX  [        U [        5      (       dB  U R                  ST" U R                  TU5      5        [	        U [        U R                  5      S9  ggg)z]Adds an activation post process module and register
a pre or post hook that calls the module
rF   ©rS   N)Ú
isinstancer   Ú
add_moduler,   rT   r   )r`   r[   rZ   r\   ra   s     €€€r*   Úinsert_activation_post_processÚ6_add_observer_.<locals>.insert_activation_post_processÝ   sa   ø€ ñ
 ˜Q×Ñ¬
°1´k×(BÑ(Bà�L‰LØ)Ù+Ø—I‘I˜vÐ'?óôô 3ØÔ5°a·i±iÓ@óð )CÐr)   rF   zfunctional class z- has no pre-defined `activation_post_process`Úweight_fake_quantrW   )r   Ú_get_unique_devices_ÚlenrP   ÚnextÚiterr6   r   ÚnnÚDropoutÚ
issubclassÚnnqÚFloatFunctionalÚQFunctionalrO   r,   rF   re   r   r   r   Ú
from_floatÚsetattrÚtupler   Ú_add_observer_r   r2   Ú
Sequential)r9   Úqconfig_propagation_listÚnon_leaf_module_listrZ   Úcustom_module_class_mappingÚdevicesrg   r@   rA   r[   Úobserved_classÚobserved_childr\   ra   s      `        @@r*   rw   rw   «   s³  ú€ ð,  Ñ'Ü#GÓ#IÐ à"Ñ*Ø&(Ð#ð �~Ü& vÓ.ˆÜˆw‹<˜!ÓÜ ØdÐelÐdmÐnóð ô ),¨G«°qÓ(8””d˜7“mÔ$¸dˆôò?÷ñ ð& ×,Ñ,×.‰ˆä'¨Ó.´"·*±*Ò<ÙÜÜ(¨Ó/´#×2EÑ2EÄsÇÁÐ1W÷
ñ 
ñ ! ×'Ñ'Ü˜uÐ&?×@Ñ@Ü(Ø+Ô,HÈÓ,OÐ+PÐP}Ð~óð ñ 1LØ—M‘M 6ó1�Ö-ñ (ô ˜œ|×,Ñ,á  ×'Ñ'Ù.¨uÖ5ñ (ð !Ñ,Ü,¨UÓ3Ð7KÓKá  ×'Ñ'Ù.¨u×5ò (ä*¨5×1Ñ1Ü'DÀUÓ'KÐ$Ù*¨5×Ká˜e×$Ñ$Ü,¨UÓ3Ð7RÓRà8Ü,¨UÓ3ñˆNð ,×6Ñ6°uÓ=ˆNÜ�F .Ô1ô ˜n¬e´OÓ4EÓ.F×GÑGÙ.¨~×>ò Hô ØØ(Ø$ØØ+÷ñU /ôj 	2°&×9Ñ9Ü˜6¤5§8¡8×#6Ñ#6×7Ñ7Ü(¨Ó0Ð4LÓLá& vÔ.ô 	�Ð+×,Ñ,Ü˜6¤5§8¡8×#6Ñ#6×7Ñ7Ü(¨Ó0Ð4LÓLá& vÕ.ð Mð 8ð 	-r)   c                 ó0  • U R                  5        Vs1 s H*  oR                  R                  S:w  d  M  UR                  iM,     snU R                  5        Vs1 s H*  oR                  R                  S:w  d  M  UR                  iM,     sn-  $ s  snf s  snf )NÚmeta)Ú
parametersrZ   r7   Úbuffers)r9   Úps     r*   rj   rj   6  sy   € Ø$×/Ñ/Ô1ÓMÒ1˜·X±X·]±]ÀfÑ5L‹HˆA�HŒHÑ1ÑMØ Ÿ.™.Ô*óQÚ*�Q¯h©h¯m©m¸vÑ.E‹ˆ�ŒÑ*ñQñ ð ùÒMùò Qs   “B´BÁBÁ9Bc                 óâ   • [        U 5      (       a-  [        U S5      (       a  U R                  (       a  [        U 5      $ U R	                  5        H  u  p[        U5      U R                  U'   M     U $ )aO  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   <  s[   € ô  	2°&×9Ñ9Ü�F˜I×&Ñ&Ø�N�Nä˜FÓ#Ð#à×,Ñ,Ö.‰ˆÜ 1°%Ó 8ˆ�‰˜Óñ /à€Mr)   c                 óz  • [         R                  R                  S5        Uc
  [        5       nUR	                  S0 5      nU(       d  [
        R                  " U 5      n UnUc
  [        5       n[        U SS9  [        S U R                  5        5       5      (       d  [        R                  " SSS9  [        U UUUS	9  U $ )
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   ó`   #   • U  H$  n[        US 5      =(       a    UR                  v •  M&     g7f)r,   Nr_   )Ú.0r`   s     r*   Ú	<genexpr>Úprepare.<locals>.<genexpr>Š  s#   é € ÐLºO°qŒw�q˜)Ó$×2¨¯©Ô2ºOùs   ‚,.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)r{   )r2   Ú_CÚ_log_api_usage_oncer   r0   ÚcopyÚdeepcopyr   r   ÚanyÚmodulesÚwarningsÚwarnrw   )ÚmodelÚinplaceÚ
allow_listÚobserver_non_leaf_module_listr=   r{   ry   s          r*   r   r   W  s¾   € ôD 
‡H�H× Ñ Ð!DÔEØ!Ñ)Ü%CÓ%EÐ"Ø"<×"@Ñ"@Ø/°ó#Ðö Ü—’˜eÓ$ˆð  *ÐØÑÜ#GÓ#IÐ Ü�u¨4Ò0ô ÑL¸E¿M¹M¼OÓL×LÑLÜ�ŠðKð ò		
ô ØØ Ø%Ø$?ò	ð €Lr)   c                 óœ   ^ • [        T S5      (       a&  [        T R                  5      (       a  [        T S5        SU 4S jjnU" SS9  U" SS9  g )NrF   Fc                 ó  >• U (       a  TR                   OTR                  nU (       a  [        O[        n[	        5       nUR                  5        H  u  pEXRL d  M  UR                  U5        M     U H  nUR                  U5        M     g rW   )Ú_forward_pre_hooksÚ_forward_hooksrL   rJ   ÚsetÚitemsÚaddÚpop)rS   Úhook_mapÚobserver_hookÚhandle_ids_to_removeÚ	handle_idÚhook_fnr9   s         €r*   Úremove_hooksÚ5_remove_activation_post_process.<locals>.remove_hooks¤  sp   ø€ Þ08�6×,Ò,¸f×>SÑ>Sˆæ*2Õ&Ô8Nð 	ô  #›uÐØ"*§.¡.Ö"2ÑˆIØÔ'Ø$×(Ñ(¨Ö3ñ #3ó .ˆIØ�L‰L˜Ö#ò .r)   Trd   ©F)rO   r   rF   Údelattr)r9   r§   s   ` r*   Ú_remove_activation_post_processr«   ›  sM   ø€ ô ˆvÐ0×1Ñ1Ô6QØ×&Ñ&÷7ñ 7ô 	�Ð1Ô2÷
$ñ ˜$ÒÙ˜%Ó r)   c                 ó„   • U R                  5        H  n[        U5        M     [        U S5      (       a  U ?[	        U 5        g)zzClean 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®   µ  s9   € ð —‘Ö"ˆÜ˜Öñ #ô ˆv�y×!Ñ!ØˆNä# FÕ+r)   c                 óò   • [         R                  R                  S5        Uc
  [        5       nU(       d  [        R
                  " U 5      n U R                  5         [        U SS9  U" U /UQ76   [        XSS9  U $ )aS  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.quantizeT©r—   )	r2   rŽ   r�   r   r�   r‘   Úevalr   r"   )r–   Úrun_fnÚrun_argsÚmappingr—   s        r*   r   r   Å  sd   € ô$ 
‡H�H× Ñ Ð!EÔFØ�Ü:Ó<ˆÞÜ—’˜eÓ$ˆØ	‡J�J„LÜˆE˜4Ò Ù
ˆ5Ð�8ÓÜˆE DÒ)Ø€Lr)   c                 ó’  • [         R                  R                  S5        UGc™  U[         R                  :X  a|  [        R
                  [        [        R                  [        [        R                  [        [        R                  [        [        R                  [        [        R                  [        0nGOÄU[         R                  :X  a|  [        R
                  [        [        R                  [        [        R                  [        [        R                  [        [        R                  [        [        R                  [        0nGO4U[         R                  :X  a+  [        R                  [         [        R"                  [         0nOõU[         R$                  :X  a  [        R                  [&        0nOÊ[)        SU S35      e[+        U[,        5      (       a¦  U[         R                  L a  [        nOcU[         R                  L a  [        nOIU[         R                  L a  [         nO/U[         R$                  L a  [&        nO[/        S[1        U5      5      e[3        [5        U[6        R8                  " U5      5      5      nUc
  [;        5       nU(       d  [<        R>                  " U 5      n U RA                  5         [C        X5        [E        XSS9  U $ )a*  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_dynamicz5Don't know how to quantize with default settings for z. Provide full qconfig pleasez.Unknown dtype specified for quantize_dynamic: Tr°   )#r2   rŽ   r�   Úqint8rn   ÚLinearr   ÚLSTMÚGRUÚLSTMCellÚRNNCellÚGRUCellÚfloat16r   Úquint8ÚEmbeddingBagr	   Ú	EmbeddingÚquint4x2r
   Ú
ValueErrorre   rž   ÚRuntimeErrorÚstrÚdictÚzipÚ	itertoolsÚrepeatr   r�   r‘   r±   r   r"   )r–   Úqconfig_specÚdtyper´   r—   Údefault_qconfigs         r*   r   r   ã  sç  € ô@ 
‡H�H× Ñ Ð!MÔNØÒØ”E—K‘KÓä—	‘	Ô2Ü—‘Ô0Ü—‘Ô/Ü—‘Ô4Ü—
‘
Ô3Ü—
‘
Ô3ðŠLð ”e—m‘mÓ#ä—	‘	Ô2Ü—‘Ô0Ü—‘Ô/Ü—‘Ô4Ü—
‘
Ô3Ü—
‘
Ô3ðŠLð ”e—l‘lÓ"ä—‘Ô!BÜ—‘Ô?ð‰Lð ”e—n‘nÓ$ä—‘Ô!Gð‰Lô ØGÈÀwÐNkÐlóð ô 
�L¤#×	&Ñ	&Ø”E—K‘KÒÜ5‰OØ”e—m‘mÒ#Ü5‰OØ”e—l‘lÒ"Ü?‰OØ”e—n‘nÒ$ÜD‰OäØ@Ä#ÀeÃ*óð ô œC ¬i×.>Ò.>¸Ó.OÓPÓQˆà�Ü;Ó=ˆæÜ—’˜eÓ$ˆØ	‡J�J„LÜ�uÔ+ÜˆE DÒ)Ø€Lr)   c                 ó>  • [         R                  R                  S5        U R                  (       d  [	        S5      eUc
  [        5       nU(       d  [        R                  " U 5      n [        U SS9  [        XSSS9  [        U [        UR                  5       5      SS9  U $ )	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�   ÚtrainingrP   r   r�   r‘   r   r"   r   rž   Úvalues)r–   r´   r—   s      r*   r    r    >  s|   € ô  
‡H�H× Ñ Ð!HÔIØ�>�>ÜÐPÓQÐQØ�Ü1Ó3ˆæÜ—’˜eÓ$ˆä�u¨4Ò0ÜˆE¨DÀÒGÜˆE´°W·^±^Ó5EÓ1FÐPTÒUØ€Lr)   c                 óØ   • [         R                  R                  S5        U(       d  [        R                  " U 5      n U R                  5         [        U SS9  U" U /UQ76   [        U SS9  U $ )aC  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!   ]  sW   € ô 
‡H�H× Ñ Ð!IÔJÞÜ—’˜eÓ$ˆØ	‡K�K„MÜ�˜tÒ$Ù
ˆ5Ð�8ÓÜˆE˜4Ò Ø€Lr)   c           	      ó¾   • [         R                  R                  S5        U(       d  [        R                  " U 5      n [        U USUUUS9  U(       a  [        U 5        U $ )ad  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"   u  sU   € ôJ 
‡H�H× Ñ Ð!DÔEÞÜ—’˜vÓ&ˆÜØØØØ!Ø#=Ø#=òö Ü˜ÔØ€Mr)   c           
      ó¾  • Uc  U(       a
  [        5       O	[        5       nUc
  [        5       nUR                  S0 5      nU(       d  [        R
                  " U 5      n 0 nU R                  5        HE  u  p‰[        U	[        5      (       d  [        U	5      U;  a  [        U	USUUUS9  [        X‘Xe5      Xx'   MG     UR                  5        H  u  p«X°R                  U
'   M     U $ )aC  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

r%   T©rÕ   )r   r   r   r0   r�   r‘   r6   re   r   r   rÖ   r#   rŸ   r…   )r9   r´   r—   rÓ   rÔ   rÕ   r{   Úreassignr@   ÚmodÚkeyÚvalues               r*   rÖ   rÖ   ª  sè   € ð, �ö ô ?Ô@ä9Ó;ð 	ð
 "Ñ)Ü%CÓ%EÐ"Ø"<×"@Ñ"@Ø3°Ró#Ðö Ü—’˜vÓ&ˆØ€HØ×*Ñ*Ö,‰	ˆô ˜3¤×-Ñ-Ü,¨SÓ1Ð9TÓTäØØØØØ*Ø+Eòô %ØÐ5ó
ˆ‹ñ -ð& —n‘nÖ&‰
ˆØ$�‰˜Óñ 'ð €Mr)   c                 óš  • U n[        U S5      (       Ga6  U R                  Gb(  Sn[        U 5      U;   a   U[        U 5         R                  U 5      nSnOó[        U 5      U;   aä  U[        U 5         n[        US5      (       ar  UR                  (       aa  U R                  c  [        S5      eU R                  R                  5       nU" U R                  5        [        U5      nUR                  X5      nOQ[        R                  " UR                  5      n	SU	R                  ;   a  UR                  XS9nOUR                  U 5      nSnU(       aý  U R                  R                  5        H  n
UR                  U
5        M     U R                  R                  5        H  nU[         Ld  M  UR#                  U5        M!     [%        U 5      n['        U5      S	::  d7  ['        U5      S
:X  a  [(        R*                  " S5      U;   d  [        SU 35      e['        U5      S:”  a  [-        [/        U5      5      OSnU(       a  UR1                  U5        U$ )zí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   )rO   r,   r   Úfrom_observedrÞ   rP   Úweightr   rt   ÚinspectÚ	signaturer�   rœ   rÏ   rQ   r�   rJ   rR   rj   rk   r2   rZ   rl   rm   rY   )rÚ   r´   r{   rÕ   Únew_modÚswappedÚqmodÚweight_post_processÚweight_qparamsÚsigÚpre_hook_fnr¦   r|   rZ   s                 r*   r#   r#   è  s  € ð €GÜˆs�I×Ò 3§;¡;Ò#:ØˆÜ'¨Ó,Ð0KÓKØ1Ü,¨SÓ1ñç‰m˜CÓ ð ð ‰GÜ)¨#Ó.°'Ó9ØÔ7¸Ó<Ñ=ˆDÜ�t˜_×-Ñ-°$×2D×2DØ—;‘;Ñ&Ü(Ø[óð ð '*§k¡k×&8Ñ&8Ó&:Ð#Ù# C§J¡JÔ/Ü!0Ð1DÓ!E�ØŸ/™/¨#Ó>‘ä×'Ò'¨¯©Ó8�Ø/°3·>±>ÓAØ"Ÿo™oØð .ð ‘Gð #Ÿo™o¨cÓ2�GØˆGæà"×5Ñ5×<Ñ<Ö>�Ø×1Ñ1°+Ö>ñ  ?ð ×-Ñ-×4Ñ4Ö6�ØÔ"8Ô8Ø×1Ñ1°'Ö:ñ 7ô
 +¨3Ó/ˆGä�G“ Ó!Ü˜“L AÓ%¬%¯,ª,°vÓ*>À'Ó*Iä$ØeÐfmÐenÐoóð ô -0°«L¸1Ó,<”Tœ$˜w›-Ô(À$ˆFÞØ—
‘
˜6Ô"Ø€Nr)   c                 óÌ   • S n[        U S5      (       a  U R                  X" U5      S-   '   U R                  5        H%  u  pEU(       a  U" U5      U-   OUn[        XQU5        M'     g)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                 ó   • U S:X  a  U $ U S-   $ )NÚ r-   r(   )r<   s    r*   Ú
get_prefixÚ&_get_observer_dict.<locals>.get_prefix4  s   € Ø 2›ˆvÐ7¨6°C©<Ð7r)   rF   N)rO   rF   r6   Ú_get_observer_dict)rÚ   Útarget_dictr<   rí   r@   rA   rB   s          r*   rï   rï   +  si   € ò8ô ˆsÐ-×.Ñ.à×'Ñ'ð 	�J˜vÓ&Ð)BÑBÑCð ×)Ñ)Ö+‰ˆÞ5;™
 6Ó*¨TÒ1ÀˆÜ˜5¨}Ö=ò ,r)   )Nrì   N)NNr©   )NNNN)FNNN)NF)NFTFNF)NFFNF)rì   )Er�   rá   rÇ   Útyping_extensionsr”   r2   Útorch.ao.nn.quantizedr3   rn   Ú	quantizedrq   Ú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   r8   r   rJ   rL   rT   rw   rj   r   Ú
deprecatedr   r«   r®   r   r¶   r   r    r!   r"   rÖ   r#   rï   r(   r)   r*   Ú<module>r     sE  ðã Û Û Û Û ã ß #Ó #Ý Ý .Ý F÷÷ ÷	÷ 	ó 	÷ BÝ C÷ñ ò€ð 9Ð ð
 	�‰�—‘×$Ñ$Ø
×Ñ˜rŸ~™~×@Ñ@ð.ð
 	�‰×Ñ˜RŸ\™\×.Ñ.Ø
�‰×)Ñ)¨2¯<©<×+JÑ+Jð2ñ	Ð ò'ð ØØ#ô.ôbò20ò
2ô
Kð "ØØØ $ôH/òVòð6 ×ÒÐ1Ó2ð ØØ"&Ø#ó@ó 3ð@òF!ò4,ð  ×ÒÐ1Ó2óó 3ðð: ×ÒÐ1Ó2à E§K¡K¸ÀuóWó 3ðWðt ×ÒÐ1Ó2óó 3ðð< ×ÒÐ1Ó2óó 3ðð. ×ÒÐ1Ó2ð ØØØØ#Ø$ó1ó 3ð1ðl ØØØ#Ø$ô;ð~ KPô@õF>r)   