ó
    EñiX-  ã                   óð  • S SK r S SKrS SKrS SKrS SKJrJr  S SKJr  S SK	J
r
JrJr  S SKrS SKJr  S SKJr  \R$                  " \5      r " S S\5      r\" S	S
9 " S S5      5       r\" S	S
9 " S S5      5       rS\S\\   S\4S jr\ R8                  S\\\\   4   S\4S j5       rS\4S jrS\\\\   4   S\4S jr S\\\\   4   S\SS4S jr!S\S\\\\   4   4S jr"S\S\\\\   4   4S jr#g)é    N)ÚCallableÚ	Generator)Ú	dataclass)ÚAnyÚOptionalÚUnion)Ú_maybe_get_opdef)ÚFileLikec                   ó   • \ rS rSrSrSrg)ÚMissingOpProfileé   zW
This is raised when we don't have an operator profile available for the
given inputs.
© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__static_attributes__r   ó    ÚX/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/_library/fake_profile.pyr   r      s   † ôr   r   T)Úfrozenc                   óœ   • \ rS rSr% \\S'   \R                  \S'   \R                  \S'   \R                  \S'   \
S\S\S    4S j5       rS	rg
)ÚTensorMetadataé   ÚrankÚdtypeÚdeviceÚlayoutÚtÚreturnc                 ó¶   • [        U [        R                  5      (       d  g [        U R	                  5       U R
                  U R                  U R                  5      $ ©N)Ú
isinstanceÚtorchÚTensorr   Údimr   r   r   )r   s    r   Úmaybe_from_tensorÚ TensorMetadata.maybe_from_tensor   s9   € ä˜!œUŸ\™\×*Ñ*ØÜ˜aŸe™e›g q§w¡w°·±¸!¿(¹(ÓCÐCr   r   N)r   r   r   r   ÚintÚ__annotations__r$   r   r   r   Ústaticmethodr   r   r'   r   r   r   r   r   r      sO   ‡ à
ƒIØ�;‰;ÓØ�L‰LÓØ�L‰LÓàðD˜Sð D XÐ.>Ñ%?ó Dó óDr   r   c                   óF   • \ rS rSr% \\\      \S'   \\\\   4   \S'   Sr	g)Ú	OpProfileé&   Úargs_profileÚout_profiler   N)
r   r   r   r   Útupler   r   r*   r   r   r   r   r   r-   r-   &   s'   ‡ à˜ Ñ0Ñ1Ó1Ø�~ u¨^Ñ'<Ð<Ñ=Ö=r   r-   Úop_nameÚ
op_profiler    c                 ó  ^ ^^^• S[         [        [              S[        S[        4S jmS[
        [        [         [           4   S[
        [        R                  [        [        R                     4   4S jmUUU U4S jnU$ )Nr/   Úargsr    c                 ó@   ^ • [        U 4S j[        U5       5       5      $ )Nc              3   ó^   >#   • U  H"  u  p[         R                  U5      TU   :H  v •  M$     g 7fr"   )r   r'   )Ú.0ÚiÚargr/   s      €r   Ú	<genexpr>Ú=_generate_fake_kernel.<locals>._match_args.<locals>.<genexpr>.   s.   øé € ð 
â)‘�ô ×,Ñ,¨SÓ1°\À!±_ÖDÚ)ùs   ƒ*-)ÚallÚ	enumerate)r/   r5   s   ` r   Ú_match_argsÚ*_generate_fake_kernel.<locals>._match_args-   s!   ø€ Üô 
ä# Dœ/ó
ó 
ð 	
r   r0   c                 óð   ^• [         R                  R                  5       mS[        S[         R                  4U4S jjn[        U [        5      (       a  U" U 5      $ U  Vs/ s H
  o!" U5      PM     sn$ s  snf )Nr   r    c                 óV  >• [        U R                  5       Vs/ s H  nTR                  5       PM     nnS/U R                  -  nSnUn[        U R                  5       H  nXSU'   XRU   -  nM     [        R                  " UUU R
                  U R                  U R                  S9$ s  snf )Néÿÿÿÿé   )r   r   r   )Úranger   Únew_dynamic_sizer$   Úempty_stridedr   r   r   )r   Ú_Ú
fake_shapeÚfake_stridesÚexpectedÚfake_strider9   Úctxs          €r   Ú_generate_tensor_outÚJ_generate_fake_kernel.<locals>._generate_res.<locals>._generate_tensor_out8   sœ   ø€ Ü:?ÀÇÁ¼-ÓHº-°Q˜#×.Ñ.Ö0¹-ˆJÐHØ˜4 !§&¡&™=ˆLØˆHØ"ˆKä˜1Ÿ6™6–]�Ø"-˜Q‘Ø)°q©MÑ9’ñ #ô ×&Ò&ØØØ—x‘xØ—g‘gØ—x‘xñð ùò Is   ™B&)r$   ÚlibraryÚget_ctxr   r%   r#   )r0   rN   r   rM   s      @r   Ú_generate_resÚ,_generate_fake_kernel.<locals>._generate_res3   sf   ø€ ô �m‰m×#Ñ#Ó%ˆð	¤Nð 	´u·|±|÷ 	ô$ �k¤>×2Ñ2Ù'¨Ó4Ð4á5@ÓA²[°Ð(¨Ö+±[ÑAÐAùÒAs   ÁA3c                  óº   >• T HB  nT" UR                   / U QUR                  5       Q75      (       d  M0  T" UR                  5      s  $    [        ST SX4 S35      e)NzNo fake kernel was found for zz, and although we have previously registered some profiles to generate a fake kernel, no profiles match the given inputs: Ú.)r/   Úvaluesr0   r   )r5   ÚkwargsÚprofilerR   r?   r2   r3   s      €€€€r   Ú_fake_kernelÚ+_generate_fake_kernel.<locals>._fake_kernelO   sl   ø€ Û!ˆGÙ˜7×/Ñ/Ð1J°4Ð1J¸&¿-¹-»/Ñ1J×KÓKÙ$ W×%8Ñ%8Ó9Ò9ñ "ô Ø+¨G¨9ð 53à37°<°.ÀðCó
ð 	
r   )	r1   r   r   r   Úboolr   r$   r%   Úlist)r2   r3   rY   rR   r?   s   `` @@r   Ú_generate_fake_kernelr]   ,   sx   û€ ð
¤%¬´Ñ(@Ñ"Að 
Ìð 
ÔQUô 
ðBÜœ>¬5´Ñ+@Ð@ÑAðBä	Œu�|‰|œT¤%§,¡,Ñ/Ð/Ñ	0ôB÷8	
ð 	
ð Ðr   Úop_profilesc              #   ó~  #   • / n0 nU R                  5        H×  u  p4[        R                  SU5        UR                  S5      nUS   US   pvU SU 3n[	        X„5      n	[        U5      =n
(       a.  U
R                  b  U
R                  X('   U
R                  U	5        M‡  [        R                  R                  US5      n[        R                  R                  X‰USS	9  UR                  U5        MÙ      Uv •  U H  nUR                  5         M     UR                  5        H3  u  p�[        U5      n
U
c  [        S
U S35      eU
R                  U5        M5     g! U H  nUR                  5         M     UR                  5        H3  u  p�[        U5      n
U
c  [        S
U S35      eU
R                  U5        M5     f = f7f)a­  
Registers a fake kernel based on the given operator profiles. This fake
kernel registration will override any existing fake kernel registrations.

The input is a dictionary mapping operator names to a set of operator
profiles, which we will use to generate fake kernels. The operator profiles
are a record of the input and output tensor metadata. Based on this
information we will match a given input to the recorded profile, and return
an output with the same metadata as in the recorded profile. If a profile
doesn't exist then an exception will be thrown.

The fake kernel generation is considered unsafe because it relies on the
rigid, pre-defined operator profiles that do not account for potential
variations in output behavior. Specifically, the generated kernels assume a
fixed relationship between input and output ranks. However, in reality, it's
possible that data-dependent operations may produce outputs of different
ranks even when given inputs of the same rank. The generated fake kernels
are inflexible and unable to accommodate these nuances, making them
potentially unsafe.

Args:
    op_profiles (dict[str, set[OpProfile]]): A dictionary mapping operator
        name to a set of operator profiles from which we will generate fake
        kernels.

Examples:

    >>> # Example: Registering an op-profile from draft-export
    >>> import torch
    >>> from torch.export._draft_export import draft_export
    >>>
    >>> @torch.library.custom_op("mylib::foo", mutates_args=())
    >>> def foo(x: Tensor, y: Tensor) -> Tensor:
    >>>     return x + y
    >>>
    >>> class M(torch.nn.Module):
    >>>     def forward(self, a, b):
    >>>         res = torch.ops.mylib.foo(a, b)  # no fake impl
    >>>         return res
    >>>
    >>> ep = draft_export(M(), (torch.ones(3, 4), torch.ones(3, 4))
    >>>
    >>> with torch._library.fake_profile.unsafe_generate_fake_kernels(ep._report.op_profiles):
    >>>     decomp = ep.run_decompositions()

zZRegistering fake profile for %s. This will override any existing fake kernel registration.rU   r   rD   z::NÚFRAGMENTT)ÚlibÚallow_overridez
opdef for z must not be None)ÚitemsÚlogÚwarningÚsplitr]   r	   Ú_abstract_fnÚregister_faker$   rP   ÚLibraryÚappendÚ_destroyÚAssertionError)r^   ÚlibsÚold_fake_implsr2   ÚprofilesÚop_name_splitÚ	namespaceÚop_name_strÚop_strÚfake_kernelÚopdefÚnewlibra   Úold_fakes                 r   Úunsafe_generate_fake_kernelsrx   ]   s¾  é € ðb )+€Dà*,€NØ(×.Ñ.Ö0ÑˆÜ�‰ð(àô	
ð  Ÿ™ cÓ*ˆØ!.¨qÑ!1°=ÀÑ3C�;Ø�;˜b  Ð.ˆä+¨FÓ=ˆä$ VÓ,Ð,ˆ5Õ,ð ×!Ñ!Ñ-Ø).×);Ñ);�Ñ&Ø×Ñ Ö,ô —]‘]×*Ñ*¨9°jÓAˆFÜ�M‰M×'Ñ'Ø¨Àð (ñ ð �K‰K˜Öñ; 1ð>*ØŠ
ó ˆCØ�L‰LŽNñ ð !/× 4Ñ 4Ö 6ÑˆFÜ$ VÓ,ˆEØ‰}Ü$ z°&°Ð9JÐ%KÓLÐLØ×Ñ Ö)ò	 !7øó	 ˆCØ�L‰LŽNñ ð !/× 4Ñ 4Ö 6ÑˆFÜ$ VÓ,ˆEØ‰}Ü$ z°&°Ð9JÐ%KÓLÐLØ×Ñ Ö)ò	 !7üs    ‚C0F=Ã3E Ã7A!F=ÅA"F:Æ:F=c                  ó~   • [         R                  R                  S5      n [        U S   5       S[        U S   5       3$ )NrU   r   rD   )r$   Ú__version__rf   r)   )Úversions    r   Úget_torch_versionr|   À   s;   € Ü×Ñ×%Ñ% cÓ*€GÜ�'˜!‘*‹oÐ˜a¤ G¨A¡J£Ð0Ð1Ð1r   c                 ó8  ^^^	• SSK nSSKJmJm  S[        S[
        4UU4S jjm	S[        S[
        4U	4S jjnU R                  5        VVVs0 s H  u  p4X4 Vs/ s H
  oR" U5      PM     sn_M     nnnnUR                  [        5       US	.S
S9$ s  snf s  snnnf )z¿
Generates a yaml string from the given operator profiles which can be saved
to a file. The yaml string can be loaded back into an operator profile
structure using `read_profiles_from_yaml`.
r   N)Ú_TORCH_TO_SERIALIZE_DTYPEÚ_TORCH_TO_SERIALIZE_LAYOUTr   r    c                 ó¨   >• U R                   TU R                     R                  [        U R                  5      TU R
                     R                  S.$ )N©r   r   r   r   )r   r   ÚvalueÚstrr   r   )r   r~   r   s    €€r   Úserialize_tensor_metadataÚ>generate_yaml_from_profiles.<locals>.serialize_tensor_metadataÓ   sC   ø€ à—F‘FØ.¨q¯w©wÑ7×=Ñ=Ü˜!Ÿ(™(“mØ0°·±Ñ:×@Ñ@ñ	
ð 	
r   Úopc                 ó  >• U R                    Vs/ s H  nUc  M  T" U5      PM     sn[        U R                  [        5      (       a  T" U R                  5      S.$ U R                   Vs/ s H  nT" U5      PM     snS.$ s  snf s  snf )N©r/   r0   )r/   r#   r0   r   )r†   r:   Úoutr„   s      €r   Úserialize_op_profileÚ9generate_yaml_from_profiles.<locals>.serialize_op_profileÛ   s�   ø€ ð Ÿ?š?óâ*�CØó /Ñ)¨#Ö.Ù*ñô ˜bŸn™n¬n×=Ñ=ñ *¨"¯.©.Ó9ñ
ð 	
ð ACÇÂÓOÂ¸Ñ/°Ö4ÁÑOñ
ð 	
ùòùò Ps   �B›BÁ*B)Útorch_versionÚ	operatorsF)Ú	sort_keys)
ÚyamlÚtorch._export.serde.serializer~   r   r   Údictr-   rc   Údumpr|   )
r^   r�   rŠ   Úoperatorro   rX   Úserialized_datar~   r   r„   s
          @@@r   Úgenerate_yaml_from_profilesr•   Å   s±   ú€ ó ÷ð

¤^ð 
¼÷ 
ð 
ð
¤ð 
¬t÷ 
ð  #.×"3Ñ"3Ô"5õâ"5ÑˆHð 	ÀÓIÂ°WÐ'¨Ö0ÁÑIÒIÙ"5ð ò ð �9‰9Ü+Ó-¸OÑLØð ð ð ùò Jùôs   ÁBÁBÁ*BÂBÚfc                 óš  • [        U 5      n[        U[        [        R                  45      (       a=  [        R
                  " U5      n[        US5       nUR                  U5        SSS5        g[        U[        R                  5      (       a!  UR                  UR                  S5      5        g[        SU 35      e! , (       d  f       g= f)z�
Serializes the given operator profiles into a yaml format and saves it to
the given file. The operator profile can be loaded back using `load_op_profiles`.
ÚwNúutf-8úInvalid type of file )r•   r#   rƒ   ÚosÚPathLikeÚfspathÚopenÚwriteÚioÚBytesIOÚencodeÚ
ValueError)r^   r–   Úyaml_strÚfiles       r   Úsave_op_profilesr¦   ó   s”   € ô
 +¨;Ó7€Hä�!”cœ2Ÿ;™;Ð'×(Ñ(Ü�IŠI�a‹Lˆä�!�SŒ\˜TØ�J‰J�xÔ ÷ ˆ\ô 
�A”r—z‘z×	"Ñ	"Ø	�‰�—‘ Ó(Õ)ô Ð0°°Ð4Ó5Ð5÷ �\ús   ÁB<Â<
C
r¤   c                 óˆ  ^	^
^• SSK nSSKJm	Jm
  S[        S[
        4U	U
4S jjmS[        S[        4U4S jjnUR                  U 5      nUS   nU[        5       :w  a  [        S	U S
[        5        35      eUS   nUR                  5        VVVs0 s H  u  pgXg Vs1 s H
  o‚" U5      iM     sn_M     snnn$ s  snf s  snnnf )zO
Reads the yaml saved by `save_op_profiles` and returns the operator profiles.
r   N)Ú_SERIALIZE_TO_TORCH_DTYPEÚ_SERIALIZE_TO_TORCH_LAYOUTÚdatar    c                 óh   >• [        U S   TU S      [        R                  " U S   5      TU S      S9$ )Nr   r   r   r   r�   )r   r$   r   )rª   r¨   r©   s    €€r   Údeserialize_tensor_metadataÚ<read_profiles_from_yaml.<locals>.deserialize_tensor_metadata  s?   ø€ ÜØ�f‘Ø+¨D°©MÑ:Ü—<’<  X¡Ó/Ø-¨d°8©nÑ=ñ	
ð 	
r   c                 ó´   >• [        U4S jU S    5       5      nU S   n[        U[        5      (       a  [        U4S jU 5       5      OT" U5      n[        XS9$ )Nc              3   ó4   >#   • U  H  nT" U5      v •  M     g 7fr"   r   )r8   r:   r¬   s     €r   r;   ÚJread_profiles_from_yaml.<locals>.deserialize_op_profile.<locals>.<genexpr>  s   øé € ð 
Ú8L°Ñ'¨×,Ð,Ò8Lùó   ƒr/   r0   c              3   ó4   >#   • U  H  nT" U5      v •  M     g 7fr"   r   )r8   r‰   r¬   s     €r   r;   r°   !  s   øé € ÐOÒ>N°sÑ-¨c×2Ð2Ò>Nùr±   rˆ   )r1   r#   r\   r-   )rª   r/   Úout_profile_datar0   r¬   s       €r   Údeserialize_op_profileÚ7read_profiles_from_yaml.<locals>.deserialize_op_profile  sh   ø€ Üô 
Ø8<¸^Ò8Ló
ó 
ˆð   Ñ.Ðô Ð*¬D×1Ñ1ô ÔOÑ>NÓOÔOá,Ð-=Ó>ð 	ô
  lÑLÐLr   rŒ   zBUnable to load outdated profile. It was saved with torch version: z# but the current torch version is: r�   )r�   r�   r¨   r©   r‘   r   r-   Ú	safe_loadr|   ÚRuntimeErrorrc   )r¤   r�   r´   Úloaded_dataÚloaded_torch_versionÚoperators_datar“   ro   rX   r¨   r©   r¬   s            @@@r   Úread_profiles_from_yamlr»     sæ   ú€ ó
 ÷ð

¬$ð 
´>÷ 
ð 
ð
M¤Tð 
M¬i÷ 
Mð —.‘. Ó*€KØ& Ñ7ÐàÔ0Ó2Ó2ÜØPØ#Ð$Ð$GÔHYÓH[ÐG\ð^ó
ð 	
ð
 ! Ñ-€Nð #1×"6Ñ"6Ô"8õâ"8ÑˆHð 	À(ÓKÂ(°wÐ)¨'Ö2Á(ÑKÒKÙ"8óð ùÚKùôs   ÂB=ÂB8Â,B=Â8B=c                 ó”  • [        U [        [        R                  45      (       a;  [        R                  " U 5      n [        U 5       nUR                  5       nSSS5        OM[        U [        R                  5      (       a   U R                  5       R                  S5      nO[        SU  35      e[        W5      $ ! , (       d  f       N= f)z<
Loads the saved operator profiles from `save_op_profiles`.
Nr™   rš   )r#   rƒ   r›   rœ   r�   rž   Úreadr    r¡   Údecoder£   r»   )r–   r¥   r¤   s      r   Úload_op_profilesr¿   7  s‘   € ô �!”cœ2Ÿ;™;Ð'×(Ñ(Ü�IŠI�a‹Lˆä�!ŒW˜Ø—y‘y“{ˆH÷ ˆWô 
�A”r—z‘z×	"Ñ	"Ø—6‘6“8—?‘? 7Ó+‰ô Ð0°°Ð4Ó5Ð5ä" 8Ó,Ð,÷ �Wús   ÁB9Â9
C)$Ú
contextlibr    Úloggingr›   Úcollections.abcr   r   Údataclassesr   Útypingr   r   r   r$   Útorch._library.custom_opsr	   Útorch.typesr
   Ú	getLoggerr   rd   r·   r   r   r-   rƒ   Úsetr]   Úcontextmanagerr‘   rx   r|   r•   r¦   r»   r¿   r   r   r   Ú<module>rÊ      sv  ðÛ Û 	Û Û 	ß /Ý !ß 'Ñ 'ã Ý 6Ý  ð ×Ò˜Ó!€ô�|ô ñ �$Ñ÷
Dð 
Dó ð
Dñ �$Ñ÷>ð >ó ð>ð
. 3ð .°C¸	±Nð .Àxô .ðb ×Ñð_*¨d°3¸¸I¹Ð3FÑ.Gð _*ÈIó _*ó ð_*ðD2˜3ô 2ð
+¨T°#°s¸9±~Ð2EÑ-Fð +È3ô +ð\6 $ s¨C°	©NÐ':Ñ";ð 6Àð 6ÈTô 6ð(- cð -¨d°3¸¸I¹Ð3FÑ.Gô -ð`-˜ð - T¨#¨s°9©~Ð*=Ñ%>õ -r   