# backends/onnx

Handler file for choosing the correct version of ONNX Runtime, based on the environment.
Ideally, we could import the `onnxruntime-web` and `onnxruntime-node` packages only when needed,
but dynamic imports don't seem to work with the current webpack version and/or configuration.
This is possibly due to the experimental nature of top-level await statements.
So, we just import both packages, and use the appropriate one based on the environment:
  - When running in node, we use `onnxruntime-node`.
  - When running in the browser, we use `onnxruntime-web` (`onnxruntime-node` is not bundled).

This module is not directly exported, but can be accessed through the environment variables:
```javascript
import { env } from '@huggingface/transformers';
console.log(env.backends.onnx);
```

* [backends/onnx](#module_backends/onnx)
    * _static_
        * [`.deviceToExecutionProviders([device])`](#module_backends/onnx.deviceToExecutionProviders) ⇒ Array.&lt;ONNXExecutionProviders&gt;
        * [`.createInferenceSession(buffer_or_path, session_options, session_config)`](#module_backends/onnx.createInferenceSession) ⇒ *
        * [`.runInferenceSession(session, ortFeed)`](#module_backends/onnx.runInferenceSession) ⇒ *
        * [`.isONNXTensor(x)`](#module_backends/onnx.isONNXTensor) ⇒ boolean
        * [`.isONNXProxy()`](#module_backends/onnx.isONNXProxy) ⇒ boolean
    * _inner_
        * [`~defaultDevices`](#module_backends/onnx..defaultDevices) : Array.&lt;ONNXExecutionProviders&gt;
        * [`~wasmInitPromise`](#module_backends/onnx..wasmInitPromise) : Promise&lt;any&gt; | null
        * [`~webInferenceChain`](#module_backends/onnx..webInferenceChain) : Promise.&lt;any&gt;
        * [`~DEVICE_TO_EXECUTION_PROVIDER_MAPPING`](#module_backends/onnx..DEVICE_TO_EXECUTION_PROVIDER_MAPPING) : *
        * [`~supportedDevices`](#module_backends/onnx..supportedDevices) : *
        * [`~ONNX_ENV`](#module_backends/onnx..ONNX_ENV) : *
        * [`~ONNXExecutionProviders`](#module_backends/onnx..ONNXExecutionProviders) : *

* * *

## `backends/onnx.deviceToExecutionProviders([device])` ⇒ Array.&lt;ONNXExecutionProviders&gt;

Map a device to the execution providers to use for the given device.

**Kind**: static method of [backends/onnx](#module_backends/onnx)  
**Returns**: Array.&lt;ONNXExecutionProviders&gt; - The execution providers to use for the given device.  

  
    
      ParamTypeDefaultDescription
    
  
  

    [device]*(Optional) The device to run the inference on.

      

* * *

## `backends/onnx.createInferenceSession(buffer_or_path, session_options, session_config)` ⇒ *

Create an ONNX inference session.

**Kind**: static method of [backends/onnx](#module_backends/onnx)  
**Returns**: * - The ONNX inference session.  

  
    
      ParamTypeDescription
    
  
  

    buffer_or_pathUint8Array | stringThe ONNX model buffer or path.

    
    session_options*ONNX inference session options.

    
    session_configObjectONNX inference session configuration.

      

* * *

## `backends/onnx.runInferenceSession(session, ortFeed)` ⇒ *

Run an inference session.

**Kind**: static method of [backends/onnx](#module_backends/onnx)  
**Returns**: * - The output tensors.  

  
    
      ParamTypeDescription
    
  
  

    session*The ONNX inference session.

    
    ortFeed*The input tensors.

      

* * *

## `backends/onnx.isONNXTensor(x)` ⇒ boolean

Check if an object is an ONNX tensor.

**Kind**: static method of [backends/onnx](#module_backends/onnx)  
**Returns**: boolean - Whether the object is an ONNX tensor.  

  
    
      ParamTypeDescription
    
  
  

    xanyThe object to check

      

* * *

## `backends/onnx.isONNXProxy()` ⇒ boolean

Check if ONNX's WASM backend is being proxied.

**Kind**: static method of [backends/onnx](#module_backends/onnx)  
**Returns**: boolean - Whether ONNX's WASM backend is being proxied.  

* * *

## `backends/onnx~defaultDevices` : Array.&lt;ONNXExecutionProviders&gt;

**Kind**: inner property of [backends/onnx](#module_backends/onnx)  

* * *

## `backends/onnx~wasmInitPromise` : Promise&lt;any&gt; | null

To prevent multiple calls to `initWasm()`, we store the first call in a Promise
that is resolved when the first InferenceSession is created. Subsequent calls
will wait for this Promise to resolve before creating their own InferenceSession.

**Kind**: inner property of [backends/onnx](#module_backends/onnx)  

* * *

## `backends/onnx~webInferenceChain` : Promise.&lt;any&gt;

Currently, Transformers.js doesn't support simultaneous execution of sessions in WASM/WebGPU.
For this reason, we need to chain the inference calls (otherwise we get "Error: Session already started").

**Kind**: inner property of [backends/onnx](#module_backends/onnx)  

* * *

## `backends/onnx~DEVICE_TO_EXECUTION_PROVIDER_MAPPING` : *

**Kind**: inner constant of [backends/onnx](#module_backends/onnx)  

* * *

## `backends/onnx~supportedDevices` : *

The list of supported devices, sorted by priority/performance.

**Kind**: inner constant of [backends/onnx](#module_backends/onnx)  

* * *

## `backends/onnx~ONNX_ENV` : *

**Kind**: inner constant of [backends/onnx](#module_backends/onnx)  

* * *

## `backends/onnx~ONNXExecutionProviders` : *

**Kind**: inner typedef of [backends/onnx](#module_backends/onnx)  

* * *

