Intel NPU (OpenVino) with LiteRT

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):

  • Windows 10 或 11
  • Python 3.10 以上版本 - 從 python.org 安裝
  • Intel NPU 驅動程式 32.0.100.4724+ - 請參閱「Windows NPU 設定

如要從來源建構,也必須使用 Bazelisk 或密封式 Docker 建構,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 每夜版 Wheel,因此 pip 需要 --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly 才能找到該 Wheel。在 Linux 上,如果發行版本自動偵測功能選取了錯誤的封存檔,請在 pip install 前設定 LITERT_OV_OS_ID=ubuntu22ubuntu24

2. 建立 Python 虛擬環境

建議將每晚的 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 夜間版 Wheel,以及 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 傳送至任一 OS 的目標,並在該處運作執行。

輸出檔案名稱為 <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 on 切換 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 關閉 額外執行階段記錄。

進階 / 覆寫旗標 - 僅在指向自訂建構版本時需要:--dispatch_library_path--compiler_plugin_path--runtime_path

混合供應商的 Wheel:將 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 或在 PATH 前加上 <openvino>/libs


從來源建構

使用 Proxy 嗎?請在執行建構指令碼前匯出 http_proxy / https_proxy / no_proxy,這些指令碼會將這些變數轉送至 Docker 和容器。

Linux (Docker、密封):

cd LiteRT/docker_build && ./build_wheel_with_docker.sh

Windows (PowerShell,Bazel 位於 PATH 中):

.\ci\build_pip_package_with_bazel_windows.ps1

輸出內容會顯示在 dist/ 中:

  • ai_edge_litert-*.whl,即執行階段輪。
  • ai_edge_litert_sdk_{intel,qualcomm,mediatek,samsung}-*.tar.gz - 供應商 sdists。
  • Intel sdist 約為 5 KB;NPU 編譯器 .so/.dll 會在 pip 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 版本:LNK2001 fixed_address_empty_stringC2491 dllimportPython 3.12+ fails Protobuf ABI / Python 版本限制 - 請參閱 ci/build_pip_package_with_bazel_windows.ps1;Windows 建構作業需要 Python 3.11。

限制

透過 OpenVINO 傳送路徑,僅支援 NPU 裝置。如要進行 CPU 推論,請單獨使用 HardwareAccelerator.CPU (XNNPACK)。

後續步驟

  1. 請先參閱統一 NPU 指南:使用 LiteRT 進行 NPU 加速
  2. 請按照該頁面的轉換和部署步驟操作,並視需要選擇 Qualcomm。
  3. 如要瞭解如何使用 LiteRT-LM 在 NPU 上執行 LLM,請參閱這篇文章