
    קgG                        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m	Z	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 d dlmZ erd dlmZ  G d	 d
e          ZdedefdZ dej!        j"        ddfdZ#dej!        j"        ddfdZ$dej!        j"        ddfdZ% G d de&          Z'dede(defdZ) G d de'          Z* G d de*          Z+d dZ,d dZ-de(dee*         fdZ.dS )!    N)Iterable)AnyDictfinalListTupleTypeTYPE_CHECKING)HigherOrderOperator
OpOverload)
FakeTensor)CustomObjArgument	InputKindSymIntArgumentTensorArgumentTokenArgument)GraphModule)ExportedProgramc                       e Zd ZdS )SpecViolationErrorN)__name__
__module____qualname__     R/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_export/verifier.pyr   r      s        Dr   r   opreturnc                     | j         j         S N)_schema
is_mutabler   s    r   is_functionalr$      s    z$$$r   nodec                      t          |           S r    )
_check_val)r%   s    r   _check_has_fake_tensorr(      s    dr   c                    ddl mmm fdd }d| j        vr5| j        dk    r || j                  rd S t          d| j         d          | j        d         } |          st          d| j         d	|           d S )
Nr   )SymBoolSymFloatSymIntc                    | 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          rdS t          | t                    rdS t          | t                    rt          fd| D                       S dS )NTc              3   .   K   | ]} |          V  d S r    r   ).0x_check_correct_vals     r   	<genexpr>z9_check_val.<locals>._check_correct_val.<locals>.<genexpr>5   s/      ::))!,,::::::r   F)
isinstanceintboolstrfloattorchmemory_formatdtypedevicelayoutr   Tensorr   r   all)valr*   r+   r,   r1   s    r   r1   z&_check_val.<locals>._check_correct_val'   s    ;4c4e455 	;4e15;el[\\ 		;4j%,788 	;4fh899 	;4.// 	;4X&& 	;::::c::::::ur   c                 j    t          | t                    sdS t          | j        j                  dk    S )NFr   )r3   r   lenr!   returnsr#   s    r   _no_returnsz_check_val.<locals>._no_returns8   s1    "j)) 	52:%&&!++r   r?   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%   rC   r?   r*   r+   r,   r1   s      @@@@r   r'   r'   $   s    OOOOOOOOOO       ", , ,
 DI7o%%++dk*B*B%F !Odi!O!O!OPPP
)E
Cc"" W !Udi!U!UPS!U!UVVVW Wr   c                 >   | j                             d          }|t          d| j                   t	          |t
                    sPt	          |d         t                    r7t	          |d         t                    rt          d| j         d|           d S d S d S )Ntorch_fnz*Unable to find torch_fn metadata for node r      rE   z has invalid torch_fn field )rG   getr   rI   r3   tupler6   )r%   rK   s     r   _check_torch_fnrO   G   s    y}}Z((H !Ydi!Y!YZZZx''a8A;$$a 	8A;$$a
 !!_di!_!_U]!_!_```a a a a a ar   c                   @    e Zd ZU i Zeeed         f         ed<   d ZdS )_VerifierMetaVerifier	_registryc                 &   |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                    sJ t                              | |||          }|| j        |d         <   |S )Ncheck_check_graph_modulez'Overriding method check is not allowed.dialectATEN)SyntaxErrorr3   r6   type__new__rS   )metaclsrI   basesattrsrets        r   r[   z_VerifierMeta.__new__U   s     	.%#8E#A#A!"KLLL%%%	*:f*D*D*D*D*De####(E1111#v----%	*C00000ll7D%77.1%	*+
r   N)	r   r   r   rS   r   r6   r	   __annotations__r[   r   r   r   rQ   rQ   R   sD         -/ItCj))*///    r   rQ   objrH   c           	          |                     d          }| }t          |          D ]R\  }}t          ||          s-t          dd                    |d |                              t          ||          }S|S )N.z#Node referenced nonexistent target )split	enumeratehasattrRuntimeErrorjoingetattr)ra   rH   target_atomsattr_itriatoms         r   getattr_recursivern   d   s    <<$$LH\** + +4x&& 	caSXXl[]\][]N^E_E_aabbb8T**Or   c                       e Zd ZdZdefdZdeee         df         fdZ	deee         df         fdZ
d Zdedd	fd
Zedd            Zedej        j        dd	fd            Zd	S )rR   rX   r   c                    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        gS r    )operatorgetitemaddmulsubtruedivgelegtlteqnefloordivmodand_or_not_pownegabsmathceilfloortruncselfs    r   allowed_builtin_opszVerifier.allowed_builtin_opsq   s}    LLLKKKKKKLMLMLLLIJJ-
 	
r   .c                     t           t          fS r    )r   r   r   s    r   allowed_op_typeszVerifier.allowed_op_types   s    /00r   c                 &    t           j        j        fS r    )r8   fxr   r   s    r   allowed_getattr_typeszVerifier.allowed_getattr_types   s    $&&r   c                     d S r    r   )r   r   s     r   check_valid_opzVerifier.check_valid_op   s    r   gmNc                     dS )zG
        Additional checks that are specific to some dialects.
        Nr   )r   r   s     r   check_additionalzVerifier.check_additional   s      r   epr   c                 v    |                      |j                   t          |           t          |           d S r    )rV   graph_module*_verify_exported_program_module_call_graph"_verify_exported_program_signature)r   r   s     r   rU   zVerifier.check   s:      11122666*2.....r   c                 h   
 dt           t          t                   df         f fd}d fd}|                                D ]Y}t	          |t
          j        j                  s#|j        	                                 |j        j
        D ]}|j        dv rt          d|j         d          |j        dk    r t          |            ||j                   O|j        d	k    rt	          |j        t                    s$t          d
t!          |j                             t#          ||j                  
t	          
t
          j        j                  r
fd}t!          
          j        dk    r |dt                    r4 |dt*                    r# |dt,                    rt/          
d          r/t1          
dd           }t1          
dd           }t1          
dd           }	t          dt!          
           dt!          |           dt!          |           dt!          |	                     t	          
 |                      s*t          dt!          
           d |                       |j        dk    rt          |           [                     |           d S )Nr   .c                  f                                     } t          d | D                       rJ | S )Nc              3   (   K   | ]}|t           u V  d S r    objectr/   ts     r   r2   zOVerifier._check_graph_module.<locals>._allowed_getattr_types.<locals>.<genexpr>   s&      4411;444444r   )r   anyr_   r   s    r   _allowed_getattr_typesz<Verifier._check_graph_module.<locals>._allowed_getattr_types   s<    ,,..C4444444444Jr   c           
         dt           ffd}dt          t          t                   df         ffd}t          j        j        j        t          j        t          j	        t          j
        t          j        t          j        t          j        t          j        t          j        j        f	}t#          |  |                      s;|  |            vr/| |vr+t%          d|  d |             d |             d|           t#          | t&                    r-j        d	k    r"t+          |           st%          d
|  d                              |            d S )Nr   c                  f                                     } t          d | D                       sJ | S )Nc              3   >   K   | ]}t          j        |          V  d S r    )inspect	isbuiltin)r/   r   s     r   r2   zfVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops.<locals>.<genexpr>   s-      ??R7,R00??????r   )r   r>   r   s    r   _allowed_builtin_opszSVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops   s<    ..00??3????????
r   .c                  f                                     } t          d | D                       rJ | S )Nc              3   (   K   | ]}|t           u V  d S r    r   r   s     r   r2   zcVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_types.<locals>.<genexpr>   s&      88qqF{888888r   )r   r   r   s    r   _allowed_op_typeszPVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_types   s<    ++--88C88888888
r   z
Operator 'z#' is not an allowed operator type: z
Valid builtin ops: zValid torch functions: TRAININGz
operator 'z' is not functional)r   r   r	   r   r8   autograd	grad_modeset_grad_enabledsym_int	sym_floatsym_itesym_maxsym_minsym_notsym_sqrt_C_set_grad_enabledr3   r   r   rW   r$   r   )r   r   r   _allowed_torch_functionsr   s       r   _check_valid_opz5Verifier._check_graph_module.<locals>._check_valid_op   s   $      
uT#Y^'<       (9 *($ b"3"3"5"566 113333BZ8Z8Z,MR M ML]L]L_L_ M M.B.B.D.DM M2JM M   "j))  <:--mB6G6G-,<R<<<   #####r   >   call_methodcall_modulez'call_module is not valid: got a class 'z' rD   get_attrz/Expected get_attr target to be string, but got c                 B    t          t          | d           |          S r    )r3   ri   )rI   tyattrs     r   _is_typez.Verifier._check_graph_module.<locals>._is_type   s    #-gdD$.G.G#L#LLr   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) : z. 
Valid get_attr types: placeholderr   N)r   r	   r   modulesr3   r8   r   r   graphlintnodesr   r   rH   r'   r6   rZ   rn   nnModuler   byteslistrf   ri   r   )r   r   r   r   r~   r%   r   r   r   r   r   s   `         @r   rV   zVerifier._check_graph_module   sa   	d3in(= 	 	 	 	 	 	
*	$ *	$ *	$ *	$ *	$ *	$X ::<< 5	% 5	%Cc58#788 INN	 0% 0%7<<<,Q$+QQQ   W//t$$$#ODK0000W
**%dk377 0adSWS^N_N_aa   -S$+>>D!$88 "M M M M M::.2HHH'xc:: "(01BE(J(J"(0$(G(G" )06G(H(H" !)-4T<-N-N
29$@QSW2X2X07ot0T0T&8%ST$ZZ %S %S9=j9I9I%S %S AE_@U@U%S %S >B-=P=P	%S %S'" '" !" &d,B,B,D,DEE 0PT$ZZ P P5K5K5M5MP P   W--t$$$a0%j 	b!!!!!r   )r   r   r   N)r   r   r   rW   r   r   r   r	   r   r   r   r   r   r   r   rU   r8   r   rV   r   r   r   rR   rR   n   s       G
T 
 
 
 
41%S	3"7 1 1 1 1'uT#Y^'< ' ' ' '  ; 4    
 / / / U/
 l"eh&: l"t l" l" l" Ul" l" l"r   rR   )	metaclassc                       e Zd ZdZdS )TrainingIRVerifierr   N)r   r   r   rW   r   r   r   r   r     s        GGGr   r   c                 4   | j         }d | j        j        D             }|D ]w}|j        n|j        j        D ]*}|j        r!|j        |vrt          d|j         d          +|j        j        D ]*}|j        r!|j        |vrt          d|j         d          +xd S )Nc                     h | ]	}|j         
S r   )rI   r/   r%   s     r   	<setcomp>z=_verify_exported_program_module_call_graph.<locals>.<setcomp>  s'       	  r   zInput z does not exist in the graph.zOutput )module_call_graphr   r   	signatureinputsrI   r   outputs)exported_programr   r   entryargs        r   r   r     s    (: .4:  E #  ?&-  8  5 5,HHHH   .  8  5 5,I#(III   r   c           
      h   | j         }d | j        j        D             }t          |          t          |j                  k    r5t          dt          |           dt          |j                   d          t          |j        |          D ]\  }}t          |j        t          t          f          r/|j        j        |k    rt          d|j        j         d|           |j        t          j        k    rl|j        t          j        k    rt          |j        t                    s t          d|j         d|j         d	          |j        t          d
|j         d          |j        }|| j        vrt          d| d          t          | j        |         t$          j        j                  st          d| d          <|j        t          j        k    rt          |j        t                    s t          d|j         d|j         d	          |j        t          d
|j         d          |j        }|j        t          d| d          |j        du r|| j        vrt          d| d          |j        du r|| j        v rt          d| d          |j        t          j        k    r~t          |j        t                    s t          d|j         d|j         d	          |j        t          d
|j         d          |j        }|| j        vrt          d| d          |j        t          j        k    r~t          |j        t4                    s t          d|j         d|j         d	          |j        t          d
|j         d          |j        }|| j        vrt          d| d          =|j        t          j        k    r<t          |j        t8                    s t          d|j         d|j         d	          t          d|j         d          t;          | j        j                  d         }	|	j        dk    sJ d |	j        d         D             }
t          |
          t          |j                   k    rJt          dt          |
           d t          |j!                   d!t          |j"                   d"          t          |j#                  }t          |j!                  t          |j$                  z   |z   }|
||         }|
||t          |j"                  z            }|D ]}||j!        v r8|j!        |         |j%        vr#t          d#| d$|j!         d%|j%         d&          C||j$        v r8|j$        |         |j&        vr#t          d'| d(|j$         d)|j&         d&          t          d*| d+|j!         d,|j$                   t          ||j"                  D ]&\  }}||k    rt          d-| d.|j"         d/          'd S )0Nc                 2    g | ]}|j         d k    |j        S )r   )r   rI   r   s     r   
<listcomp>z6_verify_exported_program_signature.<locals>.<listcomp>+  s'    gggddgYfNfNf	NfNfNfr   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 rc   outputc                 ^    g | ]*}t          |t          j        j                  r|j        n|+S r   )r3   r8   r   NoderI   )r/   r   s     r   r   z6_verify_exported_program_signature.<locals>.<listcomp>  sA        sEHM22;  r   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   rA   input_specsr   zipr3   r   r   r   rI   kindr   
USER_INPUT	PARAMETERrH   
state_dictr8   r   	Parameter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_names                     r   r   r   &  sS   		)B hg.>.D.Jggg
BN 3 333 ^s+;'<'< ^ ^GJ2>GZGZ^ ^ ^
 
 	

  0@AA ^ ^
Djn~~&FGG 	~"d**(\z~':\\VZ\\   ?i222_	 333jnn== (lllS]Salll    ((EZ_EEE   %E,777(BBBB   .9%@%(BTUU (geggg  
 _	 000jnn== (ijoiiPZP^iii    ((EZ_EEE    &F$,(DfDDD   $,,?O?Z1Z1Z(@f@@@   $--&<L<W2W2W(]V]]]   _	 999jnn== (rzrrYcYgrrr    ((EZ_EEE   &,L#3#===(Y|YYY   > _	 444jn.?@@ (wZ_ww^h^lwww    ((EZ_EEE   $*J!1!;;;(UZUUU   < _	//jnm<< (rzrrYcYgrrr  
 %7Z_777  
 '-344R8K>X%%%% #A&  L
 <C0000 Bc,&7&7 B B*-b.B*C*CB B (+2?';';B B B
 
 	
 R%&&J
b"
#
#c"*B&C&C
Cj
PC*:c>:L$Ss2?/C/C)C%CD%  B000#M2"*DD(?] ? ?CECW? ?/1z? ? ?   E b666'6bnLL(G G GGIG_G G35>G G GH H H M %p p p&(&:p pUWUmp p  
 /22CR_.U.U  **///$L/ L L8:L L L   0 r   rW   c                     | dk    s| dk    r%t           j                            | t                    S t           j        |          S )NrX    )rQ   rS   rM   rR   )rW   s    r   load_verifierr    s<    &GrMM&**7H==="7++r   r   )/r   r   rq   collections.abcr   typingr   r   r   r   r   r	   r
   r8   
torch._opsr   r   torch._subclasses.fake_tensorr   torch.export.graph_signaturer   r   r   r   r   torch.fxr   torch.export.exported_programr   	Exceptionr   r5   r$   r   r   r(   r'   rO   rZ   rQ   r6   rn   rR   r   r   r   r  r   r   r   <module>r     s      $ $ $ $ $ $ E E E E E E E E E E E E E E E E E E  6 6 6 6 6 6 6 6 4 4 4 4 4 4              !           >======	 	 	 	 	 	 	 	%j %T % % % % 4    
 WUX]  Wt  W  W  W  WF	a%(- 	aD 	a 	a 	a 	a    D   $3      ^" ^" ^" ^" ^" ^" ^" ^" ^"B          &\ \ \ \~,3 ,4> , , , , , ,r   