
    
Ng-                       d dl mZ g dZd dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, d d	l-m.Z.m/Z/ d d
l0m1Z1m2Z2m3Z3 d dl4m5Z6 d dlm7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC eed         eDf         ZEdZFd9dZGd:dZHd;dZI	 d<d=dZJ	 	 d>d?d$ZK	 d<d@d&ZLeFfdAd*ZMeFfdBd+ZN	 d<d,ddd-d,d.dCd7ZO	 d<dDd8ZPeKZQeMZReOZSdS )E    )annotations)8ONNX_ML
IR_VERSIONIR_VERSION_2017_10_10IR_VERSION_2017_10_30IR_VERSION_2017_11_3IR_VERSION_2019_1_22IR_VERSION_2019_3_18IR_VERSION_2019_9_19IR_VERSION_2020_5_8IR_VERSION_2021_7_30IR_VERSION_2023_5_5EXPERIMENTALSTABLEcheckercomposedefs	gen_protohelperhubmappingnumpy_helperparserprintershape_inferenceutilsversion_converterAttributeProtoFunctionProto
GraphProtoMapProto
ModelProto	NodeProtoOperatorProtoOperatorSetIdProtoOperatorSetProtoOperatorStatusOptionalProtoSequenceProtoSparseTensorProtoStringStringEntryProtoTensorAnnotationTensorProtoTensorShapeProtoTrainingInfoProto	TypeProtoValueInfoProtoVersionconvert_model_to_external_dataload_external_data_for_modelload_model_from_string
load_modelload_tensor_from_stringload_tensor
save_modelsave_tensorwrite_external_data_tensorsN)IOLiteralUnion)serialization)r   )r4   r;   r3   )r   r   r   r    r   r   r   r   r	   r
   r   r   r   r   r"   r#   r%   r'   r   r*   r+   r,   r-   r.   r/   r0   r1   r2   )r$   r&   )r!   r(   r)   )version)r   r   r   r   r   r   r   r   r   r   r   r   r   )protobuf	textprotorA   fIO[bytes] | str | os.PathLikereturnbytesc                   t          | d          rot          t          j        t          t
                   |           j                  r8t          j        t          t
                   |                                           }nmt          j        t          t          t          j
        f         |           } t          | d          5 }|                                }d d d            n# 1 swxY w Y   |S )Nreadrb)hasattrcallabletypingcastr<   rF   rH   r>   strosPathLikeopen)rC   contentreadables      I/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/__init__.py_load_bytesrU      s    q& &hv{2e9a'@'@'EFF &+bi++0022Kc2;./33!T]] 	&hmmooG	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&Ns   C""C&)C&rR   Nonec                   t          |d          rqt          t          j        t          t
                   |          j                  r:t          j        t          t
                   |                              |            d S t          j        t          t          t          j
        f         |          }t          |d          5 }|                    |            d d d            d S # 1 swxY w Y   d S )Nwritewb)rJ   rK   rL   rM   r<   rF   rX   r>   rN   rO   rP   rQ   )rR   rC   writables      rT   _save_bytesr[      s    q' $xBuIq(A(A(GHH $BuIq!!''00000Kc2;./33!T]] 	$hNN7###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   C&&C*-C*$IO[bytes] | str | os.PathLike | None
str | Nonec                    t          | t          t          j        f          rt          j                            |           S t          | d          r(| J t          j                            | j                  S d S )Nname)
isinstancerN   rO   rP   pathabspathrJ   r_   )rC   s    rT   _get_file_pathrc      se    !c2;'(( "wq!!!q& '}}}wqv&&&4    fmt_SupportedFormat | None$str | os.PathLike | IO[bytes] | Noneserialization.ProtoSerializerc                @   | t           j                            |           S t          |          x}At          j                            |          \  }}t           j                            |          } | pt          } | J t           j                            |           S )zQGet the serializer for the given path and format from the serialization registry.)	r?   registrygetrc   rO   ra   splitextget_format_from_file_extension_DEFAULT_FORMAT)re   rC   	file_path_exts        rT   _get_serializerrr      s    
 %))#...#A&&&	3!!),,3$CCCHH 
 C???!%%c***rd   Tformatload_external_databoolr"   c                   t          ||                               t          |           t                                }|r@t	          |           }|r/t
          j                            |          }t          ||           |S )a  Loads a serialized ModelProto into memory.

    Args:
        f: can be a file-like object (has "read" function) or a string/PathLike containing a file name
        format: The serialization format. When it is not specified, it is inferred
            from the file extension when ``f`` is a path. If not specified _and_
            ``f`` is not a path, 'protobuf' is used. The encoding is assumed to
            be "utf-8" when the format is a text format.
        load_external_data: Whether to load the external data.
            Set to True if the data is under the same directory of the model.
            If not, users need to call :func:`load_external_data_for_model`
            with directory to load external data from.

    Returns:
        Loaded in-memory ModelProto.
    )	rr   deserialize_protorU   r"   rc   rO   ra   dirnamer4   )rC   rs   rt   modelmodel_filepathbase_dirs         rT   r6   r6      sr    * FA&&88QVVE :'** 	:w~66H(999Lrd   r-   c                |    t          ||                               t          |           t                                S )a  Loads a serialized TensorProto into memory.

    Args:
        f: can be a file-like object (has "read" function) or a string/PathLike containing a file name
        format: The serialization format. When it is not specified, it is inferred
            from the file extension when ``f`` is a path. If not specified _and_
            ``f`` is not a path, 'protobuf' is used. The encoding is assumed to
            be "utf-8" when the format is a text format.

    Returns:
        Loaded in-memory TensorProto.
    )rr   rw   rU   r-   )rC   rs   s     rT   r8   r8      s.      61%%77AVVVrd   sbytes | str_SupportedFormatc                `    t          |                              | t                                S )a  Loads a binary string (bytes) that contains serialized ModelProto.

    Args:
        s: a string, which contains serialized ModelProto
        format: The serialization format. When it is not specified, it is inferred
            from the file extension when ``f`` is a path. If not specified _and_
            ``f`` is not a path, 'protobuf' is used. The encoding is assumed to
            be "utf-8" when the format is a text format.

    Returns:
        Loaded in-memory ModelProto.
    )rr   rw   r"   r}   rs   s     rT   r5   r5      s&      6""44Q
EEErd   c                `    t          |                              | t                                S )a  Loads a binary string (bytes) that contains serialized TensorProto.

    Args:
        s: a string, which contains serialized TensorProto
        format: The serialization format. When it is not specified, it is inferred
            from the file extension when ``f`` is a path. If not specified _and_
            ``f`` is not a path, 'protobuf' is used. The encoding is assumed to
            be "utf-8" when the format is a text format.

    Returns:
        Loaded in-memory TensorProto.
    )rr   rw   r-   r   s     rT   r7   r7     s&      6""44QFFFrd   Fi   )save_as_external_dataall_tensors_to_one_filelocationsize_thresholdconvert_attributeprotoModelProto | bytesr   r   r   r   intr   c                  t          | t                    r4t          t                                        | t                                } |rt          | ||||           t          |          }|/t          j	        
                    |          }	t          | |	          } t          ||                              |           }
t          |
|           dS )a}  Saves the ModelProto to the specified path and optionally, serialize tensors with raw data as external data before saving.

    Args:
        proto: should be a in-memory ModelProto
        f: can be a file-like object (has "write" function) or a string containing
        a file name or a pathlike object
        format: The serialization format. When it is not specified, it is inferred
            from the file extension when ``f`` is a path. If not specified _and_
            ``f`` is not a path, 'protobuf' is used. The encoding is assumed to
            be "utf-8" when the format is a text format.
        save_as_external_data: If true, save tensors to external file(s).
        all_tensors_to_one_file: Effective only if save_as_external_data is True.
            If true, save all tensors to one external file specified by location.
            If false, save each tensor to a file named with the tensor name.
        location: Effective only if save_as_external_data is true.
            Specify the external file that all tensors to save to.
            Path is relative to the model path.
            If not specified, will use the model name.
        size_threshold: Effective only if save_as_external_data is True.
            Threshold for size of data. Only when tensor's data is >= the size_threshold it will be converted
            to external data. To convert every tensor with raw data to external data set size_threshold=0.
        convert_attribute: Effective only if save_as_external_data is True.
            If true, convert all tensors to external data
            If false, convert only non-attribute tensors to external data
    N)r`   rF   rr   rn   rw   r"   r3   rc   rO   ra   rx   r;   serialize_protor[   )r   rC   rs   r   r   r   r   r   rz   basepath
serializeds              rT   r9   r9     s    H % X00BB5*,,WW 
&*HnFW	
 	
 	
 $A&&N!7??>22+E8<< 88HHOOJ
Ard   c                l    t          ||                              |           }t          ||           dS )a  Saves the TensorProto to the specified path.

    Args:
        proto: should be a in-memory TensorProto
        f: can be a file-like object (has "write" function) or a string
        containing a file name or a pathlike object.
        format: The serialization format. When it is not specified, it is inferred
            from the file extension when ``f`` is a path. If not specified _and_
            ``f`` is not a path, 'protobuf' is used. The encoding is assumed to
            be "utf-8" when the format is a text format.
    N)rr   r   r[   )r   rC   rs   r   s       rT   r:   r:   K  s7      !++;;EBBJ
Ard   )rC   rD   rE   rF   )rR   rF   rC   rD   rE   rV   )rC   r\   rE   r]   )N)re   rf   rC   rg   rE   rh   )NT)rC   rD   rs   rf   rt   ru   rE   r"   )rC   rD   rs   rf   rE   r-   )r}   r~   rs   r   rE   r"   )r}   rF   rs   r   rE   r-   )r   r   rC   rD   rs   rf   r   ru   r   ru   r   r]   r   r   r   ru   rE   rV   )r   r-   rC   rD   rs   rf   rE   rV   )T
__future__r   __all__rO   rL   r<   r=   r>   onnxr?   onnx.onnx_cpp2py_exportr   onnx.external_data_helperr4   r;   r3   onnx.onnx_pbr   r   r   r    r   r   r   r   r	   r
   r   r   r   r   r"   r#   r%   r'   r   r*   r+   r,   r-   r.   r/   r0   r1   r2   onnx.onnx_operators_pbr$   r&   onnx.onnx_data_pbr!   r(   r)   onnx.versionr@   __version__r   r   r   r   r   r   r   r   r   r   r   r   r   rN   r   rn   rU   r[   rc   rr   r6   r8   r5   r7   r9   r:   loadload_from_stringsave rd   rT   <module>r      s4  
 # " " " " "= = =@ 
			  % % % % % % % % % %       + + + + + +         
                                                           < C B B B B B B B D D D D D D D D D D / / / / / /                             & !893>?    $ $ $ $    MQ+ + + + +* '+#    D '+W W W W W*  /F F F F F*  /G G G G G, '+2
 #($(#2 2 2 2 2 2p '+    * ) rd   