v1.7.1 release: GuitarML Proteus/NeuralPi model importer
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
// ProteusModelImporterTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("ProteusModelImporter")
|
||||
struct ProteusModelImporterTests {
|
||||
|
||||
private func makeModel(hiddenSize h: Int, residual: Bool) -> NeuralAmpModel {
|
||||
let gated: Int = NeuralAmpModel.gateCount * h
|
||||
let wih: [Double] = (0..<gated).map { 0.2 * Double($0 + 1) }
|
||||
let whh: [Double] = (0..<(gated * h)).map { -0.05 * Double($0 + 1) }
|
||||
let bih: [Double] = (0..<gated).map { 0.01 * Double($0) }
|
||||
let bhh: [Double] = (0..<gated).map { 0.02 * Double($0) }
|
||||
let wout: [Double] = (0..<h).map { 0.3 * Double($0 + 1) }
|
||||
return NeuralAmpModel(
|
||||
hiddenSize: h, residual: residual,
|
||||
weightIH: wih, weightHH: whh, biasIH: bih, biasHH: bhh,
|
||||
denseWeight: wout, denseBias: 0.07)
|
||||
}
|
||||
|
||||
/// Serializes a `NeuralAmpModel` back into the Proteus/NeuralPi file schema (the inverse of
|
||||
/// the importer), so a round-trip proves the flatten/gate mapping is consistent.
|
||||
private func proteusData(from m: NeuralAmpModel, unitType: String = "LSTM") throws -> Data {
|
||||
let h: Int = m.hiddenSize
|
||||
let gated: Int = NeuralAmpModel.gateCount * h
|
||||
let weightIH: [[Double]] = (0..<gated).map { [m.weightIH[$0]] }
|
||||
let weightHH: [[Double]] = (0..<gated).map { r in (0..<h).map { m.weightHH[r * h + $0] } }
|
||||
let linWeight: [[Double]] = [m.denseWeight]
|
||||
let object: [String: Any] = [
|
||||
"model_data": [
|
||||
"unit_type": unitType, "input_size": 1, "hidden_size": h,
|
||||
"num_layers": 1, "output_size": 1, "skip": m.residual ? 1 : 0,
|
||||
],
|
||||
"state_dict": [
|
||||
"rec.weight_ih_l0": weightIH,
|
||||
"rec.weight_hh_l0": weightHH,
|
||||
"rec.bias_ih_l0": m.biasIH,
|
||||
"rec.bias_hh_l0": m.biasHH,
|
||||
"lin.weight": linWeight,
|
||||
"lin.bias": [m.denseBias],
|
||||
],
|
||||
]
|
||||
return try JSONSerialization.data(withJSONObject: object)
|
||||
}
|
||||
|
||||
private func expectClose(_ a: [Double], _ b: [Double], _ label: String) {
|
||||
#expect(a.count == b.count, "\(label): count \(a.count) != \(b.count)")
|
||||
for i: Int in 0..<Swift.min(a.count, b.count) {
|
||||
#expect(abs(a[i] - b[i]) < 1.0e-12, "\(label)[\(i)]: \(a[i]) != \(b[i])")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func importRoundTripsModel() throws {
|
||||
let original: NeuralAmpModel = makeModel(hiddenSize: 20, residual: true)
|
||||
let data: Data = try proteusData(from: original)
|
||||
let imported: NeuralAmpModel = try ProteusModelImporter.model(fromProteusJSON: data)
|
||||
|
||||
#expect(imported.hiddenSize == original.hiddenSize)
|
||||
#expect(imported.residual == original.residual)
|
||||
expectClose(imported.weightIH, original.weightIH, "weightIH")
|
||||
expectClose(imported.weightHH, original.weightHH, "weightHH")
|
||||
expectClose(imported.biasIH, original.biasIH, "biasIH")
|
||||
expectClose(imported.biasHH, original.biasHH, "biasHH")
|
||||
expectClose(imported.denseWeight, original.denseWeight, "denseWeight")
|
||||
#expect(abs(imported.denseBias - original.denseBias) < 1.0e-12)
|
||||
}
|
||||
|
||||
@Test func skipZeroMapsToNoResidual() throws {
|
||||
let original: NeuralAmpModel = makeModel(hiddenSize: 4, residual: false)
|
||||
let data: Data = try proteusData(from: original)
|
||||
let imported: NeuralAmpModel = try ProteusModelImporter.model(fromProteusJSON: data)
|
||||
#expect(imported.residual == false)
|
||||
}
|
||||
|
||||
@Test func unsupportedUnitTypeThrows() throws {
|
||||
let data: Data = try proteusData(from: makeModel(hiddenSize: 4, residual: true), unitType: "GRU")
|
||||
#expect(throws: ProteusModelImporter.ImportError.self) {
|
||||
_ = try ProteusModelImporter.model(fromProteusJSON: data)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func inconsistentShapeThrows() throws {
|
||||
// hidden_size says 4 but the recurrent matrix rows won't be 16 → shape error.
|
||||
let object: [String: Any] = [
|
||||
"model_data": [
|
||||
"unit_type": "LSTM", "input_size": 1, "hidden_size": 4,
|
||||
"num_layers": 1, "output_size": 1, "skip": 1,
|
||||
],
|
||||
"state_dict": [
|
||||
"rec.weight_ih_l0": [[0.1], [0.2]], // 2 rows, expected 16
|
||||
"rec.weight_hh_l0": [[0.1, 0.2, 0.3, 0.4]],
|
||||
"rec.bias_ih_l0": [0.0, 0.0],
|
||||
"rec.bias_hh_l0": [0.0, 0.0],
|
||||
"lin.weight": [[0.1, 0.2, 0.3, 0.4]],
|
||||
"lin.bias": [0.0],
|
||||
],
|
||||
]
|
||||
let data: Data = try JSONSerialization.data(withJSONObject: object)
|
||||
#expect(throws: ProteusModelImporter.ImportError.self) {
|
||||
_ = try ProteusModelImporter.model(fromProteusJSON: data)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func importedModelRunsInLSTMAmp() throws {
|
||||
// The imported model must drive LSTMAmp without shape errors and produce finite output.
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
let original: NeuralAmpModel = makeModel(hiddenSize: 8, residual: true)
|
||||
let imported: NeuralAmpModel = try ProteusModelImporter.model(fromProteusJSON: proteusData(from: original))
|
||||
let amp: LSTMAmp = LSTMAmp(input: OscSin.Audio(freq: 220.0, mul: 0.5), model: imported)
|
||||
let out: [Double] = collectBlocks(amp, engine: engine, blocks: 20)
|
||||
#expect(out.allSatisfy { $0.isFinite })
|
||||
#expect(out.contains { abs($0) > 1.0e-6 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user