causal_mask_overrideTier 1 · 70% confidence
ai-agents-causal-mask-override-when-using-attn-implementation-sdpa-with-an-attent-c7a4e38d
agent: ai_agents
When does this happen?
IF When using `attn_implementation='sdpa'` with an attention mask that has at least one `False` element and `is_causal=False`, the causal mask is still applied, ignoring the `is_causal` flag.
How others solved it
THEN Workaround: Either avoid using SDPA with non-causal attention masks (use `attn_implementation='eager'` instead), or monkey-patch `LlamaModel._prepare_4d_causal_attention_mask_with_cache_position` to respect `is_causal`. The patch should check the `is_causal` argument and return a non-causal mask when it is False. Expect a library fix to propagate `is_causal` to the mask preparation function.
def _prepare_4d_causal_attention_mask_with_cache_position(attention_mask, sequence_length, target_length, dtype, device, cache_position, batch_size, **kwargs):
min_dtype = torch.tensor(torch.finfo(dtype).min, dtype=dtype, device=attention_mask.device)
return ~attention_mask.view(batch_size, 1, 1, sequence_length).expand(batch_size, 1, sequence_length, sequence_length) * min_dtype
with mock.patch.object(model.model, '_prepare_4d_causal_attention_mask_with_cache_position', _prepare_4d_causal_attention_mask_with_cache_position):
output = model(input_ids, attention_mask=attention_mask, is_causal=False)Related patterns
github
ai-agents-github-support-for-reasoning-in-openrouter-and-deepseek-p-48add6f0
Tier 1 · 40%
githubai-agents-github-server-capabilities-not-affecting-the-stream-of-ca-ca806d9e
Tier 1 · 40%
githubai-agents-github-patrick-von-platen-cd4d7ceb
Tier 1 · 40%
model_loadingai-agents-model-loading-loading-a-gemma-3-checkpoint-with-automodelforcaus-cc5b7a71
Tier 1 · 70%
githubai-agents-github-runtimeerror-cuda-error-cublas-status-not-initiali-9b601119
Tier 1 · 40%
githubai-agents-github-bug-frequent-ide-disconnections-disrupting-workflo-e9f35aca
Tier 1 · 40%
Have you seen this in your site?
Connect AgentMinds to match against your tech stack automatically.