o
    Xic"                     @  s   d Z ddlmZ dgZddlZddlZddlmZmZ ddl	m
Z
 ddlZddlZd$ddZd%ddZd&ddZd'ddZ	d(d)d#dZdS )*zUtilities for testing.    )annotationsassert_onnx_proto_equalN)
CollectionSequence)Anyopset_importonnx.OperatorSetIdProtoreturntuple[str, int]c                 C  s   | j | jfS N)domainversion)r    r   C/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/testing.py_opset_import_key   s   r   
value_infoonnx.ValueInfoProtostrc                 C  s   | j S r   name)r   r   r   r   _value_info_key   s   r   functiononnx.FunctionPrototuple[str, str, str]c                 C  s   | j | jt| ddfS )Noverload )r   r   getattr)r   r   r   r   _function_key   s   r   with_duplicatesCollection[Any]	list[Any]c                 C  s4   t  }g }| D ]}||v r|| || q|S )z5Return a list of duplicated elements in a collection.)setappendadd)r   seen
duplicatesxr   r   r   _find_duplicates    s   
r'   Factual%google.protobuf.message.Message | Anyexpectedignore_initializer_value_protoboolNonec                   s  t | t |u sJ dt |  dt | dd |  D }dd | D }tt| t| B }t| tjrSt|tjrSdd | jD  dd |jD nt  t g }|D ] }t	| |}t	||}	t|t
rt|	t
rt|ttfst|	ttfsg }
g }|d	krt|td
}t|	td
}	dd |D }
dd |	D }n^|dkr|rt| tjrt|tjr͇ fdd|D }fdd|	D }	t|td
}t|	td
}	dd |D }
dd |	D }n|dkrt|td
}t|	td
}	dd |D }
dd |	D }|
|kr<t|
t| }t|t|
 }d| d| d| dt | dt|
 dt| }|| q]t|t|	kr_d| dt| dt|	 dt | }|| q]tt|D ]}|| }|	| }t|tjjjrt|tjjjrz
t|||d W qe ty } z,d| d | d!t | d"t | d#| d$| }t|d% | }|| W Y d&}~qed&}~ww ||krt|trt|trt|rt|rqed| d | d!t | d"t | }tt| t| D ]	}|d'| 7 }q|| qeq]t|tjjjr0t|	tjjjr0t||	|d q]||	kr^t|trNt|	trNt|rNt|	rNq]d| d(| d)|	 }|| q]|rvtd*t |  dt | d'd' | d&S )+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 != c                 S     i | ]\}}|j |qS r   r   .0fieldvaluer   r   r   
<dictcomp>C       z+assert_onnx_proto_equal.<locals>.<dictcomp>c                 S  r.   r   r   r/   r   r   r   r3   D   r4   c                 S     h | ]}|j qS r   r   r0   ir   r   r   	<setcomp>G       z*assert_onnx_proto_equal.<locals>.<setcomp>c                 S  r5   r   r   r6   r   r   r   r8   H   r9   r   )keyc                 S     g | ]}t |qS r   r   r0   r   r   r   r   
<listcomp>_       z+assert_onnx_proto_equal.<locals>.<listcomp>c                 S  r;   r   r<   r=   r   r   r   r>   `   r?   r   c                      g | ]	}|j  vr|qS r   r   r0   r   )actual_initializer_namesr   r   r>   h   
    
c                   r@   r   r   rA   )expected_initializer_namesr   r   r>   m   rC   c                 S  r;   r   r   rA   r   r   r   r>   t   r?   c                 S  r;   r   rE   rA   r   r   r   r>   u   r?   	functionsc                 S  r;   r   r   r0   rF   r   r   r   r>   y   r?   c                 S  r;   r   rG   rH   r   r   r   r>   z   r?   z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: )r+   z 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: )!type
ListFieldssortedr!   keys
isinstanceonnx
GraphProtoinitializerr   r   r   bytesr   r   r   r'   r"   lenrangegoogleprotobufmessageMessager   AssertionErrorfloatmathisnandifflibndiff
splitlinesjoin)r(   r*   r+   a_fieldsb_fields
all_fieldserrorsr1   a_valueb_valuea_keysb_keyskeys_only_in_actualkeys_only_in_expectederror_messager7   actual_value_iexpected_value_ieliner   )rB   rD   r   r   +   s  









0
$


"


")r   r   r	   r
   )r   r   r	   r   )r   r   r	   r   )r   r   r	   r    )F)r(   r)   r*   r)   r+   r,   r	   r-   )__doc__
__future__r   __all__r]   r[   collections.abcr   r   typingr   google.protobuf.messagerU   rO   r   r   r   r'   r   r   r   r   r   <module>   s    



