Skip to content

configs refactor 1/3: split _configs.py into a package - #309

Merged
justinchuby merged 1 commit into
mainfrom
refactor-configs-1-package-skeleton
May 20, 2026
Merged

configs refactor 1/3: split _configs.py into a package#309
justinchuby merged 1 commit into
mainfrom
refactor-configs-1-package-skeleton

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Part 1 of 3

src/mobius/_configs.py had grown to 2591 lines (9 sub-configs + 19 per-model configs + 3 mega-switch extractors + a 530-line ArchitectureConfig.from_transformers). This PR is the first of three mechanical refactors that carve it into a scalable package layout. No behavior change — every public name is still importable from mobius._configs.

src/mobius/_configs/
├── __init__.py        # re-exports everything that was in _configs.py
├── _sub_configs.py    # pure-data dataclasses (RoPE/Vision/Audio/Codec/TTS)
├── _quantization.py   # QuantizationConfig + from_transformers
└── _base.py           # BaseModelConfig, ArchitectureConfig, per-model
                       # subclasses, and the _extract_* helpers

Follow-ups in this series

  • Part 2/3 — convert the _extract_audio_config / _extract_vision_config model_type switches into a decorator-registered dispatch so new models add a file under per_model/ instead of a branch in the central function.
  • Part 3/3 — move per-model config subclasses (Gemma2Config, MllamaConfig, NemotronHConfig, …) into per_model/ and carve up ArchitectureConfig.from_transformers.

Tests

  • 2769 passed (full src/ + tests/build_graph_test.py + tests/cli_test.py)
  • Ruff clean

_configs.py grew to 2591 lines (9 sub-configs, 19 per-model configs,
3 mega-extractor switch functions, a 530-line ArchitectureConfig.
from_transformers). This is the first of three mechanical refactors
that carve it into a scalable package layout. No behavior change in
this PR — every public name is still importable from mobius._configs.

src/mobius/_configs/
├── __init__.py        # re-exports everything that was in _configs.py
├── _sub_configs.py    # pure-data dataclasses (RoPE/Vision/Audio/Codec/TTS)
├── _quantization.py   # QuantizationConfig + from_transformers
└── _base.py           # BaseModelConfig, ArchitectureConfig, per-model
                      # subclasses, and the _extract_* helpers

Follow-up PRs in this series:
  Part 2/3 — convert the _extract_audio_config / _extract_vision_config
             model_type switches into a decorator-registered dispatch
             so new models add a file under per_model/ instead of a
             branch in the central function.
  Part 3/3 — move per-model config subclasses (Gemma2Config,
             MllamaConfig, NemotronHConfig, ...) into per_model/ and
             carve up ArchitectureConfig.from_transformers.

Tests: 2769 passed (full src/ + tests/build_graph_test.py + cli_test.py).
Ruff: clean.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
@github-actions

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 2a17a92076756e

Model Sub-model Changes Status

No architecture changes detected.


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

@github-actions

Copy link
Copy Markdown

Performance Comparison

Comparing 2a17a92076756e

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0%
bert (feature-extraction) num_nodes 60 60 +0.0%
falcon model_size_bytes 364 KB 364 KB +0.0%
falcon num_nodes 66 66 +0.0%
gemma2 model_size_bytes 428 KB 428 KB +0.0%
gemma2 num_nodes 107 107 +0.0%
gpt2 model_size_bytes 388 KB 388 KB +0.0%
gpt2 num_nodes 53 53 +0.0%
llama model_size_bytes 425 KB 425 KB +0.0%
llama num_nodes 61 61 +0.0%
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0%
llama (static-cache) num_nodes 58 58 +0.0%
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0%
mamba (ssm-text-generation) num_nodes 98 98 +0.0%
phi3 model_size_bytes 421 KB 421 KB +0.0%
phi3 num_nodes 59 59 +0.0%
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0%
phi3 (static-cache) num_nodes 56 56 +0.0%
qwen2 model_size_bytes 425 KB 425 KB +0.0%
qwen2 num_nodes 61 61 +0.0%
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0%
qwen2 (static-cache) num_nodes 58 58 +0.0%
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0%
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0%
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0%
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0%
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0%
qwen3_5_vl (hybrid-qwen-vl) num_nodes 413 413 +0.0%
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0%
t5 (seq2seq) num_nodes 166 166 +0.0%
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0%
whisper (speech-to-text) num_nodes 128 128 +0.0%

No performance regressions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the previously monolithic mobius._configs module into a package (src/mobius/_configs/) while preserving the existing public import surface (from mobius._configs import ...). The goal is to make the configuration system easier to scale and maintain as more model families/config variants are added.

Changes:

  • Introduces a new mobius._configs package with focused modules for sub-config dataclasses and quantization config parsing.
  • Moves pure-data sub-config dataclasses (RoPE/Vision/Audio/Codec/TTS) into _sub_configs.py.
  • Moves QuantizationConfig into _quantization.py and updates _base.py to import these types instead of defining them inline.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/mobius/_configs/_sub_configs.py New module containing pure-data dataclasses for sub-configs (vision/audio/codec/TTS/RoPE).
src/mobius/_configs/_quantization.py New module for QuantizationConfig and HF parsing helper.
src/mobius/_configs/_base.py Updated to import the moved dataclasses/configs and remove their inline definitions.
src/mobius/_configs/__init__.py New package initializer that re-exports the legacy mobius._configs public API.

Comment thread src/mobius/_configs/_quantization.py
@codecov

codecov Bot commented May 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.88268% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/_configs/_quantization.py 91.66% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@justinchuby
justinchuby merged commit 5d592c6 into main May 20, 2026
27 checks passed
@justinchuby
justinchuby deleted the refactor-configs-1-package-skeleton branch May 20, 2026 15:03
justinchuby added a commit that referenced this pull request May 21, 2026
## Part 2 of 3 — stacked on #309

Replaces the 135-line `model_type` switch in `_extract_audio_config`
with a tiny plugin registry. Each model now contributes a hook in its
own file under `src/mobius/_configs/per_model/`, mutating an
`audio_fields` dict or short-circuiting with a fully-formed sub-config.

**Adding a new audio-capable architecture no longer requires editing
`_base.py`.**

## Layout

```
src/mobius/_configs/
├── _extractors.py                 # @register_audio_hook + dispatch
└── per_model/
    ├── _audio_default.py          # audio_processor / embd_layer / speech_lora
    ├── _phi4mm_audio.py           # phi4mm audio_token_id
    ├── _qwen3_asr_audio.py        # thinker_config.audio_config + token ids
    ├── _gemma4_audio.py           # short-circuit to Gemma4AudioConfig
    └── _sensevoice_audio.py       # encoder_conf + frontend_conf mapping
```

`_extract_audio_config` in `_base.py` shrinks to a 5-line shim that
triggers the `per_model` side-effect import and calls the dispatcher.

**No behavior change**: every existing audio-capable model still
produces the same `AudioConfig` from the same HF config.

## Hook protocol

```python
@register_audio_hook
def _my_model(config, parent_config, model_type: str, fields: dict) -> dict | None:
    if model_type != "my_model":
        return None
    fields.update(attention_dim=..., ...)
    return None  # contribute fields, defer to default AudioConfig(**fields)
    # OR
    return {"audio": MySubclassAudioConfig(...)}  # short-circuit
```

## Deferred

Vision-side conversion (also a mega-switch) is intentionally deferred to
a follow-up so reviewers can verify the registry pattern on the smaller
surface first.

## Tests

| Suite | Result |
|---|---|
| `pytest src/ tests/build_graph_test.py tests/cli_test.py -n auto` |
2769 passed, 41 skipped |
| `pytest tests/arch_validation_test.py -k 'sensevoice_small or phi4mm
or qwen3_asr or gemma4'` | 15 passed |
| Ruff | clean |

---------

Signed-off-by: Justin Chu <justinchu@microsoft.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@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.

2 participants