
    VhK                        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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,y)!    N)Iterable)AnyfinalTYPE_CHECKING)HigherOrderOperator
OpOverload)
FakeTensor)CustomObjArgument	InputKindSymIntArgumentSymFloatArgumentSymBoolArgumentTensorArgumentTokenArgument)GraphModule)ExportedProgramc                       e Zd Zy)SpecViolationErrorN)__name__
__module____qualname__     F/home/dcms/DCMS/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>7   s     :)!,:s   F)
isinstanceintboolstrfloattorchmemory_formatdtypedevicelayoutr	   Tensorr
   r   all)valr(   r)   r*   r/   s    r   r/   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   )r1   r   lenr   returnsr!   s    r   _no_returnsz_check_val.<locals>._no_returns:   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#   rA   r=   r(   r)   r*   r/   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      rC   z has invalid torch_fn field )rE   getr   rG   r1   tupler4   )r#   rI   s     r   _check_torch_fnrM   I   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)SyntaxErrorr1   r4   type__new__rQ   )metaclsrG   basesattrsrets        r   rY   z_VerifierMeta.__new__W   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   rQ   dictr4   rX   __annotations__rY   r   r   r   rO   rO   T   s     -/ItCj))*/r   rO   objrF   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)r`   rF   target_atomsattr_itriatoms         r   getattr_recursiverm   f   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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
)rP   rV   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.                  t,        j0                  t,        j2                  t4        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selfs    r   allowed_builtin_opszVerifier.allowed_builtin_opss   s    LLLLLLKKKKKKKKKKKKLLMMLLMMLLLLLLOOOOIIJJJJ3
 	
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   )r6   fxr   r   s    r   allowed_getattr_typeszVerifier.allowed_getattr_types   s    $$&&r   c                 ~    t         j                  j                  t         j                  j                  j
                  fS r   )r6   r   r   nn	parameter	Parameterr   s    r   allowed_getattr_types_for_subgmz(Verifier.allowed_getattr_types_for_subgm   s'    $$ehh&8&8&B&BCCr   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   )rT   graph_module*_verify_exported_program_module_call_graph"_verify_exported_program_signature)r   eps     r   rS   zVerifier.check   s$      1226*2.r   c                 J    dt         t        t           df   f fd}d fd}|j                         D ]Z  }||u }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(  rwt	        |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/t/        dd       }t/        dd       }	t/        dd       }
t        dt               dt        |       dt        |	       dt        |
             t              j&                  dk(  rt	         ||            rt        dt               d ||             |j                  dk(  st        |        ]  j1                  |       y )Nr   .c                 v    | rj                         }nj                         }t        d |D              rJ |S )Nc              3   ,   K   | ]  }|t         u   y wr   objectr-   ts     r   r0   zOVerifier._check_graph_module.<locals>._allowed_getattr_types.<locals>.<genexpr>   s     411;4   )r   r   any)is_toplevel_gmr]   r   s     r   _allowed_getattr_typesz<Verifier._check_graph_module.<locals>._allowed_getattr_types   s;    002::<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                   t        j"                  j$                  j&                  t        j"                  j$                  j(                  t        j*                  j,                  j.                  j0                  f}t3        |  |             s.|  |       vr%| |vr!t5        d|  d |        d |        d|       t3        | t6              r)j8                  d	k7  rt;        |       st5        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   r0   zfVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops.<locals>.<genexpr>   s     ?R7,,R0?s   !)r   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   r0   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)listrL   rX   r   r6   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_guardlessr1   r   r   rU   r"   r   )r   r   r   _allowed_torch_functionsr   s       r   _check_valid_opz5Verifier._check_graph_module.<locals>._check_valid_op   sq   $ 
uT#Y^'<  ((99 **		''77		''66%%55VV($$ 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' rB   get_attrz/Expected get_attr target to be string, but got c                 2    t        t        | d       |      S r   )r1   rh   )rG   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) : AOTInductorEPModulez. 
Valid get_attr types: placeholderr   N)rL   rX   r   modulesr1   r6   r   r   graphlintnodesr   r   rF   r%   r4   rm   r   Moduler   bytesr   re   rh   r   )r   r   r   r   r}   r   r#   r   r   r   r   r   s   `          @r   rT   zVerifier._check_graph_module   sq   	eDIsN6K 	-	$^ ::< :	%C BYNc588#7#78IINN		 3%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'" !" "$Z004II$ &d,B>,RS04T$ZL A55KN5[4\^  WW-t$g3%:	%~ 	b!r   )r   r   r   N)r   r   r   rU   r   r   rL   rX   r   r   r   r   r   r   r   r   rS   r6   r   rT   r   r   r   rP   rP   p   s    G
T 
:1%S	3"7 1'uT#Y^'< 'DtCy#~1F D; 4 
 / /
 w"ehh&:&: w"t w" w"r   rP   )	metaclassc                       e Zd ZdZy)TrainingIRVerifierr   N)r   r   r   rU   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   rG   	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 ]L  \  }}t        |j                  t        t        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kt        d| d      |j                  t         j0                  k(  rt        |j                  t              s&t        d|j                   d|j                   d	      |j&                  t        d
|j                   d      |j&                  }|j2                  t        d| d      |j2                  du r|| j(                  vrt        d| d      |j2                  du s^|| j(                  v snt        d| d      |j                  t         j4                  k(  rt        |j                  t              s&t        d|j                   d|j                   d	      |j&                  t        d
|j                   d      |j&                  }|| j6                  vst        d| d      |j                  t         j8                  k(  rt        |j                  t:              s&t        d|j                   d|j                   d	      |j&                  t        d
|j                   d      |j&                  }|| j6                  vst        d| d      |j                  t         j<                  k(  rBt        |j                  t>              rt        d|j                   d|j                   d	      t        d|j                   d       tA        | j                  j                        d   }	|	j                  dk(  sJ |	jB                  d   D 
cg c]4  }
t        |
t*        jD                  jF                        r|
j                  n|
6 }}
t        |      t        |jH                        k7  rDt        dt        |       dt        |jJ                         d t        |jL                         d!      t        |jN                        }t        |jJ                        t        |jP                        z   |z   }||| }|||t        |jL                        z    }|D ]  }||jJ                  v rE|jJ                  |   |jR                  vs-t        d"| d#|jJ                   d$|jR                   d%      ||jP                  v rE|jP                  |   |jT                  vst        d&| d'|jP                   d(|jT                   d%      t        d)| d*|jJ                   d+|jP                          t        ||jL                        D ]&  \  }}||k7  st        d,| d-|jL                   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 rb   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   rG   r?   input_specsr   zipr1   r   r   r   r   r   kindr   
USER_INPUT	PARAMETERrF   
state_dictr6   r   r   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~~GWYh&ij~~""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   ]]9]rU   c                     | dk(  s| dk(  r$t         j                  j                  | t              S t         j                  |    S )NrV    )rO   rQ   rK   rP   )rU   s    r   load_verifierr    s:    &GrM&&**7H==""7++r   r   )-r   r   rp   collections.abcr   typingr   r   r   r6   
torch._opsr   r   torch._subclasses.fake_tensorr	   torch.export.graph_signaturer
   r   r   r   r   r   r   torch.fxr   torch.export.exported_programr   	Exceptionr   r3   r"   r   r   r&   r%   rM   rX   rO   r4   rm   rP   r   r   r   r  r   r   r   <module>r#     s      $ , ,  6 4   !=	 	%j %T % 4 
 WUXX]]  Wt  WF	a%((-- 	aD 	aD $3   p" p"f &\~,3 ,4> ,r   