研究发现不同注意力后端导致视觉语言模型输出不一致
来源标题:Are we interpreting the model of interest?
同一提示在 Qwen3-VL-4B-Instruct 的三种注意力后端上生成结果存在差异,20 张随机 COCO 图像中有 15 至 17 张的输出序列不一致。作者指出,即使语义差别不大,更换注意力后端也可能使想要研究的底层机制不再被使用。
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
All three executions describe roughly the same scene, but the details and wording diverge.
To see whether this was an isolated example, I ran the same prompt on 20 randomly sampled COCO images. The counts below shows whether the there exists any generated different token among the output sequences between each pair of backends:
15-17 out of 20 generations result in different output sequences across the three attention backends. While this does not necessarily mean they convey very different semantic information, it is already quite bothersome if we want to study some low-level/token-level mechanisms or predictions -- just by using another attention backend, the mechanism of interest may no longer be used.
from urllib.request import urlopen
import torch from PIL import Image from transformers import AutoModelForImageTextToText, AutoProcessor
model_id = "Qwen/Qwen3-VL-4B-Instruct" processor = AutoProcessor.from_pretrained(model_id) vlm = AutoModelForImageTextToText.from_pretrained( model_id, dtype=torch.bfloat16, attn_implementation="eager", ).to("cuda").eval() image = Image.open(urlopen( "https://s3.amazonaws.com/images.cocodataset.org/val2017/000000226903.jpg" )).convert("RGB") messages = [{"role": "user", "content": [ {"type": "image", "image": image}, {"type": "text", "text": "Describe the image."}, ]}] inputs = processor.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt").to("cuda")
def compare_backends(model, inputs, decoder, backends): logits, texts = {}, {} for backend in backends: model.set_attn_implementation(backend) with torch.inference_mode(): logits[backend] = model(**inputs, use_cache=False).logits[0, -1].cpu() tokens = model.generate(**inputs, do_sample=False, num_beams=1, max_new_tokens=128) generated = tokens[0, inputs["input_ids"].shape[1]:] texts[backend] = decoder.decode(generated, skip_special_tokens=True) return logits, texts
backends = ["eager", "sdpa", "flash_attention_2"] logits, texts = compare_backends(vlm, inputs, processor, backends) for backend, text in texts.items(): print(backend, text)
The differences are visible in next-token logits
Different continuations can amplify small numerical changes over many generation steps. To take a closer look at the upstream cause, here I compare next-token logits on the identical image-and-prompt prefix.
For every input, I calculate the root-mean-squared difference between each pair of backends' logits, then average this value over the 20 inputs:
So the runs already have measurably different next-token scores on the same prefix, even before we compare their later continuations.
For each input, I also compute the largest absolute difference over vocabulary entries, then average those per-input maximum values, to get a better sense of the magnitude of maximal differences.
import torch
def pairwise_logit_differences(logits_by_input):
One [backend, vocabulary] tensor per identical input prefix.
z = torch.stack(logits_by_input).double() # [input, backend, vocabulary] difference = z[:, :, None, :] - z[:, None, :, :] rmse = difference.square().mean(-1).sqrt().mean(0) max_abs = difference.abs().amax(-1).mean(0) return rmse, max_abs
Opening example only. For the 20-input average, pass 20 such tensors.
logits_by_input = [torch.stack([logits[b] for b in backends])] rmse, max_abs = pairwise_logit_differences(logits_by_input) print(rmse) print(max_abs)
Precision makes a substantial difference
If the choice of attention backend matters because of numerical computation, what happens when we change the floating-point precision?
I repeat the Qwen3-VL comparison on the same 20 inputs, holding precision fixed within each backend comparison:
The implementations agree much more closely at higher precision on these inputs. In FP32 especially, eager and SDPA generated identical token sequences across all 20 inputs, with a near zero logit RMSE difference. This pattern suggests that float number precision also plays an important role in determining model's behavior, as suggested in prior work (e.g. Qi et al., 2025).
However, since many models (such as the Qwen models) are trained in BF16 with flash attention and suggests inference to use the same setup, even if there is a better consistency across attention backends at FP32, switching to FP32 would cause a difference in behavior that is undesirable for carrying out interpretability analysis (see appendix for related comparisons).
FA2 was unavailable in FP32 in this setup since it does not support this precision.
import torch from transformers import AutoModelForImageTextToText
Reuse inputs, processor and compare_backends from the first example.
for dtype in (torch.float16, torch.float32): methods = ["eager", "sdpa"] if dtype == torch.float16: methods.append("flash_attention_2") # FA2 does not support FP32. precision_model = AutoModelForImageTextToText.from_pretrained( model_id, dtype=dtype, attn_implementation="eager", ).to("cuda").eval() z, text = compare_backends(precision_model, inputs, processor, methods) print(dtype, text) print(pairwise_logit_differences([torch.stack([z[b] for b in methods])])[0]) del precision_model
This isn't limited to vision-language models
Could this be a quirk of image inputs or Qwen3-VL? To check, I repeat the BF16 comparison on Qwen3-4B (a separately trained text-only model) and GPT-2 (the OG decoder-only transformer). Both models received the same 20 unfinished prose prefixes as literal text, with no chat template or special tokens added.
The following example shows the complete generations for both models, with differences highlighted.
Prefix: Fifty years later, the small coastal town had become
Both models use BF16 and greedy decoding. Yellow highlights mark spans that disagree with both other backends within that model.
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。