-
Notifications
You must be signed in to change notification settings - Fork 243
Compensate metadata.loudness when _ScaleOutputHook scales head_scale #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sdatkinson
merged 1 commit into
sdatkinson:main
from
tone-3000:fix/export-loudness-reflects-compensated-head-scale
May 22, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -218,6 +218,45 @@ def test_packed_export_writes_slimmable_container(tmp_path): | |
| _assert_container_contains_two_wavenets(container) | ||
|
|
||
|
|
||
| def test_packed_export_refreshes_loudness_after_head_scale_compensation(tmp_path): | ||
| """ | ||
| When an export hook scales `head_scale` (e.g. the dataset normalization | ||
| handshake), the exported `metadata.loudness` must describe the *compensated* | ||
| model that will be loaded at inference, not the pre-compensation snapshot. | ||
|
|
||
| Output of WaveNet (no top-level head) and SlimmableContainer is linear in | ||
| `head_scale`, so loudness moves by `20 * log10(scale)` exactly. | ||
| """ | ||
| import math as _math | ||
|
|
||
| model = _PackedWaveNet.init_from_config({**_packed_config(), "sample_rate": 48_000}) | ||
| pre_container = model.export_container(tmp_path) | ||
| pre_container_loudness = pre_container["metadata"]["loudness"] | ||
| pre_submodel_loudnesses = [ | ||
| entry["model"]["metadata"]["loudness"] | ||
| for entry in pre_container["config"]["submodels"] | ||
| ] | ||
|
|
||
| scale = 2.0 | ||
| model.export_model_dict_post_hooks.append(_data.Dataset._ScaleOutputHook(scale=scale)) | ||
| post_container = model.export_container(tmp_path) | ||
|
|
||
| offset_db = 20.0 * _math.log10(scale) | ||
| assert post_container["metadata"]["loudness"] == _pytest.approx( | ||
| pre_container_loudness + offset_db, abs=1e-3 | ||
| ) | ||
| for entry, pre_loudness in zip( | ||
| post_container["config"]["submodels"], pre_submodel_loudnesses | ||
| ): | ||
| assert entry["model"]["metadata"]["loudness"] == _pytest.approx( | ||
| pre_loudness + offset_db, abs=1e-3 | ||
| ) | ||
| # head_scale was actually compensated on disk | ||
| assert entry["model"]["config"]["head_scale"] == _pytest.approx( | ||
| 0.25 * scale | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where's the Chan you make this a const in this file |
||
| ) | ||
|
|
||
|
|
||
| def test_packed_export_applies_model_dict_post_hooks(tmp_path): | ||
| model = _PackedWaveNet.init_from_config({**_packed_config(), "sample_rate": 48_000}) | ||
| model.export_model_dict_post_hooks.append(_data.Dataset._ScaleOutputHook(scale=2.0)) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eek, these aren't supposed to be the averages of the values in the submodels 😅
It's just a test, but I think I'd prefer for these to track the values of the highest-quality submodel. If that's not happening already, then that's a bug that should also be squashed.
Ideally, there'd be validation (i.e. Pydantic) to enforce this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the end of the world if it's already happening elsewhere though--I have to admit I'm not sure I know the answer off the top of my head.