o
    toi                     @  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                   @  s2   e Zd ZdZdZedhZdddZdd	d
ZdS )_OnnxTestTextualSerializerz:Serialize and deserialize the ONNX textual representation.onnxtextz	.onnxtextreturnbytesc                 C  s   t j|}|dS )Nutf-8)onnxprinterto_textencode)selfprototext r   P/home/ubuntu/.local/lib/python3.10/site-packages/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r)tj	|S t|tj
r5tj|S tdt| )Nr   zUnsupported proto type: )decode
isinstancer   
ModelProtoparserparse_model
GraphProtoparse_graphFunctionProtoparse_function	NodeProto
parse_node
ValueErrortype)r   r   r   r   r   r   r   deserialize_proto+   s   
z,_OnnxTestTextualSerializer.deserialize_protoN)r   r   )r   r   )	__name__
__module____qualname____doc__supported_format	frozensetfile_extensionsr   r    r   r   r   r   r   !   s    

r   c                   @  s4   e Zd ZdddZdddZdddZdd	d
ZdS )TestRegistryr   Nonec                 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,   get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 d    d S 1 sw   Y  d S )Nunsupported)assertRaisesr   r   r+   r,   r0   r.   r   r   r   &test_get_raises_for_unsupported_formatA   s   "z3TestRegistry.test_get_raises_for_unsupported_formatc              	   C  s   t jt}t Q}tj|d}t j	||dd t
|dd}| }| |t j| W d    n1 s9w   Y  t j|dd}| |jdd|jdd W d    d S 1 s^w   Y  d S )Nz
model.onnxr   )formatr   )encodingTdeterministic)r   r   r   _TEST_MODELtempfileTemporaryDirectoryospathjoin
save_modelopenreadassertEqualr	   r
   
load_modelSerializeToString)r   modeltmpdir
model_pathfcontent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/   r2   r5   rL   r   r   r   r   r(   8   s
    


r(   c                   @  s   e Zd ZdddZdS )TestCustomSerializerr   r)   c                 C  sL   t  }tjt}||}||t }| |j	dd|j	dd d S )NTr8   )
r   r   r   r   r:   r   r    r   rC   rE   )r   r*   rF   r   deserializedr   r   r    test_serialize_deserialize_modelY   s   


z5TestCustomSerializer.test_serialize_deserialize_modelNrM   )r!   r"   r#   rP   r   r   r   r   rN   X   s    rN   )
__future__r   r=   r;   unittestr   r:   r+   ProtoSerializerr   TestCaser(   rN   r   r   r   r   <module>   s    