
    IЦiA*                        S SK r S SKrS SKJrJr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5      SS.S\R                  S\\   S	\4S
 jj5       rS rS r\	S\R                  \	   S\S\S\S\
R*                  S0rS r\" 5       rS\ R2                  S	\4S jrS\R6                  \R8                     S	\R6                  \R                     4S jrg)    N)ListOptionalSequenceUnion)devicedtypeTensortypes)
exposed_inztorch.library)op_nameprototype_functionr   returnc                 ^^ Sn[         R                  " U 5      mU4S jmS[        4U4S jjn/ 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        U
R                  n[        U5      [        :X  a  U" U5      nU[        R!                  5       ;  a  UR"                  [$        L a]  ['        U5      nSnU[        R!                  5       ;   a  SU S3nT" S	U	 SU
R                   SU S[        R!                  5        S3	5        O.T" S	U	 SU
R                   S[        R!                  5        S35        [        U   n[        U5      [        :X  a;  X:w  a  [)        S5      eUR+                  S5      (       a  SU SU[-        S5      S  3nO;X;   a6  UR+                  S5      (       d  T" S	U	 S35        SU SU[-        S5      S  3nUR/                  U	5        U
R0                  [         R                  R                  L a  UR                  U SU	 35        GMV  SnU
R0                  b*  [3        U
R0                  [4        [6        [8        45      (       a  [        U
R0                  5      nO[3        U
R0                  [        [:        R<                  45      (       a  SU
R0                   S3nO[3        U
R0                  [:        R>                  5      (       a>  [        U
R0                  5      nSnUR+                  U5      (       d   eU[-        U5      S nO"T" S	U	 S[        U
R0                  5       S35        UR                  U SU	 SU 35        GM     X:w  a(  [        U5      U-
  n[-        U5      S:  a  T" U S 35        TR@                  n[        U5      [        :X  a  U" U5      n[C        UT5      nUb  U S!S"RE                  U5       S#U 3$ S!S"RE                  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
unknownc                 (   > [        SU  ST S35      e)Nzinfer_schema(func): z Got func with signature ))
ValueError)whatsigs    Z/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/_library/infer_schema.pyerror_fninfer_schema.<locals>.error_fn9   s#    "4&(EcU!L
 	
    annotation_typec                 b   >  [        U 5      $ ! [         a  nT" SU  S35         S nAg S nAff = f)NzUnsupported type annotation z. It is not a type.)eval	Exception)r   er   s     r   convert_type_string)infer_schema.<locals>.convert_type_string>   s>    	(( 	..??RS 	s   
 
.).Fz>We do not support positional-only args, varargs, or varkwargs.*Tz
Parameter z must have a type annotation.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!)NzM 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) -> )#inspect	signaturestrset	enumerate
parametersitemssupported_paramkind	ParameterKEYWORD_ONLYappend
annotationemptytypeSUPPORTED_PARAM_TYPESkeys
__origin__tupletuple_to_listr   
startswithlenadddefault
isinstanceintfloatbooltorchr   r   return_annotationparse_returnjoin)r   mutates_argsr   UNKNOWN_MUTATESr   params	seen_argssaw_kwarg_only_argidxnameparamr   	list_typeexample_type_strschema_typedefault_repr
dtype_repr	torch_dotmutates_args_not_seenrF   retr   r   s                        @@r   infer_schemarY      sl   T  O


.
/C

S  FI'(<(<(>?]du%%UV::**777%c"%)"w00666z$'DEF  ** C'1/BO"7"<"<">>))U2)/:	#)  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
 ,O<$. O  %%h// (RCMN0K/LM!))(33 &st %SEKH,G+HIK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&,@AQ @R & #L 1I =$%)() *U V --#%/0AB
((
3C!DIIf-.eC599tyy !se,,r   c                 X  ^ U T4[         R                  U    T S34/nS 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?c                 N    [         R                  U    [         R                  U    /$ N)typingr   r   )typs    r   derived_seq_types(derived_types.<locals>.derived_seq_types   s$    OOC KK
 	
r   c              3   0   >#    U  H  oT S 34v   M     g7f)z[]N .0seq_typcpp_types     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?[]Nrc   rd   s     r   rh   ri      s$      
H 
#&'Hs   c              3   T   >#    U  H  n[         R                  U   T S 34v   M     g7f)z[]?N)r^   r   rd   s     r   rh   ri      s-      
7 __W%(3'787s   %()r^   r   extend)	base_typerg   	list_baseoptional_base_listoptional_list_baseresultr`   s    `     r   derived_typesrr      s     
H		#z^4F

  
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 / nU  H  nUR                  [        U6 5        M     [        U5      $ )Nr	   TFSymIntrC   rD   r+   Scalar
ScalarTypeDevice)r	   rB   rC   rD   r+   r
   Numberr   r   rl   rr   dict)datarq   lines      r   get_supported_param_typesr|      s     
4u-	heT*	ud+	vtUD)	eUE5)	xue4	eUE2	5%/
D FmT*+ <r   r	   zTensor[]rt   rC   rD   ru   c                    U c  gU [         R                  R                  L a  U" S5        [        R                  " U 5      nU[
        La4  U [        R                  5       ;  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SR                  U Vs/ s H  n[        U   PM     sn5      -   S-   $ s  snf )	Nz()z7No return type annotation was provided. Please add one.zReturn has unsupported type r#   r"   r'   r(   r   )
r)   r2   r6   r^   
get_originr;   SUPPORTED_RETURN_TYPESr9   get_argsrH   )r5   r   originargsargs        r   rG   rG      s    W&&,,,JKz*FU388::.zl ;((>'?qB &j11??:&D,,.zl ;((>'?qB  4H4C2374HIICOOHs   C*
rP   c                     U R                   [        R                  R                  [        R                  R                  4;   $ r]   )r1   r)   r2   POSITIONAL_OR_KEYWORDr3   )rP   s    r   r0   r0      s6    :://&&  r   
tuple_typec                 z   [        U SS5      nU [        R                  L d	  US:X  d  Uc  [        R                  $ [	        U5      S:X  a  [        R                  US      $ [	        U5      S:X  a"  US   [
        L a  [        R                  US      $ [        R                  [        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__Nrc      r      )getattrr^   Tupler   r>   Ellipsisr   r;   )r   	type_argss     r   r<   r<     s     
J5I V\\!Y"_	8I{{	Y1	{{9Q<((	Y1	1!9{{9Q<(({{6<<i(89::r   )r)   r^   r   r   r   r   rE   r   r   r	   r
   torch.utils._exposed_inr   Callabler+   rY   rr   r|   rB   rC   rD   rx   r   rG   r8   r2   r0   Typer   r<   rc   r   r   <module>r      s      2 2  . . . O "T-T-
 c]T- 	T- T-n>& H
KK	7&	LL( P6 23 7,,  ;fkk&,,7 ;FKK<T ;r   