Skip to content

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

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

wOvAN merged 1 commit into
mainfrom
rknn-framework-engine

Conversation

@wOvAN

@wOvAN wOvAN commented Oct 14, 2026

Copy link
Copy Markdown
Owner

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 0xShug0#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 -- 40 RKNN API touchpoints in each of rknn_speech_decoder.cpp (477 lines),
rknn_decoder.cpp (377) and rknn_generator.cpp (505): 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.
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.

1 participant