onnxruntime-napi
Introduction
onnxruntime-napi is an ONNX Runtime NAPI interface library. This library is adapted from ONNX Runtime to run on OpenHarmony, reusing its existing APIs and features. The main functionality of onnxruntime is implemented natively and exposed to ArkTS.
Installation
ohpm install @ohos/onnxruntime
For more details about OpenHarmony ohpm environment configuration, please refer to How to Install OpenHarmony ohpm Package.
Constraints
Compatibility
Verified on the following versions:
- DevEco Studio 6.0.1 Release, SDK: API12
Build & Run
This project depends on the onnxruntime library. The compiled .a files and header files need to be copied to the thirdparty directory under the cpp directory.

Manual Compilation of tpc_c_cplusplus onnxruntime C Library
Refer to onnxruntime Build Script.
After compilation, copy the folders under the tpc_c_cplusplus/lycium/usr directory to the thirdparty directory under the cpp directory, as shown below:

Build Using prebuild.sh Script (Requires Linux Environment)
- Modify the
prebuild.shscript and setSDK_DIRto your SDK directory path, e.g.,/home/OHOS_SDK/linux

- Execute the
prebuild.shscript:
./prebuild.sh
The script will automatically download the tpc_c_cplusplus library, compile and generate the onnxruntime library, and copy it to the thirdparty directory under the cpp directory.
As shown below:

Usage Example
The following example uses a SIGMOID benchmark test to demonstrate the main inference process of the onnxruntime napi library:
- Initialize the onnxruntime global environment
- Load the model
- Construct input tensors
- Run inference
- Parse inference results
- Release resources
import * as api from '@ohos/onnxruntime';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
private context: Context = this.getUIContext().getHostContext()!;
private filesDir: string = this.context.filesDir;
private resourceDir: string = this.context.resourceDir;
@State modelPath: string = this.resourceDir + '/sigmoid.ort';
build() {
Column() {
Button("Run ONNX Runtime Test")
.margin({ bottom: 10 })
.onClick(() => {
this.runOnnxRuntimeTest();
})
}
.width('100%')
.height('100%')
}
private runOnnxRuntimeTest(): void {
try {
// 1. Initialize environment and get version
api.OnnxEnvironment.InitEnvironment();
const version: string = api.OnnxEnvironment.GetVersion();
console.info(`ONNX Runtime Version: ${version}`);
// 2. Create inference session
const session: api.OnnxSession =
api.OnnxEnvironment.CreateSessionFromModelFile(this.modelPath);
const inputNames: string[] = session.GetInputNames();
const outputNames: string[] = session.GetOutputNames();
console.info(`Input nodes: ${JSON.stringify(inputNames)}`);
console.info(`Output nodes: ${JSON.stringify(outputNames)}`);
// 3. Prepare input data
const shape: number[] = [3, 4, 5];
const totalElements: number = 60;
const testdataBuffer: ArrayBuffer = new ArrayBuffer(totalElements * 4);
const expectedBuffer: ArrayBuffer = new ArrayBuffer(totalElements * 4);
const testdataView: Float32Array = new Float32Array(testdataBuffer);
const expectedView: Float32Array = new Float32Array(expectedBuffer);
let idx: number = 0;
for (let i: number = 0; i < 3; i++) {
for (let j: number = 0; j < 4; j++) {
for (let k: number = 0; k < 5; k++) {
const x: number = i + j + k;
testdataView[idx] = x;
expectedView[idx] = 1.0 / (1.0 + Math.exp(-x));
idx++;
}
}
}
const inputTensor: api.OnnxTensor =
new api.OnnxTensor(testdataBuffer, shape, api.OnnxArkTSType.FLOAT);
const inputs: Record<string, api.OnnxValue> = {};
inputs[inputNames[0]] = inputTensor;
// 4. Run inference
const result: api.OnnxResult = session.Run(inputs);
const value: api.OnnxValue = result.GetValueByName(outputNames[0]);
// 5. Validate output type
if (value.GetType() !== api.OnnxValueType.TENSOR) {
console.error('Output type error, expected TENSOR');
return;
}
// 6. Accuracy validation
const tensorValue: api.OnnxTensor = value as api.OnnxTensor;
const buffer: ArrayBuffer = tensorValue.GetDataAsArrayBuffer();
const bufferView: Float32Array = new Float32Array(buffer);
if (bufferView.length !== expectedView.length) {
console.error(`Output length mismatch: actual ${bufferView.length}, expected ${expectedView.length}`);
return;
}
let failCount: number = 0;
const epsilon: number = 1e-6;
for (let i: number = 0; i < bufferView.length; i++) {
if (Math.abs(bufferView[i] - expectedView[i]) > epsilon) {
failCount++;
}
}
if (failCount === 0) {
console.info('ONNX Runtime test passed, all output values within tolerance');
} else {
console.error(`Accuracy validation failed: ${failCount} elements exceed tolerance`);
}
// 7. Release resources (explicit release recommended to avoid high memory usage)
result.Close()
inputTensor.Close()
session.Close()
} catch (err) {
console.error(`ONNX Runtime test failed: ${err.message}`);
}
}
}
Usage Guide
A complete onnxruntime inference process requires all stages, but each stage has configurable parameters.
Initialize onnxruntime Global Environment
Before performing any model loading or inference operations, you must initialize the ONNX Runtime global environment. The environment manages shared resources such as log output and global thread pools.
Note: The global environment only needs to be initialized once.
The simplest initialization is without any parameters:
import * as api from '@ohos/onnxruntime';
// Initialize environment with default configuration
api.OnnxEnvironment.InitEnvironment();
const version = api.OnnxEnvironment.GetVersion();
console.info(`ONNX Runtime Version: ${version}`);
For more fine-grained control, the following overloaded methods are available:
- InitEnvironmentWithName(envName: string) — Specify an environment name for log identification.
- InitEnvironmentWithLogLevel(logLevel: OnnxLoggingLevel) — Set the global log level.
- InitEnvironmentWithNameAndLogLevel(envName: string, logLevel: OnnxLoggingLevel) — Specify both name and log level.
- InitEnvironmentFull(envName: string, logLevel: OnnxLoggingLevel, threadOptions: OnnxThreadOptions) — Full initialization with configurable global thread behavior.
Thread configuration (OnnxThreadOptions) allows setting inter-op parallelism, intra-op parallelism, and spin control strategy. Typical usage:
const threadOptions = new api.OnnxThreadOptions();
threadOptions.SetGlobalInterOpNumThreads(4);
threadOptions.SetGlobalIntraOpNumThreads(2);
threadOptions.SetGlobalSpinControl(true);
threadOptions.SetGlobalDenormalAsZero();
api.OnnxEnvironment.InitEnvironmentFull("MyApp", api.OnnxLoggingLevel.WARNING, threadOptions);
threadOptions.Close(); // Close the configuration object after use
Log level enum OnnxLoggingLevel includes VERBOSE, INFO, WARNING, ERROR, FATAL. It is recommended to use VERBOSE during development and adjust to ERROR for release.
Load Model
After environment initialization, you can create inference sessions through the environment object. OnnxEnvironment provides three model loading methods:
- Load from file — Suitable for models stored as
.onnxor.ortfiles on the device. - Load from memory buffer — Suitable for models packed in the app's rawfile or downloaded from the network into memory.
- Load from numeric array — Convert model bytes to
number[]and pass them in; internally converted toInt8Array.
Each method provides a basic version and a version with configuration options (...WithOptions).
Basic loading examples:
// Load from file
const session = api.OnnxEnvironment.CreateSessionFromModelFile("/data/model.onnx");
// Load from memory buffer (assuming buffer is Int8Array)
const session2 = api.OnnxEnvironment.CreateSessionFromBuffer(modelBuffer);
Loading with session options:
OnnxSessionOptions provides rich configuration items for controlling model optimization, execution mode, thread count, etc.
const options = new api.OnnxSessionOptions();
options.SetOptimizationLevel(api.OnnxOptimizationLevel.ALL); // Enable all optimizations
options.SetExecutionMode(api.OnnxExecutionMode.PARALLEL); // Parallel execution
options.SetIntraOpNumThreads(2); // Intra-op thread count
options.SetInterOpNumThreads(1); // Inter-op thread count
options.SetMemoryPatternOptimizationEnabled(true); // Memory pattern optimization
options.EnableProfiling("profile_prefix"); // Enable profiling
const session = api.OnnxEnvironment.CreateSessionFromModelFileWithOptions(
"/data/model.onnx", options
);
options.Close();
OnnxOptimizationLevel values:
- DISABLED — No optimization
- BASIC — Basic optimization (constant folding, etc.)
- EXTENDED — Extended optimization (operator fusion, etc.)
- LAYOUT — Layout optimization
- ALL — Enable all available optimizations (recommended for production)
Other common settings:
- SetCPUArenaAllocatorEnabled(true) — Enable Arena memory allocator to reduce memory fragmentation.
- SetDeterministicComputeEnabled(true) — Force deterministic computation.
- AddCPUExecutionProvider(useArena) — Add CPU EP backend executor.
- AddExecutionProvider — Register other hardware backends. Currently no other supported EP backends on OpenHarmony; this is a reserved extension interface.
- SetSymbolicDimensionValue("batch_size", 1n) — Fix dynamic dimensions, e.g., batch size.
- RegisterCustomOpLibrary(path) — Load custom operator dynamic library.
After loading, you can view model metadata (version, producer, description, etc.) via session.GetModelMetadata().
Construct Input Tensors
Before running inference, application data needs to be wrapped into subclasses of OnnxValue. The most commonly used type is the dense tensor OnnxTensor.
Get input information
Query the model's input names, shapes, and data types through the session to prepare buffers of the correct size.
const inputInfo = session.GetInputInfo(); // Record<string, NodeInfo>
const firstInputName = Object.keys(inputInfo)[0];
const nodeInfo = inputInfo[firstInputName];
const tensorInfo = nodeInfo.GetInfo() as api.TensorInfo;
const shape = tensorInfo.GetShape(); // e.g., [1, 3, 224, 224]
const elementType = tensorInfo.arktsElementType; // Corresponding ArkTS data type
Create tensors:
- Numeric tensor — Pass an ArrayBuffer, shape, and OnnxArkTSType enum value.
const buffer = new ArrayBuffer(60 * 4); // Assuming 60 floats
const dataView = new Float32Array(buffer);
// Fill dataView ...
const tensor = new api.OnnxTensor(buffer, [3, 4, 5], api.OnnxArkTSType.FLOAT);
OnnxArkTSType defines FLOAT, DOUBLE, INT8, UINT8, INT16, INT32, INT64, BOOL, STRING, etc., corresponding to ArkTS TypedArray types.
- String tensor — Pass a string array and shape directly.
const stringTensor = new api.OnnxTensor(["hello", "world"], [2]);
Build input map, with keys as input node names and values as tensor objects:
const inputs: Record<string, api.OnnxValue> = {};
inputs[firstInputName] = tensor;
If the model input includes non-tensor types (such as OnnxMap or OnnxSequence), they can also be created via corresponding constructors and placed into the map.
Run Inference
After preparing the inputs, call the session's Run series methods to trigger inference computation.
Basic call:
const result: api.OnnxResult = session.Run(inputs);
The returned OnnxResult contains all model outputs.
Advanced calls:
- RunWithOptions(inputs, runOptions) — Pass OnnxRunOptions to override log level, add configuration items, bind LoRA adapters, etc.
- RunWithSpecifiedOutputs(inputs, outputNames) — Only compute specified output nodes, reducing unnecessary computation.
- RunWithPinnedOutputs(inputs, pinnedOutputs) — Reuse pre-allocated buffers to reduce memory allocation overhead.
- RunFull(...) — All parameters optional, providing maximum flexibility.
OnnxRunOptions usage example:
const runOpts = new api.OnnxRunOptions();
runOpts.SetLogLevel(api.OnnxLoggingLevel.WARNING);
runOpts.SetRunTag("inference_1");
const result = session.RunWithOptions(inputs, runOpts);
runOpts.Close();
Specified output example (compute only required nodes):
const outputNames = ["output_0"];
const result = session.RunWithSpecifiedOutputs(inputs, outputNames);
LoRA adapter inference:
const adapter = new api.OnnxLoraAdapter("/path/to/adapter.onnx_adapter");
const runOpts = new api.OnnxRunOptions();
runOpts.AddActiveLoraAdapter(adapter);
const result = session.RunWithOptions(inputs, runOpts);
// Release resources
result.Close();
runOpts.Close();
adapter.Close();
If profiling is enabled (via OnnxSessionOptions), you can get the profiling file path after inference via session.EndProfiling().
Parse Inference Results
OnnxResult provides the ability to access output values by index or by name. In most cases, the output type is OnnxTensor.
Get output values:
const outputNames = session.GetOutputNames();
const value: api.OnnxValue = result.GetValueByName(outputNames[0]);
// Type check
if (value.GetType() !== api.OnnxValueType.TENSOR) {
console.error('Non-tensor output');
}
const outputTensor = value as api.OnnxTensor;
Read tensor data:
- Numeric data:
GetDataAsArrayBuffer()returns the raw data buffer; read with the corresponding TypedArray view. - String data:
GetDataAsStringArray()returns a string array.
const outBuffer = outputTensor.GetDataAsArrayBuffer();
const outData = new Float32Array(outBuffer); // Assuming FLOAT output type
console.info(`First 5 output values: ${outData.slice(0, 5)}`);
Other output types:
- OnnxMap — Read via
GetStringKeys()/GetLongKeys()and corresponding value getter methods. - OnnxSequence — Get element arrays via
GetTensors()orGetMaps().
Release Resources
Inference results, tensors, and sessions should call Close() promptly after use to release Native resources. Although ArkTS objects may trigger automatic collection when they go out of scope, it is recommended to always release explicitly, especially when handling large amounts of data or in loop inference scenarios.
result.Close();
inputTensor.Close();
session.Close();
Through the above five steps, a complete ONNX model inference workflow can be accomplished. Each stage provides flexible configuration interfaces that can be customized based on performance, precision, and functional requirements.
API Reference
The onnxruntime library provides the following core classes for application developers, covering the full workflow including environment management, session creation, tensor construction, inference execution, and result parsing.
Enums
| Enum Name | Description | Values |
|---|---|---|
| OnnxLoggingLevel | Log level | VERBOSE, INFO, WARNING, ERROR, FATAL |
| OnnxExecutionMode | Graph execution mode | SEQUENTIAL, PARALLEL |
| OnnxOptimizationLevel | Graph optimization level | DISABLED, BASIC, EXTENDED, LAYOUT, ALL |
| OnnxValueType | ONNX value type category | UNDEFINED, TENSOR, SEQUENCE, MAP, SPARSE_TENSOR |
| OnnxArkTSType | ArkTS native data types | FLOAT, DOUBLE, INT8, UINT8, INT16, INT32, INT64, BOOL, STRING, FLOAT16, BFLOAT16, UNKNOWN |
| OnnxTensorType | ONNX tensor element types | Similar to OnnxArkTSType but includes more numeric types such as COMPLEX64 |
| OnnxMapValueType | Map key-value data types | INVALID, STRING, LONG, FLOAT, DOUBLE |
| SparseTensorFormat | Sparse tensor storage format | UNDEFINED, COO, CSRC, BLOCK_SPARSE |
OnnxRuntime (Global Runtime Info)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| Version() | Get ONNX Runtime version string | None | string, e.g., "1.16.0" |
| IsCUDAAvailable() | Whether CUDA GPU backend is supported | None | boolean |
| IsROCMAvailable() | Whether ROCm AMD GPU backend is supported | None | boolean |
| IsDNNLAvailable() | Whether oneDNN backend is supported | None | boolean |
| IsOpenVINOAvailable() | Whether OpenVINO backend is supported | None | boolean |
| IsTensorRTAvailable() | Whether TensorRT backend is supported | None | boolean |
| IsQNNAvailable() | Whether QNN Qualcomm backend is supported | None | boolean |
OnnxThreadOptions (Global Thread Pool Configuration)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| Constructor OnnxThreadOptions() | Create thread configuration object | None | OnnxThreadOptions |
| Close() | Release underlying resources | None | None |
| SetGlobalInterOpNumThreads(numThreads) | Set inter-op parallel thread count | numThreads: number, recommended as physical core count | None |
| SetGlobalIntraOpNumThreads(numThreads) | Set intra-op parallel thread count | numThreads: number | None |
| SetGlobalSpinControl(enable) | Set spin-wait strategy | enable: boolean | None |
| SetGlobalDenormalAsZero() | Flush denormalized floats to zero | None | None |
OnnxEnvironment (Environment Management)
Note: The global environment needs to be initialized only once, after which sessions can be created through this object.
| API | Description | Parameters | Return Value |
|---|---|---|---|
| InitEnvironment() | Default environment initialization | None | None |
| InitEnvironmentWithName(envName) | Initialize with custom name | envName: string | None |
| InitEnvironmentWithLogLevel(logLevel) | Initialize with specified log level | logLevel: OnnxLoggingLevel | None |
| InitEnvironmentWithNameAndLogLevel(envName, logLevel) | Specify both name and log level | envName: string, logLevel: OnnxLoggingLevel | None |
| InitEnvironmentFull(envName, logLevel, threadOptions) | Full parameter initialization | envName: string, logLevel: OnnxLoggingLevel, threadOptions: OnnxThreadOptions | None |
| CreateSessionFromModelFile(modelPath) | Create inference session from model file | modelPath: string, model file path | OnnxSession |
| CreateSessionFromModelFileWithOptions(modelPath, sessionOptions) | Create session from file (with options) | modelPath: string, sessionOptions: OnnxSessionOptions | OnnxSession |
| CreateSessionFromBuffer(modelBuffer) | Create session from memory buffer | modelBuffer: Int8Array | OnnxSession |
| CreateSessionFromBufferWithOptions(modelBuffer, sessionOptions) | Create session from buffer (with options) | modelBuffer: Int8Array, sessionOptions: OnnxSessionOptions | OnnxSession |
| CreateSessionFromArray(modelArray) | Create session from numeric array | modelArray: number[] | OnnxSession |
| CreateSessionFromArrayWithOptions(modelArray, sessionOptions) | Create session from array (with options) | modelArray: number[], sessionOptions: OnnxSessionOptions | OnnxSession |
| GetVersion() | Get ONNX Runtime version | None | string |
OnnxSessionOptions (Session Configuration)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| Constructor OnnxSessionOptions() | Create session options object | None | OnnxSessionOptions |
| Close() | Release underlying resources | None | None |
| SetOptimizationLevel(level) | Set graph optimization level | level: OnnxOptimizationLevel | None |
| SetExecutionMode(mode) | Set graph execution mode | mode: OnnxExecutionMode | None |
| SetIntraOpNumThreads(numThreads) | Set intra-op thread count | numThreads: number | None |
| SetInterOpNumThreads(numThreads) | Set inter-op thread count | numThreads: number | None |
| SetMemoryPatternOptimizationEnabled(enabled) | Enable memory pattern optimization | enabled: boolean | None |
| SetCPUArenaAllocatorEnabled(enabled) | Enable Arena memory allocator | enabled: boolean | None |
| SetDeterministicComputeEnabled(enabled) | Force deterministic computation | enabled: boolean | None |
| EnableProfiling(prefix) | Enable profiling | prefix: string | None |
| AddCPUExecutionProvider(useArena) | Add CPU EP backend executor | useArena: boolean | None |
| AddExecutionProvider(providerName) | Register other hardware backends | providerName: string | None |
| SetSymbolicDimensionValue(name, value) | Fix dynamic dimension value | name: string, value: bigint | None |
| RegisterCustomOpLibrary(path) | Load custom operator dynamic library | path: string | None |
OnnxSession (Inference Session)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| GetInputNames() | Get model input node names | None | string[] |
| GetOutputNames() | Get model output node names | None | string[] |
| GetInputInfo() | Get input node detail info | None | Record<string, NodeInfo> |
| GetOutputInfo() | Get output node detail info | None | Record<string, NodeInfo> |
| GetModelMetadata() | Get model metadata | None | OnnxModelMetadata |
| Run(inputs) | Run inference | inputs: Record<string, OnnxValue> | OnnxResult |
| RunWithOptions(inputs, runOptions) | Run inference with run options | inputs: Record<string, OnnxValue>, runOptions: OnnxRunOptions | OnnxResult |
| RunWithSpecifiedOutputs(inputs, outputNames) | Run with specified output nodes | inputs: Record<string, OnnxValue>, outputNames: string[] | OnnxResult |
| RunWithPinnedOutputs(inputs, pinnedOutputs) | Run with pre-allocated output buffers | inputs: Record<string, OnnxValue>, pinnedOutputs: Record<string, OnnxTensor> | OnnxResult |
| RunFull(inputs, runOptions, outputNames, pinnedOutputs) | Run with full parameters | All optional | OnnxResult |
| EndProfiling() | End profiling and get file path | None | string |
| Close() | Release session resources | None | None |
OnnxTensor (Dense Tensor)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| Constructor(buffer, shape, type) | Create from ArrayBuffer | buffer: ArrayBuffer, shape: number[], type: OnnxArkTSType | OnnxTensor |
| Constructor(strings, shape) | Create from string array | strings: string[], shape: number[] | OnnxTensor |
| GetType() | Always returns TENSOR | None | OnnxValueType.TENSOR |
| Close() | Release value resources | None | None |
| IsClosed() | Whether closed | None | boolean |
| GetInfo() | Get tensor information | None | TensorInfo |
| ToString() | String representation | None | string |
| GetDataAsArrayBuffer() | Get underlying data buffer | None | ArrayBuffer |
| GetDataAsStringArray() | Get string data (STRING type only) | None | string[] |
OnnxResult (Inference Result)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| GetValueByName(name) | Get output value by name | name: string | OnnxValue |
| GetValueByIndex(index) | Get output value by index | index: number | OnnxValue |
| GetOutputCount() | Get output count | None | number |
| Close() | Release result resources | None | None |
OnnxRunOptions (Run Configuration)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| Constructor OnnxRunOptions() | Create run options object | None | OnnxRunOptions |
| SetLogLevel(level) | Set run log level | level: OnnxLoggingLevel | None |
| SetRunTag(tag) | Set run tag for identification | tag: string | None |
| AddConfigEntry(key, value) | Add custom configuration entry | key: string, value: string | None |
| AddActiveLoraAdapter(adapter) | Add active LoRA adapter | adapter: OnnxLoraAdapter | None |
| Close() | Release run options resources | None | None |
OnnxLoraAdapter (LoRA Adapter)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| Constructor(path) | Load LoRA adapter from file | path: string | OnnxLoraAdapter |
| Close() | Release adapter resources | None | None |
NodeInfo, TensorInfo, MapInfo, SequenceInfo (Node Information)
| Class | API | Description | Parameters | Return Value |
|---|---|---|---|---|
| NodeInfo | GetName() | Get node name | None | string |
| GetInfo() | Get value type info | None | ValueInfo (typically TensorInfo / MapInfo / SequenceInfo) | |
| ToString() | String representation | None | string | |
| TensorInfo | Constructor | Create tensor info | Multiple overloads: shape: number[], elementType/onnxTensorType, etc. | TensorInfo |
| GetShape() | Get tensor shape | None | number[] | |
| GetDimensionNames() | Get dimension names | None | string[] | |
| GetElementCount() | Get total element count | None | number | |
| IsScalar() | Whether scalar | None | boolean | |
| CreateDataCarrier() | Create ArrayBuffer that holds this data type | None | ArrayBuffer | |
| onnxTensorType (readonly) | ONNX tensor type | None | OnnxTensorType | |
| arktsElementType (readonly) | Corresponding ArkTS data type | None | OnnxArkTSType | |
| MapInfo | Constructor | Create map info | size, keyType, valueType, etc. | MapInfo |
| elementCount (readonly) | Element count | None | number | |
| keyType (readonly) | Key type | None | OnnxArkTSType | |
| valueType (readonly) | Value type | None | OnnxArkTSType | |
| SequenceInfo | Constructor | Create sequence info | length, elementType, etc. | SequenceInfo |
| length (readonly) | Sequence length | None | number | |
| isMapSequence (readonly) | Whether it is a Map sequence | None | boolean | |
| elementType (readonly) | Element type | None | OnnxArkTSType | |
| mapInfo (readonly) | Map info (if it is a Map sequence) | None | MapInfo |
OnnxModelMetadata (Model Metadata)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| GetProducerName() | Get model producer name | None | string |
| GetDescription() | Get model description | None | string |
| GetVersion() | Get model version | None | number |
| GetCustomMetadataKeys() | Get custom metadata keys | None | string[] |
| GetCustomMetadataValue(key) | Get custom metadata value | key: string | string |
| Close() | Release metadata resources | None | None |
OnnxValue and Subclasses (Dense Tensor, Sparse Tensor, Sequence, Map)
OnnxValue (Base Class)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| GetType() | Get value type category | None | OnnxValueType |
| Close() | Release value resources | None | None |
| IsClosed() | Whether closed | None | boolean |
OnnxMap (Map Type)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| GetType() | Returns MAP | None | OnnxValueType.MAP |
| GetElementCount() | Element count | None | number |
| GetInfo() | Map information | None | MapInfo |
| GetKeyTypeCategory() | Key type category | None | OnnxMapValueType |
| GetValueTypeCategory() | Value type category | None | OnnxValueType |
| GetStringKeys() | Get string key array | None | string[] |
| GetLongKeys() | Get long integer key array | None | bigint[] |
| GetStringValues() | Get string value array | None | string[] |
| GetLongValues() | Get long integer value array | None | bigint[] |
| GetFloatValues() | Get float value array | None | number[] |
| GetDoubleValues() | Get double value array | None | number[] |
OnnxSequence (Sequence Type)
| API | Description | Parameters | Return Value |
|---|---|---|---|
| GetType() | Returns SEQUENCE | None | OnnxValueType.SEQUENCE |
| GetMaps() | Get Map element array (when elements are Maps) | None | OnnxMap[] |
| GetTensors() | Get Tensor element array (when elements are Tensors) | None | OnnxTensor[] |
| GetInfo() | Sequence information | None | SequenceInfo |
Sparse Tensor (SparseTensor abstract class and COO/CSRC/BlockSparseTensor)
| Class | API | Description | Parameters | Return Value |
|---|---|---|---|---|
| SparseTensor (abstract) | GetSparseFormat() | Get format | None | SparseTensorFormat |
| GetNonZeroElementCount() | Non-zero element count | None | number | |
| GetDenseShape() | Dense shape | None | number[] | |
| GetIndicesType() | Index data type | None | OnnxArkTSType | |
| GetIndicesShape() | Index shape | None | number[] | |
| GetIndicesBuffer() | Index data buffer | None | ArrayBuffer | |
| GetValuesType() | Value data type | None | OnnxArkTSType | |
| GetValuesShape() | Value shape | None | number[] | |
| GetValuesBuffer() | Value data buffer | None | ArrayBuffer | |
| COOTensor | Constructor | Create COO sparse tensor | indices: BigInt64Array, indicesShape: number[], values: ArrayBuffer, denseShape: number[], elementType: OnnxArkTSType, numNonZeroElements: number | COOTensor |
| CSRCTensor | Constructor | Create CSR/CSC sparse tensor | outerIndices: BigInt64Array, innerIndices: BigInt64Array, values: ArrayBuffer, denseShape: number[], elementType: OnnxArkTSType, numNonZeroElements: number | CSRCTensor |
| GetInnerIndicesShape() | Inner index shape | None | number[] | |
| GetInnerIndicesBuffer() | Inner index buffer | None | ArrayBuffer | |
| BlockSparseTensor | Constructor | Create block sparse tensor | blockIndices: Int32Array, indicesShape: number[], values: ArrayBuffer, valuesShape: number[], denseShape: number[], elementType: OnnxArkTSType, numNonZeroElements: number | BlockSparseTensor |
| OnnxSparseTensor | Constructor(sparseTensor) | Wrap sparse tensor as OnnxValue | COO / CSRC / BlockSparseTensor instance | OnnxSparseTensor |
| GetType() | Get value type category (always returns SPARSE_TENSOR) | None | OnnxValueType.SPARSE_TENSOR | |
| Close() | Release underlying resources | None | None | |
| IsClosed() | Whether closed | None | boolean | |
| GetInfo() | Get corresponding dense tensor type info | None | TensorInfo | |
| ToString() | Get string representation (for debugging) | None | string | |
| GetSparseFormat() | Get sparse storage format | None | SparseTensorFormat | |
| GetIndicesBuffer() | Get outer index data buffer (COO coordinates, CSRC row pointers, Block indices) | None | ArrayBuffer | |
| GetInnerIndicesBuffer() | Get inner index data buffer (CSRC format only, column/row indices) | None | ArrayBuffer | |
| GetValuesBuffer() | Get non-zero element value data buffer | None | ArrayBuffer | |
| GetIndicesShape() | Get outer index array shape | None | number[] | |
| GetInnerIndicesShape() | Get inner index array shape (CSRC only) | None | number[] | |
| GetValuesShape() | Get non-zero element value array shape | None | number[] |
The above covers the main classes and methods exposed by the onnxruntime library. For more type details, refer to the Index.d.ts source code.
Directory Structure
|---- ohos_onnxruntime
| |---- entry # Sample Code Folder
| |---- library # onnxruntime Library Folder
| | |---- src
| | | └── main
| | | |---- cpp # C++ Native Code
| | | |---- napi # onnxruntime napi Code
| | | |---- thirdparty/ # Directory for onnxruntime compiled output
| | | |---- types
| | | | |---- libonnxruntime_napi/Index.d.ts # API Definitions
| | | └── CMakeLists.txt
| | |---- Index.ets # External APIs
| |---- README.md # English Documentation
| |---- README_zh.md # Chinese Documentation
How to Contribute
If you find any issues when using this project, please submit an Issue. Of course, we also welcome PR contributions.
License
This project is licensed under MIT License. Please freely enjoy and participate in open source.