o
    htE                     @   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 ZGdd ZHdd ZIdd ZJdd ZKdd  ZLd!d" ZMd#eNejO fd$d%ZPd&d' ZQd(d) ZRd/d*d+ZSd/d,d-ZTg d.ZUdS )0    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 Zdd ZdS )Modulec                 C   s   || _ || _d S N)namemembers)selfr5   r6    r8   i/var/www/html/construction_image-detection-poc/venv/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 )r6   KeyErrorRuntimeErrorr5   r7   r5   r8   r8   r9   __getattr__K   s   zModule.__getattr__N)__name__
__module____qualname__r:   r>   r8   r8   r8   r9   r3   F   s    r3   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*   )
rC   rD   rE   r*   r(   r   r)   r+   r   Awaitc                 C   s$   || _ tjj rt| jd< d S d S )Nr1   )rcbrC   distributedrpcis_availabler1   env)r7   rG   r8   r8   r9   r:   b   s   zEvalEnv.__init__c                 C   s4   || j v r
| j | S | jd ur| |S tt|d S r4   )rK   rG   getattrbuiltinsr=   r8   r8   r9   __getitem__g   s
   



zEvalEnv.__getitem__N)r?   r@   rA   r3   rC   rD   r*   r(   r   r)   r+   r   r   rK   r:   rN   r8   r8   r8   r9   rB   T   s    rB   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)	fnrG   loc	is_method	signatureparam_typesreturn_type	type_linesourcer8   r8   r9   get_signatureo   s$   

r_   c                 C   s   t | p	t | S r4   )inspect
isfunctionismethodthe_callabler8   r8   r9   is_function_or_method   s   re   c                 C   s2   t | st| r| j} t | rt| jd uS dS )NF)re   callable__call__r`   getfullargspecvarargsrc   r8   r8   r9   	is_vararg   s
   rj   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 r8   )str).0ir8   r8   r9   
<listcomp>       z#get_param_names.<locals>.<listcomp>)rP   r2   rR   re   rf   rg   r#   r`   unwraprh   argsrange)rW   n_argsr8   r8   r9   get_param_names   s   

rt   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 )NrO   r   r-   zCannot instantiate class 'z' in a script functionz$Expected a single top-level function)r   rS   r,   OSErrorrU   astparselenbodyrP   ClassDefrC   jitfrontendFrontendErrorr5   FunctionDef)rW   rX   r^   py_astr8   r8   r9   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.rO   eval)modeCALLz/Type annotation should not contain calls, but 'z' does)compiledisget_instructionsopnamer<   r   )stmtglobrX   bytecodeinsnr8   r8   r9   _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 r8   )ann_to_typerl   annrX   r8   r9   rn          z#parse_type_line.<locals>.<listcomp>)	split_type_liner   rB   	NameErrorSyntaxErrorr<   rP   tupler   )	r]   rG   rX   arg_ann_strret_ann_strarg_anneret_ann	arg_typesr8   r   r9   rV      s2   
rV   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-   r8   linetype_commentr8   r9   <lambda>   s    zget_type_line.<locals>.<lambda>z!# type:\ ignore(\[[a-zA-Z-]+\])?$c                    s     | d  S r   searchr   )type_patternr8   r9   r     s    r   z&#[	 ]*type[	 ]*(?!: ignore(\[.*\])?$):c                    s     | d S r   r   r   )wrong_type_patternr8   r9   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-   r8   )rl   r   r8   r8   r9   rn   '  ro   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 r4   )findrx   strip)r   	item_typer   r8   r9   get_parameter_type+  s   z)get_type_line.<locals>.get_parameter_typez, z...)splitlist	enumeratefilterrer   rx   r<   rk   r   appendrS   mapreplace)r^   lines
type_lineswrong_type_linesreturn_lineparameter_type_linesline_numr   r   typesparameter_typesr8   )r   r   r   r9   rT      s^   





rT   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   )rx   index
ValueErrorr<   r   )r]   start_offset	arrow_posr8   r8   r9   r   5  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 r8   )
annotationrl   pr8   r8   r9   rn   S  s    z(try_real_annotations.<locals>.<listcomp>c                 3   s    | ]}| j u V  qd S r4   )emptyr   )sigr8   r9   	<genexpr>V  s    z'try_real_annotations.<locals>.<genexpr>c                    s   g | ]}t |j qS r8   )r   r   r   r   r8   r9   rn   Y  s    )r`   rZ   r   return_annotation
parametersvaluesallr   )rW   rX   
all_annotsr   r\   r8   )rX   r   r9   rQ   I  s   rQ   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 r8   )typevalue)rl   vr8   r8   r9   	<setcomp>e  r   z&get_enum_value_type.<locals>.<setcomp>c                    r   r8   try_ann_to_type)rl   tr   r8   r9   rn   f  r   z'get_enum_value_type.<locals>.<listcomp>)r   r   	__class__rC   _Cunify_type_listr   get)r   rX   enum_valuesr   ir_typesresr8   r   r9   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)
issubclassrC   rD   
LongTensorDoubleTensorFloatTensor	IntTensorShortTensor
HalfTensor
CharTensor
ByteTensor
BoolTensorwarningswarn)r   r8   r8   r9   	is_tensorr  s(   r   c                 C   s   d S r4   r8   )inpr8   r8   r9   	_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   r8   c                    r   r8   r   )rl   ar   r8   r9   rn     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__)QrE   get_argsr`   	Signaturer   r   getInferredr   r   isclassr   r&   rx   r   r$   r   r   r!   r   	highlightr	   r%   r   r   formatreprr   r'   setintfloatcomplexr   r   r   rC   rH   rI   rJ   r0   r/   r"   r   r    r   r   r   r   SymIntr   rk   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   rX   rG   ann_args	elem_typekeyr   	contained
valid_typemsginnerr   
maybe_typeelementTypescripted_classr5   maybe_script_classr8   r   r9   r     s   



"

"










r   c                 C   s0   t | ||}|d ur|S td|  d|  )Nr   r   )r   r   r   )r   rX   rG   the_typer8   r8   r9   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   r_   r   rt   rV   rT   r   rQ   r   r   r4   )Vrv   rM   r   r  r`   r   rE   r   textwrapr   rC   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.   rH   rI   rJ   r/   r0   r1   
torch._opsr2   r3   rB   r_   re   rj   rt   r   r   rV   rT   r   rQ   r   r  r   r   r   r   r   __all__r8   r8   r8   r9   <module>   sH   \XC

k