Skip to content

lazy load voice info config#107

Merged
JarbasAl merged 1 commit into
devfrom
fixes
Feb 15, 2026
Merged

lazy load voice info config#107
JarbasAl merged 1 commit into
devfrom
fixes

Conversation

@JarbasAl
Copy link
Copy Markdown
Contributor

@JarbasAl JarbasAl commented Feb 15, 2026

Summary by CodeRabbit

  • New Features

    • Voice config loader now accepts an explicit engine override when constructing voice configs.
  • Refactor

    • Configuration is now loaded lazily for improved initialization performance and memory usage; public behavior remains consistent.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 15, 2026

📝 Walkthrough

Walkthrough

TTSModelInfo now replaces its public config dataclass field with a lazy-loaded config property backed by a private _config. VoiceConfig is constructed on first access from stored URLs/asset fields; enum/string normalization occurs in initialization and on first config resolution. VoiceConfig.from_dict accepts an optional engine override.

Changes

Cohort / File(s) Summary
Model manager (lazy config)
phoonnx/model_manager.py
Removed public config: Optional[VoiceConfig] attribute; added private _config and a config property that lazily loads/constructs a VoiceConfig from config_url, vocab/vocab_url, tokenizer_config_url, tokens_url, and local overrides. __post_init__ now normalizes enum/string fields (engine, alphabet, phoneme_type) and prepares internal state. Model-loading helpers preserved but use _config.
Config factory (engine override)
phoonnx/config.py
Extended VoiceConfig.from_dict(...) signature to accept optional engine: Optional[Union[str, Engine]]. Engine selection in detection branches now uses the provided engine when present, otherwise falls back to previous branch-specific defaults; string engines are normalized to Engine values before return.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

feature

Poem

🐇 I nibble bytes beneath the log,
Quietly shift the eager to fog—
Configs now sleep till first they're sought,
A lazy hop saves loading's trot. ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'lazy load voice info config' accurately describes the main change: converting eager config loading to lazy loading in the TTSModelInfo class through a property-based approach.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into dev

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fixes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 15, 2026

Note

Docstrings generation - SUCCESS
Generated docstrings for this pull request at #109

coderabbitai Bot added a commit that referenced this pull request Feb 15, 2026
Docstrings generation was requested by @JarbasAl.

* #107 (comment)

The following files were modified:

* `phoonnx/model_manager.py`
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
phoonnx/model_manager.py (1)

29-78: ⚠️ Potential issue | 🟡 Minor

The config property is no longer part of the dataclass __repr__.

Since config changed from a dataclass field to a property, it will no longer appear in repr(TTSModelInfo(...)). This is likely the desired behavior (avoids triggering lazy-load on repr), but the example output in the comments at the bottom of the file (lines ~396-405) is now stale and includes config=VoiceConfig(...).

🤖 Fix all issues with AI agents
In `@phoonnx/model_manager.py`:
- Around line 42-58: The conditional for handling tokens_url currently uses a
separate if that can overwrite a previously built self._config from the
vocab_override or vocab_url branches; change that if to an elif so
download_tokens_txt() and the tokens-based VoiceConfig.from_dict(...) only run
when earlier branches did not set self._config, and ensure phoneme_type is
applied to the final config object (self._config) after construction; locate the
logic around self.vocab_override, self.vocab_url, download_vocab(),
download_tokenizer_config(), self.tokens_url, download_tokens_txt(), and
VoiceConfig.from_dict to implement this change.
🧹 Nitpick comments (2)
phoonnx/model_manager.py (2)

80-87: Lazy-load init looks good; os.makedirs still runs eagerly for every voice.

The _config backing field initialization is correct. Note that os.makedirs (line 87) still runs eagerly during construction — so instantiating all ~1400+ TTSModelInfo entries (e.g., via merge_default_voices) creates that many cache directories even if config is never accessed. This pre-dates this PR, but if the goal is to defer I/O, you might consider making this lazy too.


29-32: Prefer if self._config is None: over if not self._config:.

Using truthiness to guard lazy initialization is fragile — if VoiceConfig ever defines __bool__ or __len__, this could cause the property to re-compute on every access. The idiomatic pattern for lazy init is an identity check against None.

♻️ Proposed fix
     `@property`
     def config(self) -> VoiceConfig:
         # lazy loaded
-        if not self._config:
+        if self._config is None:

Comment thread phoonnx/model_manager.py Outdated
JarbasAl pushed a commit that referenced this pull request Feb 15, 2026
Docstrings generation was requested by @JarbasAl.

* #107 (comment)

The following files were modified:

* `phoonnx/model_manager.py`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
delay network requests until needed

📝 Add docstrings to `fixes` (#108)

Docstrings generation was requested by @JarbasAl.

* #107 (comment)

The following files were modified:

* `phoonnx/model_manager.py`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
coderabbitai Bot added a commit that referenced this pull request Feb 15, 2026
Docstrings generation was requested by @JarbasAl.

* #107 (comment)

The following files were modified:

* `phoonnx/config.py`
* `phoonnx/model_manager.py`
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
phoonnx/model_manager.py (1)

99-114: Eager makedirs for every voice contradicts the lazy-loading intent.

os.makedirs(self.voice_path, exist_ok=True) on line 106 runs during __post_init__ for every TTSModelInfo instance. With 1400+ registered voices (per merge_default_voices), this issues 1400+ makedirs syscalls at startup, even for voices that are never loaded.

Move directory creation into the config property (or into each download_* method) so it only runs when a voice is actually accessed.

♻️ Proposed fix — defer makedirs to first use
     def __post_init__(self):
         self._config: Optional[VoiceConfig] = None
-        os.makedirs(self.voice_path, exist_ok=True)
 
         # cast strings to enum for consistency
         if not isinstance(self.engine, Engine) and isinstance(self.engine, str):

Then, at the start of the config property (or in a shared helper called by each download method):

     `@property`
     def config(self) -> VoiceConfig:
         if not self._config:
+            os.makedirs(self.voice_path, exist_ok=True)
             if self.config_url:

@JarbasAl JarbasAl merged commit ce66044 into dev Feb 15, 2026
4 checks passed
JarbasAl added a commit that referenced this pull request Feb 16, 2026
* refactor!: tokenizer class + deprecate phoneme_ids.py (#70)

* fix: coqui compatibility

refactor!: tokenizer class + deprecate phoneme_ids.py

fix: missing cotovia data files

feat: add new galician models from proxecto nós

* log

* fix

* fix

* Merge pull request #71 from TigreGotico/coderabbitai/docstrings/cb634ab

📝 Add docstrings to `tokenizer`

* adjust

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Increment Version to 1.0.0a1

* Update Changelog

* feat: community piper voices + pygoruut support (#73)

* feat: community piper voices + pygoruut support

update model manager voice index

Total voices: 284
Total langs: 67

* fix neurlang voice-id

* reorder funcs for readability

* 📝 Add docstrings to `models_galore` (#74)

Docstrings generation was requested by @JarbasAl.

* #73 (comment)

The following files were modified:

* `phoonnx/model_manager.py`
* `phoonnx/util.py`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Increment Version to 1.1.0a1

* Update Changelog

* feat: more piper english community voices (#76)

Total voices: 314

* Increment Version to 1.2.0a1

* Update Changelog

* feat: transformers support (#78)

feat: MMS voices

refactor: move index to static .json files

* Increment Version to 1.3.0a1

* Update Changelog

* documentation: supported voices and languages (#80)

* documentation: supported voices and languages

* documentation: supported voices and languages

* documentation: supported voices and languages

* Increment Version to 1.3.0a2

* Update Changelog

* documentation: supported voices and languages (#82)

* Increment Version to 1.3.0a3

* Update Changelog

* fix: failing MMS models indexing (#84)

* Increment Version to 1.3.0a4

* Update Changelog

* fix: improve lang code standardization (#86)

* fix: improve lang code standardization

* siimplify error handling

* Increment Version to 1.3.1a1

* Update Changelog

* Add renovate.json (#89)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* Increment Version to 1.3.1a2

* Update Changelog

* fix: mantoq2ipa + improve lang code normalization (#90)

* fix: improve lang code standardization

* siimplify error handling

* fix: better arabic ipa g2p

* fix tests

* rrm unused arg

* Increment Version to 1.3.2a1

* chore(deps): update actions/checkout action to v6 (#92)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* Increment Version to 1.3.2a2

* chore(deps): update actions/setup-python action to v6 (#96)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* Increment Version to 1.3.2a3

* Update Changelog

* add more voices (#99)

* Increment Version to 1.3.2a4

* Update Changelog

* 📝 Add docstrings to `patch-2` (#102)

Docstrings generation was requested by @JarbasAl.

* #101 (comment)

The following files were modified:

* `phoonnx/model_manager.py`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Increment Version to 1.3.2a5

* Update Changelog

* Add files via upload

* fix: dont chunk on commas, update voice index (#104)

* fix: dont chunk on commas, update voice index

* fix: dont chunk on commas, update voice index

* 📝 Add docstrings to `fixes` (#105)

Docstrings generation was requested by @JarbasAl.

* #104 (comment)

The following files were modified:

* `phoonnx/model_manager.py`
* `phoonnx/opm.py`
* `phoonnx/phonemizers/base.py`
* `phoonnx_train/vits/dataset.py`
* `phoonnx_train/vits/lightning.py`
* `scripts/index_voices.py`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix: dont chunk on commas, update voice index

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Increment Version to 1.3.3a1

* Update Changelog

* fix: lazy load VoiceConfig (#107)

delay network requests until needed

📝 Add docstrings to `fixes` (#108)

Docstrings generation was requested by @JarbasAl.

* #107 (comment)

The following files were modified:

* `phoonnx/model_manager.py`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Increment Version to 1.3.3a2

* Update Changelog

---------

Co-authored-by: JarbasAI <33701864+JarbasAl@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: JarbasAl <JarbasAl@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant