§
    Ÿ²Åjç  ã                   ó  — d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZ d dl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 d dlmZ d dlmZmZ d dl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,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3 d dl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@ ddlAmAZA ddlBmCZCmDZDmEZEmFZFmGZG ddlHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQ g d¢ZRejS        ejT        ejU        ejV        ejW        ejX        ejY        ejZ        ej[        ej\        g
Z]ej^        ej_        j`        ja        jb        ejc        ej_        j`        jd        jb        iZedejf        j/        de3deegejh        ji        f         deege
egejf         f         deegef         ddfd„Zkdejf        j/        de3deegejh        ji        f         deege
egejf         f         deegef         ddfd „Zlde3d!e2ddfd"„Zmd#ejh        ji        denfd$„Zode1deegef         denfd%„Zpd&e/d'eddfd(„Zqd)ede3d!e2ddfd*„Zrd+e3deege
egejf         f         deegef         de
egegf         fd,„Zsde3d!e2ddfd-„Ztde3deegejh        ji        f         deejh        ji                 fd.„Zude3deegejh        ji        f         dejf        j/        d/end'ee         ddfd0„Zv	 	 d@de3deegejh        ji        f         d2e	eg         deegef         d'ed3end/enddfd4„Zwde3d5e3d!e2ddfd6„Zxde3d!e2deegejh        ji        f         d7ee!eeef         f         d8e	e3         ddfd9„Zy	 	 	 	 	 	 	 dAde/d/end;ee<eegef         df         d<end=end>eeeegef         df         d'eeeegef         df         d3ende/fd?„ZzdS )Bé    N)	ÚAnyÚCallableÚDictÚListÚOptionalÚSetÚTupleÚTypeÚUnion)Ú
CUSTOM_KEYÚNUMERIC_DEBUG_HANDLE_KEY)ÚBackendConfigÚget_native_backend_config)Úget_fused_module_classesÚget_pattern_to_dtype_configsÚget_qat_module_classesÚ-get_root_module_to_quantized_reference_module)Ú_is_activation_post_process)Úqconfig_equalsÚ
QConfigAny)ÚQConfigMapping)Ú	QuantType)Ú_remove_qconfig)ÚDeQuantStub)Ú_parent_nameÚ"activation_is_statically_quantizedÚget_qparam_dictÚget_swapped_custom_module_classÚis_per_channelÚto_underlying_dtypeÚweight_is_quantized)ÚGraphModule)ÚArgumentÚGraphÚNode)Útype_before_parametrizationsé   )Úquantized_decomposed_lib)Úconvert_eq_obsÚupdate_obs_for_equalization)ÚConvertCustomConfigÚPrepareCustomConfig)Ú_is_observed_moduleÚ_is_observed_standalone_module)Úlower_to_fbgemm)Ú)_compare_prepare_convert_qconfig_mappingsÚ_generate_node_name_to_qconfigÚ&_is_qconfig_supported_by_dtype_configsÚ_update_qconfig_for_fusionÚ_update_qconfig_for_qat)	Ú_get_moduleÚ_is_custom_module_lstmÚ_is_custom_module_mhaÚassert_and_get_unique_deviceÚcollect_producer_nodesÚcreate_getattr_from_valueÚget_custom_module_class_keysÚ graph_module_from_producer_nodesÚnode_arg_is_weight)ÚconvertÚconvert_custom_moduleÚconvert_standalone_moduleÚconvert_weighted_moduleÚmodelÚnodeÚmodulesÚnode_name_to_scopeÚnode_name_to_qconfigÚreturnc           	      óš  ‡‡‡*— | j         }|€J ‚t          ‰j        t          ¦  «        sJ ‚t	          ‰|‰¦  «        \  }}|‰j                 }t          |d¦  «        r|                     | ‰¦  «         dS t          ˆfd„t          ‰j	        ¦  «        t          ‰j
                             ¦   «         ¦  «        z   D ¦   «         ¦  «        }	|	st          |¦  «        sd|                     ‰¦  «        5  ‰                     ‰j	        d         ¦  «         |                     ‰¦  «         ddd¦  «         n# 1 swxY w Y   dS |j        }
d}t          |d¦  «        r|j        }d„ }|
t$          v �rÝ|�sÚd}d}|                     ¦   «         \  }}t)          |j        ¦  «        rqt-          |j        ¦  «        }t0          j        j        j        j        }t0          j        j        j        j        }|j        }|j        }tA          |
¦  «        }||||||d	œ}nyt0          j        j        j!        j        }t0          j        j        j"        j        }tG          |¦  «        }t-          |¦  «        }|j        }|j        }tA          |
¦  «        }|||||d
œ}|                     ‰¦  «        5  ‰j	        d         }|g}| $                    ¦   «         D ]h\  }}|dv rJt          |tF          t,          f¦  «        s.tK          | |||z   |z   |¦  «        }| &                    |¦  «         ŒS| &                    |¦  «         Œi| '                    ||tQ          |¦  «        i ¦  «        }|g|dd…         z   }| )                    |tQ          |¦  «         |||¦  «        ¦  «        Š*‰                     ‰*¦  «         tT          ‰j+        v rktX          ‰j+        tT                   v rRtT          ‰*j+        vri ‰*j+        tT          <   ‰j+        tT                   tX                   ‰*j+        tT                   tX          <   |                     ‰¦  «         ddd¦  «         dS # 1 swxY w Y   dS |�r'd}t0          j        j        j!        j-        }tA          |
¦  «        }|t0          j.        t0          j/        fv s
J d¦   «         ‚|j        }|j        }ta          |dt0          j1        ¦  «        }ta          |dt1          j2        t0          j3        ¦  «        j4        ¦  «        }||||dœ}tj          |         } |                     ‰¦  «        5  ‰j	        d         }‰j	        d         g}!| $                    ¦   «         D ]\  }}"|! &                    |"¦  «         Œ| '                    d| tQ          |!¦  «        i ¦  «        }#| '                    dtl          j7        |#dfi ¦  «        }$| '                    dtl          j7        |#dfi ¦  «        }%|d         }|d         }|d         }
|$|%|||
d
œ}ddd¦  «         n# 1 swxY w Y   |                     ‰¦  «        5  ‰j	        d         }|g}| $                    ¦   «         D ]6\  }}|dv r|}| &                    |¦  «         Œ!| &                    |¦  «         Œ7| '                    ||tQ          |¦  «        i ¦  «        }|g|dd…         z   }t0          j        j        j"        j-        }| )                    |tQ          |¦  «         |||¦  «        ¦  «        Š*ˆ*ˆfd„}&‰                     ‰*¦  «         tX          ‰j+        v r‰j+        tX                   ‰*j+        tX          <   |                     ‰¦  «         ddd¦  «         dS # 1 swxY w Y   dS |
t0          j8        k    rÊt0          j        j        j9        j:        }'|                     ‰¦  «        5  ‰j	        d         }| '                    d|'|t0          j8        fi ¦  «        }(| '                    d|'|(t0          j#        fi ¦  «        })‰                     |)¦  «         |                     ‰¦  «         ddd¦  «         dS # 1 swxY w Y   dS dS )au  Replace activation_post_process module call node with quantize and
    dequantize node working with decomposed Tensor

    Before:
    ... -> observer_0(x) -> ...
    After:
    ... -> torch.ops.quantized_decomposed.quantize_per_tensor(x, ...) ->
    torch.ops.quantized_decomposed.dequantize_per_tensor() -> ...

    or quantize_per_channel and dequantize_per_channel
    Nr>   c              3   ó8   •K  — | ]}t          |‰¦  «        V — Œd S ©N©Ú_has_none_qconfig©Ú.0ÚnrF   s     €úQC:\Projects\Historical\venv\Lib\site-packages\torch/ao/quantization/fx/convert.pyú	<genexpr>zM_replace_observer_with_quantize_dequantize_node_decomposed.<locals>.<genexpr>y   óB   øè è € ð ð àõ 	˜!Ð1Ñ2Ô2ðð ð ð ð ð ó    r   FÚ
is_dynamicc                 óh   — i }d|j         v r&|j         d         j        }|t          j        k    rd|i}|S )NÚvalÚ	out_dtype)ÚmetaÚdtypeÚtorchÚfloat32)Údequantize_opÚ
input_nodeÚdequantize_op_kwargsÚdq_out_dtypes       rP   Úadd_dequantize_op_kwargsz\_replace_observer_with_quantize_dequantize_node_decomposed.<locals>.add_dequantize_op_kwargs�   sB   € Ø!ÐØ�J”OÐ#Ð#Ø%œ?¨5Ô1Ô7ˆLØ�uœ}Ò,Ð,Ø(3°\Ð'BÐ$Ø#Ð#rS   Úcall_function)Ú_scale_Ú_zero_point_Ú_axis_Ú_quant_min_Ú_quant_max_Ú_dtype_)rb   rc   re   rf   rg   ©rb   rc   r'   zVonly uint8 and int8 are supported in reference flow for dynamic quantization right nowÚqschemeÚeps)re   rf   Ú_eps_rg   re   rf   rg   c                 ó   •— | ‰u r‰n| S rJ   © )ÚxÚdequantized_noderC   s    €€rP   Úremap_fnzL_replace_observer_with_quantize_dequantize_node_decomposed.<locals>.remap_fnN  s   ø€ Ø+,°¨9¨9Ð'Ð'¸!Ð;rS   );ÚgraphÚ
isinstanceÚtargetÚstrÚ_get_module_path_and_prefixÚhasattrr>   ÚallÚlistÚargsÚusersÚkeysÚ_is_conversion_supportedÚinserting_beforeÚreplace_all_uses_withÚ
erase_noderY   rT   ÚSUPPORTED_QDTYPESÚcalculate_qparamsr   ri   ÚintÚch_axisrZ   ÚopsÚquantized_decomposedÚquantize_per_channelÚdefaultÚdequantize_per_channelÚ	quant_minÚ	quant_maxr    Úquantize_per_tensorÚdequantize_per_tensorÚfloatÚitemsr:   ÚappendÚcreate_nodeÚtuplera   r   rX   r   ÚtensorÚuint8Úint8ÚgetattrÚper_tensor_affineÚfinfor[   rj   Ú_QSCHEME_TO_CHOOSE_QPARAMS_OPÚoperatorÚgetitemÚfloat16Úconvert_element_typeÚno_fuse)+rB   rC   rD   rE   rF   rq   Úmodule_pathÚprefixÚactivation_post_processÚskip_replacementrY   rT   r`   Ú	node_typeÚquantize_opÚscaleÚ
zero_pointrƒ   r\   r‰   rŠ   Údtype_Úqparamsr]   Úquantize_op_inputsÚkeyÚvalue_or_nodeÚqparam_nodeÚquantized_nodeÚ	dq_inputsri   rj   Úchoose_qparams_opÚchoose_qparams_op_inputsÚvalueÚchoose_qparams_nodeÚ
scale_nodeÚzero_point_noderp   Údtype_convert_opÚconvert_fp16_nodeÚconvert_fp32_nodero   s+    `  `                                     @rP   Ú:_replace_observer_with_quantize_dequantize_node_decomposedr·   [   sv
  øøø€ ð$ ŒK€EØÐÐÐÝ�d”k¥3Ñ'Ô'Ð'Ð'Ð'Ý5ØÐ Ð"6ñô Ñ€K�ð & d¤kÔ2ÐÝÐ&¨	Ñ2Ô2ð Ø×'Ò'¨¨tÑ4Ô4Ð4Øˆõ ð ð ð ð å�d”i‘”¥4¨¬
¯ªÑ(9Ô(9Ñ#:Ô#:Ñ:ðñ ô ñ ô Ðð ð Õ7Ð8OÑPÔPð ð ×#Ò# DÑ)Ô)ð 	#ð 	#Ø×&Ò& t¤y°¤|Ñ4Ô4Ð4Ø×Ò˜TÑ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	ˆð $Ô)€Eà€JÝÐ&¨Ñ5Ô5ð 8Ø,Ô7ˆ
ð$ð $ð $ð Õ!Ð!Ñ!¨:Ñ!ð $ˆ	Ø*.ˆØ3×EÒEÑGÔGÑˆˆzÝÐ1Ô9Ñ:Ô:ð 	ÝÐ1Ô9Ñ:Ô:ˆGÝœ)Ô8ÔMÔUˆKå”	Ô.ÔEÔMð ð 0Ô9ˆIØ/Ô9ˆIÝ(¨Ñ/Ô/ˆFà Ø *Ø!Ø(Ø(Ø!ðð ˆGˆGõ  œ)Ô8ÔLÔTˆKÝ!œIÔ:ÔPÔXˆMÝ˜%‘L”LˆEÝ˜Z™œˆJØ/Ô9ˆIØ/Ô9ˆIÝ(¨Ñ/Ô/ˆFà Ø *Ø(Ø(Ø!ðð ˆGð ×#Ò# DÑ)Ô)ð /	#ð /	#Øœ 1œˆJØ", ÐØ&-§m¢m¡o¤oð =ð =Ñ"��]ð Ð5Ð5Ð5Ý" =µ5½#°,Ñ?Ô?ð 6õ #<Ø˜u k°FÑ&:¸SÑ&@À-ñ#ô #�Kð '×-Ò-¨kÑ:Ô:Ð:Ð:ð '×-Ò-¨mÑ<Ô<Ð<Ð<à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð (Ð(Ð+=¸a¸b¸bÔ+AÑAˆIØ$×2Ò2ØÝ�iÑ Ô Ø(Ð(¨¸
ÑCÔCñ ô  Ðð ×&Ò&Ð'7Ñ8Ô8Ð8õ ˜dœiÐ'Ð'Ý,°´	½*Ô0EÐEÐEåÐ%5Ô%:Ð:Ð:Ø8:Ð$Ô)­*Ñ5ØNRÌiÝôOå*ôO,Ð Ô%¥jÔ1Õ2JÑKð ×Ò˜TÑ"Ô"Ð"ð_/	#ð /	#ð /	#ñ /	#ô /	#ð /	#ð /	#ð /	#ð /	#ð /	#ð /	#ð /	#øøøð /	#ð /	#ð /	#ð /	#ð /	#ð /	#ð` 
ñ q#ð $ˆ	Ý”iÔ4ÔHÔOˆõ
 % UÑ+Ô+ˆØ�%œ+¥u¤zÐ2Ð2Ð2Ð2ð-ñ 3Ô2Ð2ð ,Ô5ˆ	Ø+Ô5ˆ	ÝÐ1°9½eÔ>UÑVÔVˆÝÐ-¨uµe´kÅ%Ä-Ñ6PÔ6PÔ6TÑUÔUˆð
 %Ø$ØØð	
ð 
ˆõ :¸'ÔBÐà×#Ò# DÑ)Ô)ð 	ð 	Øœ 1œˆJØ(,¬	°!¬ ~Ð$Ø%Ÿmšm™oœoð 7ð 7‘
��Uð )×/Ò/°Ñ6Ô6Ð6Ð6Ø"'×"3Ò"3ØÐ!2µEÐ:RÑ4SÔ4SÐUWñ#ô #Ðð ×*Ò*Ø¥Ô!1Ð4GÈÐ3KÈRñô ˆJð $×/Ò/Ø¥Ô!1Ð4GÈÐ3KÈRñô ˆOð   Ô.ˆIØ Ô.ˆIØ˜IÔ&ˆEà%Ø /Ø(Ø(Ø ðð ˆGð)	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð: ×#Ò# DÑ)Ô)ð (	#ð (	#Øœ 1œˆJØ", ÐØ&-§m¢m¡o¤oð =ð =Ñ"��]ð Ð5Ð5Ð5ð #0�KØ&×-Ò-¨kÑ:Ô:Ð:Ð:ð '×-Ò-¨mÑ<Ô<Ð<Ð<à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð (Ð(Ð+=¸a¸b¸bÔ+AÑAˆIõ "œIÔ:ÔPÔWˆMØ$×2Ò2ØÝ�iÑ Ô Ø(Ð(¨¸
ÑCÔCñ ô  Ðð<ð <ð <ð <ð <ð <ð ×&Ò&Ð'7Ñ8Ô8Ð8å'¨4¬9Ð4Ð4ØBFÄ)Ý,ôCÐ Ô%Õ&>Ñ?ð ×Ò˜TÑ"Ô"Ð"ðQ(	#ð (	#ð (	#ñ (	#ô (	#ð (	#ð (	#ð (	#ð (	#ð (	#ð (	#ð (	#øøøð (	#ð (	#ð (	#ð (	#ð (	#ð (	#ðR 
•%”-Ò	Ð	å œ9Ô9ÔNÔVÐØ×#Ò# DÑ)Ô)ð 		#ð 		#Øœ 1œˆJØ %× 1Ò 1ØÐ!1°JÅÄÐ3NÐPRñ!ô !Ðð !&× 1Ò 1ØÐ!1Ð4EÅuÄ{Ð3SÐUWñ!ô !Ðð ×&Ò&Ð'8Ñ9Ô9Ð9Ø×Ò˜TÑ"Ô"Ð"ð		#ð 		#ð 		#ñ 		#ô 		#ð 		#ð 		#ð 		#ð 		#ð 		#ð 		#ð 		#øøøð 		#ð 		#ð 		#ð 		#ð 		#ð 		#ð 
 Ð	s^   Ã.6D0Ä0D4Ä7D4ÊFP:Ð:P>ÑP>ÔCXØXØ	XØ"D5]$Ý$](Ý+](Þ1B `>à>aáac                 óê	  ‡— |€J ‚t          |j        t          ¦  «        sJ ‚| j        }t	          ||‰¦  «        \  }}||j                 }t          ˆfd„t          |j        ¦  «        t          |j         	                    ¦   «         ¦  «        z   D ¦   «         ¦  «        }	|	st          |¦  «        sd|                     |¦  «        5  |                     |j        d         ¦  «         |                     |¦  «         ddd¦  «         n# 1 swxY w Y   dS |j        }
d}t          |d¦  «        r|j        }|
t"          j        t"          j        t"          j        t"          j        t"          j        fv �r’|�s�d}d}|                     ¦   «         \  }}t1          |j        ¦  «        r(t5          |j        ¦  «        }||||
dœ}t"          j        }n0t;          |¦  «        }t5          |¦  «        }|||
dœ}t"          j        }|                     |¦  «        5  |j        d         }|g}|                     ¦   «         D ]L\  }}|d	v r.tA          | |||z   |z   |¦  «        }| !                    |¦  «         Œ7| !                    |¦  «         ŒM| "                    ||tG          |¦  «        i ¦  «        }| $                    d
|f¬¦  «        }|                     |¦  «         |                     |¦  «         ddd¦  «         dS # 1 swxY w Y   dS |�rd}t"          j%        }t"          j&        j'        j(        dv }|
|dœ}|                     |¦  «        5  |j        d         }|g}|                     ¦   «         D ]\  }}| !                    |¦  «         Œ| "                    ||tG          |¦  «        i ¦  «        }| $                    d
|f¬¦  «        }|                     |¦  «         |                     |¦  «         ddd¦  «         dS # 1 swxY w Y   dS |
t"          j)        k    rÞd}d}d|
i}|                     |¦  «        5  |j        d         }|g}|                     ¦   «         D ]\  }}| !                    |¦  «         Œ| "                    ||tG          |¦  «        i ¦  «        }| $                    d
|f¬¦  «        }|                     |¦  «         |                     |¦  «         ddd¦  «         dS # 1 swxY w Y   dS dS )zÙReplace activation_post_process module call node with quantize and
    dequantize node

    Before:
    ... -> observer_0(x) -> ...
    After:
    ... -> torch.quantize_per_tensor(x, ...) -> x.dequantize() -> ...
    Nc              3   ó8   •K  — | ]}t          |‰¦  «        V — Œd S rJ   rK   rM   s     €rP   rQ   zB_replace_observer_with_quantize_dequantize_node.<locals>.<genexpr>‚  rR   rS   r   FrT   ra   )rb   rc   rd   rg   )rb   rc   rg   rh   Ú
dequantize)ry   )ÚfbgemmÚx86)rg   Ú_reduce_range_Úcall_methodÚtorg   )*rr   rs   rt   rq   ru   rw   rx   ry   rz   r{   r|   r}   r~   r   rY   rv   rT   rZ   Úquint8Úqint8Úqint32Úfloat8_e5m2Úfloat8_e4m3fnr�   r   ri   r‚   rƒ   r†   r�   r‹   rŽ   r:   r�   r�   r‘   r¾   Úquantize_per_tensor_dynamicÚbackendsÚ	quantizedÚenginer›   )rB   rC   rD   rE   rF   rq   rž   rŸ   r    r¡   rY   rT   r¢   r£   r¤   r¥   rƒ   r§   r]   r¨   r©   rª   r«   r¬   ro   Úreduce_ranger°   s       `                      rP   Ú/_replace_observer_with_quantize_dequantize_noderÊ   j  s£  ø€ ð ÐÐÐÝ�d”k¥3Ñ'Ô'Ð'Ð'Ð'ØŒK€EÝ5ØÐ Ð"6ñô Ñ€K�ð & d¤kÔ2Ðõ ð ð ð ð å�d”i‘”¥4¨¬
¯ªÑ(9Ô(9Ñ#:Ô#:Ñ:ðñ ô ñ ô Ðð ð Õ7Ð8OÑPÔPð ð ×#Ò# DÑ)Ô)ð 	#ð 	#Ø×&Ò& t¤y°¤|Ñ4Ô4Ð4Ø×Ò˜TÑ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	ˆð $Ô)€Eà€JÝÐ&¨Ñ5Ô5ð 8Ø,Ô7ˆ
àÝŒÝŒÝŒÝÔÝÔðð ñ ð ñð $ˆ	Ø*.ˆØ3×EÒEÑGÔGÑˆˆzÝÐ1Ô9Ñ:Ô:ð 	4ÝÐ1Ô9Ñ:Ô:ˆGà Ø *Ø!Ø ð	ð ˆGõ  Ô4ˆKˆKå˜%‘L”LˆEÝ˜Z™œˆJØ"'¸ÐPUÐVÐVˆGÝÔ3ˆKð ×#Ò# DÑ)Ô)ð 	#ð 	#Øœ 1œˆJØ", ÐØ&-§m¢m¡o¤oð =ð =Ñ"��]ð Ð5Ð5Ð5õ #<Ø˜u k°FÑ&:¸SÑ&@À-ñ#ô #�Kð '×-Ò-¨kÑ:Ô:Ð:Ð:ð '×-Ò-¨mÑ<Ô<Ð<Ð<à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð  %×0Ò0°À^ÐDUÐ0ÑVÔVÐØ×&Ò&Ð'7Ñ8Ô8Ð8Ø×Ò˜TÑ"Ô"Ð"ð-	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð. 
ñ '#ð $ˆ	ÝÔ7ˆõ ”~Ô/Ô6Ð:KÐKˆØ#°|ÐDÐDˆà×#Ò# DÑ)Ô)ð 	#ð 	#Øœ 1œˆJØ", ÐØ%Ÿmšm™oœoð 1ð 1‘
��UØ"×)Ò)¨%Ñ0Ô0Ð0Ð0à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð  %×0Ò0°À^ÐDUÐ0ÑVÔVÐØ×&Ò&Ð'7Ñ8Ô8Ð8Ø×Ò˜TÑ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 
•%”-Ò	Ð	Ø!ˆ	ØˆØ˜eÐ$ˆØ×#Ò# DÑ)Ô)ð 	#ð 	#Øœ 1œˆJØ", ÐØ%Ÿmšm™oœoð 1ð 1‘
��Uð #×)Ò)¨%Ñ0Ô0Ð0Ð0à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð  %×0Ò0°À^ÐDUÐ0ÑVÔVÐØ×&Ò&Ð'7Ñ8Ô8Ð8Ø×Ò˜TÑ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð	 
 Ð	sK   Ã6DÄD
ÄD
ÈCK4Ë4K8Ë;K8ÍB'O8Ï8O<Ï?O<Ð2B'S&Ó&S*Ó-S*rq   c                 óØ   — | j         d         }t          |t          ¦  «        sJ d|› �¦   «         ‚|                      |¦  «         |                     | ¦  «         t          ||¦  «         d S )Nr   z@Expecting the for call custom module node to be a Node, but got )ry   rr   r%   r~   r   Ú_insert_dequantize_node)rC   rq   Úcall_custom_module_nodes      rP   Ú6_replace_observer_or_dequant_stub_with_dequantize_noderÎ   ÿ  s�   € ð #œi¨œlÐÝØ¥ñô ð dð dàcÐJaÐcÐcñdô dð ð 	×ÒÐ6Ñ7Ô7Ð7Ø	×Ò�TÑÔÐÝÐ3°UÑ;Ô;Ð;Ð;Ð;rS   r    c                 ó~   — | j         }d}t          | d¦  «        r| j        }|t          v r| p|p|t          j        k    S )NFrT   )rY   rv   rT   r€   rZ   r›   )r    rY   rT   s      rP   r|   r|     sX   € Ø#Ô)€Eà€JÝÐ&¨Ñ5Ô5ð 8Ø,Ô7ˆ
ð 
Õ#Ð	#Ð	8¨Z¨ð 	"Øð	"à•E”MÒ!ðrS   c                 ó\   — t          | t          ¦  «        o| j        |v o|| j                 du S )z\Check if a node has a qconfig of None, i.e. user requested to not quantize
    the node
    N)rr   r%   Úname)rC   rF   s     rP   rL   rL     s:   € õ 	�4�ÑÔð 	4ØŒIÐ-Ð-ð	4à  ¤Ô+¨tÐ3ðrS   ÚobservedÚbackend_configc                 óÌ   — | j         j        D ]V}|j        dk    rŒ|j        D ]@}|r<t	          ||¦  «        r,t          |¦  «        }|€Œ&t          | |¦  «        } |¦   «          ŒAŒWdS )zùExtract the subgraph that produces the weight for dynamic quant
    or weight only quant node and run the subgraph to observe the weight.
    Note that the observers of dynamic quant or weight only quant ops are
    run during the convert step.
    ra   N)rq   ÚnodesÚopry   r=   r9   r<   )rÒ   rÓ   rC   Únode_argÚweight_observer_nodesÚweight_observer_modules         rP   Ú_run_weight_observersrÚ   &  s¡   € ð ”Ô$ð )ð )ˆØŒ7�oÒ%Ð%ØØœ	ð 
	)ð 
	)ˆHàð )Õ.¨t°XÑ>Ô>ð )Ý(>¸xÑ(HÔ(HÐ%Ø(Ð0ØÝ)IØÐ3ñ*ô *Ð&ð 'Ð&Ñ(Ô(Ð(øð
	)ð)ð )rS   Úargc                 óÔ  — t          | t          ¦  «        r;| j        dk    r0| j        dk    r%| j        d         }|                     | |¦  «         dS t          | t          t          f¦  «        r| D ]}t          |||¦  «         ŒdS t          | t          ¦  «        r*|  
                    ¦   «         D ]}t          |||¦  «         ŒdS t          j        dt          | ¦  «        › �¦  «         dS )zIf the arg is a dequantize Node, or a list/tuple/dict of dequantize Node,
    we'll recursively remove the dequantize Node
    r¾   rº   r   z6Unsupported node type in recursive remove dequantize: N)rr   r%   rÖ   rs   ry   Úreplace_input_withrx   r‘   Ú"_maybe_recursive_remove_dequantizeÚdictÚvaluesÚwarningsÚwarnÚtype)rÛ   rC   rq   Úquantize_nodeÚarg_elements        rP   rÞ   rÞ   <  s  € õ �#•tÑÔð 
 ¤¨=Ò!8Ð!8¸S¼ZÈ<Ò=WÐ=WØœ œˆð 	×Ò  ]Ñ3Ô3Ð3Ð3Ð3Ý	�C�$¥˜Ñ	'Ô	'ð 	
Øð 	Ið 	IˆKÝ.¨{¸DÀ%ÑHÔHÐHÐHð	Ið 	Iå	�C�Ñ	Ô	ð 
ØŸ:š:™<œ<ð 	Ið 	IˆKÝ.¨{¸DÀ%ÑHÔHÐHÐHð	Ið 	Iõ 	ŒØPÅTÈ#ÁYÄYÐPÐPñ	
ô 	
ð 	
ð 	
ð 	
rS   Úobs_nodec                 óœ  — | j         d         }t          |t          ¦  «        sJ d|› �¦   «         ‚|j        |v r||j                 du nd}|r\t	          | j        ¦  «        }|r|d         nd}d}|D ]0}|j        dk    r#|j        t          j	        j
        j        k    r|} nŒ1|r|}d}	n|}d}	|r|j        |v r||j                 \  }
}nd}
|
|	fS )ai  Given and observer node, get the `Scope` or the fully qualified name for
    the submodule containing the observed node, also return a prefix of "_input"
    when the observed node is an input of a F.linear op, and not the output of another
    quantized op.
    TODO: this logic is hacky, we should think about how to remove it or make it more
    general
    r   z.Expecting observed node to be a Node, but got Nra   Ú_inputÚ )ry   rr   r%   rÑ   rx   rz   rÖ   rs   rZ   ÚnnÚ
functionalÚlinear)ræ   rE   rF   Úobserved_nodeÚis_input_observer_onlyrz   Úfirst_linear_use_or_first_useÚlinear_noderO   rŸ   rž   Ú_s               rP   ru   ru   Q  sI  € ð ”M !Ô$€Mõ
 Ø•tñô ð Hð HàG¸ÐGÐGñHô Hð ð
 ÔÐ!5Ð5Ð5ð 	˜]Ô/Ô0°DÐ8Ð8àð ð
 ð õ �X”^Ñ$Ô$ˆØ49Ð(C¨¨a¬¨¸tÐ%ØˆØð 	ð 	ˆAØŒt�Ò&Ð&¨1¬8µu´xÔ7JÔ7QÒ+QÐ+QØ�Ø�øØð 	8Ø,7Ð)Øˆˆð )6Ð%Øˆð 	&ð	à)Ô.Ð2DÐDÐDà+Ð,IÔ,NÔO‰ˆ�Q�Qð
 ˆØ˜ÐÐrS   c                 óô   — |                      | ¦  «        5  |                     d| f¦  «        }t          | j        ¦  «        D ]}||ur|                     | |¦  «         Œ	 ddd¦  «         dS # 1 swxY w Y   dS )z-Inserts dequantize node for `node` in `graph`rº   N)Úinserting_afterr¾   rß   rz   rÝ   )rC   rq   Údequantize_nodeÚ	user_nodes       rP   rÌ   rÌ   Š  sè   € à	×	Ò	˜tÑ	$Ô	$ð Dð DØ×+Ò+¨L¸4¸'ÑBÔBˆÝ˜dœjÑ)Ô)ð 	Dð 	DˆIØ Ð/Ð/Ø×,Ò,¨T°?ÑCÔCÐCøð	DðDð Dð Dñ Dô Dð Dð Dð Dð Dð Dð Dð Døøøð Dð Dð Dð Dð Dð Ds   –A	A-Á-A1Á4A1c                 ó®   — | j                              ¦   «         D ]:}|j        dk    r-|t          |j        ¦  «                 }t          |¦  «        r|c S Œ;dS )z\
    If the node is observed, return the observer
    instance. Otherwise, return None.
    Úcall_moduleN)rz   r{   rÖ   rt   rs   r   )rC   rD   Úmaybe_obs_nodeÚ	maybe_obss       rP   Ú_maybe_get_observer_for_noderú   “  sd   € ð œ*Ÿ/š/Ñ+Ô+ð !ð !ˆØÔ Ò-Ð-Ø¥ NÔ$9Ñ :Ô :Ô;ˆIÝ*¨9Ñ5Ô5ð !Ø Ð Ð Ð øØˆ4rS   Úis_referencec                 ój  — |rt           j        j        j        j        }nt           j        j        j        j        }|t          | j        ¦  «                 }|j        d         j	        }t          | j        ¦  «        }t          t          |¦  «        ¦  «        D ]y}	|	|v rs||	         }
|
j        dk    r`|
j        dk    rU|
j        d         }|                      |
|¦  «         t          |
j        ¦  «        dk    r|j                             |
¦  «         Œz|j        d         j        }t          |¦  «        dk    r,|d         dk    s
J d¦   «         ‚	 t)          | |j        ¦  «          |||¬¦  «        }t+          | j        ¦  «        \  }}t-          ||         ||¦  «         ||t          | j        ¦  «        <   dS )a¨  Converts a observed standalone module to a quantized standalone module by calling
    the fx convert api, currently using the same `is_reference` flag as parent, but we may
    changing this behavior in the future (e.g. separating quantization and lowering for
    standalone module as well)

    Args:
      - node: The call_module node of the observed standalone module
      - modules: named_module of original model
      - model: original model
      - is_reference: a flag from parent provided by user to decide if we want to
        produce a reference model or a fbgemm/qnnpack model
      - backend_config: backend configuration of the target backend of quantization
    Ú_observed_graph_module_attrsr¾   rº   r   zCurrently only quantized)rÓ   N)rZ   ÚaoÚquantizationÚquantize_fxÚconvert_to_reference_fxÚ
convert_fxrt   rs   rX   Ú&standalone_module_input_quantized_idxsrx   ry   ÚrangeÚlenrÖ   rÝ   rz   rq   r   Ú'standalone_module_output_quantized_idxsrÌ   r   Úsetattr)rC   rD   rB   rû   rÓ   Ú
convert_fnÚobserved_standalone_moduleÚsm_input_quantized_idxsry   ÚidxrÛ   rä   Úsm_output_quantized_idxsÚquantized_standalone_moduleÚparent_namerÑ   s                   rP   r@   r@   ¢  sÉ  € ð* ð BÝ”XÔ*Ô6ÔNˆ
ˆ
å”XÔ*Ô6ÔAˆ
ð /6µc¸$¼+Ñ6FÔ6FÔ.GÐØ8Ô=Ø&ôä,ð õ �”	‰?Œ?€DÝ•S˜‘Y”YÑÔð 0ð 0ˆØÐ)Ð)Ð)Ø�s”)ˆCØŒv˜Ò&Ð&¨3¬:¸Ò+EÐ+EØ #¤¨¤�Ø×'Ò'¨¨]Ñ;Ô;Ð;Ý�s”y‘>”> QÒ&Ð&Ø”K×*Ò*¨3Ñ/Ô/Ð/øà9Ô>Ø&ô ä-ð õ Ð#Ñ$Ô$ qÒ(Ð(Ø'¨Ô*¨aÒ/Ð/Ð/Ð1KÑ/Ô/Ð/Ø(õ 	   e¤kÑ2Ô2Ð2ð #- *Ø"°>ð#ñ #ô #Ðõ % T¤[Ñ1Ô1Ñ€K�åˆG�KÔ  $Ð(CÑDÔDÐDØ ;€G�C�”ÑÔÑÐÐrS   FÚobserved_node_namesÚis_decomposedc                 óT  — |t          | j        ¦  «                 }|j        }d}	t          |¦  «        }
t	          ||
¦  «        rI|j        }	|                     ¦   «         }t          | j        ¦  «        \  }}t          ||         ||¦  «         | j	        |v }|�t          | |¦  «        s|sdS t          |¦  «        }|                     t          |¦  «        g ¦  «        }t          ||¦  «        sdS t          |¦  «        }|sdS d}|}t	          |t           j        j        j        j        ¦  «        r
|}|d         }d|i}t	          |t           j        j        ¦  «        r€|                     ¦   «         }|                     ¦   «         } ||j        ¦  «          ||j        ¦  «         t3          |¦  «        }t3          |¦  «        }|                     ||dœ¦  «         �n1t	          |t           j        j        t           j        j        f¦  «        r†|j        D ]}}t=          ||¦  «        rk|                     d¦  «        rVtA          ||¦  «        }|                     ¦   «         }	|	j!        t           j"        k    r |	|¦  «         t3          |	¦  «        ||<   Œ~n{|	du }|r:|                     ¦   «         }	tG          |¦  «        }|r|	 $                    |¦  «         | }|r|r|s |	|j        ¦  «         |                     t3          |	¦  «        ¦  «         tK          |¦  «        }|                     tM          |¦  «        d¦  «        }|€J dtM          |¦  «        › �¦   «         ‚| '                    ||¦  «        }|�||d<   dS t          | j        ¦  «        \  }}t          ||         ||¦  «         dS )a¶  Convert a weighted module to reference quantized module in the model
    If the QConfig of a QAT module is not set, the module will still be converted to
    a float module.

    Args:
      - node: The call_module node of the observed standalone module
      - modules: named_module of original model
      - observed_node_names: names for the set of observed fx node, we can skip
        this conversion if the node is not observed
    Nr   r  )Ú	weight_ihÚ	weight_hhÚweightz3No reference quantized module class configured for )(rt   rs   Úqconfigr   rr   Úweight_fake_quantÚto_floatr   r  rÑ   rL   r   Úgetrã   r2   r!   rZ   rþ   rê   Ú	intrinsicÚ_FusedModuleÚRNNCellBaser  r  r  r   ÚupdateÚLSTMÚGRUÚ_flat_weights_namesrv   Ú
startswithr•   rY   rÁ   r8   r¿   r   r&   Ú
from_float) rC   rD   r  rF   rÓ   r  rû   Úoriginal_moduler  Úweight_post_processÚqat_module_classesr  rÑ   Úis_observedÚpattern_to_dtype_configsÚdtype_configsÚis_weight_quantizedÚfused_moduleÚfloat_moduleÚwq_or_wq_dictÚweight_post_process_ihÚweight_post_process_hhÚweight_qparams_ihÚweight_qparams_hhÚwnr  Úis_ptqÚdeviceÚis_qatÚ)root_module_to_quantized_reference_moduleÚref_qmodule_clsÚref_qmodules                                    rP   rA   rA   â  s#  € ð& �c $¤+Ñ.Ô.Ô/€OØ)Ô1€GØÐÝ/°Ñ?Ô?Ðå�/Ð#5Ñ6Ô6ð =ð .Ô?ÐØ)×2Ò2Ñ4Ô4ˆå(¨¬Ñ5Ô5Ñˆ�TÝ�˜Ô$ d¨OÑ<Ô<Ð<à”)Ð2Ð2€Kð 	ˆÝ˜TÐ#7Ñ8Ô8ð 	àð 	ð 	ˆõ  <¸NÑKÔKÐØ,×0Ò0µ°oÑ1FÔ1FÈÑKÔK€MÝ1°'¸=ÑIÔIð Øˆõ .¨gÑ6Ô6Ðð ð Øˆà€LØ"€Lå�/¥5¤8¤;Ô#8Ô#EÑFÔFð 'Ø#ˆØ# A”ˆð % mÐ4€MÝ�,¥¤Ô 4Ñ5Ô5ð 7CØ!(§¢Ñ!1Ô!1ÐØ!(§¢Ñ!1Ô!1ÐØÐ˜|Ô5Ñ6Ô6Ð6ØÐ˜|Ô5Ñ6Ô6Ð6Ý+Ð,BÑCÔCÐÝ+Ð,BÑCÔCÐØ×Òà.Ø.ðð ñ	
ô 	
ð 	
ñ 	
õ 
�L¥5¤8¤=µ%´(´,Ð"?Ñ	@Ô	@ð *Cð Ô2ð 	Ið 	IˆBÝ�| RÑ(Ô(ð I¨R¯]ª]¸8Ñ-DÔ-Dð IÝ  ¨rÑ2Ô2�Ø&-§n¢nÑ&6Ô&6Ð#Ø&Ô,µ´Ò;Ð;Ø'Ð'¨Ñ/Ô/Ð/Ý$3Ð4GÑ$HÔ$H�˜bÑ!øð	Ið %¨Ð,ˆØð 	/Ø")§.¢.Ñ"2Ô"2ÐÝ1°,Ñ?Ô?ˆFØð /Ø#×&Ò& vÑ.Ô.Ð.ð( �ˆØð 	5 ,ð 	5°6ð 	5ØÐ Ô 3Ñ4Ô4Ð4à×Ò�_Ð-@ÑAÔAÑBÔBÐBõ 	6°nÑEÔEð .ð @×CÒCÝ$ \Ñ2Ô2°Dñô €Oð 	Ð#Ð#ØiÕ=YÐZfÑ=gÔ=gÐiÐiñ 	$Ô#Ð#à!×,Ò,¨\¸=ÑIÔI€KØÐØ%ˆ�Q‰ˆˆå(¨¬Ñ5Ô5Ñˆ�TÝ�˜Ô$ d¨KÑ8Ô8Ð8Ð8Ð8rS   Ú	prev_nodec                 ó  — t          |t          ¦  «        sJ d|› �¦   «         ‚|j        dk    r[|j        dk    rR|                      ||j        d         ¦  «         t          |j        ¦  «        dk    r|                     |¦  «         dS dS dS dS )zã
    Given a custom module `node`, if the previous node is a dequantize, reroute the custom as follows:

    Before: quantize - dequantize - custom_module
    After: quantize - custom_module
                 \ - dequantize
    zDExpecting the argument for custom module node to be a Node, but got r¾   rº   r   N)	rr   r%   rÖ   rs   rÝ   ry   r  rz   r   )rC   r7  rq   s      rP   Ú,_remove_previous_dequantize_in_custom_moduler9  r  s¼   € õ Ø•4ñô ð Zð ZàYÈiÐYÐYñZô Zð ð „|�}Ò$Ð$¨Ô)9¸\Ò)IÐ)IØ×Ò 	¨9¬>¸!Ô+<Ñ=Ô=Ð=åˆyŒÑÔ 1Ò$Ð$Ø×Ò˜YÑ'Ô'Ð'Ð'Ð'ð	 %Ð$Ð)IÐ)Ið %Ð$rS   Úcustom_module_class_mappingÚ(statically_quantized_custom_module_nodesc                 ó2  — |t          | j        ¦  «                 }t          | |¦  «        }|j        }t	          |¦  «        �r|                     | ¦  «         t          | |¦  «        rÞt          | j        ¦  «        dk    r>t          | j        d         t          ¦  «        rt          | j        d         ¦  «        dk    sJ ‚| j        \  }\  }	}
t          |t          ¦  «        sJ ‚t          |	t          ¦  «        sJ ‚t          |
t          ¦  «        sJ ‚t          | ||¦  «         t          | |	|¦  «         t          | |
|¦  «         nþt          | |¦  «        ržt          | j        ¦  «        dk    sJ ‚| j        \  }}}t          |t          ¦  «        sJ ‚t          |t          ¦  «        sJ ‚t          |t          ¦  «        sJ ‚t          | ||¦  «         t          | ||¦  «         t          | ||¦  «         nP| j        d         }t          |t          ¦  «        sJ ‚t          | ||¦  «         t          | |¦  «        }|€J ‚||_        t          |||¦  «        }|                     |¦  «        }t#          | j        ¦  «        \  }}t%          ||         ||¦  «         dS )aÞ  Converts an observed custom module to a quantized custom module based on
    `custom_module_class_mapping`
    For static quantization, we'll also remove the previous `dequantize` node and
    attach the observer node for output to the module, the observer for the node
    will be converted to a dequantize node instead of quantize-dequantize pairs
    later in the graph. In the end we would have a quantized custom module that
    has the same interface as a default quantized module in nn.quantized namespace,
    i.e. quantized input and quantized output.

    Args:
      - node: The call_module node of the observed standalone module
      - graph: The graph containing the node
      - modules: named_module of original model
      - custom_module_class_mapping: mapping from observed custom module class to
        quantized custom module class, used to swap custom modules
      - statically_quantized_custom_module_nodes: we'll add the custom module node
        if we find it is statically quantized, this will be used later when converting
        observers to quant/dequant node pairs, if the observed node is a statically
        quantized custom module nodes, we'll convert the observer to a dequantize node,
        this is to keep the interface the same as the default quantized module.
        TODO: maybe we want to redesign this part to align with reference model design
        as well, but there has been some discussions around the interface, so we can do
        it later.
    é   r'   é   r   N)rt   rs   rú   r  r   Úaddr6   r  ry   rr   r‘   r%   r9  r7   r    r   Úfrom_observedr   r  )rC   rq   rD   r:  r;  Úobserved_custom_modulerù   r  ÚinputsÚhidden0Úhidden1Úqueryr©   r°   rÛ   r    Úquantized_custom_module_classÚquantized_custom_moduler  rÑ   s                       rP   r?   r?   ‡  s¿  € ð> %¥S¨¬Ñ%5Ô%5Ô6ÐÝ,¨T°7Ñ;Ô;€IØ$Ô,€GÝ)¨'Ñ2Ô2ñ (UØ0×4Ò4°TÑ:Ô:Ð:Ý! $¨Ñ0Ô0ð &	Uõ �D”I‘” !Ò#Ð#Ý˜tœy¨œ|­UÑ3Ô3ð $å˜œ	 !œÑ%Ô%¨Ò*Ð*Ð*ð+ð ,0¬9Ñ(ˆVÑ'�g˜wÝ˜f¥dÑ+Ô+Ð+Ð+Ð+Ý˜g¥tÑ,Ô,Ð,Ð,Ð,Ý˜g¥tÑ,Ô,Ð,Ð,Ð,Ý8¸¸vÀuÑMÔMÐMÝ8¸¸wÈÑNÔNÐNÝ8¸¸wÈÑNÔNÐNÐNÝ" 4¨Ñ1Ô1ð 	Uõ �t”y‘>”> QÒ&Ð&Ð&Ð&Ø $¤	ÑˆE�3˜Ý˜e¥TÑ*Ô*Ð*Ð*Ð*Ý˜c¥4Ñ(Ô(Ð(Ð(Ð(Ý˜e¥TÑ*Ô*Ð*Ð*Ð*Ý8¸¸uÀeÑLÔLÐLÝ8¸¸sÀEÑJÔJÐJÝ8¸¸uÀeÑLÔLÐLÐLð ”)˜A”,ˆCÝ˜c¥4Ñ(Ô(Ð(Ð(Ð(Ý8¸¸sÀEÑJÔJÐJå&BÀ4ÈÑ&QÔ&QÐ#Ø*Ð6Ð6Ð6Ø=TÐ"Ô:õ %DØÐ ;¸Wñ%ô %Ð!ð <×IÒIØñô Ðõ % T¤[Ñ1Ô1Ñ€K�ÝˆG�KÔ  $Ð(?Ñ@Ô@Ð@Ð@Ð@rS   TÚconvert_custom_configÚis_standalone_moduleÚ_remove_qconfig_flagÚqconfig_mappingc           
      ó„  — |€t          ¦   «         }t          |t          ¦  «        r0t          j        dt
          d¬¦  «         t          j        |¦  «        }t          |t          ¦  «        r4t          j        dt
          d¬¦  «         |rt          j        |¦  «        nd}t          j	        |¦  «        }|�t          |t          ¦  «        sJ ‚t          |t          ¦  «        r0t          j        dt
          d¬¦  «         t          j        |¦  «        }|€t          ¦   «         }t          | ¦  «        s
J d¦   «         ‚| j        d         }|j        }	|j        }
|j        }|j        }t          |                      d	¬
¦  «        ¦  «        }|rÏ|j        }t          j	        |¦  «        }|j        rt+          ||¦  «         t-          | |¦  «         t/          ||¦  «         t1          | || j        ||	¦  «        }|                     ¦   «         D ]N\  }}||v sJ d|› d�¦   «         ‚||         �/t7          |||         ¦  «        sJ d|› d|› d||         › �¦   «         ‚ŒO|}t9          |j        ¦  «        }|j        }|j        �!t?          | |¦  «        }tA          | ||¦  «         tC          | |¦  «         d„ | j        j"        D ¦   «         }d}|
j#        }|
j$        }tK          |¦  «        }tM          | '                    ¦   «         ¦  «        }tQ          |¦  «        }tS          |¦  «        }tU          ¦   «         }tW          | j        j"        ¦  «        D �]x}|j,        dk    r!|} |dz  }| |v rt[          || j        ¦  «         Œ/|j,        dk    r½t]          |¦  «        dk    rŒN|}!|j/        d         }"t          |"tV          tL          f¦  «        r"|D ]}#ta          |"|#         |!| j        ¦  «         ŒŒ›t          |"tb          t          f¦  «        rd|v rta          |"|!| j        ¦  «         ŒÒt          j        dte          |"¦  «        › �¦  «         Œ÷|j,        dk    �rutg          ||¦  «        }$|$€J ‚ti          |$¦  «        rT|j/        d         }%|%|v rtk          || j        ¦  «         �ŒN|rtm          | |||	|¦  «         �Œeto          | |||	|¦  «         �Œzt          |$tp          ¦  «        rtk          || j        ¦  «         �Œ¦ts          |$¦  «        rtu          ||| ||¦  «         �ŒÊtw          |$¦  «        tU          |¦  «         <                    |¦  «         <                    |¦  «        v rAtw          |$¦  «        |v rtw          |$d         ¦  «        |vr�Œ8t{          |||||||¦  «         �ŒOtw          |$¦  «        |v rt}          || j        |||¦  «         �Œz| j         ?                    ¦   «          t�          | | j        ¦  «        } |stƒ          | ||	¦  «        } |rt…          | ¦  «         |  C                    ¦   «          | j         D                    dd¦  «         | S )a‡  
    We will convert an observed model (a module with observer calls) to a reference
    quantized model, the rule is simple:
    1. for each observer module call in the graph, we'll convert it to calls to
       quantize and dequantize functions based on the observer instance
    2. for weighted operations like linear/conv, we need to convert them to reference
       quantized module, this requires us to know whether the dtype configured for the
       weight is supported in the backend, this is done in prepare step and the result
       is stored in observed_node_names, we can decide whether we need to swap the
       module based on this set

    Args:
       * `is_standalone_module`: when this flag is True, it means we are quantizing
       a submodule that is not inlined in parent module, and will be quantized
       separately as one unit.

       * `is_decomposed`: a boolean flag to indicate whether we want to use the
        quantize operator for decomposed quantized tensor
        (torch.ops.quantized_decomposed.quantize_per_tensor) or default/standalone
        quantized tensor (torch.quantize_per_tensor)

    Returns:
         a quantized standalone module, whether input/output is quantized is
         specified by prepare_custom_config, with
         input_quantized_idxs, output_quantized_idxs, please
         see docs for :func:`~torch.ao.quantization.prepare_fx` for details
    NzšPassing a convert_custom_config_dict to convert is deprecated and will not be supported in a future version. Please pass in a ConvertCustomConfig instead.r=  )Ú
stacklevelz�Passing a QConfig dictionary to convert is deprecated and will not be supported in a future version. Please pass in a QConfigMapping instead.z�Passing a backend_config_dict to prepare is deprecated and will not be supported in a future version. Please pass in a BackendConfig instead.z-incoming model must be produced by prepare_fxrý   F)Úremove_duplicatezExpected key z  in convert node_name_to_qconfigzExpected k zD to have the same value in prepare and convert QConfigMappings, but z was updated to c                 ó2   — g | ]}|j         d k    ¯|j        ‘ŒS )Úplaceholder)rÖ   rÑ   )rN   rC   s     rP   ú
<listcomp>zconvert.<locals>.<listcomp>j  s-   € ð ð ð Ø°4´7¸mÒ3KÐ3KˆŒ	Ð3KÐ3KÐ3KrS   r   rP  r'   Úoutputz1Unsupported node type for output_quantized_idxs: r÷   )Er+   rr   rß   rá   râ   ÚFutureWarningÚ	from_dictr   ÚcopyÚdeepcopyr   r   r-   rX   rE   Úprepare_custom_configr  rF   Únamed_modulesrK  r3  r4   r3   r0   r1   rq   rŽ   r   r;   Úobserved_to_quantized_mappingÚ!equalization_node_name_to_qconfigr*   r)   rÚ   rÕ   Úinput_quantized_indexesÚoutput_quantized_indexesr   r‘   r{   r   r   Úsetrx   rÖ   rÌ   r  ry   rÞ   r%   rã   r5   r   rÎ   r·   rÊ   r   r.   r@   r&   ÚunionrA   r?   Úeliminate_dead_coder"   r/   r   Údelete_all_unused_submodulesÚpop)&rB   rû   rH  rI  rJ  rK  rÓ   r  Úobserved_graph_module_attrsrE   rW  r  rF   rD   Úprepare_qconfig_mappingÚmodules_copyÚconvert_node_name_to_qconfigÚkÚvÚcustom_module_classesr:  Úweight_eq_obs_dictÚgraph_inputsÚplaceholder_node_seen_cntÚinput_quantized_idxsÚoutput_quantized_idxsr4  Úroot_module_classesr$  Úfused_module_classesr;  rC   Úcur_placeholder_node_idxÚreturn_noderR  r  Úmodrí   s&                                         rP   r>   r>   Þ  sÀ  € ðJ Ð$Ý 3Ñ 5Ô 5ÐåÐ'­Ñ.Ô.ð UÝŒðQåØð		
ñ 	
ô 	
ð 	
õ !4Ô =Ð>SÑ TÔ TÐå�/¥4Ñ(Ô(ð 	
ÝŒðLåØð		
ñ 	
ô 	
ð 	
ð :IÐR�NÔ$ _Ñ5Ô5Ð5Èdð 	õ ”m OÑ4Ô4€OØÐ"¥j°Å.Ñ&QÔ&QÐ"Ð"ÐQå�.¥$Ñ'Ô'ð AÝŒðKåØð		
ñ 	
ô 	
ð 	
õ 'Ô0°Ñ@Ô@ˆàÐÝ2Ñ4Ô4ˆå˜uÑ%Ô%ÐVÐVÐ'VÑVÔVÐ%Ø"'¤*Ð-KÔ"LÐð 	$Ô6ð ð 	$Ô9ð ð %@Ô$SÐØ2MÔ2bÐõ �5×&Ò&¸Ð&Ñ>Ô>Ñ?Ô?€Gð ð <Ø2MÔ2]ÐÝ”} WÑ-Ô-ˆà&Ô-ð 	EÝ# O°^ÑDÔDÐDÝ" 5¨/Ñ:Ô:Ð:å1Ð2IÈ?Ñ[Ô[Ð[Ý'EØ�< ¤¨oÐ?Qñ(
ô (
Ð$ð )×.Ò.Ñ0Ô0ð 	ð 	‰DˆAˆqàÐ1Ð1Ð1Ð1ØB˜qÐBÐBÐBñ 2Ô1Ð1à+¨AÔ.Ð:Ý% aÐ)EÀaÔ)HÑIÔIð ð ðP !ð Pð PØðPð PØ.JÈ1Ô.MðPð Pñô ÐIøð  <Ðå8ØÔ;ñô Ðð #8Ô"UÐà"ÔDÐPõ 9¸ÀÑHÔHÐÝ�u˜gÐ'9Ñ:Ô:Ð:õ ˜% Ñ0Ô0Ð0ðð Ø#œkÔ/ðñ ô €Lð !"ÐØ&;Ô&SÐØ'<Ô'UÐõ 	6°nÑEÔEð .õ  Ð I× NÒ NÑ PÔ PÑQÔQÐÝ/°Ñ?Ô?ÐÝ3°NÑCÔCÐÝ:=¹%¼%Ð,å�U”[Ô&Ñ'Ô'ð añ aˆØŒ7�mÒ#Ð#Ø'@Ð$Ø%¨Ñ*Ð%Ø'Ð+?Ð?Ð?õ
 (¨¨e¬kÑ:Ô:Ð:øØŒW˜Ò Ð åÐ(Ñ)Ô)¨QÒ.Ð.Øð ˆKØ”Y˜q”\ˆFå˜&¥4­ -Ñ0Ô0ð Ø0ð ð �CÝ6Ø˜sœ [°%´+ñô ð ð ðõ ˜F¥T­4 LÑ1Ô1ð 	ð Ð-Ð-Ð-Ý6°v¸{ÈEÌKÑXÔXÐXøå”ØVÍÈVÉÌÐVÐVñô ð ð ð ŒW˜Ò%Ñ%Ý˜d GÑ,Ô,ˆCØ�?�?�?Ý*¨3Ñ/Ô/ð ;Ø $¤	¨!¤�Ø Ð$LÐLÐLÝJØ˜eœkñô ð ñ ð %ð ÝRØ!Ø Ø#Ø.Ø0ñô ð ñ õ HØ!Ø Ø#Ø.Ø0ñô ð ñ õ ˜C¥Ñ-Ô-ð $ÝFØ˜%œ+ñô ð ñ õ 0°Ñ4Ô4ð  Ý)Ø˜' 5¨,¸ñô ð ñ õ
 .¨cÑ2Ô2µcÐ:MÑ6NÔ6N×6TÒ6TØ"ñ7ô 7çŠeÐ(Ñ)Ô)ð*ð *õ 1°Ñ5Ô5Ð9MÐMÐMÝ4°S¸´VÑ<Ô<ÐDWÐWÐWáÝ'ØØØ'Ø(Ø"Ø!Ø ñô ð ñ õ .¨cÑ2Ô2Ð6KÐKÐKÝ%ØØ”KØØ/Ø<ñô ð ùð 
„K×#Ò#Ñ%Ô%Ð%Ý˜˜uœ{Ñ+Ô+€Eð ð QÝ Ð';Ð=OÑPÔPˆð
 ð Ý˜ÑÔÐØ	×&Ò&Ñ(Ô(Ð(Ø	„J‡N‚NÐ1°4Ñ8Ô8Ð8Ø€LrS   )FF)FNFTNNF){rU  r™   rá   Útypingr   r   r   r   r   r   r	   r
   r   rZ   Útorch.ao.quantizationr   r   Ú$torch.ao.quantization.backend_configr   r   Ú*torch.ao.quantization.backend_config.utilsr   r   r   r   Útorch.ao.quantization.observerr   Útorch.ao.quantization.qconfigr   r   Ú%torch.ao.quantization.qconfig_mappingr   Ú torch.ao.quantization.quant_typer   Útorch.ao.quantization.quantizer   Útorch.ao.quantization.stubsr   Útorch.ao.quantization.utilsr   r   r   r   r   r    r!   Útorch.fxr"   Útorch.fx.graphr#   r$   r%   Útorch.nn.utils.parametrizer&   Ú_decomposedr(   Ú	_equalizer)   r*   Úcustom_configr+   r,   Úgraph_moduler-   r.   r/   Úqconfig_mapping_utilsr0   r1   r2   r3   r4   Úutilsr5   r6   r7   r8   r9   r:   r;   r<   r=   Ú__all__rÀ   rÁ   rÂ   r“   r”   Úuint16Úint16Úint32rÃ   rÄ   r€   r–   r„   r…   Úchoose_qparamsr’   Úper_tensor_symmetricÚchoose_qparams_symmetricr˜   Úfxrt   rê   ÚModulerã   r·   rÊ   rÎ   Úboolr|   rL   rÚ   rÞ   ru   rÌ   rú   r@   rA   r9  r?   r>   rm   rS   rP   ú<module>r‘     sì  ðð €€€Ø €€€Ø €€€Ø OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ Oà €€€Ø FÐ FÐ FÐ FÐ FÐ FÐ FÐ Fðð ð ð ð ð ð ð ðð ð ð ð ð ð ð ð ð ð ð ð GÐ FÐ FÐ FÐ FÐ FØ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DØ @Ð @Ð @Ð @Ð @Ð @Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø :Ð :Ð :Ð :Ð :Ð :Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð !Ð  Ð  Ð  Ð  Ð  Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø CÐ CÐ CÐ CÐ CÐ Cð 2Ð 1Ð 1Ð 1Ð 1Ð 1Ø BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ MÐ MÐ MÐ MÐ MÐ MÐ MÐ MØ ,Ð ,Ð ,Ð ,Ð ,Ð ,ðð ð ð ð ð ð ð ð ð ð ð ð ð ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ðð ð €ð 
„LØ	„KØ	„LØ	„KØ	„JØ	„LØ	„KØ	„KØ	ÔØ	ÔðÐ ð 
Ô˜UœYÔ;ÔJÔQØ	Ô ¤	Ô >Ô WÔ ^ð!Ð ðI#ØŒ8ÔðI#à
ðI#ð �#�u”x”Ð&Ô'ðI#ð ˜S %¨¨T¨	Ô"2Ð2Ô3ð	I#ð
 ˜s J˜Ô/ðI#ð 
ðI#ð I#ð I#ð I#ð^J#ØŒ8ÔðJ#à
ðJ#ð �#�u”x”Ð&Ô'ðJ#ð ˜S %¨¨T¨	Ô"2Ð2Ô3ð	J#ð
 ˜s J˜Ô/ðJ#ð 
ðJ#ð J#ð J#ð J#ðj	<Ø
ð	<Øð	<à	ð	<ð 	<ð 	<ð 	<ð°e´h´oð È$ð ð ð ð ð
Ø
ð
Ø*.¨s°J¨Ô*?ð
à	ð
ð 
ð 
ð 
ð) Kð )Àð )ÐSWð )ð )ð )ð )ð,
¨Cð 
°tð 
ÀEð 
Èdð 
ð 
ð 
ð 
ð*6Øð6à˜S %¨¨T¨	Ô"2Ð2Ô3ð6ð ˜s J˜Ô/ð6ð ˆ3�ˆ8„_ð	6ð 6ð 6ð 6ðrD $ð D¨uð D¸ð Dð Dð Dð DðØ
ðØ˜c 5¤8¤?Ð2Ô3ðàˆeŒhŒoÔðð ð ð ð=<Ø
ð=<à�#�u”x”Ð&Ô'ð=<ð Œ8Ôð=<ð ð	=<ð
 ˜]Ô+ð=<ð 
ð=<ð =<ð =<ð =<ðL  ØðM9ð M9Ø
ðM9à�#�u”x”Ð&Ô'ðM9ð ˜SœðM9ð ˜s J˜Ô/ð	M9ð
 "ðM9ð ðM9ð ðM9ð 
ðM9ð M9ð M9ð M9ð`(Ø
ð(Øð(Ø(-ð(à	ð(ð (ð (ð (ð*TAØ
ðTAàðTAð �#�u”x”Ð&Ô'ðTAð "& i°°d¸D°jÔ1AÐ&AÔ!Bð	TAð
 /2°$¬iðTAð 
ðTAð TAð TAð TAðr ØNRØ!&Ø!%ØCGØAEØðQð QØðQàðQð !Ð!4°d¸3À¸8´nÀdÐ!JÔKðQð ð	Qð
 ðQð ˜>¨4°°S°¬>¸4Ð?Ô@ðQð ˜-¨¨c°3¨h¬¸Ð=Ô>ðQð ðQð ðQð Qð Qð Qð Qð QrS   