o
    iE                     @   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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 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.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8 ddl9m:Z: ej;j<= rd d	lm>Z> d d
l"m?Z?m@Z@ d dlAmBZB G dd dZCG dd dZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLd d! ZMd"d# ZNd$e
ejO fd%d&ZPd'd( ZQd)d* ZRd0d+d,ZSd0d-d.ZTg d/ZUdS )1    N)dedent)Type)_GeneratorTypeAnyType	AwaitTypeBoolTypeComplexTypeDeviceObjTypeDictTypeEnumType	FloatType
FutureTypeInterfaceTypeIntTypeListTypeNoneType
NumberTypeOptionalTypeStreamObjType
StringType
TensorType	TupleType	UnionType)_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_source_lines_and_file   )_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   S/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/jit/annotations.py__init__H   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__L   s   zModule.__getattr__N)__name__
__module____qualname__r;   r?   r9   r9   r9   r:   r4   G   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;   c   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__h   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   U   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   loc	is_method	signatureparam_typesreturn_type	type_linesourcer9   r9   r:   get_signaturep   s$   

r`   c                 C   s   t | p	t | S r5   )inspect
isfunctionismethodthe_callabler9   r9   r:   is_function_or_method   s   rf   c                 C   s2   t | st| r| j} t | rt| jd uS dS )NF)rf   callable__call__ra   getfullargspecvarargsrd   r9   r9   r:   	is_vararg   s
   rk   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   rf   rg   rh   r$   ra   unwrapri   argsrange)rX   n_argsr9   r9   r:   get_param_names   s   

ru   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   jitfrontendFrontendErrorr6   FunctionDef)rX   rY   r_   py_astr9   r9   r:   check_fn   s$   
  r   c                 C   sD   t | ddd}t|D ]}d|jv rtd|  dqt|||S )zLEvaluate statement as long as it does not contain any method/function calls.rP   eval)modeCALLz/Type annotation should not contain calls, but 'z' does)compiledisget_instructionsopnamer=   r   )stmtglobrY   bytecode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 )zParse 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rm   annrY   r9   r:   ro          z#parse_type_line.<locals>.<listcomp>)	split_type_liner   rC   	NameErrorSyntaxErrorr=   rQ   tupler   )	r^   rH   rY   arg_ann_strret_ann_strarg_anneret_ann	arg_typesr9   r   r:   rW      s2   
rW   c                    s`  d |  d}tt|}tt fdd|}tdttfdd|}t|dkrXtdttfd	d|}t|dkrVtd
t|d d  d d d dS t|dkrf|d d 	 S d}g }|D ]\}}d|v rz||f} n
 |v r|
| ql|du rtdddd |D  d  fdd}t||}	d|	}
|d d|
S )zCTry to find the line containing a comment with the type annotation.# type:
c                    s    | d v S Nr.   r9   line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   )rm   r   r9   r9   r:   ro   (  rp   z!get_type_line.<locals>.<listcomp>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   )findry   strip)r   	item_typer   r9   r:   get_parameter_type,  s   z)get_type_line.<locals>.get_parameter_typez, z...)splitlist	enumeratefilterrer   ry   r=   rl   r   appendrT   mapreplace)r_   lines
type_lineswrong_type_linesreturn_lineparameter_type_linesline_numr   r   types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  Split 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->z4Syntax error in type annotation (couldn't find `->`)N   )ry   index
ValueErrorr=   r   )r^   start_offset	arrow_posr9   r9   r:   r   6  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 )z8Try to use the Py3.5+ annotation syntax to get the type.Nc                 S   s   g | ]}|j qS r9   )
annotationrm   pr9   r9   r:   ro   T  s    z(try_real_annotations.<locals>.<listcomp>c                 3   s    | ]}| j u V  qd S r5   )emptyr   )sigr9   r:   	<genexpr>W  s    z'try_real_annotations.<locals>.<genexpr>c                    s   g | ]}t |j qS r9   )r   r   r   r   r9   r:   ro   Z  s    )ra   r[   r   return_annotation
parametersvaluesallr   )rX   rY   
all_annotsr   r]   r9   )rY   r   r:   rR   J  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)rm   vr9   r9   r:   	<setcomp>f  r   z&get_enum_value_type.<locals>.<setcomp>c                    r   r9   try_ann_to_type)rm   tr   r9   r:   ro   g  r   z'get_enum_value_type.<locals>.<listcomp>)r   r   	__class__rD   _Cunify_type_listr   get)r   rY   enum_valuesr   ir_typesresr9   r   r:   get_enum_value_typea  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   
LongTensorDoubleTensorFloatTensor	IntTensorShortTensor
HalfTensor
CharTensor
ByteTensor
BoolTensorwarningswarn)r   r9   r9   r:   	is_tensors  s(   r   c                 C   s   d S r5   r9   )inpr9   r9   r:   	_fake_rcb  s   r   c                    sD  t | }| tjju rt S | d u rt S t	| r$t
| r$t S t| rCt|dkr8|d dkr8tg S t fdd|D S t| rTt|d  }|rTt|S t| rt|d  }t|d  }|d u rxtd|d  d   |d u rtd|d  d   t||S t| rt|d td r|d }n|d }t| }d}	|sJ |	t| t|t t|S t| r	t|ttthkrt  S g }
t | D ]*}|d u r|
!t  t| }d}	|sJ |	t| t|t |
!| qt"|
S t#j$j%& rt'| rt(t|d  S t)| r,t*t|d  S t+| rC|r;t|d  nt, }t-|S | tu rLt. S | tu rUt/ S | tu s`| t#j0u rdt1 S | t2u rmt3 S | t4u rvt5 S | t6u rt, S | td u rt S t	| rt7| d	rt8| j9S | t#j:u rt; S | t#j<u rt= S | t#j>u rt? S | t#j@u rt1 S t	| rt| tAjBrtC| d u rt#jDjEF|  }|G }ntH| }tI|tJ|  tK| S t	| rtC| }|d ur|S t#jLM| rt#jDjEF|  S |d u rtN}t#jOP|  |S )
Nr.   r   r9   c                    r   r9   r   )rm   ar   r9   r:   ro     r   z#try_ann_to_type.<locals>.<listcomp>Unknown type annotation: '' at zWUnsupported annotation {} could not be resolved because {} could not be resolved. At
{}__torch_script_interface__)QrF   get_argsra   	Signaturer   r   getInferredr   r   isclassr   r'   ry   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!   r   r   r   r   SymIntr   rl   r   boolr   r   hasattrr   r   devicer	   	Generatorr   Streamr   dtypeenumEnumr/   r|   _script_recursive_compile_classqualified_namer   r   r   r   _jit_internalcan_compile_classr   r   _resolve_type_from_object)r   rY   rH   ann_args	elem_typekeyr   	contained
valid_typemsginnerr   
maybe_typeelementTypescripted_classr6   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   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`   r   ru   rW   rU   r   rR   r   r   r5   )Vrw   rN   r   r  ra   r   rF   r   textwrapr   r   rD   torch._Cr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   torch._jit_internalr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   torch._sourcesr-   _stater/   rI   rJ   rK   r0   r1   r2   
torch._opsr3   r4   rC   r`   rf   rk   ru   r   r   rW   rU   r   rR   r  r   r   r   r   r   __all__r9   r9   r9   r:   <module>   sJ   \XC

k