o
    iHE                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlZd dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z  d dl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 dd	l8m9Z9 ej:j;< rd d
lm=Z= ddl#m>Z>m?Z? d dl@mAZA G dd dZBG dd dZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd  ZKd!d" ZLd#d$ ZMd%e
ejN fd&d'ZOd(d) ZPd*d+ ZQd1d,d-ZRd1d.d/ZSg d0ZTdS )2    N)dedent)Type)AnyType	AwaitTypeBoolTypeComplexTypeDeviceObjTypeDictTypeEnumType	FloatType
FutureTypeInterfaceTypeIntTypeListTypeNoneType
NumberTypeOptionalTypeStreamObjType
StringType
TensorType	TupleType	UnionType)get_source_lines_and_file   )_Await_qualified_nameAnyBroadcastingList1BroadcastingList2BroadcastingList3DictFutureis_awaitis_dict	is_futureis_ignored_fnis_listis_optionalis_tupleis_unionListOptionalTupleUnion   )_get_script_class)RRefType)is_rrefRRef)OpOverloadPacketc                   @   s   e Zd Zdd Zdd ZdS )Modulec                 C   s   || _ || _d S N)namemembers)selfr6   r7    r9   d/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torch/jit/annotations.py__init__E   s   
zModule.__init__c                 C   s4   z| j | W S  ty   td| j d| d w )NzModule z has no member called )r7   KeyErrorRuntimeErrorr6   r8   r6   r9   r9   r:   __getattr__I   s   zModule.__getattr__N)__name__
__module____qualname__r;   r?   r9   r9   r9   r:   r4   D   s    r4   c                   @   sN   e Zd Zeddejiejeddeieeee	e
eed
Zdd Zdd	 Zd
S )EvalEnvtorchTensortypingr,   )
rD   rE   rF   r,   r*   r    r+   r-   r!   Awaitc                 C   s$   || _ tjj rt| jd< d S d S )Nr2   )rcbrD   distributedrpcis_availabler2   env)r8   rH   r9   r9   r:   r;   `   s   zEvalEnv.__init__c                 C   s4   || j v r
| j | S | jd ur| |S tt|d S r5   )rL   rH   getattrbuiltinsr>   r9   r9   r:   __getitem__e   s
   



zEvalEnv.__getitem__N)r@   rA   rB   r4   rD   rE   r,   r*   r    r+   r-   r!   r   rL   r;   rO   r9   r9   r9   r:   rC   R   s    rC   c           	      C   s   t | trt| j|}nt| |}|d ur%|r%|\}}|dd  }||f}|d u rRd\}}ztdt| d }t|}W n	 tyG   Y nw |d urRt	|||}|S )Nr.   )NN r   )

isinstancer3   try_real_annotationsopr   joinr   get_type_line	TypeErrorparse_type_line)	fnrH   locZ	is_method	signatureZparam_typesreturn_type	type_linesourcer9   r9   r:   get_signaturem   s$   

r^   c                 C   s   t | p	t | S r5   )inspect
isfunctionismethodZthe_callabler9   r9   r:   is_function_or_method   s   rc   c                 C   s2   t | st| r| j} t | rt| jd uS dS )NF)rc   callable__call__r_   getfullargspecvarargsrb   r9   r9   r:   	is_vararg   s
   rh   c                 C   sh   t | tr| j} t| st| rt| jr| j} t| r+t| r%t| } t	| j
S dd t|D S )Nc                 S   s   g | ]}t |qS r9   )str).0ir9   r9   r:   
<listcomp>       z#get_param_names.<locals>.<listcomp>)rQ   r3   rS   rc   rd   re   r%   r_   unwraprf   argsrange)rX   Zn_argsr9   r9   r:   get_param_names   s   

rq   c              	   C   s   zt dt| d }W n ttfy   Y d S w |d u r d S t|}t|jdkrEt	|jd tj
rEtjj|d|jd j dt|jdksUt	|jd tjs]tjj|dd S )NrP   r   r.   zCannot instantiate class 'z' in a script functionz$Expected a single top-level function)r   rT   r   OSErrorrV   astparselenbodyrQ   ClassDefrD   jitZfrontendZFrontendErrorr6   FunctionDef)rX   rY   r]   Zpy_astr9   r9   r:   check_fn   s$   
  rz   c                 C   sD   t | ddd}t|D ]}d|jv rtd|  dqt|||S )zKEvaluate statement as long as it does not contain any method/function callsrP   eval)modeCALLz/Type annotation should not contain calls, but 'z' does)compiledisget_instructionsopnamer=   r{   )stmtglobrY   bytecodeZinsnr9   r9   r:   _eval_no_call   s   

r   c           	   
      s   t | \}}z
t|i t|}W n ttfy# } ztd|d}~ww t|ts,|f}z
t|i t|}W n ttfyI } ztd|d}~ww  fdd|D }|t| fS )zParses a type annotation specified as a comment.

    Example inputs:
        # type: (Tensor, torch.Tensor) -> Tuple[Tensor]
        # type: (Tensor, Tuple[Tensor, Tensor]) -> Tensor
    z6Failed to parse the argument list of a type annotationNz4Failed to parse the return type of a type annotationc                       g | ]}t | qS r9   )ann_to_typerj   annrY   r9   r:   rl          z#parse_type_line.<locals>.<listcomp>)	split_type_liner   rC   	NameErrorSyntaxErrorr=   rQ   tupler   )	r\   rH   rY   Zarg_ann_strZret_ann_strZarg_anneZret_ann	arg_typesr9   r   r:   rW      s2   
rW   c                    sf  d |  d}dd t|D }tt fdd|}tdttfdd|}t|d	kr[td
ttfdd|}t|d	krYtdt|d	 d	  d d d dS t|dkri|d	 d 	 S d}g }|D ]\}}d|v r}||f} n
 |v r|
| qo|du rtdddd |D  d  fdd}t||}	d|	}
|d d|
S )zETries to find the line containing a comment with the type annotation.# type:
c                 S   s   g | ]\}}||fqS r9   r9   )rj   line_numliner9   r9   r:   rl          z!get_type_line.<locals>.<listcomp>c                    s    | d v S Nr.   r9   r   type_commentr9   r:   <lambda>   s    zget_type_line.<locals>.<lambda>z!# type:\ ignore(\[[a-zA-Z-]+\])?$c                    s     | d  S r   searchr   )type_patternr9   r:   r     s    r   z&#[	 ]*type[	 ]*(?!: ignore(\[.*\])?$):c                    s     | d S r   r   r   )wrong_type_patternr9   r:   r     s    zThe annotation prefix in line z* is probably invalid.
It must be '# type:'zl
See PEP 484 (https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code)z
for examplesNr.   z# type: (...) -> z_Return type line '# type: (...) -> ...' not found on multiline type annotation
for type lines:
c                 S   s   g | ]}|d  qS )r.   r9   )rj   r   r9   r9   r:   rl   %  rm   zl
(See PEP 484 https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code)c                    s"   | |   t  d  }| S r5   )findru   strip)r   Z	item_typer   r9   r:   get_parameter_type)  s   z)get_type_line.<locals>.get_parameter_typez, z...)split	enumeratelistfilterrer~   ru   r=   ri   r   appendrT   mapreplace)r]   linesZ
type_linesZwrong_type_linesZreturn_lineZparameter_type_linesr   r   r   typesZparameter_typesr9   )r   r   r   r:   rU      s^   





rU   c                 C   sT   t d}z| d}W n ty   tddw | ||  | |d d  fS )a  Splits the comment with the type annotation into parts for argument and return types.

    For example, for an input of:
        # type: (Tensor, torch.Tensor) -> Tuple[Tensor, Tensor]

    This function will return:
        ("(Tensor, torch.Tensor)", "Tuple[Tensor, Tensor]")

    r   z->z3Syntax error in type annotation (cound't find `->`)Nr   )ru   index
ValueErrorr=   r   )r\   start_offsetZ	arrow_posr9   r9   r:   r   3  s   
$r   c                    s   zt | W n
 ty   Y dS w jgdd j D  }tfdd|D r-dS  fddj D }tj }||fS )z:Tries to use the Py3.5+ annotation syntax to get the type.Nc                 S   s   g | ]}|j qS r9   )
annotationrj   pr9   r9   r:   rl   Q  s    z(try_real_annotations.<locals>.<listcomp>c                 3   s    | ]}| j u V  qd S r5   )emptyr   )sigr9   r:   	<genexpr>T  s    z'try_real_annotations.<locals>.<genexpr>c                    s   g | ]}t |j qS r9   )r   r   r   r   r9   r:   rl   W  r   )r_   rZ   r   return_annotation
parametersvaluesallr   )rX   rY   Z
all_annotsr   r[   r9   )rY   r   r:   rR   G  s   rR   r   c                    sZ   t | }|std| j ddd |D } fdd|D }tj|}|s+t S |S )NzNo enum values defined for: ''c                 S   s   h | ]}t |jqS r9   )typevalue)rj   vr9   r9   r:   	<setcomp>c  r   z&get_enum_value_type.<locals>.<setcomp>c                    r   r9   try_ann_to_type)rj   tr   r9   r:   rl   d  r   z'get_enum_value_type.<locals>.<listcomp>)r   r   	__class__rD   _CZunify_type_listr   get)r   rY   Zenum_valuesr   Zir_typesresr9   r   r:   get_enum_value_type^  s   r   c                 C   sP   t | tjrdS t | tjtjtjtjtjtjtj	tj
tjf	r&td dS dS )NTzTorchScript will treat type annotations of Tensor dtype-specific subtypes as if they are normal Tensors. dtype constraints are not enforced in compilation either.F)
issubclassrD   rE   Z
LongTensorZDoubleTensorZFloatTensorZ	IntTensorZShortTensorZ
HalfTensorZ
CharTensorZ
ByteTensorZ
BoolTensorwarningswarn)r   r9   r9   r:   	is_tensorp  s(   r   c                 C   s   d S r5   r9   )inpr9   r9   r:   	_fake_rcb  s   r   c                    s<  | t jju r
t S | d u rt S t | rt| rt S t	| rAt
| jdkr5| jd dkr5tg S t fdd| jD S t| rSt| jd  }|rSt|S t| rt| jd  }t| jd  }|d u rztd| jd  d   |d u rtd| jd  d   t||S t| rt| jd td r| jd }n| jd }t| }d}|sJ |t| t|t t|S t| rt| jttthkrt S g }	| jD ]+}
|
d u r|	 t  t|
 }d}|sJ |t| t|t |	 | qt!|	S t"j#j$% r%t&| r%t't| jd  S t(| r4t)t| jd  S t*| rOt+| d	rGt| jd  nt, }t-|S | tu rXt. S | tu rat/ S | tu rjt0 S | t1u rst2 S | t3u r|t4 S | t5u rt, S | td u rt S t | rt+| d
rt6| j7S | t"j8u rt9 S | t"j:u rt; S | t"j<u rt0 S t | rt| t=j>rt?| d u rt"j@jAB|  }|C }ntD| }tE|tF|  tG| S t | rt?| }|d ur |S t"jHI| rt"j@jAB|  S |d u rtJ}t"jKL|  |S )Nr.   r   r9   c                    r   r9   r   )rj   ar   r9   r:   rl     r   z#try_ann_to_type.<locals>.<listcomp>Unknown type annotation: '' at zWUnsupported annotation {} could not be resolved because {} could not be resolved. At
{}__args____torch_script_interface__)Mr_   	Signaturer   r   ZgetInferredr   r   isclassr   r(   ru   r   r   r&   r   r   r#   r   	highlightr	   r'   r   r   formatreprr   r)   setintfloatcomplexr   r   r   rD   rI   rJ   rK   r1   r0   r$   r   r"   hasattrr   r   r   r   r   ri   r   boolr   r   r   r   Zdevicer   ZStreamr   ZdtypeenumEnumr/   rx   _scriptZ_recursive_compile_classZqualified_namer   r
   r   r   _jit_internalZcan_compile_classr   r   Z_resolve_type_from_object)r   rY   rH   Z	elem_typekeyr   	containedZ
valid_typemsginnerr   Z
maybe_typeZelementTypeZscripted_classr6   Zmaybe_script_classr9   r   r:   r     s   


"


$











r   c                 C   s0   t | ||}|d ur|S td|  d|  )Nr   r   )r   r   r   )r   rY   rH   Zthe_typer9   r9   r:   r     s   r   )r   r*   r   r   r   r,   r(   r&   r    r#   r'   r)   r   r   r   r   r   r   r   r	   r   r4   r^   rz   rq   rW   rU   r   rR   r   r   r5   )Urs   rN   r   r   r_   r   r   textwrapr   rF   r   rD   Ztorch._Cr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   Ztorch._sourcesr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   _stater/   rI   rJ   rK   r0   r1   r2   Z
torch._opsr3   r4   rC   r^   rc   rh   rq   rz   r   rW   rU   r   rR   r   r   r   r   r   r   __all__r9   r9   r9   r:   <module>   sH    XXC

k