Repository navigation
Extract the shared librknnrt plumbing used by the model-local NPU sidecars - #900
Conversation
…sidecars The Rockchip NPU compute paths are model-local modules, not a ggml backend: RKNN has no runtime graph builder, so each graph-isolated stage is compiled offline for one fixed shape and voiced through librknnrt. Every one of them currently carries its own copy of the driver plumbing — read the engine image, rknn_init, pick the NPU cores, query the tensor attributes, inputs_set/run/outputs_get, destroy the context, and format the status codes. Adds the shared part in one place: - `include/engine/framework/rknn/engine.h` / `src/framework/rknn/engine.cpp`: `engine::rknn::read_engine`, `engine::rknn::status` (both build with or without `ENGINE_HAVE_RKNN`) and, with it, `engine::rknn::Engine` — a `rknn_context` plus its cached input/output attributes and one fused `run(inputs, outputs)` step. The core-mask choice, including the `AUDIOCPP_RKNN_CORE_MASK` override and the AUTO fallback, moves here too. - `tests/unittests/test_rknn_engine.cpp` covers the part that is testable without a board: the engine-image reader (round trip, missing file, empty file) and the status format. Nothing is registered, selected, or called by any model in this commit; the sidecar PRs are rebased onto it and keep only their tensor geometry, window walk and seam stitching.
|
This is the base of the RKNN stack: #896 (qwen3_tts) and #897 (piper_tts) are rebased onto this branch and use Each of the three sidecar PRs carries the same plumbing today, which is what the review on #896 asked to extract. Merging this first makes each of them a model-local diff: tensor geometry, window walk, seam stitching, and the |
|
@wOvAN Thanks! PR merged! We now have a shared foundation for adding NPU optimizations to more models 🎉 |
|
Thanks! Rebased all three sidecar branches onto |
Extract the shared librknnrt plumbing used by the model-local NPU sidecars
Branch:
rknn-framework-engine, basemain. No behaviour change: nothing is registered,selected, or called by any model in this commit.
Why
You asked (on #896) for "a small shared RKNN library to reduce duplication and make it easier
to scale in the future". The three sidecar PRs each carry their own copy of the same driver
plumbing: 20, 30 and 27 driver call sites in
rknn_speech_decoder.cpp(477 lines),rknn_decoder.cpp(370) andrknn_generator.cpp(505) -- 29, 42 and 38rknn_*identifieroccurrences in the same three files. The calls are: read the engine image,
rknn_init,pick the NPU cores, query the tensor attributes,
rknn_inputs_set/rknn_run/rknn_outputs_get/rknn_outputs_release,rknn_destroyin the destructor, and the statusformatting.
This is that library, as its own PR so it can be reviewed as framework code once. The model
PRs are rebased onto it and keep only what is model-local: tensor geometry (which named input
is the latent, what shape it has), the window walk, and the seam stitching.
What it adds
include/engine/framework/rknn/engine.h,src/framework/rknn/engine.cpp(compiled into
engine_core):engine::rknn::read_engine(path, label)andengine::rknn::status(label, what, code)--available with or without
ENGINE_HAVE_RKNN, so a model file can report a missing enginein a default build.
engine::rknn::Engine(withENGINE_HAVE_RKNN): arknn_contextplus its cached inputand output attributes, and one fused
run(inputs, outputs)that doesrknn_inputs_set→rknn_run→rknn_outputs_get(fp32) →rknn_outputs_release.load()asks forRKNN_NPU_CORE_0_1_2by name (withRKNN_NPU_CORE_AUTOas thefallback) because
AUTOlets the driver pick one core for graphs this small, and keepsthe
AUDIOCPP_RKNN_CORE_MASKoverride in one place.attr.sizetrap is documented at theInputstruct: withpass_through = 0theruntime wants
n_elems * sizeof(declared_type), notrknn_tensor_attr.size, whichreports the engine's native (fp16) width -- getting this wrong is
rknn_inputs_set→-5,param input size(4096) < model input size(8192).tests/unittests/test_rknn_engine.cpp(rknn_engine_test): the part testable without aboard -- engine-image round trip, missing file, empty file, status format.
Build and run (as tested)
Host (x86_64, gcc 13.3), the no-RKNN variant and the test:
Board (Orange Pi 5 Plus, RK3588, gcc 15.2, librknnrt 2.3.2,
rknn_api.hin/usr/local/include) -- the RKNN variant, linked against the real runtime:g++ -std=c++20 -O2 -Wall -Wextra -DENGINE_HAVE_RKNN=1 -Iinclude -I/usr/local/include \ -Itests/unittests src/framework/rknn/engine.cpp tests/unittests/test_rknn_engine.cpp \ -lrknnrt -o rknn_engine_test && ./rknn_engine_testBoth compile clean with
-Wall -Wextraand the test passes on both. The context callsthemselves (
rknn_init, core mask, the step) are exercised on the board by the sidecar PRsthat use them, not by this test.
python3 tools/check_loader_catalog_sync.py --self-test && python3 tools/check_loader_catalog_sync.pypasses (nothing in the catalog changes).
Known limitations
rknn_contextpersidecar is what measured fastest on RK3588 (three
rknn_dup_contextcontexts runningwindows concurrently measured 1.15× on the window loop with corrupted output, because
duplicated contexts share the runtime's tensor staging).
rknn_dup_context, no zero-copy (rknn_create_mem) and no async path here -- thesidecars are compute/scheduler-bound, not bandwidth-bound (they stream weights at
0.08-0.10 GB/s against a measured 26.4-29.4 GB/s DRAM ceiling), so those APIs buy nothing
today and are not wrapped speculatively.