U
    whE                     @   sL  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# 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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 dd	l9m:Z: ej;j<= rVd 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$ ZNe
ejO d%d&d'ZPd(d) ZQd*d+ ZRdEd,d-ZSdFd.d/ZTd0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdddddd d"d$d-d/gZUdS )G    N)dedent)Type)_GeneratorType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)selfr7   r8    r:   G/var/www/html/venv/lib/python3.8/site-packages/torch/jit/annotations.py__init__H   s    zModule.__init__c                 C   s>   z| j | W S  tk
r8   td| j d| d Y nX d S )NzModule z has no member called )r8   KeyErrorRuntimeErrorr7   r9   r7   r:   r:   r;   __getattr__L   s    zModule.__getattr__N)__name__
__module____qualname__r<   r@   r:   r:   r:   r;   r5   G   s   r5   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-   )
rE   rF   rG   r-   r+   r!   r,   r.   r"   Awaitc                 C   s    || _ tjj rt| jd< d S )Nr3   )rcbrE   distributedrpcis_availabler3   env)r9   rI   r:   r:   r;   r<   c   s    zEvalEnv.__init__c                 C   s4   || j kr| j | S | jd k	r(| |S tt|d S r6   )rM   rI   getattrbuiltinsr?   r:   r:   r;   __getitem__h   s
    



zEvalEnv.__getitem__N)rA   rB   rC   r5   rE   rF   r-   r+   r!   r,   r.   r"   r   rM   r<   rP   r:   r:   r:   r;   rD   U   s   rD   c           	      C   s   t | trt| j|}n
t| |}|d k	rJ|rJ|\}}|dd  }||f}|d krd\}}z"tdt| d }t|}W n tk
r   Y nX |d k	rt	|||}|S )Nr/   )NN r   )

isinstancer4   try_real_annotationsopr   joinr   get_type_line	TypeErrorparse_type_line)	fnrI   locZ	is_method	signatureZparam_typesreturn_type	type_linesourcer:   r:   r;   get_signaturep   s"    

r_   c                 C   s   t | pt | S r6   )inspect
isfunctionismethodZthe_callabler:   r:   r;   is_function_or_method   s    rd   c                 C   s6   t | st| r| j} t | r.t| jd k	S dS d S )NF)rd   callable__call__r`   getfullargspecvarargsrc   r:   r:   r;   	is_vararg   s
    ri   c                 C   sl   t | tr| j} t| s0t| r0t| jr0| j} t| rVt| rJt| } t	| j
S dd t|D S d S )Nc                 S   s   g | ]}t |qS r:   )str).0ir:   r:   r;   
<listcomp>   s     z#get_param_names.<locals>.<listcomp>)rR   r4   rT   rd   re   rf   r&   r`   unwraprg   argsrange)rY   Zn_argsr:   r:   r;   get_param_names   s    

rq   c              	   C   s   zt dt| d }W n ttfk
r4   Y d S X |d krBd S t|}t|jdkrt	|jd tj
rtjj|d|jd j dt|jdkst	|jd tjst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   jitZfrontendZFrontendErrorr7   FunctionDef)rY   rZ   r^   Zpy_astr:   r:   r;   check_fn   s"    
   rz   c                 C   sD   t | ddd}t|D ]}d|jk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   bytecodeZinsnr:   r:   r;   _eval_no_call   s    

r   c           	   
      s   t | \}}zt|i t|}W n0 ttfk
rP } ztd|W 5 d}~X Y nX t|tsb|f}zt|i t|}W n0 ttfk
r } ztd|W 5 d}~X Y nX  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                    s   g | ]}t | qS r:   )ann_to_typerk   annrZ   r:   r;   rm      s     z#parse_type_line.<locals>.<listcomp>)	split_type_liner   rD   	NameErrorSyntaxErrorr>   rR   tupler   )	r]   rI   rZ   Zarg_ann_strZret_ann_strZarg_anneZret_ann	arg_typesr:   r   r;   rX      s*    
rX   c                    sf  d |  d}tt|}tt fdd|}tdttfdd|}t|dkrtdttfd	d|}t|dkrtd
t|d d  d d d dS t|dkr|d d 	 S d}g }|D ]2\}}d|kr||f} qq؈ |kr|
| q|dkr6t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 kS Nr/   r:   linetype_commentr:   r;   <lambda>       zget_type_line.<locals>.<lambda>z!# type:\ ignore(\[[a-zA-Z-]+\])?$c                    s     | d  S r   searchr   )type_patternr:   r;   r     r   r   z&#[	 ]*type[	 ]*(?!: ignore(\[.*\])?$):c                    s     | d S r   r   r   )wrong_type_patternr:   r;   r     r   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/   r:   )rk   r   r:   r:   r;   rm   (  s     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 r6   )findru   strip)r   Z	item_typer   r:   r;   get_parameter_type,  s    z)get_type_line.<locals>.get_parameter_typez, z...)splitlist	enumeratefilterrer~   ru   r>   rj   r   appendrU   mapreplace)r^   linesZ
type_linesZwrong_type_linesZreturn_lineZparameter_type_linesline_numr   r   typesZparameter_typesr:   )r   r   r   r;   rV      s\    





rV   c                 C   sZ   t d}z| d}W n tk
r4   tddY nX | ||  | |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 `->`)Nr   )ru   index
ValueErrorr>   r   )r]   Zstart_offsetZ	arrow_posr:   r:   r;   r   6  s    
r   c                    s   zt | W n tk
r$   Y dS X 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 r:   )
annotationrk   pr:   r:   r;   rm   T  s    z(try_real_annotations.<locals>.<listcomp>c                 3   s   | ]}| j kV  qd S r6   )emptyr   )sigr:   r;   	<genexpr>W  s     z'try_real_annotations.<locals>.<genexpr>c                    s   g | ]}t |j qS r:   )r   r   r   r   r:   r;   rm   Z  s     )r`   r[   r   return_annotation
parametersvaluesallr   )rY   rZ   Z
all_annotsr   r\   r:   )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|}|sVt S |S )NzNo enum values defined for: ''c                 S   s   h | ]}t |jqS r:   )typevalue)rk   vr:   r:   r;   	<setcomp>f  s     z&get_enum_value_type.<locals>.<setcomp>c                    s   g | ]}t | qS r:   try_ann_to_type)rk   tr   r:   r;   rm   g  s     z'get_enum_value_type.<locals>.<listcomp>)r   r   	__class__rE   _CZunify_type_listr   get)r   rZ   Zenum_valuesr   Zir_typesresr:   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	rLt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)
issubclassrE   rF   Z
LongTensorZDoubleTensorZFloatTensorZ	IntTensorZShortTensorZ
HalfTensorZ
CharTensorZ
ByteTensorZ
BoolTensorwarningswarn)r   r:   r:   r;   	is_tensors  s(    r   c                 C   s   d S r6   r:   )Zinpr:   r:   r;   	_fake_rcb  s    r   c                    sV  t | }| tjjkrt S | d kr.t S t	| rHt
| rHt S t| rt|dkrp|d dkrptg S t fdd|D S t| rt|d  }|rt|S t| r"t|d  }t|d  }|d krtd|d  d   |d krtd|d  d   t||S t| rt|d td rJ|d }n|d }t| }d}	|st|	t| t|t t|S t| r$t|ttt hkrt! S g }
t | D ]Z}|d kr|
"t  t| }d}	|st|	t| t|t |
"| qt#|
S t$j%j&' rNt(| rNt)t|d  S t*| rjt+t|d  S t,| r|rt|d  nt- }t.|S | tkrt/ S | t krt0 S | tks| t$j1krt2 S | t3krt4 S | t5krt6 S | t7krt- S | td kr&t S t	| rHt8| d	rHt9| j:S | t$j;kr\t< S | t$j=krpt> S | t$j?krt@ S | t$jAkrt2 S t	| rt| tBjCrtD| d krt$jEjFG|  }|H }ntI| }tJ|tK|  tL| S t	| r8tD| }|d k	r|S t$jMN| r8t$jEjFG|  S |d krFtO}t$jPQ|  |S )
Nr/   r   r:   c                    s   g | ]}t | qS r:   r   )rk   ar   r:   r;   rm     s     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_argsr`   	Signaturer   r   ZgetInferredr   r   isclassr   r)   ru   r   r'   r   r   r$   r   	highlightr
   r(   r   r   AssertionErrorformatreprr   r*   setintfloatcomplexr   r   r   rE   rJ   rK   rL   r2   r1   r%   r   r#   r   r   r   r   ZSymIntr   rj   r   boolr   r   hasattrr   r   Zdevicer	   	Generatorr   ZStreamr   ZdtypeenumEnumr0   rx   _scriptZ_recursive_compile_classZqualified_namer   r   r   r   _jit_internalZcan_compile_classr   r   Z_resolve_type_from_object)r   rZ   rI   Zann_argsZ	elem_typekeyr   Z	containedZ
valid_typemsginnerr   Z
maybe_typeZelementTypeZscripted_classr7   Zmaybe_script_classr:   r   r;   r     s    






$


$










r   c                 C   s4   t | ||}|d k	r|S td|  d|  d S )Nr   r   )r   r   r   )r   rZ   rI   Zthe_typer:   r:   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   )N)N)Vrs   rO   r   r   r`   r   rG   r   textwrapr   r   rE   Ztorch._Cr   r   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.   _stater0   rJ   rK   rL   r1   r2   r3   Z
torch._opsr4   r5   rD   r_   rd   ri   rq   rz   r   rX   rV   r   rS   r   r   r   r   r   r   __all__r:   r:   r:   r;   <module>   s   \XC
k
