Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime NNAPIFlags

From Leeroopedia


Knowledge Sources Description
Source File java/src/main/java/ai/onnxruntime/providers/NNAPIFlags.java
Repository Microsoft/onnxruntime

Domains

  • Machine Learning Runtime
  • Execution Provider Configuration
  • Android NNAPI

Overview

NNAPIFlags is a public enum providing bitwise flags for configuring the Android NNAPI (Neural Networks API) execution provider. It implements the OrtFlags interface, allowing flags to be combined into a bitmask via EnumSet.

Description

The enum defines four flags:

  • USE_FP16 (1): Enables fp16 support for improved performance on devices with fp16 acceleration.
  • USE_NCHW (2): Uses channels-first (NCHW) format. Only recommended for development and validation.
  • CPU_DISABLED (4): Disables NNAPI CPU fallback. If an operator can only run on CPU via NNAPI, model load will fail.
  • CPU_ONLY (8): Force NNAPI to use CPU only. Not recommended for production due to performance impact.

Code Reference

Source Location

// File: java/src/main/java/ai/onnxruntime/providers/NNAPIFlags.java
// Package: ai.onnxruntime.providers

Signature

public enum NNAPIFlags implements OrtFlags {
    USE_FP16(1), USE_NCHW(2), CPU_DISABLED(4), CPU_ONLY(8);

    public final int value;
    public int getValue();
}

Import

import ai.onnxruntime.providers.NNAPIFlags;

I/O Contract

Inputs

Name Type Description
(enum constant) NNAPIFlags The flag to include in the configuration

Outputs

Name Type Description
getValue() int The native bitmask value for this flag

Usage Examples

import ai.onnxruntime.*;
import ai.onnxruntime.providers.NNAPIFlags;
import java.util.EnumSet;

try (OrtSession.SessionOptions opts = new OrtSession.SessionOptions()) {
    // Add NNAPI with fp16 support, disabling CPU fallback
    opts.addNnapi(EnumSet.of(NNAPIFlags.USE_FP16, NNAPIFlags.CPU_DISABLED));

    OrtEnvironment env = OrtEnvironment.getEnvironment();
    try (OrtSession session = env.createSession("/path/to/model.onnx", opts)) {
        // Model will use NNAPI with fp16 on Android
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment