Ë
    P/æjŒ*  ã                   óœ  — d dl Z d dlZd dlmZmZmZmZ d dlZd dlmZmZm	Z	m
Z
 d dlmZ  ed«      ddœdej                  dee   d	efd
„«       Zd„ Zd„ Ze	dej                  e	   dededede
j*                  diZd„ Z e«       Zde j2                  d	efd„Zdej6                  ej8                     d	ej6                  ej                     fd„Zy)é    N)ÚListÚOptionalÚSequenceÚUnion)ÚdeviceÚdtypeÚTensorÚtypes)Ú
exposed_inztorch.library)Úop_nameÚprototype_functionr   Úreturnc               ó„  ‡‡— d}t        j                  | «      Šˆfd„Šdt        fˆfd„}g }t        «       }d}t	        ‰j
                  j                  «       «      D �]R  \  }\  }	}
t        |
«      s ‰d«       |
j                  t         j                  j                  k(  r|s|j                  d«       d}|
j                  t         j                  j                  u r ‰d	|	› d
�«       |
j                  }t        |«      t        k(  r ||«      }|t        j!                  «       vr›|j"                  t$        u r[t'        |«      }d}|t        j!                  «       v rd|› d�} ‰d	|	› d|
j                  › d|› dt        j!                  «       › d�	«       n. ‰d	|	› d|
j                  › dt        j!                  «       › d�«       t        |   }t        |«      t        k(  r6||k7  rt)        d«      ‚|j+                  d«      rJd|› d|t-        d«      d › �}n5|	|v r1|j+                  d«      s ‰d	|	› d�«       d|› d|t-        d«      d › �}|j/                  |	«       |
j0                  t         j                  j                  u r|j                  |› d|	› �«       �Œ7d}|
j0                  �%t3        |
j0                  t4        t6        t8        f«      rt        |
j0                  «      }nºt3        |
j0                  t        t:        j<                  f«      rd|
j0                  › d�}nt3        |
j0                  t:        j>                  «      r9t        |
j0                  «      }d}|j+                  |«      sJ ‚|t-        |«      d }n" ‰d	|	› dt        |
j0                  «      › d�«       |j                  |› d|	› d|› �«       �ŒU ||k7  r't        |«      |z
  }t-        |«      dkD  r ‰|› d �«       ‰j@                  }t        |«      t        k(  r ||«      }tC        |‰«      }|�|› d!d"jE                  |«      › d#|› �S d!d"jE                  |«      › d#|› �S )$aJ  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                 ó(   •— t        d| › d‰› d�«      ‚)Nzinfer_schema(func): z Got func with signature ú))Ú
ValueError)ÚwhatÚsigs    €ú^/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/_library/infer_schema.pyÚerror_fnzinfer_schema.<locals>.error_fn9   s#   ø€ ÜØ" 4 &Ð(EÀcÀUÈ!ÐLó
ð 	
ó    Úannotation_typec                 ó`   •— 	 t        | «      S # t        $ r} ‰d| › d�«       Y d }~y d }~ww xY w)NzUnsupported type annotation z. It is not a type.)ÚevalÚ	Exception)r   Úer   s     €r   Úconvert_type_stringz)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_seenrD   Úretr   r   s                        @@r   Úinfer_schemarW      sf  ù€ ðT  €OÜ
×
Ñ
Ð.Ó
/€Cô
ð
¬Sõ ð €FÜ“€IØÐÜ'¨¯©×(<Ñ(<Ó(>Ó?ó HBÑˆ‰]ˆd�EÜ˜uÔ%ÙÐUÔVà�:‰:œ×*Ñ*×7Ñ7Ò7á%Ø—‘˜cÔ"Ø%)Ð"à×Ñœw×0Ñ0×6Ñ6Ñ6Ù�z $ Ð'DÐEÔFð  ×*Ñ*ˆÜ�Ó ¤CÒ'Ù1°/ÓBˆOàÔ"7×"<Ñ"<Ó">Ñ>Ø×)Ñ)¬UÑ2Ü)¨/Ó:�	Ø#)Ð àÔ 5× :Ñ :Ó <Ñ<Ø)6°y°kÀÐ'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À»M¸NÐ0KÐ/LÐM‘Ø�\Ñ!Ø×)Ñ)¨(Ô3ÙØ   Ð&sÐtôð % S E¨¨K¼¸H»¸Ð,GÐ+HÐIˆKØ�‰�dÔØ�=‰=œG×-Ñ-×3Ñ3Ñ3Ø�M‰M˜[˜M¨¨4¨&Ð1Ö2àˆLØ�}‰}Ð$¬
°5·=±=Ä3ÌÌtÐBTÔ(UÜ" 5§=¡=Ó1‘Ü˜EŸM™M¬C´·±Ð+>Ô?Ø!" 5§=¡= /°Ð3‘Ü˜EŸM™M¬5¯;©;Ô7Ü  §¡Ó/�
Ø$�	Ø!×,Ñ,¨YÔ7Ð7Ð7Ø)¬#¨i«.Ð*:Ð;‘áØ   Ð&MÌdÐSX×S`ÑS`ÓNaÐMbð cPð Qôð �M‰M˜[˜M¨¨4¨&°°,°Ð@ÖAðQHBðR �Ò&Ü # LÓ 1°IÑ =ÐÜÐ$Ó%¨Ò)ÙØ(Ð)ð *Uð Vôð ×-Ñ-ÐÜÐÓ¤#Ò%Ù/Ð0AÓBÐÜ
Ð(¨(Ó
3€CØÐØ�˜!˜DŸI™I fÓ-Ð.¨e°C°5Ð9Ð9Øˆt�y‰y˜Ó Ð!  s eÐ,Ð,r   c                 óf  — | |ft         j                  |    |› d�fg}d„ }|r# || «      D ]  }|j                  ||› d�f«       Œ |r4 |t         j                  |    «      D ]  }|j                  ||› d�f«       Œ |r4 || «      D ])  }|j                  t         j                  |   |› d�f«       Œ+ |S )NÚ?c                 óN   — t         j                  |    t         j                  |    gS ©N)Útypingr   r   )Útyps    r   Úderived_seq_typesz(derived_types.<locals>.derived_seq_types¬   s$   € ä�O‰O˜CÑ Ü�K‰K˜Ñð
ð 	
r   z[]z?[]z[]?)r\   r   r2   )Ú	base_typeÚcpp_typeÚ	list_baseÚoptional_base_listÚoptional_list_baseÚresultr^   Úseq_typs           r   Úderived_typesrf   ¤   sÖ   € ð 
�HÐÜ	�‰˜Ñ	#¨ z° ^Ð4ð€Fò

ñ Ù(¨Ó3ò 	6ˆGØ�M‰M˜7 x j° OÐ4Õ5ð	6áÙ(¬¯©¸Ñ)CÓDò 	7ˆGØ�M‰M˜7 x j°Ð$4Ð5Õ6ð	7áÙ(¨Ó3ò 	HˆGØ�M‰Mœ6Ÿ?™?¨7Ñ3¸°zÀÐ5EÐFÕGð	Hà€Mr   c                  ó  — t         ddddft        ddddft        ddddft        ddddft        ddddft
        j                  ddddft        d	dddft        d
dddfg} g }| D ]  }|j                  t        |Ž «       Œ t        |«      S )Nr	   TFÚSymIntrA   rB   r)   ÚScalarÚ
ScalarTypeÚDevice)r	   r@   rA   rB   r)   r
   ÚNumberr   r   Úextendrf   Údict)Údatard   Úlines      r   Úget_supported_param_typesrq   ¾   s°   € ô 
�˜4  uÐ-Ü	ˆh˜˜e TÐ*Ü	�˜˜u dÐ+Ü	ˆv�t˜U DÐ)Ü	ˆe�U˜E 5Ð)Ü	�‰�x  u¨eÐ4Ü	�˜e U¨EÐ2Ü	�˜5 %¨Ð/ð
€Dð €FØò ,ˆØ�‰”m TÐ*Õ+ð,ä�‹<Ðr   r	   zTensor[]rh   rA   rB   ri   c                 óÀ  — | €y| t         j                  j                  u r |d«       t        j                  | «      }|t
        ur2| t        j                  «       vr |d| › dt        › d�«       t        |    S t        j                  | «      }|D ]  }|t        vsŒ |d| › dt        › d�«       Œ  ddj                  |D �cg c]  }t        |   ‘Œ c}«      z   dz   S c c}w )	Nz()z7No return type annotation was provided. Please add one.zReturn has unsupported type r!   r    r%   r&   r   )
r'   r0   r4   r\   Ú
get_originr9   ÚSUPPORTED_RETURN_TYPESr7   Úget_argsrF   )r3   r   ÚoriginÚargsÚargs        r   rE   rE   Ú   sø   € ØÐØà”W×&Ñ&×,Ñ,Ñ,ÙÐJÔKä×Ñ˜zÓ*€FØ”UÑØÔ3×8Ñ8Ó:Ñ:ÙØ.¨z¨lð ;(Ü(>Ð'?¸qðBôô & jÑ1Ð1ä�?‰?˜:Ó&€DØò ˆØÔ,Ò,ÙØ.¨z¨lð ;(Ü(>Ð'?¸qðBõðð �—‘À4ÖH¸CÔ2°3Ó7ÒHÓIÑIÈCÑOÐOùÒHs   Â?C
rN   c                 ó‚   — | j                   t        j                  j                  t        j                  j                  fv S r[   )r/   r'   r0   ÚPOSITIONAL_OR_KEYWORDr1   )rN   s    r   r.   r.   ø   s6   € Ø�:‰:Ü×Ñ×/Ñ/Ü×Ñ×&Ñ&ðð ð r   Ú
tuple_typec                 ón  — t        | dd«      }| t        j                  u s|dk(  s|€t        j                  S t	        |«      dk(  rt        j                  |d      S t	        |«      dk(  r!|d   t
        u rt        j                  |d      S t        j                  t        j                  t        |«            S )z}
    Convert `tuple_type` into a list type with the same type arguments. Assumes that `tuple_type` is typing.Tuple type.
    Ú__args__N© é   r   é   )Úgetattrr\   ÚTupler   r<   ÚEllipsisr   r9   )r{   Ú	type_argss     r   r:   r:   ÿ   s›   € ô ˜
 J°Ó5€Ið ”V—\‘\Ñ! Y°"¢_¸	Ð8Iä�{‰{ÐÜ	ˆY‹˜1Ò	ä�{‰{˜9 Q™<Ñ(Ð(Ü	ˆY‹˜1Ò	 ¨1¡´Ñ!9Ü�{‰{˜9 Q™<Ñ(Ð(ä�{‰{œ6Ÿ<™<¬¨iÓ(8Ñ9Ñ:Ð:r   )r'   r\   r   r   r   r   rC   r   r   r	   r
   Útorch.utils._exposed_inr   ÚCallabler)   rW   rf   rq   r@   rA   rB   rl   rt   rE   r6   r0   r.   ÚTyper‚   r:   r~   r   r   ú<module>rˆ      sý   ðó Û ß 2Ó 2ã ß .Ó .Ý .ñ ˆOÓð "òT-ØŸ™ðT-ð
 �c‰]ðT-ð 	òT-ó ðT-ònò4ð& ˆHØ
‡K�K�Ñ˜ØˆØ	ˆ7Øˆ&Ø	‡L�L�(ðÐ òPñ6 2Ó3Ð ð˜7×,Ñ,ð °ó ð;˜fŸk™k¨&¯,©,Ñ7ð ;¸F¿K¹KÈÏÉÑ<Tô ;r   