o
    q::jE<                     @  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@mAZAmBZBmCZCmDZDmEZEmFZF ejGrd dlHmIZI ze;jJKdZLW n! e;jJjMy   ze;jJKdZLW n e;jJjMy   dZLY nw Y nw ed eNB ZOdZPdTddZQdUddZRdVddZS	dWdXd$d%ZT		&dYdZd+d,ZU	dWd[d.d/ZVePfd\d3d4ZWePfd]d5d6ZX	dWd7d&dd8d7d9d^dBdCZY	dWd_dDdEZZeUZ[eWZ\eYZ]d`dHdIZ^dadKdLZ_dbdNdOZ`dcdRdSZae^e!_be_e_be`e_bdS )d    )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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   )r8   r?   r7   )#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.   r/   r0   r1   r2   r3   r4   r5   r6   )r%   r'   )r!   r)   r*   )r   r   r   r   r   r   r   r   r   r   r   )Sequenceonnxzonnx-weeklyunknown)protobuf	textprotoonnxtxtjsonrF   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castrN   open)rJ   contentreadable rY   T/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnx/__init__.py_load_bytes   s   

r[   rW   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writerO   rP   wb)rR   rS   rT   rU   r]   rV   )rW   rJ   writablerY   rY   rZ   _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abspathrR   rc   )rJ   rY   rY   rZ   _get_file_path   s   
rj   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)	rB   registrygetrj   rf   rh   splitextget_format_from_file_extension_DEFAULT_FORMAT)rk   rJ   	file_path_extrY   rY   rZ   _get_serializer   s   rw   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.
    )	rw   deserialize_protor[   r"   rj   rf   rh   dirnamer8   )rJ   rx   ry   modelmodel_filepathbase_dirrY   rY   rZ   r:      s   
r:   r1   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.
    )rw   r{   r[   r1   )rJ   rx   rY   rY   rZ   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.
    )rw   r{   r"   r   rx   rY   rY   rZ   r9        r9   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.
    )rw   r{   r1   r   rY   rY   rZ   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)rd   rM   rw   rs   r{   r"   r7   rj   rf   rh   r|   r?   serialize_protor`   )r   rJ   rx   r   r   r   r   r   r~   basepath
serializedrY   rY   rZ   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)rw   r   r`   )r   rJ   rx   r   rY   rY   rZ   r>   `  s   r>   selfre   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   rY   rY   rZ   _model_proto_reprz  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outputrc   )r   r   r   r   r   r   rY   rY   rZ   _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   rc   )r   r   r   r   r   r   r   r   rY   rY   rZ   _function_proto_repr  s,   ,r   protosSequence[OperatorSetIdProto]c                 C  s   dd | D }t |S )Nc                 S  s   i | ]}|j |jqS rY   )r   version).0r   rY   rY   rZ   
<dictcomp>  s    z-_operator_set_protos_repr.<locals>.<dictcomp>)repr)r   opset_importsrY   rY   rZ   r     s   r   )rJ   rK   rL   rM   )rW   rM   rJ   rK   rL   r\   )rJ   ra   rL   rb   )N)rk   rl   rJ   rm   rL   rn   )NT)rJ   rK   rx   rl   ry   rz   rL   r"   )rJ   rK   rx   rl   rL   r1   )r   r   rx   r   rL   r"   )r   rM   rx   r   rL   r1   )r   r   rJ   rK   rx   rl   r   rz   r   rz   r   rb   r   r   r   rz   rL   r\   )r   r1   rJ   rK   rx   rl   rL   r\   )r   r"   rL   re   )r   r   rL   re   )r   r   rL   re   )r   r   rL   re   )c
__future__r   __all__rf   rT   r@   rA   rD   rB   onnx.onnx_cpp2py_exportr   onnx.external_data_helperr8   r?   r7   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.   r/   r0   r1   r2   r3   r4   r5   r6   onnx.onnx_operators_pbr%   r'   onnx.onnx_data_pbr!   r)   r*   importlib.metadata	importlibr   r   r   r   r   r   r   r   r   r   r   TYPE_CHECKINGcollections.abcrC   metadatar   __version__PackageNotFoundErrorre   r   rs   r[   r`   rj   rw   r:   r<   r9   r;   r=   r>   loadload_from_stringsaver   r   r   r   __repr__rY   rY   rY   rZ   <module>   sz   E%4	



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