-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX] Store model's expected sample rate in Lightning checkpoints (#…
…357) * Rework sample_rate implementation to be storable in PyTorch artifacts * Store sample rate in checkpoint
- Loading branch information
1 parent
a2f54f0
commit a284650
Showing
5 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,13 @@ | |
# Created Date: Thursday March 16th 2023 | ||
# Author: Steven Atkinson ([email protected]) | ||
|
||
""" | ||
Tests for the base network and Lightning module | ||
""" | ||
|
||
import math | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from typing import Optional | ||
|
||
import numpy as np | ||
|
@@ -106,5 +111,46 @@ def mocked_loss( | |
assert obj._mrstft_device == "cpu" | ||
|
||
|
||
class TestSampleRate(object): | ||
""" | ||
Tests for sample_rate interface | ||
""" | ||
|
||
@pytest.mark.parametrize("expected_sample_rate", (None, 44_100.0, 48_000.0)) | ||
def test_on_init(self, expected_sample_rate: Optional[float]): | ||
model = _MockBaseNet(gain=1.0, sample_rate=expected_sample_rate) | ||
self._wrap_assert(model, expected_sample_rate) | ||
|
||
@pytest.mark.parametrize("expected_sample_rate", (None, 44_100.0, 48_000.0)) | ||
def test_setter(self, expected_sample_rate: Optional[float]): | ||
model = _MockBaseNet(gain=1.0) | ||
model.sample_rate = expected_sample_rate | ||
self._wrap_assert(model, expected_sample_rate) | ||
|
||
@pytest.mark.parametrize("expected_sample_rate", (None, 44_100.0, 48_000.0)) | ||
def test_state_dict(self, expected_sample_rate: Optional[float]): | ||
""" | ||
Assert that it makes it into the state dict | ||
https://github.com/sdatkinson/neural-amp-modeler/issues/351 | ||
""" | ||
model = _MockBaseNet(gain=1.0, sample_rate=expected_sample_rate) | ||
with TemporaryDirectory() as tmpdir: | ||
model_path = Path(tmpdir, "model.pt") | ||
torch.save(model.state_dict(), model_path) | ||
model2 = _MockBaseNet(gain=1.0) | ||
model2.load_state_dict(torch.load(model_path)) | ||
self._wrap_assert(model2, expected_sample_rate) | ||
|
||
@classmethod | ||
def _wrap_assert(cls, model: _MockBaseNet, expected: Optional[float]): | ||
actual = model.sample_rate | ||
if expected is None: | ||
assert actual is None | ||
else: | ||
assert isinstance(actual, float) | ||
assert actual == expected | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main() |