Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Update flex_attention.py

Daniel Han0da004c70ec1
diff --git a/unsloth/kernels/flex_attention.py b/unsloth/kernels/flex_attention.py
index 1eb24869980..a992a023829 100644
--- a/unsloth/kernels/flex_attention.py
+++ b/unsloth/kernels/flex_attention.py
@@ -25,18 +25,23 @@ torch_compile_options = {
}
# Flex Attention supported from torch 2.5 onwards only
-import torch.nn.attention
-if hasattr(torch.nn.attention, "flex_attention"):
- import torch.nn.attention.flex_attention
- from torch.nn.attention.flex_attention import flex_attention
- from torch.nn.attention.flex_attention import create_block_mask
- FLEX_ATTENTION_PADDING = getattr(
- torch.nn.attention.flex_attention,
- "_DEFAULT_SPARSE_BLOCK_SIZE",
- 1,
- )
- flex_attention = torch.compile(flex_attention, dynamic = False)
- HAS_FLEX_ATTENTION = True
+import torch.nn
+if hasattr(torch.nn, "attention"):
+ import torch.nn.attention
+ if hasattr(torch.nn.attention, "flex_attention"):
+ import torch.nn.attention.flex_attention
+ from torch.nn.attention.flex_attention import flex_attention
+ from torch.nn.attention.flex_attention import create_block_mask
+ FLEX_ATTENTION_PADDING = getattr(
+ torch.nn.attention.flex_attention,
+ "_DEFAULT_SPARSE_BLOCK_SIZE",
+ 1,
+ )
+ flex_attention = torch.compile(flex_attention, dynamic = False)
+ HAS_FLEX_ATTENTION = True
+ else:
+ HAS_FLEX_ATTENTION = False
+ pass
else:
HAS_FLEX_ATTENTION = False
pass