
    bIg-                    R   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, d d	l-m.Z.m/Z/ d d
l0m1Z1m2Z2m3Z3 d dl4m5Z6 d dlm7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC eed   eDf   ZEdZFddZGddZHddZI	 d 	 	 	 	 	 d!dZJ	 	 d"	 	 	 	 	 	 	 d#dZK	 d 	 	 	 	 	 d$dZLeFf	 	 	 	 	 d%dZMeFf	 	 	 	 	 d&dZN	 d dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZO	 d 	 	 	 	 	 	 	 d(dZPeKZQeMZReOZSy))    )annotations)8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EXPERIMENTALSTABLEcheckercomposedefs	gen_protohelperhubmappingnumpy_helperparserprintershape_inferenceutilsversion_converterAttributeProtoFunctionProto
GraphProtoMapProto
ModelProto	NodeProtoOperatorProtoOperatorSetIdProtoOperatorSetProtoOperatorStatusOptionalProtoSequenceProto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   )r4   r;   r3   )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   )r$   r&   )r!   r(   r)   )version)r   r   r   r   r   r   r   r   r   r   r   r   r   )protobuf	textprotoonnxtxtjsonrA   c                   t        | d      ret        t        j                  t        t
           |       j                        r1t        j                  t        t
           |       j                         }|S t        j                  t        t        t        j                  f   |       } t        | d      5 }|j                         }d d d        |S # 1 sw Y   S xY w)Nreadrb)hasattrcallabletypingcastr<   bytesrF   r>   strosPathLikeopen)fcontentreadables      J/var/www/html/answerous/venv/lib/python3.12/site-packages/onnx/__init__.py_load_bytesrU      s    q&hv{{2e9a'@'E'EF++bi+002
 N KKc2;;./3!T] 	&hmmoG	&N	&Ns   /C

Cc                   t        |d      ret        t        j                  t        t
           |      j                        r1t        j                  t        t
           |      j                  |        y t        j                  t        t        t        j                  f   |      }t        |d      5 }|j                  |        d d d        y # 1 sw Y   y xY w)Nwritewb)rH   rI   rJ   rK   r<   rL   rW   r>   rM   rN   rO   rP   )rR   rQ   writables      rT   _save_bytesrZ      s    q'xBuIq(A(G(GHBuIq!''0KKc2;;./3!T] 	$hNN7#	$ 	$ 	$s   /C

Cc                    t        | t        t        j                  f      rt        j                  j                  |       S t        | d      r-| J t        j                  j                  | j                        S y )Nname)
isinstancerM   rN   rO   pathabspathrH   r\   )rQ   s    rT   _get_file_pathr`      sV    !c2;;'(wwq!!q&}}wwqvv&&    c                :   | t         j                  j                  |       S t        |      x}At        j
                  j                  |      \  }}t         j                  j                  |      } | xs t        } | J t         j                  j                  |       S )zQGet the serializer for the given path and format from the serialization registry.)	r?   registrygetr`   rN   r^   splitextget_format_from_file_extension_DEFAULT_FORMAT)fmtrQ   	file_path_exts        rT   _get_serializerrl      s    
 %%))#..#A&&	3!!),3$$CCCH 
 C??!!%%c**ra   Tc                    t        ||       j                  t        |       t                     }|r8t	        |       }|r+t
        j                  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.
    )	rl   deserialize_protorU   r"   r`   rN   r^   dirnamer4   )rQ   formatload_external_datamodelmodel_filepathbase_dirs         rT   r6   r6      sT    * FA&88QVE'*ww~6H(9Lra   c                \    t        ||       j                  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.
    )rl   rn   rU   r-   )rQ   rp   s     rT   r8   r8      s$      61%77AVVra   c                H    t        |      j                  | 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.
    )rl   rn   r"   srp   s     rT   r5   r5      s      6"44Q
EEra   c                H    t        |      j                  | 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.
    )rl   rn   r-   rw   s     rT   r7   r7     s      6"44QFFra   Fi   )save_as_external_dataall_tensors_to_one_filelocationsize_thresholdconvert_attributec               R   t        | t              r't        t              j	                  | t                     } |rt        | ||||       t        |      }|+t        j                  j                  |      }	t        | |	      } t        ||      j                  |       }
t        |
|       y)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)r]   rL   rl   rg   rn   r"   r3   r`   rN   r^   ro   r;   serialize_protorZ   )protorQ   rp   rz   r{   r|   r}   r~   rs   basepath
serializeds              rT   r9   r9     s    H %0BB5*,W&*HnFW	
 $A&N!77??>2+E8< 8HHOJ
Ara   c                R    t        ||      j                  |       }t        ||       y)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)rl   r   rZ   )r   rQ   rp   r   s       rT   r:   r:   M  s%      !+;;EBJ
Ara   )rQ   IO[bytes] | str | os.PathLikereturnrL   )rR   rL   rQ   r   r   None)rQ   z$IO[bytes] | str | os.PathLike | Noner   
str | None)N)rh   _SupportedFormat | NonerQ   z$str | os.PathLike | IO[bytes] | Noner   zserialization.ProtoSerializer)NT)rQ   r   rp   r   rq   boolr   r"   )rQ   r   rp   r   r   r-   )rx   zbytes | strrp   _SupportedFormatr   r"   )rx   rL   rp   r   r   r-   )r   zModelProto | bytesrQ   r   rp   r   rz   r   r{   r   r|   r   r}   intr~   r   r   r   )r   r-   rQ   r   rp   r   r   r   )T
__future__r   __all__rN   rJ   r<   r=   r>   onnxr?   onnx.onnx_cpp2py_exportr   onnx.external_data_helperr4   r;   r3   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/   r0   r1   r2   onnx.onnx_operators_pbr$   r&   onnx.onnx_data_pbr!   r(   r)   onnx.versionr@   __version__r   r   r   r   r   r   r   r   r   r   r   r   r   rM   r   rg   rU   rZ   r`   rl   r6   r8   r5   r7   r9   r:   loadload_from_stringsave ra   rT   <module>r      s3  
 #=@ 
  % %  + 
       < C D D /   & 67<  $ MQ+	 +%I+"+* '+#$#  	D '+W$W#W W*  /FFF F*  /GGG G, '+2
 #($(#22$2 $2
  2 "2 2 2 2 
2p '+$ $ 
	* ) ra   