o
    Vôj´  ã                   @  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                   @  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   úe/var/www/html/academy/tools/voices/clone/cosy-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__1   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>9   s    z&_Registry.register.<locals>.<dictcomp>)r"   r   r#   Úupdater   )r   r%   r   r(   r   Úregister6   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 )zæGet 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   r    )r,   r   r   r   )r1   r   r   r2   )r   r   r   r$   r+   r0   r3   r   r   r   r   r   0   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Ú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 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   q   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>   z
.textprotoz	.prototxtz.pbtxtr   r	   r   r6   c                 C  s   t jj |¡}| t¡S r!   )rG   r5   Útext_formatZMessageToStringÚencodeÚ	_ENCODING)r   r   rO   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: )rA   r6   r   r=   r>   ÚdecoderR   rG   r5   rP   ÚParser   r   r   r   r   ˆ   ó   ÿ

z&_TextProtoSerializer.deserialize_protoNrK   ©r   rS   r   r	   r   r	   rL   r   r   r   r   rN   ~   s    
rN   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 )Ú_JsonSerializerzSerialize and deserialize JSON.Újsonz.jsonz	.onnxjsonr   r	   r   r6   c                 C  s   t jjj|dd�}| t¡S )NT)Zpreserving_proto_field_name)rG   r5   Újson_formatZMessageToJsonrQ   rR   )r   r   Zjson_messager   r   r   r   ™   s   ÿ
z_JsonSerializer.serialize_protor   rS   c                 C  rT   rU   )rA   r6   r   r=   r>   rW   rR   rG   r5   r]   rX   r   r   r   r   r   Ÿ   rY   z!_JsonSerializer.deserialize_protoNrK   rZ   rL   r   r   r   r   r[   “   s    
r[   c                   @  s2   e Zd ZdZdZedhƒZddd	„Zddd„ZdS )Ú_TextualSerializerz:Serialize and deserialize the ONNX textual representation.Zonnxtxtz.onnxtxtr   r	   r   r6   c                 C  s   t j |¡}| t¡S r!   )r;   ÚprinterZto_textrQ   rR   )r   r   Útextr   r   r   r   °   s   
z"_TextualSerializer.serialize_protor   rS   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.é   )Ú
stacklevelrV   zUnsupported proto type: )ÚwarningsÚwarnrA   r6   r   r=   r>   rW   rR   r;   Ú
ModelProtoÚparserZparse_modelÚ
GraphProtoZparse_graphÚFunctionProtoZparse_functionÚ	NodeProtoÚ
parse_noder.   )r   r   r   r`   r   r   r   r   ´   s(   þÿ
z$_TextualSerializer.deserialize_protoNrK   rZ   rL   r   r   r   r   r^   ª   s    

r^   )Ú
__future__r   rc   Ú__all__rB   r   r   r   r   r   Zgoogle.protobuf.json_formatrG   Úgoogle.protobuf.messageZgoogle.protobuf.text_formatr;   r5   rH   ÚMessager	   rR   r   r   r4   rN   r[   r^   r   r+   r   r   r   r   Ú<module>   s.   ÿ+##