Skip to content

fix(models): accept a Mamba config that names a setting both ways - #2995

Merged
inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/mamba-config-duplicate-spellings
Nov 14, 2026
Merged

inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/mamba-config-duplicate-spellings

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

A Mamba checkpoint that carries a Hugging Face MambaConfig did not load: duplicate field hidden_size``. That config writes three settings under both of their names, and MambaConfig here was a serde derive with aliases, which refuses a document that has both. The config is now built from the JSON object, taking each setting under the first spelling present, Hugging Face name first.

Related issues

Closes #2994.

Type of change

  • fix — bug fix

Cause

src/models/mamba.rs: #[serde(alias = "d_model")] pub hidden_size, #[serde(alias = "d_inner")] pub intermediate_size, #[serde(alias = "n_layer", alias = "n_layers")] pub num_hidden_layers. A derive with an alias accepts either spelling and rejects a document that names both. state-spaces/mamba-130m-hf and every conversion of it name all three both ways, so the parse failed before a tensor was read. rope_utils.rs describes the same hazard for type / rope_type and reads that block as a map for the same reason.

Change

  • MambaConfig is #[serde(try_from = "serde_json::Value")]. config_field reads a setting under the first spelling present; a null reads as absent. The order is the Hugging Face name, then the original state-spaces one: hidden_size / d_model, intermediate_size / d_inner, state_size / d_state, num_hidden_layers / n_layer / n_layers, conv_kernel / d_conv, use_bias / bias, use_conv_bias / conv_bias.
  • The Hugging Face name wins when the two disagree, as in transformers. state-spaces/mamba-370m-hf carries d_inner: 160 beside intermediate_size: 2048, and its weights are 2048 wide.
  • Defaults, time_step_rank: "auto", and the error for a missing or malformed setting are kept; a missing setting is reported by its Hugging Face name, as before.

Before / after

GB10 (sm_121, CUDA 13.0), greedy, 24 new tokens. main is the release build of ce6855bc8; the fix column is a test-fast build of this branch (src/models/mamba.rs identical to this commit). The third column is main on a copy of the checkpoint whose config.json has the d_model, d_inner and n_layer keys removed; transformers is 4.56.1 on the original state-spaces checkpoint in float32.

Checkpoint main This PR main, duplicate names removed transformers
mlx-community/mamba-130m-hf-f32 @ ef70de24ea66 duplicate field "The capital of France is in the city of Paris. The city of Paris is in the city of Paris. The capital of France is in th…" the same text the same text
mlx-community/mamba-130m-hf-bf16 @ c316871ed060 duplicate field "… is in the city of Paris, and the city of Nice is in the city of Nice. …" the same text (float32 reference above)
mlx-community/mamba-370m-hf-f16 @ c7cf1bc953ad duplicate field "… is the capital of the French Republic. It is located in the south-" the same text "… It is located in the south-west of the country, in the department of the"

A second prompt, Once upon a time, there was a little, agrees the same way. The bf16 and f16 conversions continue differently from the float32 reference after a few tokens, on main with the cleaned config as well; that is not part of this change.

Test plan

Run on this PR's commit (9b1d76dcf, one commit on ce6855bc8) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings (the Linux spelling of the template's line)
  • cargo build --release --features cuda
  • cargo test --profile test-fast --features cuda --lib models::mamba:: -- --test-threads=1: 18 passed, 1 ignored. The four new tests cover a config that names a setting both ways, the two names disagreeing, the original spellings alone, and the errors for a missing setting, an invalid time_step_rank, a wrongly typed value and a non-object config
  • Negative control: with mamba.rs put back as it is on main and the new tests kept, a_config_that_names_a_setting_both_ways_parses fails with duplicate field hidden_size``
  • make verify-test-cuda (cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 186 of 187 test binaries pass (15490 tests passed, 1 failed, 496 ignored). The one failure is native_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a clean main (test(cuda): make verify-test-cuda fails the cache-default test on every CUDA host since #2876 #2987)
  • The self-hosted ci.yml jobs a pull request from a fork does not get, run by hand: clippy (cargo clippy -p mlxcel --lib --tests -- -D warnings, default features) and xla-compile (cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets): all pass
  • make verify-python-tooling (219 tests OK), license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refs
  • Validated with real checkpoints: the table above
  • cargo deny check: not run and not applicable (no manifest, lockfile or deny.toml change)
  • Metal and ROCm: not built or run here. The change is config parsing

Notes for reviewers

  • The table's fix column comes from a test-fast build, not from the release build of the test plan; the two share this commit's mamba.rs. The release build was built and gated, and those three prompts were not repeated on it.
  • Mamba2 has one alias (d_conv); it is not changed here.
  • Implemented and validated with AI assistance (Claude) driving the GB10 host. Every number above comes from the logs of those runs.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: not needed (no option, flag or documented behaviour changes)
  • Updated // Used by: ... comments: the new helpers are private to the config
  • No secrets, credentials, or .env files committed

Jaeyeong-CHOI and others added 2 commits November 12, 2026 03:10
A Hugging Face `MambaConfig` writes three settings under both of their
names: `hidden_size` and `d_model`, `intermediate_size` and `d_inner`,
`num_hidden_layers` and `n_layer`. `MambaConfig` here was a serde derive
with aliases, and serde refuses a document that names an aliased field
twice, so every `*-hf` Mamba checkpoint stopped at

    Error: duplicate field `hidden_size` at line 13 column 17

before a single tensor was read.

The config is now built from the JSON object, taking each setting under
the first spelling present. The Hugging Face name comes first, as in
transformers and mlx-lm: `state-spaces/mamba-370m-hf` carries `d_inner`
160 beside `intermediate_size` 2048, and the weights are 2048 wide. A
config with only the original spellings parses as before, and a missing
setting is still reported by its Hugging Face name.
@inureyes

Copy link
Copy Markdown
Member

Thanks for the careful fix and the before/after table. Review summary before merge:

  • Precedence: the Hugging Face name is read first and the state-spaces name only as a fallback, which matches transformers (it reads only the HF attribute) and mlx-lm (it copies d_model and friends only when the HF name is absent). The d_inner: 160 beside intermediate_size: 2048 case is covered by a test.
  • Other Mamba-family parsers: the only remaining dual-spelling aliases are Mamba2Config.conv_kernel / d_conv and NemotronHConfig.chunk_size / mamba_chunk_size. Left alone on purpose: the published Mamba2 configs (mlx-community/mamba2-130m, AntonV/mamba2-130m-hf) and Nemotron-H configs (Nemotron-H-8B, Nemotron-Nano-9B-v2, Nemotron-3-Nano-30B-A3B and its MLX conversion) carry only one spelling each, so the duplicate-field failure cannot occur there today. Jamba, Falcon-H1, GraniteMoeHybrid and PLaMo-2 have no such aliases.
  • No security or performance concern: parsing is a one-time map lookup per field at load.

Validation on Apple M3 Ultra (Metal), after merging current main into the branch:

  • cargo test --lib models::mamba: 28 passed, 2 ignored. cargo clippy --lib --tests -- -D warnings and cargo fmt --all -- --check pass.
  • mlx-community/mamba-130m-hf-bf16 on main: duplicate field hidden_size, reproduced. With this branch it loads.
  • mlx-community/mamba-130m-hf-f32 with this branch, greedy, 24 tokens: "The capital of France is in the city of Paris. The city of Paris is in the city of Paris. The capital of", matching the transformers reference in the issue.

One unrelated observation for a follow-up: on Metal the bf16 130m conversion generates 0 tokens, both with this branch and with main on a config whose duplicate keys are removed, so it predates this change (the bf16 weights are converted to f16 at load on Apple Silicon).

@inureyes
inureyes merged commit 2dc0dec into lablup:main Nov 14, 2026
31 checks passed
@inureyes inureyes added the status:done Completed label Nov 14, 2026
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata modeltype:text Text-only language model arch:ssm State-space / recurrent model (Mamba, RWKV) labels Nov 30, 2026
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:ssm State-space / recurrent model (Mamba, RWKV) area:models Model architectures, weights, loading, metadata modeltype:text Text-only language model priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(models): accept a Mamba config that names a setting both ways

2 participants