
    gw                       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mZmZ d dl	m
Z ej                  rd dlZddZ G d d      Z	 	 	 	 	 	 ddZ G d	 d
      Z G d de      Z G d d      Z G d d      Z G d d      Z G d d      Zy)    )annotationsN)AnySequence)_pybind_statec                   | dk(  rt         j                  j                         S | dk(  rt         j                  j                         S | dk(  rt         j                  j	                         S | dk(  rt         j                  j                         S | dk(  r#t        j                  |      j                         S t        d| z         )NcudacanncpudmlortzUnsupported device type: )	C	OrtDevicer   r	   r
   r   get_ort_devicedevice_type	Exception)r   device_indexs     f/var/www/openai/venv/lib/python3.12/site-packages/onnxruntime/capi/onnxruntime_inference_collection.pyget_ort_device_typer      s    f{{!!		{{!!		{{  		{{  		-99;;3kABB    c                  f    e Zd ZdZdddZedd       ZddZd ZddZ	d Z
dd	Zd
 ZddZddZy)AdapterFormatzK
    This class is used to create adapter files from python structures
    Nc                J    |t        j                         | _        y || _        y N)r   r   _adapter)selfadapters     r   __init__zAdapterFormat.__init__(   s    ?OO-DM#DMr   c                R    t        t        j                   j                  |             S r   )r   r   read_adapter)	file_paths    r   r   zAdapterFormat.read_adapter.   s    Q__99)DEEr   c                :    | j                   j                  |       y)z
        This function writes a file at the specified location
        in onnxrunitme adapter format containing Lora parameters.

        :param file_path: absolute path for the adapter
        N)r   export_adapter)r   r    s     r   r"   zAdapterFormat.export_adapter2   s     	$$Y/r   c                .    | j                   j                  S r   )r   format_versionr   s    r   get_format_versionz AdapterFormat.get_format_version;   s    }}+++r   c                &    || j                   _        y r   r   adapter_version)r   r)   s     r   set_adapter_versionz!AdapterFormat.set_adapter_version>   s    (7%r   c                .    | j                   j                  S r   r(   r%   s    r   get_adapter_versionz!AdapterFormat.get_adapter_versionA   s    }},,,r   c                &    || j                   _        y r   r   model_version)r   r/   s     r   set_model_versionzAdapterFormat.set_model_versionD   s    &3#r   c                .    | j                   j                  S r   r.   r%   s    r   get_model_versionzAdapterFormat.get_model_versionG   s    }}***r   c                    |j                         D ci c]  \  }}||j                   c}}| j                  _        y c c}}w r   )items	_ortvaluer   
parameters)r   paramskvs       r   set_parameterszAdapterFormat.set_parametersJ   s2    ?E||~#N~tq!Aq{{N~#N #Ns   ?c                    | j                   j                  j                         D ci c]  \  }}|t        |       c}}S c c}}w r   )r   r6   r4   OrtValue)r   r8   r9   s      r   get_parameterszAdapterFormat.get_parametersM   s;    +/==+C+C+I+I+KL+K41a8A;+KLLLs   Ar   )returnNone)r    os.PathLiker>   r   )r    r@   )r)   int)r/   rA   )r7   dict[str, OrtValue])r>   rB   )__name__
__module____qualname____doc__r   staticmethodr   r"   r&   r*   r,   r0   r2   r:   r=    r   r   r   r   #   sL    $ F F0,8-4+OMr   r   c           	        | g g fS t        j                         fd}t        | t         j                  j                        st        d      |t        |t         j                  j                        st        d      t        |       t        |      k7  rt        d      t        | D cg c]  }t        |t               c}      st        d      t        |D cg c]  }t        |t               c}      st        d      t        | |      D ]  \  }} |||        n| D ]  }t        |t              r ||t                      %t        |t              rDt        |      dk(  r6t        |d   t              r#t        |d	   t              r ||d   |d	          yt        d
       t        j                               t        j                               fS c c}w c c}w )a  
    Validates the 'providers' and 'provider_options' arguments and returns a
        normalized version.

    :param providers: Optional sequence of providers in order of decreasing
        precedence. Values can either be provider names or tuples of
        (provider name, options dict).
    :param provider_options: Optional sequence of options dicts corresponding
        to the providers listed in 'providers'.
    :param available_provider_names: The available provider names.

    :return: Tuple of (normalized 'providers' sequence, normalized
        'provider_options' sequence).

    'providers' can contain either names or names and options. When any options
        are given in 'providers', 'provider_options' should not be used.

    The normalized result is a tuple of:
    1. Sequence of provider names in the same order as 'providers'.
    2. Sequence of corresponding provider options dicts with string keys and
        values. Unspecified provider options yield empty dicts.
    c                2   | vr4t        j                  dj                  | dj                                     | v rt        j                  d|  d       y |j	                         D ci c]  \  }}t        |      t        |       }}}|| <   y c c}}w )NzTSpecified provider '{}' is not in available provider names.Available providers: '{}'z, zDuplicate provider 'z' encountered, ignoring.)warningswarnformatjoinr4   str)nameoptionskeyvaluenormalized_optionsavailable_provider_namesprovider_name_to_optionss        r   set_provider_optionsz?check_and_normalize_provider_args.<locals>.set_provider_optionsq   s    //MM,,2F4C[9\,]
 ++MM06NOPEL]]_U_zsEc#hE
2_U); & Vs   +Bz!'providers' should be a sequence.z('provider_options' should be a sequence.zO'providers' and 'provider_options' should be the same length if both are given.zPOnly string values for 'providers' are supported if 'provider_options' is given.z('provider_options' values must be dicts.   r      zC'providers' values must be either strings or (string, dict) tuples.)collectionsOrderedDict
isinstanceabcr   
ValueErrorlenallrO   dictziptuplelistkeysvalues)		providersprovider_optionsrU   rW   provideroptions_for_providerrP   rQ   rV   s	     `     @r   !check_and_normalize_provider_argsrk   Q   s   6 2v*668< i!9!9:<==#*KOO,D,DEGHHy>S!122nooiHi(Jx-iHIoppWghWg?SJ3T:WghiGHH ,<=MD' w/ > "H(C($Xtv68U+MQ&x{C0x{D1$Xa[(1+> !fgg " (--/0$7O7V7V7X2YYY/ I is   .G  G%c                      e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 ZddZd Zd Zd ZddZddZddZd Zd Zd ZddZd ZdddZd Zy
)Session5
    This is the main class used to run a model.
    c                     d | _         d| _        y )NT)_sess_enable_fallbackr%   s    r   r   zSession.__init__   s    
 $r   c                    | j                   S )zDReturn the session options. See :class:`onnxruntime.SessionOptions`.)_sess_optionsr%   s    r   get_session_optionszSession.get_session_options       !!!r   c                    | j                   S )zEReturn the inputs metadata as a list of :class:`onnxruntime.NodeArg`.)_inputs_metar%   s    r   
get_inputszSession.get_inputs   s       r   c                    | j                   S )zFReturn the outputs metadata as a list of :class:`onnxruntime.NodeArg`.)_outputs_metar%   s    r   get_outputszSession.get_outputs   ru   r   c                    | j                   S )z^Return the inputs (including initializers) metadata as a list of :class:`onnxruntime.NodeArg`.)_overridable_initializersr%   s    r   get_overridable_initializersz$Session.get_overridable_initializers   s    ---r   c                    | j                   S )z<Return the metadata. See :class:`onnxruntime.ModelMetadata`.)_model_metar%   s    r   get_modelmetazSession.get_modelmeta   s    r   c                    | j                   S )z.Return list of registered execution providers.)
_providersr%   s    r   get_providerszSession.get_providers   s    r   c                    | j                   S )z6Return registered execution providers' configurations.)_provider_optionsr%   s    r   get_provider_optionszSession.get_provider_options   s    %%%r   Nc                (    | j                  ||       y)a  
        Register the input list of execution providers. The underlying session is re-created.

        :param providers: Optional sequence of providers in order of decreasing
            precedence. Values can either be provider names or tuples of
            (provider name, options dict). If not provided, then all available
            providers are used with the default precedence.
        :param provider_options: Optional sequence of options dicts corresponding
            to the providers listed in 'providers'.

        'providers' can contain either names or names and options. When any options
        are given in 'providers', 'provider_options' should not be used.

        The list of providers is ordered by precedence. For example
        `['CUDAExecutionProvider', 'CPUExecutionProvider']`
        means execute a node using CUDAExecutionProvider if capable,
        otherwise execute using CPUExecutionProvider.
        N)_reset_sessionr   rg   rh   s      r   set_providerszSession.set_providers   s    ( 	I'78r   c                    d| _         y)z;
        Disable session.run() fallback mechanism.
        FNrq   r%   s    r   disable_fallbackzSession.disable_fallback   s     !&r   c                    d| _         y)a4  
        Enable session.Run() fallback mechanism. If session.Run() fails due to an internal Execution Provider failure,
        reset the Execution Providers enabled for this session.
        If GPU is enabled, fall back to CUDAExecutionProvider.
        otherwise fall back to CPUExecutionProvider.
        TNr   r%   s    r   enable_fallbackzSession.enable_fallback   s     !%r   c                    g }| j                   D ]H  }|j                  |vs|j                  j                  d      r.|j	                  |j                         J |rt        d| d| d      y )NoptionalzRequired inputs (z) are missing from input feed (z).)rw   rP   type
startswithappendr^   )r   feed_input_namesmissing_input_namesinputs       r   _validate_inputzSession._validate_input   sx     &&Ezz!11%**:O:OPZ:[#**5::6 ' #$7#88WXhWiikl  r   c                4   | j                  t        |j                                      |s#| j                  D cg c]  }|j                   }}	 | j
                  j                  |||      S c c}w # t        j                  $ r}| j                  rt        d|d| j                          t        d| j                   d       | j                  | j                         | j                          | j
                  j                  |||      cY d}~S  d}~ww xY w)a  
        Compute the predictions.

        :param output_names: name of the outputs
        :param input_feed: dictionary ``{ input_name: input_value }``
        :param run_options: See :class:`onnxruntime.RunOptions`.
        :return: list of results, every result is either a numpy array,
            a sparse tensor, a list or a dictionary.

        ::

            sess.run([output_name], {input_name: x})
        
EP Error:  using Falling back to  and retrying.N)r   rd   re   rz   rP   rp   runr   EPFailrq   printr   _fallback_providersr   r   )r   output_names
input_feedrun_optionsoutputerrs         r   r   zSession.run   s     	T*//"3456:6H6HI6HFFKK6HLI
	::>>,
KHH J xx 	$$
3'0ABC()A)A(B.QR""4#;#;<%%'zz~~lJLL	s*   A,A1 1DBDDDDc                    | j                  t        |j                                      |s#| j                  D cg c]  }|j                   }}| j
                  j                  |||||      S c c}w )a  
        Compute the predictions asynchronously in a separate cxx thread from ort intra-op threadpool.

        :param output_names: name of the outputs
        :param input_feed: dictionary ``{ input_name: input_value }``
        :param callback: python function that accept array of results, and a status string on error.
            The callback will be invoked by a cxx thread from ort intra-op threadpool.
        :param run_options: See :class:`onnxruntime.RunOptions`.

        ::
            class MyData:
                def __init__(self):
                    # ...
                def save_results(self, results):
                    # ...

            def callback(results: np.ndarray, user_data: MyData, err: str) -> None:
              if err:
                 print (err)
              else:
                # save results to user_data

            sess.run_async([output_name], {input_name: x}, callback)
        )r   rd   re   rz   rP   rp   	run_async)r   r   r   callback	user_datar   r   s          r   r   zSession.run_async  se    2 	T*//"3456:6H6HI6HFFKK6HLIzz##L*h	S^__ Js   A-c                   d }| j                  t        |j                                      |s#| j                  D cg c]  }|j                   }}	  || j
                  |||      S c c}w # t        j                  $ r}| j                  ryt        d|d| j                          t        d| j                   d       | j                  | j                         | j                           || j
                  |||      cY d}~S  d}~ww xY w)a  
        Compute the predictions.

        :param output_names: name of the outputs
        :param input_dict_ort_values: dictionary ``{ input_name: input_ort_value }``
            See ``OrtValue`` class how to create `OrtValue`
            from numpy array or `SparseTensor`
        :param run_options: See :class:`onnxruntime.RunOptions`.
        :return: an array of `OrtValue`

        ::

            sess.run([output_name], {input_name: x})
        c                
   i }|j                         D ]  \  }}|j                         ||<    | j                  |||      }t        |t        j
                        st        d      |D cg c]  }t        |       }}|S c c}w )NzFrun_with_ort_values() must return a instance of type 'OrtValueVector'.)r4   _get_c_valuerun_with_ort_valuesr\   r   OrtValueVector	TypeErrorr<   )	sessr   input_dict_ort_valuesr   
input_dictnr9   result
ort_valuess	            r   invokez+Session.run_with_ort_values.<locals>.invokeC  s    J-3351 ! 0
1 6--j,TFfa&6&67 hii/56v!(1+vJ6 7s   *B r   r   r   r   N)r   rd   re   rz   rP   rp   r   r   rq   r   r   r   r   r   )r   r   r   r   r   r   r   s          r   r   zSession.run_with_ort_values3  s     	 	T"7"<"<">?@6:6H6HI6HFFKK6HLI
	$**l4I;WW J xx 	$$
3'0ABC()A)A(B.QR""4#;#;<%%'djj,8M{[[	s*   A'A, ,D
?A?D>D
DD
c                6    | j                   j                         S )z
        End profiling and return results in a file.

        The results are stored in a filename if the option
        :meth:`onnxruntime.SessionOptions.enable_profiling`.
        )rp   end_profilingr%   s    r   r   zSession.end_profiling\  s     zz''))r   c                .    | j                   j                  S )a  
        Return the nanoseconds of profiling's start time
        Comparable to time.monotonic_ns() after Python 3.3
        On some platforms, this timer may not be as precise as nanoseconds
        For instance, on Windows and MacOS, the precision will be ~100ns
        )rp   get_profiling_start_time_nsr%   s    r   r   z#Session.get_profiling_start_time_nse  s     zz555r   c                    t        |       S )z(Return an onnxruntime.IOBinding object`.)	IOBindingr%   s    r   
io_bindingzSession.io_bindingn  s    r   c                P    | j                   j                  |j                  |       y)z
        Compute the predictions.

        :param iobinding: the iobinding object that has graph inputs/outputs bind.
        :param run_options: See :class:`onnxruntime.RunOptions`.
        N)rp   run_with_iobinding
_iobinding)r   	iobindingr   s      r   r   zSession.run_with_iobindingr  s     	

%%i&:&:KHr   c                6    | j                   j                         S r   )rp   get_tuning_resultsr%   s    r   r   zSession.get_tuning_results{  s    zz,,..r   F)error_on_invalidc               :    | j                   j                  ||      S r   )rp   set_tuning_results)r   resultsr   s      r   r   zSession.set_tuning_results~  s    zz,,W6FGGr   c                D    | j                   j                  ||||||       y)a  
        Compute the predictions similar to other run_*() methods but with minimal C++/Python conversion overhead.

        :param run_options: See :class:`onnxruntime.RunOptions`.
        :param feed_names: list of input names.
        :param feeds: list of input OrtValue.
        :param fetch_names: list of output names.
        :param fetches: list of output OrtValue.
        :param fetch_devices: list of output devices.
        N)rp   run_with_ortvaluevector)r   r   
feed_namesfeedsfetch_namesfetchesfetch_devicess          r   r   zSession.run_with_ortvaluevector  s"     	

**;
E;X_anor   )NNr   )rC   rD   rE   rF   r   rt   rx   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   r   r   rm   rm      s    %
"!". &9,&%:`<'R*6I/ ?D Hpr   rm   c                  R     e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 d fdZddZd Zd Z xZS )	InferenceSessionrn   c                b   t         	|           t        |t        t        j
                  f      r"t	        j                  |      | _        d| _        n7t        |t              rd| _        || _        nt        dt        |       d      || _        || _        d| _        d|v rt        |d         dk(  | _        n't        j"                  j%                  d      dk(  | _        |j%                  d	      }	 | j'                  |||       y# t(        t*        f$ r}| j                  r	 t-        d
       t-        d| d|        t-        d| j.                   d       t-        d       | j'                  | j.                  d       | j1                          Y d}~y# t2        $ r}||d}~ww xY w|d}~ww xY w)a  
        :param path_or_bytes: Filename or serialized ONNX or ORT format model in a byte string.
        :param sess_options: Session options.
        :param providers: Optional sequence of providers in order of decreasing
            precedence. Values can either be provider names or tuples of
            (provider name, options dict). If not provided, then all available
            providers are used with the default precedence.
        :param provider_options: Optional sequence of options dicts corresponding
            to the providers listed in 'providers'.

        The model type will be inferred unless explicitly set in the SessionOptions.
        To explicitly set:

        ::

            so = onnxruntime.SessionOptions()
            # so.add_session_config_entry('session.load_model_format', 'ONNX') or
            so.add_session_config_entry('session.load_model_format', 'ORT')

        A file extension of '.ort' will be inferred as an ORT format model.
        All other filenames are assumed to be ONNX format models.

        'providers' can contain either names or names and options. When any options
        are given in 'providers', 'provider_options' should not be used.

        The list of providers is ordered by precedence. For example
        `['CUDAExecutionProvider', 'CPUExecutionProvider']`
        means execute a node using `CUDAExecutionProvider`
        if capable, otherwise execute using `CPUExecutionProvider`.
        NzUnable to load from type ''Tread_config_from_modelrY   ORT_LOAD_CONFIG_FROM_MODEL1disabled_optimizersz(*************** EP Error ***************z	EP Error z when using r   r   z(****************************************)superr   r\   rO   osPathLikefspath_model_path_model_bytesbytesr   r   rs   _sess_options_initialrq   rA   _read_config_from_modelenvironget_create_inference_sessionr^   RuntimeErrorr   r   r   r   )
r   path_or_bytessess_optionsrg   rh   kwargsr   efallback_error	__class__s
            r   r   zInferenceSession.__init__  s   L 	mc2;;%78!yy7D $Du-#D -D8m9L8MQOPP)%1" $#v-+.v6N/O+PTU+UD(+-::>>:V+W[^+^D( %jj)>?	**96FH[\L) 	$$
0DEIaSYK@A,T-E-E,FnUVDE2243K3KTR))+  0(a/0 G	s7   2D F.F)"A,F	F$FF$$F))F.c                   t        j                         }d|v r9|r.t        d |D              rt        d |D              r
ddg| _        n<dg| _        n3d|v r'|rt        d |D              r
ddg| _        ndg| _        ndg| _        t	        |||      \  }}| j
                  r| j
                  nt        j                         }| j                  ||||       | j                  r-t        j                  || j                  d	| j                        }n,t        j                  || j                  d
| j                        }|t               }nt        |t              st        |      }|j                  |||       || _        | j                  j                   | _        | j                  j"                  | _        | j                  j&                  | _        | j                  j*                  | _        | j                  j.                  | _        | j                  j3                         | _        | j                  j7                         | _        | j                  j:                  | _        y )NTensorrtExecutionProviderc              3  ^   K   | ]%  }|d k(  xs t        |t              xr |d   d k(   ' yw)CUDAExecutionProviderr   Nr\   rc   .0ri   s     r   	<genexpr>z=InferenceSession._create_inference_session.<locals>.<genexpr>  sE       %.  77 `"8U3^G^8^`$-   +-c              3  ^   K   | ]%  }|d k(  xs t        |t              xr |d   d k(   ' yw)r   r   Nr   r   s     r   r   z=InferenceSession._create_inference_session.<locals>.<genexpr>  sE       %.  ;; d"8U3bGb8bd$-r   r   CPUExecutionProviderMIGraphXExecutionProviderc              3  ^   K   | ]%  }|d k(  xs t        |t              xr |d   d k(   ' yw)ROCMExecutionProviderr   Nr   r   s     r   r   z=InferenceSession._create_inference_session.<locals>.<genexpr>  sE      ! !*H 33 \x/ZHQKCZ4Z\ )r   r   TF)r   get_available_providersanyr   rk   rs   get_default_session_options_register_ep_custom_opsr   r   r   r   setr\   initialize_sessionrp   session_optionsinputs_metarw   outputs_metarz   overridable_initializersr}   
model_metar   r   r   r   r   r   _profiling_start_time_ns)r   rg   rh   r   available_providersr   r   s          r   r   z*InferenceSession._create_inference_session  s+   779 '*==  %. 
   %.  -DE[+\(,B+C((,??S ! !*! 
 -DE[+\(,B+C((>'?D$ 'H')<'
#	# 150B0B$,,HeHeHg$$_iAQSfg%%ot7G7GtOkOklD%%ot7H7H%QUQmQmnD&"%%/5"%&9": 		+;=PQ
!ZZ77 JJ22!ZZ44)-)L)L&::00**224!%!@!@!B(,

(N(N%r   c                    d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        | j                  | _         | j                  ||       y)z"release underlying session object.N)rs   rw   rz   r}   r   r   r   r  rp   r   r   r   s      r   r   zInferenceSession._reset_session%  sk     " !)-&!%(,% 
!77&&y2BCr   c                   t        t        |            D ]r  }||   |v r"||   dk(  rt        j                  |||          ,t	        ||   t
              s@||   d   |v sK||   d   dk(  sWt        j                  |||   d          t y )Nr   r   rY   )ranger_   r   'register_tensorrt_plugins_as_custom_opsr\   rc   )r   r   rg   rh   r  is         r   r   z(InferenceSession._register_ep_custom_ops7  s    s9~&A|22y|Gb7b99/K[\]K^_9Q</aLO'::aLO'BB99/9UV<XY?[ 'r   )NNN)
r   zstr | bytes | os.PathLiker   z!onnxruntime.SessionOptions | Nonerg   1Sequence[str | tuple[str, dict[Any, Any]]] | Nonerh   Sequence[dict[Any, Any]] | Noner>   r?   r   )	rC   rD   rE   rF   r   r   r   r   __classcell__)r   s   @r   r   r     sd     ;?GK<@L0L 8L E	L
 :L 
L\AOFD$	\r   r   c                  l    e Zd ZdZddZd Zd Zd Zd Z	 	 	 	 	 ddZ	d	 Z
d
 Zd Zd Zd Zd Zd Zy)r   zW
    This class provides API to bind input/output to a specified device, e.g. GPU.
    c                Z    t        j                  |j                        | _        i | _        y r   )r   SessionIOBindingrp   r   _numpy_obj_references)r   sessions     r   r   zIOBinding.__init__H  s     ,,W]];%'"r   c                Z    || j                   |<   | j                  j                  ||       y)z
        bind an input to array on CPU
        :param name: input name
        :param arr_on_cpu: input values as a python array on CPU
        N)r  r   
bind_input)r   rP   
arr_on_cpus      r   bind_cpu_inputzIOBinding.bind_cpu_inputL  s(     ,6""4(""44r   c           	         | j                   j                  |t        j                  t	        ||      t        j                  j                         |      |||       y)a  
        :param name: input name
        :param device_type: e.g. cpu, cuda, cann
        :param device_id: device id, e.g. 0
        :param element_type: input element type. It can be either numpy type (like numpy.float32) or an integer for onnx type (like onnx.TensorProto.BFLOAT16)
        :param shape: input shape
        :param buffer_ptr: memory pointer to input data
        N)r   r  r   r   r   default_memoryr   rP   r   	device_idelement_typeshape
buffer_ptrs          r   r  zIOBinding.bind_inputX  sM     	""KK#K;**,
 
	
r   c                P    | j                   j                  ||j                         y)z\
        :param name: input name
        :param ortvalue: OrtValue instance to bind
        N)r   bind_ortvalue_inputr5   r   rP   ortvalues      r   r   zIOBinding.bind_ortvalue_inputm  s    
 	++D(2D2DEr   c                8    | j                   j                          y r   )r   synchronize_inputsr%   s    r   r$  zIOBinding.synchronize_inputst  s    **,r   Nc           	        |X| j                   j                  |t        j                  t	        ||      t        j                  j                         |             y||t        d      | j                   j                  |t        j                  t	        ||      t        j                  j                         |      |||       y)a  
        :param name: output name
        :param device_type: e.g. cpu, cuda, cann, cpu by default
        :param device_id: device id, e.g. 0
        :param element_type: output element type. It can be either numpy type (like numpy.float32) or an integer for onnx type (like onnx.TensorProto.BFLOAT16)
        :param shape: output shape
        :param buffer_ptr: memory pointer to output data
        NzQ`element_type` and `shape` are to be provided if pre-allocated memory is provided)r   bind_outputr   r   r   r  r^   r  s          r   r&  zIOBinding.bind_outputw  s    0 OO'''Y?KK..0 #u} !tuuOO'''Y?KK..0
 
r   c                P    | j                   j                  ||j                         y)z]
        :param name: output name
        :param ortvalue: OrtValue instance to bind
        N)r   bind_ortvalue_outputr5   r!  s      r   r(  zIOBinding.bind_ortvalue_output  s    
 	,,T83E3EFr   c                8    | j                   j                          y r   )r   synchronize_outputsr%   s    r   r*  zIOBinding.synchronize_outputs  s    ++-r   c                    | j                   j                         }t        |t        j                        st        d      |D cg c]  }t        |       c}S c c}w )z
        Returns the output OrtValues from the Run() that preceded the call.
        The data buffer of the obtained OrtValues may not reside on CPU memory
        z?get_outputs() must return an instance of type 'OrtValueVector'.)r   r{   r\   r   r   r   r<   )r   outputsr"  s      r   r{   zIOBinding.get_outputs  sO    
 //--/'1#3#34]^^3:;7x"7;;;s   Ac                6    | j                   j                         S r   )r   r{   r%   s    r   get_outputs_as_ortvaluevectorz'IOBinding.get_outputs_as_ortvaluevector  s    **,,r   c                6    | j                   j                         S )zCopy output contents to CPU.)r   copy_outputs_to_cpur%   s    r   r0  zIOBinding.copy_outputs_to_cpu  s    2244r   c                8    | j                   j                          y r   )r   clear_binding_inputsr%   s    r   r2  zIOBinding.clear_binding_inputs  s    ,,.r   c                8    | j                   j                          y r   )r   clear_binding_outputsr%   s    r   r4  zIOBinding.clear_binding_outputs  s    --/r   )r  rm   )r
   r   NNN)rC   rD   rE   rF   r   r  r  r   r$  r&  r(  r*  r{   r.  r0  r2  r4  rH   r   r   r   r   C  s\    (
5
*F- .`G.<-5/0r   r   c                      e Zd ZdZddZd Zedd       Zedd       Zeddd       Z	ed        Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)r<   a  
    A data structure that supports all ONNX data formats (tensors and non-tensors) that allows users
    to place the data backing these on a device, for example, on a CUDA supported device.
    This class provides APIs to construct and deal with OrtValues.
    Nc                j    t        |t        j                        r|| _        || _        y t        d      )Nz^`Provided ortvalue` needs to be of type `onnxruntime.capi.onnxruntime_pybind11_state.OrtValue`)r\   r   r<   r5   
_numpy_objr^   )r   r"  	numpy_objs      r   r   zOrtValue.__init__  s2    h

+%DN (DO p r   c                    | j                   S r   )r5   r%   s    r   r   zOrtValue._get_c_value  s    ~~r   c                    t        t        j                   j                  | t        j                  t	        ||      t        j                  j                         |            |j                         dk(  r|       S d      S )a  
        Factory method to construct an OrtValue (which holds a Tensor) from a given Numpy object
        A copy of the data in the Numpy object is held by the OrtValue only if the device is NOT cpu

        :param numpy_obj: The Numpy object to construct the OrtValue from
        :param device_type: e.g. cpu, cuda, cann, cpu by default
        :param device_id: device id, e.g. 0
        r
   N)r<   r   ortvalue_from_numpyr   r   r  lower)r8  r   r  s      r   r;  zOrtValue.ortvalue_from_numpy  st     JJ**'Y?KK..0 %**,5I

 
	
 <@

 
	
r   c                V    t        t        j                   j                  | |      |       S )a
  
        This method creates an instance of OrtValue on top of the numpy array.
        No data copy is made and the lifespan of the resulting OrtValue should never
        exceed the lifespan of bytes object. The API attempts to reinterpret
        the data type which is expected to be the same size. This is useful
        when we want to use an ONNX data type that is not supported by numpy.

        :param data: numpy.ndarray.
        :param onnx_elemenet_type: a valid onnx TensorProto::DataType enum value
        )r<   r   "ortvalue_from_numpy_with_onnx_type)dataonnx_element_types     r   r>  z+OrtValue.ortvalue_from_numpy_with_onnx_type  s$     

EEdL]^`deer   c                   t        |t              ret        t        j                  j	                  | |t        j
                  t        ||      t        j
                  j                         |                  S t        t        j                  j                  | |t        j
                  t        ||      t        j
                  j                         |                  S )a  
        Factory method to construct an OrtValue (which holds a Tensor) from given shape and element_type

        :param shape: List of integers indicating the shape of the OrtValue
        :param element_type: The data type of the elements. It can be either numpy type (like numpy.float32) or an integer for onnx type (like onnx.TensorProto.BFLOAT16).
        :param device_type: e.g. cpu, cuda, cann, cpu by default
        :param device_id: device id, e.g. 0
        )	r\   rA   r<   r   !ortvalue_from_shape_and_onnx_typer   r   r  ortvalue_from_shape_and_type)r  r  r   r  s       r   rC  z%OrtValue.ortvalue_from_shape_and_type  s     lC(

<< KK+KC224!
 
 JJ33'Y?KK..0

 
	
r   c                n    t        t        j                   j                  | j                                     S )z
        The function will construct an OrtValue instance from a valid SparseTensor
        The new instance of OrtValue will assume the ownership of sparse_tensor
        )r<   r   ort_value_from_sparse_tensor_get_c_tensor)sparse_tensors    r   rE  z%OrtValue.ort_value_from_sparse_tensor+  s'     

??@[@[@]^__r   c                H    t        | j                  j                               S )zR
        The function will return SparseTensor contained in this OrtValue
        )SparseTensorr5   as_sparse_tensorr%   s    r   rJ  zOrtValue.as_sparse_tensor3  s     DNN;;=>>r   c                6    | j                   j                         S )zX
        Returns the address of the first element in the OrtValue's data buffer
        )r5   data_ptrr%   s    r   rL  zOrtValue.data_ptr9  s     ~~&&((r   c                R    | j                   j                         j                         S )zn
        Returns the name of the device where the OrtValue's data buffer resides e.g. cpu, cuda, cann
        )r5   device_namer<  r%   s    r   rN  zOrtValue.device_name?  s      ~~))+1133r   c                6    | j                   j                         S )z?
        Returns the shape of the data in the OrtValue
        )r5   r  r%   s    r   r  zOrtValue.shapeE  s     ~~##%%r   c                6    | j                   j                         S )zC
        Returns the data type of the data in the OrtValue
        )r5   	data_typer%   s    r   rQ  zOrtValue.data_typeK       ~~''))r   c                6    | j                   j                         S )zi
        Returns the proto type of the data in the OrtValue
        if the OrtValue is a tensor.
        )r5   r  r%   s    r   r  zOrtValue.element_typeQ  s    
 ~~**,,r   c                6    | j                   j                         S )zz
        Returns True if the OrtValue corresponding to an
        optional type contains data, else returns False
        )r5   	has_valuer%   s    r   rU  zOrtValue.has_valueX  s    
 ~~''))r   c                6    | j                   j                         S )zT
        Returns True if the OrtValue contains a Tensor, else returns False
        )r5   	is_tensorr%   s    r   rW  zOrtValue.is_tensor_  rR  r   c                6    | j                   j                         S )zZ
        Returns True if the OrtValue contains a SparseTensor, else returns False
        )r5   is_sparse_tensorr%   s    r   rY  zOrtValue.is_sparse_tensore  s     ~~..00r   c                6    | j                   j                         S )z]
        Returns True if the OrtValue contains a Tensor Sequence, else returns False
        )r5   is_tensor_sequencer%   s    r   r[  zOrtValue.is_tensor_sequencek  s     ~~0022r   c                6    | j                   j                         S )z
        Returns a Numpy object from the OrtValue.
        Valid only for OrtValues holding Tensors. Throws for OrtValues holding non-Tensors.
        Use accessors to gain a reference to non-Tensor objects such as SparseTensor
        )r5   numpyr%   s    r   r]  zOrtValue.numpyq  s     ~~##%%r   c                :    | j                   j                  |       y)av  
        Update the OrtValue in place with a new Numpy array. The numpy contents
        are copied over to the device memory backing the OrtValue. It can be used
        to update the input valuess for an InferenceSession with CUDA graph
        enabled or other scenarios where the OrtValue needs to be updated while
        the memory address can not be changed.
        N)r5   update_inplace)r   np_arrs     r   r_  zOrtValue.update_inplacey  s     	%%f-r   r   )r
   r   )r@  rA   )r   rO   r  rA   )rC   rD   rE   rF   r   r   rG   r;  r>  rC  rE  rJ  rL  rN  r  rQ  r  rU  rW  rY  r[  r]  r_  rH   r   r   r<   r<     s    
 
 
0 f f "
 "
H ` `?)4&*-**13&.r   r<   c                  8    e Zd ZdZd Zd Zed        Zd Zd Z	y)r   zD
    A data structure that exposes the underlying C++ OrtDevice
    c                \    t        |t        j                        r|| _        yt	        d      )&
        Internal constructor
        z]`Provided object` needs to be of type `onnxruntime.capi.onnxruntime_pybind11_state.OrtDevice`N)r\   r   r   _ort_devicer^   )r   c_ort_devices     r   r   zOrtDevice.__init__  s*     lAKK0+Do r   c                    | j                   S )z8
        Internal accessor to underlying object
        )rd  r%   s    r   _get_c_devicezOrtDevice._get_c_device  s     r   c                    t        t        j                   t        | |      t        j                   j                         |            S r   )r   r   r   r  )ort_device_namer  s     r   makezOrtDevice.make  s8    KK#OY?**,
 	
r   c                6    | j                   j                         S r   )rd  r  r%   s    r   r  zOrtDevice.device_id  s    ))++r   c                6    | j                   j                         S r   )rd  r   r%   s    r   r   zOrtDevice.device_type  s    ++--r   N)
rC   rD   rE   rF   r   rg  rG   rj  r  r   rH   r   r   r   r     s/    	  
 
,.r   r   c                  r    e Zd ZdZd Zd Zed        Zed        Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zy)rI  z
    A data structure that project the C++ SparseTensor object
    The class provides API to work with the object.
    Depending on the format, the class will hold more than one buffer
    depending on the format
    c                \    t        |t        j                        r|| _        yt	        d      )rc  z``Provided object` needs to be of type `onnxruntime.capi.onnxruntime_pybind11_state.SparseTensor`N)r\   r   rI  _tensorr^   )r   rG  s     r   r   zSparseTensor.__init__  s+     mQ^^4(DL r r   c                    | j                   S r   )ro  r%   s    r   rF  zSparseTensor._get_c_tensor  s    ||r   c           	     t    t        t        j                   j                  | |||j                                     S )a  
        Factory method to construct a SparseTensor in COO format from given arguments

        :param dense_shape: 1-D  numpy array(int64) or a python list that contains a dense_shape of the sparse tensor
            must be on cpu memory
        :param values: a homogeneous, contiguous 1-D numpy array that contains non-zero elements of the tensor
            of a type.
        :param coo_indices:  contiguous numpy array(int64) that contains COO indices for the tensor. coo_indices may
            have a 1-D shape when it contains a linear index of non-zero values and its length must be equal to
            that of the values. It can also be of 2-D shape, in which has it contains pairs of coordinates for
            each of the nnz values and its length must be exactly twice of the values length.
        :param ort_device: - describes the backing memory owned by the supplied nummpy arrays. Only CPU memory is
            suppored for non-numeric data types.

        For primitive types, the method will map values and coo_indices arrays into native memory and will use
        them as backing storage. It will increment the reference count for numpy arrays and it will decrement it
        on GC. The buffers may reside in any storage either CPU or GPU.
        For strings and objects, it will create a copy of the arrays in CPU memory as ORT does not support those
        on other devices and their memory can not be mapped.
        )rI  r   sparse_coo_from_numpyrg  )dense_shaperf   coo_indices
ort_devices       r   rr  z"SparseTensor.sparse_coo_from_numpy  s3    , NN00fkS]SkSkSmn
 	
r   c           
     v    t        t        j                   j                  | ||||j                                     S )a`  
        Factory method to construct a SparseTensor in CSR format from given arguments

        :param dense_shape: 1-D numpy array(int64) or a python list that contains a dense_shape of the
            sparse tensor (rows, cols) must be on cpu memory
        :param values: a  contiguous, homogeneous 1-D numpy array that contains non-zero elements of the tensor
            of a type.
        :param inner_indices:  contiguous 1-D numpy array(int64) that contains CSR inner indices for the tensor.
            Its length must be equal to that of the values.
        :param outer_indices:  contiguous 1-D numpy array(int64) that contains CSR outer indices for the tensor.
            Its length must be equal to the number of rows + 1.
        :param ort_device: - describes the backing memory owned by the supplied nummpy arrays. Only CPU memory is
            suppored for non-numeric data types.

        For primitive types, the method will map values and indices arrays into native memory and will use them as
        backing storage. It will increment the reference count and it will decrement then count when it is GCed.
        The buffers may reside in any storage either CPU or GPU.
        For strings and objects, it will create a copy of the arrays in CPU memory as ORT does not support those
        on other devices and their memory can not be mapped.
        )rI  r   sparse_csr_from_numpyrg  )rs  rf   inner_indicesouter_indicesru  s        r   rw  z"SparseTensor.sparse_csr_from_numpy  s;    , NN00((*
 	
r   c                6    | j                   j                         S )z
        The method returns a numpy array that is backed by the native memory
        if the data type is numeric. Otherwise, the returned numpy array that contains
        copies of the strings.
        )ro  rf   r%   s    r   rf   zSparseTensor.values  s     ||""$$r   c                6    | j                   j                         S )a}  
        The method will return coo representation of the sparse tensor which will enable
        querying COO indices. If the instance did not contain COO format, it would throw.
        You can query coo indices as:

        ::

            coo_indices = sparse_tensor.as_coo_view().indices()

        which will return a numpy array that is backed by the native memory.
        )ro  get_coo_datar%   s    r   as_coo_viewzSparseTensor.as_coo_view  s     ||((**r   c                6    | j                   j                         S )a  
        The method will return CSR(C) representation of the sparse tensor which will enable
        querying CRS(C) indices. If the instance dit not contain CSR(C) format, it would throw.
        You can query indices as:

        ::

            inner_ndices = sparse_tensor.as_csrc_view().inner()
            outer_ndices = sparse_tensor.as_csrc_view().outer()

        returning numpy arrays backed by the native memory.
        )ro  get_csrc_datar%   s    r   as_csrc_viewzSparseTensor.as_csrc_view  s     ||))++r   c                6    | j                   j                         S )a  
        The method will return coo representation of the sparse tensor which will enable
        querying BlockSparse indices. If the instance did not contain BlockSparse format, it would throw.
        You can query coo indices as:

        ::

            block_sparse_indices = sparse_tensor.as_blocksparse_view().indices()

        which will return a numpy array that is backed by the native memory
        )ro  get_blocksparse_datar%   s    r   as_blocksparse_viewz SparseTensor.as_blocksparse_view!  s     ||0022r   c                f    t        | j                  j                  |j                                     S )a  
        Returns a copy of this instance on the specified cuda device

        :param ort_device: with name 'cuda' and valid gpu device id

        The method will throw if:

        - this instance contains strings
        - this instance is already on GPU. Cross GPU copy is not supported
        - CUDA is not present in this build
        - if the specified device is not valid
        )rI  ro  to_cudarg  )r   ru  s     r   r  zSparseTensor.to_cuda/  s'     DLL001I1I1KLMMr   c                .    | j                   j                  S )z7
        Returns a OrtSparseFormat enumeration
        )ro  rM   r%   s    r   rM   zSparseTensor.format>  s     ||"""r   c                6    | j                   j                         S )zZ
        Returns a numpy array(int64) containing a dense shape of a sparse tensor
        )ro  rs  r%   s    r   rs  zSparseTensor.dense_shapeD  s     ||''))r   c                6    | j                   j                         S )zH
        Returns a string data type of the data in the OrtValue
        )ro  rQ  r%   s    r   rQ  zSparseTensor.data_typeJ  s     ||%%''r   c                R    | j                   j                         j                         S )zj
        Returns the name of the device where the SparseTensor data buffers reside e.g. cpu, cuda
        )ro  rN  r<  r%   s    r   rN  zSparseTensor.device_nameP  s      ||'')//11r   N)rC   rD   rE   rF   r   rF  rG   rr  rw  rf   r}  r  r  r  rM   rs  rQ  rN  rH   r   r   rI  rI    sg    
 
 
2 
 
>%+,3N#*(2r   rI  )r   rO   r>   zC.OrtDevice)rg   r  rh   r  rU   zSequence[str])
__future__r   rZ   collections.abcr   typingrK   r   r   onnxruntime.capir   r   TYPE_CHECKINGonnxruntimer   r   rk   rm   r   r   r<   r   rI  rH   r   r   <module>r     s   
 #   	     /	C+M +M\OZ@OZ5OZ ,OZdip ipXq\w q\hC0 C0Lx. x.v$. $.Ni2 i2r   