From ea0aa84ca796d9a21667e4b5a7d76a1ac2c280b6 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sat, 1 Aug 2026 20:31:38 -0700 Subject: [PATCH] v1.7.1 release: GuitarML Proteus/NeuralPi model importer Co-Authored-By: Claude Opus 4.8 --- RELEASES.md | 12 ++ .../Lyrebird/Generators/Neural/LSTMAmp.swift | 4 +- .../Generators/Neural/NeuralAmpModel.swift | 7 +- .../Neural/ProteusModelImporter.swift | 173 ++++++++++++++++++ .../Neural/ProteusModelImporterTests.swift | 122 ++++++++++++ docs/stdlib/generators.md | 20 +- 6 files changed, 330 insertions(+), 8 deletions(-) create mode 100644 Sources/Lyrebird/Generators/Neural/ProteusModelImporter.swift create mode 100644 Tests/LyrebirdTests/Generators/Neural/ProteusModelImporterTests.swift diff --git a/RELEASES.md b/RELEASES.md index c408ea0..ecf4ccb 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -7,6 +7,18 @@ functionality in a backward-compatible way. --- +## 1.7.1 — Neural-amp model importer + +A patch release. One backward-compatible addition: + +- **`ProteusModelImporter`** — loads a GuitarML **Proteus / NeuralPi** model JSON (single-layer + LSTM capture) into a `NeuralAmpModel` for `LSTMAmp`. The state-dict tensors map straight onto + `NeuralAmpModel`'s layout (PyTorch gate order; `skip` → residual); unsupported topologies and + inconsistent tensor shapes throw. Model files are user-supplied — none are bundled (GuitarML + models are GPL-3.0). A binary NAM `.nam` importer remains future work. + +--- + ## 1.7.0 — Amp-modeling core: tone stack, triode stage, neural amp A minor release. Three new, backward-compatible generators — the DSP core for guitar-amp diff --git a/Sources/Lyrebird/Generators/Neural/LSTMAmp.swift b/Sources/Lyrebird/Generators/Neural/LSTMAmp.swift index f79eb47..40be34a 100644 --- a/Sources/Lyrebird/Generators/Neural/LSTMAmp.swift +++ b/Sources/Lyrebird/Generators/Neural/LSTMAmp.swift @@ -21,8 +21,8 @@ import Darwin /// Output is mono. The `Double` output is whatever the trained model produces; a well-trained /// capture stays roughly in `[-1, 1]`, but this generator does not clamp. /// -/// > Note: An importer for external NAM `.nam` / Proteus JSON model files onto -/// > ``NeuralAmpModel`` is a planned follow-up; construct a `NeuralAmpModel` directly for now. +/// > Note: Load a GuitarML Proteus / NeuralPi JSON model with ``ProteusModelImporter``, or +/// > construct a ``NeuralAmpModel`` directly. A binary NAM `.nam` importer is a planned follow-up. /// /// - Parameters: /// - input: Signal to process (dry guitar DI). diff --git a/Sources/Lyrebird/Generators/Neural/NeuralAmpModel.swift b/Sources/Lyrebird/Generators/Neural/NeuralAmpModel.swift index 726d1dc..12df1ee 100644 --- a/Sources/Lyrebird/Generators/Neural/NeuralAmpModel.swift +++ b/Sources/Lyrebird/Generators/Neural/NeuralAmpModel.swift @@ -20,10 +20,9 @@ /// - `biasIH`, `biasHH` — `4·H` each (PyTorch keeps two bias vectors; they are summed). /// - `denseWeight` — `H` entries, `denseBias` — scalar. /// -/// This is Lyrebird's native, `Codable` container. An importer for external NAM `.nam` / -/// Proteus JSON model files is a planned follow-up (it maps those schemas onto this layout); -/// it is intentionally not included here so the format mapping can be validated against real -/// model files rather than guessed. +/// This is Lyrebird's native, `Codable` container. Load a GuitarML Proteus / NeuralPi JSON +/// model onto this layout with ``ProteusModelImporter``; a binary NAM `.nam` importer is a +/// planned follow-up. public struct NeuralAmpModel: Codable, Sendable { /// Number of LSTM hidden units. diff --git a/Sources/Lyrebird/Generators/Neural/ProteusModelImporter.swift b/Sources/Lyrebird/Generators/Neural/ProteusModelImporter.swift new file mode 100644 index 0000000..db85299 --- /dev/null +++ b/Sources/Lyrebird/Generators/Neural/ProteusModelImporter.swift @@ -0,0 +1,173 @@ +// ProteusModelImporter.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import Foundation + +/// Imports a GuitarML **Proteus / NeuralPi** model JSON into a ``NeuralAmpModel``. +/// +/// Those tools export a single-layer PyTorch LSTM as JSON with two objects: +/// +/// ```json +/// { "model_data": { "unit_type": "LSTM", "input_size": 1, "hidden_size": 20, +/// "num_layers": 1, "skip": 1, "output_size": 1 }, +/// "state_dict": { "rec.weight_ih_l0": [[…]], // 4H × input_size +/// "rec.weight_hh_l0": [[…]], // 4H × H +/// "rec.bias_ih_l0": [ … ], // 4H +/// "rec.bias_hh_l0": [ … ], // 4H +/// "lin.weight": [[…]], // output_size × H +/// "lin.bias": [ … ] } } // output_size +/// ``` +/// +/// PyTorch stacks the LSTM gates in the order **input, forget, cell, output** (i, f, g, o) — +/// the same order ``NeuralAmpModel`` documents — so the tensors flatten straight onto its +/// layout. `skip == 1` maps to ``NeuralAmpModel/residual``. +/// +/// Only the single-layer, `input_size == 1`, `output_size == 1`, `unit_type == "LSTM"` shape +/// (the Proteus/NeuralPi capture topology `LSTMAmp` runs) is supported; anything else throws. +/// +/// > The model files themselves are **not** redistributed with Lyrebird — this only parses a +/// > file the user supplies. GuitarML models are GPL-licensed; keep them out of MIT bundles. +public enum ProteusModelImporter { + + /// Errors thrown while importing a Proteus/NeuralPi model. + public enum ImportError: Error, Equatable { + /// `model_data.unit_type` was not `"LSTM"` (the only recurrent unit `LSTMAmp` runs). + case unsupportedUnitType(String) + /// A tensor shape (or `input_size`/`output_size`/`num_layers`) was inconsistent with the + /// single-layer, mono LSTM topology or with `hidden_size`. The message names the field. + case unsupportedShape(String) + } + + private static let supportedInputSize: Int = 1 + private static let supportedOutputSize: Int = 1 + private static let supportedLayerCount: Int = 1 + + /// Parses Proteus/NeuralPi model JSON into a validated ``NeuralAmpModel``. + /// + /// - Parameter data: The raw JSON file contents. + /// - Returns: The imported model. + /// - Throws: `DecodingError` if the JSON is malformed, or ``ImportError`` if the topology is + /// unsupported or a tensor shape is inconsistent with `hidden_size`. + public static func model(fromProteusJSON data: Data) throws -> NeuralAmpModel { + let file: ProteusFile = try JSONDecoder().decode(ProteusFile.self, from: data) + let meta: ProteusFile.ModelData = file.modelData + let dict: ProteusFile.StateDict = file.stateDict + + guard meta.unitType.uppercased() == "LSTM" else { + throw ImportError.unsupportedUnitType(meta.unitType) + } + let hidden: Int = meta.hiddenSize + guard hidden > 0 else { throw ImportError.unsupportedShape("hidden_size must be > 0; got \(hidden)") } + guard meta.numLayers == supportedLayerCount else { + throw ImportError.unsupportedShape("only num_layers == 1 is supported; got \(meta.numLayers)") + } + guard meta.inputSize == supportedInputSize else { + throw ImportError.unsupportedShape("only input_size == 1 is supported; got \(meta.inputSize)") + } + guard meta.outputSize == supportedOutputSize else { + throw ImportError.unsupportedShape("only output_size == 1 is supported; got \(meta.outputSize)") + } + + let gated: Int = NeuralAmpModel.gateCount * hidden + + // rec.weight_ih_l0: 4H × input_size(1) → 4H (take the single column). + guard dict.weightIH.count == gated else { + throw ImportError.unsupportedShape("rec.weight_ih_l0 rows must be 4·hidden (\(gated)); got \(dict.weightIH.count)") + } + var weightIH: [Double] = [Double](repeating: 0.0, count: gated) + for r: Int in 0..