transformers - 💡(How to fix) Fix from_config(dtype=...) builds weights in the requested dtype but leaves model.config.dtype stale [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Fixed

Code Example

import torch
from transformers import AutoModelForCausalLM, LlamaConfig

cfg = LlamaConfig(vocab_size=32, hidden_size=8, intermediate_size=16,
                  num_hidden_layers=1, num_attention_heads=2)
model = AutoModelForCausalLM.from_config(cfg, dtype=torch.float16)
print(model.dtype)         # torch.float16  <- weights honored
print(model.config.dtype)  # None           <- stale, expected torch.float16
RAW_BUFFERClick to expand / collapse

System Info

  • transformers version: 5.10.0.dev0 (also reproduces on current main)
  • Platform: macOS-26.1-arm64-arm-64bit
  • Python version: 3.11.15
  • Huggingface_hub version: 1.14.0
  • Safetensors version: 0.7.0
  • Accelerate version: 1.13.0
  • PyTorch version (accelerator?): 2.11.0 (NA)
  • Using distributed or parallel set-up in script?: No

This is a pure model-loading logic bug, not hardware-dependent.

Who can help?

@Cyrilvallez @zucchini-nlp

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

from_config(dtype=X) builds the weights in X but never writes X to the top-level config.dtype, so model.dtype and model.config.dtype disagree:

import torch
from transformers import AutoModelForCausalLM, LlamaConfig

cfg = LlamaConfig(vocab_size=32, hidden_size=8, intermediate_size=16,
                  num_hidden_layers=1, num_attention_heads=2)
model = AutoModelForCausalLM.from_config(cfg, dtype=torch.float16)
print(model.dtype)         # torch.float16  <- weights honored
print(model.config.dtype)  # None           <- stale, expected torch.float16

In _from_config the resolved dtype is written to every sub-config but not to the top-level config, so for a leaf config (no sub_configs) the loop is a no-op and config.dtype is never updated:

https://github.com/huggingface/transformers/blob/d6a82ba896fcc59e6c40af95cbbd167dcf736e2f/src/transformers/modeling_utils.py#L1570-L1574

Expected behavior

After from_config(dtype=X), model.config.dtype == X, matching what from_pretrained already does.

The core fix is one line (config.dtype = dtype before the loop), but it is not a clean one-liner: it is a global _from_config change that breaks 4 tests in tests/utils/test_modeling_utils.py (test_model_from_config_dtype{,_str,_composite}, test_model_from_pretrained_dtype) and needs to handle the deprecated per-sub-config dtype-dict path (where dtype is a dict).

Surfaced while investigating #46459.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

After from_config(dtype=X), model.config.dtype == X, matching what from_pretrained already does.

The core fix is one line (config.dtype = dtype before the loop), but it is not a clean one-liner: it is a global _from_config change that breaks 4 tests in tests/utils/test_modeling_utils.py (test_model_from_config_dtype{,_str,_composite}, test_model_from_pretrained_dtype) and needs to handle the deprecated per-sub-config dtype-dict path (where dtype is a dict).

Surfaced while investigating #46459.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING