
     TiU                        S r SSKJr  SS/rSSKrSSKJr  SSKrSSKJ	r	J
r
Jr  SSKJr  SS	KJr  SSS
 jjr    S             SS jjrg)zLoad and save ONNX models.    )annotationsloadsaveN)Callable)_core
_protocolsserde)external_data)zipc                    [         R                  " XSS9n[        R                  " U5      n[        R
                  R                  U 5      n[        R                  " UR                  U5        U$ )zLoad an ONNX model from a file.

Args:
    path: The path to the ONNX file.
    format: The format of the file (e.g. protobuf, textproto, json, etc.).
        If None, the format is inferred from the file extension.

Returns:
    The loaded model.
F)formatload_external_data)
onnxr   r	   deserialize_modelospathdirname_external_dataset_base_dirgraph)r   r   protomodelbase_dirs        J/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/_io.pyr   r      sQ     IIdeDE##E*Ewwt$H X6L    c                   UGb  [         R                  R                  U5      (       a  [        SU S35      e[         R                  R	                  U5      n/ nU R                  5        H,  nUR                  UR                  R                  5       5        M.     U V	s/ s H  oR                  PM     n
n	 [        R                  " U UUUUS9n [        R                  " U 5      n[        R                  " XUS9  [!        XzSS9 H  u  pXl	        M     g[        R                  " U 5      n[        R                  " XUS9  gs  sn	f ! [!        XzSS9 H  u  pXl	        M     f = f)a  Save an ONNX model to a file.

The model remains unchanged after the call. If any existing external tensor
references the provided ``external_data`` path, it will be invalidated
after the external data is overwritten. To obtain a valid model, use :func:`load`
to load the newly saved model, or provide a different external data path that
is not currently referenced by any tensors in the model.

.. tip::

    A simple progress bar can be implemented by passing a callback function as the following::

        import onnx_ir as ir
        import tqdm

        with tqdm.tqdm() as pbar:
            total_set = False

            def callback(tensor: ir.TensorProtocol, metadata: ir.external_data.CallbackInfo) -> None:
                nonlocal total_set
                if not total_set:
                    pbar.total = metadata.total
                    total_set = True

                pbar.update()
                pbar.set_description(f"Saving {tensor.name} ({tensor.dtype}, {tensor.shape}) at offset {metadata.offset}")

            ir.save(
                ...,
                callback=callback,
            )

Args:
    model: The model to save.
    path: The path to save the model to. E.g. "model.onnx".
    format: The format of the file (e.g. ``protobuf``, ``textproto``, ``json``, etc.).
        If None, the format is inferred from the file extension.
    external_data: The relative path to save external data to. When specified,
        all initializers in the model will be converted to external data and
        saved to the specified directory. If None, all tensors will be saved unmodified.
        That is, if a tensor in the model is already external, it will be saved
        with the same external information; if the tensor is not external,
        it will be serialized in the ONNX Proto message.
    size_threshold_bytes: Save to external data if the tensor size in bytes is larger than this threshold.
        Effective only when ``external_data`` is set.
    callback: A callback function that is called for each tensor that is saved to external data
        for debugging or logging purposes.

Raises:
    ValueError: If the external data path is an absolute path.
NzDThe external data path must be relative to the ONNX file path, not 'z'.)size_threshold_bytescallback)r   T)strict)r   r   isabs
ValueErrorr   graphsextendinitializersvaluesconst_valuer   unload_from_modelr	   serialize_modelr   r   r   )r   r   r   r
   r   r   r   initialized_valuesr   vtensorsr   initializertensors                 r   r   r   )   sD   x  77==''VWdVeegh  77??4( 13\\^E%%e&8&8&?&?&AB $ +==*<Q==*<=	1"44%9!E ))%0EIIe&1 (++=t'T#*0' (U %%e,		%f-) > (++=t'T#*0' (Us   D>5AE E)N)r   str | os.PathLiker   
str | Nonereturn_core.Model)NN   N)r   r1   r   r.   r   r/   r
   zstr | os.PathLike | Noner   intr   zOCallable[[_protocols.TensorProtocol, _external_data.CallbackInfo], None] | Noner0   None)__doc__
__future__r   __all__r   typingr   r   onnx_irr   r   r	   r
   r   onnx_ir._polyfillr   r   r    r   r   <module>r<      s    ! "6
 	   , , 3 !2 .2 #\.\.
\. \. ,	\.
 \.\. 
\.r   