采用 LiteRT 的 Intel NPU (OpenVino)

LiteRT 通过 CompiledModel API 支持 Intel OpenVino,适用于 AOT 和设备端编译。

设置开发环境

Linux (x86_64)

  • Ubuntu 22.04 或 24.04 LTS
  • Python 3.10+ - 从 python.org 或您的发行版安装 (sudo apt install python3 python3-venv)
  • Intel NPU 驱动程序 v1.32.1 - 请参阅 Linux NPU 设置

Windows (x86_64)

如需从源代码构建,还需要使用 Bazelisk或 hermetic Docker build 的 Bazel 7.4.1+。

支持的 SoC

平台 NPU 代号 操作系统
Intel Core Ultra Series 2 NPU4000 Lunar Lake (LNL) Linux、Windows
Intel Core Ultra Series 3 NPU5010 Panther Lake (PTL) Linux、Windows

快速入门

1. 安装 NPU 驱动程序

请参阅 Linux NPU 设置Windows NPU 设置。如果您只需要 AOT,请跳过此步骤。

只有在 NPU 硬件上执行 模型的系统才需要 NPU 驱动程序。纯 AOT 构建系统可以跳过此步骤。

注意ai-edge-litert-sdk-intel-nightly 会按 PEP 440 版本(例如 openvino==2026.2.0.dev20260506)固定匹配的 OpenVINO nightly 轮,因此 pip 需要 --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly 才能找到它。在 Linux 上,如果发行版自动检测选择了错误的归档,请在 pip install 之前设置 LITERT_OV_OS_ID=ubuntu22ubuntu24

2. 创建 Python 虚拟环境

建议将 nightly openvino 轮与任何系统范围的 OpenVINO 安装隔离开。

python -m venv litert_env
# Linux / macOS
source litert_env/bin/activate
# Windows (PowerShell)
.\litert_env\Scripts\Activate.ps1

python -m pip install --upgrade pip

3. 安装 pip 软件包

pip install --pre \
    --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly \
    ai-edge-litert-nightly ai-edge-litert-sdk-intel-nightly

--extra-index-url 可让 pip 从 OpenVINO 的索引中解析固定的 openvino nightly 轮,以及 PyPI 上的软件包。

4. 验证安装

python -c "
from ai_edge_litert.aot.vendors.intel_openvino import intel_openvino_backend
import ai_edge_litert_sdk_intel, openvino, os
print('Backend:', intel_openvino_backend.IntelOpenVinoBackend.id())
print('Dispatch:', intel_openvino_backend.get_dispatch_dir())
print('OpenVINO:', openvino.__version__)
print('SDK libs:', sorted(os.listdir(ai_edge_litert_sdk_intel.path_to_sdk_libs())))
print('Available devices:', openvino.Core().available_devices)
"

输出中要检查的内容:

  • SDK libs 列出 libopenvino_intel_npu_compiler.so (Linux) 或 openvino_intel_npu_compiler.dll (Windows) - AOT 所需。
  • Available devices 包含 NPU - 确认已安装 NPU 驱动程序,并且 OpenVINO 可以与设备通信。在仅限 AOT 的系统(未安装驱动程序)和没有 Intel NPU 硬件的系统上,将没有 NPU

5. AOT 编译(可选)

  • 为特定的 Intel NPU 目标(PTL 或 LNL)预编译 .tflite,以便运行时跳过编译器插件步骤。
  • 不需要 实体 NPU 或 NPU 驱动程序 - 仅需要 ai-edge-litert-nightlyai-edge-litert-sdk-intel-nightly
  • 支持交叉编译:在任何 Linux 或 Windows 主机上编译,将生成的 .tflite 运送到任一操作系统的目标,并在那里运行。

输出文件的名称为 <model>_IntelOpenVINO_<SoC>_apply_plugin.tflite

from ai_edge_litert.aot import aot_compile
from ai_edge_litert.aot.vendors.intel_openvino import target as intel_target

# Compile for a single Intel NPU target (PTL or LNL).
aot_compile.aot_compile(
    "model.tflite",
    output_dir="out",
    target=intel_target.Target(soc_model=intel_target.SocModel.PTL),
)

# Or omit target= to compile for every registered backend/target.
aot_compile.aot_compile("model.tflite", output_dir="out", keep_going=True)

6. 运行 NPU 推理

LiteRT 支持 Intel NPU 上的两条推理路径:

  • JIT - 加载原始 .tflite;编译器插件在 CompiledModel.from_file() 时间为 NPU 分区和编译受支持的操作。增加了一些首次运行延迟时间(因模型而异)。
  • AOT 编译 - 加载由第 4 步生成的 <model>_IntelOpenVINO_<SoC>_apply_plugin.tflite 。在加载时间跳过分区和编译步骤。

此代码段适用于以下两种情况:

from ai_edge_litert.compiled_model import CompiledModel
from ai_edge_litert.hardware_accelerator import HardwareAccelerator

model = CompiledModel.from_file(
    "model.tflite",  # raw tflite (JIT) or ..._apply_plugin.tflite (AOT)
    hardware_accel=HardwareAccelerator.NPU | HardwareAccelerator.CPU,
)

sig_key = list(model.get_signature_list().keys())[0]
sig_idx = model.get_signature_index(sig_key)
input_buffers = model.create_input_buffers(sig_idx)
output_buffers = model.create_output_buffers(sig_idx)
model.run_by_index(sig_idx, input_buffers, output_buffers)
print("Fully accelerated:", model.is_fully_accelerated())

确认 JIT 实际运行

如果 JIT 成功,日志将包含(文件扩展名在 Linux 上为 .so,在 Windows 上为 .dll):

INFO: [compiler_plugin.cc:236] Loaded plugin at: .../LiteRtCompilerPlugin_IntelOpenvino.{so,dll}
INFO: [compiler_plugin.cc:690] Partitioned subgraph<0>, selected N ops, from a total of N ops
INFO: [compiled_model.cc:1006] JIT compilation changed model, reserializing...

如果缺少这些行,但仍报告 Fully accelerated: True,则模型在 XNNPACK CPU 回退上运行,而不是在 NPU 上运行 - 请参阅 JIT 问题排查行。

7. 基准

# Dispatch library and the NPU compiler are auto-discovered from the wheel.
litert-benchmark --model=model.tflite --use_npu --num_runs=50

常用标志:

标志 默认 说明
--model PATH .tflite 模型的路径(必需)。
--signature KEY 第一个 要运行的签名密钥。
--use_cpu / --no_cpu 开启 切换 CPU 加速器 / CPU 回退。
--use_gpu 关闭 启用 GPU 加速器。
--use_npu 关闭 启用 Intel NPU 加速器。
--require_full_delegation 关闭 如果模型未完全卸载到所选加速器,则失败。
--num_runs N 50 计时推理迭代次数。
--warmup_runs N 5 测量前未计时的热身迭代。
--num_threads N 1 CPU 线程数。
--result_json PATH 写入 JSON 摘要(延迟时间统计信息、吞吐量、加速器列表)。
--verbose 关闭 额外的运行时日志记录。

高级 / 替换标志 - 仅在指向自定义 build 时需要:--dispatch_library_path--compiler_plugin_path--runtime_path

混合供应商轮:将 JIT 固定到 Intel OV

注意 :当调用 Environment.create() 时没有显式路径,它会按字母顺序自动发现 ai_edge_litert/vendors/ 下的供应商,并注册找到的第一个供应商。在混合供应商安装中,这可能不是 Intel OV - 显式传递 Intel OV 目录以强制选择正确的供应商。

  • pip 轮为每个注册的供应商(intel_openvino/google_tensor/mediatek/qualcomm/samsung/)提供编译器插件。
  • 如需强制使用 Intel OV 路径(建议在安装多个供应商 SDK 时使用),请手动传递 Intel OV 目录:
from ai_edge_litert.environment import Environment
from ai_edge_litert.compiled_model import CompiledModel
from ai_edge_litert.hardware_accelerator import HardwareAccelerator
from ai_edge_litert.aot.vendors.intel_openvino import intel_openvino_backend as ov

env = Environment.create(
    compiler_plugin_path=ov.get_compiler_plugin_dir(),   # JIT compiler
    dispatch_library_path=ov.get_dispatch_dir(),          # runtime
)
model = CompiledModel.from_file(
    "model.tflite",
    hardware_accel=HardwareAccelerator.NPU | HardwareAccelerator.CPU,
    environment=env,
)

运行时会加载在给定目录中找到的每个共享库,因此指向 vendors/intel_openvino/compiler/ 仅加载 Intel 插件;同级目录中的 Google Tensor / MediaTek / Qualcomm / Samsung 插件永远不会被触及。

对于 CLI,等效标志为:

DISPATCH_DIR=$(python3 -c 'from ai_edge_litert.aot.vendors.intel_openvino import intel_openvino_backend as ov; print(ov.get_dispatch_dir())')
COMPILER_DIR=$(python3 -c 'from ai_edge_litert.aot.vendors.intel_openvino import intel_openvino_backend as ov; print(ov.get_compiler_plugin_dir())')

litert-benchmark --model=model.tflite --use_npu \
    --compiler_plugin_path=$COMPILER_DIR \
    --dispatch_library_path=$DISPATCH_DIR

验证 NPU 执行

如需确认模型实际上在 NPU 上运行,请检查这两个 信号:

  1. 日志包含 Loading shared library: .../LiteRtDispatch_IntelOpenvino.{so,dll} - Intel 调度库已 加载(在 Linux 上为 .so,在 Windows 上为 .dll)。
  2. model.is_fully_accelerated() 返回 True - 每个操作都已卸载到所选加速器。

is_fully_accelerated() 不足以 确定:如果调度库从未加载,则操作已完全卸载到 XNNPACK/CPU,而不是 NPU。


Linux NPU 设置

注意 :如果您只需要 AOT,请跳过此部分 - 不需要实体 NPU。

信息:使用 NPU 驱动程序 v1.32.1 (与 OpenVINO 2026.1 配对)。旧版驱动程序会失败,并显示 Level0 pfnCreate2 result: ZE_RESULT_ERROR_UNSUPPORTED_FEATURE

# 1. NPU driver (Ubuntu 24.04 use -ubuntu2204 tarball for 22.04).
sudo dpkg --purge --force-remove-reinstreq \
  intel-driver-compiler-npu intel-fw-npu intel-level-zero-npu intel-level-zero-npu-dbgsym || true
wget https://github.com/intel/linux-npu-driver/releases/download/v1.32.1/linux-npu-driver-v1.32.1.20260422-24767473183-ubuntu2404.tar.gz
tar -xf linux-npu-driver-v1.32.1.*.tar.gz
sudo apt update && sudo apt install -y libtbb12
sudo dpkg -i intel-fw-npu_*.deb intel-level-zero-npu_*.deb intel-driver-compiler-npu_*.deb

# 2. Level Zero loader v1.27.0.
wget https://snapshot.ppa.launchpadcontent.net/kobuk-team/intel-graphics/ubuntu/20260324T100000Z/pool/main/l/level-zero-loader/libze1_1.27.0-1~24.04~ppa2_amd64.deb
sudo dpkg -i libze1_*.deb

# 3. Permissions + verify.
sudo gpasswd -a ${USER} render && newgrp render
ls /dev/accel/accel0   # must exist after reboot

然后,运行快速应用中的安装 + 验证代码段。


Windows NPU 设置

注意 :如果您只需要 AOT,请跳过此部分 - 不需要实体 NPU。

  • Intel 下载中心安装 Intel NPU 驱动程序(32.0.100.4724+)。
  • 验证设备管理器是否在神经处理器 下列出了 NPU 设备(根据驱动程序显示为 Intel(R) AI BoostIntel(R) NPU)。
  • 运行快速应用中的安装 + 验证代码段,将 pip替换为python -m pip

信息import ai_edge_litert 使用 os.add_dll_directory() 自动注册 DLL 目录,因此 Python 脚本不需要 PATH 设置。对于 非 Python 使用者,请运行 setupvars.bat 或将 <openvino>/libs 添加到 PATH 前面。


从源代码构建

使用代理?在运行 build 脚本之前导出 http_proxy / https_proxy / no_proxy - 它们会将这些转发到 Docker 和容器中。

Linux(Docker,hermetic):

cd LiteRT/docker_build && ./build_wheel_with_docker.sh

Windows(PowerShell,PATH 中的 Bazel):

.\ci\build_pip_package_with_bazel_windows.ps1

输出位于 dist/ 中:

  • ai_edge_litert-*.whl - 运行时轮。
  • ai_edge_litert_sdk_{intel,qualcomm,mediatek,samsung}-*.tar.gz - 供应商 sdist。
  • Intel sdist 大约为 5 KB;NPU 编译器 .so/.dllpip install 时提取,因此相同的 sdist 适用于 Linux 和 Windows。

单元测试

bazel test \
  //litert/python/aot/vendors/intel_openvino:intel_openvino_backend_test \
  //litert/c/options:litert_intel_openvino_options_test \
  //litert/cc/options:litert_intel_openvino_options_test \
  //litert/tools/flags/vendors:intel_openvino_flags_test

问题排查

问题 修复
AOT 失败:Device with "NPU" name is not registered 未提取 NPU 编译器。检查 ai_edge_litert_sdk_intel.path_to_sdk_libs() 是否列出了 libopenvino_intel_npu_compiler.so / .dll。如果为空,请重新安装并具有网络访问权限,或设置 LITERT_OV_OS_ID=ubuntu22/ubuntu24
JIT 在 CPU 上运行而不是在 NPU 上运行(没有 Partitioned subgraph 日志,没有 Loaded plugin 日志,仍然打印 Fully accelerated: True 未发现编译器插件。确认 ov.get_compiler_plugin_dir() 返回 ai_edge_litert/vendors/intel_openvino/compiler/ 下的路径。如果安装了多个供应商 SDK,请将 compiler_plugin_path=ov.get_compiler_plugin_dir() 显式传递给 Environment.create()(或将 --compiler_plugin_path=... 传递给 litert-benchmark)。
JIT 失败:Cannot load library .../openvino/libs/libopenvino_intel_npu_compiler.so (Linux) / openvino_intel_npu_compiler.dll (Windows) SDK sdist 在首次 import ai_edge_litert_sdk_intel 时将 NPU 编译器复制到 openvino/libs/。如果跳过了复制(只读 FS,缺少 openvino),请在安装 openvino 后重新安装 ai-edge-litert-sdk-intel,然后在新进程中 import ai_edge_litert
Level0 pfnCreate2 result: ZE_RESULT_ERROR_UNSUPPORTED_FEATURE 将 NPU 驱动程序升级到 v1.32.1 (Linux)。
找不到 /dev/accel/accel0 sudo dmesg | grep -i vpu 以调试驱动程序;安装后重新启动。
对 NPU 的权限被拒绝 sudo gpasswd -a ${USER} render && newgrp render
Windows:设备管理器中没有 NPU Intel 下载中心 安装 NPU 驱动程序 32.0.100.4724+。
Windows:Failed to initialize Dispatch API / 缺少 DLL 确保首先运行 import ai_edge_litert(自动注册 DLL 目录);对于非 Python 调用方,请运行 setupvars.bat 或将 <openvino>/libs 添加到 PATH 前面。
Windows build:LNK2001 fixed_address_empty_stringC2491 dllimportPython 3.12+ fails Protobuf ABI / Python 版本限制 - 请参阅 ci/build_pip_package_with_bazel_windows.ps1;Windows build 需要 Python 3.11。

限制

仅通过 OpenVINO 调度路径支持 NPU 设备。对于 CPU 推理,请单独使用 HardwareAccelerator.CPU (XNNPACK)。

后续步骤

  1. 首先阅读统一的 NPU 指南:使用 LiteRT 进行 NPU 加速
  2. 按照其中的转换和部署步骤操作,并在适用情况下选择 Qualcomm。
  3. 对于 LLM,请参阅 使用 LiteRT-LM 在 NPU 上执行 LLM