o
    e                     @   sP  d dl Z ddlmZ d dlmZmZmZ ddlT ddlT ddl	T ddl
m
Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZmZmZmZmZmZ deee ef d	efd
dZdedeee ef d	dfddZdeee ef d	ee fddZdeeej j!j"f d	efddZ#edej j!j"dZ$dede$d	e$fddZ%d/deee ef dee de&d	e'fddZ(d0deee ef dee d	e)fddZ*d0dedee d	e'fd d!Z+d0dedee d	e)fd"d#Z,d1dee'ef deee ef dee d&e&d'e&d(ee d)e-d*e&d	dfd+d,Z.de)deee ef d	dfd-d.Z/e(Z0e+Z1e.Z2dS )2    N   )ONNX_ML)load_external_data_for_modelwrite_external_data_tensorsconvert_model_to_external_data)*)version)UnionIOOptionalcastTypeVarAnyfreturnc                 C   sr   t | drtttt | jrttt |  }|S ttt| d}| }W d    |S 1 s2w   Y  |S )Nreadrb)hasattrcallabler   r
   bytesr   openstr)r   sreadable r   =D:\Projects\ConvertPro\env\Lib\site-packages\onnx/__init__.py_load_bytes   s   

r   contentc                 C   sv   t |drtttt |jrttt ||  d S ttt|d}||  W d    d S 1 s4w   Y  d S )Nwritewb)r   r   r   r
   r   r   r   r   )r   r   writabler   r   r   _save_bytes$   s
   "r!   c                 C   s2   t | trtj| S t| drtj| jS d S )Nname)
isinstancer   ospathabspathr   r"   )r   r   r   r   _get_file_path-   s
   

r'   protoc                 C   s@   t | tr| S t| drt| jr|  }|S tdt| )z
    Serialize a in-memory proto to bytes

    Arguments:
        proto: a in-memory proto, such as a ModelProto, TensorProto, etc

    Returns:
        Serialized proto in bytes
    SerializeToStringzKNo SerializeToString method is detected. neither proto is a str.
type is {})r#   r   r   r   r)   	TypeErrorformattype)r(   resultr   r   r   
_serialize5   s   


r.   _Proto)boundr   c                 C   s   t | tstdt|  t|drt|js!tdt|tt	t
 || }|durB|t| krBtjjd|t| |S )z
    Parse bytes into a in-memory proto

    Arguments:
        s: bytes containing serialized proto
        proto: a in-memory proto object

    Returns:
        The proto instance filled in by s
    z)Parameter s must be bytes, but got type: ParseFromStringz2No ParseFromString method is detected. 
type is {}Nz6Protobuf decoding consumed too few bytes: {} out of {})r#   r   
ValueErrorr,   r   r   r1   r+   r   r   intlengoogleprotobufmessageDecodeError)r   r(   decodedr   r   r   _deserializeL   s   

r:   Tr+   load_external_datac                 C   s>   t | }t||d}|rt| }|rtj|}t|| |S )a  
    Loads a serialized ModelProto into memory
    load_external_data is true if the external data under the same directory of the model and load the external data
    If not, users need to call load_external_data_for_model with directory to load

    Arguments:
        f: can be a file-like object (has "read" function) or a string containing a file name
        format: for future use

    Returns:
        Loaded in-memory ModelProto
    r+   )r   load_model_from_stringr'   r$   r%   dirnamer   )r   r+   r;   r   modelmodel_filepathbase_dirr   r   r   
load_modelf   s   
rB   c                 C   s   t | }t||dS )z
    Loads a serialized TensorProto into memory

    Arguments:
        f: can be a file-like object (has "read" function) or a string containing a file name
        format: for future use

    Returns:
        Loaded in-memory TensorProto
    r<   )r   load_tensor_from_string)r   r+   r   r   r   r   load_tensor   s   rD   c                 C      t | t S )z
    Loads a binary string (bytes) that contains serialized ModelProto

    Arguments:
        s: a string, which contains serialized ModelProto
        format: for future use

    Returns:
        Loaded in-memory ModelProto
    )r:   
ModelProtor   r+   r   r   r   r=         r=   c                 C   rE   )z
    Loads a binary string (bytes) that contains serialized TensorProto

    Arguments:
        s: a string, which contains serialized TensorProto
        format: for future use

    Returns:
        Loaded in-memory TensorProto
    )r:   TensorProtorG   r   r   r   rC      rH   rC   F   save_as_external_dataall_tensors_to_one_filelocationsize_thresholdconvert_attributec                 C   sb   t | trt| t } |rt| |||| t|}|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.

    Arguments:
        proto: should be a in-memory ModelProto
        f: can be a file-like object (has "write" function) or a string containing a file name format for future use
        all_tensors_to_one_file: 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: specify the external file that all tensors to save to.
            If not specified, will use the model name.
        size_threshold: 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: If true, convert all tensors to external data
            If false, convert only non-attribute tensors to external data
    N)r#   r   r:   rF   r   r'   r$   r%   r>   r   r.   r!   )r(   r   r+   rK   rL   rM   rN   rO   r@   Zbasepathr   r   r   r   
save_model   s   

rP   c                 C   s   t | }t|| dS )z
    Saves the TensorProto to the specified path.

    Arguments:
        proto: should be a in-memory TensorProto
        f: can be a file-like object (has "write" function) or a string containing a file name
        format: for future use
    N)r.   r!   )r(   r   r   r   r   r   save_tensor   s   	rQ   )NT)N)NFTNrJ   F)3r$   Zonnx_cpp2py_exportr   Zonnx.external_data_helperr   r   r   Zonnx_pbZonnx_operators_pbZonnx_data_pbr   __version__Zonnx.checkerZonnxZ	onnx.defsZonnx.helperZ
onnx.utilsZonnx.composeZgoogle.protobuf.messager5   typingr	   r
   r   r   r   r   r   r   r   r!   r'   r6   r7   Messager.   r/   r:   boolrF   rB   rI   rD   r=   rC   r3   rP   rQ   loadZload_from_stringsaver   r   r   r   <module>   s:    ""	 ,(L"