
    Ǆ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jB                  jD                  ddfdZ#dejB                  jD                  ddfdZ$dejB                  jD                  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.y)!    N)Iterable)AnyDictfinalListTupleTypeTYPE_CHECKING)HigherOrderOperator
OpOverload)
FakeTensor)CustomObjArgument	InputKindSymIntArgumentTensorArgumentTokenArgument)GraphModule)ExportedProgramc                       e Zd Zy)SpecViolationErrorN)__name__
__module____qualname__     ^/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/_export/verifier.pyr   r      s    r   r   opreturnc                 0    | j                   j                   S N)_schema
is_mutabler   s    r   is_functionalr$      s    zz$$$$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r;| j
                  dk(  r || j                        ry t        d| j                   d      | j                  d   } |      st        d| j                   d	|       y )
Nr   )SymBoolSymFloatSymIntc                    | yt        | t        t        t        t        f      ryt        | t
        j                  t
        j                  t
        j                  t
        j                  f      ryt        | t        t
        j                  f      ryt        | f      ryt        | t              ryt        | t              rt        fd| D              S y)NTc              3   .   K   | ]  } |        y wr    r   ).0x_check_correct_vals     r   	<genexpr>z9_check_val.<locals>._check_correct_val.<locals>.<genexpr>5   s     :)!,:s   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    ;c4e45e115;;ell[\j%,,78fh89./X&:c:::r   c                 h    t        | t              syt        | j                  j                        dk(  S )NFr   )r3   r   lenr!   returnsr#   s    r   _no_returnsz_check_val.<locals>._no_returns8   s)    "j)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",
 DII77o%+dkk*B :dii[8N!OPP
))E
Cc" :dii[8OPSu!UVV #r   c                    | j                   j                  d      }|t        d| j                         t	        |t
              sCt	        |d   t              r/t	        |d   t              rt        d| j                   d|       y y y )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    yy}}Z(H #Mdii[!YZZx'8A;$8A;$ :dii[8TU]T^!_`` 	% 	% (r   c                   2    e Zd ZU i Zeeed   f   ed<   d Zy)_VerifierMetaVerifier	_registryc                     |r!d|v sd|v rt        d      d|v r|d   dk7  sJ d|v sJ d|v sJ |d   dk(  sJ t        |d   t              sJ t        j	                  | |||      }|| 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!"KLL%%	*:f*DDDe###(E111#v---%	*C000ll7D%7.1%	*+
r   N)	r   r   r   rS   r   r6   r	   __annotations__r[   r   r   r   rQ   rQ   R   s     -/ItCj))*/r   rQ   objrH   c           	          |j                  d      }| }t        |      D ]=  \  }}t        ||      s t        ddj	                  |d |              t        ||      }? |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   sp    <<$LH\* +4x&!DSXXl[]\]N^E_D`a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y	)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    LLLLLLKKKKKKKKKKKKLLMMLLMMLLLLLLIIJJJJ-
 	
r   .c                     t         t        fS r    )r   r   r   s    r   allowed_op_typeszVerifier.allowed_op_types   s    /00r   c                 8    t         j                  j                  fS r    )r8   fxr   r   s    r   allowed_getattr_typeszVerifier.allowed_getattr_types   s    $$&&r   c                      y r    r   )r   r   s     r   check_valid_opzVerifier.check_valid_op   s    r   gmNc                      y)zG
        Additional checks that are specific to some dialects.
        Nr   )r   r   s     r   check_additionalzVerifier.check_additional   s    r   c                 f    | j                  |j                         t        |       t        |       y r    )rV   graph_module*_verify_exported_program_module_call_graph"_verify_exported_program_signature)r   eps     r   rU   zVerifier.check   s$      1226*2.r   c                 
   
 dt         t        t           df   f fd}d fd}|j                         D ]:  }t	        |t
        j                  j                        s)|j                  j                          |j                  j                  D ]  }|j                  dv rt        d|j                   d      |j                  dk(  rt        |        ||j                         X|j                  d	k(  r[t	        |j                  t              s!t        d
t!        |j                               t#        ||j                        
t	        
t
        j$                  j&                        r
fd}t!        
      j(                  dk(  r |dt              r( |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	        
 |             rt        dt!        
       d |              |j                  dk(  st        |        =  j3                  |       y )Nr   .c                  P    j                         } t        d | D              rJ | S )Nc              3   ,   K   | ]  }|t         u   y wr    objectr/   ts     r   r2   zOVerifier._check_graph_module.<locals>._allowed_getattr_types.<locals>.<genexpr>   s     411;4   )r   anyr_   r   s    r   _allowed_getattr_typesz<Verifier._check_graph_module.<locals>._allowed_getattr_types   s*    ,,.C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	k7  rt+        |       st%        d
|  d      j-                  |        y )Nr   c                  P    j                         } t        d | D              sJ | S )Nc              3   F   K   | ]  }t        j                  |        y w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?s   !)r   r>   r   s    r   _allowed_builtin_opszSVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops   s*    ..0?3????
r   .c                  P    j                         } t        d | D              rJ | S )Nc              3   ,   K   | ]  }|t         u   y wr    r   r   s     r   r2   zcVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_types.<locals>.<genexpr>   s     8qqF{8r   )r   r   r   s    r   _allowed_op_typeszPVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_types   s*    ++-8C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   s2   $ 
uT#Y^'<  ((99 **($ b"3"56133BZ8Z,$RD(KL]L_K` a..B.D-E12J1KM  "j) <<:-mB6G,$RD(;<  #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                 2    t        t        | d       |      S r    )r3   ri   )rI   tyattrs     r   _is_typez.Verifier._check_graph_module.<locals>._is_type   s    #-gdD$.G#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   sL   	d3in(= 	
*	$X ::< 5	%Cc588#7#78IINN		 0%77<<,A$++bQ  WW/t$#DKK0WW
*%dkk370MdSWS^S^N_M`a  -S$++>D!$8M:..2HH'c:(01BE(J(0$(G(/6G(H (-4T<-N
29$@QSW2X07ot0T&8&<T$ZL I99=j9I8J K@@D_@U?V W==A-=P<Q	%S'" !" &d,B,DE04T$ZL A55K5M4NP  WW-t$a0%5	%t 	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'uT#Y^'< '; 4 
 / /
 l"ehh&:&: l"t l" l"r   rR   )	metaclassc                       e Zd ZdZy)TrainingIRVerifierr   N)r   r   r   rW   r   r   r   r   r     s    Gr   r   c                    | j                   }| j                  j                  D ch c]  }|j                   }}|D ]  }|j                  |j                  j
                  D ]6  }|j                  s|j                  |vst        d|j                   d       |j                  j                  D ]6  }|j                  s|j                  |vst        d|j                   d        y c c}w )NzInput z does not exist in the graph.zOutput )module_call_graphr   r   rI   	signatureinputsr   outputs)exported_programr   r%   r   entryargs         r   r   r     s    (::.44::		E  # ??&-- 88 5, 
*GH 
 .. 88 5,!#((+HI s   C-c           
         | j                   }| j                  j                  D cg c]  }|j                  dk(  s|j                    }}t        |      t        |j                        k7  r.t        dt        |       dt        |j                         d      t        |j                  |      D ]B  \  }}t        |j                  t        t        f      r>|j                  j                  |k7  r%t        d|j                  j                   d|       |j                  t        j                  k(  r|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*                        rat        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 sT|| j$                  v sdt        d| d      |j                  t        j0                  k(  rt        |j                  t              s&t        d|j                   d|j                   d	      |j"                  t        d
|j                   d      |j"                  }|| j2                  vst        d| d      |j                  t        j4                  k(  rt        |j                  t6              s&t        d|j                   d|j                   d	      |j"                  t        d
|j                   d      |j"                  }|| j2                  vst        d| d      |j                  t        j8                  k(  rBt        |j                  t:              rt        d|j                   d|j                   d	      t        d|j                   d       t=        | j                  j                        d   }	|	j                  dk(  sJ |	j>                  d   D 
cg c]4  }
t        |
t&        j@                  jB                        r|
j                  n|
6 }}
t        |      t        |jD                        k7  rDt        dt        |       dt        |jF                         d t        |jH                         d!      t        |jJ                        }t        |jF                        t        |jL                        z   |z   }||| }|||t        |jH                        z    }|D ]  }||jF                  v rE|jF                  |   |jN                  vs-t        d"| d#|jF                   d$|jN                   d%      ||jL                  v rE|jL                  |   |jP                  vst        d&| d'|jL                   d(|jP                   d%      t        d)| d*|jF                   d+|jL                          t        ||jH                        D ]&  \  }}||k7  st        d,| d-|jH                   d.       y c c}w c c}
w )/Nr   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   outputr   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   r   rI   rA   input_specsr   zipr3   r   r   r   kindr   
USER_INPUT	PARAMETERrH   
state_dictr8   r   	ParameterBUFFER
persistentCONSTANT_TENSOR	constants
CUSTOM_OBJr   TOKENr   r   argsr   Nodeoutput_specsbuffers_to_mutateuser_outputsoutput_tokensuser_inputs_to_mutatebuffersuser_inputs)r   gsr%   input_node_names
input_specparambuffertensor_const
custom_objoutput_noder   output_nodes
num_tokensendmutate_nodesuser_output_nodesmutation_nodeuser_output_nodeuser_output_names                      r   r   r   &  s   		)	)B /?.D.D.J.JgddggYfNf		gg
BNN 33 &s+;'<&= >GGJ2>>GZF[[\^
 	

  0@A ^
Djnn~~&FG~~""d*(&z~~':':&;;UVZU[\  ??i222__	 3 33jnnn=(  11RS]SaSaRbbkl    (($Z__$5_E  %%E,777( 'AB  .99%@%((BTBTU(5eW<fg  __	 0 00jnnn=(joo..OPZP^P^O__hi    (($Z__$5_E   &&F$$,(fX%CD  $$,?O?Z?Z1Z(fX%?@  $$-&<L<W<W2W(,VH4\]  __	 9 99jnnn=(&z&77XYcYgYgXhhqr    (($Z__$5_E  &,,L#3#=#==(&|n4XY  __	 4 44jnn.?@($Z__$55]^h^l^l]mmvw    (($Z__$5_E  $**J!1!;!;;($ZL0TU  __	/jnnm<(&z&77XYcYgYgXhhqr  %$Z__$5Q7 y^B '--334R8K>>X%%% ##A& sEHHMM2;L 
 <C00 %c,&7%8 9**-b.B.B*C)D E''*2??';&<DB
 	
 R%%&J
b""
#c"*B*B&C
Cj
PC*:c:L$Ss2??/C)CD% B000##M2"**D($]O 4CCECWCWBX Y//1zzl#? 
 b666''6bnnL(( 8GGIG_G_F` a335>>2B#GH H
 %  0&&(&:&:%;;TUWUmUmTnp ( /22CR__.U **//$/0 188:7HL c hVs   ]]99]rW   c                     | dk(  s| dk(  r$t         j                  j                  | t              S t         j                  |    S )NrX    )rQ   rS   rM   rR   )rW   s    r   load_verifierr    s:    &GrM&&**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  6 4  !=	 	%j %T % 4 
 WUXX]]  Wt  WF	a%((-- 	aD 	aD $3   ^" ^"B &\~,3 ,4> ,r   