o
    soio;                     @  s\  d dl mZ g dZd dlZd dlZd dl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.m/Z/m0Z0m1Z1m2Z2 d d	l3m4Z4m5Z5 d d
l6m7Z7m8Z8m9Z9 d dl:Zd dlm;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF ejGrd dlHmIZI ejJjJZKed eLB ZMdZNdQddZOdRddZPdSddZQ	dTdUd!d"ZR		#dVdWd(d)ZS	dTdXd+d,ZTeNfdYd0d1ZUeNfdZd2d3ZV	dTd4d#dd5d4d6d[d?d@ZW	dTd\dAdBZXeSZYeUZZeWZ[d]dEdFZ\d^dHdIZ]d_dKdLZ^d`dOdPZ_e\e!_`e]e_`e^e_`dS )a    )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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)serialization)r   )r9   r@   r8   )#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.   r/   r0   r1   r2   r3   r4   r5   r6   r7   )r&   r(   )r"   r*   r+   )r   r   r   r   r   r   r   r   r   r   r   r   )Sequence)protobuf	textprotoonnxtxtjsonrE   fIO[bytes] | str | os.PathLikereturnbytesc                 C  st   t | drttd| jrtd|  }|S td| } t| d}| }W d    |S 1 s3w   Y  |S )Nread	IO[bytes]str | os.PathLikerb)hasattrcallabletypingcastrM   open)rI   contentreadable rX   A/home/ubuntu/.local/lib/python3.10/site-packages/onnx/__init__.py_load_bytes   s   

rZ   rV   Nonec                 C  sx   t |drttd|jrtd||  d S td|}t|d}||  W d    d S 1 s5w   Y  d S )NwriterN   rO   wb)rQ   rR   rS   rT   r\   rU   )rV   rI   writablerX   rX   rY   _save_bytes   s   "r_   $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)
isinstancestrosPathLikepathabspathrQ   rb   )rI   rX   rX   rY   _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)	rC   registrygetri   re   rg   splitextget_format_from_file_extension_DEFAULT_FORMAT)rj   rI   	file_path_extrX   rX   rY   _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_protorZ   r#   ri   re   rg   dirnamer9   )rI   rw   rx   modelmodel_filepathbase_dirrX   rX   rY   r;      s   
r;   r2   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   rZ   r2   )rI   rw   rX   rX   rY   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   rX   rX   rY   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   r2   r   rX   rX   rY   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)rc   rL   rv   rr   rz   r#   r8   ri   re   rg   r{   r@   serialize_protor_   )r   rI   rw   r   r   r   r   r   r}   basepath
serializedrX   rX   rY   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   r_   )r   rI   rw   r   rX   rX   rY   r?   \  s   r?   selfrd   c              	   C  s   | j rd| j  d}nd}| jrd| j d}nd}| jr%d| j d}nd}| jr1d| j}nd}| jr@dt| j d}nd}| jrNd	t| j }nd}d
| j | | | | | | d	S )N
, domain='' z, producer_name='z, producer_version='z, graph=z, functions=<z functions>, opset_import=zModelProto(ir_version=))	domainproducer_nameproducer_versiongraph	functionslenopset_import_operator_set_protos_repr
ir_version)r   r   r   r   r   r   r   rX   rX   rY   _model_proto_reprv  s&   &r   r    c              	   C  s   | j rdt| j  d}nd}| jrdt| j d}nd}| jr+dt| j d}nd}| jr:dt| j d	}nd}| jrId
t| j d}nd}d| j d| | | | | d	S )Nz, initializer=<z initializers>r   , node=< nodes>z, value_info=<z value_info>	, input=< inputs>
, output=<	 outputs>zGraphProto('r   r   )initializerr   node
value_infoinputoutputrb   )r   r   r   r   r   r   rX   rX   rY   _graph_proto_repr  s    $r   r   c                 C  s   | j rd| j  d}nd}| jrd| j d}nd}| jr'dt| j d}nd}| jr3d| j }nd}| jrAdt| j }nd}| jrPd	t| j d
}nd}| jr_dt| j d}nd}d| j	 d| | | | | | | dS )Nr   r   r   z, overload='r   r   z, attribute=r   r   r   r   r   zFunctionProto('r   )
r   overloadr   r   	attributer   r   r   r   rb   )r   r   r   r   r   r   r   r   rX   rX   rY   _function_proto_repr  s,   ,r   protosSequence[OperatorSetIdProto]c                 C  s   dd | D }t |S )Nc                 S  s   i | ]}|j |jqS rX   )r   version).0r   rX   rX   rY   
<dictcomp>  s    z-_operator_set_protos_repr.<locals>.<dictcomp>)repr)r   opset_importsrX   rX   rY   r     s   r   )rI   rJ   rK   rL   )rV   rL   rI   rJ   rK   r[   )rI   r`   rK   ra   )N)rj   rk   rI   rl   rK   rm   )NT)rI   rJ   rw   rk   rx   ry   rK   r#   )rI   rJ   rw   rk   rK   r2   )r   r   rw   r   rK   r#   )r   rL   rw   r   rK   r2   )r   r   rI   rJ   rw   rk   r   ry   r   ry   r   ra   r   r   r   ry   rK   r[   )r   r2   rI   rJ   rw   rk   rK   r[   )r   r#   rK   rd   )r   r    rK   rd   )r   r   rK   rd   )r   r   rK   rd   )a
__future__r   __all__re   rS   rA   rB   onnxrC   onnx.onnx_cpp2py_exportr   onnx.external_data_helperr9   r@   r8   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.   r/   r0   r1   r2   r3   r4   r5   r6   r7   onnx.onnx_operators_pbr&   r(   onnx.onnx_data_pbr"   r*   r+   onnx.versionr   r   r   r   r   r   r   r   r   r   r   r   TYPE_CHECKINGcollections.abcrD   r   __version__rd   r   rr   rZ   r_   ri   rv   r;   r=   r:   r<   r>   r?   loadload_from_stringsaver   r   r   r   __repr__rX   rX   rX   rY   <module>   sh   F%8



	
"8



 
