
     
3j'                         S r SSKrSSK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KJrJrJrJr  SSKJr  \" 1 Sk5      r " S	 S
\
5      r\R.                  r\R0                  r\R2                  r\R6                  rg)z 
Implements ONNX's backend API.
    N)
ModelProtohelperversion)Backend)check_model)InferenceSessionSessionOptionsget_available_providers
get_device)OnnxRuntimeBackendRep>   logidexecution_modeexecution_orderenable_mem_reuseenable_mem_patternlog_severity_levellog_verbosity_levelenable_cpu_mem_arenainter_op_num_threadsintra_op_num_threadsuse_per_session_threadsgraph_optimization_leveluse_deterministic_computec                       \ rS rSrSr\" \R                  " SS5      S:H  5      r\	SS j5       r
\	S 5       r\	S 5       r\	SS	 j5       r\	SS
 j5       r\	SS j5       rSrg)OnnxRuntimeBackend,   a  
Implements
`ONNX's backend API <https://github.com/onnx/onnx/blob/main/docs/ImplementingAnOnnxBackend.md>`_
with *ONNX Runtime*.
The backend is mostly used when you need to switch between
multiple runtimes with the same API.
`Importing models from ONNX to Caffe2 <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxCaffe2Import.ipynb>`_
shows how to use *caffe2* as a backend for a converted model.
Note: This is not the official Python API.
ALLOW_RELEASED_ONNX_OPSET_ONLY1Nc                 >    Uc
  [        5       nU R                  U5      $ )z
Return whether the model is compatible with the backend.

:param model: unused
:param device: None to use the default device or a string (ex: `'CPU'`)
:return: boolean
)r   supports_device)clsmodeldevicekwargss       U/home/wildlama/miniconda3/lib/python3.13/site-packages/onnxruntime/backend/backend.pyis_compatible OnnxRuntimeBackend.is_compatible:   s!     >\F""6**    c                    U R                   (       am  UR                   H]  nUR                  (       a  UR                  OSn X2R                  4nU[        R
                  ;  a  SU SUR                   S3nSU4s  $ M_     g	! [         aN    US:X  a  UR                  S:  d  US:X  a.  UR                  S:  a  SU SUR                   S3nSU4s s  $  M   M  f = f)
aS  
Return whether the opset for the model is supported by the backend.
When By default only released onnx opsets are allowed by the backend
To test new opsets env variable ALLOW_RELEASED_ONNX_OPSET_ONLY should be set to 0

:param model: Model whose opsets needed to be verified.
:return: boolean and error message if opset is not supported.
zai.onnxzSkipping this test as only released onnx opsets are supported.To run this test set env variable ALLOW_RELEASED_ONNX_OPSET_ONLY to 0. Got Domain 'z' version 'z'.F   z
ai.ommx.ml   )T )allowReleasedOpsetsOnlyopset_importdomainr   r   OP_SET_ID_VERSION_MAPAttributeError)r!   r"   opsetr/   keyerror_messages         r%   is_opset_supported%OnnxRuntimeBackend.is_opset_supportedG   s     &&++).94!==1C&">">>,,28;u}}oRQ &
  %m33 ?	 ,,  & 
4 )+0BR^H^chcpcpstct,,28;u}}oRQ &
  %m33 duH^	
4s   7B  ACCc                     US:X  a  SnSU-   [        5       ;   =(       d$    US-   [        5       ;   =(       d    U[        5       :H  $ )zu
Check whether the backend is compiled with particular device support.
In particular it's used in the testing suite.
CUDAGPU-)r   )r!   r#   s     r%   r    "OnnxRuntimeBackend.supports_devicej   s@     VFV|z|+ev|z|/KevYcYeOeer(   c                    [        U[        5      (       a  U$ [        U[        5      (       a  [        U5      $ [        U[        [        45      (       Ga  [        5       nUR                  5        HZ  u  pVU[        ;   a  [        XEU5        M  [        XE5      (       d  M/  [        SU SSR                  [        [        5      5       35      e   [        R                  " SSS9R                  S5      n[!        5        Vs/ s H  oU;  d  M
  UPM     n	n[        XU	S9n
U
R#                  5         Ub0  U R%                  U5      (       d  [        S	U S
['        5        S35      eU R(                  " X40 UD6$ [*        R,                  R/                  [,        R,                  5      =(       d    [*        R,                  R1                  S5      nUR2                  S:  nU(       a  UR5                  5       OUn[7        U5        U R9                  U5      u  pU(       d  [:        R<                  " U5      eUn[        U[        [        45      (       d  UR5                  5       nU R(                  " UU40 UD6$ s  snf )u  
Load the model and creates an :class:`onnxruntime.backend.backend_rep.OnnxRuntimeBackendRep`
ready to be used as a backend.

:param model: the model to prepare — accepts a file path (str), serialized
    model (bytes), :class:`onnx.ModelProto`, :class:`onnxruntime.InferenceSession`,
    or :class:`onnxruntime.backend.backend_rep.OnnxRuntimeBackendRep` (returned as-is)
:param device: requested device for the computation,
    None means the default one which depends on
    the compilation settings
:param kwargs: only a safe subset of :class:`onnxruntime.SessionOptions` attributes are
    accepted; see ``_ALLOWED_SESSION_OPTIONS`` for the list
:return: :class:`onnxruntime.backend.backend_rep.OnnxRuntimeBackendRep`
zSessionOptions attribute 'z<' is not permitted via the backend API. Allowed attributes: z, "ORT_ONNX_BACKEND_EXCLUDE_PROVIDERSr,   )default,)sess_options	providerszIncompatible device expected 'z', got ''0)   
   r   )
isinstancer   r   strbytesr	   items_ALLOWED_SESSION_OPTIONSsetattrhasattrRuntimeErrorjoinsortedosgetenvsplitr
   disable_fallbackr    r   prepare	packagingr   parseVersionreleaseSerializeToStringr   r5   unittestSkipTest)r!   r"   r#   r$   optionskvexcluded_providersxrA   infonnx_version$onnx_supports_serialized_model_checkbin_or_modelopset_supportedr4   bins                    r%   rT   OnnxRuntimeBackend.preparet   s     e233L/00(//U|,,$&G00G*W((&4QC 8//3yy@X9Y/Z.[] 	 ' "$+OY[!\!b!bcf!g$;$=_$=qK]B]$=I_"5)TC   "!#*=*=f*E*E"%CF88T^T`Saab#cdd;;s5f55 %,,227??CeyGXGXG`G`adGeL3?3G3G:3U08\5224bgL%-0-C-CE-J*O"''66 CcC<00++-;;sF5f555 `s   <	I(	I(c                 N    U R                   " X40 UD6nUR                  " U40 UD6$ )u  
Compute the prediction.

:param model: the model to run — accepts a file path (str), serialized
    model (bytes), :class:`onnx.ModelProto`, :class:`onnxruntime.InferenceSession`,
    or :class:`onnxruntime.backend.backend_rep.OnnxRuntimeBackendRep`
:param inputs: inputs
:param device: requested device for the computation,
    None means the default one which depends on
    the compilation settings
:param kwargs: ``run_model()`` forwards kwargs to both ``prepare()`` and ``rep.run()``.
    ``prepare()`` validates and applies ``_ALLOWED_SESSION_OPTIONS`` only when creating
    a new session from a model path or bytes; if ``model`` is already an
    ``InferenceSession`` or ``OnnxRuntimeBackendRep``, session-option kwargs are
    silently ignored. ``rep.run()`` always validates against ``_ALLOWED_RUN_OPTIONS``
    and raises ``RuntimeError`` for known-but-blocked run attributes.
    Logging-related kwargs (``log_severity_level``, ``log_verbosity_level``, ``logid``)
    appear in both allowlists.
:return: predictions
)rT   run)r!   r"   inputsr#   r$   reps         r%   	run_modelOnnxRuntimeBackend.run_model   s+    , kk%262wwv(((r(   c                     [        S5      e)zq
This method is not implemented as it is much more efficient
to run a whole model than every node independently.
zMIt is much more efficient to run a whole model than every node independently.)NotImplementedError)r!   noderj   r#   outputs_infor$   s         r%   run_nodeOnnxRuntimeBackend.run_node   s     ""qrrr(    )N)NN)__name__
__module____qualname____firstlineno____doc__boolrP   rQ   r-   classmethodr&   r5   r    rT   rl   rr   __static_attributes__rt   r(   r%   r   r   ,   s    	 #299-Ms#SWZ#Z[
+ 
+    D f f :6 :6x ) )0 s sr(   r   )ry   rP   rZ   packaging.versionrU   onnxr   r   r   onnx.backend.baser   onnx.checkerr   onnxruntimer   r	   r
   r   onnxruntime.backend.backend_repr   	frozensetrJ   r   r&   rT   rl   ri   r    rt   r(   r%   <module>r      s{   
 
   , , % $ ] ] A % &ds dsN #00

$
$""$44r(   