Repository navigation
fix(tenstorrent): preserve checkpoint architecture and bias precision - #3843
Merged
Merged
Conversation
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.
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.
Summary
Fixes #3821.
Two block-float checkpoint fidelity defects:
modeling_tt_blockfloat.pyalways subclassedLlamaForCausalLM, so an exported OPT checkpoint failedAutoModelForCausalLM.from_pretrained(..., trust_remote_code=True)withAttributeError: '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.What Changed
_modeling_base()derives the modeling-file base class from the exported model, falling back toMODEL_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_SOURCEis rendered per checkpoint with that import/class pair.TTBlockfloatLinearregisters its bias at the declared dtype instead of FP16,pack()preserves the source bias dtype, andswap_blockfloat_modules()passesmodule.weight.dtypeinto each replacement.docs/tenstorrent.mdwith 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
Model Support / Module Plan
Not applicable: no
MODEL_MAPentry, model definition, alias, or module-plan consumer changed.Logic Flow Documentation
file:lineevidence below.docs/tenstorrent.mdrepeats the diagrams and is linked fromREADME.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;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"]_MODELING_SOURCEimportedLlamaForCausalLMunconditionally_modeling_base()derives the import from the model ormodel_typegptqmodel/utils/tt_blockfloat_quant.py:1365TTBlockfloatLinear.__init__registered an FP16 biasgptqmodel/nn_modules/qlinear/tt_blockfloat.py:295pack()cast the bias to FP16pack()preserves the source dtypegptqmodel/nn_modules/qlinear/tt_blockfloat.py:316swap_blockfloat_modules()created linears without a dtypemodule.weight.dtypegptqmodel/nn_modules/qlinear/tt_blockfloat.py:553Review Requirements
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.