
    ȅi_5                        S SK r S SKrS SKrS SKJr  S SKJrJr  S SKrS SKJrJ	r	J
r
Jr  S SKJr  SSKJrJr  \R                  r\" S5      SS	.S
\R"                  S\\   S\4S jj5       rS\\\R*                  4   S\S\S\S\4
S jrS r\
S\R2                  \
   S\\
   S\S\S\S\R:                  S0rS r\" 5       r S\RB                  S\4S jr"S\\#   S\\   4S jr$g)     N)GenericAlias)OptionalUnion)devicedtypeTensortypes)
exposed_in   )_OPAQUE_TYPESis_opaque_typeztorch.library)op_nameprototype_functionr   returnc               
  ^^^^^^^ SnU R                   mSm[        R                  " U 5      mU4S jmS[        4UUU4S jjmS[        [
        [        [           [        4   S4   S[        [        [        R                  S4   [        4   4U4S	 jjmS
[
        [        [           [        4   S[        [        R                  [        4   4UU4S jjm/ n[        5       nSn[        TR                  R                  5       5       GH  u  nu  p[        U	5      (       d  T" S5        U	R                   [        R"                  R$                  :X  a  U(       d  UR'                  S5        SnU	R(                  [        R"                  R*                  L a  T" SU S35        T" U	R(                  5      u  pSnU
[,        ;  a  [/        U
5      (       a  [0        U
   R2                  nOU
[4        R6                  R8                  :X  a  T" SU S35        O[;        U
S5      (       ab  U
R<                  [        L aO  [?        U
5      nSnU[,        ;   a  SU S3nT" SU SU	R(                   SU S[,        RA                  5        S3	5        O8T" SU SU	R(                   S[,        RA                  5        S35        O	[,        U
   nUc   e[        U5      [        L a;  X:w  a  [C        S5      eURE                  S5      (       a  SU SU[G        S5      S  3nO;X;   a6  URE                  S5      (       d  T" SU S 35        SU SU[G        S5      S  3nURI                  U5        U	RJ                  [        R"                  R*                  L a  UR'                  U S!U 35        GM  SnU	RJ                  b*  [M        U	RJ                  [N        [P        [        45      (       a  [        U	RJ                  5      nO[M        U	RJ                  [        [4        RR                  45      (       a  S"U	RJ                   S"3nO[M        U	RJ                  [4        RT                  5      (       a>  [        U	RJ                  5      nS#nURE                  U5      (       d   eU[G        U5      S nO"T" SU S$[        U	RJ                  5       S%35        UR'                  U S!U S&U 35        GM     X:w  a(  [        U5      U-
  n[G        U5      S':  a  T" U S(35        T" TRV                  5      u  nn[Y        UT5      nUb  U S)S*R[                  U5       S+U 3$ S)S*R[                  U5       S+U 3$ ),a  Parses the schema of a given function with type hints. The schema is inferred from the
function's type hints, and can be used to define a new operator.

We make the following assumptions:

* None of the outputs alias any of the inputs or each other.
* | String type annotations "device, dtype, Tensor, types" without library specification are
  | assumed to be torch.*. Similarly, string type annotations "Optional, List, Sequence, Union"
  | without library specification are assumed to be typing.*.
* | Only the args listed in ``mutates_args`` are being mutated. If ``mutates_args`` is "unknown",
  | it assumes that all inputs to the operator are being mutates.

Callers (e.g. the custom ops API) are responsible for checking these assumptions.

Args:
    prototype_function: The function from which to infer a schema for from its type annotations.
    op_name (Optional[str]): The name of the operator in the schema. If ``name`` is None, then the
        name is not included in the inferred schema. Note that the input schema to
        ``torch.library.Library.define`` requires a operator name.
    mutates_args ("unknown" | Iterable[str]): The arguments that are mutated in the function.

Returns:
    The inferred schema.

Example:
    >>> def foo_impl(x: torch.Tensor) -> torch.Tensor:
    >>>     return x.sin()
    >>>
    >>> infer_schema(foo_impl, op_name="foo", mutates_args={})
    foo(Tensor x) -> Tensor
    >>>
    >>> infer_schema(foo_impl, mutates_args={})
    (Tensor x) -> Tensor
unknownNc                 (   > [        SU  ST S35      e)Nzinfer_schema(func): z Got func with signature ))
ValueError)whatsigs    U/home/james-whalen/.local/lib/python3.13/site-packages/torch/_library/infer_schema.pyerror_fninfer_schema.<locals>.error_fnF   s     /v5NseSTUVV    annotation_typec                 X   >  [        U TT5      $ ! [         a    T" SU  S35         g f = f)NzUnsupported type annotation z. It is not a type.)eval	Exception)r   r   
pf_globals	pf_localss    r   convert_type_string)infer_schema.<locals>.convert_type_stringI   s<    	Y?? 	..??RS	s    ))tys.r   c                    > / nSnU  H"  nT" U5      u  p4UR                  U5        X$-  nM$     U(       a  [        U5      S4$ U S4$ )NFT)appendtuple)r$   reschangedty
ty_changedunstringify_types        r   unstringify_types'infer_schema.<locals>.unstringify_typesQ   sV     B-b1NBJJrN!G  :t##:r   r*   c                    > [        U [        5      (       a
  T" U 5      S4$ [        R                  " U 5      =n(       a2  T" [        R                  " U 5      5      u  p#U(       a  [        X5      S4$ U S4$ )NTF)
isinstancestrtyping
get_originget_argsr   )r*   originargsargs_changedr"   r-   s       r   r,   &infer_schema.<locals>.unstringify_type_   sk     b#&r*D00((,,V,!26??23F!GD#F14775yr   Fz>We do not support positional-only args, varargs, or varkwargs.*Tz
Parameter z must have a type annotation.z's type cannot be inferred from the schema as it is a ScriptObject. Please manually specify the schema using the `schema=` kwarg with the actual type of the ScriptObject.
__origin__z

zFor example, z.

z has unsupported type z]. We do not support Tuple inputs in schema. As a workaround, please try to use List instead. zThe valid types are: .. The valid types are: zomutates_args must either be a sequence of the names of the arguments that are mutated or the string 'unknown'. r   zTensor(az!)zM is in mutable_args but only Tensors or collections of Tensors can be mutated "ztorch.z' has an unsupported default value type z;. Please file an issue on GitHub so we can prioritize this.=r   z in mutates_args were not found in the custom op's signature. mutates_args should contain the names of all args that the custom op mutates, or just the string 'unknown' if you don't know.(, z) -> ).__globals__inspect	signaturer1   r'   r   typeobjectr2   Anyboolset	enumerate
parametersitemssupported_paramkind	ParameterKEYWORD_ONLYr&   
annotationemptySUPPORTED_PARAM_TYPESr   r   
class_nametorch_CScriptObjecthasattrr:   tuple_to_listkeysr   
startswithlenadddefaultr0   intfloatr   r   return_annotationparse_returnjoin)r   mutates_argsr   UNKNOWN_MUTATESparams	seen_argssaw_kwarg_only_argidxnameparamr   _schema_type	list_typeexample_type_strdefault_repr
dtype_repr	torch_dotmutates_args_not_seenra   retr"   r   r    r!   r   r,   r-   s                        @@@@@@@r   infer_schemaru      s3   T  O#//JI 

.
/CWS  5fs*+S01	uVZZ_%t+	,U4<#45 %

D@P:Q   FI'(<(<(>?]du%%UV::**777%c"%)"w00666z$'DEF .e.>.>?"77o..+O<GG EHH$9$99  'Z Z 66#..%7)/:	#)  55)6yk'G$ &<U=M=M<N Or'(+,A,F,F,H+IL  &<U=M=M<N O,,A,F,F,H+IL
 0@K&&&$. O  %%h// (RCMO0L/MN!))(33 &st %SEKH,H+IJKd==G--333MM[M4&12L}}$
5==3tBT(U(U"5==1EMMC+>??!"5==/3EMM5;;77 /
$	!,,Y7777)#i.*:; &MdSXS`S`NaMb cP Q
 MM[M4&,@Ao @p & #L 1I =$%)() *U V ,C,A,ABq
((
3C!DIIf-.eC599tyy !se,,r   	base_typecpp_type	list_baseoptional_base_listoptional_list_basec                   ^ U T4[         R                  U    T S34/nS[        [        [         R                  4   4S jnU(       a!  UR                  U4S jU" U 5       5       5        U(       a2  UR                  U4S jU" [         R                  U    5       5       5        U(       a!  UR                  U4S jU" U 5       5       5        U$ )N?typc                     [         R                  U    [         R                  U    [        [        R
                  R                  U 45      [        [        U 45      4$ N)r2   SequenceListr   collectionsabclist)r}   s    r   derived_seq_types(derived_types.<locals>.derived_seq_types   sF    OOC KK11C6:v&	
 	
r   c              3   0   >#    U  H  oT S 34v   M     g7f)z[]N .0seq_typrw   s     r   	<genexpr> derived_types.<locals>.<genexpr>   s      
6R7
"o&6Rs   c              3   2   >#    U  H  nUT S 34v   M     g7f)z?[]Nr   r   s     r   r   r      s'      
 I 
#&'Hs   c              3   T   >#    U  H  n[         R                  U   T S 34v   M     g7f)z[]?N)r2   r   r   s     r   r   r      s-      
7 __W%(3'787s   %()r2   r   r   rE   _SpecialFormextend)rv   rw   rx   ry   rz   resultr   s    `     r   derived_typesr      s     
H		#z^4PF
uT6+>+>%>? 
  
6G	6R
 	
  
 -V__Y-GH
 	

  
,Y7
 	
 Mr   c                     [         SSSS4[        SSSS4[        SSSS4[        SSSS4[        SSSS4[
        R                  SSSS4[        S	SSS4[        S
SSS4/n [        R                  R                  5       (       a5  SSKJn  U R                  [        R                   " ["        U5      SSSS45        / nU  H  nUR%                  ['        U6 5        M     [)        U5      $ )Nr   TFSymIntr`   rH   r1   Scalar
ScalarTypeDevicer   )	GroupName)r   r_   r`   rH   r1   r	   Numberr   r   rU   distributedis_available"torch.distributed.distributed_c10dr   r&   r2   castrE   r   r   dict)datar   r   lines       r   get_supported_param_typesr      s     
4u-	heT*	ud+	vtUD)	eUE5)	xue4	eUE2	5%/
RD %%''@V[[y15%NOFmT*+ <r   r   zTensor[]r   r`   rH   r   c                    U c  gU [         R                  R                  L a  U" S5        [        R                  " U 5      nU[
        La&  U [        ;  a  U" SU  S[         S35        [        U    $ [        R                  " U 5      nU H"  nU[        ;  d  M  U" SU  S[         S35        M$     SR                  U Vs/ s H  n[        U   PM     sn5      n[        U5      S:X  a  SU-   S	-   nSU-   S	-   $ s  snf )
Nz()z7No return type annotation was provided. Please add one.zReturn has unsupported type r<   r;   rA   r   r@   r   )
rC   rO   rR   r2   r3   r'   SUPPORTED_RETURN_TYPESr4   rc   r\   )rQ   r   r5   r6   arg	output_tys         r   rb   rb   !  s   W&&,,,JKz*FU33.zl ;((>'?qB
 &j11??:&D,,.zl ;((>'?qB  		$G$31#6$GHI 4yA~)Oc)	?S   Hs   <C5rk   c                     U R                   [        R                  R                  [        R                  R                  4;   $ r   )rN   rC   rO   POSITIONAL_OR_KEYWORDrP   )rk   s    r   rM   rM   D  s6    :://&&  r   
tuple_typec                 <   [        U SS5      nU [        R                  L d  U [        L d	  US:X  d  Uc  [        $ [        U5      S:X  a  [        US      $ [        U5      S:X  a  US   [        L a  [        US      $ [        [        R                  [        U5            $ )zu
Convert `tuple_type` into a list type with the same type arguments. Assumes that `tuple_type` is typing.Tuple type.
__args__Nr   r   r      )getattrr2   Tupler'   r   r\   Ellipsisr   )r   	type_argss     r   rY   rY   K  s     
J5I 	fll"? 	Y1	IaL!!	Y1	1!9IaL!!FLLy!1233r   )%r   rC   r2   r	   r   r   r   rU   r   r   r   torch.utils._exposed_inr
   opaque_objectr   r   _TestTensorCallabler1   ru   rE   r   rH   r   r   r   r   r_   r`   r   r   rb   rS   rO   rM   r'   rY   r   r   r   <module>r      sB       "  . . . 8
 ll O "--
 c]- 	- -D$T6.../$$ $ 	$
 $N2 H
KKL*	7&	LL( !@ 23 7,,  4d5k 4d4j 4r   