
    
Ng                    B   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                     e                       e                     e                       e                     e                       e                     e                       dS )    )annotationsNregistry)Any
CollectionOptionalProtocolTypeVar_Proto)boundzutf-8c                  8    e Zd ZU dZded<   ded<   dd
Z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                    dS )z6Serialize a in-memory proto to a serialized data type.N )selfr   s     N/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/serialization.pyserialize_protozProtoSerializer.serialize_proto)             
serializedc                    dS )z4Parse a serialized data type into a in-memory proto.Nr   r   r   r   s      r   deserialize_protoz!ProtoSerializer.deserialize_proto,   r   r   N)r   r
   r   r   )r   r   r   r
   r   r
   )__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   r      sj         [[  %$$$E E E EC C C C C Cr   r   c                  .    e Zd ZddZddZdd	ZddZdS )	_Registryr   Nonec                "    i | _         i | _        d S N)_serializers_extension_to_format)r   s    r   __init__z_Registry.__init__1   s    8:46!!!r   
serializerr   c                |    | j         j        <   | j                            fdj        D                        d S )Nc                     i | ]
}|j         S r   )r   ).0extr+   s     r   
<dictcomp>z&_Registry.register.<locals>.<dictcomp>9   s    TTT#S*-TTTr   )r(   r   r)   updater   )r   r+   s    `r   registerz_Registry.register6   sR    9C*56!((TTTT9STTT	
 	
 	
 	
 	
r   fmtr   c                    	 | j         |         S # t          $ r. t          d| d| 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   r3   s     r   getz_Registry.get<   sg    	$S)) 	 	 	aaatGXG]G]G_G_aa 	s	    8Afile_extension
str | Nonec                6    | 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)   r8   )r   r9   s     r   get_format_from_file_extensionz(_Registry.get_format_from_file_extensionO   s     (,,^<<<r   N)r   r%   )r+   r   r   r%   )r3   r   r   r   )r9   r   r   r:   )r   r   r    r*   r2   r8   r<   r   r   r   r$   r$   0   sd        7 7 7 7

 
 
 
   &	= 	= 	= 	= 	= 	=r   r$   c                  @    e Zd ZdZdZ eddh          Zdd	ZddZdS )_ProtobufSerializerz+Serialize and deserialize protobuf message.protobufz.onnxz.pbr   r
   r   bytesc                L   t          |d          rvt          |j                  rb	 |                                }nJ# t          $ r=}|                                t
          j        j        k    rt          d          | d }~ww xY w|S 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callablerB   r6   ByteSizeonnxcheckerMAXIMUM_PROTOBUF	TypeErrortype)r   r   resultes       r   r   z#_ProtobufSerializer.serialize_protoa   s    5-.. 
	8E<S3T3T 
	0022   >>##t|'DDD$k    MNeNN
 
 	
s   ; 
B8A==Br   c                v   t          |t                    st          dt          |                     t	          j        t          t                   |                    |                    }|J|t          |          k    r7t          j        j                            d| dt          |                     |S )Nz4Parameter 'serialized' must be bytes, but got type: z*Protobuf decoding consumed too few bytes: z out of )
isinstancer@   rI   rJ   typingcastr   intParseFromStringlengoogler?   messageDecodeError)r   r   r   decodeds       r   r   z%_ProtobufSerializer.deserialize_protoq   s    *e,, 	YtJGWGWYY   +hsmU-B-B:-N-NOO7c*oo#=#=/)55_W__cR\oo__   r   Nr   r
   r   r@   )r   r@   r   r
   r   r
   	r   r   r    r!   r   	frozensetr   r   r   r   r   r   r>   r>   [   s\        55!i% 011O
 
 
 
 
 
 
 
 
 
r   r>   c                  @    e Zd ZdZdZ eh d          ZddZddZdS )_TextProtoSerializerz%Serialize and deserialize text proto.	textproto>   .pbtxt	.prototxt
.textprotor   r
   r   r@   c                ~    t           j        j                            |          }|                    t
                    S r'   )rT   r?   text_formatMessageToStringencode	_ENCODING)r   r   r]   s      r   r   z$_TextProtoSerializer.serialize_proto   s.    O/??FF		***r   r   bytes | strc                N   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: )rN   r@   r   rI   rJ   decodere   rT   r?   rb   Parser   s      r   r   z&_TextProtoSerializer.deserialize_proto       *ucl33 	`dS]N^N^``   j%(( 	6#**955J*c******00UCCCr   NrX   r   rf   r   r
   r   r
   rY   r   r   r   r\   r\   ~   sd        //"i E E EFFO+ + + +D D D D D Dr   r\   c                  @    e Zd ZdZdZ eddh          Zdd	ZddZdS )_JsonSerializerzSerialize and deserialize JSON.jsonz.jsonz	.onnxjsonr   r
   r   r@   c                    t           j        j                            |d          }|                    t
                    S )NT)preserving_proto_field_name)rT   r?   json_formatMessageToJsonrd   re   )r   r   json_messages      r   r   z_JsonSerializer.serialize_proto   s=    2@@t A 
 
 ""9---r   r   rf   c                N   t          |t          t          f          st          dt	          |                     t          |t                    r|                    t                    }t          |t                    sJ t          j        j	        
                    ||          S rh   )rN   r@   r   rI   rJ   rj   re   rT   r?   rs   rk   r   s      r   r   z!_JsonSerializer.deserialize_proto   rl   r   NrX   rm   rY   r   r   r   ro   ro      sb        ))i+ 677O. . . .D D D D D Dr   ro   c                  >    e Zd ZdZdZ edh          ZddZddZdS )_TextualSerializerz:Serialize and deserialize the ONNX textual representation.onnxtxtz.onnxtxtr   r
   r   r@   c                t    t           j                            |          }|                    t                    S r'   )rF   printerto_textrd   re   )r   r   texts      r   r   z"_TextualSerializer.serialize_proto   s)    |##E**{{9%%%r   r   rf   c                   t          j        dd           t          |t          t          f          st          dt          |                     t          |t                    r|                    t                    }n|}t          |t          j
                  rt          j                            |          S t          |t          j                  rt          j                            |          S t          |t          j                  rt          j                            |          S t          |t          j                  rt          j                            |          S t'          dt          |                     )Nz[The onnxtxt format is experimental. Please report any errors to the ONNX GitHub repository.   )
stacklevelri   zUnsupported proto type: )warningswarnrN   r@   r   rI   rJ   rj   re   rF   
ModelProtoparserparse_model
GraphProtoparse_graphFunctionProtoparse_function	NodeProto
parse_noder6   )r   r   r   r}   s       r   r   z$_TextualSerializer.deserialize_proto   sS   i	
 	
 	
 	
 *ucl33 	`dS]N^N^``   j%(( 	$$Y//DDDeT_-- 	1;**4000eT_-- 	1;**4000eT/00 	4;--d333eT^,, 	0;))$///ADKKAABBBr   NrX   rm   rY   r   r   r   rx   rx      s_        DD i--O& & & &C C C C C Cr   rx   )
__future__r   r   __all__rO   r   r   r   r   r	   google.protobuf.json_formatrT   google.protobuf.messagegoogle.protobuf.text_formatrF   r?   rU   Messager
   re   r   r$   r>   r\   ro   rx   r   r2   r   r   r   <module>r      sW  
 # " " " " "    ? ? ? ? ? ? ? ? ? ? ? ? ? ? " " " "     " " " " 	!8!@	A	A	A	C C C C Ch C C C*(= (= (= (= (= (= (= (=V         /      FD D D D D? D D D*D D D D Do D D D.C C C C C C C CF 9;;   %%'' ( ( (   &&(( ) ) )   //## $ $ $   $$&& ' ' ' ' 'r   