Skip to content

fix(tenstorrent): preserve checkpoint architecture and bias precision - #3843

Merged
Qubitium merged 1 commit into
mainfrom
fix/3821-blockfloat-fidelity
Nov 13, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/3821-blockfloat-fidelity

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #3821.

Two block-float checkpoint fidelity defects:

  1. The shipped modeling_tt_blockfloat.py always subclassed LlamaForCausalLM, so an exported OPT checkpoint failed AutoModelForCausalLM.from_pretrained(..., trust_remote_code=True) with AttributeError: 'OPTConfig' object has no attribute 'num_key_value_heads' before loading any packed weight. Native loading worked, so the advertised self-contained Transformers path did not.
  2. Block-float linears registered and packed dense biases as FP16, so FP32/BF16 biases lost precision on both direct packing and checkpoint reload even when FP32 was requested. Finite values such as 65536 became infinity.

What Changed

  • _modeling_base() derives the modeling-file base class from the exported model, falling back to MODEL_FOR_CAUSAL_LM_MAPPING_NAMES[config.model_type] for wrappers and subclasses. An unresolvable architecture raises at save time instead of writing a checkpoint that only loads Llama.
  • _MODELING_SOURCE is rendered per checkpoint with that import/class pair.
  • TTBlockfloatLinear registers its bias at the declared dtype instead of FP16, pack() preserves the source bias dtype, and swap_blockfloat_modules() passes module.weight.dtype into each replacement.
  • Added regression tests for OPT auto-load/native logit parity, direct FP32/BF16 packing, checkpoint bias round trip, and the fail-closed architecture path.
  • Added a reference section to docs/tenstorrent.md with the new flow and the replaced mechanisms.

No API or storage-format change; checkpoints keep the same safetensors layout and tensor names. Out of scope: other non-Llama architectures are not executed beyond the OPT case.

Tests

  • Unit tests have been executed and passed.
  • Added simple/fast targeted unit tests for this change.
python -m pytest -q tests/test_tenstorrent_support.py -k "modeling_file or modeling_base or bias_keeps_source"
# 4 passed, 124 deselected

python -m pytest -q tests/test_tenstorrent_support.py
# 112 passed, 15 skipped

python -m pytest -q tests/test_tt_blockfloat_cuda.py tests/test_tt_blockfloat_cuda_dispatch.py
# 86 passed

python -m pytest -q tests/test_tt_blockfloat_hip.py
# 64 passed

python -m ruff check gptqmodel/nn_modules/qlinear/tt_blockfloat.py gptqmodel/utils/tt_blockfloat_quant.py tests/test_tenstorrent_support.py
# All checks passed
git diff --check
# clean

Model Support / Module Plan

Not applicable: no MODEL_MAP entry, model definition, alias, or module-plan consumer changed.

Logic Flow Documentation

  • The change replaces a decision mechanism, so diagrams and a docs page are included.
  • New-flow Mermaid diagram uses shipped symbol names.
  • The removed mechanism is tabulated with file:line evidence below.
  • Failure paths are marked and visually distinct.
  • docs/tenstorrent.md repeats the diagrams and is linked from README.md.

New flow:

flowchart TD
    A["save_blockfloat_model(model)"] --> B{"type(model).__module__<br/>starts with transformers.?"}
    B -- yes --> C["base = type(model).__module__, type(model).__name__"]
    B -- no --> D["MODEL_FOR_CAUSAL_LM_MAPPING_NAMES[model.config.model_type]"]
    D -- missing --> E["ValueError: no known causal-LM class"]:::fail
    C --> F["render modeling_tt_blockfloat.py"]
    D --> F
    F --> G["config.json auto_map -><br/>TTBlockfloatForCausalLM"]
    G --> H["AutoModelForCausalLM.from_pretrained"]
    H --> I["TTBlockfloatForCausalLM(config)"]
    I --> J["swap_blockfloat_modules()"]
    J --> K["load packed state"]

    classDef fail fill:#fee2e2,stroke:#b91c1c,color:#7f1d1d;
Loading

Bias flow:

flowchart LR
    A["dense model cast to requested dtype"] --> B["swap_blockfloat_modules"]
    B --> C["TTBlockfloatLinear(dtype=module.weight.dtype)"]
    C --> D["pack(): bias = source bias dtype"]
    D --> E["load_state_dict: bias copied exactly"]
    E --> F["forward: bias.to(x.dtype)"]
    F --> G["kernel or F.linear"]
Loading
Old mechanism New mechanism Evidence
_MODELING_SOURCE imported LlamaForCausalLM unconditionally _modeling_base() derives the import from the model or model_type gptqmodel/utils/tt_blockfloat_quant.py:1365
TTBlockfloatLinear.__init__ registered an FP16 bias the buffer uses the declared dtype gptqmodel/nn_modules/qlinear/tt_blockfloat.py:295
pack() cast the bias to FP16 pack() preserves the source dtype gptqmodel/nn_modules/qlinear/tt_blockfloat.py:316
swap_blockfloat_modules() created linears without a dtype passes module.weight.dtype gptqmodel/nn_modules/qlinear/tt_blockfloat.py:553

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

Not applicable: no quantization math, weight packer, or inference kernel changed. Bias is dense state copied verbatim; the regression test asserts exact equality after packing and checkpoint reload.

The shipped modeling file always subclassed LlamaForCausalLM, so an exported OPT checkpoint failed AutoModelForCausalLM.from_pretrained with "'OPTConfig' object has no attribute 'num_key_value_heads'". Derive the base class from the exported model, fall back to the declared model_type, and fail at save time when neither resolves.

Block-float linears registered and packed biases as float16, so an fp32 or bf16 checkpoint lost bias precision (65536 became inf) even when fp32 was requested. Keep the source dtype through construction, packing and the module swap.

Fixes #3821.
@Qubitium
Qubitium merged commit 36ebdd0 into main Nov 13, 2026
4 of 6 checks passed
@Qubitium
Qubitium deleted the fix/3821-blockfloat-fidelity branch November 13, 2026 09:04
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.

[BUG] Block-float checkpoint loading loses source architecture and bias precision

2 participants