
    9i,                        S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	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)	Generator)	dataclass)AnyCallable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       [/var/www/html/land-doc-ocr/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\\**aeegqww!((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;;LLLL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_outJ_generate_fake_kernel.<locals>._generate_res.<locals>._generate_tensor_out8   s    :?-H-Q#..0-JH4!&&=LH"K166]"-Q)qM9 # &&xxggxx  Is   B&)r$   libraryget_ctxr   r%   r#   )r0   rN   r   rM   s      @r   _generate_res,_generate_fake_kernel.<locals>._generate_res3   sf     mm##%	N 	u|| 	" k>22'445@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_kernelN   sl    !G7//1J41J&--/1JKK$W%8%899 " +G9 5337<.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6	
 	
 r   op_profilesc              #   J  #    / 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[	        X5      n	[        U5      =n
(       a.  U
R                  b  U
R                  X('   U
R                  U	5        M  [        R                  R                  US5      n[        R                  R                  XUSS	9  UR                  U5        M      Uv   U H  nUR                  5         M     UR                  5        H&  u  p[        U5      n
U
c   eU
R                  U5        M(     g! U H  nUR                  5         M     UR                  5        H&  u  p[        U5      n
U
c   eU
R                  U5        M(     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_override)itemslogwarningsplitr]   r	   _abstract_fnregister_faker$   rP   Libraryappend_destroy)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_kernelsrw   \   s    b )+D*,N(..0(	
  c*!.q!1=3C;;b.+F=$V,,5, !!-).););&, ]]**9jAFMM'' (  KK; 1>*
 CLLN  !/ 4 4 6F$V,E$$$) !7	 CLLN  !/ 4 4 6F$V,E$$$) !7s    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GAJ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    FF.qww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_profile9generate_yaml_from_profiles.<locals>.serialize_op_profile   s     ??*C /)#.* bnnn== *"..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   operatorrn   rX   serialized_datar}   r~   r   s
          @@@r   generate_yaml_from_profilesr      s     

^ 
 
 

 
t 
  #."3"3"5"5H 	IW'0II"5   99+-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    
 +;7H!c2;;'((IIaL!S\TJJx  \ 
Arzz	"	"	() 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/-d8n=	
 	
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>Ns-c22>Nr   r   )r1   r#   r\   r-   )r   r/   out_profile_datar0   r   s       r   deserialize_op_profile7read_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   rn   rX   r   r   r   s            @@@r   read_profiles_from_yamlr     s     

$ 
> 
 

MT 
Mi 
M ..*K&7022P#$$GHYH[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   3  s     !c2;;'((IIaL!Wyy{H W 
Arzz	"	"668??7+ 0455"8,, Ws   B99
C)$
contextlibr   loggingr   collections.abcr   dataclassesr   typingr   r   r   r   r$   torch._library.custom_opsr	   torch.typesr
   	getLoggerr   rd   r   r   r   r-   r   setr]   contextmanagerr   rw   r{   r   r   r   r   r   r   r   <module>r      sv    	  	 % ! 1 1  6   !|  $
D 
D 
D $> > >
-3 -C	N -x -` ^*d3I3F.G ^*I ^* ^*B23 2
*T#s9~2E-F *3 *Z6$sC	N':"; 6 6T 6(,c ,d3I3F.G ,^- -T#s9~*=%> -r   