U
    qh                     @  s  d dl mZ d dlZdgZd dlZd dlmZmZmZmZm	Z	 d dl
Zd dlZd dlZd dlZe	dejjjdZdZG dd	 d	eZG d
d dZG dd deZG dd deZG dd deZG dd deZe Zee  ee  ee  ee  dS )    )annotationsNregistry)Any
CollectionOptionalProtocolTypeVar_Proto)boundzutf-8c                   @  sD   e Zd ZU dZded< ded< dddd	d
ZddddddZdS )ProtoSerializerzQA serializer-deserializer to and from in-memory Protocol Buffers representations.strsupported_formatzCollection[str]file_extensionsr	   r   protoreturnc                 C  s   dS )z6Serialize a in-memory proto to a serialized data type.N )selfr   r   r   6/tmp/pip-unpacked-wheel-xnis5xre/onnx/serialization.pyserialize_proto)   s    zProtoSerializer.serialize_proto
serializedr   r   c                 C  s   dS )z4Parse a serialized data type into a in-memory proto.Nr   r   r   r   r   r   r   deserialize_proto,   s    z!ProtoSerializer.deserialize_protoN)__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   r      s
   
r   c                   @  sJ   e Zd ZddddZdddddZd	dd
ddZd	ddddZdS )	_RegistryNone)r   c                 C  s   i | _ i | _d S N)_serializers_extension_to_format)r   r   r   r   __init__1   s    z_Registry.__init__r   )
serializerr   c                   s,    | j  j< | j fdd jD  d S )Nc                   s   i | ]}| j qS r   )r   ).0extr%   r   r   
<dictcomp>9   s      z&_Registry.register.<locals>.<dictcomp>)r"   r   r#   updater   )r   r%   r   r(   r   register6   s    z_Registry.registerr   )fmtr   c              	   C  sB   z| j | W S  tk
r<   td| d| j   dY nX dS )a  Get a serializer for a format.

        Args:
            fmt: The format to get a serializer for.

        Returns:
            ProtoSerializer: The serializer for the format.

        Raises:
            ValueError: If the format is not supported.
        zUnsupported format: 'z'. Supported formats are: N)r"   KeyError
ValueErrorkeys)r   r,   r   r   r   get<   s    z_Registry.getz
str | None)file_extensionr   c                 C  s   | j |S )zGet the corresponding format from a file extension.

        Args:
            file_extension: The file extension to get a format for.

        Returns:
            The format for the file extension, or None if not found.
        )r#   r0   )r   r1   r   r   r   get_format_from_file_extensionO   s    	z(_Registry.get_format_from_file_extensionN)r   r   r   r$   r+   r0   r2   r   r   r   r   r   0   s   r   c                   @  sB   e Zd ZdZdZeddhZddddd	Zdddd
ddZdS )_ProtobufSerializerz+Serialize and deserialize protobuf message.protobufz.onnxz.pbr	   bytesr   c              
   C  sz   t |drdt|jrdz| }W n> tk
r^ } z | tjjkrLtd| W 5 d }~X Y nX |S tdt	| d S )NSerializeToStringz~The proto size is larger than the 2 GB limit. Please use save_as_external_data to save tensors separately from the model file.z1No SerializeToString method is detected.
type is )
hasattrcallabler6   r.   ZByteSizeonnxcheckerZMAXIMUM_PROTOBUF	TypeErrortype)r   r   resulter   r   r   r   a   s    z#_ProtobufSerializer.serialize_protor   c                 C  sh   t |tstdt| ttt ||}|d k	rd|t	|krdt
jjd| dt	| |S )Nz4Parameter 'serialized' must be bytes, but got type: z*Protobuf decoding consumed too few bytes: z out of )
isinstancer5   r;   r<   typingcastr   intZParseFromStringlengoogler4   messageDecodeError)r   r   r   decodedr   r   r   r   q   s    
z%_ProtobufSerializer.deserialize_protoN	r   r   r   r   r   	frozensetr   r   r   r   r   r   r   r3   [   s
   r3   c                   @  sD   e Zd ZdZdZedddhZdddd	d
ZddddddZdS )_TextProtoSerializerz%Serialize and deserialize text proto.	textprotoz
.textprotoz	.prototxtz.pbtxtr	   r5   r   c                 C  s   t jj|}|tS r!   )rD   r4   text_formatZMessageToStringencode	_ENCODING)r   r   rK   r   r   r   r      s    z$_TextProtoSerializer.serialize_protobytes | strr   c                 C  sR   t |ttfs tdt| t |tr4|t}t |tsBttj	j
||S N;Parameter 'serialized' must be bytes or str, but got type: )r?   r5   r   r;   r<   decoderN   AssertionErrorrD   r4   rL   Parser   r   r   r   r      s    

z&_TextProtoSerializer.deserialize_protoNrH   r   r   r   r   rJ   ~   s
   rJ   c                   @  sB   e Zd ZdZdZeddhZddddd	Zd
dddddZdS )_JsonSerializerzSerialize and deserialize JSON.jsonz.jsonz	.onnxjsonr	   r5   r   c                 C  s   t jjj|dd}|tS )NT)Zpreserving_proto_field_name)rD   r4   json_formatZMessageToJsonrM   rN   )r   r   Zjson_messager   r   r   r      s
     z_JsonSerializer.serialize_protorO   r   c                 C  sR   t |ttfs tdt| t |tr4|t}t |tsBttj	j
||S rP   )r?   r5   r   r;   r<   rR   rN   rS   rD   r4   rW   rT   r   r   r   r   r      s    

z!_JsonSerializer.deserialize_protoNrH   r   r   r   r   rU      s
   rU   c                   @  sB   e Zd ZdZdZeddhZddddd	Zd
dddddZdS )_TextualSerializerz:Serialize and deserialize the ONNX textual representation.Zonnxtxtz.onnxtxtz	.onnxtextr	   r5   r   c                 C  s   t j|}|tS r!   )r9   printerZto_textrM   rN   )r   r   textr   r   r   r      s    z"_TextualSerializer.serialize_protorO   r   c                 C  s   t jddd t|ttfs.tdt| t|trD|t}n|}t|t	j
r`t	j|S t|t	jrxt	j|S t|t	jrt	j|S t|t	jrt	j|S tdt| d S )Nz[The onnxtxt format is experimental. Please report any errors to the ONNX GitHub repository.   )
stacklevelrQ   zUnsupported proto type: )warningswarnr?   r5   r   r;   r<   rR   rN   r9   Z
ModelProtoparserZparse_modelZ
GraphProtoZparse_graphZFunctionProtoZparse_functionZ	NodeProtoZ
parse_noder.   )r   r   r   rZ   r   r   r   r      s(    
z$_TextualSerializer.deserialize_protoNrH   r   r   r   r   rX      s
   rX   )
__future__r   r]   __all__r@   r   r   r   r   r   Zgoogle.protobuf.json_formatrD   Zgoogle.protobuf.messageZgoogle.protobuf.text_formatr9   r4   rE   Messager	   rN   r   r   r3   rJ   rU   rX   r   r+   r   r   r   r   <module>   s,   +##