
    Ǆgr)                     P   d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	 d dl
Z
d dl
mZmZ d dlmZ e j                   G d d             Z G d d      Zd	ed
efdZded
eeef   fdZded
efdZded
efdZded
efdZded
efdZded
efdZdefdZd Zdej<                  deedf   deeef   d
eeej>                  ef      fdZ d Z!ded
efdZ"d
efdZ#d Z$dej<                  fd Z%dej<                  fd!Z&dej<                  d
efd"Z'dej<                  d
e	edf   fd#Z(y)$    N)AnyCallableDictIterableTupleUnion)_C_utils_internal)
OpOverloadc                   ,    e Zd ZU dZeed<   eed<   d Zy)Kernelz$Models a (function, source location)funcsourcec                 &     | j                   |i |S N)r   )selfargskwargss      \/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/_library/utils.py__call__zKernel.__call__   s    tyy$)&))    N)__name__
__module____qualname____doc__r   __annotations__strr    r   r   r   r      s    .
NK*r   r   c                   $    e Zd ZdZdefdZddZy)RegistrationHandlez2Does something when someone calls .destroy() on it
on_destroyc                     || _         y r   _on_destroy)r   r!   s     r   __init__zRegistrationHandle.__init__   s
    %r   Nc                 $    | j                          y r   r#   )r   s    r   destroyzRegistrationHandle.destroy   s    r   )returnN)r   r   r   r   r   r%   r'   r   r   r   r    r       s    <&8 &r   r    
stacklevelr(   c                     t        j                  t        j                  |             }|j                   d|j
                   }|S )zGet a string that represents the caller.

    Example: "/path/to/foo.py:42"

    Use stacklevel=1 to get the caller's source
    Use stacklevel=2 to get the caller's caller's source
    etc.
    :)inspectgetframeinfosys	_getframefilenamelineno)r)   framer   s      r   
get_sourcer3   !   s;       z!:;Eq/FMr   qualnamec                 r    | j                  d      }t        |      dk7  rt        d|  d      |d   |d   fS )Nz::   zAExpected `qualname` to be of the form "namespace::name", but got zf. The qualname passed to the torch.library APIs must consist of a namespace and a name, e.g. aten::sinr      )splitlen
ValueError)r4   splitss     r   parse_namespacer<   /   sR    ^^D!F
6{a**2 489
 	
 !9fQir   c                     t        |       \  }}d|v r|j                  d      \  }}nd}t        t        j                  |      }t        ||      }t        ||      S )N.default)r<   r8   getattrtorchops)r4   	namespacenameoverloadnspackets         r   	lookup_oprH   ;   sV    %h/OIt
d{Ch	I	&BRF68$$r   opc                 B    t        | t              sJ | j                  dv S )N>   atenprimprims)
isinstancer   rC   )rI   s    r   
is_builtinrO   F   s"    b*%%%<<444r   schemac                     d }t        | t        j                  j                        r ||       S ddlm} t        | t
              r|j                  |       } t        | |      sJ  ||       S )zCheck if the schema is functional.

    An operator is functional if:
    - it does not mutate any of its inputs
    - it does not return a view on any of its inputs
    - it has at least one return
    c                     | j                   ry| j                  }t        |      dkD  xr t        d |D              }|ry| j                  syy)NFr   c              3   j   K   | ]+  }|j                   d uxr |j                   j                    - y wr   )
alias_infois_write).0rs     r   	<genexpr>z>is_functional_schema.<locals>.is_functional.<locals>.<genexpr>X   s3      5
GHALL$BQ\\-B-B)BB5
s   13T)
is_mutablereturnsr9   any)rP   retsis_non_mutating_views      r   is_functionalz+is_functional_schema.<locals>.is_functionalT   sS    ~~"4y1}  
 5
LP5
 2
  ~~r   r   )FunctionSchema)rN   rA   r	   r_   torchgen.modelr   parse)rP   r^   r_   s      r   is_functional_schemarb   K   sb     &%((112V$$ .&#%%f-fn---  r   typc           	      F   | t        j                  t         j                  j                               k(  xs | t        j                  t        j                  t         j                  j                                     k(  xs | t        j                  t        j                  t         j                  j                                     k(  xsZ | t        j                  t        j                  t        j                  t         j                  j                                           k(  S r   )r	   ListType
TensorTypegetOptionalTyperc   s    r   is_tensorlist_like_typerj   n   s    r{{2==,,.// 	U"++boobmm.?.?.ABCC	U"//"++bmm.?.?.A"BCC	U "//"++boobmm>O>O>Q.R"STT	r   c                     | t         j                  j                         k(  xs4 | t        j                  t         j                  j                               k(  S r   )r	   rf   rg   rh   ri   s    r   is_tensor_like_typerl   x   s:    "--##%%T@Q@Q@S0T)TTr   c                    | j                   dk7  ry| j                  }t        |j                        dk(  sy|j                  d   j                  y|j                  d   j                  j
                  }t        |      dk7  ryt        t        |            }t        |j                        dk  ry|j                  d   }|j                  y|j                  j                  sy|j                  j
                  }t        |      dk7  ry|t        t        |            k7  ry|j                  dd D ]  }|j                   y y)aN  Check if an op is an inplace aten op, i.e. it mutates and returns the first arg.

    TODO: torchgen/model.py's FunctionSchema.parse is the source of truth for this,
    but not all PyTorch builds have torchgen (due to the yaml dependency being weird).
    Figure this out.

    Example: add_(Tensor(a!) x, Tensor y) -> Tensor(a)
    rK   Fr7   r   NT)
rC   _schemar9   rZ   rT   	after_setnextiter	argumentsrU   )rI   rP   	alias_setloc	first_argargs         r   mutates_and_returns_first_argrw   |   s(    
||vZZFv~~!#~~a##+q!,,66I
9~
tI
C
6q   #I#(($$..I
9~
d4	?### >>% r   c                    g }i }t        t        | j                              D ]  }| j                  |   }|j                  rE|j                  |v r||j                     ||j                  <   I|j
                  ||j                  <   c|t        |      k  r|j                  ||          |j                  |j
                          t        |      |fS r   )ranger9   rr   
kwarg_onlyrD   default_valueappendtuple)rP   r   r   new_args
new_kwargsiinfos          r   fill_defaultsr      s    HJ3v''() 4"??yyF"(.tyy(9
499%(,(:(:
499%3t9}Q( 2 234 ?J&&r   r   .r   c              #   Z  K   t        | j                        t        |      t        |      z   k\  sJ t        t        | j                              D ]W  }| j                  |   }|j                  r"|j                  |v r|||j                     f @|t        |      k\  rO|||   f Y yw)zzips schema.arguments and (args, kwargs) together.

    Assumes that (args, kwargs) were the inputs to some torch._ops.OpOverload:
    that is, kwargs must be keyword-only arguments and default values may be omitted.
    N)r9   rr   ry   rz   rD   )rP   r   r   r   r   s        r   
zip_schemar      s      v CIF$;;;;3v''() "??yyF"F499---D	> DGm s   B)B+c           	      \   ddl m} | j                  }t        |t        j
                  j                        st        d      d }g }| j                  D ]  }t        |t        j                  j                  t        j                  j                  j                  f      r|j                   ||             ct        |t        j                  j                  j                  t        t         f      r&|j                  |D cg c]
  } ||       c}       t        dt#        |               t%        j&                  |j(                        j*                  | } ||       }|j-                  |j.                  t!        |j0                  j3                               t        |      f      S c c}w )Nr   )FunctionSchemaGenzfx_node's target must be a hop.c                     | j                   j                  dd       }|;| j                  dk(  sJ t        | j                  j
                  | j                        }|S )Nvalget_attr)metarg   rI   r@   graphowning_moduletarget)nodemeta_vals     r   _collect_example_valz5hop_schema_from_fx_node.<locals>._collect_example_val   sM    99==-77j(((tzz77EHr   zUnsupported arg type )torchgen.gen_schema_utilsr   r   rN   rA   _opsHigherOrderOperatorRuntimeErrorr   fxNoder   r|   immutable_collectionsimmutable_listlistr}   typer,   	signaturer   bindfrom_example_namerr   items)	r   r   hopr   example_inputsrv   x
bound_argsexample_outputs	            r   hop_schema_from_fx_noder      sW   ;
++Cc5::99:<== Nyy DcEHHMM588==+=+=>?!!"6s";<%((00??uM
 !!C"Hq#7#:"HI!6tCykBCCD *N):):3<<)H)M)M	*J *$/N))		5--3356n9M8O  #Is   >F)
c                     t        | t              sJ t        |       ry| j                  }|j                  syt        |j                        dkD  ryy)NFr   T)rN   r   rO   rn   rY   r9   rZ   )rI   rP   s     r   can_generate_trivial_fake_implr      sJ    b*%%%"~ ZZF
6>>Qr   c                  $    t        t        dd      S )zIf an op was defined in C++ and extended from Python using the
    torch.library APIs, returns if we require that there have been a
    m.set_python_module("mylib.ops") call from C++ that associates
    the C++ op with a python module.
    REQUIRES_SET_PYTHON_MODULET)r@   r
   r   r   r   requires_set_python_moduler     s     ?$@$GGr   c                     t        | t        j                  j                  j                        sJ g }t        j                  j
                  j                  ||j                         f      \  }}|D ]  }t        |t        j                        st        j                  j                  |      j                  t        j                  j                  j                        so|j                  t        |              | j!                  ||||      S r   )rN   rA   utils_python_dispatchTorchDispatchMode_pytreetree_flattenvaluesTensorr	   _dispatch_keyshasDispatchKeyPythonr|   r   __torch_dispatch__)	curr_modeop_overloadr   r   overload_typesargs_flattened_as           r   handle_dispatch_moder     s    i!=!=!O!OPPPN++88$9PQNA +
 a&588+B+B1+E+I+IHH  '',
 !!$q'*+ ''^T6RRr   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr   )rz   rV   r   s     r   rX   z&has_kwarg_only_args.<locals>.<genexpr>  s     6q||6s   r[   rr   rP   s    r   has_kwarg_only_argsr     s    6V%5%5666r   c                     | j                   D ];  }t        |j                        st        |j                        s.|j                  s; y y)NTF)rr   rl   r   rj   rz   )rP   r   s     r   has_kwarg_only_tensorsr   !  sD     #AFF+/Fqvv/N|| r   c                 :    t        d | j                  D              S )z
    Given a schema, returns True if the schema has a Tensor arg.
    A Tensor arg is any arg with a type annotation that might involve Tensor.
    c              3   t   K   | ]0  }t        |j                        xs t        |j                         2 y wr   )rl   r   rj   r   s     r   rX   z!has_tensor_arg.<locals>.<genexpr>0  s3       
QVV	$	G(?(G	Gs   68r   r   s    r   has_tensor_argr   +  s$    
  !!  r   c                     t        | j                        D ]C  \  }}|j                  t        j                  j                         u s1|j                  dk(  sA|c S  y)zx
    Given a schema, returns the id of the `device: torch.device` argument.
    If it does not exist, returns None.
    deviceN)	enumeraterr   r   r	   DeviceObjTyperg   rD   )rP   indexrv   s      r   get_device_arg_indexr   6  sR    
   0 01 
s88r''++--#((h2FL r   ))dataclassesr,   r.   typingr   r   r   r   r   r   rA   r	   r
   
torch._opsr   	dataclassr   r    intr   r3   r<   rH   boolrO   rb   rj   rl   rw   r   r_   Argumentr   r   r   r   r   r   r   r   r   r   r   r   <module>r      s     
 > >  % ! * * * 3 3 	 c 	 eCHo 	 % %
 %5: 5$ 5
! ! !F  US UT U#j #L'$%*38_>B38neBKK$%&0#Lz d  HD HS$7 1 1 72#4#4 2,,  !2!2 uS$Y7G r   