Skip to content

refactor(rknn): build every engine through one converter helper - #961

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:rknn-converter-build-helper
Oct 17, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
wOvAN:rknn-converter-build-helper

Conversation

@wOvAN

@wOvAN wOvAN commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

What moves

Follow-up to the review on #951, where «(3) is safe» was the shared converter build helper.

All five tools/rknn/prepare_* converters ended in the same five calls on the RKNPU2 Python API —
config, load_onnx, the register-field-gated build, export_rknn, release — written out five
times, with the fp16/single_core_mode comments repeated with them. The sequence now lives in
tools/rknn/rknn_build.py as build_engine(onnx, output, target, *, names, shapes, opt_level, verbose, gate_label, gate_log). The gate itself stays where it lives
(rknn_build_log.rknn_build_gate) and is still what wraps rknn.build.

Everything that produces the arguments stays in the converter: the ONNX surgery, the input names
and shapes it declares, the per-engine file naming, the print of what it wrote. CosyVoice3's
verify_fp16 is deliberately untouched — its build + init_runtime() + inference is the
simulator parity probe, not an engine export.

-54 net lines in the converters, +65 with the new module and its docstring.

Behaviour kept, behaviour changed

Kept per converter: optimization_level (CosyVoice3, Qwen3-TTS), RKNN(verbose=True) (Qwen3-TTS),
the gate labels (build <name>.rknn where that was the label, <name>.rknn where it was that) and
the log paths (<out>.build.log).

Changed: one set of failure messages. load_onnx failed / build failed (see the rknn log above) /
export_rknn failed everywhere, instead of also ERROR: rknn build failed: {ret} in the Qwen3
converter. The code in that message was always the same nonzero value.

Not touched: no C++, no spec, no model_specs, no docs beyond a three-line pointer in
docs/build/rockchip-npu.md next to the paragraph that documents the gate.

Validation

Host, x86_64, rknn-toolkit2 2.3.2 in a dedicated venv (onnx 1.22.0, numpy 1.26.4, onnxruntime
1.26.0), Python 3.12 with -I. No board involved — this path is PC-side conversion.

  • python3 -I -m py_compile tools/rknn/*.py → clean.

  • python3 -I tools/rknn/rknn_build_log.py (gate self-test) → rknn_build_log self-test ok.

  • Three real rk3588 builds through build_engine on a synthetic 4-input step graph
    (audio_codes int64 [1,16,4], freqs_in, cache_valid, cache_in), one per call shape the
    converters make:

    call shape used by result
    nothing declared (static graph), default gate label/log Inflect V2 28369-byte engine, a.build.log
    declared names/shapes + opt_level=3 CosyVoice3 flow 28369-byte engine, b.build.log
    str paths + verbose=True + explicit gate_label/gate_log Qwen3-TTS vocoder 28369-byte engine, c.rknn.build.log

    A synthetic graph is enough here because the thing under test is argument wiring — that
    load_onnx gets the declared names and shapes, that optimization_level reaches config, that
    the gate is still the thing wrapping build and still writes its log — not the engine's quality.

  • One converter end-to-end through its own CLI, real ONNX, real target:

    python3 -I tools/rknn/prepare_piper_tts_rknn_vocoder.py --onnx decoder_z.onnx \
        --output decoder_z.rknn --target rk3588 --window 93 --skip-check
    → wrote decoder_z.rknn (0.1 MB, inputs ['z', 'y_mask', 'g'], window 93)
    

    plus decoder_z.build.log.

  • python3 -I tools/check_loader_catalog_sync.py → ok: runtime loaders, model_specs, and model_manager_v2 are in sync.

Limitation, stated plainly: only the Piper converter was run as a whole script; the other four were
exercised through build_engine with their exact argument sets, and their non-build stages (ONNX
surgery, gates, verification) are untouched by this diff.

All five tools/rknn/prepare_* converters ended in the same five calls on the
RKNPU2 Python API -- config, load_onnx, the register-field-gated build,
export_rknn, release -- written out five times, with the fp16/single_core_mode
comments repeated with them. The sequence moves to tools/rknn/rknn_build.py as
build_engine(onnx, output, target, names=..., shapes=..., opt_level=...,
verbose=..., gate_label=..., gate_log=...); the gate itself stays where it
lives (rknn_build_log.rknn_build_gate) and is still what wraps rknn.build.

What stays per converter is everything that produces the arguments: the ONNX
surgery, the input names and shapes, per-engine file naming, and the
simulator-side verification (CosyVoice3's verify_fp16 keeps its own build +
init_runtime path, which is an inference call and not part of this).

Net -54 lines in the converters. The shared failure messages replace the
per-file variants ("build failed (see the rknn log above)" everywhere,
instead of also "ERROR: rknn build failed: {ret}" in the Qwen3 converter); the
return code was always the same nonzero value.

Validation (host, x86_64, rknn-toolkit2 2.3.2 in a dedicated venv):
- python3 -I -m py_compile tools/rknn/*.py
- python3 -I tools/rknn/rknn_build_log.py (gate self-test) -> ok
- three real rk3588 builds through build_engine on a synthetic 4-input step
  graph, one per call shape the converters make: static graph with nothing
  declared (Inflect), declared names+shapes with optimization_level
  (CosyVoice3), str paths + verbose + explicit gate label and log (Qwen3-TTS).
  All three export a 28369-byte engine and write their build log.
- one converter end-to-end through the CLI: prepare_piper_tts_rknn_vocoder.py
  --onnx decoder_z.onnx --output decoder_z.rknn --target rk3588 --window 93
  --skip-check -> "wrote decoder_z.rknn (0.1 MB, inputs ['z', 'y_mask', 'g'],
  window 93)" plus decoder_z.build.log.
- tools/check_loader_catalog_sync.py -> ok (no C++ or spec surface touched).
@0xShug0
0xShug0 merged commit d612a81 into 0xShug0:main Oct 17, 2026
6 checks passed
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