huggingface/pytorch-image-models · warning

This version of pytorch does not have F.scaled_dot_product_a

Error message

This version of pytorch does not have F.scaled_dot_product_attention, fused_attn flag ignored.

What it means

timm.layers.config.set_fused_attn enables fused scaled-dot-product attention globally, but if the installed PyTorch lacks torch.nn.functional.scaled_dot_product_attention (pre-2.0), it warns that the flag is ignored and leaves the setting unchanged — timm falls back to its manual attention implementation.

Source

Thrown at timm/layers/config.py:149

        global _NO_JIT
        global _NO_ACTIVATION_JIT
        _SCRIPTABLE, _EXPORTABLE, _NO_JIT, _NO_ACTIVATION_JIT = self.prev
        return False


def use_fused_attn(experimental: bool = False) -> bool:
    # NOTE: ONNX export cannot handle F.scaled_dot_product_attention as of pytorch 2.0
    if not _HAS_FUSED_ATTN or _EXPORTABLE:
        return False
    if experimental:
        return _USE_FUSED_ATTN > 1
    return _USE_FUSED_ATTN > 0


def set_fused_attn(enable: bool = True, experimental: bool = False):
    global _USE_FUSED_ATTN
    if not _HAS_FUSED_ATTN:
        warnings.warn('This version of pytorch does not have F.scaled_dot_product_attention, fused_attn flag ignored.')
        return
    if experimental and enable:
        _USE_FUSED_ATTN = 2
    elif enable:
        _USE_FUSED_ATTN = 1
    else:
        _USE_FUSED_ATTN = 0


def use_reentrant_ckpt() -> bool:
    return _USE_REENTRANT_CKPT


def set_reentrant_ckpt(enable: bool = True):
    global _USE_REENTRANT_CKPT
    _USE_REENTRANT_CKPT = enable

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Upgrade to torch >= 2.0 to actually get fused attention
  2. If stuck on old torch, remove the set_fused_attn call to silence the warning
  3. Verify with timm.layers.config.is_fused_attn() what state you're in

Example fix

# before
import torch; print(torch.__version__)  # 1.13
from timm.layers.config import set_fused_attn
set_fused_attn(True)  # warning
# after
pip install -U 'torch>=2.0'
set_fused_attn(True)
Defensive patterns

Strategy: fallback

Validate before calling

import torch\nHAS_SDPA = hasattr(torch.nn.functional, 'scaled_dot_product_attention')\nif HAS_SDPA:\n    set_fused_attn(True)

Prevention

When it happens

Trigger: Calling set_fused_attn(True) (or a model factory doing so) under torch < 2.0; environments where an old torch is pinned by another dependency.

Common situations: CUDA-driver or conda constraints forcing torch 1.13 or older; CI images with legacy torch. Functionality is unaffected (slower attention); the warning explains why fused attention never activates.

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/34abc01604e6f208. Report an issue: GitHub.