
    /j[;                    x   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@mAZAmBZBmCZCmDZDmEZE ejF        rd dlGmHZH ejI        jI        ZJed         eKz  ZLdZMdCdZNdDdZOdEdZP	 dFdGdZQ	 	 dHdId$ZR	 dFdJd&ZSeMfdKd*ZTeMfdLd+ZU	 dFd,ddd-d,d.dMd7ZV	 dFdNd8ZWeRZXeTZYeVZZdOd;Z[dPd=Z\dQd?Z]dRdBZ^e[e!__        e\e__        e]e__        dS )S    )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   )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   )Sequence)protobuf	textprotoonnxtxtjsonrE   fIO[bytes] | str | os.PathLikereturnbytesc                f   t          | d          rOt          t          j        d|           j                  r(t          j        d|                                           }nQt          j        d|           } t          | d          5 }|                                }d d d            n# 1 swxY w Y   |S )Nread	IO[bytes]str | os.PathLikerb)hasattrcallabletypingcastrN   open)rI   contentreadables      R/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/onnx/__init__.py_load_bytesrZ      s    q& &hv{;'B'B'GHH &+k1--2244K+Q//!T]] 	&hmmooG	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&Ns   B&&B*-B*rW   Nonec                n   t          |d          rQt          t          j        d|          j                  r*t          j        d|                              |            d S t          j        d|          }t          |d          5 }|                    |            d d d            d S # 1 swxY w Y   d S )NwriterO   rP   wb)rR   rS   rT   rU   r]   rV   )rW   rI   writables      rY   _save_bytesr`      s    q' $xK(C(C(IJJ $K##))'22222K+Q//!T]] 	$hNN7###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   B**B.1B.$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)
isinstancestrosPathLikepathabspathrR   rd   )rI   s    rY   _get_file_pathrk      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.)	rC   registrygetrk   rg   ri   splitextget_format_from_file_extension_DEFAULT_FORMAT)rm   rI   	file_path_exts        rY   _get_serializerrz      s    
 %))#...#A&&&	3!!),,3$CCCHH 
 C???!%%c***rl   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.
    )	rz   deserialize_protorZ   r#   rk   rg   ri   dirnamer9   )rI   r{   r|   modelmodel_filepathbase_dirs         rY   r;   r;      sr    * FA&&88QVVE :'** 	:w~66H(999Lrl   r2   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.
    )rz   r   rZ   r2   )rI   r{   s     rY   r=   r=      s.      61%%77AVVVrl   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.
    )rz   r   r#   r   r{   s     rY   r:   r:      s&      6""44Q
EEErl   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.
    )rz   r   r2   r   s     rY   r<   r<     s&      6""44QFFFrl   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)re   rL   rz   rv   r   r#   r8   rk   rg   ri   r   r@   serialize_protor`   )r   rI   r{   r   r   r   r   r   r   basepath
serializeds              rY   r>   r>   %  s    H % X00BB5*,,WW 
&*HnFW	
 	
 	
 $A&&N!7??>22+E8<< 88HHOOJ
Arl   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)rz   r   r`   )r   rI   r{   r   s       rY   r?   r?   Z  s7      !++;;EBBJ
Arl   selfrf   c           	     \   | j         rd| j          d}nd}| j        rd| j         d}nd}| j        rd| j         d}nd}| j        rd| j        }nd}| j        rdt          | j                   d}nd}| j        rd	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   s          rY   _model_proto_reprr   t  s   { ,dk,,, AD,>AAA J$2GJJJz )4:))~ DC$7$7DDD			 W)B4CT)U)UWWDO\6=Zjlqs|rl   r    c           	        | 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        rd
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outputrd   )r   r   r   r   r   r   s         rY   _graph_proto_reprr     s    MD,<(=(=MMMy 1#di..111 Hc$/&:&:HHH


z 5C
OO555{ 9c$+..999U$)UUeUVU[U$U
UUUUrl   r   c                   | j         rd| j          d}nd}| j        rd| j         d}nd}| j        rdt          | j                   d}nd}| j        rd| j         }nd}| j        rdt          | j                   }nd}| j        rd	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   rd   )r   r   r   r   r   r   r   r   s           rY   _function_proto_reprr     sL   { ,dk,,,} 2$-222y 1#di..111~ 34>33			 W)B4CT)U)UWWz 5C
OO555{ 9c$+..999jTYjjjj<jjPVjXajcgjjjjrl   protosSequence[OperatorSetIdProto]c                8    d | D             }t          |          S )Nc                (    i | ]}|j         |j        S  )r   version).0r   s     rY   
<dictcomp>z-_operator_set_protos_repr.<locals>.<dictcomp>  s    EEEUU\5=EEErl   )repr)r   opset_importss     rY   r   r     s#    EEfEEEMrl   )rI   rJ   rK   rL   )rW   rL   rI   rJ   rK   r[   )rI   ra   rK   rb   )N)rm   rn   rI   ro   rK   rp   )NT)rI   rJ   r{   rn   r|   r}   rK   r#   )rI   rJ   r{   rn   rK   r2   )r   r   r{   r   rK   r#   )r   rL   r{   r   rK   r2   )r   r   rI   rJ   r{   rn   r   r}   r   r}   r   rb   r   r   r   r}   rK   r[   )r   r2   rI   rJ   r{   rn   rK   r[   )r   r#   rK   rf   )r   r    rK   rf   )r   r   rK   rf   )r   r   rK   rf   )`
__future__r   __all__rg   rT   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   TYPE_CHECKINGcollections.abcrD   r   __version__rf   r   rv   rZ   r`   rk   rz   r;   r=   r:   r<   r>   r?   loadload_from_stringsaver   r   r   r   __repr__r   rl   rY   <module>r      s  
 # " " " " "B B BJ 
			                + + + + + +         
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $J C B B B B B B B D D D D D D D D D D                              
 )((((((l"
 EFL    $ $ $ $    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 '+    * ) @ @ @ @8V V V V0k k k k@    (
 '
 -   rl   