Skip to content

fix(hymba): materialize root-level bare parameters and honour construction dtypes (#4888) - #5021

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4888
Nov 17, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4888

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4888

Hymba could not be quantized in half precision with either offload_to_disk setting, for two independent generic reasons.

1. offload_to_disk=True (default): input capture crashed on model.memory_tokens

HymbaModel owns its 128 meta tokens as a bare nn.Parameter on the decoder root, not as a child module, and prepends them before layer 0. The lazy offload shell only materialized the base modules returned by get_base_modules() plus the modules a definition declares, so a parameter sitting directly on the decoder root stayed on meta and the capture forward raised Tensor on device cpu is not on the expected device meta!.

get_modules_with_direct_meta_tensors now also walks every module-tree prefix path and returns the nodes that own a direct meta tensor, so no definition has to enumerate them. The scan is deduplicated and costs a handful of getattrs per prefix node.

2. offload_to_disk=False: the #4140 dtype guard rejected a valid load

assert_loaded_model_state failed with floating parameters are not all torch.bfloat16: {'torch.float32': 2} — model.layers.{0,1}.mamba.A_log.0. The remote code builds them as nn.Parameter(torch.log(A)) from an explicit fp32 A, and transformers loads the bf16 checkpoint value into that fp32 parameter without changing its dtype (values exact). #4209 only exempts _keep_in_fp32_modules(_strict).

The dtype each parameter had at construction is now recorded in PreTrainedModel.post_init (dtypes only, never tensors). A parameter that is still fp32 and was already fp32 at construction is treated as intentional. Two guards keep a broken cast from being masked:

  • a parameter the load changed away from its construction dtype is still reported;
  • if the record is uninformative (every parameter fp32, i.e. no transformers dtype context was active), it is discarded and the strict check applies. test_dtype_guard_ignores_an_all_fp32_construction_record pins this.

_keep_in_fp32_modules-matched parameters stay excluded from the record, so clearing a declaration still makes them reportable again (#4170).

Verification

New tests/test_hymba_bf16_load_4888.py (6 tests), using small synthetic fixtures because the real Hymba needs remote code with mamba_ssm/causal_1d/flash_attn:

  • test_root_level_bare_parameter_is_detected_as_direct_meta_module — decoder root with a bare meta parameter is returned by HymbaQModel.get_modules_with_direct_meta_tensors.
  • test_stage_inputs_capture_materializes_the_root_bare_parameter — the real StageInputsCapture path materializes it through shell_direct_meta_materialize.
  • test_dtype_guard_accepts_fp32_widened_from_bf16 — mixed construction record, fp32 parameter accepted.
  • test_dtype_guard_still_rejects_corrupted_fp32 — a bf16-at-construction parameter left fp32 is still rejected.
  • test_dtype_guard_ignores_an_all_fp32_construction_record — an all-fp32 record does not exempt anything.
  • test_real_bf16_load_keeps_the_construction_fp32_parameter — a real save_pretrained → from_pretrained(dtype=bf16) round trip keeps A_log fp32 and passes the guard.

Red on parent (git checkout origin/main -- gptqmodel/models/base.py gptqmodel/utils/hf.py, then restored): 4 failed, 1 passed — the detection, capture-materialization, mixed-record and real-load tests; only the "still rejects corrupted fp32" test passes on the parent, as intended.

Green after merging current main: 6 passed. Neighbouring suites — tests/test_load_state_assertions.py tests/test_stage_inputs_capture.py tests/test_ernie4_5_vl_moe_offload_hook.py tests/test_looped_decoder_capture.py tests/test_hrm_text_support.py → 64 passed, 2 skipped; tests/module_tree/test_registered_module_plans.py tests/test_cpu_offload_tied_both_copies_4943.py → 436 passed. ruff check gptqmodel tests clean.

tests/models/test_hymba.py keeps its OFFLOAD_TO_DISK = False workaround removed only if it can run; it needs the real checkpoint + remote code, which is not available on this host, so the workaround line was left untouched and the fix is proven at the unit level instead.

Residual risk

  • The post_init patch is global to PreTrainedModel. It records dtypes only (a dict of name → dtype) and is wrapped in a defensive try, and it skips models that declare no dtype context (see above). Remote-code models that do not call post_init simply record nothing and keep the strict check.
  • The prefix-node scan adds a few attribute reads per prefix path on every get_modules_with_direct_meta_tensors() call; it does not traverse layers.
  • The real nvidia/Hymba-1.5B-Base bf16 quantization run was not performed here (no mamba_ssm/flash_attn, remote code).

…ction dtypes (#4888)

Hymba could not be quantized in half precision, for two independent generic
reasons.

1. `get_modules_with_direct_meta_tensors` only returned the modules a
   definition declares. A decoder root (or any other module-tree prefix node)
   can own a bare `nn.Parameter`/buffer that is not a child module -- Hymba's
   `model.memory_tokens` is one. Nothing in the base-module or layer traversal
   materializes it, so with the default offload shell it stayed on `meta` and
   the input-capture forward raised
   `Tensor on device cpu is not on the expected device meta!`.
   The scan now also walks every module-tree prefix path and returns the nodes
   that own a direct meta tensor, so no definition has to enumerate them.

2. `assert_loaded_model_state` refused a valid bf16 load of Hymba because its
   remote constructor allocates `mamba.A_log` in fp32 from an explicit fp32
   `A`, and transformers loads the bf16 checkpoint value into that fp32
   parameter without changing its dtype (values exact). `#4209` only exempts
   `_keep_in_fp32_modules`.
   The dtype each parameter had when the model was constructed is now recorded
   in `PreTrainedModel.post_init` (dtypes only, never tensors). A parameter
   that is still fp32 *and* was already fp32 at construction is treated as
   intentional; one the load changed away from its construction dtype is still
   reported, so a broken cast is not masked.

Fixes #4888
A record taken without transformers' dtype context (every parameter fp32)
says nothing about which parameters were intentionally fp32, so it must not
exempt anything: keep the strict check in that case. Also move the
keep-in-fp32 matcher inside the defensive try, since it now runs on every
model construction rather than only at the post-load guard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant