o
    wi                     @  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 d dl	Z
d dlZ
d dlZ
d dlZejr6d dlm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OptionalProtocolTypeVar)
Collection_Proto)boundzutf-8c                   @  s6   e Zd ZU dZded< ded< dd
dZdddZdS )ProtoSerializerzQA serializer-deserializer to and from in-memory Protocol Buffers representations.strsupported_formatzCollection[str]file_extensionsprotor	   returnr   c                 C     dS )z6Serialize a in-memory proto to a serialized data type.N )selfr   r   r   O/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/serialization.pyserialize_proto,       zProtoSerializer.serialize_proto
serializedc                 C  r   )z4Parse a serialized data type into a in-memory proto.Nr   r   r   r   r   r   r   deserialize_proto/   r   z!ProtoSerializer.deserialize_protoN)r   r	   r   r   )r   r   r   r	   r   r	   )__name__
__module____qualname____doc____annotations__r   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 )	_Registryr   Nonec                 C  s   i | _ i | _d S N)_serializers_extension_to_format)r   r   r   r   __init__4   s   
z_Registry.__init__
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><   s    z&_Registry.register.<locals>.<dictcomp>)r"   r   r#   updater   )r   r%   r   r(   r   register9   s   z_Registry.registerfmtr   c              	   C  s8   z| j | W S  ty   td| d| j   dw )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.getfile_extension
str | Nonec                 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_extensionR   s   	z(_Registry.get_format_from_file_extensionN)r   r    )r%   r   r   r    )r,   r   r   r   )r1   r   r   r2   )r   r   r   r$   r+   r0   r3   r   r   r   r   r   3   s
    


r   c                   @  s4   e Zd ZdZdZeddhZdd	d
ZdddZdS )_ProtobufSerializerz+Serialize and deserialize protobuf message.protobufz.onnxz.pbr   r	   r   bytesc              
   C  sj   t |dr,t|jr,z| }W |S  ty+ } z| tjjkr&td| d }~ww tdt	| )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callabler7   r.   ByteSizeonnxcheckerMAXIMUM_PROTOBUF	TypeErrortype)r   r   resulter   r   r   r   d   s"   
z#_ProtobufSerializer.serialize_protor   c                 C  sh   t |tstdt| ttt ||}|d ur2|t	|kr2t
jjd| dt	| |S )Nz4Parameter 'serialized' must be bytes, but got type: z*Protobuf decoding consumed too few bytes: z out of )
isinstancer6   r>   r?   typingcastr   intParseFromStringlengoogler5   messageDecodeError)r   r   r   decodedr   r   r   r   t   s   
z%_ProtobufSerializer.deserialize_protoNr   r	   r   r6   )r   r6   r   r	   r   r	   	r   r   r   r   r   	frozensetr   r   r   r   r   r   r   r4   ^   s    
r4   c                   @  s4   e Zd ZdZdZeh dZddd	ZdddZdS )_TextProtoSerializerz%Serialize and deserialize text proto.	textproto>   .pbtxt	.prototxt
.textprotor   r	   r   r6   c                 C  s   t jj|}|tS r!   )rH   r5   text_formatMessageToStringencode	_ENCODING)r   r   rP   r   r   r   r      s   
z$_TextProtoSerializer.serialize_protor   bytes | strc                 C  R   t |ttfstdt| t |tr|t}t |ts!J tjj	
||S N;Parameter 'serialized' must be bytes or str, but got type: )rB   r6   r   r>   r?   decoderW   rH   r5   rT   Parser   r   r   r   r         

z&_TextProtoSerializer.deserialize_protoNrL   r   rX   r   r	   r   r	   rM   r   r   r   r   rO          
rO   c                   @  4   e Zd ZdZdZeddhZdd	d
ZdddZdS )_JsonSerializerzSerialize and deserialize JSON.jsonz.jsonz	.onnxjsonr   r	   r   r6   c                 C  s   t jjj|dd}|tS )NT)preserving_proto_field_name)rH   r5   json_formatMessageToJsonrV   rW   )r   r   json_messager   r   r   r      s   
z_JsonSerializer.serialize_protor   rX   c                 C  rY   rZ   )rB   r6   r   r>   r?   r\   rW   rH   r5   re   r]   r   r   r   r   r      r^   z!_JsonSerializer.deserialize_protoNrL   r_   rM   r   r   r   r   rb      s    
rb   c                   @  ra   )_TextualSerializerz:Serialize and deserialize the ONNX textual representation.onnxtxtz.onnxtxtz	.onnxtextr   r	   r   r6   c                 C  s   t j|}|tS r!   )r;   printerto_textrV   rW   )r   r   textr   r   r   r      s   
z"_TextualSerializer.serialize_protor   rX   c                 C  s   t jddd t|ttfstdt| t|tr"|t}n|}t|t	j
r0t	j|S t|t	jr<t	j|S t|t	jrHt	j|S t|t	jrTt	j|S tdt| )Nz[The onnxtxt format is experimental. Please report any errors to the ONNX GitHub repository.   )
stacklevelr[   zUnsupported proto type: )warningswarnrB   r6   r   r>   r?   r\   rW   r;   
ModelProtoparserparse_model
GraphProtoparse_graphFunctionProtoparse_function	NodeProto
parse_noder.   )r   r   r   rl   r   r   r   r      s(   
z$_TextualSerializer.deserialize_protoNrL   r_   rM   r   r   r   r   rh      r`   rh   )
__future__r   ro   __all__rC   r   r   r   r   google.protobuf.json_formatrH   google.protobuf.messagegoogle.protobuf.text_formatr;   TYPE_CHECKINGcollections.abcr   r5   rI   Messager	   rW   r   r   r4   rO   rb   rh   r   r+   r   r   r   r   <module>   s2   +##