a
    hL                     @   sp  d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	Z	d dl
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 d dlmZ erd dlmZ G d	d
 d
eZeedddZe	jj ddddZ!e	jj ddddZ"e	jj ddddZ#G dd de$Z%ee&edddZ'G dd de%dZ(G dd de(Z)ddd d!Z*ddd"d#Z+e&e$e( d$d%d&Z,dS )'    N)Iterable)AnyfinalTYPE_CHECKING)HigherOrderOperator
OpOverload)
FakeTensor)CustomObjArgument	InputKindSymBoolArgumentSymFloatArgumentSymIntArgumentTensorArgumentTokenArgument)GraphModule)ExportedProgramc                   @   s   e Zd ZdS )SpecViolationErrorN)__name__
__module____qualname__ r   r   D/var/www/auris/lib/python3.9/site-packages/torch/_export/verifier.pyr      s   r   )opreturnc                 C   s
   | j j S N)_schemaZ
is_mutabler   r   r   r   is_functional   s    r   )noder   c                 C   s   t | S r   )
_check_val)r   r   r   r   _check_has_fake_tensor#   s    r    c                    s   ddl m mm  fdddd }d| jvrb| jdkrP|| jrPd S td	| j d
| jd }|std	| j d| d S )Nr   )SymBoolSymFloatSymIntc                    s   | d u rdS t | ttttfr"dS t | tjtjtjtj	fr@dS t | t
tjfrTdS t |  frhdS t | trvdS t | trtfdd| D S dS )NTc                 3   s   | ]} |V  qd S r   r   ).0x)_check_correct_valr   r   	<genexpr>=       z9_check_val.<locals>._check_correct_val.<locals>.<genexpr>F)
isinstanceintboolstrfloattorchZmemory_formatZdtypeZdeviceZlayoutr   Tensorr	   r   all)valr!   r"   r#   r&   r   r   r&   +   s&    


z&_check_val.<locals>._check_correct_valc                 S   s   t | tsdS t| jjdkS )NFr   )r)   r   lenr   returnsr   r   r   r   _no_returns@   s    
z_check_val.<locals>._no_returnsr1   call_function
Node.meta z is missing val field.z has invalid val field )	Z%torch.fx.experimental.symbolic_shapesr!   r"   r#   metar   targetr   name)r   r5   r1   r   r2   r   r   (   s    

r   c                 C   sd   | j d}|d u r$td| j t|ts`t|d tr`t|d tr`td| j d| d S )Ntorch_fnz*Unable to find torch_fn metadata for node r      r7   z has invalid torch_fn field )r8   getr   r:   r)   tupler,   )r   r;   r   r   r   _check_torch_fnO   s    
r?   c                   @   s.   e Zd ZU i Zeeed f ed< dd ZdS )_VerifierMetaVerifier	_registryc                 C   s   |r6d|v sd|v rt dd|v r0|d dks^J n(d|v sBJ d|v sNJ |d dks^J t|d tspJ t| |||}|| j|d < |S )Ncheck_check_graph_modulez'Overriding method check is not allowed.dialectATEN)SyntaxErrorr)   r,   type__new__rB   )metaclsr:   basesattrsretr   r   r   rI   b   s    z_VerifierMeta.__new__N)	r   r   r   rB   dictr,   rH   __annotations__rI   r   r   r   r   r@   _   s   
r@   )objr9   r   c                 C   sT   | d}| }t|D ]8\}}t||sDtdd|d |  t||}q|S )N.z#Node referenced nonexistent target )split	enumeratehasattrRuntimeErrorjoingetattr)rP   r9   Ztarget_atomsZattr_itriZatomr   r   r   getattr_recursiver   s    

rY   c                   @   s   e Zd ZdZedddZeee df dddZ	eee df ddd	Z
eee df dd
dZdd ZeddddZedddddZeejjddddZdS )rA   rF   r   c                 C   sf   t jt jt jt jt jt jt jt jt j	t j
t jt jt jt jt jt jt jt jt jt jt jtjtjtjtgS r   )operatorgetitemaddmulsubtruedivgelegtlteqnefloordivmodand_or_not_pownegabslshiftrshiftmathceilfloortruncroundselfr   r   r   allowed_builtin_ops   s4    zVerifier.allowed_builtin_ops.c                 C   s   t tfS r   )r   r   rv   r   r   r   allowed_op_types   s    zVerifier.allowed_op_typesc                 C   s   t jjt jjjfS r   )r.   fxr   utils_pytreeTreeSpecrv   r   r   r   allowed_getattr_types   s    zVerifier.allowed_getattr_typesc                 C   s   t jjt jjjt jt jjj	fS r   )
r.   rz   r   nnZ	parameter	Parameterr/   r{   r|   r}   rv   r   r   r   allowed_getattr_types_for_subgm   s
    z(Verifier.allowed_getattr_types_for_subgmc                 C   s   d S r   r   )rw   r   r   r   r   check_valid_op   s    zVerifier.check_valid_opN)gmr   c                 C   s   dS )zG
        Additional checks that are specific to some dialects.
        Nr   )rw   r   r   r   r   check_additional   s    zVerifier.check_additionalr   )epr   c                 C   s    |  |j t| t| d S r   )rD   Zgraph_module*_verify_exported_program_module_call_graph"_verify_exported_program_signature)rw   r   r   r   r   rC      s    zVerifier.checkc                    s  t tt df dfdd}d dfdd}| D ]}||u }t|tjjsVq8|j	  |jj
D ]}|jdv rtd|j d	qh|jd
krt| ||j qh|jdkrt|jtstdt|j t||j t tjjr fdd}t jdkr|dtrB|dtrB|dtrBt drBqhnTt dd }t dd }	t dd }
tdt  dt| dt|	 dt|
 n&t jdkrqhnt jdkrqht ||stdt  d|j d|| qh|jdkrht| qhq8| d S )N.rZ   c                    s0   | r   }n  }tdd |D r,J |S )Nc                 s   s   | ]}|t u V  qd S r   objectr$   tr   r   r   r'      r(   zOVerifier._check_graph_module.<locals>._allowed_getattr_types.<locals>.<genexpr>)r~   r   any)is_toplevel_gmrM   rv   r   r   _allowed_getattr_types   s
    
z<Verifier._check_graph_module.<locals>._allowed_getattr_typesc                    s   t d fdd}ttt df d fdd}tjjjtjtj	tj
tjtjtjtjtjjtjjjtjjjtjjjjf}t| | s| | vr| |vrtd|  d|  d	|  d
| t| tr jdkrt| std|  d |  d S )NrZ   c                     s"      } tdd | D sJ | S )Nc                 s   s   | ]}t |V  qd S r   )inspect	isbuiltin)r$   r   r   r   r   r'      r(   zfVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops.<locals>.<genexpr>)rx   r0   rM   rv   r   r   _allowed_builtin_ops   s    zSVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops.c                     s"      } tdd | D rJ | S )Nc                 s   s   | ]}|t u V  qd S r   r   r   r   r   r   r'      r(   zcVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_types.<locals>.<genexpr>)ry   r   r   rv   r   r   _allowed_op_types   s    zPVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_typesz
Operator 'z#' is not an allowed operator type: z
Valid builtin ops: zValid torch functions: TRAININGz
operator 'z' is not functional)listr>   rH   r   r.   ZautogradZ	grad_modeZset_grad_enabledZsym_intZ	sym_floatZsym_iteZsym_maxZsym_minZsym_notZsym_sqrtZ_CZ_set_grad_enabledampZautocast_modeZ_enter_autocastZ_exit_autocastrz   ZexperimentalZsymbolic_shapesZ cast_symbool_to_symint_guardlessr)   r   r   rE   r   r   )r   r   r   Z_allowed_torch_functionsrv   r   r   _check_valid_op   s>    

z5Verifier._check_graph_module.<locals>._check_valid_op>   Zcall_methodZcall_modulez'call_module is not valid: got a class 'z' r6   Zget_attrz/Expected get_attr target to be string, but got c                    s   t t | d |S r   )r)   rW   )r:   ty)attrr   r   _is_type  s    z.Verifier._check_graph_module.<locals>._is_typeZLoweredBackendModule
backend_idprocessed_bytescompile_specsZoriginal_modulezInvalid get_attr type z2. 
LoweredBackendModule fields: backend_id(str) : z, processed_bytes(bytes) : z, compile_specs(list) : ZAOTInductorEPModuleZAOTInductorRunnerWrapperz on target z. 
Valid get_attr types: placeholder)r>   rH   r   modulesr)   r.   rz   r   graphZlintnodesr   r   r9   r   r,   rY   r   Moduler   bytesr   rT   rW   r   )rw   r   r   r   rh   r   r   r   r   r   r   r   )r   rw   r   rD      st    0



zVerifier._check_graph_module)r   r   r   rE   r   rx   r>   rH   r   ry   r~   r   r   r   r   r   rC   r.   rz   rD   r   r   r   r   rA   ~   s   	rA   )	metaclassc                   @   s   e Zd ZdZdS )TrainingIRVerifierr   N)r   r   r   rE   r   r   r   r   r   <  s   r   rZ   c                 C   s   | j }dd | jjD }|D ]n}|jd ur|jjD ]&}|jr2|j|vr2td|j dq2|jjD ]&}|jrb|j|vrbtd|j dqbqd S )Nc                 S   s   h | ]
}|j qS r   )r:   r$   r   r   r   r   	<setcomp>B  r(   z=_verify_exported_program_module_call_graph.<locals>.<setcomp>zInput z does not exist in the graph.zOutput )module_call_graphr   r   	signatureinputsr:   r   outputs)exported_programr   r   entryargr   r   r   r   @  s    
r   c              	   C   s  | j }dd | jjD }t|t|jkrJtdt| dt|j dt|j|D ]\}}t|jt	t
ttfr|jj|krtd|jj d| |jtjkrqVqV|jtjkrBt|jt	std|j d	|j d
|jd u rtd|j d|j}|| jvrtd| dt| j| tjjsDtd| dqV|jtjkr
t|jt	sxtd|j d	|j d
|jd u rtd|j d|j}|jd u rtd| d|jdu r|| jvrtd| d|jdu rD|| jv rDtd| dqV|jtjkrt|jt	s@td|j d	|j d
|jd u r^td|j d|j}|| jvrDtd| dqV|jtjkrt|jtstd|j d|j d
|jd u rtd|j d|j}|| jvrDtd| dqV|jtjkr2t|jtsDtd|j d	|j d
qVtd|j dqVt| jjd }	|	j dksfJ dd |	j!d D }
t|
t|j"krtd t|
 d!t|j# d"t|j$ d#t|j%}t|j#t|j& | }|
|| }|
||t|j$  }|D ]}||j#v rF|j#| |j'vrtd$| d%|j# d&|j' d'n^||j&v r|j&| |j(vrtd(| d)|j& d*|j( d'ntd+| d,|j# d-|j& qt||j$D ],\}}||krtd.| d/|j$ d0qd S )1Nc                 S   s   g | ]}|j d kr|jqS )r   )r   r:   r   r   r   r   
<listcomp>V  s   z6_verify_exported_program_signature.<locals>.<listcomp>zNumber of graph inputs (z:) does not match number of inputs in the graph signature ()zInput spec name z does not match node name z
Parameter z! is not a tensor argument. Found z	 instead.zInputSpec for z has no target.z is not in the state dict.zState dict entry for parameter z* is not an instance of torch.nn.Parameter.zBuffer z is missing a persistence flagTFzNon-persistent buffer z( is in the state dict, it should not be.zConstant tensor z$ is not in the constants dictionary.zCustom object z( is not a custom object argument. Found zUnknown InputKind rQ   outputc                 S   s$   g | ]}t |tjjr|jn|qS r   )r)   r.   rz   Noder:   )r$   r   r   r   r   r     s   r   zNumber of output nodes zg is different Than the number of outputs specified by the graph signature: 
Number of mutated buffers: z. 
Number of user outputs: z. 
zBuffer output zV does not point to a buffer that exists. 
Dict of buffers that are mutated, in order: z 
Buffer nodes available: z 
zUser input output z^ does not point to a user input that exists. 
Dict of user inputs that are mutated, in order: z 
User input nodes available: zMutation node z: is neither a buffer nor a user input. Buffers to mutate: z, User inputs to mutate: zUser output zY is not in the correct order or is not found in the exported program's user_output list: z. ))Zgraph_signaturer   r   r3   Zinput_specsr   zipr)   r   r   r   r   r   r:   kindr
   Z
USER_INPUTZ	PARAMETERr9   Z
state_dictr.   r   r   ZBUFFER
persistentZCONSTANT_TENSOR	constantsZ
CUSTOM_OBJr	   ZTOKENr   r   r   argsZoutput_specsZbuffers_to_mutateZuser_outputsZoutput_tokensZuser_inputs_to_mutatebuffersZuser_inputs)r   ZgsZinput_node_namesZ
input_specr   parambufferZtensor_constZ
custom_objZoutput_nodeZoutput_nodesZ
num_tokensendZmutate_nodesZuser_output_nodesZmutation_nodeZuser_output_nodeZuser_output_namer   r   r   r   Q  s    








r   )rE   r   c                 C   s(   | dks| dkrt j| tS t j|  S )NrF    )r@   rB   r=   rA   )rE   r   r   r   load_verifier  s    r   )-r   rq   r[   collections.abcr   typingr   r   r   r.   Z
torch._opsr   r   Ztorch._subclasses.fake_tensorr   Ztorch.export.graph_signaturer	   r
   r   r   r   r   r   Ztorch.fxr   Ztorch.export.exported_programr   	Exceptionr   r+   r   rz   r   r    r   r?   rH   r@   r,   rY   rA   r   r   r   r   r   r   r   r   <module>   s2   $	' ? #