Skip to content

omnivoice: harden the rknn_zeroop packed-weight disk cache - #1302

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:omnivoice-rknn-zeroop-cache
Nov 15, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:omnivoice-rknn-zeroop-cache

Conversation

@wOvAN

@wOvAN wOvAN commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to the merged zero-op generator (#1297): harden the packed-weight disk cache, which
was the one thing the review round flagged as easy to get wrong and left for a second PR.

What was fragile

pack_weights() cached the per-(layer, shape) packed B operands under
<cache_dir>/ovz_<fingerprint>.<layer>.<shape>.bin and a warm run trusted that cache on file
size alone
. Three holes:

  • Fresh install: nothing created cache_dir, so on a clean checkout the first fopen(..., "wb") failed on every file, the cache was silently never written, and every run paid the full
    cold pack.
  • Failed / partial writes: fwrite's return was ignored and files were written in place, so
    a full disk, an ENOSPC mid-write, or a crash mid-pack left a short file the size-check caught
    (good) - but the real exposure is the inverse, a crash that left a full-size file with a torn
    tail, which the size-check accepted as warm.
  • Invalidation / bit-rot: the fingerprint (added in omnivoice: run the generator on the vendor zero-op matmul API (rknn_zeroop) #1297) pins the source weights, so a
    swapped checkpoint repacks; but a cache file that got bit-rotted or half-committed on disk to
    the right size was loaded and synced to the NPU as if valid.

What it does now

  • pack_weights() creates cache_dir (with std::filesystem::create_directories) when caching
    is on, so a fresh install populates the cache on its first run.
  • Each cache file is [packed body][FNV-1a-64 trailer], published atomically: mkstemp a
    unique temp in the cache dir (unique across pid namespaces, so containerized deploys sharing a
    bind-mounted model dir cannot collide on one temp name), fflush + fsync, then rename
    over the final name. A half-written pack can never masquerade as a complete file, and the
    temp is unlinked on any failure. Temps left by a process killed mid-pack are swept at the
    start of the next pack_weights() (they are never read back, so they are a pure disk leak).
  • The warm path loads each file and verifies the FNV-1a-64 trailer over the body before
    syncing it to the NPU. Any miss (missing/truncated/corrupt file, bad checksum, failed device
    sync) drops to the cold pack-and-write path, so the engine self-heals instead of running on
    garbage, and names the file in the warning (a silent warm->cold flip is otherwise visible
    only as a pack_ms jump).
  • The cold pack zeroes each device buffer before pack_b, so file + trailer never carry
    uninitialized device-pool bytes if a future librknnrt rounds B.size above the packed grid.
  • Cache-directory creation and cache writes are non-fatal: a failure records a one-line
    warning on the engine, rknn_zeroop_generator surfaces it through
    debug::log_message(Warning, "omnivoice.rknn_zeroop", ...), and the run proceeds by
    repacking. A read-only cache directory costs a warm start, not a session.

The file-format change (adding the 8-byte trailer) self-invalidates the pre-trailer caches: the
old files fail the size check (size != wsz + 8) and repack, so no fingerprint-tag bump is
needed and the transition needs no migration step.

Validation (RK3588 board, twin geometry, seed 42, g=2.0)

The twin gate renders a byte-identical WAV when the cache is correct, so all five gates check
that md5 == 31eaa272118dd19fad97ac326a72f91d (the established twin hash) is preserved.

  • cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DENGINE_ENABLE_RKNN=ON -DENGINE_RKNN_INCLUDE_DIR=/usr/local/include -DENGINE_RKNN_LIBRARY=/usr/lib/librknnrt.so -DAUDIOCPP_MODEL_SET=custom -DAUDIOCPP_MODELS=omnivoice then
    cmake --build build --target audiocpp_cli; RC=0.
  • Twin run used by all five gates (cache dir default <model_directory>/rknn-zeroop-cache):
    ./build/bin/audiocpp_cli --task clon --family omnivoice --model models/OmniVoice-GGUF/omnivoice-f16.gguf --backend cpu --threads 4 --text "<2 Russian sentences>" --voice-ref assets/resources/b.wav --reference-text "Some call me nature..." --seed 42 --guidance-scale 2.0 --session-option 'omnivoice.exec_plan={"generator":{"impl":"rknn_zeroop"}}'
  • Gate A (fresh install): cache dir removed, twin run - the directory is created, 112 pack
    files written (28 layers x 4 shapes), pack_ms 8797.9 (cold pack), audio md5 matches.
  • Gate B (warm load): second twin run - pack_ms 2868.9 (checksum-verified warm load),
    audio md5 matches, zero cache warnings in the log.
  • Gate C (write failure): cache dir made immutable (chattr +i; chmod 500 is not a gate
    for root, it has CAP_DAC_OVERRIDE), twin run - rc=0, audio md5 matches, the directory is
    left empty (mkstemp temp + rename: nothing survives), and the log carries
    cache: write failed for .../ovz_omnivoice-zeroop-v1-<fp>.0.0.bin.
  • Gate D (bit-rot): one warm pack file flipped mid-file (full size, corrupt body), twin run
    • the checksum mismatch drops it to a cold repack (pack_ms 9831.6), audio md5 matches, and
      the log carries cache: warm miss at .../ovz_...0.0.bin (read/checksum), repacking (the
      repack rewrites all 112 packs: the miss check is per-file, the fallback is deliberately
      global - 8 s of repack beats per-file bookkeeping).
  • Gate E (killed packer): twin run pkill -9 -x audiocpp_cli mid cold pack (112-file dir
    had 9 final + 1 .tmp.XXXXXX at the kill), then relaunch - the sweep removes the orphaned
    temp (rerun leaves 112 final, 0 tmp), full cold repack pack_ms 8880.3, audio md5 matches.

x86: ovz_engine.cpp (stub tier) and rknn_zeroop_generator.cpp pass
g++ -fsyntax-only -std=c++17 -DENGINE_HAVE_RKNN=1; tools/check_loader_catalog_sync.py
passes. The warm-path FNV is ~19 MB/layer over 112 files, well under the NPU pack/sync time, so
warm pack_ms is unchanged for practical purposes.

Known limitations

  • Cache GC: repacked fingerprint versions of the cache are not pruned; an old cache dir from a
    swapped checkpoint lingers until removed by hand (~812 MB per fingerprint). Out of scope here.
  • One bad pack file triggers a full repack of all 112 (per-file detection, global fallback) -
    ~8 s on the board, accepted for the simplicity.
  • No unit tests: the path needs a board. The stub tier keeps non-aarch64 builds compiling.

The zero-op generator's cache (rknn_zeroop) trusted a warm pack file on
its size alone, never created the cache directory, and wrote pack files
in place with unchecked fwrite. Three consequences: a fresh install
never populates the cache (silent cold pack every run); a crash, ENOSPC
or power loss can leave a full-size file with a torn tail that the size
check accepts and the NPU then runs on; and any cache I/O failure was a
hard pack error that killed the session.

- create_directories() the cache dir when caching is on
- each cache file is [packed body][FNV-1a-64 trailer], published
  atomically (per-pid .tmp, fflush+fsync, rename, tmp unlinked on
  failure); pre-trailer caches self-invalidate via the size check, so
  the format change needs no fingerprint bump
- the warm path verifies every trailer before syncing to the NPU; any
  miss (missing/truncated/corrupt/bad checksum) falls back to a cold
  repack instead of failing
- mkdir/write/flush failures are non-fatal: recorded in a new
  Engine::last_warning() and logged by the generator; the run repacks

Board gates (RK3588, twin geometry, seed 42, g=2.0, all audio md5
31eaa272118dd19fad97ac326a72f91d): fresh install creates the dir and
writes 112 files (pack_ms 8135.8); warm load verifies (2844.9);
chattr +i dir -> rc=0, empty dir, no .tmp, warning logged; flipping two
bytes mid-file in one full-size pack -> checksum miss, cold repack
(9317.2). x86: ovz_engine.cpp stub tier passes g++ -fsyntax-only with
ENGINE_HAVE_RKNN=1; check_loader_catalog_sync.py passes.

Co-Authored-By: Claude <[email protected]>
Performed-by: Claude-Fable-5
@0xShug0
0xShug0 merged commit da95c6a into 0xShug0:main Nov 15, 2026
7 checks passed
@0xShug0

0xShug0 commented Nov 15, 2026

Copy link
Copy Markdown
Owner

@wOvAN Thanks! PR mergd.

One minor issue: Servers may register the same OmniVoice checkpoint under two different IDs, such as voice-a and voice-b. If both entries use rknn_zeroop and load on first request, simultaneous first requests can make them write to the same temporary file when the cache is empty.

@wOvAN

wOvAN commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Merged before the review-round amend landed - the fixes (temp sweep after a killed packer, mkstemp publish, named warm-miss warnings, pre-pack zeroing, docs' warm-load number) continued as #1303 with the same board gates (p18-p23, twin md5 parity).

0xShug0 pushed a commit that referenced this pull request Nov 16, 2026
Follow-up to #1302 (merged as the pre-review commit; these fixes were
validated on the board in the same session, p18-p23):

- sweep *.tmp.* temps at the start of pack_weights: a process killed
  mid-cold-pack leaked up to 112 ~7 MB temps into the cache dir forever
  (only exact final names are ever read back)
- publish temps via mkstemp instead of a per-pid name: pid namespaces
  sharing a bind-mounted model dir collide on ...bin.<pid>.tmp
- the warm->cold fallback (the main new path) now names the file in
  last_warning(): cache: warm miss at <file> (read/checksum) | (device
  sync), repacking - a silent repack was visible only as a pack_ms
  jump, and a failed device sync masqueraded as a cache miss
- zero each device buffer before pack_b so file + trailer never carry
  uninitialized device-pool bytes if a future librknnrt rounds B.size
  above the packed grid
- docs: warm load is ~2.8 s (the FNV verification pass), not ~1.1 s;
  document the sweep, mkstemp, the warm-miss warning, and cache growth
  (~812 MB per fingerprint, never pruned)

Board gates (RK3588 twin, seed 42, g=2.0, all audio md5
31eaa272118dd19fad97ac326a72f91d): fresh install cold pack 8797.9 ms,
112 files; warm 2868.9 ms, zero warnings; chattr +i dir -> rc=0, dir
empty, write-failed warning; mid-file corruption -> warm-miss warning
names the file, repack 9831.6; kill -9 at 9 final + 1 tmp -> rerun
leaves 112 final / 0 tmp, 8880.3. x86 stub tier g++ -fsyntax-only with
ENGINE_HAVE_RKNN=1 and check_loader_catalog_sync.py pass.


Performed-by: Claude-Fable-5

Co-authored-by: wOvAN <[email protected]>
Co-authored-by: Claude <[email protected]>
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