o
    IhK                     @   sv  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rNd dlmZ G d	d
 d
eZdedefddZde	jj ddfddZ!de	jj ddfddZ"de	jj ddfddZ#G dd de$Z%dede&defddZ'G dd de%dZ(G dd  d e(Z)d(d!d"Z*d(d#d$Z+d%e&de$e( fd&d'Z,dS ))    N)Iterable)AnyfinalTYPE_CHECKING)HigherOrderOperator
OpOverload)
FakeTensor)CustomObjArgument	InputKindSymIntArgumentSymFloatArgumentSymBoolArgumentTensorArgumentTokenArgument)GraphModule)ExportedProgramc                   @   s   e Zd ZdS )SpecViolationErrorN)__name__
__module____qualname__ r   r   J/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_export/verifier.pyr      s    r   opreturnc                 C   s
   | j j S N)_schema
is_mutabler   r   r   r   is_functional      
r   nodec                 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r1| jdkr(|| jr(d S td	| j d
| jd }|sEt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r*dS t |  fr4dS t | tr;dS t | trKtfdd| D S dS )NTc                 3   s    | ]} |V  qd S r   r   ).0x)_check_correct_valr   r   	<genexpr>7       z9_check_val.<locals>._check_correct_val.<locals>.<genexpr>F)
isinstanceintboolstrfloattorchmemory_formatdtypedevice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_returnsr7   call_function
Node.meta z is missing val field.z has invalid val field )	%torch.fx.experimental.symbolic_shapesr#   r$   r%   metar   targetr   name)r    r;   r7   r   r8   r   r!   &   s   

r!   c                 C   sl   | j d}|d u rtd| j t|ts0t|d tr2t|d tr4td| j d| d S d S d S )Ntorch_fnz*Unable to find torch_fn metadata for node r      r=   z has invalid torch_fn field )r?   getr   rA   r+   tupler.   )r    rB   r   r   r   _check_torch_fnI   s   rF   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   |rd|v s
d|v rt dd|v r|d dksJ nd|v s!J d|v s'J |d dks/J t|d ts8J t| |||}|| j|d < |S )Ncheck_check_graph_modulez'Overriding method check is not allowed.dialectATEN)SyntaxErrorr+   r.   type__new__rI   )metaclsrA   basesattrsretr   r   r   rP   W   s   z_VerifierMeta.__new__N)	r   r   r   rI   dictr.   rO   __annotations__rP   r   r   r   r   rG   T   s   
 rG   objr@   c                 C   sT   | d}| }t|D ]\}}t||s"tdd|d |  t||}q|S )N.z#Node referenced nonexistent target )split	enumeratehasattrRuntimeErrorjoingetattr)rW   r@   target_atomsattr_itriatomr   r   r   getattr_recursivef   s   

rc   c                   @   s   e Zd ZdZdefddZdeee df fddZ	deee df fdd	Z
deee df fd
dZdd ZdeddfddZedddZedejjddfddZdS )rH   rM   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_opss   s4   zVerifier.allowed_builtin_ops.c                 C   s   t tfS r   )r   r   r   r   r   r   allowed_op_types   s   zVerifier.allowed_op_typesc                 C   s
   t jjfS r   )r0   fxr   r   r   r   r   allowed_getattr_types   r   zVerifier.allowed_getattr_typesc                 C   s   t jjt jjjfS r   )r0   r   r   nn	parameter	Parameterr   r   r   r   allowed_getattr_types_for_subgm   s   z(Verifier.allowed_getattr_types_for_subgmc                 C   s   d S r   r   )r   r   r   r   r   check_valid_op   s   zVerifier.check_valid_opgmNc                 C   s   dS )zG
        Additional checks that are specific to some dialects.
        Nr   )r   r   r   r   r   check_additional   s    zVerifier.check_additionalepr   c                 C   s    |  |j t| t| d S r   )rK   graph_module*_verify_exported_program_module_call_graph"_verify_exported_program_signature)r   r   r   r   r   rJ      s   zVerifier.checkc                    s  dt tt df ffdd}dfdd}| D ]}||u }t|tjjs(q|j	  |jj
D ]}|jdv rAtd|j d	|jd
krPt| ||j q1|jdkrt|jtsetdt|j t||j t tjjrƇ fdd}t jdkr|dtr|dtr|dtrt drq1t dd }t dd }	t dd }
tdt  dt| dt|	 dt|
 t jdkrq1t ||stdt  d|| q1|jdkrt| q1q| d S )Nr   .c                    s0   | r   }n  }tdd |D rJ |S )Nc                 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_gmrT   r   r   r   _allowed_getattr_types   s
   
z<Verifier._check_graph_module.<locals>._allowed_getattr_typesc                    s   dt f fdd}dttt df f 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r]td|  d|  d	|  d
| t| trs jdkrst| sstd|  d |  d S )Nr   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)      s    zfVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops.<locals>.<genexpr>)r   r6   rT   r   r   r   _allowed_builtin_ops      zSVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops.c                     s"      } tdd | D rJ | S )Nc                 s   r   r   r   r   r   r   r   r)      r*   zcVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_types.<locals>.<genexpr>)r   r   r   r   r   r   _allowed_op_types   r   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)listrE   rO   r   r0   autograd	grad_modeset_grad_enabledsym_int	sym_floatsym_itesym_maxsym_minsym_notsym_sqrt_C_set_grad_enabledampautocast_mode_enter_autocast_exit_autocastr   experimentalsymbolic_shapes cast_symbool_to_symint_guardlessr+   r   r   rL   r   r   )r   r   r   _allowed_torch_functionsr   r   r   _check_valid_op   s<   


z5Verifier._check_graph_module.<locals>._check_valid_op>   call_methodcall_modulez'call_module is not valid: got a class 'z' r<   get_attrz/Expected get_attr target to be string, but got c                    s   t t | d |S r   )r+   r^   )rA   ty)attrr   r   _is_type   s   z.Verifier._check_graph_module.<locals>._is_typeLoweredBackendModule
backend_idprocessed_bytescompile_specsoriginal_modulezInvalid get_attr type z2. 
LoweredBackendModule fields: backend_id(str) : z, processed_bytes(bytes) : z, compile_specs(list) : AOTInductorEPModulez. 
Valid get_attr types: placeholderr   N)rE   rO   r   modulesr+   r0   r   r   graphlintnodesr   r   r@   r!   r.   rc   r   Moduler   bytesr   r[   r^   r   )r   r   r   r   rq   r   r    r   r   r   r   r   )r   r   r   rK      st   /





8zVerifier._check_graph_module)r   r   r   N)r   r   r   rL   r   r   rE   rO   r   r   r   r   r   r   r   r   rJ   r0   r   rK   r   r   r   r   rH   p   s    rH   )	metaclassc                   @   s   e Zd ZdZdS )TrainingIRVerifierr   N)r   r   r   rL   r   r   r   r   r   #  s    r   c                 C   s   | j }dd | jjD }|D ]7}|jd urE|jjD ]}|jr,|j|vr,td|j dq|jjD ]}|jrD|j|vrDtd|j dq1qd S )Nc                 S   s   h | ]}|j qS r   )rA   r&   r    r   r   r   	<setcomp>)  s    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inputsrA   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r%tdt| dt|j dt|j|D ]i\}}t|jt	t
ttfrL|jj|krLtd|jj d| |jtjkrSq+|jtjkrt|jt	sltd|j d	|j d
|jd u rztd|j d|j}|| jvrtd| dt| j| tjjstd| dq+|jtjkrt|jt	st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 r|| jv rtd| dq+|jtjkr5t|jt	std|j d	|j d
|jd u r#td|j d|j}|| jvr4td| dq+|jtjkrqt|jtsPtd|j d|j d
|jd u r_td|j d|j}|| jvrptd| dq+|jtjkrt|jtstd|j d	|j d
q+td|j dt| jjd }	|	j dksJ 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 ]R}||j#v r|j#| |j'vrtd$| d%|j# d&|j' d'q||j&v r8|j&| |j(vr6td(| d)|j& d*|j( d'qtd+| d,|j# d-|j& t||j$D ]\}}||krbtd.| d/|j$ d0qMd S )1Nc                 S   s   g | ]
}|j d kr|jqS )r   )r   rA   r   r   r   r   
<listcomp>?  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 rX   outputc                 S   s$   g | ]}t |tjjr|jn|qS r   )r+   r0   r   NoderA   )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. ))graph_signaturer   r   r9   input_specsr   zipr+   r   r   r   r   r   rA   kindr
   
USER_INPUT	PARAMETERr@   
state_dictr0   r   r   BUFFER
persistentCONSTANT_TENSOR	constants
CUSTOM_OBJr	   TOKENr   r   r   argsoutput_specsbuffers_to_mutateuser_outputsoutput_tokensuser_inputs_to_mutatebuffersuser_inputs)r   gsinput_node_names
input_specr    parambuffertensor_const
custom_objoutput_nodeoutput_nodes
num_tokensendmutate_nodesuser_output_nodesmutation_nodeuser_output_nodeuser_output_namer   r   r   r   :  s.  












r   rL   c                 C   s(   | dks| dkrt j| tS t j|  S )NrM    )rG   rI   rD   rH   )rL   r   r   r   load_verifier  s   
r   r   )-r   rz   rd   collections.abcr   typingr   r   r   r0   
torch._opsr   r   torch._subclasses.fake_tensorr   torch.export.graph_signaturer	   r
   r   r   r   r   r   torch.fxr   torch.export.exported_programr   	Exceptionr   r-   r   r   r   r"   r!   rF   rO   rG   r.   rc   rH   r   r   r   r   r   r   r   r   <module>   s4   $	#
 4

  