Ë
    g^(hêm  ã                   ó–  — d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZ ddlZddlmZ ddlmZ ddlmZ eeeef   eeeeef   f   e	f   Zd	e_        e	Zee
ee
e
f   ee
ee
e
f   f   e	f   Zd	e_         G d
„ d«      Zej4                  j6                  ej4                  j8                  ej4                  j:                  ej4                  j<                  ej4                  j>                  ej4                  j@                  ej4                  jB                  ej4                  jD                  ej4                  jF                  ej4                  jH                  ej4                  jJ                  ej4                  jL                  ej4                  jN                  ej4                  jP                  ej4                  jR                  hZ*ej4                  jV                  jX                  ej4                  jV                  jZ                  ej4                  jV                  j\                  ej4                  jV                  j^                  ej4                  jV                  j`                  ej4                  jV                  jb                  ej4                  jV                  jd                  ej4                  jV                  jf                  ej4                  jV                  jh                  ej4                  jV                  jj                  ej4                  jV                  jl                  ej4                  jV                  jn                  ej4                  jV                  jp                  ej4                  jV                  jr                  ej4                  jV                  jt                  ej4                  jV                  jv                  ej4                  jV                  jx                  ej4                  jV                  jz                  ej4                  jV                  j|                  ej~                  ej€                  ej|                  ej‚                  ej„                  ej†                  ejˆ                  ejŠ                  ejŒ                  hZGej�                  ddddddddddddddddddddd d!d"d#hZId$„ ZJd%„ ZKd&„ ZLd'„ ZMd(e	d)eNd*e	fd+„ZOd,„ ZPd-„ ZQd.„ ZRd/„ ZSd0„ ZTd1„ ZUd2„ ZVd3„ ZWd4„ ZXd5„ ZYd6„ ZZd*e[fd7„Z\d8„ Z]d9„ Z^d:ej¾                  d;ej¾                  d*e[fd<„Z`d=ead>ead?e[d@ejÄ                  dAe[d*eeaeaf   fdB„ZcdC„ ZddD„ ZedEej4                  jÌ                  dFej4                  jÌ                  d*eeN   fdG„ZgdHe
dIeheNe	f   d*eheNe	f   fdJ„ZidHe
d*dKfdL„ZjdMe
dIeheNe	f   d*dKfdN„Zkd=ead>ead*dfdO„ZlejÚ                  fd:ej¾                  d;ej¾                  d=ead>ead@ejÄ                  dPej¾                  d?e[dQejÜ                  d*eej¾                  ej¾                  f   fdR„ZodHe
d*eafdS„ZpdTej4                  jÌ                  dUee	dVf   d*eheNee	dVf   f   fdW„ZqdXej4                  jÌ                  d*e	fdY„Zrg dZ¢Zsy)[z?
Utils shared by different modes of quantization (eager/graph)
é    N)ÚOrderedDict)ÚgetfullargspecÚ	signature)ÚAnyÚCallableÚOptionalÚUnion)Ú	QuantType)ÚNode)Úis_parametrizedztorch.ao.quantization.utilsc                   ó   — e Zd ZdZy)ÚMatchAllNodeznA node pattern that matches all nodes, used in defining
    fusion patterns in FX Graph Mode Quantization
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/ao/quantization/utils.pyr   r   %   s   „ òr   r   ÚreluÚrelu_Ú
contiguousÚdetachÚdetach_ÚhardsigmoidÚhardsigmoid_ÚpermuteÚrepeatÚrepeat_interleaveÚreshapeÚresize_ÚshapeÚsigmoidÚsigmoid_ÚsizeÚsqueezeÚsqueeze_ÚtanhÚtanh_Ú	transposeÚ	unsqueezeÚ
unsqueeze_Úviewc                 ó  — | j                   dk(  xr | j                  t        v }| j                   dk(  xr | j                  t        v }| j                   dk(  xr' t	        |t        | j                  «         «      t        v }|||fS )NÚcall_functionÚcall_methodÚcall_module)ÚopÚtargetÚ	func_listÚmethod_listÚtypeÚstrÚmodule_type_list)ÚnodeÚmodulesÚis_call_functionÚis_call_methodÚis_call_modules        r   Ú
check_noder>   x   sy   € à—w‘w /Ñ1ÒN°d·k±kÄYÐ6NÐØ—W‘W Ñ-ÒL°$·+±+ÄÐ2L€Nà�‰�=Ñ ÒX¤T¨'´#°d·k±kÓ2BÑ*CÓ%DÔHXÐ%Xð ð ˜^¨^Ð;Ð;r   c                 óH   — | j                  «       }|j                  |«       |S )a»  
    Combines two dictionaries.

    This function takes two dictionaries as input and returns a new dictionary
    that contains all the key-value pairs from both input dictionaries.
    If there are any duplicate keys in the `additional_dict`, the values
    from the `additional_dict` will overwrite those in the `default_dict`.
    Args:
        default_dict (dict): The main dictionary that will be used as the base
        additional_dict (dict): The dictionary used to update `default_dict`

    Returns:
        dict: The resulting dictionary
    Example:
        >>> x = dict(a=1, b=1)
        >>> y = dict(b=2, c=3)
        >>> get_combined_dict(x, y)
        {'a': 1, 'b': 2, 'c': 3}
    )ÚcopyÚupdate)Údefault_dictÚadditional_dictÚds      r   Úget_combined_dictrE   ‚   s#   € ð( 	×ÑÓ€AØ‡H�Hˆ_ÔØ€Hr   c                 óR   — | t         j                  k(  xs | t         j                  k(  S ©N)ÚtorchÚper_tensor_affineÚper_tensor_symmetric©Úqschemes    r   Úis_per_tensorrM   ›   s#   € Ø”e×-Ñ-Ñ-ÒV°¼E×<VÑ<VÑ1VÐVr   c                 ód   — | t         j                  t         j                  t         j                  fv S rG   )rH   Úper_channel_affineÚ per_channel_affine_float_qparamsÚper_channel_symmetricrK   s    r   Úis_per_channelrR   Ÿ   s/   € ØÜ× Ñ Ü×.Ñ.Ü×#Ñ#ðð ð r   ÚobjÚfqnÚreturnc                 óV   — t        j                  t        |j                  d«      | «      S )zO
    Given an obj and a fqn such as "foo.bar.baz", returns gm.foo.bar.baz.
    ú.)Ú	functoolsÚreduceÚgetattrÚsplit)rS   rT   s     r   Úgetattr_from_fqnr\   §   s!   € ô ×ÑœG S§Y¡Y¨s£^°SÓ9Ð9r   c                 ó  — t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  i}| |v sJ dt        | «      z   «       ‚||    S )NzUnsupported dtype: )rH   Úquint8Úuint8Úqint8Úint8Úqint32Úint32Úquint4x2Úquint2x4Úuint16Úint16Úfloat8_e5m2Úfloat8_e4m3fnr7   )ÚqdtypeÚDTYPE_MAPPINGs     r   Úto_underlying_dtyperl   ®   s×   € ä�‰”e—k‘kÜ�‰”U—Z‘ZÜ�‰”e—k‘kÜ�‰œŸ™Ü�‰œŸ™Ü�‰”U—[‘[Ü�
‰
”E—J‘JÜ�‰”e—l‘lÜ�‰”U—[‘[Ü�‰”U—[‘[Ü×Ñœ5×,Ñ,Ü×ÑœU×0Ñ0ð€Mð �]Ñ"ÐGÐ$9¼CÀ»KÑ$GÓGÐ"Ø˜Ñ Ð r   c                 óô  — ddl m} t        | dd «      }| j                  }||dœ}|rt	        | |«      rd |dœS t        |«      rt        j                  }nLt        |«      r3|t        j                  k(  rt        j                  }| j                  |d<   nt        d|› �«      ‚||d<   | j                  «       \  }}||d<   ||d<   t        | d	«      r| j                  |d	<   t        | d
«      r| j                   |d
<   |S )Nr   )ÚPlaceholderObserverrL   )rL   ÚdtypeÚaxiszUnrecognized qscheme: ÚscaleÚ
zero_pointÚ	quant_minÚ	quant_max)Útorch.ao.quantization.observerrn   rZ   ro   Ú
isinstancerM   rH   rI   rR   rQ   rO   Úch_axisÚRuntimeErrorÚcalculate_qparamsÚhasattrrs   rt   )Úobserver_or_fake_quantrn   rL   ro   Úqparamsrq   rr   s          r   Úget_qparam_dictr}   Á   s  € ÝBäÐ,¨i¸Ó>€GØ"×(Ñ(€EØ!¨EÑ2€Gá”jÐ!7Ð9LÔMØ¨%Ñ0Ð0ä�WÔÜ×)Ñ)‰Ü	˜Ô	 ð ”e×1Ñ1Ò1Ü×.Ñ.ˆGØ0×8Ñ8ˆ�ŠäÐ3°G°9Ð=Ó>Ð>ð !€GˆIÑà.×@Ñ@ÓBÑ€Eˆ:Ø€GˆGÑØ&€GˆLÑäÐ% {Ô3Ø5×?Ñ?ˆ�ÑÜÐ% {Ô3Ø5×?Ñ?ˆ�Ñà€Nr   c                 óž   — t        |«      }|j                  |i «      }t        | «      |v sJ dt        | «      › d|› �«       ‚|t        | «         S )aÜ  Get the observed/quantized custom module class that we need
    to swap `custom_module` to
    Input:
        custom_module: input, can be an instance of either a float or observed custom module
        custom_module_class_mapping: the float to observed or observed to quantized custom module class mapping
        qconfig: qconfig configured for the custom module

    Output:
        corresponding observed/quantized custom module class for input custom module instance
    z5did not find corresponding observed module class for z in mapping: )Úget_quant_typeÚgetr6   )Úcustom_moduleÚcustom_module_class_mappingÚqconfigÚ
quant_typeÚclass_mappings        r   Úget_swapped_custom_module_classr†   å   sh   € ô   Ó(€JØ/×3Ñ3°JÀÓC€MÜ�Ó -Ñ/ð ð	Ü  Ó/Ð0°¸m¸_ð	NóÐ/ð œ˜mÓ,Ñ-Ð-r   c                 óB   — | €J ‚| j                  «       }|j                  S rG   )Ú
activationro   )rƒ   rˆ   s     r   Úactivation_dtyper‰   û   s(   € ØÐÐÐØ×#Ñ#Ó%€JØ×ÑÐr   c                 óB   — | €J ‚| j                  «       }|j                  S rG   )Úweightro   )rƒ   r‹   s     r   Úweight_dtyperŒ     s$   € ØÐÐÐØ�^‰^Ó€FØ�<‰<Ðr   c                 ód  — t        | «      t        j                  t        j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  f
v xr t        | «       S )z‘Given a qconfig, decide if the activation needs to be
    quantized or not, this includes quantizing to quint8, qint8 and qint32 and float16
    )r‰   rH   r^   r`   rb   Úfloat16r_   ra   rg   rc   rh   ri   Ú#activation_is_dynamically_quantized©rƒ   s    r   Ú"activation_is_statically_quantizedr‘     sw   € ô ˜GÓ$Ü�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�
‰
Ü�‰Ü�‰Ü×ÑÜ×Ñð)ð ò =ô 3°7Ó;Ð;ð=r   c                 ó$   — t        | «      \  }}}|S )z¢Given a qconfig, decide if the activation needs to be
    dynamically quantized or not, this includes dynamically quantizing to
    quint8, qint8 and float16
    )Úget_qconfig_dtypes)rƒ   Ú_activation_dtypeÚ_Úactivation_is_dynamics       r   r�   r�     s   € ô
 3EÀWÓ2MÑ/Ð�qÐ/Ø Ð r   c                 ó”   — t        | «      t        j                  t        j                  t        j                  t        j
                  fv S )z‚Given a qconfig, decide if the activation needs to be
    quantized to int8 or not, this includes quantizing to quint8, qint8
    )r‰   rH   r^   r`   r_   ra   r�   s    r   Úactivation_is_int8_quantizedr˜   "  s7   € ô ˜GÓ$Ü�‰Ü�‰Ü�‰Ü�
‰
ð	)ð ð r   c                 óX   — t        | «      t        j                  t        j                  fv S )zXGiven a qconfig, decide if the activation needs to be
    quantized to int32 or not
    )r‰   rH   rb   rc   r�   s    r   Úactivation_is_int32_quantizedrš   .  s!   € ô ˜GÓ$¬¯©´u·{±{Ð(CÐCÐCr   c                 óH  — t        | «      t        j                  t        j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  f
v S )zKGiven a qconfig, decide if the weight needs to be
    quantized or not
    )rŒ   rH   r^   r`   rŽ   rd   r_   ra   rg   rc   rh   ri   r�   s    r   Úweight_is_quantizedrœ   5  se   € ô ˜Ó Ü�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�
‰
Ü�‰Ü�‰Ü×ÑÜ×Ñð%ð ð r   c                 ó”   — t        | «      t        j                  t        j                  t        j                  t        j
                  fv S )zVGiven a qconfig, decide if the weight needs to be statically
    quantized or not
    )rŒ   rH   r^   r`   r_   ra   r�   s    r   Úweight_is_statically_quantizedrž   G  s-   € ô ˜Ó ¤U§\¡\´5·;±;ÄÇÁÌUÏZÉZÐ$XÐXÐXr   c                 ó´   — t        | «      \  }}}|t        j                  t        j                  fv xr& |t        j                  t        j
                  fv xr |S )zTGiven a qconfig, returns True if this op is using int8 dynamic
    quantization
    )r“   rH   r^   r_   r`   ra   )rƒ   r‰   rŒ   r–   s       r   Ú op_is_int8_dynamically_quantizedr    N  sX   € ô =OÈwÓ<WÑ9Ð�lÐ$9àœUŸ\™\¬5¯;©;Ð7Ð7ò 	"ð 	œŸ™¤e§j¡jÐ1Ð1ò	"ð "ðr   c                 ó–   — | €J ‚| j                  «       }| j                  «       }t        |dd«      }|j                  |j                  |fS )zgreturns the qconfig tuple for qconfig:
    (activation_dtype, weight_dtype, activation_is_dynamic)
    Ú
is_dynamicF)rˆ   r‹   rZ   ro   )rƒ   rˆ   r‹   Úact_is_dynamics       r   r“   r“   \  sO   € ð ÐÐÐØ×#Ñ#Ó%€JØ�^‰^Ó€FÜ˜Z¨°uÓ=€NØ×Ñ˜fŸl™l¨NÐ;Ð;r   c           
      ór  — | €J ‚| j                  «       }| j                  «       }t        j                  t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  g
}|j                  |v rVt        |d«      r|j                  rt         j"                  S |j                  |v rt         j$                  S t         j&                  S |j                  t        j(                  k(  rUt        |d«      r|j                  rt         j"                  S |j                  t        j(                  k(  rt         j$                  S t+        d|j                  › d|j                  › d�«      ‚)Nr¢   z=Unrecognized dtype combination in get_quant_type: activation(z	),weight(ú))rˆ   r‹   rH   r^   r`   rd   rb   r_   ra   rg   rc   rh   ri   ro   rz   r¢   r
   ÚDYNAMICÚSTATICÚWEIGHT_ONLYrŽ   Ú	Exception)rƒ   rˆ   r‹   Ústatic_dtypess       r   r   r   g  sH  € ØÐÐÐØ×#Ñ#Ó%€JØ�^‰^Ó€Fä�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�
‰
Ü�‰Ü�‰Ü×ÑÜ×Ñð€Mð ‡|�|�}Ñ$Ü�:˜|Ô,°×1FÒ1FÜ×$Ñ$Ð$Ø×Ñ Ñ.Ü×#Ñ#Ð#ä×(Ñ(Ð(à‡|�|”u—}‘}Ò$Ü�:˜|Ô,°×1FÒ1FÜ×$Ñ$Ð$Ø×Ñ¤§¡Ò.Ü×#Ñ#Ð#ä
Ø
GÈ
×HXÑHXÐGYð ZØ—,‘,�˜qð	"óð r   Úmin_valÚmax_valc                 óœ  — | j                  «       dk(  s|j                  «       dk(  rt        j                  d«       y| j                  «       dk(  s|j                  «       dk(  rE| t	        d«      k(  r$|t	        d«      k(  rt        j                  d«       y| |k  sJ d| › d|› �«       ‚yt        j                  | |k  «      sJ d| › d|› �«       ‚y)	zˆChecks if the given minimum and maximum values are valid, meaning that
    they exist and the min value is less than the max value.
    r   zMmust run observer before calling calculate_qparams. Returning default values.FÚinfz-infzmin z should be less than max T)ÚnumelÚwarningsÚwarnÚdimÚfloatrH   Úall)r«   r¬   s     r   Úcheck_min_max_validrµ   ‹  sÞ   € ð ‡}�}ƒ˜!Ò˜wŸ}™}›°!Ò3Ü�‰ð*ô	
ð à‡{�{ƒ}˜Ò˜WŸ[™[›]¨aÒ/Ø”e˜E“lÒ" w´%¸³-Ò'?Ü�M‰Mð.ôð
 à˜'Ò!ÐU T¨'¨Ð2KÈGÈ9Ð#UÓUÐ!ð ô	 �y‰yØ�wÑô
ð 	>à�'�Ð3°G°9Ð=ó	>ð 
ð r   rs   rt   Úhas_customized_qrangero   Úreduce_rangec                 óH  — |rÉ|t         j                  t         j                  fv rd\  }}nd\  }}| |}}|�|�||}}||z
  dz   }	|t         j                  t         j                  fv rd|	cxk  rdk  sKJ d«       ‚ J d«       ‚|t         j                  t         j                  fv rd|	cxk  rdk  sJ d«       ‚ J d«       ‚|r
| d	z  |d	z  }} | |fS |t         j                  t         j                  fv r|r	d
\  } }| |fS d\  } }| |fS |t         j
                  t         j                  fv r|r	d\  } }| |fS d\  } }| |fS |t         j                  t         j                  fv r	d\  } }| |fS |t         j                  fv r	d\  } }| |fS |t         j                  fv r	d\  } }| |fS d\  } }| |fS )ztCalculates actual qmin and qmax based on the quantization range,
    observer datatype and if range is reduced.
    )r   l   ÿÿ )r   éÿ   é   r   é   zRquantization range should be positive and not exceed the maximum bit range (=256).l        zYquantization range should be positive and not exceed the maximum bit range (=4294967296).é   )iÀÿÿÿé?   )i€ÿÿÿé   )r   r¾   )i   €iÿÿÿ)r   iÿÿ  )i €ÿÿiÿ  )r   é   )	rH   rb   rc   r`   ra   r^   r_   rf   rg   )
rs   rt   r¶   ro   r·   Úinitial_quant_minÚinitial_quant_maxÚcustom_quant_minÚcustom_quant_maxÚ
qrange_lens
             r   Úcalculate_qmin_qmaxrÅ   ¨  sC  € ñ ð ”U—\‘\¤5§;¡;Ð/Ñ/Ø3?Ñ0ÐÑ0à39Ñ0ÐÐ0ð .7¸	Ð*ÐØÐ'Ð,<Ð,Hà Ø ð  1Ðð
 'Ð):Ñ:¸QÑ>ˆ
Ø”U—[‘[¤%§*¡*Ð-Ñ-à�JÔ% #Ò%ðdàcódÙ%ðdàcódØ%à”u—|‘|¤U§[¡[Ð1Ñ1à�JÔ' %Ò'ðkàjókÙ'ðkàjókØ'áØ#,°¡>°9À±>�yˆIð* �iÐÐð% ”U—[‘[¤%§*¡*Ð-Ñ-ÙØ'.Ñ$�	˜9ð  �iÐÐð (1Ñ$�	˜9ð �iÐÐð ”u—|‘|¤U§[¡[Ð1Ñ1ÙØ'-Ñ$�	˜9ð �iÐÐð (.Ñ$�	˜9ð �iÐÐð ”u—|‘|¤U§[¡[Ð1Ñ1Ø#<Ñ ˆI�yð �iÐÐð ”u—|‘|�nÑ$Ø#/Ñ ˆI�yð
 �iÐÐð	 ”u—{‘{�mÑ#Ø#6Ñ ˆI�yð �iÐÐð $)Ñ ˆI�yØ�iÐÐr   c                 ód   — | j                  dd«      }t        |«      dk(  rd|d   fS |d   |d   fS )z,
    Turn 'foo.bar' into ['foo', 'bar']
    rW   rº   Ú r   )ÚrsplitÚlen)r3   Úrs     r   Ú_parent_namerË   æ  s>   € ð 	�‰�c˜1Ó€AÜ
ˆ1ƒv�‚{Ø�1�Q‘4ˆxˆà�‰t�Q�q‘TˆzÐr   c                 óœ   — t        | j                  «      dk(  ryt        | «      r(t        | j                  «      dk(  xr d| j                  v S y)zš
    Checks if module._modules is empty or
    if module is a parametrization, checks that module._modules only has
    the 'parametrizations' module
    r   Trº   ÚparametrizationsF)rÉ   Ú_modulesr   )Úmodules    r   Ú)has_no_children_ignoring_parametrizationsrÐ   ñ  sF   € ô ˆ6�?‰?Ó˜qÒ ØÜ	˜Ô	 Ü�6—?‘?Ó# qÑ(ÒRÐ-?À6Ç?Á?Ð-RÐRàr   ÚrootÚ	submodulec                 óD   — | j                  «       D ]  \  }}||u sŒ|c S  y)aZ  Get the path (fully qualified name) of a submodule

    Example::

    >> class M(torch.nn.Module):
           def __init__(self) -> None:
               self.linear = torch.nn.Linear(5, 5)
           def forward(self, x):
               return self.linear(x)

    >> m = M()
    >> l = m.linear
    >> _get_path_of_module(m, l)
    "linear"
    N)Únamed_modules)rÑ   rÒ   ÚnÚps       r   Ú_get_path_of_moduler×   ÿ  s1   € ð$ ×"Ñ"Ó$ò ‰ˆˆ1Ø˜Š>ØŠHðð r   ÚfÚlocc                 ó†   — |j                  «       D ��ci c]   \  }}|t        | «      j                  v sŒ||“Œ" c}}S c c}}w )z¬Get local keyword arguments

    Example::

    >> def f(self, a, b=9):
           pass
    >> loc = {"a": 6, "c": 7}
    >> _get_signature_locals(f, loc)
    {"a": 6}
    )Úitemsr   Ú
parameters)rØ   rÙ   ÚkÚvs       r   Ú_get_signature_localsrß     s7   € ð !ŸY™Y›[×I‘T�Q˜¨A´¸1³×1HÑ1HÒ,HˆAˆq‰DÓIÐIùÓIs   ”=´=zOrderedDict[str, Any]c                 ó:  — i }t        | «      j                  j                  «       D ]i  \  }}|j                  |j                  ur|j                  ||<   Œ.|j
                  |j                  u rd||<   ŒL|j
                  |j                  u sŒei ||<   Œk t        |«      S )z¥Get all default keyword arguments from function signature

    Example::

    >> def f(self, a, b=9):
           pass
    >> _get_default_kwargs(f)
    {"b": 9}
    r   )	r   rÜ   rÛ   ÚdefaultÚemptyÚkindÚVAR_POSITIONALÚVAR_KEYWORDr   )rØ   ÚkwargsÚnameÚparams       r   Ú_get_default_kwargsré   %  s�   € ð €FÜ  “|×.Ñ.×4Ñ4Ó6ò ‰ˆˆeØ�=‰= §¡Ñ+Ø Ÿ=™=ˆF�4ŠLØ�Z‰Z˜5×/Ñ/Ñ/ØˆF�4ŠLØ�Z‰Z˜5×,Ñ,Ò,ØˆF�4ŠLðô �vÓÐr   Úfuncc                 ó˜   — t        | «      }t        | |«      }|j                  «       }|j                  «       D ]  \  }}||v sŒ|||<   Œ |S )a(  Given a function and local function arguments, normalize the keyword
    arguments by filling in default arguments from function signature

    Example::

    >> def f(self, key1=3, key2=3):
           pass
    >> loc = {"key2": 6}
    >> _normalize_kwargs(f, loc)
    {"key1": 3, "key2": 6}
    )ré   rß   r@   rÛ   )rê   rÙ   Údefault_kwargsÚlocal_kwargsÚnormalized_kwargsÚattrÚvals          r   Ú_normalize_kwargsrñ   :  sa   € ô )¨Ó.€NÜ(¨¨sÓ3€LØ&×+Ñ+Ó-ÐØ!×'Ñ'Ó)ò *‰	ˆˆcØÐ$Ò$à&)Ð˜dÒ#ð*ð Ðr   c                 óP   — | dcxk  r|k  sJ d«       ‚ J d«       ‚| |k  sJ d«       ‚y)ae  Validates that the user-specified quantization range is properly initialized
    and within the given bound supported by the observer dtype.

    To accommodate lower-bit quantization with respect to the existing torch.qint8 and
    torch.quint8 datatypes, the user can choose to use dynamic quantization range by passing
    in a tuple of initial qmin and qmax values. One use case is these customized qmin and qmax
    values are used to calculate static estimates of the scale and zero point for aggressive lower-bit
    fake quantization. These estimates are compared against parameters learned through backpropagation.
    The related literatures for scale and zero point via backpropagation are as follows:

    Learned Step Size Quantization: https://openreview.net/pdf?id=rkgO66VKDS
    Trained Quantization Thresholds: https://arxiv.org/pdf/1903.08066.pdf
    r   z1Used-specified quantization range must include 0.zKqmin must be strictly less than qmax for user-specified quantization range.Nr   )rs   rt   s     r   Úvalidate_qmin_qmaxró   P  sM   € ð" 	�QÔ#˜)Ò#ð;à:ó;Ù#ð;à:ó;Ø#ð 	�IÒðUàTóUÙr   ÚepsrL   c                 óÖ  — t        | |«      sXt        j                  dg| j                  j                  ¬«      t        j                  dg| j                  j                  ¬«      fS t        j
                  | t        j                  | «      «      }t        j                  |t        j                  |«      «      }	|j                  }
t        j                  |j                  «       t        j                  |
¬«      }t        j                  |j                  «       t        j                  |
¬«      }|j                  |
«      }|t        j                  k(  s|t        j                  k(  r®t        j                  | |	«      }	|	t!        ||z
  «      dz  z  }t        j                  ||«      }|t        j"                  t        j$                  fv �r |r'|j'                  |j                  «       ||z   dz  «      }n÷|j'                  |j                  «       d«      }nÖ|t        j(                  k(  rJ|| z
  t!        ||z
  «      z  }t        j*                  ||kD  |t        j,                  |«      «      }d| z  |z  }ny|	|z
  t!        ||z
  «      z  }t        j                  ||«      }|t        j.                  ||z  «      j                  t        j0                  «      z
  }t        j2                  |||«      }t5        |j6                  «      dk(  r,t        j                  t!        |«      g|j8                  |
¬«      }t5        |j6                  «      dk(  rkt        j                  t1        |«      g|j8                  |
¬«      }|t        j(                  k(  r,t        j                  t!        |«      g|j8                  |
¬«      }|j                  t        j                  «      |j                  t        j                  «      fS )ad  Calculates the quantization parameters, given min and max
    value tensors. Works for both per tensor and per channel cases

    Args:
        min_val: Minimum values per channel
        max_val: Maximum values per channel

    Returns:
        scales: Scales tensor of shape (#channels,)
        zero_points: Zero points tensor of shape (#channels,)
    g      ð?)Údevicer   )ro   rö   r¼   é€   éÿÿÿÿ)rµ   rH   Útensorrö   r6   ÚminÚ
zeros_likeÚmaxÚonesr%   ÚdoubleÚzerosÚint64ÚtorJ   rQ   r³   r_   r^   Únew_fullrP   ÚwhereÚ	ones_likeÚroundÚintÚclamprÉ   r"   ro   )r«   r¬   rs   rt   ro   rô   r¶   rL   Úmin_val_negÚmax_val_posrö   rq   rr   s                r   Údetermine_qparamsr
  l  s  € ô* ˜w¨Ô0Ü�|‰|˜S˜E¨'¯.©.×*=Ñ*=Ô>ÄÇÁØˆC˜Ÿ™×+Ñ+ôA
ð 
ð 	
ô —)‘)˜G¤U×%5Ñ%5°gÓ%>Ó?€KÜ—)‘)˜G¤U×%5Ñ%5°gÓ%>Ó?€Kà×Ñ€FÜ�J‰J�{×'Ñ'Ó)´·±ÀfÔM€EÜ—‘˜[×-Ñ-Ó/´u·{±{È6ÔR€JØ
�&‰&�‹.€Cà”%×,Ñ,Ò,°¼5×;VÑ;VÒ0VÜ—i‘i  ¨kÓ:ˆØœu Y°Ñ%:Ó;¸aÑ?Ñ@ˆÜ—	‘	˜% Ó%ˆØ”U—[‘[¤%§,¡,Ð/Ò/Ù$à'×0Ñ0Ø—O‘OÓ%¨	°IÑ(=À!Ñ'Có‘
ð (×0Ñ0°·±Ó1BÀCÓH‘
Ø	”E×:Ñ:Ò	:Ø˜7Ñ"¤e¨I¸	Ñ,AÓ&BÑBˆÜ—‘˜E C™K¨´·±ÀÓ0FÓGˆð
 ˜'‘\ EÑ)‰
à˜{Ñ*¬e°IÀ	Ñ4IÓ.JÑJˆÜ—	‘	˜% Ó%ˆØ¤§¡¨[¸5Ñ-@Ó!A×!DÑ!DÄUÇYÁYÓ!OÑOˆ
Ü—[‘[ ¨Y¸	ÓBˆ
ô ˆ5�;‰;Ó˜1Òä—‘œe E›l˜^°5·;±;ÀvÔNˆÜ
ˆ:×ÑÓ Ò!ä—\‘\Ü�‹_Ð Z×%5Ñ%5¸fô
ˆ
ð ”e×<Ñ<Ò<ÜŸ™Ü�zÓ"Ð#¨:×+;Ñ+;ÀFôˆJð �8‰8”E—L‘LÓ! :§=¡=´·±Ó#=Ð=Ð=r   c                 ó>   — t        t        | «      j                  «      S )z—Get number of positional args for a function

    Example::

    >> def f(self, key1=3, key2=3):
           pass
    >> _get_num_pos_args(f)
    3
    )rÉ   r   Úargs)rØ   s    r   Ú_get_num_pos_argsr  º  s   € ô Œ~˜aÓ ×%Ñ%Ó&Ð&r   ÚmodelÚexample_inputs.c                 ó:  ‡‡‡— | Ši Šˆˆˆfd„}t         j                  j                  j                  Š|t         j                  j                  _        	  | |Ž  ‰t         j                  j                  _        ‰S # ‰t         j                  j                  _        w xY w)a  Given a model and its example inputs, return a dictionary from
    fully qualified name of submodules to example_inputs for that submodule,
    e.g. {"linear1": (tensor1,), "linear2": (tensor2,), "sub": (tensor3,),
          "sub.linear1": (tensor4,), ...}

    Used to make quantizing submodules easier now that FX Graph Mode Quantization requires
    example inputs.

    Also works for keyword arguments with default values, we would flatten keyword
    arguments as positional arguments and fill in the missing keyword args with default
    values, e.g. if we have a forward function:
    def forward(self, x, key1=3, key2=3):
        ...

    and we call it with self.submodule(x, key2=6)
    we'll get example_inputs: (x, 3, 6)

    user can also override `key1` with positional arguments as well:
    for self.submodule(x, 5, key2=6)
    we'll get: (x, 5, 6)

    variable positional arguments and variable positional keyword arguments in forward
    function are not supported currently, so please make sure no submodules is using
    them.
    c                 ó€  •— t        |«      j                  «       }t        | j                  |«      }t	        | j                  «      dz
  }|t        |«      z
  }|r|r|j                  d¬«       |dz  }|r|rŒ|j                  |j                  «       «       t        |«      }t        ‰| «      }|�|‰	|<    ‰
| g|¢­i |¤ŽS )Nrº   F)Úlast)Úlistr@   rñ   Úforwardr  rÉ   ÚpopitemÚextendÚvaluesÚtupler×   )Úselfr  ræ   Úsubmodule_example_inputsrî   Únum_argsÚ
num_to_popÚsubmodule_example_inputs_tuplerT   Úfqn_to_example_inputsÚorig_module_callrÑ   s            €€€r   Ú_patched_module_callz7get_fqn_to_example_inputs.<locals>._patched_module_callæ  sÇ   ø€ Ü#'¨£:§?¡?Ó#4Ð Ü-¨d¯l©l¸FÓCÐä$ T§\¡\Ó2°QÑ6ˆØ¤Ð$<Ó =Ñ=ˆ
ÙÑ.Ø×%Ñ%¨5Ð%Ô1Ø˜!‰OˆJñ Ò.ð 	!×'Ñ'Ð(9×(@Ñ(@Ó(BÔCÜ).Ð/GÓ)HÐ&Ü! $¨Ó-ˆØˆ?Ø)GÐ! #Ñ&Ù Ð6 tÒ6¨vÑ6Ð6r   )rH   ÚnnÚModuleÚ__call__)r  r  r   r  r  rÑ   s      @@@r   Úget_fqn_to_example_inputsr$  Ç  st   ú€ ð8 €DØÐö7ô  —x‘x—‘×/Ñ/ÐØ3„E‡H�H‡O�OÔð4Ùˆ~Ñð $4Œ�‰�‰Ô Ø Ð øð $4Œ�‰�‰Õ ús   ÁA9 Á9!BrÏ   c                 óì  — | j                  «       D �ch c]  }|j                  ’Œ c}| j                  «       D �ch c]  }|j                  ’Œ c}z  }	 t        j                  d«      t        j                  d«      h|k(  r+t	        j
                  d«       t        j                  d«      h}	 t        |«      dk  s
J d|› �«       ‚t        |«      dkD  rt        t        |«      «      }|S d}|S c c}w c c}w )z†
    Returns the unique device for a module, or None if no device is found.
    Throws an error if multiple devices are detected.
    ÚcpuÚmetazfBoth 'meta' and 'cpu' are present in the list of devices. Module can have one device. We Select 'cpu'.rº   zKprepare only works with cpu or single-device CUDA modules, but got devices r   N)	rÜ   rö   ÚbuffersrH   r°   r±   rÉ   ÚnextÚiter)rÏ   rÖ   Údevicesrö   s       r   Ú_assert_and_get_unique_devicer,     sí   € ð
 "(×!2Ñ!2Ó!4Ö5˜Aˆq�x‹xÒ5Ø Ÿ.™.Ó*ö9Øˆ�‹ò9ñ €Gðô 	�‰�UÓœUŸ\™\¨&Ó1Ð2°gÒ=Ü�‰Øtô	
ô —<‘< Ó&Ð'ˆØÜˆw‹<˜1Òð ð	Ø"˜)ð	%óÐô %(¨£L°1Ò$4ŒT”$�w“-Ó €FØ€Mð ;?€FØ€Mùò# 6ùò 9s
   “C,ºC1)ÚNodePatternÚPatternr   r>   rE   rM   rR   r\   r}   r†   r‰   rŒ   r‘   r�   r˜   rš   rœ   rž   r    r“   r   rµ   rÅ   rÐ   r$  rl   r
  ró   )tr   rX   r°   Úcollectionsr   Úinspectr   r   Útypingr   r   r   r	   rH   Ú torch.ao.quantization.quant_typer
   Útorch.fxr   Útorch.nn.utils.parametrizer   r  r-  r   ÚQuantizerClsr.  r   r!  ÚReLUÚReLU6ÚAdaptiveAvgPool1dÚAdaptiveAvgPool2dÚAdaptiveAvgPool3dÚ	AvgPool1dÚ	AvgPool2dÚ	AvgPool3dÚ	MaxPool1dÚ	MaxPool2dÚ	MaxPool3dÚIdentityÚHardsigmoidÚSigmoidÚTanhr8   Ú
functionalÚadaptive_avg_pool1dÚadaptive_avg_pool2dÚadaptive_avg_pool3dÚeluÚ	hardswishÚinstance_normÚ
layer_normÚ
leaky_reluÚsiluÚmishÚdropoutÚ
max_pool1dÚ
max_pool2dÚ
max_pool3dr   ÚhardtanhÚ	hardtanh_r   r#   r*   r   r&   ÚstackÚsumr(   r+   Úcatr4   Úmeanr5   r>   rE   rM   rR   r7   r\   rl   r}   r†   r‰   rŒ   r‘   r�   r˜   rš   rœ   rž   Úboolr    r“   r   ÚTensorrµ   r  ro   rÅ   rË   rÐ   r"  r×   Údictrß   ré   rñ   ró   rI   rL   r
  r  r$  r,  Ú__all__r   r   r   ú<module>r^     s<  ðñó Û Ý #ß -ß 1Ó 1ã Ý 6Ý Ý 6ð �E˜$ ˜*Ñ% u¨T°5¸¸t¸Ñ3DÐ-DÑ'EÀsÐJÑK€Ø6€Ô ð €ð
 Øˆe�H˜hÐ&Ñ'¨¨x¸¸xÈÐ?QÑ9RÐ/RÑ)SÐUXÐXñ€ð 3€Ô ÷ñ ð 
‡H�H‡M�MØ	‡H�H‡N�NØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H×ÑØ	‡H�H‡M�MðÐ ð$ 
‡H�H×Ñ×+Ñ+Ø	‡H�H×Ñ×+Ñ+Ø	‡H�H×Ñ×+Ñ+Ø	‡H�H×Ñ×ÑØ	‡H�H×Ñ×!Ñ!Ø	‡H�H×Ñ×%Ñ%Ø	‡H�H×Ñ×"Ñ"Ø	‡H�H×Ñ×"Ñ"Ø	‡H�H×Ñ×ÑØ	‡H�H×Ñ×ÑØ	‡H�H×Ñ×ÑØ	‡H�H×Ñ×"Ñ"Ø	‡H�H×Ñ×"Ñ"Ø	‡H�H×Ñ×"Ñ"Ø	‡H�H×Ñ×ÑØ	‡H�H×Ñ× Ñ Ø	‡H�H×Ñ×!Ñ!Ø	‡H�H×Ñ×#Ñ#Ø	‡H�H×Ñ×ÑØ	‡O�OØ	×ÑØ	‡M�MØ	‡M�MØ	‡K�KØ	‡I�IØ	‡J�JØ	‡O�OØ	‡I�Ið9€	ð> 
‡J�JØ
ØØØØØØØØØØØØØØØ
ØØØ
ØØØØØ
ð3€ò<<òò2Wòð:˜#ð : Cð :¨Có :ò!ò&!òH.ò,òò=ò$!ò	òDòò$Yð°ó ò<ò!ðH §¡ð ¸¿¹ð Èó ð:; Øð; àð; ð  ð; ð �;‰;ð	; ð
 ð; ð ˆ3�ˆ8�_ó; ò|òðØ
�(‰(�/‰/ðØ&+§h¡h§o¡oðàˆc�]óð0J˜Xð J¨D°°c°©Nð J¸tÀCÈÀH¹~ó Jð˜8ð Ð(?ó ð*˜Hð ¨4°°S°©>ð Ð>Uó ð,U #ð U°#ð U¸$ó UðH #×4Ñ4ñK>Ø�\‰\ðK>à�\‰\ðK>ð ðK>ð ð	K>ð
 �;‰;ðK>ð 
�‰ðK>ð  ðK>ð �]‰]ðK>ð ˆ5�<‰<˜Ÿ™Ð%Ñ&óK>ð\
'˜ð 
' có 
'ð6!Ø�8‰8�?‰?ð6!Ø,1°#°s°(©Oð6!à	ˆ#ˆu�S˜#�X‰Ð
Ñó6!ðr¨%¯(©(¯/©/ð ¸có ò2�r   