Repository navigation
Extract the shared librknnrt plumbing used by the model-local NPU sidecars - #1
Merged
Merged
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 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) andrknn_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_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.