U
    qh                     @  sl   d dl mZ d dlZd dlZd dlZd dlZdZG dd dejjZ	G dd dej
ZG dd	 d	ej
ZdS )
    )annotationsNaH  <
    ir_version: 8,
    opset_import: ["" : 17, "local" : 1]
>
agraph (float[N] X) => (float[N] Y) {
    Y = local.foo (X)
}

<opset_import: ["" : 17, "local" : 1], domain: "local">
foo (x) => (y) {
    temp = Add(x, x)
    y = local.bar(temp)
}

<opset_import: ["" : 17], domain: "local">
bar (x) => (y) {
    y = Mul (x, x)
}c                   @  s:   e Zd ZdZdZedhZddddZddd	d
ZdS )_OnnxTestTextualSerializerz:Serialize and deserialize the ONNX textual representation.onnxtextz	.onnxtextbytesreturnc                 C  s   t j|}|dS )Nutf-8)onnxprinterto_textencode)selfprototext r   @/tmp/pip-unpacked-wheel-xnis5xre/onnx/test/serialization_test.pyserialize_proto'   s    z*_OnnxTestTextualSerializer.serialize_proto)
serializedc                 C  s   | d}t|tjr"tj|S t|tjr:tj|S t|tjrRtj	|S t|tj
rjtj|S tdt| d S )Nr   zUnsupported proto type: )decode
isinstancer	   
ModelProtoparserparse_modelZ
GraphProtoZparse_graphZFunctionProtoZparse_functionZ	NodeProtoZ
parse_node
ValueErrortype)r   r   r   r   r   r   r   deserialize_proto+   s    
z,_OnnxTestTextualSerializer.deserialize_protoN)	__name__
__module____qualname____doc__Zsupported_format	frozensetZfile_extensionsr   r   r   r   r   r   r   !   s
   
r   c                   @  sD   e Zd ZddddZddddZddddZddd	d
ZdS )TestRegistryNoner   c                 C  s   t  | _tjj| j d S )N)r   
serializerr	   serializationregistryregisterr   r   r   r   setUp9   s    zTestRegistry.setUpc                 C  s    t jjd}| || j d S )Nr   )r	   r$   r%   getZassertIsr#   )r   r#   r   r   r   (test_get_returns_the_registered_instance=   s    z5TestRegistry.test_get_returns_the_registered_instancec              	   C  s(   |  t tjjd W 5 Q R X d S )Nunsupported)assertRaisesr   r	   r$   r%   r)   r'   r   r   r   &test_get_raises_for_unsupported_formatA   s    z3TestRegistry.test_get_raises_for_unsupported_formatc              
   C  s   t jt}t }tj|d}t j	||dd t
|dd"}| }| |t j| W 5 Q R X t j|dd}| |jdd|jdd W 5 Q R X d S )Nz
model.onnxr   )formatr   )encodingTZdeterministic)r	   r   r   _TEST_MODELtempfileTemporaryDirectoryospathjoinZ
save_modelopenreadassertEqualr
   r   Z
load_modelSerializeToString)r   modelZtmpdirZ
model_pathfcontentZloaded_modelr   r   r   4test_onnx_save_load_model_uses_the_custom_serializerE   s    


zATestRegistry.test_onnx_save_load_model_uses_the_custom_serializerN)r   r   r   r(   r*   r-   r>   r   r   r   r   r!   8   s   r!   c                   @  s   e Zd ZddddZdS )TestCustomSerializerr"   r   c                 C  sL   t  }tjt}||}||t }| |j	dd|j	dd d S )NTr0   )
r   r	   r   r   r1   r   r   r   r9   r:   )r   r#   r;   r   Zdeserializedr   r   r    test_serialize_deserialize_modelY   s    


z5TestCustomSerializer.test_serialize_deserialize_modelN)r   r   r   r@   r   r   r   r   r?   X   s   r?   )
__future__r   r4   r2   Zunittestr	   r1   r$   ZProtoSerializerr   ZTestCaser!   r?   r   r   r   r   <module>   s    