§
    ŠŠtj¯ê  ã                   ó  — d dl Z d dlZd dlZd dlmZmZ d dl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l&m'Z'm(Z(m)Z) d dl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m7Z7 ddl8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZG erd dlHmIZI dZJdZKg d¢ZLejM        ejN        ejO        ejP        ejQ        ejR        ejS        ejT        ejU        ejV        g
ZWejX        ejY        jZ        j[        j\        ej]        ejY        jZ        j^        j\        iZ_	 dDdej`        j%        de)deaebejc        jd        f         deaebeeebeff         f         deaebef         d ejg        dz  d!dfd"„Zh	 dDdej`        j%        de)deaebejc        jd        f         deaebeeebeff         f         deaebef         d ejg        dz  d!dfd#„Zide)d$e(d!dfd%„Zjd&ejc        jd        d!ekfd'„Zlde'deaebef         d!ekfd(„Zmd)e%d*ed!dfd+„Znd,ede)d$e(d!dfd-„Zod.e)deaebeeebeff         f         deaebef         d!eeebebf         fd/„Zpde)d$e(d!dfd0„Zqde)deaebejc        jd        f         d!ejc        jd        dz  fd1„Zrde)deaebejc        jd        f         dej`        j%        d2ekd*edz  d!dfd3„Zs	 	 	 dEde)deaebejc        jd        f         d5eteb         deaebef         d*ed6ekd2ekd ejg        dz  d!dfd7„Zude)d8e)d$e(d!dfd9„Zvde)d$e(deaebejc        jd        f         d:eaeeaefeff         f         d;ete)         d!dfd<„Zw	 	 	 	 	 	 	 	 dFde%d2ekd>e2eaebef         z  dz  d?ekd@ekdAeeaebef         z  dz  d*eeaebef         z  dz  d6ekdBekd!e%fdC„ZxdS )Gé    N)ÚAnyÚTYPE_CHECKING)Ú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)ÚCallableÚnumeric_debug_handleÚcustom)ÚconvertÚconvert_custom_moduleÚconvert_standalone_moduleÚconvert_weighted_moduleÚmodelÚnodeÚmodulesÚnode_name_to_scopeÚnode_name_to_qconfigÚmodel_deviceÚreturnc           	      ó4  ‡— | j         }|€t          d¦  «        ‚t          |j        t          ¦  «        s$t          dt          |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        ¦  «        rqt1          |	j        ¦  «        }t4          j        j        j        j        }t4          j        j        j        j        }|	j         }|	j!        }tE          |¦  «        }||||||dœ}nyt4          j        j        j#        j        }t4          j        j        j$        j        }tK          |¦  «        }t1          |¦  «        }|	j         }|	j!        }tE          |¦  «        }|||||dœ}|                     |¦  «        5  |j        d         }|g}| &                    ¦   «         D ]i\  }}|dv rKt          |tJ          t0          f¦  «        s/tO          | |||z   |z   ||¦  «        }| (                    |¦  «         ŒT| (                    |¦  «         Œj| )                    ||tU          |¦  «        i ¦  «        }|g|dd…         z   }| +                    |tU          |¦  «         |||¦  «        ¦  «        }|                     |¦  «         tX          |j-        v r(t\          |j-        tX                   v rt_          d¦  «        ‚|                     |¦  «         ddd¦  «         dS # 1 swxY w Y   dS |�r	d
}t4          j        j        j#        j0        }tE          |¦  «        }|t4          j1        t4          j2        fvrt          d¦  «        ‚|	j         }|	j!        }tg          |	dt4          j4        ¦  «        } tg          |	dt5          j5        t4          j6        ¦  «        j7        ¦  «        }!|||!|dœ}tp          |          }"|                     |¦  «        5  |j        d         }|j        d         gt          | 9                    ¦   «         ¦  «        z   }#| )                    d
|"tU          |#¦  «        i ¦  «        }$| )                    d
tt          j;        |$dfi ¦  «        }%| )                    d
tt          j;        |$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| )                    ||tU          |¦  «        i ¦  «        }|g|dd…         z   }t4          j        j        j$        j0        }| +                    |tU          |¦  «         |||¦  «        ¦  «        }|                     |¦  «         t\          |j-        v rt_          d¦  «        ‚|                     |¦  «         ddd¦  «         dS # 1 swxY w Y   dS |t4          j<        k    rÊt4          j        j        j=        j>        }'|                     |¦  «        5  |j        d         }| )                    d
|'|t4          j<        fi ¦  «        }(| )                    d
|'|(t4          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
    Númodules must not be Noneú*Expected node.target to be a str, but got r8   c              3   ó8   •K  — | ]}t          |‰¦  «        V — Œd S ©N©Ú_has_none_qconfig©Ú.0Únr@   s     €ú^/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/ao/quantization/fx/convert.pyú	<genexpr>zM_replace_observer_with_quantize_dequantize_node_decomposed.<locals>.<genexpr>„   ó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       rM   Ú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Ð$Ø#Ð#rP   Úcall_function)Ú_scale_Ú_zero_point_Ú_axis_Ú_quant_min_Ú_quant_max_Ú_dtype_)r_   r`   rb   rc   rd   ©r_   r`   r   zOpt2e numeric suite has been migrated to torchao (https://github.com/pytorch/ao)zVonly uint8 and int8 are supported in reference flow for dynamic quantization right nowÚqschemeÚeps)rb   rc   Ú_eps_rd   rb   rc   rd   )?ÚgraphÚAssertionErrorÚ
isinstanceÚtargetÚstrÚtypeÚ_get_module_path_and_prefixÚhasattrr8   ÚallÚlistÚargsÚusersÚkeysÚ_is_conversion_supportedÚinserting_beforeÚreplace_all_uses_withÚ
erase_noderV   rQ   ÚSUPPORTED_QDTYPESÚcalculate_qparamsr   rf   ÚintÚch_axisrW   ÚopsÚquantized_decomposedÚquantize_per_channelÚdefaultÚdequantize_per_channelÚ	quant_minÚ	quant_maxr   Úquantize_per_tensorÚdequantize_per_tensorÚfloatÚitemsr1   ÚappendÚcreate_nodeÚtupler^   Ú
CUSTOM_KEYrU   ÚNUMERIC_DEBUG_HANDLE_KEYÚNotImplementedErrorÚtensorÚuint8Úint8ÚgetattrÚper_tensor_affineÚfinforX   rg   Ú_QSCHEME_TO_CHOOSE_QPARAMS_OPÚvaluesÚoperatorÚgetitemÚfloat16Úconvert_element_typeÚno_fuse)*r<   r=   r>   r?   r@   rA   ri   Úmodule_pathÚprefixÚactivation_post_processÚskip_replacementrV   rQ   r]   Ú	node_typeÚquantize_opÚscaleÚ
zero_pointr}   rY   rƒ   r„   Údtype_ÚqparamsrZ   Úquantize_op_inputsÚkeyÚvalue_or_nodeÚqparam_nodeÚquantized_nodeÚ	dq_inputsÚdequantized_noderf   rg   Úchoose_qparams_opÚchoose_qparams_op_inputsÚchoose_qparams_nodeÚ
scale_nodeÚzero_point_nodeÚdtype_convert_opÚconvert_fp16_nodeÚconvert_fp32_nodes*       `                                     rM   Ú:_replace_observer_with_quantize_dequantize_node_decomposedrµ   a   s9
  ø€ ð& ŒK€EØ€ÝÐ7Ñ8Ô8Ð8Ý�d”k¥3Ñ'Ô'ð 
ÝØL½¸d¼kÑ9JÔ9JÐLÐLñ
ô 
ð 	
õ 6ØÐ Ð"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	#ð 1	#Øœ 1œˆJØ", ÐØ&-§m¢m¡o¤oð =ð =Ñ"��]ð Ð5Ð5Ð5Ý" =µ5½#°,Ñ?Ô?ð 6õ #<ØØØ# fÑ,¨sÑ2Ø%Ø$ñ#ô #�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å)Øeñô ð ð ×Ò˜TÑ"Ô"Ð"ðc1	#ð 1	#ð 1	#ñ 1	#ô 1	#ð 1	#ð 1	#ð 1	#ð 1	#ð 1	#ð 1	#ð 1	#øøøð 1	#ð 1	#ð 1	#ð 1	#ð 1	#ð 1	#ðd 
ñ l#ð $ˆ	Ý”iÔ4ÔHÔOˆõ
 % UÑ+Ô+ˆØ�%œ+¥u¤zÐ2Ð2Ð2Ý Øhñô ð ð ,Ô5ˆ	Ø+Ô5ˆ	ÝÐ1°9½eÔ>UÑVÔVˆÝÐ-¨uµe´kÅ%Ä-Ñ6PÔ6PÔ6TÑUÔUˆð
 %Ø$ØØð	
ð 
ˆõ :¸'ÔBÐà×#Ò# DÑ)Ô)ð 	ð 	Øœ 1œˆJØ(,¬	°!¬ ~½¸W¿^º^Ñ=MÔ=MÑ8NÔ8NÑ'NÐ$Ø"'×"3Ò"3ØÐ!2µEÐ:RÑ4SÔ4SÐUWñ#ô #Ðð ×*Ò*Ø¥Ô!1Ð4GÈÐ3KÈRñô ˆJð $×/Ò/Ø¥Ô!1Ð4GÈÐ3KÈRñô ˆOð
   Ô.ˆIØ Ô.ˆIØ˜IÔ&ˆEà%Ø /Ø(Ø(Ø ðð ˆGð%	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð6 ×#Ò# 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Ý)Øeñô ð ð ×Ò˜TÑ"Ô"Ð"ðK%	#ð %	#ð %	#ñ %	#ô %	#ð %	#ð %	#ð %	#ð %	#ð %	#ð %	#ð %	#øøøð %	#ð %	#ð %	#ð %	#ð %	#ð %	#ðL 
•%”-Ò	Ð	å œ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^   Ä6EÅE!Å$E!ËEP%Ð%P)Ð,P)ÔCW%×%W)×,W)ØD\1Ü1\5Ü8\5Ý>B `à`à`c           	      ó<
  ‡— |€t          d¦  «        ‚t          |j        t          ¦  «        s$t          dt	          |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}|	                     ¦   «         \  }}t5          |	j        ¦  «        r(t9          |	j        ¦  «        }||||d	œ}t&          j        }n0t?          |¦  «        }t9          |¦  «        }|||d
œ}t&          j         }|                     |¦  «        5  |j	        d         }|g}| !                    ¦   «         D ]M\  }}|dv r/tE          | |||z   |z   ||¦  «        }| #                    |¦  «         Œ8| #                    |¦  «         ŒN| $                    ||tK          |¦  «        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 ]}| #                    |¦  «         Œ| $                    ||tK          |¦  «        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 ]}| #                    |¦  «         Œ| $                    ||tK          |¦  «        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() -> ...
    NrD   rE   c              3   ó8   •K  — | ]}t          |‰¦  «        V — Œd S rG   rH   rJ   s     €rM   rN   zB_replace_observer_with_quantize_dequantize_node.<locals>.<genexpr>�  rO   rP   r   FrQ   r^   )r_   r`   ra   rd   )r_   r`   rd   re   Ú
dequantize)rs   )ÚfbgemmÚx86)rd   Ú_reduce_range_Úcall_methodÚtord   )-rj   rk   rl   rm   rn   ri   ro   rq   rr   rs   rt   ru   rv   rw   rx   ry   rV   rp   rQ   rW   Úquint8Úqint8Úqint32Úfloat8_e5m2Úfloat8_e4m3fnr{   r   rf   r|   r}   r€   r‡   r…   rˆ   r1   r‰   rŠ   r‹   r¼   Úquantize_per_tensor_dynamicÚbackendsÚ	quantizedÚenginer–   r™   )r<   r=   r>   r?   r@   rA   ri   rœ   r�   rž   rŸ   rV   rQ   r    r¡   r¢   r£   r}   r¥   rZ   r¦   r§   r¨   r©   rª   r¬   Úreduce_rangeÚvalues       `                       rM   Ú/_replace_observer_with_quantize_dequantize_noderÉ   r  sÒ  ø€ ð  €ÝÐ7Ñ8Ô8Ð8Ý�d”k¥3Ñ'Ô'ð 
ÝØL½¸d¼kÑ9JÔ9JÐLÐLñ
ô 
ð 	
ð Œ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õ #<ØØØ# fÑ,¨sÑ2Ø%Ø$ñ#ô #�Kð '×-Ò-¨kÑ:Ô:Ð:Ð:ð '×-Ò-¨mÑ<Ô<Ð<Ð<à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð  %×0Ò0°À^ÐDUÐ0ÑVÔVÐØ×&Ò&Ð'7Ñ8Ô8Ð8Ø×Ò˜TÑ"Ô"Ð"ð5	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð6 
ð '#ð $ˆ	ÝÔ7ˆõ ”~Ô/Ô6Ð:KÐKˆØ#°|ÐDÐDˆà×#Ò# DÑ)Ô)ð 	#ð 	#Øœ 1œˆJØ", ÐØ ŸšÑ)Ô)ð 1ð 1�Ø"×)Ò)¨%Ñ0Ô0Ð0Ð0à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð  %×0Ò0°À^ÐDUÐ0ÑVÔVÐØ×&Ò&Ð'7Ñ8Ô8Ð8Ø×Ò˜TÑ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 
•%”-Ò	Ð	Ø!ˆ	ØˆØ˜eÐ$ˆØ×#Ò# DÑ)Ô)ð 	#ð 	#Øœ 1œˆJØ", ÐØ ŸšÑ)Ô)ð 1ð 1�ð #×)Ò)¨%Ñ0Ô0Ð0Ð0à"×.Ò.Ø˜;­Ð.@Ñ(AÔ(AÀ2ñô ˆNð  %×0Ò0°À^ÐDUÐ0ÑVÔVÐØ×&Ò&Ð'7Ñ8Ô8Ð8Ø×Ò˜TÑ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð	 
 Ð	sK   Ã36D5Ä5D9Ä<D9È=CL$Ì$L(Ì+L(Í3B$P$Ð$P(Ð+P(ÑB$TÔTÔTri   c                 óâ   — | j         d         }t          |t          ¦  «        st          d|› �¦  «        ‚|                      |¦  «         |                     | ¦  «         t          ||¦  «         d S )Nr   z@Expecting the for call custom module node to be a Node, but got )rs   rk   r   rj   rx   ry   Ú_insert_dequantize_node)r=   ri   Úcall_custom_module_nodes      rM   Ú6_replace_observer_or_dequant_stub_with_dequantize_noderÍ     sƒ   € ð #œi¨œlÐÝÐ-­tÑ4Ô4ð 
ÝØhÐOfÐhÐhñ
ô 
ð 	
ð 	×ÒÐ6Ñ7Ô7Ð7Ø	×Ò�TÑÔÐÝÐ3°UÑ;Ô;Ð;Ð;Ð;rP   rž   c                 ó~   — | j         }d}t          | d¦  «        r| j        }|t          v r| p|p|t          j        k    S )NFrQ   )rV   rp   rQ   rz   rW   r™   )rž   rV   rQ   s      rM   rv   rv     sX   € Ø#Ô)€Eà€JÝÐ&¨Ñ5Ô5ð 8Ø,Ô7ˆ
ð 
Õ#Ð	#Ð	8¨Z¨ð 	"Øð	"à•E”MÒ!ðrP   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)rk   r   Úname)r=   r@   s     rM   rI   rI   +  s:   € õ 	�4�ÑÔð 	4ØŒIÐ-Ð-ð	4à  ¤Ô+¨tÐ3ðrP   Ú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.
    r^   N)ri   ÚnodesÚoprs   r4   r0   r3   )rÑ   rÒ   r=   Únode_argÚweight_observer_nodesÚweight_observer_modules         rM   Ú_run_weight_observersrÙ   8  s¡   € ð ”Ô$ð )ð )ˆØŒ7�oÒ%Ð%ØØœ	ð 
	)ð 
	)ˆHàð )Õ.¨t°XÑ>Ô>ð )Ý(>¸xÑ(HÔ(HÐ%Ø(Ð0ØÝ)IØÐ3ñ*ô *Ð&ð 'Ð&Ñ(Ô(Ð(øð
	)ð)ð )rP   Ú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¬¦  «         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: é   ©Ú
stacklevelN)rk   r   rÕ   rl   rs   Úreplace_input_withrr   r‹   Ú"_maybe_recursive_remove_dequantizeÚdictr–   ÚwarningsÚwarnrn   )rÚ   r=   ri   Úquantize_nodeÚarg_elements        rM   rà   rà   N  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Øð	
ñ 	
ô 	
ð 	
ð 	
ð 	
rP   Úobs_nodec                 ó¢  — | j         d         }t          |t          ¦  «        st          d|› �¦  «        ‚|j        |v r||j                 du nd}|rZt          | j        ¦  «        }|r|d         nd}d}|D ].}|j        dk    r!|j        t          j
        j        j        u r|} nŒ/|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 Nr^   Ú_inputÚ )rs   rk   r   rj   rÐ   rr   rt   rÕ   rl   rW   ÚnnÚ
functionalÚlinear)ræ   r?   r@   Úobserved_nodeÚis_input_observer_onlyrt   Úfirst_linear_use_or_first_useÚlinear_noderL   r�   rœ   Ú_s               rM   ro   ro   d  s>  € ð ”M !Ô$€Mõ
 �m¥TÑ*Ô*ð 
ÝØL¸]ÐLÐLñ
ô 
ð 	
ð
 ÔÐ!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ð
 ˆØ˜ÐÐrP   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á   rt   rß   )r=   ri   Údequantize_nodeÚ	user_nodes       rM   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)rt   rÕ   rm   rl   r   )r=   r>   Úmaybe_obs_nodeÚ	maybe_obss       rM   Ú_maybe_get_observer_for_noderú   §  sZ   € ð œ*ð !ð !ˆØÔ Ò-Ð-Ø¥ NÔ$9Ñ :Ô :Ô;ˆIÝ*¨9Ñ5Ô5ð !Ø Ð Ð Ð øØˆ4rP   Úis_referencec                 ór  — |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    r0|d         dk    rt)          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   z7Currently only quantized output idxs = [0] is supported)rÒ   N)rW   ÚaoÚquantizationÚquantize_fxÚconvert_to_reference_fxÚ
convert_fxrm   rl   rU   Ú&standalone_module_input_quantized_idxsrr   rs   ÚrangeÚlenrÕ   rß   rt   ri   ry   Ú'standalone_module_output_quantized_idxsrj   rË   r   Úsetattr)r=   r>   r<   rû   rÒ   Ú
convert_fnÚobserved_standalone_moduleÚsm_input_quantized_idxsrs   ÚidxrÚ   rä   Úsm_output_quantized_idxsÚquantized_standalone_moduleÚparent_namerÐ   s                   rM   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Ô&¨!Ò+Ð+Ý ØIñô ð õ 	   e¤kÑ2Ô2Ð2ð #- *Ø"°>ð#ñ #ô #Ðõ % T¤[Ñ1Ô1Ñ€K�åˆG�KÔ  $Ð(CÑDÔDÐDØ ;€G�C�”ÑÔÑÐÐrP   FÚobserved_node_namesÚis_decomposedc                 óh  — |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œ¦  «         �n6t	          |t           j        j        t           j        j        f¦  «        r†|j        D ]}}t=          ||¦  «        rk|                     d¦  «        rVtA          ||¦  «        }|	                     ¦   «         }
|
j!        t           j"        k    r |
|¦  «         t3          |
¦  «        ||<   Œ~n€|
du }|r?|	                     ¦   «         }
|�|}ntG          |¦  «        }|r|
 $                    |¦  «         | }|r|r|s |
|j        ¦  «         |                     t3          |
¦  «        ¦  «         tK          |¦  «        }|                     tM          |¦  «        d¦  «        }|€tO          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 ))rm   rl   Úqconfigr	   rk   Úweight_fake_quantÚto_floatr   r  rÐ   rI   r   Úgetrn   r)   r   rW   rþ   rê   Ú	intrinsicÚ_FusedModuleÚRNNCellBaser  r  r  r   ÚupdateÚLSTMÚGRUÚ_flat_weights_namesrp   Ú
startswithr’   rV   r¿   r/   r½   r
   r   rj   Ú
from_float)!r=   r>   r  r@   rÒ   r  rû   rA   Ú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!                                    rM   r;   r;   ø  s6  € ð( �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ð :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ÐØÐ'Ø%��å5°lÑCÔC�Øð /Ø#×&Ò& vÑ.Ô.Ð.ð( �ˆØð 	5 ,ð 	5°6ð 	5ØÐ Ô 3Ñ4Ô4Ð4à×Ò�_Ð-@ÑAÔAÑBÔBÐBõ 	6°nÑEÔEð .ð @×CÒCÝ$ \Ñ2Ô2°Dñô €Oð ÐÝØnÕB^Ð_kÑBlÔBlÐnÐnñ
ô 
ð 	
ð "×,Ò,¨\¸=ÑIÔI€KØÐØ%ˆ�Q‰ˆˆå(¨¬Ñ5Ô5Ñˆ�TÝ�˜Ô$ d¨KÑ8Ô8Ð8Ð8Ð8rP   Ú	prev_nodec                 ó(  — t          |t          ¦  «        st          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)
rk   r   rj   rÕ   rl   rß   rs   r  rt   ry   )r=   r7  ri   s      rM   Ú,_remove_previous_dequantize_in_custom_moduler9  �  s²   € õ �i¥Ñ&Ô&ð 
ÝØ^ÐS\Ð^Ð^ñ
ô 
ð 	
ð „|�}Ò$Ð$¨Ô)9¸\Ò)IÐ)IØ×Ò 	¨9¬>¸!Ô+<Ñ=Ô=Ð=åˆyŒÑÔ 1Ò$Ð$Ø×Ò˜YÑ'Ô'Ð'Ð'Ð'ð	 %Ð$Ð)IÐ)Ið %Ð$rP   Úcustom_module_class_mappingÚ(statically_quantized_custom_module_nodesc                 ó  — |t          | j        ¦  «                 }|j        }t          |¦  «        �r…|                     | ¦  «         t          | |¦  «        �rt          | j        ¦  «        dk    r>t          | j        d         t          ¦  «        rt          | j        d         ¦  «        dk    st          d¦  «        ‚| j        \  }\  }}	t          |t          ¦  «        st          d¦  «        ‚t          |t          ¦  «        st          d¦  «        ‚t          |	t          ¦  «        st          d¦  «        ‚t          | ||¦  «         t          | ||¦  «         t          | |	|¦  «         �nLt          | |¦  «        rÒt          | j        ¦  «        dk    rt          d¦  «        ‚| j        \  }
}}t          |
t          ¦  «        st          d	¦  «        ‚t          |t          ¦  «        st          d
¦  «        ‚t          |t          ¦  «        st          d¦  «        ‚t          | |
|¦  «         t          | ||¦  «         t          | ||¦  «         nj| j        d         }t          |t          ¦  «        st          d¦  «        ‚t          | ||¦  «         t          | |¦  «        }|€t          d¦  «        ‚||_        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   zDExpected LSTM custom module inputs to be (input, (hidden0, hidden1))zExpected inputs to be a NodezExpected hidden0 to be a NodezExpected hidden1 to be a Nodeé   z;Expected MHA custom module inputs to be (query, key, value)zExpected query to be a NodezExpected key to be a NodezExpected value to be a Noder   zExpected arg to be a NodeNzIExpected activation_post_process to be present for observed custom module)rm   rl   r  r   Úaddr-   r  rs   rk   r‹   rj   r   r9  r.   rú   rž   r   Úfrom_observedr   r  )r=   ri   r>   r:  r;  Úobserved_custom_moduler  ÚinputsÚhidden0Úhidden1Úqueryr§   rÈ   rÚ   rž   Úquantized_custom_module_classÚquantized_custom_moduler  rÐ   s                      rM   r9   r9   £  s4  € ð> %¥S¨¬Ñ%5Ô%5Ô6ÐØ$Ô,€GÝ)¨'Ñ2Ô2ñ 8UØ0×4Ò4°TÑ:Ô:Ð:Ý! $¨Ñ0Ô0ñ 6	Uõ �D”I‘” !Ò#Ð#Ý˜tœy¨œ|­UÑ3Ô3ð $å˜œ	 !œÑ%Ô%¨Ò*Ð*å$ØZñô ð ð ,0¬9Ñ(ˆVÑ'�g˜wÝ˜f¥dÑ+Ô+ð EÝ$Ð%CÑDÔDÐDÝ˜g¥tÑ,Ô,ð FÝ$Ð%DÑEÔEÐEÝ˜g¥tÑ,Ô,ð FÝ$Ð%DÑEÔEÐEÝ8¸¸vÀuÑMÔMÐMÝ8¸¸wÈÑNÔNÐNÝ8¸¸wÈÑNÔNÐNÑNÝ" 4¨Ñ1Ô1ð !	Uõ �4”9‰~Œ~ Ò"Ð"Ý$ØQñô ð ð !%¤	ÑˆE�3˜Ý˜e¥TÑ*Ô*ð DÝ$Ð%BÑCÔCÐCÝ˜c¥4Ñ(Ô(ð BÝ$Ð%@ÑAÔAÐAÝ˜e¥TÑ*Ô*ð DÝ$Ð%BÑCÔCÐCÝ8¸¸uÀeÑLÔLÐLÝ8¸¸sÀEÑJÔJÐJÝ8¸¸uÀeÑLÔLÐLÐLð ”)˜A”,ˆCÝ˜c¥4Ñ(Ô(ð BÝ$Ð%@ÑAÔAÐAÝ8¸¸sÀEÑJÔJÐJå&BÀ4ÈÑ&QÔ&QÐ#Ø&Ð.Ý$Ø_ñô ð ð >UÐ"Ô:õ %DØÐ ;¸Wñ%ô %Ð!ð <×IÒIØñô Ðõ % T¤[Ñ1Ô1Ñ€K�ÝˆG�KÔ  $Ð(?Ñ@Ô@Ð@Ð@Ð@rP   TÚconvert_custom_configÚis_standalone_moduleÚ_remove_qconfig_flagÚqconfig_mappingÚkeep_original_weightsc	                 óÔ  — |€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t          d¦  «        ‚t          |t          ¦  «        r0t          j        dt
          d¬¦  «         t          j        |¦  «        }|€t          ¦   «         }t          | ¦  «        st          d¦  «        ‚| j        d	         }	|	j        }
|	j        }|	j        }|	j        }t          |                      d
¬¦  «        ¦  «        }|rÙ|	j        }t          j	        |¦  «        }|	j        rt-          ||¦  «         t/          | |¦  «         t1          ||¦  «         t3          | || j        ||
¦  «        }|                     ¦   «         D ]X\  }}||vrt          d|› d�¦  «        ‚||         �4t9          |||         ¦  «        st          d|› d|› d||         › �¦  «        ‚ŒY|}t;          |j        ¦  «        }|j        }|	j        �!tA          | |¦  «        }tC          | ||¦  «         tE          | |¦  «         d}|j#        }|j$        }tK          |¦  «        }tM          | '                    ¦   «         ¦  «        }tQ          |¦  «        }tS          |¦  «        }tU          ¦   «         }tW          | ¦  «        }tY          | j        j-        ¦  «        D �]Š} | j.        dk    r!|}!|dz  }|!|v rt_          | | j        ¦  «         Œ/| j.        dk    r¿ta          |¦  «        dk    rŒN| }"| j1        d         }#t          |#tX          tL          f¦  «        r"|D ]}$te          |#|$         |"| j        ¦  «         ŒŒ›t          |#tf          t          f¦  «        rd|v rte          |#|"| j        ¦  «         ŒÒt          j        dti          |#¦  «        › �d¬¦  «         Œù| j.        dk    �r…tk          | |¦  «        }%|%€t          d¦  «        ‚tm          |%¦  «        rV| j1        d         }&|&|v rto          | | j        ¦  «         �Œ]|rtq          | | ||
||¦  «         �Œuts          | | ||
||¦  «         �Œ‹t          |%tt          ¦  «        rto          | | j        ¦  «         �Œ·tw          |%¦  «        rty          | || ||¦  «         �ŒÛt{          |%¦  «        tU          |¦  «         >                    |¦  «         >                    |¦  «        v rBt{          |%¦  «        |v rt{          |%d         ¦  «        |vr�ŒIt          | |||||||¦  «         �Œat{          |%¦  «        |v rt�          | | j        |||¦  «         �ŒŒ| j         A                    ¦   «          t…          | | j        ¦  «        } |st‡          | ||
|¦  «        } |rt‰          | ¦  «         |  E                    ¦   «          | j         F                    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Ü   rÝ   z�Passing a QConfig dictionary to convert is deprecated and will not be supported in a future version. Please pass in a QConfigMapping instead.z0qconfig_mapping must be None or a QConfigMappingz�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 r   Úplaceholderr   Úoutputz1Unsupported node type for output_quantized_idxs: r÷   zEExpected module for call_module node to be present in modules mapping)Gr"   rk   rá   râ   rã   ÚFutureWarningÚ	from_dictr   ÚcopyÚdeepcopyrj   r   r   r$   rU   r?   Úprepare_custom_configr  r@   Únamed_modulesrJ  r3  r+   r*   r'   r(   ri   rˆ   r   r2   Úobserved_to_quantized_mappingÚ!equalization_node_name_to_qconfigr!   r    rÙ   Úinput_quantized_indexesÚoutput_quantized_indexesr
   r‹   ru   r	   r   Úsetr/   rr   rÔ   rÕ   rË   r  rs   rà   r   rn   r,   r   rÍ   rµ   rÉ   r   r%   r:   r   Úunionr;   r9   Úeliminate_dead_coder   r&   r   Údelete_all_unused_submodulesÚpop)'r<   rû   rG  rH  rI  rJ  rÒ   r  rK  Úobserved_graph_module_attrsr?   rT  r  r@   r>   Úprepare_qconfig_mappingÚmodules_copyÚconvert_node_name_to_qconfigÚkÚvÚcustom_module_classesr:  Úweight_eq_obs_dictÚplaceholder_node_seen_cntÚinput_quantized_idxsÚoutput_quantized_idxsr4  Úroot_module_classesr$  Úfused_module_classesr;  rA   r=   Úcur_placeholder_node_idxÚreturn_noderO  r  Úmodrí   s'                                          rM   r8   r8   	  sô  € ðL Ð$Ý 3Ñ 5Ô 5ÐåÐ'­Ñ.Ô.ð UÝŒðQåØð		
ñ 	
ô 	
ð 	
õ !4Ô =Ð>SÑ TÔ TÐå�/¥4Ñ(Ô(ð 	
ÝŒðLåØð		
ñ 	
ô 	
ð 	
ð :IÐR�NÔ$ _Ñ5Ô5Ð5Èdð 	õ ”m OÑ4Ô4€OØÐ#¥z°/Å>Ñ'RÔ'RÐ#ÝÐOÑPÔPÐPå�.¥$Ñ'Ô'ð AÝŒðKåØð		
ñ 	
ô 	
ð 	
õ 'Ô0°Ñ@Ô@ˆàÐÝ2Ñ4Ô4ˆå˜uÑ%Ô%ð NÝÐLÑMÔMÐMØ"'¤*Ð-KÔ"LÐà#Ô6ð ð 	$Ô9ð ð %@Ô$SÐà#Ô8ð õ �5×&Ò&¸Ð&Ñ>Ô>Ñ?Ô?€Gð ð <à'Ô7ð 	 õ ”} WÑ-Ô-ˆà&Ô-ð 	EÝ# O°^ÑDÔDÐDÝ" 5¨/Ñ:Ô:Ð:å1Ø# _ñ	
ô 	
ð 	
õ (FØ�< ¤¨oÐ?Qñ(
ô (
Ð$ð )×.Ò.Ñ0Ô0ð 
	ð 
	‰DˆAˆqØÐ4Ð4Ð4Ý$ØG AÐGÐGÐGñô ð ð ,¨AÔ.Ð:Ý% aÐ)EÀaÔ)HÑIÔIð Ý(ðT að Tð TØ ðTð TØ2NÈqÔ2QðTð Tñô ð øð  <Ðå8ØÔ;ñô Ðð #8Ô"UÐà"ÔDÐPõ 9¸ÀÑHÔHÐÝ�u˜gÐ'9Ñ:Ô:Ð:õ ˜% Ñ0Ô0Ð0ð !"ÐØ&;Ô&SÐØ'<Ô'UÐõ 	6°nÑEÔEð .õ  Ð I× NÒ NÑ PÔ PÑQÔQÐÝ/°Ñ?Ô?ÐÝ3°NÑCÔCÐÝ:=¹%¼%Ð,Ý/°Ñ6Ô6€Lå�U”[Ô&Ñ'Ô'ð hñ hˆØŒ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ð ð 
ÝØÐ'Ð);Ð=Rñ
ô 
ˆð ð Ý˜ÑÔÐØ	×&Ò&Ñ(Ô(Ð(Ø	„J‡N‚NÐ1°4Ñ8Ô8Ð8Ø€LrP   rG   )FFN)FNFTNNFF)yrR  r—   râ   Útypingr   r   rW   Ú$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_utilsr'   r(   r)   r*   r+   Úutilsr,   r-   r.   r/   r0   r1   r2   r3   r4   Úcollections.abcr5   r�   rŒ   Ú__all__r¾   r¿   rÀ   r�   r‘   Úuint16Úint16Úint32rÁ   rÂ   rz   r“   r~   r   Úchoose_qparamsr�   Úper_tensor_symmetricÚchoose_qparams_symmetricr•   Úfxrá   rm   rê   ÚModuler‹   rn   r2  rµ   rÉ   rÍ   Úboolrv   rI   rÙ   rà   ro   rË   rú   r:   rZ  r;   r9  r9   r8   © rP   rM   ú<module>rŽ     s)	  ðð €€€Ø €€€Ø €€€Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %à €€€ðð ð ð ð ð ð ð ðð ð ð ð ð ð ð ð ð ð ð ð 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Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,ðð ð ð ð ð ð ð ð ð ð ð ð ð ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð ð )Ø(Ð(Ð(Ð(Ð(Ð(ð 2Ð Ø€
ðð ð €ð 
„LØ	„KØ	„LØ	„KØ	„JØ	„LØ	„KØ	„KØ	ÔØ	ÔðÐ ð 
Ô˜UœYÔ;ÔJÔQØ	Ô ¤	Ô >Ô WÔ ^ð!Ð ð )-ðK#ð K#ØŒ8ÔðK#à
ðK#ð �#�u”x”Ð&Ô'ðK#ð ˜S %¨¨T¨	Ô"2Ð2Ô3ð	K#ð
 ˜s J˜Ô/ðK#ð ”, Ñ%ðK#ð 
ðK#ð K#ð K#ð K#ðn )-ðS#ð S#ØŒ8ÔðS#à
ðS#ð �#�u”x”Ð&Ô'ðS#ð ˜S %¨¨T¨	Ô"2Ð2Ô3ð	S#ð
 ˜s J˜Ô/ðS#ð ”, Ñ%ðS#ð 
ðS#ð S#ð S#ð S#ð|
<Ø
ð
<Øð
<à	ð
<ð 
<ð 
<ð 
<ð°e´h´oð È$ð ð ð ð ð
Ø
ð
Ø*.¨s°J¨Ô*?ð
à	ð
ð 
ð 
ð 
ð) Kð )Àð )ÐSWð )ð )ð )ð )ð,
¨Cð 
°tð 
ÀEð 
Èdð 
ð 
ð 
ð 
ð,7Øð7à˜S %¨¨T¨	Ô"2Ð2Ô3ð7ð ˜s J˜Ô/ð7ð ˆ3�ˆ8„_ð	7ð 7ð 7ð 7ðtD $ð D¨uð D¸ð Dð Dð Dð DðØ
ðØ˜c 5¤8¤?Ð2Ô3ðà
„X„_�tÑðð ð ð ð?<Ø
ð?<à�#�u”x”Ð&Ô'ð?<ð Œ8Ôð?<ð ð	?<ð
 " DÑ(ð?<ð 
ð?<ð ?<ð ?<ð ?<ðP  ØØ(,ðR9ð R9Ø
ðR9à�#�u”x”Ð&Ô'ðR9ð ˜SœðR9ð ˜s J˜Ô/ð	R9ð
 "ðR9ð ðR9ð ðR9ð ”, Ñ%ðR9ð 
ðR9ð R9ð R9ð R9ðj(Ø
ð(Øð(Ø(-ð(à	ð(ð (ð (ð (ð,cAØ
ðcAàðcAð �#�u”x”Ð&Ô'ðcAð "& i°°d¸D°jÔ1AÐ&AÔ!Bð	cAð
 /2°$¬iðcAð 
ðcAð cAð cAð cAðP ØIMØ!&Ø!%Ø>BØ<@ØØ"'ðbð bØðbàðbð /°°c¸3°h´Ñ?À$ÑFðbð ð	bð
 ðbð $ d¨3°¨8¤nÑ4°tÑ;ðbð " D¨¨c¨¤NÑ2°TÑ9ðbð ðbð  ðbð ðbð bð bð bð bð brP   