Repository navigation
Commit 4f7e5c8
Derive Qwen3-TTS codec conv encoder from config instead of hardcoding widths (#552)
## The bug
`_MimiConvEncoder` in `src/mobius/models/qwen3_tts_tokenizer.py` took
**no config at all** and hardcoded its channel widths and kernels
(`1→64→128→256→512→1024→512`). Its output fed
`CodecEncoderTransformerModel(hidden_size=config.codec_encoder.hidden_size)`,
which has no input projection — so the conv output width and
`hidden_size` had to agree, and nothing checked that they did.
When they disagreed, the builder happily emitted a malformed graph:
```
Conv encoder/encoder/layers/14/conv weight [512,1024,3] -> [batch, T, 512]
LayerNormalization weight [32] bias [32] over a 512-wide tensor <-- malformed
MatMul q_proj [32,32] vs 512 -> "Incompatible MatMul contraction dimensions: 512 vs 32"
```
## Why it was invisible
`SymbolicShapeInferencePass` catches inference errors and logs
`"Symbolic shape inference failed (upstream bug); skipping"` — a
deliberate generic guard. The tiny config in
`TestBuildCodecGraph._codec_config()` set
`codec_encoder.hidden_size=32`, so **the codec tests were building a
broken graph and passing**.
## The fix
The hardcoded widths were not dictated by the checkpoint — HF's
`MimiEncoder.__init__` derives the entire stack from config, and
mobius's literals were just that derivation inlined with
`num_filters=64`, `upsampling_ratios=[8,6,5,4]`,
`num_residual_layers=1`, `kernel_size=7`, `last_kernel_size=3`. So
rather than adding a validation guard, this parameterizes the stack and
eliminates the mismatch class:
- **`CodecEncoderConfig`**: added the missing HF fields
(`audio_channels`, `num_filters`, `num_residual_layers`, `kernel_size`,
`last_kernel_size`, `residual_kernel_size`, `compress`,
`upsampling_ratios`) with HF's own defaults, extracted in
`ArchitectureConfig.from_transformers`.
- **`_MimiConvEncoder`**: builds by HF's derivation. The parameterless
ELU slots are preserved so `encoder.encoder.layers.*` numbering still
matches the checkpoint exactly. `kernel = ratio * 2` and the trailing
conv's out-channels is `hidden_size`, both derived.
- **`_EncoderResBlock`**: inner width is now `dim // compress` with a
configurable kernel.
## Also fixed: the encoder RVQ could not load the real checkpoint
This one is a **weight-loading correctness fix, not just a
shape-consistency fix**, which is why it belongs here despite being
adjacent to the original scope.
`_EncoderSplitRVQ` used `input_dim=codebook_dim` and `dim=codebook_dim
// 2`. The real checkpoint has `encoder.quantizer.*.input_proj.weight
[256, 512, 1]` and `codebook.embed_sum [2048, 256]`, with
`encoder_config.codebook_dim=256`. So the old code built
`Conv1d(256→128)` projections and 128-wide codebooks: **the encoder
could never have loaded the real checkpoint's weights**, independently
of the malformed-graph issue. It only happened to be self-consistent on
the `enc is None` fallback path, where `codebook_dim` defaulted to 512.
Now it projects `hidden_size → codebook_dim`, matching HF
`MimiResidualVectorQuantizer`.
### The encoder/decoder asymmetry is intentional — please don't unify
these paths
The two sides interpret `codebook_dim` differently, and both are
correct:
| | config value | RVQ input | codebook dim | checkpoint `input_proj` |
|---|---|---|---|---|
| encoder | `codebook_dim=256` (Mimi semantics: the codebook dim itself)
| `hidden_size=512` | 256 | `[256, 512, 1]` |
| decoder | `codebook_dim=512` (the RVQ *input* dim) | 512 | `512 // 2 =
256` | `[256, 512, 1]` |
So the decoder's `// 2` convention in `SplitResidualVectorQuantizer` is
right and is left untouched; only the encoder path was misreading its
field. A future "cleanup" that unifies the two would reintroduce this
bug.
## Verification
**Symbolic-shape-inference failures across `TestBuildCodecGraph`**
(measured by temporarily instrumenting `infer_symbolic_shapes`, since
the pass swallows them — instrumentation not committed):
| | failures |
|---|---|
| before | **6** |
| after | **0** |
**Default config reproduces the real checkpoint exactly** — verified
against the safetensors header of `Qwen/Qwen3-TTS-Tokenizer-12Hz`: all
14 conv-stack weights match by name and shape, including the ResBlock
`layers.N.block.1/.3` names and the `0,1,3,4,6,7,9,10,12,14` numbering,
as do both RVQ `input_proj`/`output_proj` pairs and the codebooks. This
is asserted by tests, so indexing or width drift (which would otherwise
surface far away at weight-load time) fails loudly.
**Tiny test config** keeps the real depth and only shrinks widths
(`num_filters=4`, `hidden_size=32` → `1→4→8→16→32→64→32`), so it
exercises the same weight-name structure the real model depends on.
Codec test runtime: 2.05s (4 tests) → ~2.1s (9 tests).
**Suite**: `pytest tests/build_graph_test.py tests/cli_test.py src/ -k
"not phi4mm and not apply_weights_unknown" -n auto` → 8 failed, 4613
passed — the same 8 pre-existing failures on `main` (onnx_genai
jsonschema drift + one qwen_image golden). `lintrunner` clean.
## New tests
- `test_default_config_conv_stack_matches_checkpoint` — asserts derived
parameter **names** and shapes against the real checkpoint.
- `test_tiny_config_keeps_checkpoint_layer_names` — tiny config has
identical naming, narrower tensors.
- `test_conv_stack_indices_follow_config` — 2 ratios → final conv at
`layers.8`; 4 ratios × 2 residual layers → `layers.18`.
- `test_hidden_size_drives_conv_output_width` — direct regression guard
for the original bug.
- `test_default_config_rvq_projections_match_checkpoint` — asserts
`input_proj (256, 512, 1)`, `output_proj (512, 256, 1)` and `(2048,
256)` codebooks, guarding the weight-loading fix.
## Decoder conv stack
Checked: the codec **decoder** does **not** have the mirrored conv
problem. `Qwen3TTSCodecDecoderModel` already derives everything from
config (`latent_dim`, `decoder_dim`, `upsample_rates`,
`upsampling_ratios`), and `CodecDecoderTransformerModel` has an
`input_proj`/`output_proj` pair that adapts `latent_dim ↔ hidden_size`,
so no width can be stranded. No change needed there.
## Scope
Limited to the codec tokenizer model, its config, and its tests. Does
not touch `reuse.py`, `_ssm.py`, `_mamba_block.py`, the onnx_genai
integration (covered by #551), or `SymbolicShapeInferencePass`.
---------
Signed-off-by: Justin Chu <[email protected]>
Co-authored-by: Copilot App <[email protected]>1 parent e5d337b commit 4f7e5c8
6 files changed
Lines changed: 401 additions & 56 deletions
File tree
- src/mobius
- _configs
- models
- tasks
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1125 | 1125 | | |
1126 | 1126 | | |
1127 | 1127 | | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
1128 | 1136 | | |
1129 | 1137 | | |
1130 | 1138 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
151 | 163 | | |
152 | 164 | | |
153 | 165 | | |
| |||
0 commit comments