
    Ph:                        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 d dlmZmZmZmZ d dlmZ d d	lmZmZmZ  G d
 de       Z!dede"fdZ#dejH                  jJ                  ddfdZ&dejH                  jJ                  ddfdZ' G d de(      Z) G d de)      Z*ddZ+de,de
ee*      fdZ-y)    N)Iterable)AnyDictfinalListOptionalTupleType)HigherOrderOperator
OpOverload)
FakeTensor)ExportedProgram)ExportGraphSignature	InputKindSymIntArgumentTensorArgument)GraphModule)SymBoolSymFloatSymIntc                       e Zd Zy)SpecViolationErrorN)__name__
__module____qualname__     aC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\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                    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 )Nc                    | 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        | t        t        t        f      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>/   s     :c)!,cs   F)
isinstanceintboolstrfloattorchmemory_formatdtypedevicelayoutr   Tensorr   r   r   r   all)valr0   s    r   r0   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   )r2   r   lenr#   returnsr%   s    r   _no_returnsz_check_val.<locals>._no_returns2   s)    "j)2::%%&!++r   r>   call_functionz
Node.meta z is missing val field.z has invalid val field )metar   targetr   name)r'   rB   r>   r0   s      @r   r)   r)   "   s    ,
 DII77o%+dkk*B :dii[8N!OPP
))E
Cc" :dii[8OPSu!UVV #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)SyntaxErrorr2   r5   type__new__rJ   )metaclsrF   basesattrsrets        r   rR   z_VerifierMeta.__new__D   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   rJ   r   r5   r
   __annotations__rR   r   r   r   rH   rH   A   s     -/ItCj))*/r   rH   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edd	fd       Zedej$                  j                  dd	fd       Zy	)rI   rO   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,                  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selfs    r   allowed_builtin_opszVerifier.allowed_builtin_opsW   s    LLLLLLKKKKKKKKKKKKLLMMLLMMLLLLLLIIJJ+
 	
r   .c                     t         t        fS r"   )r   r   rq   s    r   allowed_op_typeszVerifier.allowed_op_typesp   s    /00r   c                 8    t         j                  j                  fS r"   )r7   fxr   rq   s    r   allowed_getattr_typeszVerifier.allowed_getattr_typess   s    $$&&r   c                      y r"   r   )rr   r   s     r   check_valid_opzVerifier.check_valid_opv   s    r   gmNc                      y)zG
        Additional checks that are specific to some dialects.
        Nr   )rr   r{   s     r   check_additionalzVerifier.check_additionaly   s     	r   epc                     t        |j                  t              sy | j                  |j                         	 t        |       y # t        $ r}|j                  dk(  rn|Y d }~y d }~ww xY w)NEDGE)r2   graph_signaturer   rM   graph_module"_verify_exported_program_signaturer   rN   )rr   r~   es      r   rL   zVerifier.check   s^    ",,.BC  1	.r2! 	zzV# 	s   A 	A)A$$A)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/t#        
dd       }t#        
dd       }t#        
dd       }	t        dt!        
       dt!        |       dt!        |       dt!        |	             t	        
 |             rt        dt!        
       d |              |j                  dk(  st        |        =  j1                  |       y )Nr    .c                  P    j                         } t        d | D              rJ | S )Nc              3   ,   K   | ]  }|t         u   y wr"   objectr.   ts     r   r1   zOVerifier._check_graph_module.<locals>._allowed_getattr_types.<locals>.<genexpr>   s     411;   )rx   anyrV   rr   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                  f}t        |  |             s.|  |       vr%| |vr!t        d|  d |        d |        d|       t        | t              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   r1   zfVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_builtin_ops.<locals>.<genexpr>   s     ?3R7,,R03s   !)rs   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   r1   zcVerifier._check_graph_module.<locals>._check_valid_op.<locals>._allowed_op_types.<locals>.<genexpr>   s     8CqqF{Cr   )ru   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: z
operator 'z' is not functional)r   r	   r
   r   r7   autograd	grad_modeset_grad_enabledr2   r   r   r&   rz   )r   r   r   _allowed_torch_functionsrr   s       r   _check_valid_opz5Verifier._check_graph_module.<locals>._check_valid_op   s    $ 
uT#Y^'<  ).(@(@(Q(Q'S$b"3"56133BZ8Z,$RD(KL]L_K` a..B.D-E12J1KM  "j)$R(,$RD(;<  #r   >   call_methodcall_modulez'call_module is not valid: got a class 'z' rC   get_attrz/Expected get_attr target to be string, but got c                 2    t        t        | d       |      S r"   )r2   getattr)rF   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   modulesr2   r7   rw   r   graphlintnodesr   r   rE   r)   r5   rQ   r   nnModuler   byteslisthasattrr}   )rr   r{   r   r   rg   r'   r   r   r   r   r   s   `         @r   rM   zVerifier._check_graph_module   sA   	d3in(= 	
	$< ::<Cc588#7#78IINN		77<<,A$++bQ  WW/t$#DKK0WW
*%dkk370MdSWS^S^N_M`a  #34D!$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 (  t 	b!r   )r   r   r   rN   r   rs   r	   r
   r   ru   rx   rz   r   r}   r   r   rL   r7   rw   rM   r   r   r   rI   rI   T   s    G
T 
21%S	3"7 1'uT#Y^'< '; 4   D   ^"ehh&:&: ^"t ^" ^"r   rI   )	metaclassc           
      t   | 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 ]  \  }}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&                  vst        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      t        d|j                   d       t5        | j                  j                        d   }|j                  dk(  sJ |j6                  d   D 	cg c]  }	|	j                   }
}	t        |
      t        |j8                        k7  rDt        dt        |
       dt        |j:                         dt        |j<                         d      t        |j:                        t        |j>                        z   }|
d | }|
||t        |j<                        z    }|D ]  }||j:                  v rE|j:                  |   |j@                  vs-t        d| d|j:                   d|j@                   d      ||j>                  v rE|j>                  |   |j                  vst        d| d |j>                   d!|j                   d      t        d"| d#|j:                   d$|j>                          t        ||j<                        D ]&  \  }}||k7  st        d%| d&|j<                   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Constant tensor z+ is not in the tensor constants dictionary.zUnknown InputKind .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. )!r   r   r   r   rF   r@   input_specsr   user_inputszipr2   argr   r   kindr   
USER_INPUT	PARAMETERrE   
state_dictr7   r   	ParameterBUFFERCONSTANT_TENSORtensor_constantsr   argsoutput_specsbuffers_to_mutateuser_outputsuser_inputs_to_mutatebuffers)exported_programgsr'   input_node_names
input_specparambuffertensor_constoutput_noder   output_nodesendmutate_nodesuser_output_nodesmutation_nodeuser_output_nodeuser_output_names                    r   r   r      s   		)	)B /?.D.D.J.Jg.JddggYfNf		.J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-888(fX%?@  __	 9 99jnnn=(&z&77XYcYgYgXhhqr    (($Z__$5_E  &,,L#3#D#DD(&|n4_`  %$Z__$5Q7 } BF '--334R8K>>X%%%(3(8(8(;<(;CHH(;L<
<C00 %c,&7%8 9**-b.B.B*C)D E''*2??';&<DB
 	
 b""
#c"*B*B&C
CC*4C0L$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  /V_ hZ =s   V0V0V5rN   c                 p    | dk(  rt         j                  j                  |       S t         j                  |    S )NrO   )rH   rJ   get)rN   s    r   load_verifierr   m  s2    &&&**733""7++r   r   ).r   rn   rZ   collections.abcr   typingr   r   r   r   r   r	   r
   r7   
torch._opsr   r   torch._subclasses.fake_tensorr   torch.export.exported_programr   torch.export.graph_signaturer   r   r   r   torch.fxr   %torch.fx.experimental.symbolic_shapesr   r   r   	Exceptionr   r4   r&   rw   Noder*   r)   rQ   rH   rI   r   r5   r   r   r   r   <module>r      s       $ @ @ @  6 4 9  ! K K	 	%j %T % 4 
WUXX]] Wt W>D &Y" Y"xzz,3 ,8DN#; ,r   