
     Ti[O                    V   S SK Jr  / SQrS SKrS SKrS SKJrJrJr  S SK	r
S SKrS SKrS SKJr  S SKrS SKJr  S rS rS	 rS
 r\4S jr\4S jrS rS"S jrS rS rS rS rS rS rS rS r  " S S5      r!S r"S r#S r$S r%S#S jr&S$S jr'S%S jr(S&S  jr) S'       S(S! jjr*g))    )annotations)assert_isomorphicassert_isomorphic_graphassert_isomorphic_functionassert_onnx_proto_equalN)Any
CollectionSequence)parser)irc                N    [        [        U 5      [        U5      5      (       d   eg)z.Assert two graphs or functions are isomorphic.N)_isomorphic_to_function_or_graph)graph_or_function_1graph_or_function_2s     Z/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxscript/testing/__init__.pyr   r      s+    1212       c                N    [        [        U 5      [        U5      5      (       d   eg)z!Assert two graphs are isomorphic.N)r   _to_graph_proto)graph1graph2s     r   r   r   !   s    v.0GHHHHr   c                N    [        [        U 5      [        U5      5      (       d   eg)z$Assert two functions are isomorphic.N)r   _to_function_proto)fn1fn2s     r   r   r   &   s!    )#.0B30GHHHHr   c                
    X:H  $ N )xys     r   _default_equality_opr!   +   s	    6Mr   c                    UR                  U 5      (       a3  UR                  U 5      =(       a    U" [        X5      [        X 5      5      $ UR                  U 5      (       + $ )zCheck two proto object have same value for optional field.
This is restricted to simple field types where == comparison is sufficient.
)HasFieldgetattr)fieldobj1obj2equalss       r   _same_optionalr)   /   sJ     }}U}}U#Zwt/CWTEY(ZZ}}U###r   c                r   ^ [        U 5      [        U5      :w  a  g[        U4S j[        X5       5       5      $ )NFc              3  8   >#    U  H  u  pT" X5      v   M     g 7fr   r   ).0val1val2r(   s      r   	<genexpr>!_same_repeated.<locals>.<genexpr>;   s     J4Ijdvd!!4Is   lenallzip)values1values2r(   s     `r   _same_repeatedr7   8   s,    
7|s7|#JC4IJJJr   c                *    S nU" U 5      U" U5      :H  $ )z0Compare repeated StringStringEntryProto as maps.c                Z    U  Vs0 s H  oR                   UR                  _M     sn$ s  snf r   )keyvalue)protor   s     r   to_map'_same_string_string_map.<locals>.to_mapA   s$    (-.1qww...s    (r   )proto1proto2r=   s      r   _same_string_string_maprA   >   s    / &>VF^++r   c                   U R                   UR                   :w  a  g[        SX5      (       d  gU R                  S5      (       d  UR                  S5      (       a  gU R                  UR                  s=:X  a  U R                  R
                  :X  ag  O  Od[        R                  " U 5      n[        R                  " U5      n[        R                  " UR                  5       UR                  5       SS9(       d  g[        SX5      (       d  g[        SX5      (       d  g[        U R                  UR                  5      (       d  gg)NF	data_typesegmentT)	equal_nan
doc_stringdata_location)dimsr)   r#   rG   DataLocationDEFAULTr   
from_protonparray_equalnumpyrA   external_data)tp1tp2tensor1tensor2s       r   _same_tensorrT   G   s    
xx388+s00
||I#,,y"9"9
C--I1A1A1I1II--$--$~~gmmow}}$O ,11/344"3#4#4c6G6GHHr   c                @    [        SX5      =(       a    [        SX5      $ )N	dim_value	dim_param)r)   )dim1dim2s     r   	_same_dimrZ   _   s    +t2^~kSW7^^r   c                L    [        U R                  UR                  [        5      $ r   )r7   dimrZ   )shape1shape2s     r   _same_shaper_   c   s    &**fjj)<<r   c                d    U R                   UR                   :H  =(       a    [        SX[        5      $ )Nshape)	elem_typer)   r_   )tt1tt2s     r   _same_tensor_typere   g   s$    MMS]]*^wR]0^^r   c                $    [        SX[        5      $ )Ntensor_type)r)   re   )rP   rQ   s     r   
_same_typerh   k   s    -3DEEr   c                p    [        SX5      =(       a$    [        SX[        5      =(       a    [        SX5      $ )NnametyperF   )r)   rh   )vi1vi2s     r   _same_value_inforn   p   s1    vs( 	363Z8	3<2r   c                   S H  n[        X0U5      (       a  M    g   [        SX[        5      (       d  g[        U R                  UR                  [        5      (       d  gS H  n[	        X5      [	        X5      :w  d  M    g   [        SXU5      (       d  g[        U R
                  UR
                  U5      (       d  gS H1  nU R                  U5      (       d  UR                  U5      (       d  M1    g   g)N)rk   ref_attr_namefisFt)floatsintsstringsg)sparse_tensortpT)r)   rT   r7   tensorsr$   graphsr#   )attr1attr2graph_equalityr%   s       r   
_same_attrr   x   s    9eE22 : #u\::%--EE.5 GE$99 / #u^<<%,,nEE(>>%  ENN5$9$9 ) r   c                    [        U 5      [        U5      :w  a  gU  Vs0 s H  o3R                  U_M     nnU H6  nUR                  U;  a    gXER                     n[        XeU5      (       a  M6    g   gs  snf )NFT)r2   rj   r   )attrs1attrs2r   a	attrs1mapr~   r}   s          r   _same_attrsr      sn    
6{c&k!$*+FqFI+::Y&**%%77   ,s   A3c                \    [        U [        R                  5      (       a  U R                  $ U $ )z9Return the name of an input/output of a function or graph)
isinstanceonnxValueInfoProtorj   )r   s    r   _ionamer      s#    4#6#677166>Q>r   c                  F    \ rS rSrSrSS jrS rS rS rS r	S r
S	 rS
rg)_Matcher   z7An isomorphism matcher for two functions or two graphs.c                p    S nU" U5      U l         U" U5      U l        Xl        X l        0 U l        X0l        g )Nc                    0 n[        U R                  5       H  u  p#SU4U[        U5      '   M     [        U R                  5       H(  u  pE[        UR                  5       H
  u  pcXF4X'   M     M*     U$ )zCompute a map from variables v to their definition-sites.
A definition-site (n, i) indicates the i-th output of n-th node
The special value (-1, i) is used to indicate the i-th input of a function/graph.
)	enumerateinputr   nodeoutput)rq   resultrr   r   ninxis          r   defmap!_Matcher.__init__.<locals>.defmap   sj    
 F!!''*&(!Wwqz" +"166*&qxx0EB!#FI 1 + Mr   defmap1defmap2fg1fg2node_mappingouter_scope)selfr   r   r   r   s        r   __init___Matcher.__init__   s7    	 c{c{BD&r   c                l   US:X  a  US:H  $ US:X  a  gXR                   ;  d  X R                  ;  aG  XR                   ;   d  X R                  ;   d  U R                  c  gU R                  R                  X5      $ U R                   U   u  p4U R                  U   u  pVXF:H  =(       a    U R	                  X55      $ )zMatch two variables (strings). F)r   r   r   
same_value	same_node)r   var1var2node1index1node2index2s          r   r   _Matcher.same_value   s    2:2:2:||#t<<'?$$,,*>DDTDTD\##..t::,,t,,,t, BdnnU&BBr   c                    US:X  a  US:X  a  gUS:X  d  US:X  a  gXR                   ;   a  U R                   U   U:H  $ U R                  R                  U   nU R                  R                  U   nUR                  UR                  :w  a  gUR
                  UR
                  :w  a  g[        UR                  UR                  U R                  5      (       d  gU R                  UR                  UR                  5      (       d  gX R                   U'   g)zDMatch two node-indices. The special node-index -1 represents inputs.r   TF)r   r   r   r   op_typedomainr   	attributesame_sub_graphsame_value_listr   )r   n1n2r   r   s        r   r   _Matcher.same_node   s    "H28"H"("""$$R(B..b!b!==EMM)<<5<<'5??EOOT=P=PQQ##EKK== !#"r   c                r   ^  [        U5      [        U5      :w  a  g[        U 4S j[        X5       5       5      $ )z@Match two lists of variables (either a string or ValueInfoProto)Fc              3  p   >#    U  H+  u  pTR                  [        U5      [        U5      5      v   M-     g 7fr   )r   r   )r,   r   r    r   s      r   r/   +_Matcher.same_value_list.<locals>.<genexpr>   s+     YGXtq4??71:wqz::GXs   36r1   )r   list1list2s   `  r   r   _Matcher.same_value_list   s,    u:U#Ys5GXYYYr   c                :    [        XU 5      nUR                  5       $ )Match two sub-graphs.)r   
same_graph)r   g1g2sub_graph_matchers       r   r   _Matcher.same_sub_graph   s    $RT2 ++--r   c                n   U R                   nU R                  n[        UR                  UR                  [        5      (       d  gUR
                  (       d  UR
                  (       a  gUR                  (       d  UR                  (       a  gU R                  UR                  UR                  5      (       d  gg)r   FT)	r   r   r7   r   rn   initializersparse_initializerr   r   )r   r   r   s      r   r   _Matcher.same_graph   st    XXXXbhh2BCC>>R^^  B$9$9##BIIryy99r   c                h   [        U R                  R                  5      [        U R                  R                  5      :w  a  g[	        U R                  R
                  5      [	        U R                  R
                  5      :w  a  gS nU" U R                  5      U" U R                  5      :w  a  g[        U R                  R                  5      [        U R                  R                  5      :w  a  gU R                  U R                  R                  U R                  R                  5      (       d  g[        U R                  5      [        U R                  R                  5      :w  a  g[        [	        U R                  R                  5       5      5      [        U R                  R                  5      :w  a  gg)z Match (top-level) two functions.Fc                n    U R                    Vs0 s H  oR                  UR                  _M     sn$ s  snf r   )opset_importr   version)rq   entrys     r   imports'_Matcher.same_function.<locals>.imports  s)    =>^^L^ELL%--/^LLLs    2T)r2   r   r   r   setr   r   r   r   r   values)r   r   s     r   same_function_Matcher.same_function  s   
 txx~~#dhhnn"55txx!!"c$((*<*<&==	M 488 11 txx}}TXX]]!33##DHHOOTXX__EE t  !S%77s4$$++-./3txx}}3EEr   r   N)returnNone)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   __static_attributes__r   r   r   r   r      s*    A'*C 0Z.
 %r   r   c                   [        XS5      n[        U [        R                  5      (       a:  [        U[        R                  5      (       d  [	        S5      eUR                  5       $ [        U [        R                  5      (       a:  [        U[        R                  5      (       d  [	        S5      eUR                  5       $ [	        S5      e)zChecks that two function/graph bodies are isomorphic.
Assumes that the inputs are valid FunctionProto/GraphProto.
Use a separate check to verify that the inputs satisfy
FunctionProto/GraphProto requirements (like no duplicate attributes).
Nz1Both inputs must be same type (function or graph)z3Inputs must be either a FunctionProto or GraphProto)r   r   r   FunctionProto	TypeErrorr   
GraphProtor   )r   r   matchers      r   r   r   *  s     s&G#t))**#t1122OPP$$&&#t''#t//OPP!!##
I
JJr   c                (   [        U [        R                  5      (       a  U $ [        U [        R                  5      (       a  U R                  5       $ [        U [        5      (       a  [        R                  " U 5      $ [        S[        U 5       S35      e)NCannot convert z to FunctionProto)r   r   r   
onnxscriptOnnxFunctionto_function_protostrr   parse_functionr   rk   )rq   s    r   r   r   <  ss    !T''((!Z,,--""$$!S$$Q''
od1gY.?@
AAr   c                <   [        U [        R                  5      (       a  U $ [        U [        R                  5      (       a  U R                  5       R                  $ [        U [        5      (       a  [        R                  " U 5      $ [        S[        U 5       S35      e)Nr   z to ModelProto)r   r   r   r   r   to_model_protographr   r   parse_graphr   rk   )rx   s    r   r   r   F  sv    !T__%%!Z,,--!'''!S!!!$$
od1gYn=
>>r   c                j   [        U [        R                  5      (       a  U $ [        U [        R                  5      (       a  U $ [        U [        R                  5      (       a  U R
                  $ [        U [        R                  5      (       a  U R                  5       $ [        S[        U 5       S35      e)Nr   z to FunctionProto or GraphProto)r   r   r   r   
ModelProtor   r   r   r   r   rk   )objs    r   r   r   P  s    #t))**
#t''
#t''yy#z..//$$&&
od3i[0OP
QQr   c                2    U R                   U R                  4$ r   )r   r   )r   s    r   _opset_import_keyr   \  s    !5!566r   c                    U R                   $ r   )rj   )
value_infos    r   _value_info_keyr   `  s    ??r   c                J    U R                   U R                  [        U SS5      4$ )Noverloadr   )r   rj   r$   )functions    r   _function_keyr   d  s     OOX]]GHj",MNNr   c                ~    [        5       n/ nU  H*  nX1;   a  UR                  U5        UR                  U5        M,     U$ )z5Return a list of duplicated elements in a collection.)r   appendadd)with_duplicatesseen
duplicatesr   s       r   _find_duplicatesr   h  s>    5DJ9a   r   c                |   [        U 5      [        U5      L d   S[        U 5       S[        U5       35       eU R                  5        VVs0 s H  u  p4UR                  U_M     nnnUR                  5        VVs0 s H  u  p4UR                  U_M     nnn[        [	        UR                  5       5      [	        UR                  5       5      -  5      n[        U [        R                  5      (       aj  [        U[        R                  5      (       aK  U R                   Vs1 s H  oR                  iM     n	nUR                   Vs1 s H  oR                  iM     n
nO[	        5       n	[	        5       n
/ nU GHj  n[        X5      n[        X5      n[        U[        5      (       Ga@  [        U[        5      (       Ga*  [        U[        [        45      (       Gd  [        U[        [        45      (       Gd  / n/ nUS:X  aT  [        U[        S9n[        U[        S9nU Vs/ s H  n[        U5      PM     nnU Vs/ s H  n[        U5      PM     nnGO>US:X  a  U(       a  [        U [        R                  5      (       ag  [        U[        R                  5      (       aH  U Vs/ s H  nUR                  U	;  d  M  UPM     nnU Vs/ s H  nUR                  U
;  d  M  UPM     nn[        U[        S9n[        U[        S9nU Vs/ s H  n[        U5      PM     nnU Vs/ s H  n[        U5      PM     nnOXUS:X  aR  [        U[         S9n[        U[         S9nU Vs/ s H  n[!        U5      PM     nnU Vs/ s H  n[!        U5      PM     nnX:w  aq  [	        U5      [	        U5      -
  n[	        U5      [	        U5      -
  nSU SU S	U S
[        U5       S[#        U5       S[#        U5       3nUR%                  U5        GM  ['        U5      ['        U5      :w  a=  SU S['        U5       S['        U5       S[        U5       3nUR%                  U5        GM  [)        ['        U5      5       GHo  nX   nX   n[        U[*        R,                  R.                  R0                  5      (       aA  [        U[*        R,                  R.                  R0                  5      (       a   [3        UUUS9  M  UU:w  d  M  [        U[6        5      (       aM  [        U[6        5      (       a8  [8        R:                  " U5      (       a  [8        R:                  " U5      (       a  M  SU SU S[        U5       S[        U5       3n[<        R>                  " [        U5      RA                  5       [        U5      RA                  5       5       H  nUSU-   -  nM     UR%                  U5        GMr     GMp  [        U[*        R,                  R.                  R0                  5      (       a@  [        U[*        R,                  R.                  R0                  5      (       a  [3        XUS9  GM  X:w  d  GM  [        U[6        5      (       aN  [        U[6        5      (       a9  [8        R:                  " U5      (       a  [8        R:                  " U5      (       a  GMN  SU SU SU 3nUR%                  U5        GMm     U(       a6  [5        S[        U 5       S[        U5       S3SRC                  U5      -   5      egs  snnf s  snnf s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf ! [4         aT  nSU SU S[        U5       S[        U5       SU SU 3n[        U5      S-   U-   nUR%                  U5         SnAGMK  SnAff = f)a  Assert that two ONNX protos are equal.

Equality is defined as having the same fields with the same values. When
a field takes the default value, it is considered equal to the field
not being set.

Sequential fields with name `opset_import`, `value_info`, and `functions` are
compared disregarding the order of their elements.

Args:
    actual: The first ONNX proto.
    expected: The second ONNX proto.
    ignore_initializer_value_proto: Ignore value protos for initializers if there
        are extra ones in the actual proto.
zType not equal: z != r   )r:   r   	functionszField z  not equal: keys_only_in_actual=z, keys_only_in_expected=z. Field type: z. Duplicated a_keys: z, duplicated b_keys: z not equal: len(a)=z	, len(b)=z Field type: )ignore_initializer_value_protoz index z. in sequence not equal. type(actual_value_i): z, type(expected_value_i): z, actual_value_i: z, expected_value_i: z

Caused by the above error

N
z not equal. field_actual: z, field_expected: zProtos not equal: )"rk   
ListFieldsrj   sortedr   keysr   r   r   r   r$   r
   r   bytesr   r   r   r   r   r2   rangegoogleprotobufmessageMessager   AssertionErrorfloatmathisnandifflibndiff
splitlinesjoin)actualexpectedr  r%   r;   a_fieldsb_fields
all_fieldsrr   actual_initializer_namesexpected_initializer_nameserrorsa_valueb_valuea_keysb_keysr   r   r  keys_only_in_actualkeys_only_in_expectederror_messageactual_value_iexpected_value_ielines                             r   r   r   s  sb   ( <4>) 
4<.T(^,<=) 7=6G6G6IJ6Ile

E!6IHJ6>6I6I6KL6Kle

E!6KHLHMMO,s8==?/CCDJ&$//**z(DOO/T/T4:4F4F#G4FqFF4F #G6>6J6J%K6Jff6J"%K"#&5 %(U" F&((*w))7H--we55we55 !#F "F& .?@ .?@NUVgl+L9gVNUVgl+L9gV,&2"64??;;"8T__==
 +2*1J%??2JJ #*1   +2*1J%??2LL #*1  
 !o> o>HOP*/*5PHOP*/*5P+% m< m<DKLGy-	2GLDKLGy-	2GL&)&kCK&?#(+Fc&k(A%UG#CDWCXXp  rG  qH H##'=/ 2**:6*B)CCXYijpYqXrt 
 m,WW-UG#6s7|nIcRYl^ \##'=/3  m, s7|,A%,ZN'.z$!&(?(?(G(G $%5v7N7N7V7VWW93 . 0?] (+;;&~u== *+;U C C $

> : : $

+; < < %*0wqcAopt  vD  qE  pF  F`  ae  fv  aw  `x  )y$+MM/::< 01<<>%D *TD[8M	%
 m4C -D !8!8!@!@AAjV__,,44G
 G
 $A_ 7E**w..JJw''JJw'' 9'BTU\T]^  MM-(e f  fd4>2B"E		RXHYY
 	
  KL $H%K. WV
 QP ML>  . 9.4UG71#Estx  zH  uI  tJ  Jd  ei  jz  e{  d|  |N  O]  N^  ^r  sC  rD  -EM #A)L L} \ * #MM-889sm   
\9\%\+\0\55\:\?5\?]]]	]]5]]
^;'A^66^;)rP   onnx.TensorProtorQ   r)  )r   zonnx.OperatorSetIdProtor   ztuple[str, int])r   zonnx.ValueInfoProtor   r   )r   zonnx.FunctionProtor   ztuple[str, str, str])r   zCollection[Any]r   z	list[Any])F)r  %google.protobuf.message.Message | Anyr  r*  r  boolr   r   )+
__future__r   __all__r  r  typingr   r	   r
   google.protobuf.messager
  rN   rL   r   r   r   r   r   r   r   r!   r)   r7   rA   rT   rZ   r_   re   rh   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r0     s    #   , ,      I
I
 .B $ -A K,0_=_F
:
?
@ @FK$B?	R7O ,1Z
1Z
3Z
 %)Z
 
	Z
r   