
    bIg                       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"                        ZdZ G d d	e      Z G d
 d      Z G d de      Z G d de      Z G d de      Z G d de      Z e       Zej7                   e              ej7                   e              ej7                   e              ej7                   e              y)    )annotationsNregistry)Any
CollectionOptionalProtocolTypeVar_Proto)boundzutf-8c                  6    e Zd ZU dZded<   ded<   d	dZd
dZy)ProtoSerializerzQA serializer-deserializer to and from in-memory Protocol Buffers representations.strsupported_formatzCollection[str]file_extensionsc                     y)z6Serialize a in-memory proto to a serialized data type.N )selfprotos     O/var/www/html/answerous/venv/lib/python3.12/site-packages/onnx/serialization.pyserialize_protozProtoSerializer.serialize_proto)           c                     y)z4Parse a serialized data type into a in-memory proto.Nr   r   
serializedr   s      r   deserialize_protoz!ProtoSerializer.deserialize_proto,   r   r   N)r   r
   returnr   )r   r   r   r
   r   r
   )__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   r      s!    [  %$ECr   r   c                  ,    e Zd ZddZddZddZd	dZy)
	_Registryc                     i | _         i | _        y N)_serializers_extension_to_format)r   s    r   __init__z_Registry.__init__1   s    8:46!r   c                    || j                   |j                  <   | j                  j                  |j                  D ci c]  }||j                   c}       y c c}w r&   )r'   r   r(   updater   )r   
serializerexts      r   registerz_Registry.register6   sN    9C*556!!((9C9S9ST#S*---T	
Ts   Ac                    	 | j                   |   S # t        $ r+ t        d| d| j                   j                                dw xY 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   fmts     r   getz_Registry.get<   sX    	$$S)) 	'u,FtGXGXG]G]G_F`a	s	    4Ac                8    | j                   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(   r4   )r   file_extensions     r   get_format_from_file_extensionz(_Registry.get_format_from_file_extensionO   s     ((,,^<<r   N)r   None)r,   r   r   r8   )r3   r   r   r   )r6   r   r   z
str | None)r   r   r    r)   r.   r4   r7   r   r   r   r$   r$   0   s    7

&	=r   r$   c                  8    e Zd ZdZdZ eddh      ZddZd	dZy)
_ProtobufSerializerz+Serialize and deserialize protobuf message.protobufz.onnxz.pbc                *   t        |d      r(t        |j                        r	 |j                         }|S t        dt        |             # t        $ r=}|j	                         t
        j                  j                  k\  rt        d      | d }~ww xY w)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callabler=   r1   ByteSizeonnxcheckerMAXIMUM_PROTOBUF	TypeErrortype)r   r   resultes       r   r   z#_ProtobufSerializer.serialize_protoa   s    5-.8E<S<S3T002 M@eN
 	
  >>#t||'D'DD$k  s   A 	B8BBc                D   t        |t              st        dt        |             t	        j
                  t        t           |j                  |            }|F|t        |      k7  r8t        j                  j                  j                  d| dt        |             |S )Nz4Parameter 'serialized' must be bytes, but got type: z*Protobuf decoding consumed too few bytes: z out of )
isinstancebytesrD   rE   typingcastr   intParseFromStringlengoogler;   messageDecodeError)r   r   r   decodeds       r   r   z%_ProtobufSerializer.deserialize_protoq   s    *e,FtJGWFXY  ++hsmU-B-B:-NO7c*o#=//))55<WIXcR\oM^_  r   Nr   r
   r   rJ   )r   rJ   r   r
   r   r
   	r   r   r    r!   r   	frozensetr   r   r   r   r   r   r:   r:   [   s$    5!% 01O
 
r   r:   c                  8    e Zd ZdZdZ eh d      ZddZddZy)	_TextProtoSerializerz%Serialize and deserialize text proto.	textproto>   .pbtxt	.prototxt
.textprotoc                ~    t         j                  j                  j                  |      }|j	                  t
              S r&   )rP   r;   text_formatMessageToStringencode	_ENCODING)r   r   rY   s      r   r   z$_TextProtoSerializer.serialize_proto   s-    OO//??F		**r   c                   t        |t        t        f      st        dt	        |             t        |t              r|j                  t              }t        |t              sJ t        j                  j                  j                  ||      S N;Parameter 'serialized' must be bytes or str, but got type: )rI   rJ   r   rD   rE   decodera   rP   r;   r^   Parser   s      r   r   z&_TextProtoSerializer.deserialize_proto   w    *ucl3MdS]N^M_`  j%(#**95J*c*****00UCCr   NrT   r   zbytes | strr   r
   r   r
   rU   r   r   r   rX   rX   ~   s!    /" EFO+Dr   rX   c                  8    e Zd ZdZdZ eddh      ZddZd	dZy)
_JsonSerializerzSerialize and deserialize JSON.jsonz.jsonz	.onnxjsonc                    t         j                  j                  j                  |d      }|j	                  t
              S )NT)preserving_proto_field_name)rP   r;   json_formatMessageToJsonr`   ra   )r   r   json_messages      r   r   z_JsonSerializer.serialize_proto   s:    22@@t A 
 ""9--r   c                   t        |t        t        f      st        dt	        |             t        |t              r|j                  t              }t        |t              sJ t        j                  j                  j                  ||      S rc   )rI   rJ   r   rD   rE   re   ra   rP   r;   rn   rf   r   s      r   r   z!_JsonSerializer.deserialize_proto   rg   r   NrT   rh   rU   r   r   r   rj   rj      s%    )+ 67O.Dr   rj   c                  8    e Zd ZdZdZ eddh      ZddZd	dZy)
_TextualSerializerz:Serialize and deserialize the ONNX textual representation.onnxtxtz.onnxtxtz	.onnxtextc                j    t         j                  j                  |      }|j                  t              S r&   )rA   printerto_textr`   ra   )r   r   texts      r   r   z"_TextualSerializer.serialize_proto   s%    ||##E*{{9%%r   c                   t        j                  dd       t        |t        t        f      st        dt        |             t        |t              r|j                  t              }n|}t        |t        j                        rt        j                  j                  |      S t        |t        j                        rt        j                  j                  |      S t        |t        j                        rt        j                  j!                  |      S t        |t        j"                        rt        j                  j%                  |      S t'        dt        |             )Nz[The onnxtxt format is experimental. Please report any errors to the ONNX GitHub repository.   )
stacklevelrd   zUnsupported proto type: )warningswarnrI   rJ   r   rD   rE   re   ra   rA   
ModelProtoparserparse_model
GraphProtoparse_graphFunctionProtoparse_function	NodeProto
parse_noder1   )r   r   r   rx   s       r   r   z$_TextualSerializer.deserialize_proto   s   i	
 *ucl3MdS]N^M_`  j%($$Y/DDeT__-;;**400eT__-;;**400eT//0;;--d33eT^^,;;))$//3DK=ABBr   NrT   rh   rU   r   r   r   rs   rs      s%    D [ 9:O&Cr   rs   )
__future__r   r|   __all__rK   r   r   r   r   r	   google.protobuf.json_formatrP   google.protobuf.messagegoogle.protobuf.text_formatrA   r;   rQ   Messager
   ra   r   r$   r:   rX   rj   rs   r   r.   r   r   r   <module>r      s   
 #    ? ? "  " 	!8!8!@!@	A	Ch C*(= (=V /  FD? D*Do D.C CF ;   %' (   &( )   /# $   $& 'r   