Skip to content

The open-source AI voice studio. Clone, dictate, create.

style: normalize line endings in test_omnivoice_compat.py

SKALb32b97057f6f
diff --git a/backend/tests/test_omnivoice_compat.py b/backend/tests/test_omnivoice_compat.py
index 94d0f745..ed959ff2 100644
--- a/backend/tests/test_omnivoice_compat.py
+++ b/backend/tests/test_omnivoice_compat.py
@@ -1,265 +1,265 @@
-"""
-Unit tests for the OmniVoice engine's transformers 5 -> 4.57.6 compatibility layer.
-
-OmniVoice is built against transformers >= 5.3; Voicebox is pinned to <= 4.57.6
-because several engine workarounds read 4.57.x internals. ``backend/vendor/``
-carries the one class that gap costs us (the Higgs Audio V2 codec) and
-``backend/utils/transformers5_compat.py`` grafts it on.
-
-None of this downloads a model or touches a GPU. The full round trip — load,
-clone, generate, unload — is covered by the manual E2E suite described in
-``backend/tests/E2E_MODEL_TEST_DESIGN.md``.
-
-NOTE: ``patch_extra_special_tokens`` mutates ``transformers.PreTrainedTokenizerBase``
-globally; run serially, not under ``pytest-xdist`` with per-worker isolation.
-"""
-
-import sys
-
-import pytest
-import torch
-from torch import nn
-from transformers.tokenization_utils_base import PreTrainedTokenizerBase
-
-import backend.utils.transformers5_compat as compat
-from backend.vendor import _compat
-
-
-# ── conv1d_output_length ──────────────────────────────────────────────
-# Absent from 4.57.6's audio_utils; the vendored codec computes encoder
-# frame counts with it.
-
-
- "kwargs,length,expected",
- [
- ({"kernel_size": 1, "stride": 1}, 100, 100),
- ({"kernel_size": 3, "stride": 1, "padding": 1}, 100, 100),
- ({"kernel_size": 4, "stride": 2, "padding": 1}, 100, 50),
- ({"kernel_size": 3, "stride": 1, "dilation": 2, "padding": 2}, 100, 100),
- ],
-)
-def test_conv1d_output_length_matches_torch(kwargs, length, expected):
- layer = nn.Conv1d(1, 1, **kwargs)
- assert _compat.conv1d_output_length(layer, length) == expected
- # Cross-check against what the layer actually produces.
- actual = layer(torch.zeros(1, 1, length)).shape[-1]
- assert _compat.conv1d_output_length(layer, length) == actual
-
-
-# ── initialization module ─────────────────────────────────────────────
-# 5.x-only. Only reached from _init_weights, i.e. for weights missing from
-# the checkpoint.
-
-
-def test_initialization_functions_mutate_in_place_and_return_tensor():
- tensor = torch.empty(4, 4)
- result = _compat.initialization.zeros_(tensor)
- assert result is tensor
- assert torch.all(result == 0)
-
- assert torch.all(_compat.initialization.ones_(torch.empty(3)) == 1)
- assert _compat.initialization.normal_(torch.empty(8), mean=0.0, std=0.02) is not None
-
-
-def test_initialization_copy_writes_through():
- target = torch.zeros(2)
- source = torch.tensor([1.0, 2.0])
- assert torch.equal(_compat.initialization.copy_(target, source), source)
-
-
-# ── PreTrainedConfig dataclass adapter ────────────────────────────────
-# 5.x configs are dataclasses whose generated __init__ ends in __post_init__.
-# 4.57.6's PretrainedConfig never calls it, which leaves sub-configs as raw
-# dicts and blows up later in to_diff_dict().
-
-
-class _SampleConfig(_compat.PreTrainedConfig):
- model_type = "sample_compat_config"
-
- plain_field: int = 7
- listy_field: list = None
- nested: dict = None
-
- def __post_init__(self, **kwargs):
- if isinstance(self.nested, dict):
- self.nested = dict(self.nested, promoted=True)
- super().__post_init__(**kwargs)
-
-
-def test_annotated_defaults_are_applied():
- config = _SampleConfig()
- assert config.plain_field == 7
- assert config.nested is None
-
-
-def test_kwargs_override_defaults():
- config = _SampleConfig(plain_field=42)
- assert config.plain_field == 42
-
-
-def test_post_init_runs():
- config = _SampleConfig(nested={"a": 1})
- assert config.nested == {"a": 1, "promoted": True}
-
-
-def test_mutable_defaults_are_not_shared_between_instances():
- class _ListConfig(_compat.PreTrainedConfig):
- model_type = "list_compat_config"
- items: list = []
-
- first, second = _ListConfig(), _ListConfig()
- first.items.append("x")
- assert second.items == []
-
-
-def test_leftover_kwargs_reach_pretrained_config():
- config = _SampleConfig(plain_field=1, output_hidden_states=True)
- assert config.output_hidden_states is True
-
-
-# ── auto_docstring / strict no-ops ────────────────────────────────────
-# Both raise on 4.57.6 for the vendored class: auto_docstring rejects names
-# outside its registry, strict requires a dataclass.
-
-
-def test_auto_docstring_returns_target_unchanged():
- class Target:
- pass
-
- assert _compat.auto_docstring(Target) is Target
- assert _compat.auto_docstring(custom_intro="x")(Target) is Target
-
-
-def test_strict_returns_target_unchanged():
- class Target:
- pass
-
- assert _compat.strict(Target) is Target
- assert _compat.strict()(Target) is Target
-
-
-# ── extra_special_tokens list form ────────────────────────────────────
-# OmniVoice's tokenizer_config.json writes a list (5.x form); 4.57.6 calls
-# .keys() on it.
-
-
-def restore_special_tokens_hook():
- saved = PreTrainedTokenizerBase.__dict__.get("_set_model_specific_special_tokens")
- yield
- if saved is not None:
- PreTrainedTokenizerBase._set_model_specific_special_tokens = saved
-
-
-def test_patch_coerces_list_to_mapping(restore_special_tokens_hook):
- seen = {}
-
- def fake(self, special_tokens):
- seen.update(special_tokens)
-
- PreTrainedTokenizerBase._set_model_specific_special_tokens = fake
- assert compat.patch_extra_special_tokens() is True
-
- PreTrainedTokenizerBase._set_model_specific_special_tokens(
- object(), ["<|lang_start|>", "<|text_end|>"]
- )
- assert seen == {
- "lang_start_token": "<|lang_start|>",
- "text_end_token": "<|text_end|>",
- }
-
-
-def test_patch_leaves_mapping_form_alone(restore_special_tokens_hook):
- seen = {}
-
- def fake(self, special_tokens):
- seen.update(special_tokens)
-
- PreTrainedTokenizerBase._set_model_specific_special_tokens = fake
- compat.patch_extra_special_tokens()
-
- PreTrainedTokenizerBase._set_model_specific_special_tokens(
- object(), {"boundary_token": "<|b|>"}
- )
- assert seen == {"boundary_token": "<|b|>"}
-
-
-def test_patch_is_idempotent(restore_special_tokens_hook):
- PreTrainedTokenizerBase._set_model_specific_special_tokens = lambda self, tokens: None
- assert compat.patch_extra_special_tokens() is True
- assert compat.patch_extra_special_tokens() is False
-
-
- "token,expected",
- [
- ("<|denoise|>", "denoise_token"),
- ("<|instruct_start|>", "instruct_start_token"),
- ("[SEP]", "sep_token"),
- ("<mask>", "mask_token"),
- ("<|weird-one|>", "weird_one_token"),
- ],
-)
-def test_attribute_name_derivation(token, expected):
- assert compat._attribute_name(token) == expected
-
-
-# ── engine wiring ─────────────────────────────────────────────────────
-
-
-def test_omnivoice_is_registered():
- from backend.backends import TTS_ENGINES, get_model_config
-
- assert TTS_ENGINES["omnivoice"] == "OmniVoice"
-
- config = get_model_config("omnivoice")
- assert config is not None
- assert config.engine == "omnivoice"
- assert config.hf_repo_id == "k2-fsa/OmniVoice"
- assert config.supports_instruct is True
-
-
-def test_omnivoice_accepts_cloned_profiles():
- """Regression: the service-layer allow-list is separate from the frontend one."""
- from backend.services.profiles import CLONING_ENGINES
-
- assert "omnivoice" in CLONING_ENGINES
-
-
-def test_declared_languages_are_resolvable_by_omnivoice():
- """Every advertised code must map to something OmniVoice actually knows.
-
- OmniVoice speaks ISO 639-3. Most two-letter codes pass straight through,
- but Arabic does not: it enumerates varieties instead of the macrolanguage.
- """
- # omnivoice imports HiggsAudioV2TokenizerModel at module scope, so the
- # shim has to be in place before the import -- which is itself the point.
- compat.install()
- try:
- from omnivoice.utils.lang_map import LANG_NAME_TO_ID
- except ImportError as exc: # pragma: no cover - omnivoice not installed
- pytest.skip(f"omnivoice unavailable: {exc}")
-
- from backend.backends import get_model_config
- from backend.backends.omnivoice_backend import LANGUAGE_CODE_OVERRIDES
-
- known = set(LANG_NAME_TO_ID.values())
- unresolved = [
- code
- for code in get_model_config("omnivoice").languages
- if LANGUAGE_CODE_OVERRIDES.get(code, code) not in known
- ]
- assert unresolved == []
-
-
-def test_clear_codec_layer_cache_is_safe_when_codec_never_imported():
- from backend.backends.omnivoice_backend import _clear_codec_layer_cache
-
- name = "backend.vendor.higgs_audio_v2_tokenizer.modeling_higgs_audio_v2_tokenizer"
- saved = sys.modules.pop(name, None)
- try:
- _clear_codec_layer_cache() # must not raise
- finally:
- if saved is not None:
- sys.modules[name] = saved
+"""
+Unit tests for the OmniVoice engine's transformers 5 -> 4.57.6 compatibility layer.
+
+OmniVoice is built against transformers >= 5.3; Voicebox is pinned to <= 4.57.6
+because several engine workarounds read 4.57.x internals. ``backend/vendor/``
+carries the one class that gap costs us (the Higgs Audio V2 codec) and
+``backend/utils/transformers5_compat.py`` grafts it on.
+
+None of this downloads a model or touches a GPU. The full round trip — load,
+clone, generate, unload — is covered by the manual E2E suite described in
+``backend/tests/E2E_MODEL_TEST_DESIGN.md``.
+
+NOTE: ``patch_extra_special_tokens`` mutates ``transformers.PreTrainedTokenizerBase``
+globally; run serially, not under ``pytest-xdist`` with per-worker isolation.
+"""
+
+import sys
+
+import pytest
+import torch
+from torch import nn
+from transformers.tokenization_utils_base import PreTrainedTokenizerBase
+
+import backend.utils.transformers5_compat as compat
+from backend.vendor import _compat
+
+
+# ── conv1d_output_length ──────────────────────────────────────────────
+# Absent from 4.57.6's audio_utils; the vendored codec computes encoder
+# frame counts with it.
+
+
+ "kwargs,length,expected",
+ [
+ ({"kernel_size": 1, "stride": 1}, 100, 100),
+ ({"kernel_size": 3, "stride": 1, "padding": 1}, 100, 100),
+ ({"kernel_size": 4, "stride": 2, "padding": 1}, 100, 50),
+ ({"kernel_size": 3, "stride": 1, "dilation": 2, "padding": 2}, 100, 100),
+ ],
+)
+def test_conv1d_output_length_matches_torch(kwargs, length, expected):
+ layer = nn.Conv1d(1, 1, **kwargs)
+ assert _compat.conv1d_output_length(layer, length) == expected
+ # Cross-check against what the layer actually produces.
+ actual = layer(torch.zeros(1, 1, length)).shape[-1]
+ assert _compat.conv1d_output_length(layer, length) == actual
+
+
+# ── initialization module ─────────────────────────────────────────────
+# 5.x-only. Only reached from _init_weights, i.e. for weights missing from
+# the checkpoint.
+
+
+def test_initialization_functions_mutate_in_place_and_return_tensor():
+ tensor = torch.empty(4, 4)
+ result = _compat.initialization.zeros_(tensor)
+ assert result is tensor
+ assert torch.all(result == 0)
+
+ assert torch.all(_compat.initialization.ones_(torch.empty(3)) == 1)
+ assert _compat.initialization.normal_(torch.empty(8), mean=0.0, std=0.02) is not None
+
+
+def test_initialization_copy_writes_through():
+ target = torch.zeros(2)
+ source = torch.tensor([1.0, 2.0])
+ assert torch.equal(_compat.initialization.copy_(target, source), source)
+
+
+# ── PreTrainedConfig dataclass adapter ────────────────────────────────
+# 5.x configs are dataclasses whose generated __init__ ends in __post_init__.
+# 4.57.6's PretrainedConfig never calls it, which leaves sub-configs as raw
+# dicts and blows up later in to_diff_dict().
+
+
+class _SampleConfig(_compat.PreTrainedConfig):
+ model_type = "sample_compat_config"
+
+ plain_field: int = 7
+ listy_field: list = None
+ nested: dict = None
+
+ def __post_init__(self, **kwargs):
+ if isinstance(self.nested, dict):
+ self.nested = dict(self.nested, promoted=True)
+ super().__post_init__(**kwargs)
+
+
+def test_annotated_defaults_are_applied():
+ config = _SampleConfig()
+ assert config.plain_field == 7
+ assert config.nested is None
+
+
+def test_kwargs_override_defaults():
+ config = _SampleConfig(plain_field=42)
+ assert config.plain_field == 42
+
+
+def test_post_init_runs():
+ config = _SampleConfig(nested={"a": 1})
+ assert config.nested == {"a": 1, "promoted": True}
+
+
+def test_mutable_defaults_are_not_shared_between_instances():
+ class _ListConfig(_compat.PreTrainedConfig):
+ model_type = "list_compat_config"
+ items: list = []
+
+ first, second = _ListConfig(), _ListConfig()
+ first.items.append("x")
+ assert second.items == []
+
+
+def test_leftover_kwargs_reach_pretrained_config():
+ config = _SampleConfig(plain_field=1, output_hidden_states=True)
+ assert config.output_hidden_states is True
+
+
+# ── auto_docstring / strict no-ops ────────────────────────────────────
+# Both raise on 4.57.6 for the vendored class: auto_docstring rejects names
+# outside its registry, strict requires a dataclass.
+
+
+def test_auto_docstring_returns_target_unchanged():
+ class Target:
+ pass
+
+ assert _compat.auto_docstring(Target) is Target
+ assert _compat.auto_docstring(custom_intro="x")(Target) is Target
+
+
+def test_strict_returns_target_unchanged():
+ class Target:
+ pass
+
+ assert _compat.strict(Target) is Target
+ assert _compat.strict()(Target) is Target
+
+
+# ── extra_special_tokens list form ────────────────────────────────────
+# OmniVoice's tokenizer_config.json writes a list (5.x form); 4.57.6 calls
+# .keys() on it.
+
+
+def restore_special_tokens_hook():
+ saved = PreTrainedTokenizerBase.__dict__.get("_set_model_specific_special_tokens")
+ yield
+ if saved is not None:
+ PreTrainedTokenizerBase._set_model_specific_special_tokens = saved
+
+
+def test_patch_coerces_list_to_mapping(restore_special_tokens_hook):
+ seen = {}
+
+ def fake(self, special_tokens):
+ seen.update(special_tokens)
+
+ PreTrainedTokenizerBase._set_model_specific_special_tokens = fake
+ assert compat.patch_extra_special_tokens() is True
+
+ PreTrainedTokenizerBase._set_model_specific_special_tokens(
+ object(), ["<|lang_start|>", "<|text_end|>"]
+ )
+ assert seen == {
+ "lang_start_token": "<|lang_start|>",
+ "text_end_token": "<|text_end|>",
+ }
+
+
+def test_patch_leaves_mapping_form_alone(restore_special_tokens_hook):
+ seen = {}
+
+ def fake(self, special_tokens):
+ seen.update(special_tokens)
+
+ PreTrainedTokenizerBase._set_model_specific_special_tokens = fake
+ compat.patch_extra_special_tokens()
+
+ PreTrainedTokenizerBase._set_model_specific_special_tokens(
+ object(), {"boundary_token": "<|b|>"}
+ )
+ assert seen == {"boundary_token": "<|b|>"}
+
+
+def test_patch_is_idempotent(restore_special_tokens_hook):
+ PreTrainedTokenizerBase._set_model_specific_special_tokens = lambda self, tokens: None
+ assert compat.patch_extra_special_tokens() is True
+ assert compat.patch_extra_special_tokens() is False
+
+
+ "token,expected",
+ [
+ ("<|denoise|>", "denoise_token"),
+ ("<|instruct_start|>", "instruct_start_token"),
+ ("[SEP]", "sep_token"),
+ ("<mask>", "mask_token"),
+ ("<|weird-one|>", "weird_one_token"),
+ ],
+)
+def test_attribute_name_derivation(token, expected):
+ assert compat._attribute_name(token) == expected
+
+
+# ── engine wiring ─────────────────────────────────────────────────────
+
+
+def test_omnivoice_is_registered():
+ from backend.backends import TTS_ENGINES, get_model_config
+
+ assert TTS_ENGINES["omnivoice"] == "OmniVoice"
+
+ config = get_model_config("omnivoice")
+ assert config is not None
+ assert config.engine == "omnivoice"
+ assert config.hf_repo_id == "k2-fsa/OmniVoice"
+ assert config.supports_instruct is True
+
+
+def test_omnivoice_accepts_cloned_profiles():
+ """Regression: the service-layer allow-list is separate from the frontend one."""
+ from backend.services.profiles import CLONING_ENGINES
+
+ assert "omnivoice" in CLONING_ENGINES
+
+
+def test_declared_languages_are_resolvable_by_omnivoice():
+ """Every advertised code must map to something OmniVoice actually knows.
+
+ OmniVoice speaks ISO 639-3. Most two-letter codes pass straight through,
+ but Arabic does not: it enumerates varieties instead of the macrolanguage.
+ """
+ # omnivoice imports HiggsAudioV2TokenizerModel at module scope, so the
+ # shim has to be in place before the import -- which is itself the point.
+ compat.install()
+ try:
+ from omnivoice.utils.lang_map import LANG_NAME_TO_ID
+ except ImportError as exc: # pragma: no cover - omnivoice not installed
+ pytest.skip(f"omnivoice unavailable: {exc}")
+
+ from backend.backends import get_model_config
+ from backend.backends.omnivoice_backend import LANGUAGE_CODE_OVERRIDES
+
+ known = set(LANG_NAME_TO_ID.values())
+ unresolved = [
+ code
+ for code in get_model_config("omnivoice").languages
+ if LANGUAGE_CODE_OVERRIDES.get(code, code) not in known
+ ]
+ assert unresolved == []
+
+
+def test_clear_codec_layer_cache_is_safe_when_codec_never_imported():
+ from backend.backends.omnivoice_backend import _clear_codec_layer_cache
+
+ name = "backend.vendor.higgs_audio_v2_tokenizer.modeling_higgs_audio_v2_tokenizer"
+ saved = sys.modules.pop(name, None)
+ try:
+ _clear_codec_layer_cache() # must not raise
+ finally:
+ if saved is not None:
+ sys.modules[name] = saved