T-015 — Piano model file v1: T-007’s draft format fixed, written by Julia and validated by Rust
Status: done — D1–D7 accepted (maintainer, Dec 2026) · Area: julia, rust · Depends on: T-003, T-004 (T-007 for the content)
Read for that: Piano model file (incl. Draft format_version = 0), Model pipeline (row-major rule, agreement tests), the T-004 page (write_model, read_model), the T-007 page (H1, H2, H6, Final), models/piano/salamander-c5/2026-12-01/manifest.toml, ADR-0002, ADR-0003.
Why: T-007 wrote the fitted piano as format_version = 0, a draft whose rules (excitation, output, damper, golden events) are prose strings in the manifest and which nothing but Julia reads. T-008 (the Rust engine) needs a format it can rely on: a fixed list of tensors with shapes and dtypes, rules as structured fields, and a loader that refuses anything else (Piano model file, Rules). T-015 fixes that format as v1, writes it from Julia, and reads and validates it in Rust. It is the model-file half of T-008’s dependencies; T-017 (convolution) is the other.
What
- The v1 specification on the Piano model file page: every manifest field and tensor (name, dtype, shape in terms of K, S, Q, P, V, C, meaning, valid range), replacing the v0 draft section and the stage-2/3 tables that v1 does not contain (D1).
- Julia writer:
InstrumentsTraining.PianoModelFilewithwrite_piano_model(dir, tables, manifest; golden)andvalidate_piano_model(dir), built onwrite_model; it runs the same checks as Rust before writing and refuses a model that fails. T-007’s driver writes v1 through it;models/piano/salamander-c5/2026-12-01/is rewritten as v1 (D6). - Rust reader: a new crate
crates/model-format(D2): the generic part (safetensors header, dtype and shape checks, manifest with its generated[tensors]table, the golden file) andPianoModelV1, a struct of owned, validated arrays (D5). Loading happens off the audio thread; nothing in the crate is called fromprocess. - Golden file v1: events and expected output with fixed dtypes (D7); the Rust crate reads them (
PianoGolden). The comparison with an engine is T-008’s.
Decisions
| Question | Options | Proposal | |
|---|---|---|---|
| D1 | What v1 contains | (a) v0 formalised: the stage-1 measured modal model of T-007 (H1, H2): components on the string axis (S = 3: prompt, slow, slow_partner), Q = 1, impulse excitation partial.excite_db [V, K, S, Q, P], velocity.layers, complex partial.out_gain [C, K, S, Q, P, 2], freq_hz, decay, damped_decay, n_partials, has_damper, curves; the physical tables (hammer pulses, mode/bridge gains, soundboard IR, noises) become v2 when T-035/T-012/T-010 produce them; (b) v1 = the page’s full physical table list, with stage-1 data filling it (gain 1, an impulse “pulse”, a Dirac IR) | (a): a format describes what exists; (b) would freeze shapes for tables nobody has fitted yet and force T-008 to run a convolution of a Dirac. The manifest names model = "measured-modal" so a v2 can add "physical" beside it |
| D2 | Where the Rust reader lives | (a) a new crate crates/model-format (instruments-model-format), as Overview plans: generic safetensors + manifest + golden, plus the piano’s v1 schema; crates/dsp depends on it (T-008); (b) crates/dsp/src/piano/model.rs as Piano 13 sketches; (c) generic part in model-format, piano schema in dsp | (a): keeps file parsing, serde and TOML out of the DSP core; the standalone app and tools (a model inspector, later the synth) read models without the engine. Piano 13’s model.rs then holds the engine’s own layout (SoA, coefficients) built from PianoModelV1 |
| D3 | Rust dependencies | (a) safetensors (Hugging Face’s crate: header parsing and bounds checks, no unsafe needed on our side) + toml + serde (derive) for the manifest; (b) hand-written safetensors parser (~80 lines) and toml only | (a): the crate is the format’s reference implementation, small and without heavy dependencies; versions checked against the real crate source when implementing |
| D4 | The rules now written as prose in the manifest (excitation, velocity below the first layer, output, damper, golden events) | (a) structured fields Rust reads and Julia writes from the same constants: [excitation] kind = "impulse", below_first_layer_db_per_decade = 40.0, [output] kind = "re_complex_gain", [damper] placeholder = true (rule fixed by the format), [golden] max_abs_error_rel_peak, block_sizes, rate_hz; the prose moves to the model-file page; (b) keep the prose, parameters hard-coded in Rust | (a): only the values that a later model could change are fields; the mechanisms are fixed by format_version and documented once, on the page. Unknown fields are refused (deny_unknown_fields), so a typo cannot pass silently |
| D5 | What validation checks (both sides, same list) | (a) structure: format = "piano-modal", format_version = 1, model = "measured-modal", every expected tensor present with dtype and shape matching the manifest’s dimensions, no unexpected tensors, the manifest’s [tensors] table equal to the file’s header; ranges: K = 88 and first_midi = 21; 1 ≤ n_partials ≤ P; for valid partials 0 < freq_hz < reference_rate_hz/2, decay > 0, damped_decay ≥ decay of every component, excite_db and out_gain finite; invalid partials exactly zero; velocity.layers strictly increasing in (0, 127]; has_damper ∈ {0, 1}; license and sources present; (b) structure only | (a): the page’s rule “refused with an error, never half-loaded”. Partials above a lower host rate’s Nyquist are not an error: the engine culls them at activate (T-008). Each check gets a broken-model test on both sides (one fixture per rule, written by Julia tests to a temp dir; Rust builds its broken fixtures in memory) |
| D6 | The existing model salamander-c5/2026-12-01 (v0) | (a) rewrite it in place as v1 from the same tables (bytes of the tensors unchanged, manifest restructured, status dropped), commit; v0 is not supported anywhere; (b) a new version directory with today’s date; (c) keep v0 beside v1 | (a): the fit is the same, only its description changes; model_version dates the fit, not the file format. A Rust test loads the committed directory and checks a few known values (key count, A0’s first partial) |
| D7 | Golden events dtype | (a) events [E, 4] F32 as in v0 (sample index exact below 2²⁴ ≈ 5.8 min at 48 kHz) with that limit checked; (b) events.sample [E] U32, events.kind [E] U8, events.key [E] U8, events.velocity [E] F32; (c) events [E, 4] F64 | (b): each column in its natural type, no float index, and velocity stays fractional (CLAP’s note velocity is a float) |
Done when
Tests first. Julia: write_piano_model → validate_piano_model → read_model round trip on a small synthetic piano (e.g. P = 4, V = 2); one refused fixture per D5 rule; T-007’s tests still green; the rewritten salamander-c5/2026-12-01/ validates and its tensors equal the v0 tensors. Rust (cargo nextest run -p instruments-model-format): the committed model loads as PianoModelV1 with the expected shapes and a few spot values; the golden file loads with its events; every D5 rule has a failing case (missing tensor, wrong dtype, wrong shape, extra tensor, manifest/header mismatch, wrong version, unknown manifest field, NaN gain, decay ≤ 0, frequency at Nyquist, non-zero invalid partial, non-monotonic layers) that returns a descriptive error and no model. All gates green.
The Piano model file page holds the v1 spec (D1, D4, D5, D7) and keeps the physical tables as planned for v2; Overview lists crates/model-format as built; Piano 13’s model.rs line points to it; the backlog row and Status updated.
Out of scope: the engine and the golden-vector comparison (T-008, T-005), resampling sampled tables (v1 has none), Git LFS (the model is 1.5 MB), the physical stage-2 tables.
Outcome
- Spec: the Piano model file page holds format v1 (mechanisms, manifest, tables, golden vectors, the validation list) and the physical tables as planned for v2; the v0 draft section is gone.
- Julia:
training/src/PianoModelFile.jl(InstrumentsTraining.PianoModelFile:write_piano_model,validate_piano_model,validate_piano); T-007’sReferencePianogainedpiano_manifestandgolden_tensors, and its driver writes v1. Tests intraining/test/pianomodelfile.jl: a small synthetic piano (K = 88, P = 4, V = 2) round-trips with dtypes; 33 broken fixtures are each refused for the intended reason (the error message is checked) and nothing is written; a manifest whose[tensors]table differs from the file is refused on disk. - Rust:
crates/model-format(instruments-model-format, depssafetensors0.7,serde,toml0.9):Array<T>(owned, row-major,at,row),piano::PianoModelV1::{load, from_bytes},piano::PianoGolden::{load, from_bytes},Error::{Io, SafeTensors, Manifest, Invalid}. The manifest is parsed in two steps (format and version first, so a v0 file gets “format_version must be 1”; then the full schema withdeny_unknown_fields). Tests (tests/piano_v1.rs): the same small piano built in memory, 27 broken models and 9 broken golden files each refused with the expected message, a truncated safetensors file, a missing directory, and the committed Salamander model with its golden vectors (shapes, layers, A0’s f1, dampers, 19 events, peak 0.85). - The model
models/piano/salamander-c5/2026-12-01/rewritten as v1 (D6) from the rebuilt tables, which equal v0’s except for one fix the validation found: v0 held the −200 dB floor inpartial.excite_dbbeyondn_partials(34 464 entries, never computed); v1 requires zeros there, sopiano_tensorsnow writes zeros. Golden output and events identical to v0 (events now in columns, D7);fit.commitkeeps the fit’s commit. - Gates:
cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo nextest run(32 tests, 6 of them new), JuliaPkg.test(). No plugin code changed.