| Title: | Bindings to 'ONNX' Runtime |
|---|---|
| Description: | Provides native access to the 'Open Neural Network Exchange' (ONNX) Runtime <https://onnxruntime.ai/>, which is a performant engine for running machine learning models that are saved to a standardized format. Rather than interfacing with 'ONNX' via 'Python', as in the official 'onnx' package, 'onnxr' directly interfaces with the runtime's 'C++' API via 'cpp11'. Models saved to '.onnx' files can be loaded and run on various backends, including CPUs and Apple's 'CoreML' library. |
| Authors: | Cory McCartan [aut, cre, cph], Caleb Carr [cph] (Author of 'nativeORT' package that is the basis for this package), Microsoft Corporation [cph] (Copyright holder of src/onnxruntime headers) |
| Maintainer: | Cory McCartan <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-06-08 22:40:20 UTC |
| Source: | https://github.com/cran/onnxr |
Searches for the ONNX Runtime shared library in standard locations:
the ORT_ROOT environment variable, common system library paths,
the per-user install from onnx_install(), the Python onnxruntime
package, and pkg-config.
onnx_find_lib()onnx_find_lib()
Full path to the shared library, or NULL if not found.
onnx_find_lib()onnx_find_lib()
Downloads pre-built ONNX Runtime binaries for the current platform and installs them to a per-user data directory. The library is loaded immediately after installation, so there is no need to restart R.
onnx_install(cuda = NULL)onnx_install(cuda = NULL)
cuda |
Whether to install the CUDA-enabled build for GPU acceleration.
|
Invisibly, the path to the installation directory.
Check whether ONNX Runtime is available
onnx_is_installed()onnx_is_installed()
TRUE if the ONNX Runtime shared library can be found
in any of the standard search locations, FALSE otherwise.
onnx_is_installed()onnx_is_installed()
Check whether ONNX Runtime is loaded
TRUE if the ONNX Runtime shared library has been loaded
in the current R session, FALSE otherwise.
onnx_is_loaded()onnx_is_loaded()
Loads an .onnx model file and creates a model object.
onnx_model( path, backend = c("cpu", "coreml", "cuda", "xnnpack", "openvino"), cache_dir = tools::R_user_dir("onnxr", "cache"), threads = 1L, opt_level = 99L )onnx_model( path, backend = c("cpu", "coreml", "cuda", "xnnpack", "openvino"), cache_dir = tools::R_user_dir("onnxr", "cache"), threads = 1L, opt_level = 99L )
path |
Path to an |
backend |
Execution backend. Available options depend on the platform and ORT build:
|
cache_dir |
Optional directory for CoreML model cache. Set to |
threads |
Number of threads. |
opt_level |
Graph optimization level. |
An "onnx_model" object (a named list) with model metadata
and internal pointers used by onnx_run().
model_path <- system.file("extdata", "lm_iris.onnx", package = "onnxr") if (onnx_is_loaded() && nzchar(model_path)) { sess <- onnx_model(model_path) sess }model_path <- system.file("extdata", "lm_iris.onnx", package = "onnxr") if (onnx_is_loaded() && nzchar(model_path)) { sess <- onnx_model(model_path) sess }
Passes input through the model and returns the results. For models
with a single output, returns the output array directly. For models
with multiple outputs, returns a named list of arrays.
onnx_run(model, ..., simplify = FALSE)onnx_run(model, ..., simplify = FALSE)
model |
An |
... |
Input arrays, either as unnamed arguments (matched to model inputs by position) or as named arguments (matched by name). Each input must be a numeric or integer matrix/array with dimensions matching the model's expected input shape. For single-input models, a single array can be passed directly. |
simplify |
If |
Handles conversion between R's column-major arrays and ONNX's row-major tensors, and between R's numeric types and the model's declared element types (float, double, int32, int64, bool). Note that int64 outputs are cast to doubles, which may lose precision for large integers.
A named list of output arrays, or (if simplify = TRUE and
the model has a single output) the output array directly.
model_path <- system.file("extdata", "lm_iris.onnx", package = "onnxr") if (onnx_is_loaded() && nzchar(model_path)) { sess <- onnx_model(model_path) input <- as.matrix(iris[1:5, c("Sepal.Length", "Sepal.Width", "Petal.Length")]) onnx_run(sess, input) }model_path <- system.file("extdata", "lm_iris.onnx", package = "onnxr") if (onnx_is_loaded() && nzchar(model_path)) { sess <- onnx_model(model_path) input <- as.matrix(iris[1:5, c("Sepal.Length", "Sepal.Width", "Petal.Length")]) onnx_run(sess, input) }