o
    wif/                     @  s  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,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 d d	l4m5Z5m6Z6 d d
l7m8Z8m9Z9m:Z: d dl;m<Z= d dlm>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJ eed eKf ZLdZMdCddZNdDddZOdEddZP	dFdGd!d"ZQ		#dHdId(d)ZR	dFdJd+d,ZSeMfdKd0d1ZTeMfdLd2d3ZU	dFd4d#dd5d4d6dMd?d@ZV	dFdNdAdBZWeRZXeTZYeVZZdS )O    )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IR_VERSION_2024_3_25EXPERIMENTALSTABLEcheckercomposedefs	gen_protohelperhubmappingnumpy_helperparserprintershape_inferenceutilsversion_converterAttributeProtoDeviceConfigurationProtoFunctionProto
GraphProtoIntIntListEntryProtoMapProto
ModelProtoNodeDeviceConfigurationProto	NodeProtoOperatorProtoOperatorSetIdProtoOperatorSetProtoOperatorStatusOptionalProtoSequenceProtoSimpleShardedDimProtoShardedDimProtoShardingSpecProto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   )r:   rA   r9   )#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   r2   r3   r4   r5   r6   r7   r8   )r'   r)   )r#   r+   r,   )version)r   r   r   r   r   r   r   r   r   r   r   r   r   )protobuf	textprotoonnxtxtjsonrG   fIO[bytes] | str | os.PathLikereturnbytesc                 C  s   t | drtttt | jrttt |  }|S tttt	j
f | } t| d}| }W d    |S 1 s<w   Y  |S )Nreadrb)hasattrcallabletypingcastrB   rN   rO   rD   strosPathLikeopen)rK   contentreadable r[   J/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/__init__.py_load_bytes   s    

r]   rY   Nonec                 C  s   t |drtttt |jrttt ||  d S tttt	j
f |}t|d}||  W d    d S 1 s>w   Y  d S )Nwritewb)rQ   rR   rS   rT   rB   rN   r_   rD   rU   rV   rW   rX   )rY   rK   writabler[   r[   r\   _save_bytes   s    "rb   $IO[bytes] | str | os.PathLike | None
str | Nonec                 C  sD   t | ttjfrtj| S t| dr | d usJ tj| jS d S )Nname)
isinstancerU   rV   rW   pathabspathrQ   re   )rK   r[   r[   r\   _get_file_path   s   
ri   fmt_SupportedFormat | None$str | os.PathLike | IO[bytes] | Noneserialization.ProtoSerializerc                 C  s`   | dur
t j| S t| }dur tj|\}}t j|} | p#t} | dus*J t j| S )zQGet the serializer for the given path and format from the serialization registry.N)	rE   registrygetri   rV   rg   splitextget_format_from_file_extension_DEFAULT_FORMAT)rj   rK   	file_path_extr[   r[   r\   _get_serializer   s   rv   Tformatload_external_databoolr$   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.
    )	rv   deserialize_protor]   r$   ri   rV   rg   dirnamer:   )rK   rw   rx   modelmodel_filepathbase_dirr[   r[   r\   r<      s   
r<   r3   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.
    )rv   rz   r]   r3   )rK   rw   r[   r[   r\   r>      s   r>   sbytes | str_SupportedFormatc                 C     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.
    )rv   rz   r$   r   rw   r[   r[   r\   r;         r;   c                 C  r   )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.
    )rv   rz   r3   r   r[   r[   r\   r=     r   r=   Fi   )save_as_external_dataall_tensors_to_one_filelocationsize_thresholdconvert_attributeprotoModelProto | bytesr   r   r   r   intr   c                C  st   t | trtt| t } |rt| |||| t|}|dur+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)rf   rN   rv   rr   rz   r$   r9   ri   rV   rg   r{   rA   serialize_protorb   )r   rK   rw   r   r   r   r   r   r}   basepath
serializedr[   r[   r\   r?   &  s   
$

r?   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)rv   r   rb   )r   rK   rw   r   r[   r[   r\   r@   [  s   r@   )rK   rL   rM   rN   )rY   rN   rK   rL   rM   r^   )rK   rc   rM   rd   )N)rj   rk   rK   rl   rM   rm   )NT)rK   rL   rw   rk   rx   ry   rM   r$   )rK   rL   rw   rk   rM   r3   )r   r   rw   r   rM   r$   )r   rN   rw   r   rM   r3   )r   r   rK   rL   rw   rk   r   ry   r   ry   r   rd   r   r   r   ry   rM   r^   )r   r3   rK   rL   rw   rk   rM   r^   )[
__future__r   __all__rV   rS   rB   rC   rD   onnxrE   onnx.onnx_cpp2py_exportr   onnx.external_data_helperr:   rA   r9   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   r2   r3   r4   r5   r6   r7   r8   onnx.onnx_operators_pbr'   r)   onnx.onnx_data_pbr#   r+   r,   onnx.versionrF   __version__r   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   rr   r]   rb   ri   rv   r<   r>   r;   r=   r?   r@   loadload_from_stringsaver[   r[   r[   r\   <module>   sX   G%<




	
"8