Repository navigation
fix(hymba): materialize root-level bare parameters and honour construction dtypes (#4888) - #5021
Merged
Merged
Conversation
…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.
This was referenced Nov 17, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4888
Hymba could not be quantized in half precision with either
offload_to_disksetting, for two independent generic reasons.1.
offload_to_disk=True(default): input capture crashed onmodel.memory_tokensHymbaModelowns its 128 meta tokens as a barenn.Parameteron the decoder root, not as a child module, and prepends them before layer 0. The lazy offload shell only materialized the base modules returned byget_base_modules()plus the modules a definition declares, so a parameter sitting directly on the decoder root stayed onmetaand the capture forward raisedTensor on device cpu is not on the expected device meta!.get_modules_with_direct_meta_tensorsnow 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 ofgetattrs per prefix node.2.
offload_to_disk=False: the #4140 dtype guard rejected a valid loadassert_loaded_model_statefailed withfloating parameters are not all torch.bfloat16: {'torch.float32': 2}—model.layers.{0,1}.mamba.A_log.0. The remote code builds them asnn.Parameter(torch.log(A))from an explicit fp32A, 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:test_dtype_guard_ignores_an_all_fp32_construction_recordpins 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 withmamba_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 byHymbaQModel.get_modules_with_direct_meta_tensors.test_stage_inputs_capture_materializes_the_root_bare_parameter— the realStageInputsCapturepath materializes it throughshell_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 realsave_pretrained→from_pretrained(dtype=bf16)round trip keepsA_logfp32 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 testsclean.tests/models/test_hymba.pykeeps itsOFFLOAD_TO_DISK = Falseworkaround 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
post_initpatch is global toPreTrainedModel. It records dtypes only (a dict of name → dtype) and is wrapped in a defensivetry, and it skips models that declare no dtype context (see above). Remote-code models that do not callpost_initsimply record nothing and keep the strict check.get_modules_with_direct_meta_tensors()call; it does not traverse layers.nvidia/Hymba-1.5B-Basebf16 quantization run was not performed here (nomamba_ssm/flash_attn, remote code).