Skip to content

refactor(rknn): split the aperture accounting from the aperture measurements - #957

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:rknn-aperture-ladder
Oct 17, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:rknn-aperture-ladder

Conversation

@wOvAN

@wOvAN wOvAN commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What splits

Rework of the first shape of this branch, following the review on #951: engine selection and
residency management are separate concerns, model-specific execution policy stays local, and a
measured RK3588 constraint should not become a universal runtime constant.

  • Shared, engine::rknn: the accounting. ApertureBudget(mib_per_unit, budget_mib) — the
    resident set is the sum of the units the loaded engines reserve, and a new engine is pinned only
    while the whole set stays inside the budget; a unit that cannot share the aperture with the set
    evicts it and is itself evicted by the next change of shape. The type holds no ceiling and no cost
    of its own, so it carries no board's measurement.
  • Model-local, cosyvoice3/rknn_flow.cpp: the measurements. kApertureMiBPerFrame = 5 and
    kAperturePinBudgetMiB = 2800 with the full co-residency matrix that derived them (0.27 GiB at
    S=110, 0.48 at 200, 0.77 at 250, 1.93 at 400, 3.04 at 500, 4.39 at 900, against 661-714 MB engine
    files; S=200 x3, 250+250+110 and 110 x4 initialize together at <= 1.81 GiB, S=400 coexists only
    with 110/200, S=500 with nothing).
  • Model-local and untouched: the selection. pick() still decides which rung serves a request
    (smallest compiled length that fits) and what runs off-NPU (the CPU graph past the largest rung).
    It loses only its scan over the slot vector, which the budget now accounts.

What can genuinely be shared between CosyVoice3 and Qwen3-TTS

Asked first, so here it is, from the two implementations:

CosyVoice3 flow Qwen3-TTS speech_decoder
engines ladder, compiled per sequence length (S=110…900), count is the operator's choice fixed pair: the 64-token group model, plus an optional 16-token first-group model
selection per request yes — smallest rung that fits the utterance, CPU graph past the largest none — the pair is positional (first group vs the rest), not length-selected
residency 2-3 short rungs coexist on RK3588, long rungs evict everything; needs accounting both are needed at once whenever first is configured; the pair is mandatory, so a budget that refuses one is a load-time assert, not a policy
shared part arithmetic over (loaded set, per-unit cost, budget) the same arithmetic, degenerate: two units, budget checked once at load
not shared rung naming, ladder length, fallback the pairing rule, cache-size equality check

So the intersection is exactly the accounting, which is what this branch moves; selection and the
numbers are not in it. Qwen3-TTS is deliberately not wired to the type here: it has no selection to
feed, and wiring it would mean inventing a policy (refuse the pair, or run one engine) that nobody
measured.

Measurements

RK3588 (Orange Pi 5 Plus), the stand that produced the numbers above: one audiocpp_server with
cosyvoice3.exec_plan={"flow":{"impl":"rknn","artifacts":{"main":"...seq110.rknn,...seq200.rknn,..."}}},
3 cold ladders × 6 alternating-rung requests (4/10/14/20/26/34 chars → 118/158/202/192/220/228 mel
frames), the pre-move side recorded from the same build tree.

before after
audio 18 clips 18 clips, bit-identical (identical md5 sets per run)
engine loads per ladder 2 2
wall, 18 requests 283.034 s 281.837 s (−0.42%)
peak RSS 5090-5099 MiB 5099 MiB

A relocation, and the wall/RSS columns say so.

Validation

  • rknn_engine_test (existing target, no new registration) replays that board trace through
    ApertureBudget(5, 2800) on a host build: the served sequence must load exactly two engines and
    end at a 450-frame resident set, fits(500) must be false beside 200+250 and true on a cleared
    budget, and fits(900) false on its own. A budget that evicts a short rung it should keep fails
    ctest without a board.
  • cosyvoice3/rknn_flow.cpp compiles with -DENGINE_HAVE_RKNN (stub rknn_api.h here; the board
    build is the real one, ENGINE_ENABLE_RKNN=ON, rc=0).
  • tools/check_loader_catalog_sync.py → ok (no spec surface touched).
  • docs/build/rockchip-npu.md: the ladder bullet no longer says «single-resident» (it stopped being
    true when the estimator ladder started keeping short rungs pinned) and now states which half of
    the accounting is shared and which half is a board measurement.

…rements

The resident-set arithmetic a sidecar needs when it keeps more than one compiled engine loaded is
model-agnostic; the numbers that drive it are not. So the arithmetic moves to `engine::rknn` and the
numbers stay with the sidecar that measured them.

- `ApertureBudget(mib_per_unit, budget_mib)`: the resident set is the sum of the units the loaded
  engines reserve, and a new engine is pinned only while the whole set stays inside the budget. It
  holds no ceiling of its own.
- `cosyvoice3/rknn_flow.cpp` keeps `kApertureMiBPerFrame = 5` and `kAperturePinBudgetMiB = 2800`
  with the full RK3588 co-residency matrix that derived them, and `pick()` loses its scan over the
  slot vector. Engine selection (which rung serves a request) is untouched and stays model-local.

Behaviour is unchanged, measured on RK3588 (Orange Pi 5 Plus) against the recorded pre-move side of
the same stand: one `audiocpp_server` with the flow-estimator ladder
(`cosyvoice3.exec_plan={"flow":{"impl":"rknn",...}}`), 3 cold ladders x 6 alternating-rung requests
(4/10/14/20/26/34 chars -> 118/158/202/192/220/228 mel frames) — all 18 clips bit-identical,
`loads 2` on every run, wall 283.034 -> 281.837 s, peak RSS 5090-5099 -> 5099 MiB.

The arithmetic gets a unit test on that board trace (`rknn_engine_test`): the served sequence must
load exactly two engines and end with a 450-frame resident set, and a 900-frame rung must not fit the
aperture on its own. It reproduces the recorded board numbers, so a budget that evicts a short rung
it should keep fails on a host build.
@wOvAN
wOvAN force-pushed the rknn-aperture-ladder branch from 0f333f0 to f32245c Compare October 17, 2026 22:38
@wOvAN wOvAN changed the title refactor(rknn): move the aperture ceiling and the ladder budget into the shared layer refactor(rknn): split the aperture accounting from the aperture measurements Oct 17, 2026
@0xShug0
0xShug0 merged commit da93d73 into 0xShug0:main Oct 17, 2026
6 checks passed
@0xShug0

0xShug0 commented Oct 17, 2026

Copy link
Copy Markdown
Owner

@wOvAN Thanks! PRs merged!

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