o
    soi                     @  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 d dlZ	d dl
Z	d dlZ	d dlZejr4d 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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   F/home/ubuntu/.local/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                 C  s(   || j |j< | jt|j|j d S r    )r!   r   r"   updatedictfromkeysr   )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"   r-   )r   r.   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
   )r.   r   r   r/   )r   r   r   r#   r(   r-   r0   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callabler4   r+   ByteSizeonnxcheckerMAXIMUM_PROTOBUF	TypeErrortype)r   r   resulter   r   r   r   d   s"   
z#_ProtobufSerializer.serialize_protor   c                 C  sd   t |tstdt| td||}|d ur0|t|kr0tj	j
d| dt| |S )Nz4Parameter 'serialized' must be bytes, but got type: z
int | Nonez*Protobuf decoding consumed too few bytes: z out of )
isinstancer3   r;   r<   typingcastParseFromStringlengoogler2   messageDecodeError)r   r   r   decodedr   r   r   r   t   s   
z%_ProtobufSerializer.deserialize_protoNr   r   r   r3   )r   r3   r   r   r   r   	r   r   r   r   r   	frozensetr   r   r   r   r   r   r   r1   ^   s    
r1   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.txtpb	.prototxt
.textprotor   r   r   r3   c                 C  s   t jj|}|tS r    )rD   r2   text_formatMessageToStringencode	_ENCODING)r   r   rL   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: )r?   r3   r   r;   r<   decoderT   rD   r2   rQ   Parser   r   r   r   r         

z&_TextProtoSerializer.deserialize_protoNrH   r   rU   r   r   r   r   rI   r   r   r   r   rK          
rK   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   r3   c                 C  s   t jjj|dd}|tS )NT)preserving_proto_field_name)rD   r2   json_formatMessageToJsonrS   rT   )r   r   json_messager   r   r   r      s   
z_JsonSerializer.serialize_protor   rU   c                 C  rV   rW   )r?   r3   r   r;   r<   rY   rT   rD   r2   rb   rZ   r   r   r   r   r      r[   z!_JsonSerializer.deserialize_protoNrH   r\   rI   r   r   r   r   r_      s    
r_   c                   @  r^   )_TextualSerializerz:Serialize and deserialize the ONNX textual representation.onnxtxtz.onnxtxtz	.onnxtextr   r   r   r3   c                 C  s   t j|}|tS r    )r8   printerto_textrS   rT   )r   r   textr   r   r   r      s   
z"_TextualSerializer.serialize_protor   rU   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.   )
stacklevelrX   zUnsupported proto type: )warningswarnr?   r3   r   r;   r<   rY   rT   r8   
ModelProtoparserparse_model
GraphProtoparse_graphFunctionProtoparse_function	NodeProto
parse_noder+   )r   r   r   ri   r   r   r   r      s(   
z$_TextualSerializer.deserialize_protoNrH   r\   rI   r   r   r   r   re      r]   re   )
__future__r   rl   __all__r@   r   r   r   google.protobuf.json_formatrD   google.protobuf.messagegoogle.protobuf.text_formatr8   TYPE_CHECKINGcollections.abcr   r2   rE   Messager   rT   r
   r   r1   rK   r_   re   r   r(   r   r   r   r   <module>   s2   +##