U
    qh-                  8   @  s  d dl mZ dddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9g8Z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 dAl0m1Z1m2Z2m3Z3 d dBl4m5Z6 d dClm7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC eedD eDf ZEdEZFdFdGdHdIdJZGdGdFdKdLdMdNZHdOdPdHdQdRZIdmdSdTdUdVdWdXZJdndFdSdZd d[d\d4ZKdodFdSd+d]d^d6ZLeFfd_d`d dadbd3ZMeFfdGd`d+dadcd5ZNdpdddYd:dedddfdgdFdSdZdZdPdhdZdKdi	djd7ZOdqd+dFdSdKdkdld8ZPeKZQeMZReOZSd:S )r    )annotations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   )r3   r:   r2   )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.   r/   r0   r1   )r#   r%   )r    r'   r(   )version)r   r   r   r   r   r   r   r   r   r   r   r   r   )protobufZ	textprotoZonnxtxtjsonr@   zIO[bytes] | str | os.PathLikebytes)freturnc              	   C  sn   t | dr6tttt | jr6ttt |  }n4tttt	j
f | } t| d}| }W 5 Q R X |S )Nreadrb)hasattrcallabletypingcastr;   rB   rE   r=   strosPathLikeopen)rC   contentreadable rQ   1/tmp/pip-unpacked-wheel-xnis5xre/onnx/__init__.py_load_bytes   s     rS   None)rO   rC   rD   c              	   C  sr   t |dr8tttt |jr8ttt ||  n6tttt	j
f |}t|d}||  W 5 Q R X d S )Nwritewb)rG   rH   rI   rJ   r;   rB   rU   r=   rK   rL   rM   rN   )rO   rC   writablerQ   rQ   rR   _save_bytes   s
     rX   z$IO[bytes] | str | os.PathLike | Nonez
str | Nonec                 C  sD   t | ttjfrtj| S t| dr@| d k	s2ttj| jS d S )Nname)	
isinstancerK   rL   rM   pathabspathrG   AssertionErrorrY   )rC   rQ   rQ   rR   _get_file_path   s    
r^   z_SupportedFormat | Nonez$str | os.PathLike | IO[bytes] | Nonezserialization.ProtoSerializer)fmtrC   rD   c                 C  s`   | dk	rt j| S t| }dk	r@tj|\}}t j|} | pFt} | dk	sTt	t j| S )zQGet the serializer for the given path and format from the serialization registry.N)
r>   registrygetr^   rL   r[   splitextZget_format_from_file_extension_DEFAULT_FORMATr]   )r_   rC   	file_path_extrQ   rQ   rR   _get_serializer   s    rg   Tbool)rC   formatload_external_datarD   c                 C  sB   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.
    )	rg   deserialize_protorS   r!   r^   rL   r[   dirnamer3   )rC   ri   rj   modelmodel_filepathbase_dirrQ   rQ   rR   r5      s    
)rC   ri   rD   c                 C  s   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.
    )rg   rk   rS   r,   )rC   ri   rQ   rQ   rR   r7      s    zbytes | str_SupportedFormat)sri   rD   c                 C  s   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.
    )rg   rk   r!   rq   ri   rQ   rQ   rR   r4      s    c                 C  s   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.
    )rg   rk   r,   rr   rQ   rQ   rR   r6     s    Fi   )save_as_external_dataall_tensors_to_one_filelocationsize_thresholdconvert_attributezModelProto | bytesint)	protorC   ri   rs   rt   ru   rv   rw   rD   c                C  st   t | trtt| t } |r0t| |||| t|}|dk	rVt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)rZ   rB   rg   rc   rk   r!   r2   r^   rL   r[   rl   r:   serialize_protorX   )ry   rC   ri   rs   rt   ru   rv   rw   rn   Zbasepath
serializedrQ   rQ   rR   r8     s     $
    
)ry   rC   ri   rD   c                 C  s   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)rg   rz   rX   )ry   rC   ri   r{   rQ   rQ   rR   r9   M  s    )N)NT)N)N)N)T
__future__r   __all__rL   rI   r;   r<   r=   Zonnxr>   Zonnx.onnx_cpp2py_exportr   Zonnx.external_data_helperr3   r:   r2   Z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.   r/   r0   r1   Zonnx.onnx_operators_pbr#   r%   Zonnx.onnx_data_pbr    r'   r(   Zonnx.versionr?   __version__r   r   r   r   r   r   r   r   r   r   r   r   r   rK   rp   rc   rS   rX   r^   rg   r5   r7   r4   r6   r8   r9   loadZload_from_stringsaverQ   rQ   rQ   rR   <module>   s   @x<

	
   "  "8 