o
    iSF                     @   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
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.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7 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 ZGd0ddZHdd ZIdd ZJdd ZKd d! ZLd"d# ZMd$eNejO fd%d&ZPdeQfd'd(ZRd0d)d*ZSd1d+d,ZTd1d-d.ZUg d/ZVdS )2    N)dedent)_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dZdd ZdS )ModulereturnNc                 C   s   || _ || _d S N)namemembers)selfr6   r7    r9   W/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/jit/annotations.py__init__G   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__K   s   zModule.__getattr__r4   N)__name__
__module____qualname__r;   r?   r9   r9   r9   r:   r3   F   s    
r3   c                   @   sP   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d	Zd
d ZdS )EvalEnvtorchTensortypingr*   )
rE   rF   rG   r*   r(   r   r)   r+   r   Awaitr4   Nc                 C   s$   || _ tjj rt| jd< d S d S )Nr1   )rcbrE   distributedrpcis_availabler1   env)r8   rI   r9   r9   r:   r;   b   s   zEvalEnv.__init__c                 C   s4   || j v r
| j | S | jd ur| |S tt|d S r5   )rM   rI   getattrbuiltinsr>   r9   r9   r:   __getitem__h   s
   



zEvalEnv.__getitem__r@   )rA   rB   rC   r3   rE   rF   r*   r(   r   r)   r+   r   r   rM   r;   rP   r9   r9   r9   r:   rD   T   s    
rD   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   )

isinstancer2   try_real_annotationsopr   joinr,   get_type_line	TypeErrorparse_type_line)	fnrI   loc	is_method	signatureparam_typesreturn_type	type_linesourcer9   r9   r:   get_signaturep   s$   

ra   c                 C   s   t | p	t | S r5   )inspect
isfunctionismethodthe_callabler9   r9   r:   is_function_or_method   s   rg   c                 C   s2   t | st| r| j} t | rt| jd uS dS )NF)rg   callable__call__rb   getfullargspecvarargsre   r9   r9   r:   	is_vararg   s
   rl   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>)rR   r2   rT   rg   rh   ri   r#   rb   unwraprj   argsrange)rY   n_argsr9   r9   r:   get_param_names   s   

rv   r4   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 )NrQ   r   r-   zCannot instantiate class 'z' in a script functionz$Expected a single top-level function)r   rU   r,   OSErrorrW   astparselenbodyrR   ClassDefrE   jitfrontendFrontendErrorr6   FunctionDef)rY   rZ   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.rQ   eval)modeCALLz/Type annotation should not contain calls, but 'z' does)compiledisget_instructionsopnamer=   r   )stmtglobrZ   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rn   annrZ   r9   r:   rp          z#parse_type_line.<locals>.<listcomp>)	split_type_liner   rD   	NameErrorSyntaxErrorr=   rR   tupler   )	r_   rI   rZ   arg_ann_strret_ann_strarg_anneret_ann	arg_typesr9   r   r:   rX      s2   
rX   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   )rn   r   r9   r9   r:   rp   (  rq   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   )findrz   strip)r   	item_typer   r9   r:   get_parameter_type,  s   z)get_type_line.<locals>.get_parameter_typez, z...)splitlist	enumeratefilterrer   rz   r=   rm   r   appendrU   mapreplace)r`   lines
type_lineswrong_type_linesreturn_lineparameter_type_linesline_numr   r   typesparameter_typesr9   )r   r   r   r:   rV      s^   





rV   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   )rz   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rn   pr9   r9   r:   rp   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:   rp   Z  s    )rb   r\   r   return_annotation
parametersvaluesallr   )rY   rZ   
all_annotsr   r^   r9   )rZ   r   r:   rS   J  s   rS   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)rn   vr9   r9   r:   	<setcomp>f  r   z&get_enum_value_type.<locals>.<setcomp>c                    r   r9   try_ann_to_type)rn   tr   r9   r:   rp   g  r   z'get_enum_value_type.<locals>.<listcomp>)r   r   	__class__rE   _Cunify_type_listr   get)r   rZ   enum_valuesr   ir_typesresr9   r   r:   get_enum_value_typea  s   r   c                 C   sT   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jdd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.r   )
stacklevelF)
issubclassrE   rF   
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                    sX  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#jAu rt1 S t	| rt| tBjCrtD| d u rt#jEjFG|  }|H }ntI| }tJ|tK|  tL| S t	| rtD| }|d ur|S t#jMN| rt#jEjFG|  S |d u r$tO}t#jPQ|  |S )
Nr-   r   r9   c                    r   r9   r   )rn   ar   r9   r:   rp     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__)RrG   get_argsrb   	Signaturer   r   getInferredr   r   isclassr   r&   rz   r   r$   r   r   r!   r   	highlightr	   r%   r   r   formatreprr   r'   setintfloatcomplexr   r   r   rE   rJ   rK   rL   r0   r/   r"   r   r    r   r   r   r   SymIntr   rm   r   boolr   r   hasattrr   r   devicer   	Generatorr   Streamr   dtypeqschemeenumEnumr.   r}   _script_recursive_compile_classqualified_namer   r
   r   r   _jit_internalcan_compile_classr   r   _resolve_type_from_object)r   rZ   rI   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   rZ   rI   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   r3   ra   r   rv   rX   rV   r   rS   r   r   r@   r5   )Wrx   rO   r   r  rb   r   rG   r   textwrapr   rE   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.   rJ   rK   rL   r/   r0   r1   
torch._opsr2   r3   rD   ra   rg   rl   rv   r   r   rX   rV   r   rS   r   r  r   r   r   r   r   r   __all__r9   r9   r9   r:   <module>   sH   \X
C


n