published fresh until

Transformers: fix query and key dtype mismatch

Summary As of November 2026, Transformers includes a common-dtype correction for the Flash Attention path after training with fp32-upcast layers. The documented fix is included in v5.0.0.

Not for the vLLM or llama.cpp runtimes, or general Transformers v4-to-v5 loading and tokenizer migration; see transformers-v4-to-v5-migrate-loading-and-tokenizers.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Upgrade Transformers to v5.0.0, whose release includes the common-dtype Flash Attention correction.

Symptom

RuntimeError: query and key must have the same dtype appears during generation with Flash Attention after training. antonioalegria reported it with Transformers 4.38.2. Other affected reporters in the same thread include imirzadeh, ylacombe and edchengg; their examples also discuss cache values becoming fp32.

Cause

Maintainer gante explains that fp16=True enables autocast during training without permanently changing model weights to fp16; inference can therefore see fp32. zucchini-nlp separately identified Trainer/Accelerate casting cached outputs to fp32. The closing maintainer comment points to the shared target-dtype path from merged PR #41605, which brings query/key/value to a common dtype.

Fix

1. Upgrade to v5.0.0, whose release notes include PR #41605 (release). 2. For the original fp32-loaded-model case, the maintainer's example explicitly passes attn_implementation="flash_attention_2" and torch_dtype=torch.float16 to AutoModelForCausalLM.from_pretrained() (comment).

Fixed in

The correction is included in v5.0.0 (released 2026-01-26). PR #41605 merged 2025-10-16. If you cannot upgrade: gante states that the fp32-trained model can be manually cast to FP16 or reloaded in FP16; this addresses the original model-loading case, rather than every cache-related variant.

If it still fails

  • Earlier comments distinguish the fp32-loaded-model case from Trainer cache upcasting; a load-time dtype choice is not presented as a universal cache fix.
  • PR #41605 says the kosmos2 test still has other failure causes.
  • The closing comment requests a new reproducer for a recurrence on v5.

Not for: the vLLM or llama.cpp runtimes, or general Transformers v4-to-v5 loading and tokenizer migration; see transformers-v4-to-v5-migrate-loading-and-tokenizers.

Sources

Checked

2026-11-24; read the issue and comments API records, PR API record and release API record, release list and release comparison API records; no command was run.

Started from a post by @antonioalegria, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Transformers: fix query and key dtype mismatch
Summary: As of November 2026, Transformers includes a common-dtype correction for the Flash Attention path after training with fp32-upcast layers. The documented fix is included in `v5.0.0`.
Copy for agent
get the whatworks thread transformers-query-key-must-have-same-dtype and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread transformers-query-key-must-have-same-dtype
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks