a
    dB                     @   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	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 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+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5 d d	l6m7Z7 d d
l8m9Z9 ej:j;< rRddlm=Z=m>Z> d dl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% ZMe9ejN d&d'd(ZOd)d* ZPd+d, ZQd-d. ZRg d/ZSdS )0    N   )ListTupleis_tupleis_listDictis_dictOptionalis_optional_qualified_nameAnyFuture	is_future_Awaitis_awaitis_ignored_fnUnionis_union)BroadcastingList1BroadcastingList2BroadcastingList3   )_get_script_class)
TensorType	TupleType	FloatTypeIntTypeComplexTypeListType
StringTypeDictTypeBoolTypeOptionalTypeInterfaceTypeAnyTypeNoneTypeDeviceObjTypeStreamObjType
FutureType	AwaitTypeEnumType	UnionType
NumberType)dedent)get_source_lines_and_file)Type)RRefis_rref)RRefType)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   ^/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/jit/annotations.py__init__   s    zModule.__init__c                 C   s<   z| j | W S  ty6   td| j d| d Y n0 d S )NzModule z has no member called )r7   KeyErrorRuntimeErrorr6   r8   r6   r9   r9   r:   __getattr__"   s    zModule.__getattr__N)__name__
__module____qualname__r;   r?   r9   r9   r9   r:   r4      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 )Nr0   )rcbrD   distributedrpcis_availabler0   env)r8   rH   r9   r9   r:   r;   7   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__<   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   )   s   rC   c           	      C   s   t | trt| j|}n
t| |}|d urJ|rJ|\}}|dd  }||f}|d u rd\}}z"tdt| d }t|}W n ty   Y n0 |d urt	|||}|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_signatureC   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   s8   t | st| dr| j} t | r0t| jd uS dS d S )N__call__F)rc   hasattrrd   r_   getfullargspecvarargsrb   r9   r9   r:   	is_varargf   s
    rh   c                 C   sn   t | tr| j} t| s2t| dr2t| jr2| j} t| rXt| rLt| } t	| j
S dd t|D S d S )Nrd   c                 S   s   g | ]}t |qS r9   )str).0ir9   r9   r:   
<listcomp>       z#get_param_names.<locals>.<listcomp>)rQ   r3   rS   rc   re   rd   r   r_   unwraprf   argsrange)rX   Zn_argsr9   r9   r:   get_param_namesr   s    

rq   c              	   C   s   zt dt| d }W n ttfy2   Y d S 0 |d u r@d 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 )NrP   r   r   zCannot instantiate class 'z' in a script functionz$Expected a single top-level function)r-   rT   r.   rV   IOError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 n2 ttfyR } ztd|W Y d}~n
d}~0 0 t|tsd|f}zt|i t|}W n2 ttfy } ztd|W Y d}~n
d}~0 0  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                    s   g | ]}t | qS r9   )ann_to_typerj   annrY   r9   r:   rl      rm   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      s     
 rW   c                    sn  d |  d}dd t|D }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 ]4\}}d|v r ||f} qqވ |v r|
| q|du r>t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      rm   z!get_type_line.<locals>.<listcomp>c                    s    | d v S Nr   r9   r   type_commentr9   r:   <lambda>   rm   zget_type_line.<locals>.<lambda>z!# type:\ ignore(\[[a-zA-Z-]+\])?$c                    s     | d  S r   searchr   )type_patternr9   r:   r      rm   r   z&#[	 ]*type[	 ]*(?!: ignore(\[.*\])?$):c                    s     | d S r   r   r   )wrong_type_patternr9   r:   r      rm   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      sT    






rU   c                 C   sX   t d}z| d}W n ty2   tddY n0 | ||  | |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\   Zstart_offsetZ	arrow_posr9   r9   r:   r      s    
r   c                    s   zt | W n ty"   Y dS 0 jgdd j D  }tfdd|D rZd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     rm   z(try_real_annotations.<locals>.<listcomp>c                 3   s   | ]}| j u V  qd S r5   )emptyr   )sigr9   r:   	<genexpr>  rm   z'try_real_annotations.<locals>.<genexpr>c                    s   g | ]}t |j qS r9   )r   r   r   r   r9   r:   rl     s   )r_   rZ   r   return_annotation
parametersvaluesallr   )rX   rY   Z
all_annotsr   r[   r9   )rY   r   r:   rR     s    
rR   )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 r9   )typevalue)rj   vr9   r9   r:   	<setcomp>#  rm   z&get_enum_value_type.<locals>.<setcomp>c                    s   g | ]}t | qS r9   try_ann_to_type)rj   tr   r9   r:   rl   $  rm   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	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)
issubclassrD   rE   Z
LongTensorZDoubleTensorZFloatTensorZ	IntTensorZShortTensorZ
HalfTensorZ
CharTensorZ
ByteTensorZ
BoolTensorwarningswarn)r   r9   r9   r:   	is_tensor/  s    
r   c                    s:  | t jju rt S | d u r$t S t | r>t| r>t S t	| rt
| jdkrj| jd dkrjtg S t fdd| jD S t| rt| jd  }|rt|S t| r(t| jd  }t| jd  }|d u rtd| jd  d   |d u rtd| jd  d   t||S t| rt| jd td rT| jd }n
| jd }t| }d}|sJ |t| t|t|S t| r"t| jttthkrt S g }| jD ]T}	|	d u r| t  t|	 }
d}|
sJ |t| t|
| |
 qt!|S t"j#j$% rNt&| rNt't| jd  S t(| rlt)t| jd  S t*| rt+| d	rt| jd  nt, }t-|S | tu rt. S | tu rt/ S | tu rt0 S | t1u rt2 S | t3u rt4 S | t5u rt, S | td u r$t S t | rFt+| d
rFt6| j7S | t"j8u rZt9 S | t"j:u rnt; 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d }t"jJK|  |S )Nr   r   r9   c                    s   g | ]}t | qS r9   r   )rj   ar   r9   r:   rl   J  rm   z#try_ann_to_type.<locals>.<listcomp>Unknown type annotation: '' at zQUnsupported annotation {} could not be resolved because {} could not be resolved.__args____torch_script_interface__c                 S   s   d S r5   r9   )keyr9   r9   r:   fake_rcb  s    z!try_ann_to_type.<locals>.fake_rcb)Lr_   	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   r2   r   r(   r   re   r$   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   Z_resolve_type_from_object)r   rY   Z	elem_typer   r   Z	containedZ
valid_typemsginnerr   Z
maybe_typeZelementTypeZscripted_classr6   Zmaybe_script_classr   r9   r   r:   r   ?  s    











$








r   c                 C   s2   t | |}|d ur|S td|  d|  d S )Nr   r   )r   r   r   )r   rY   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   )Trs   r   r   r_   r   rN   rD   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   _stater   Ztorch._Cr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   textwrapr-   Ztorch._sourcesr.   rF   r/   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   __all__r9   r9   r9   r:   <module>   sF   LX	>c