Skip to content

Extract the shared librknnrt plumbing used by the model-local NPU sidecars - #900

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:rknn-framework-engine
Oct 14, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:rknn-framework-engine

Conversation

@wOvAN

@wOvAN wOvAN commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Extract the shared librknnrt plumbing used by the model-local NPU sidecars

Branch: rknn-framework-engine, base main. 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) and rknn_generator.cpp (505) -- 29, 42 and 38 rknn_* identifier
occurrences 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_destroy in the destructor, and the status
formatting.

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) and engine::rknn::status(label, what, code) --
      available with or without ENGINE_HAVE_RKNN, so a model file can report a missing engine
      in a default build.
    • engine::rknn::Engine (with ENGINE_HAVE_RKNN): a rknn_context plus its cached input
      and output attributes, and one fused run(inputs, outputs) that does
      rknn_inputs_set → rknn_run → rknn_outputs_get (fp32) → rknn_outputs_release.
      load() asks for RKNN_NPU_CORE_0_1_2 by name (with RKNN_NPU_CORE_AUTO as the
      fallback) because AUTO lets the driver pick one core for graphs this small, and keeps
      the AUDIOCPP_RKNN_CORE_MASK override in one place.
    • The attr.size trap is documented at the Input struct: with pass_through = 0 the
      runtime wants n_elems * sizeof(declared_type), not rknn_tensor_attr.size, which
      reports 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 a
    board -- 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:

g++ -std=c++20 -Wall -Wextra -Iinclude -c src/framework/rknn/engine.cpp -o engine.o
g++ -std=c++20 -Wall -Wextra -Iinclude -Itests/unittests \
  src/framework/rknn/engine.cpp tests/unittests/test_rknn_engine.cpp -o rknn_engine_test
./rknn_engine_test

Board (Orange Pi 5 Plus, RK3588, gcc 15.2, librknnrt 2.3.2, rknn_api.h in
/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_test

Both compile clean with -Wall -Wextra and the test passes on both. The context calls
themselves (rknn_init, core mask, the step) are exercised on the board by the sidecar PRs
that use them, not by this test.

python3 tools/check_loader_catalog_sync.py --self-test && python3 tools/check_loader_catalog_sync.py
passes (nothing in the catalog changes).

Known limitations

  • The class is deliberately not movable and does not pool contexts: one rknn_context per
    sidecar is what measured fastest on RK3588 (three rknn_dup_context contexts running
    windows concurrently measured 1.15× on the window loop with corrupted output, because
    duplicated contexts share the runtime's tensor staging).
  • No rknn_dup_context, no zero-copy (rknn_create_mem) and no async path here -- the
    sidecars 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.

…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.
@wOvAN

wOvAN commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

This is the base of the RKNN stack: #896 (qwen3_tts) and #897 (piper_tts) are rebased onto this branch and use engine::rknn::Engine/read_engine/status instead of their own copies of the plumbing (−118 and −125 lines of raw rknn_* touchpoints respectively). The kokoro_tts sidecar (#898) is the third consumer; it is temporarily closed only because contributors without write access are limited to 3 open PRs, and it will be reopened on top of this once this one merges.

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 ENGINE_HAVE_RKNN-gated registration.

@0xShug0
0xShug0 merged commit fe6f778 into 0xShug0:main Oct 14, 2026
6 checks passed
@0xShug0

0xShug0 commented Oct 14, 2026

Copy link
Copy Markdown
Owner

@wOvAN Thanks! PR merged! We now have a shared foundation for adding NPU optimizations to more models 🎉

@wOvAN

wOvAN commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Rebased all three sidecar branches onto main and dropped the framework commit from each, so their diffs against main are now model-local only: #896 (qwen3_tts) 13 files, #897 (piper_tts) 10 files, and the kokoro_tts sidecar, reopened as #904 (14 files) — #898 could not be reopened after the force-push, so it is superseded by #904.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants