rocket-userspace: librocketnpu
AI disclosure
Except for the prior work it builds on, rocket-userspace was developed by AI, primarily Claude.
Human involvement was mostly limited to setting project goals and providing hardware access.
rocket-userspace is a side project for curiosity's sake, and it comes with no guarantee of
quality, accuracy, or update frequency.
About rocket-userspace
A userspace driver, tiled/multicore matmul and on-NPU op library for Rockchip NPUs, built on
the mainline rocket DRM-accel driver. It is validated on the RK3588 and runs a growing
subset on the RK3576. Other Rockchip NPUs follow as their machine parameters are confirmed
on-device.
librocketnpu is a self-contained C library in three layers: a thin device shim over
/dev/accel/accel0, a tiled/multicore/resident matmul, and an on-NPU op library. It builds
and runs on its own, as the substrate for custom NPU compute. The ggml-rocket ggml backend,
the tflite-rocket delegate and the ort-rocket execution provider link it.
The rocket driver is a generic register-command submitter (CREATE_BO / SUBMIT /
PREP_BO / FINI_BO). It runs whatever register program it is handed, so op coverage is a
userspace matter. The library emits its own matmul register programs and the kernel runs
them, where a matmul is a 1×1 convolution over the NVDLA-style CNA→CORE→DPU blocks.
Gemma-4-12B prefill runs on the NPU through this path, and the frontends run decode, an M=1
GEMV, on the CPU (Decode).
Operations
- Matmul: tiled, K-accumulating, fanned across the 3 NPU cores, weights resident. Runs
in fp16, int8, int4, bf16 and tf32, plus bit-exact int16 via int8 byte-decomposition.
A single int16 task writes int32 natively, saturating.
- Convolution: general
CONV_2D with depthwise, transpose and resize.
- Pooling and reductions: Max and Average pool, spatial and feature-axis reductions,
cumsum.
- Activations: the DPU LUT family (sigmoid, tanh, SiLU, GELU, sqrt, rsqrt, reciprocal,
exp, softplus, mish, abs, ELU, SELU, LeakyReLU, PReLU), plus elementwise
add/sub/mul/div/max/min/clip.
- Transformer and Whisper primitives: RMSNorm, LayerNorm, softmax, gated FFN,
multi-head self-attention, and a full encoder block. Together they run a
Whisper/transformer encoder block end to end on the NPU.
- Resident encoders: whole models with their weights packed once, for SigLIP, SAM and
ModernBERT. The ModernBERT encoder takes a batch of variable-length sequences.
API.md is the function reference for the entry points a caller uses, per dtype and
op. The headers under include/ declare every public function and state each one's full
contract.
Terms
The register-level vocabulary the rest of this document uses. The first three count
different things, and conflating them is the common mistake:
| Term |
What it means here |
| Submit |
One ROCKET_SUBMIT ioctl. It costs one syscall, one fence and one IOMMU attach. |
| Task |
One register program inside a submit. A submit contains one task or many. |
| Kick |
One hardware start of the datapath, and one completion interrupt. On-chip state lives inside a kick and clears between kicks. |
| regcmd |
The register program itself: the CNA, CORE and DPU register writes the kernel replays. |
| CNA, CORE, DPU |
The three datapath blocks a program drives in sequence. Feature and weight fetch, the MAC array, then post-processing and write-out. |
| CBUF |
The on-chip buffer the CNA fills. Tile sizes are chosen to fit it. |
| Cube layout |
The NPU's native tensor format. The host scatters row-major data into it and de-tiles the result. |
| Resident weights |
Weights left in device memory across calls, so a repeated layer packs them once. |
| Prefill, decode |
The two LLM phases. Prefill processes the prompt as a batched GEMM. Decode generates one token a step, an M=1 GEMV. |
| The submit seam |
The rocket_* symbols every kernel interaction goes through, and the swap point for a non-mainline driver. |
| SPLL |
The PLL in the RK3588's secure bus clock unit that clocks the center interconnect, the path the NPU reads DDR through. Only the boot loader programs it (Boot loader). |
Submits, tasks and kicks vary independently, which is what makes them worth separating:
| Path |
Submits |
Tasks |
Kicks |
| Default convolution, one job per tile |
n |
n |
n |
| Default matmul, up to 64 tiles a job, gapped |
⌈n/64⌉ |
n |
n |
ROCKET_BATCH_SUBMIT=1, self-chained, fp16 matmul |
⌈n/64⌉ |
n |
⌈n/64⌉ |
ROCKET_CONV_BATCH=1, gapped, int8 |
1 |
n |
n |
The two matmul rows cost the same syscalls and run the same programs, and differ only in
kicks. On-chip state lives inside one kick, so a self-chained task can read what an earlier
task in the same kick just wrote. ROCKET_KACC_CHAIN depends on that read-after-write.
Independent int8 tasks are bit-exact either way, gapped or chained.
Requirements
- An RK3588 board. It is the only fully validated target, and
Hardware support has the rest.
- A mainline kernel with the
rocket DRM-accel driver, developed against ~v7.1, with
the device node /dev/accel/accel0 present. Confirm with ls /dev/accel/accel0 and
lsmod | grep rocket, where the module is named rocket.
- The
drm/rocket_accel.h uAPI header, which is in /usr/include/drm on a kernel with
rocket. CMake checks for it at configure time. Only the built-in submit provider needs
it, because a BSP-kernel board has no mainline rocket uAPI and supplies its own provider
instead. See Submit provider.
- libdrm and pthreads. No ML-framework dependency.
- Privilege to open the accel node. Run as a user in the node's group, or with
sudo -E,
where the -E preserves the ROCKET_* environment variables that plain sudo strips.
The RK3576 power-management rule
Install udev/99-rocket-npu-pm.rules on the RK3576.
On the RK3576, a program that writes a wide output element through a partial output stage
leaves the next submit writing nothing. The next submit completes normally, and nothing
reports the missing output. On the defaults only the fp16 packed-image first convolution, at
four or fewer input channels, does it. The older int32 matmul writers
(ROCKET_RK3576_I32_DENSE=0) and the fp16 convolution's fp16 writer
(ROCKET_RK3576_FP16_F32OUT=0) do it too. The runtime-PM autosuspend clears the condition by
cycling the NPU power domain.
The library drives that cycle itself rather than waiting out the driver's 50 ms timer.
Driving it needs write access to one sysfs file. The rule gives that file to the group that
already owns /dev/accel/accel0, so it grants nothing to anyone who could not already submit
NPU work. Without the rule the library falls back to waiting, and an fp16 224×224 stem costs
226 ms instead of 13.8 ms. The RK3588 has no such hazard and needs no rule.
Clock
The NPU boots at 200 MHz and the library is correct there. Above 200 MHz the RK3588's NPU
clock comes from a PVTPLL, a ring oscillator whose frequency follows the rail voltage. A
requested rate is therefore not the running clock.
Apply the patches/rocket clock and voltage patches and load the module with
rocket_npu_clk_hz=900000000. On a Turing RK1's 0.80 V rail that request runs at 928 MHz. It
held 922-928 MHz through 15 minutes of continuous load, at a 49 °C peak, with every matmul
bit-exact. A second RK1 runs the same request at 1028 MHz, so each RK3588 figure below states
the clock its board ran at. The patches also trim dispatch latency.
The share of a call's device time that scales with the NPU clock depends on the boot loader
(Boot loader). With SPLL programmed, ~70% of one core's fp16 matmul device time
at 512×3840×4096 scales with the clock at 1028 MHz. With SPLL unprogrammed, ~39% of the
whole-tile fp16 program's device time on two cores scales with it at ~816 MHz (the 600 MHz
request). A call dominated by its weight read gains 1.02x from 905 to 1028 MHz, the 600 and
900 MHz requests on the second RK1 [HW sweep 2026-11-21]. The RK3576 figures state their own
clock, which the board's device tree assigns.
[!CAUTION]
With the NPU cores pinned on (power/control=on), an rmmod at the 900 MHz request
hard-locks the board, and only a power cycle recovers it. Unload the module only while all
three cores are runtime-suspended.
Boot loader
On the RK3588, the NPU's DDR read rate depends on the boot loader. The NPU reads DDR through
the center interconnect, whose clock, aclk_center_root, runs from SPLL. Linux describes SPLL as
a fixed 702 MHz clock and neither programs nor measures it. Behind a boot loader that leaves it
unprogrammed, the NPU reads DDR at half the rate. The NPU's output writes and the CPU's reads
keep their rate [HW sweep 2026-11-21].
| Boot loader |
SPLL |
| Mainline U-Boot v2026.07 and v2026.10, Turing RK1, eMMC boot |
Not programmed [HW sweep] |
| Other mainline U-Boot from v2026.01 to v2027.01-rc1, where no SPL device requests a secure bus clock |
Not programmed [expected] |
Mainline U-Boot with commit 514663543ecf, first released in v2027.01-rc2 |
Programmed [HW sweep] |
Armbian's 2026.07_armbian build for the Turing RK1 |
Programmed [HW sweep] |
With SPLL programmed, a read-dominated call runs up to 1.98x as fast, and three cores stream
1.7x as fast. Work that host time or output writes bound gains less: LLM prefill through
ggml-rocket gains 1.01-1.11x, and a Whisper encoder 0.99-1.03x [HW sweep 2026-11-21].
Conditions: Turing RK1, kernel 7.3.3, rocket 1.3.0, the 900 MHz request (1028 MHz on that
board), CPU governor performance, DDR at 2112 MHz. Each ratio compares two boot loaders on one
board. Measured 2026-11-21.
[!CAUTION]
clk_summary reports SPLL and aclk_center_root at 702 MHz whether or not the boot loader
programmed SPLL. To check a board, time a weight read on one core with
sudo -E build/gemv_bw_probe int8 4096 4096. It streams about 10.4 GB/s with SPLL
programmed, and about 5.4 GB/s without.
Quickstart
Build the static library and run the correctness tests. They pass off-device, because the
tests that need the NPU skip:
cmake -S . -B build && cmake --build build -j
ctest --test-dir build
Call it from C. Open the device, run one fp16 matmul, close it:
#include "rocket_npu.h"
#include "rocket_matmul.h"
int fd = rocket_open(); /* opens /dev/accel/accel0 (needs privilege) */
/* C[M,N] = A[M,K] . B[N,K]^T, row-major fp16; requires M%4, K%32, N%16 */
rocket_matmul_fp16(fd, M, K, N, A, B, C);
rocket_close(fd);
Link against the rocketnpu::rocketnpu CMake target. API.md has the multicore,
resident-weight and int8/int4/bf16/tf32 entry points, and the conv, activation and
transformer op library.
Hardware support
RK3588
The RK3588 (rknpu v2, 3 NPU cores) is the full target: every datatype, the whole op library,
and every correctness test under tests/ bit-exact.
RK3576
The RK3576 (2 cores) runs a growing subset through its own encoder. CONV_2D is bit-exact
there, direct at int8 and fp16 and depthwise at int8. The int8 matmul,
rocket_matmul_int8_rk3576(), is bit-exact too. Int8 pooling and the int8 LUT activations
have their own RK3576 entries.
Three of that matmul's properties are the RK3588's inverted, and a caller porting between
the two must expect all three:
- int8 is the default matmul precision here. One int8 task contracts up to 6144 input
channels. A caller choosing K for speed keeps it at or below 4608. Past 4992 the output
tile drops from 1024 channels to 32, which multiplies the submit count by 32 at M 512 and
N 2048.
- The M axis has no alignment constraint, so
M=1 is correct.
- The output is int8 through the DPU's requant rather than raw int32.
rocket_matmul_int8_rk3576() refuses a K of 6176 or more with ROCKET_E_UNSUPPORTED. For a
larger K, rocket_matmul_int8_rk3576_i32() returns the raw int32 product, splitting K and
summing the partials on the host. It runs on a dense writer that programs only the real
output channels and leaves the next submit unaffected. A call at 8×64×32 takes 1.0 ms, where
the older writers behind ROCKET_RK3576_I32_DENSE=0 take 106.9 ms (rocket 1.6.0,
2026-09-30).
rocket_matmul_fp16_rk3576() is the fp16 matmul, with fp32 output. It is a different register
program from the fp16 convolution, and it contracts the whole of K in one task. It accepts K up
to 6144, N up to 8192 and M up to 2048. One task holds as many rows as fit the CBUF, 85 at
K 1536, so a larger M runs as several tasks in one submit. It is exact against a CPU reference
up to 2048×1536×256 (tests/rk3576_mm_fp16_gate). Its output leaves the next submit
unaffected.
rocket_matmul_fp16_rk3576_wbo() keeps the weight resident in a handle. Against the int8 entry
with resident weights it costs 0.86-0.94x at most shapes up to M 16. At a prefill's M it costs
about 2x: 2.0-2.1x at 512×1536×1536, and 1.8-2.6x across N at M 512 and K 1536. At K 6144 the two
tie, because the int8 entry slows there. On the device alone this program contracts at about
half the int8 program's rate (tests/rk3576_mm_fp16_cost, 786 MHz, warm).
The rest of the op library emits the RK3588 encoding. On the RK3576 its generators refuse
rather than submit a program the hardware will not run.
Prepacked convolution