Instructions to use j4s0ch3/Qwen-image-diffusers with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use j4s0ch3/Qwen-image-diffusers with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("j4s0ch3/Qwen-image-diffusers", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
#7
by j4s0ch3 - opened
- .gitattributes +1 -0
- flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/__init__.cpython-310.pyc +0 -0
- flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/_ops.cpython-310.pyc +0 -0
- flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/flash_attn_interface.cpython-310.pyc +0 -0
- flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/_flash_attn3_1d39a44.abi3.so +3 -0
- flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/_ops.py +3 -3
- flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/flash_attn3/__init__.py +26 -0
- flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/flash_attn_interface.py +44 -31
.gitattributes
CHANGED
|
@@ -37,3 +37,4 @@ Qwen-Image-Edit-2509/processor/tokenizer.json filter=lfs diff=lfs merge=lfs -tex
|
|
| 37 |
Qwen-Image-i2i_merged/processor/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
| 38 |
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/_vllm_flash_attn3_28fbd26_dirty.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 39 |
vllm_flash_attn3/_flash_attn3_1d39a44.abi3.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 37 |
Qwen-Image-i2i_merged/processor/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
| 38 |
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/_vllm_flash_attn3_28fbd26_dirty.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 39 |
vllm_flash_attn3/_flash_attn3_1d39a44.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/_flash_attn3_1d39a44.abi3.so filter=lfs diff=lfs merge=lfs -text
|
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/__init__.cpython-310.pyc
CHANGED
|
Binary files a/flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/__init__.cpython-310.pyc and b/flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/__init__.cpython-310.pyc differ
|
|
|
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/_ops.cpython-310.pyc
CHANGED
|
Binary files a/flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/_ops.cpython-310.pyc and b/flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/_ops.cpython-310.pyc differ
|
|
|
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/flash_attn_interface.cpython-310.pyc
CHANGED
|
Binary files a/flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/flash_attn_interface.cpython-310.pyc and b/flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/__pycache__/flash_attn_interface.cpython-310.pyc differ
|
|
|
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/_flash_attn3_1d39a44.abi3.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:17fbfaef77c6560c59ca5751ace86ca16a3af0471da3d265c4b101e3b1b2ec30
|
| 3 |
+
size 779759224
|
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/_ops.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
from . import
|
| 3 |
-
ops = torch.ops.
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
-
return f"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from . import _flash_attn3_1d39a44
|
| 3 |
+
ops = torch.ops._flash_attn3_1d39a44
|
| 4 |
|
| 5 |
def add_op_namespace_prefix(op_name: str):
|
| 6 |
"""
|
| 7 |
Prefix op by namespace.
|
| 8 |
"""
|
| 9 |
+
return f"_flash_attn3_1d39a44::{op_name}"
|
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/flash_attn3/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ctypes
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
import importlib
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from types import ModuleType
|
| 7 |
+
|
| 8 |
+
def _import_from_path(file_path: Path) -> ModuleType:
|
| 9 |
+
# We cannot use the module name as-is, after adding it to `sys.modules`,
|
| 10 |
+
# it would also be used for other imports. So, we make a module name that
|
| 11 |
+
# depends on the path for it to be unique using the hex-encoded hash of
|
| 12 |
+
# the path.
|
| 13 |
+
path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
|
| 14 |
+
module_name = path_hash
|
| 15 |
+
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 16 |
+
if spec is None:
|
| 17 |
+
raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
|
| 18 |
+
module = importlib.util.module_from_spec(spec)
|
| 19 |
+
if module is None:
|
| 20 |
+
raise ImportError(f"Cannot load module {module_name} from spec")
|
| 21 |
+
sys.modules[module_name] = module
|
| 22 |
+
spec.loader.exec_module(module) # type: ignore
|
| 23 |
+
return module
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
|
flash-attn3-kernel/torch28-cxx11-cu128-x86_64-linux/vllm_flash_attn3/flash_attn_interface.py
CHANGED
|
@@ -5,12 +5,7 @@ from typing import Optional, Union
|
|
| 5 |
import torch
|
| 6 |
import torch.nn as nn
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# We need to import the CUDA kernels after importing torch
|
| 10 |
-
from ._ops import ops
|
| 11 |
-
|
| 12 |
-
# isort: on
|
| 13 |
-
|
| 14 |
|
| 15 |
def maybe_contiguous(x):
|
| 16 |
return x.contiguous() if x is not None and x.stride(-1) != 1 else x
|
|
@@ -43,13 +38,13 @@ def _flash_attn_forward(
|
|
| 43 |
softmax_scale,
|
| 44 |
causal,
|
| 45 |
window_size=(-1, -1),
|
|
|
|
| 46 |
softcap=0.0,
|
| 47 |
rotary_interleaved=True,
|
| 48 |
scheduler_metadata=None,
|
| 49 |
num_splits=1,
|
| 50 |
pack_gqa=None,
|
| 51 |
-
sm_margin=0
|
| 52 |
-
s_aux=None):
|
| 53 |
q, k, k_new, v_new = [maybe_contiguous(x) for x in (q, k, k_new, v_new)]
|
| 54 |
v = v.contiguous() if v.stride(-1) != 1 and v.stride(-3) != 1 else v
|
| 55 |
cu_seqlens_q, cu_seqlens_k, cu_seqlens_k_new = [
|
|
@@ -61,7 +56,7 @@ def _flash_attn_forward(
|
|
| 61 |
]
|
| 62 |
rotary_cos, rotary_sin = [maybe_contiguous(x) for x in (rotary_cos, rotary_sin)]
|
| 63 |
seqlens_rotary = maybe_contiguous(seqlens_rotary)
|
| 64 |
-
out, softmax_lse, *rest =
|
| 65 |
q,
|
| 66 |
k,
|
| 67 |
v,
|
|
@@ -89,13 +84,13 @@ def _flash_attn_forward(
|
|
| 89 |
causal,
|
| 90 |
window_size[0],
|
| 91 |
window_size[1],
|
|
|
|
| 92 |
softcap,
|
| 93 |
rotary_interleaved,
|
| 94 |
scheduler_metadata,
|
| 95 |
num_splits,
|
| 96 |
pack_gqa,
|
| 97 |
sm_margin,
|
| 98 |
-
s_aux
|
| 99 |
)
|
| 100 |
return out, softmax_lse, *rest
|
| 101 |
|
|
@@ -125,7 +120,7 @@ def _flash_attn_backward(
|
|
| 125 |
):
|
| 126 |
# dq, dk, dv are allocated by us so they should already be contiguous
|
| 127 |
dout, q, k, v, out = [maybe_contiguous(x) for x in (dout, q, k, v, out)]
|
| 128 |
-
dq, dk, dv, softmax_d, *rest =
|
| 129 |
dout,
|
| 130 |
q,
|
| 131 |
k,
|
|
@@ -161,9 +156,11 @@ class FlashAttnQKVPackedFunc(torch.autograd.Function):
|
|
| 161 |
causal,
|
| 162 |
q_descale=None, k_descale=None, v_descale=None,
|
| 163 |
window_size=(-1, -1),
|
|
|
|
| 164 |
softcap=0.0,
|
| 165 |
deterministic=False,
|
| 166 |
num_heads_q=None,
|
|
|
|
| 167 |
):
|
| 168 |
if softmax_scale is None:
|
| 169 |
softmax_scale = qkv.shape[-1] ** (-0.5)
|
|
@@ -192,22 +189,27 @@ class FlashAttnQKVPackedFunc(torch.autograd.Function):
|
|
| 192 |
softmax_scale,
|
| 193 |
causal=causal,
|
| 194 |
window_size=window_size,
|
|
|
|
| 195 |
softcap=softcap,
|
|
|
|
| 196 |
)
|
| 197 |
# ctx.save_for_backward(q, k, v, out_padded, softmax_lse)
|
| 198 |
ctx.save_for_backward(q, k, v, out, softmax_lse)
|
| 199 |
ctx.softmax_scale = softmax_scale
|
| 200 |
ctx.causal = causal
|
| 201 |
ctx.window_size = window_size
|
|
|
|
| 202 |
ctx.softcap = softcap
|
| 203 |
ctx.deterministic = deterministic
|
| 204 |
ctx.ndim = qkv.dim()
|
|
|
|
| 205 |
# return out, softmax_lse
|
| 206 |
return out
|
| 207 |
|
| 208 |
@staticmethod
|
| 209 |
def backward(ctx, dout, *args):
|
| 210 |
q, k, v, out, softmax_lse = ctx.saved_tensors
|
|
|
|
| 211 |
if ctx.ndim == 5:
|
| 212 |
qkv_shape = q.shape[:-2] + (3, *q.shape[-2:])
|
| 213 |
dqkv = torch.empty(qkv_shape, dtype=q.dtype, device=q.device)
|
|
@@ -235,10 +237,11 @@ class FlashAttnQKVPackedFunc(torch.autograd.Function):
|
|
| 235 |
ctx.causal,
|
| 236 |
ctx.window_size,
|
| 237 |
ctx.softcap,
|
| 238 |
-
ctx.deterministic,
|
|
|
|
| 239 |
)
|
| 240 |
dqkv = dqkv[..., : dout.shape[-1]] # We could have padded the head dimension
|
| 241 |
-
return dqkv, None, None, None, None, None, None, None, None, None, None
|
| 242 |
|
| 243 |
|
| 244 |
class FlashAttnFunc(torch.autograd.Function):
|
|
@@ -254,12 +257,12 @@ class FlashAttnFunc(torch.autograd.Function):
|
|
| 254 |
qv=None,
|
| 255 |
q_descale=None, k_descale=None, v_descale=None,
|
| 256 |
window_size=(-1, -1),
|
|
|
|
| 257 |
softcap=0.0,
|
| 258 |
num_splits=1,
|
| 259 |
pack_gqa=None,
|
| 260 |
deterministic=False,
|
| 261 |
sm_margin=0,
|
| 262 |
-
s_aux=None,
|
| 263 |
):
|
| 264 |
if softmax_scale is None:
|
| 265 |
softmax_scale = (q.shape[-1] + (qv.shape[-1] if qv is not None else 0)) ** (-0.5)
|
|
@@ -280,17 +283,18 @@ class FlashAttnFunc(torch.autograd.Function):
|
|
| 280 |
softmax_scale,
|
| 281 |
causal=causal,
|
| 282 |
window_size=window_size,
|
|
|
|
| 283 |
softcap=softcap,
|
| 284 |
num_splits=num_splits,
|
| 285 |
pack_gqa=pack_gqa,
|
| 286 |
sm_margin=sm_margin,
|
| 287 |
-
s_aux=s_aux,
|
| 288 |
)
|
| 289 |
# ctx.save_for_backward(q, k, v, out_padded, softmax_lse)
|
| 290 |
ctx.save_for_backward(q, k, v, out, softmax_lse)
|
| 291 |
ctx.softmax_scale = softmax_scale
|
| 292 |
ctx.causal = causal
|
| 293 |
ctx.window_size = window_size
|
|
|
|
| 294 |
ctx.softcap = softcap
|
| 295 |
ctx.deterministic = deterministic
|
| 296 |
ctx.sm_margin = sm_margin
|
|
@@ -299,6 +303,7 @@ class FlashAttnFunc(torch.autograd.Function):
|
|
| 299 |
@staticmethod
|
| 300 |
def backward(ctx, dout, *args):
|
| 301 |
q, k, v, out, softmax_lse = ctx.saved_tensors
|
|
|
|
| 302 |
dq, dk, dv = torch.empty_like(q), torch.empty_like(k), torch.empty_like(v)
|
| 303 |
_flash_attn_backward(
|
| 304 |
dout,
|
|
@@ -320,9 +325,9 @@ class FlashAttnFunc(torch.autograd.Function):
|
|
| 320 |
ctx.deterministic,
|
| 321 |
ctx.sm_margin,
|
| 322 |
)
|
| 323 |
-
dq = dq[..., :
|
| 324 |
-
dk = dk[..., :
|
| 325 |
-
dv = dv[..., :
|
| 326 |
return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None, None, None, None
|
| 327 |
|
| 328 |
|
|
@@ -345,12 +350,12 @@ class FlashAttnVarlenFunc(torch.autograd.Function):
|
|
| 345 |
qv=None,
|
| 346 |
q_descale=None, k_descale=None, v_descale=None,
|
| 347 |
window_size=(-1, -1),
|
|
|
|
| 348 |
softcap=0.0,
|
| 349 |
num_splits=1,
|
| 350 |
pack_gqa=None,
|
| 351 |
deterministic=False,
|
| 352 |
sm_margin=0,
|
| 353 |
-
s_aux=None,
|
| 354 |
):
|
| 355 |
if softmax_scale is None:
|
| 356 |
softmax_scale = (q.shape[-1] + (qv.shape[-1] if qv is not None else 0)) ** (-0.5)
|
|
@@ -375,11 +380,11 @@ class FlashAttnVarlenFunc(torch.autograd.Function):
|
|
| 375 |
softmax_scale,
|
| 376 |
causal=causal,
|
| 377 |
window_size=window_size,
|
|
|
|
| 378 |
softcap=softcap,
|
| 379 |
num_splits=num_splits,
|
| 380 |
pack_gqa=pack_gqa,
|
| 381 |
sm_margin=sm_margin,
|
| 382 |
-
s_aux=s_aux,
|
| 383 |
)
|
| 384 |
# ctx.save_for_backward(q, k, v, out_padded, softmax_lse, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k)
|
| 385 |
ctx.save_for_backward(q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k)
|
|
@@ -388,6 +393,7 @@ class FlashAttnVarlenFunc(torch.autograd.Function):
|
|
| 388 |
ctx.softmax_scale = softmax_scale
|
| 389 |
ctx.causal = causal
|
| 390 |
ctx.window_size = window_size
|
|
|
|
| 391 |
ctx.softcap = softcap
|
| 392 |
ctx.deterministic = deterministic
|
| 393 |
ctx.sm_margin = sm_margin
|
|
@@ -396,6 +402,7 @@ class FlashAttnVarlenFunc(torch.autograd.Function):
|
|
| 396 |
@staticmethod
|
| 397 |
def backward(ctx, dout, *args):
|
| 398 |
q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k = ctx.saved_tensors
|
|
|
|
| 399 |
dq, dk, dv = torch.empty_like(q), torch.empty_like(k), torch.empty_like(v)
|
| 400 |
_flash_attn_backward(
|
| 401 |
dout,
|
|
@@ -420,9 +427,9 @@ class FlashAttnVarlenFunc(torch.autograd.Function):
|
|
| 420 |
ctx.deterministic,
|
| 421 |
ctx.sm_margin,
|
| 422 |
)
|
| 423 |
-
dq = dq[..., :
|
| 424 |
-
dk = dk[..., :
|
| 425 |
-
dv = dv[..., :
|
| 426 |
return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None
|
| 427 |
|
| 428 |
|
|
@@ -432,9 +439,11 @@ def flash_attn_qkvpacked_func(
|
|
| 432 |
causal=False,
|
| 433 |
q_descale=None, k_descale=None, v_descale=None,
|
| 434 |
window_size=(-1, -1),
|
|
|
|
| 435 |
softcap=0.0,
|
| 436 |
deterministic=False,
|
| 437 |
num_heads_q=None,
|
|
|
|
| 438 |
):
|
| 439 |
"""dropout_p should be set to 0.0 during evaluation
|
| 440 |
If Q, K, V are already stacked into 1 tensor, this function will be faster than
|
|
@@ -476,9 +485,11 @@ def flash_attn_qkvpacked_func(
|
|
| 476 |
causal,
|
| 477 |
q_descale, k_descale, v_descale,
|
| 478 |
window_size,
|
|
|
|
| 479 |
softcap,
|
| 480 |
deterministic,
|
| 481 |
num_heads_q,
|
|
|
|
| 482 |
)
|
| 483 |
|
| 484 |
|
|
@@ -491,12 +502,12 @@ def flash_attn_func(
|
|
| 491 |
qv=None,
|
| 492 |
q_descale=None, k_descale=None, v_descale=None,
|
| 493 |
window_size=(-1, -1),
|
|
|
|
| 494 |
softcap=0.0,
|
| 495 |
num_splits=1,
|
| 496 |
pack_gqa=None,
|
| 497 |
deterministic=False,
|
| 498 |
sm_margin=0,
|
| 499 |
-
s_aux=None,
|
| 500 |
):
|
| 501 |
"""dropout_p should be set to 0.0 during evaluation
|
| 502 |
Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads
|
|
@@ -552,12 +563,12 @@ def flash_attn_func(
|
|
| 552 |
qv,
|
| 553 |
q_descale, k_descale, v_descale,
|
| 554 |
window_size,
|
|
|
|
| 555 |
softcap,
|
| 556 |
num_splits,
|
| 557 |
pack_gqa,
|
| 558 |
deterministic,
|
| 559 |
sm_margin,
|
| 560 |
-
s_aux,
|
| 561 |
)
|
| 562 |
|
| 563 |
|
|
@@ -576,12 +587,12 @@ def flash_attn_varlen_func(
|
|
| 576 |
qv=None,
|
| 577 |
q_descale=None, k_descale=None, v_descale=None,
|
| 578 |
window_size=(-1, -1),
|
|
|
|
| 579 |
softcap=0.0,
|
| 580 |
num_splits=1,
|
| 581 |
pack_gqa=None,
|
| 582 |
deterministic=False,
|
| 583 |
sm_margin=0,
|
| 584 |
-
s_aux=None,
|
| 585 |
):
|
| 586 |
return FlashAttnVarlenFunc.apply(
|
| 587 |
q,
|
|
@@ -598,17 +609,17 @@ def flash_attn_varlen_func(
|
|
| 598 |
qv,
|
| 599 |
q_descale, k_descale, v_descale,
|
| 600 |
window_size,
|
|
|
|
| 601 |
softcap,
|
| 602 |
num_splits,
|
| 603 |
pack_gqa,
|
| 604 |
deterministic,
|
| 605 |
sm_margin,
|
| 606 |
-
s_aux,
|
| 607 |
)
|
| 608 |
|
| 609 |
|
| 610 |
def flash_attn_combine(out_partial, lse_partial, out=None, out_dtype=None):
|
| 611 |
-
return
|
| 612 |
|
| 613 |
|
| 614 |
def flash_attn_with_kvcache(
|
|
@@ -634,6 +645,7 @@ def flash_attn_with_kvcache(
|
|
| 634 |
softmax_scale=None,
|
| 635 |
causal=False,
|
| 636 |
window_size=(-1, -1), # -1 means infinite context window
|
|
|
|
| 637 |
softcap=0.0, # 0.0 means deactivated
|
| 638 |
rotary_interleaved=True,
|
| 639 |
scheduler_metadata=None,
|
|
@@ -641,7 +653,6 @@ def flash_attn_with_kvcache(
|
|
| 641 |
pack_gqa=None, # Can be tuned for speed
|
| 642 |
sm_margin=0, # Can be tuned if some SMs are used for communication
|
| 643 |
return_softmax_lse=False,
|
| 644 |
-
s_aux=None,
|
| 645 |
):
|
| 646 |
"""
|
| 647 |
If k and v are not None, k_cache and v_cache will be updated *inplace* with the new values from
|
|
@@ -762,13 +773,13 @@ def flash_attn_with_kvcache(
|
|
| 762 |
softmax_scale,
|
| 763 |
causal=causal,
|
| 764 |
window_size=window_size,
|
|
|
|
| 765 |
softcap=softcap,
|
| 766 |
rotary_interleaved=rotary_interleaved,
|
| 767 |
scheduler_metadata=scheduler_metadata,
|
| 768 |
num_splits=num_splits,
|
| 769 |
pack_gqa=pack_gqa,
|
| 770 |
sm_margin=sm_margin,
|
| 771 |
-
s_aux=s_aux,
|
| 772 |
)
|
| 773 |
# return (out, softmax_lse) if return_softmax_lse else out
|
| 774 |
return (out, softmax_lse, *rest) if return_softmax_lse else out
|
|
@@ -786,6 +797,7 @@ def get_scheduler_metadata(
|
|
| 786 |
max_seqlen_k_new=0,
|
| 787 |
causal=False,
|
| 788 |
window_size=(-1, -1), # -1 means infinite context window
|
|
|
|
| 789 |
has_softcap=False,
|
| 790 |
num_splits=0, # Can be tuned for speed
|
| 791 |
pack_gqa=None, # Can be tuned for speed
|
|
@@ -794,7 +806,7 @@ def get_scheduler_metadata(
|
|
| 794 |
cache_seqlens = maybe_contiguous(cache_seqlens)
|
| 795 |
if headdim_v is None:
|
| 796 |
headdim_v = headdim
|
| 797 |
-
scheduler_metadata =
|
| 798 |
batch_size, max_seqlen_q, max_seqlen_k, num_heads_q, num_heads_kv, headdim, headdim_v,
|
| 799 |
qkv_dtype,
|
| 800 |
cache_seqlens,
|
|
@@ -807,6 +819,7 @@ def get_scheduler_metadata(
|
|
| 807 |
max_seqlen_k_new,
|
| 808 |
causal,
|
| 809 |
window_size[0], window_size[1],
|
|
|
|
| 810 |
has_softcap,
|
| 811 |
num_splits,
|
| 812 |
pack_gqa,
|
|
|
|
| 5 |
import torch
|
| 6 |
import torch.nn as nn
|
| 7 |
|
| 8 |
+
from ._ops import ops as flash_attn_3_cuda
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def maybe_contiguous(x):
|
| 11 |
return x.contiguous() if x is not None and x.stride(-1) != 1 else x
|
|
|
|
| 38 |
softmax_scale,
|
| 39 |
causal,
|
| 40 |
window_size=(-1, -1),
|
| 41 |
+
attention_chunk=0,
|
| 42 |
softcap=0.0,
|
| 43 |
rotary_interleaved=True,
|
| 44 |
scheduler_metadata=None,
|
| 45 |
num_splits=1,
|
| 46 |
pack_gqa=None,
|
| 47 |
+
sm_margin=0):
|
|
|
|
| 48 |
q, k, k_new, v_new = [maybe_contiguous(x) for x in (q, k, k_new, v_new)]
|
| 49 |
v = v.contiguous() if v.stride(-1) != 1 and v.stride(-3) != 1 else v
|
| 50 |
cu_seqlens_q, cu_seqlens_k, cu_seqlens_k_new = [
|
|
|
|
| 56 |
]
|
| 57 |
rotary_cos, rotary_sin = [maybe_contiguous(x) for x in (rotary_cos, rotary_sin)]
|
| 58 |
seqlens_rotary = maybe_contiguous(seqlens_rotary)
|
| 59 |
+
out, softmax_lse, *rest = flash_attn_3_cuda.fwd(
|
| 60 |
q,
|
| 61 |
k,
|
| 62 |
v,
|
|
|
|
| 84 |
causal,
|
| 85 |
window_size[0],
|
| 86 |
window_size[1],
|
| 87 |
+
attention_chunk,
|
| 88 |
softcap,
|
| 89 |
rotary_interleaved,
|
| 90 |
scheduler_metadata,
|
| 91 |
num_splits,
|
| 92 |
pack_gqa,
|
| 93 |
sm_margin,
|
|
|
|
| 94 |
)
|
| 95 |
return out, softmax_lse, *rest
|
| 96 |
|
|
|
|
| 120 |
):
|
| 121 |
# dq, dk, dv are allocated by us so they should already be contiguous
|
| 122 |
dout, q, k, v, out = [maybe_contiguous(x) for x in (dout, q, k, v, out)]
|
| 123 |
+
dq, dk, dv, softmax_d, *rest = flash_attn_3_cuda.bwd(
|
| 124 |
dout,
|
| 125 |
q,
|
| 126 |
k,
|
|
|
|
| 156 |
causal,
|
| 157 |
q_descale=None, k_descale=None, v_descale=None,
|
| 158 |
window_size=(-1, -1),
|
| 159 |
+
attention_chunk=0,
|
| 160 |
softcap=0.0,
|
| 161 |
deterministic=False,
|
| 162 |
num_heads_q=None,
|
| 163 |
+
sm_margin=0,
|
| 164 |
):
|
| 165 |
if softmax_scale is None:
|
| 166 |
softmax_scale = qkv.shape[-1] ** (-0.5)
|
|
|
|
| 189 |
softmax_scale,
|
| 190 |
causal=causal,
|
| 191 |
window_size=window_size,
|
| 192 |
+
attention_chunk=attention_chunk,
|
| 193 |
softcap=softcap,
|
| 194 |
+
sm_margin=sm_margin,
|
| 195 |
)
|
| 196 |
# ctx.save_for_backward(q, k, v, out_padded, softmax_lse)
|
| 197 |
ctx.save_for_backward(q, k, v, out, softmax_lse)
|
| 198 |
ctx.softmax_scale = softmax_scale
|
| 199 |
ctx.causal = causal
|
| 200 |
ctx.window_size = window_size
|
| 201 |
+
ctx.attention_chunk = attention_chunk
|
| 202 |
ctx.softcap = softcap
|
| 203 |
ctx.deterministic = deterministic
|
| 204 |
ctx.ndim = qkv.dim()
|
| 205 |
+
ctx.sm_margin = sm_margin
|
| 206 |
# return out, softmax_lse
|
| 207 |
return out
|
| 208 |
|
| 209 |
@staticmethod
|
| 210 |
def backward(ctx, dout, *args):
|
| 211 |
q, k, v, out, softmax_lse = ctx.saved_tensors
|
| 212 |
+
assert ctx.attention_chunk == 0, "FA3 backward does not support attention_chunk"
|
| 213 |
if ctx.ndim == 5:
|
| 214 |
qkv_shape = q.shape[:-2] + (3, *q.shape[-2:])
|
| 215 |
dqkv = torch.empty(qkv_shape, dtype=q.dtype, device=q.device)
|
|
|
|
| 237 |
ctx.causal,
|
| 238 |
ctx.window_size,
|
| 239 |
ctx.softcap,
|
| 240 |
+
ctx.deterministic,
|
| 241 |
+
ctx.sm_margin,
|
| 242 |
)
|
| 243 |
dqkv = dqkv[..., : dout.shape[-1]] # We could have padded the head dimension
|
| 244 |
+
return dqkv, None, None, None, None, None, None, None, None, None, None, None
|
| 245 |
|
| 246 |
|
| 247 |
class FlashAttnFunc(torch.autograd.Function):
|
|
|
|
| 257 |
qv=None,
|
| 258 |
q_descale=None, k_descale=None, v_descale=None,
|
| 259 |
window_size=(-1, -1),
|
| 260 |
+
attention_chunk=0,
|
| 261 |
softcap=0.0,
|
| 262 |
num_splits=1,
|
| 263 |
pack_gqa=None,
|
| 264 |
deterministic=False,
|
| 265 |
sm_margin=0,
|
|
|
|
| 266 |
):
|
| 267 |
if softmax_scale is None:
|
| 268 |
softmax_scale = (q.shape[-1] + (qv.shape[-1] if qv is not None else 0)) ** (-0.5)
|
|
|
|
| 283 |
softmax_scale,
|
| 284 |
causal=causal,
|
| 285 |
window_size=window_size,
|
| 286 |
+
attention_chunk=attention_chunk,
|
| 287 |
softcap=softcap,
|
| 288 |
num_splits=num_splits,
|
| 289 |
pack_gqa=pack_gqa,
|
| 290 |
sm_margin=sm_margin,
|
|
|
|
| 291 |
)
|
| 292 |
# ctx.save_for_backward(q, k, v, out_padded, softmax_lse)
|
| 293 |
ctx.save_for_backward(q, k, v, out, softmax_lse)
|
| 294 |
ctx.softmax_scale = softmax_scale
|
| 295 |
ctx.causal = causal
|
| 296 |
ctx.window_size = window_size
|
| 297 |
+
ctx.attention_chunk = attention_chunk
|
| 298 |
ctx.softcap = softcap
|
| 299 |
ctx.deterministic = deterministic
|
| 300 |
ctx.sm_margin = sm_margin
|
|
|
|
| 303 |
@staticmethod
|
| 304 |
def backward(ctx, dout, *args):
|
| 305 |
q, k, v, out, softmax_lse = ctx.saved_tensors
|
| 306 |
+
assert ctx.attention_chunk == 0, "FA3 backward does not support attention_chunk"
|
| 307 |
dq, dk, dv = torch.empty_like(q), torch.empty_like(k), torch.empty_like(v)
|
| 308 |
_flash_attn_backward(
|
| 309 |
dout,
|
|
|
|
| 325 |
ctx.deterministic,
|
| 326 |
ctx.sm_margin,
|
| 327 |
)
|
| 328 |
+
dq = dq[..., : q.shape[-1]] # We could have padded the head dimension
|
| 329 |
+
dk = dk[..., : k.shape[-1]]
|
| 330 |
+
dv = dv[..., : v.shape[-1]]
|
| 331 |
return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None, None, None, None
|
| 332 |
|
| 333 |
|
|
|
|
| 350 |
qv=None,
|
| 351 |
q_descale=None, k_descale=None, v_descale=None,
|
| 352 |
window_size=(-1, -1),
|
| 353 |
+
attention_chunk=0,
|
| 354 |
softcap=0.0,
|
| 355 |
num_splits=1,
|
| 356 |
pack_gqa=None,
|
| 357 |
deterministic=False,
|
| 358 |
sm_margin=0,
|
|
|
|
| 359 |
):
|
| 360 |
if softmax_scale is None:
|
| 361 |
softmax_scale = (q.shape[-1] + (qv.shape[-1] if qv is not None else 0)) ** (-0.5)
|
|
|
|
| 380 |
softmax_scale,
|
| 381 |
causal=causal,
|
| 382 |
window_size=window_size,
|
| 383 |
+
attention_chunk=attention_chunk,
|
| 384 |
softcap=softcap,
|
| 385 |
num_splits=num_splits,
|
| 386 |
pack_gqa=pack_gqa,
|
| 387 |
sm_margin=sm_margin,
|
|
|
|
| 388 |
)
|
| 389 |
# ctx.save_for_backward(q, k, v, out_padded, softmax_lse, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k)
|
| 390 |
ctx.save_for_backward(q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k)
|
|
|
|
| 393 |
ctx.softmax_scale = softmax_scale
|
| 394 |
ctx.causal = causal
|
| 395 |
ctx.window_size = window_size
|
| 396 |
+
ctx.attention_chunk = attention_chunk
|
| 397 |
ctx.softcap = softcap
|
| 398 |
ctx.deterministic = deterministic
|
| 399 |
ctx.sm_margin = sm_margin
|
|
|
|
| 402 |
@staticmethod
|
| 403 |
def backward(ctx, dout, *args):
|
| 404 |
q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k = ctx.saved_tensors
|
| 405 |
+
assert ctx.attention_chunk == 0, "FA3 backward does not support attention_chunk"
|
| 406 |
dq, dk, dv = torch.empty_like(q), torch.empty_like(k), torch.empty_like(v)
|
| 407 |
_flash_attn_backward(
|
| 408 |
dout,
|
|
|
|
| 427 |
ctx.deterministic,
|
| 428 |
ctx.sm_margin,
|
| 429 |
)
|
| 430 |
+
dq = dq[..., : q.shape[-1]] # We could have padded the head dimension
|
| 431 |
+
dk = dk[..., : k.shape[-1]]
|
| 432 |
+
dv = dv[..., : v.shape[-1]]
|
| 433 |
return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None
|
| 434 |
|
| 435 |
|
|
|
|
| 439 |
causal=False,
|
| 440 |
q_descale=None, k_descale=None, v_descale=None,
|
| 441 |
window_size=(-1, -1),
|
| 442 |
+
attention_chunk=0,
|
| 443 |
softcap=0.0,
|
| 444 |
deterministic=False,
|
| 445 |
num_heads_q=None,
|
| 446 |
+
sm_margin=0,
|
| 447 |
):
|
| 448 |
"""dropout_p should be set to 0.0 during evaluation
|
| 449 |
If Q, K, V are already stacked into 1 tensor, this function will be faster than
|
|
|
|
| 485 |
causal,
|
| 486 |
q_descale, k_descale, v_descale,
|
| 487 |
window_size,
|
| 488 |
+
attention_chunk,
|
| 489 |
softcap,
|
| 490 |
deterministic,
|
| 491 |
num_heads_q,
|
| 492 |
+
sm_margin,
|
| 493 |
)
|
| 494 |
|
| 495 |
|
|
|
|
| 502 |
qv=None,
|
| 503 |
q_descale=None, k_descale=None, v_descale=None,
|
| 504 |
window_size=(-1, -1),
|
| 505 |
+
attention_chunk=0,
|
| 506 |
softcap=0.0,
|
| 507 |
num_splits=1,
|
| 508 |
pack_gqa=None,
|
| 509 |
deterministic=False,
|
| 510 |
sm_margin=0,
|
|
|
|
| 511 |
):
|
| 512 |
"""dropout_p should be set to 0.0 during evaluation
|
| 513 |
Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads
|
|
|
|
| 563 |
qv,
|
| 564 |
q_descale, k_descale, v_descale,
|
| 565 |
window_size,
|
| 566 |
+
attention_chunk,
|
| 567 |
softcap,
|
| 568 |
num_splits,
|
| 569 |
pack_gqa,
|
| 570 |
deterministic,
|
| 571 |
sm_margin,
|
|
|
|
| 572 |
)
|
| 573 |
|
| 574 |
|
|
|
|
| 587 |
qv=None,
|
| 588 |
q_descale=None, k_descale=None, v_descale=None,
|
| 589 |
window_size=(-1, -1),
|
| 590 |
+
attention_chunk=0,
|
| 591 |
softcap=0.0,
|
| 592 |
num_splits=1,
|
| 593 |
pack_gqa=None,
|
| 594 |
deterministic=False,
|
| 595 |
sm_margin=0,
|
|
|
|
| 596 |
):
|
| 597 |
return FlashAttnVarlenFunc.apply(
|
| 598 |
q,
|
|
|
|
| 609 |
qv,
|
| 610 |
q_descale, k_descale, v_descale,
|
| 611 |
window_size,
|
| 612 |
+
attention_chunk,
|
| 613 |
softcap,
|
| 614 |
num_splits,
|
| 615 |
pack_gqa,
|
| 616 |
deterministic,
|
| 617 |
sm_margin,
|
|
|
|
| 618 |
)
|
| 619 |
|
| 620 |
|
| 621 |
def flash_attn_combine(out_partial, lse_partial, out=None, out_dtype=None):
|
| 622 |
+
return flash_attn_3_cuda.fwd_combine(out_partial, lse_partial, out, out_dtype)
|
| 623 |
|
| 624 |
|
| 625 |
def flash_attn_with_kvcache(
|
|
|
|
| 645 |
softmax_scale=None,
|
| 646 |
causal=False,
|
| 647 |
window_size=(-1, -1), # -1 means infinite context window
|
| 648 |
+
attention_chunk=0,
|
| 649 |
softcap=0.0, # 0.0 means deactivated
|
| 650 |
rotary_interleaved=True,
|
| 651 |
scheduler_metadata=None,
|
|
|
|
| 653 |
pack_gqa=None, # Can be tuned for speed
|
| 654 |
sm_margin=0, # Can be tuned if some SMs are used for communication
|
| 655 |
return_softmax_lse=False,
|
|
|
|
| 656 |
):
|
| 657 |
"""
|
| 658 |
If k and v are not None, k_cache and v_cache will be updated *inplace* with the new values from
|
|
|
|
| 773 |
softmax_scale,
|
| 774 |
causal=causal,
|
| 775 |
window_size=window_size,
|
| 776 |
+
attention_chunk=attention_chunk,
|
| 777 |
softcap=softcap,
|
| 778 |
rotary_interleaved=rotary_interleaved,
|
| 779 |
scheduler_metadata=scheduler_metadata,
|
| 780 |
num_splits=num_splits,
|
| 781 |
pack_gqa=pack_gqa,
|
| 782 |
sm_margin=sm_margin,
|
|
|
|
| 783 |
)
|
| 784 |
# return (out, softmax_lse) if return_softmax_lse else out
|
| 785 |
return (out, softmax_lse, *rest) if return_softmax_lse else out
|
|
|
|
| 797 |
max_seqlen_k_new=0,
|
| 798 |
causal=False,
|
| 799 |
window_size=(-1, -1), # -1 means infinite context window
|
| 800 |
+
attention_chunk=0,
|
| 801 |
has_softcap=False,
|
| 802 |
num_splits=0, # Can be tuned for speed
|
| 803 |
pack_gqa=None, # Can be tuned for speed
|
|
|
|
| 806 |
cache_seqlens = maybe_contiguous(cache_seqlens)
|
| 807 |
if headdim_v is None:
|
| 808 |
headdim_v = headdim
|
| 809 |
+
scheduler_metadata = flash_attn_3_cuda.get_scheduler_metadata(
|
| 810 |
batch_size, max_seqlen_q, max_seqlen_k, num_heads_q, num_heads_kv, headdim, headdim_v,
|
| 811 |
qkv_dtype,
|
| 812 |
cache_seqlens,
|
|
|
|
| 819 |
max_seqlen_k_new,
|
| 820 |
causal,
|
| 821 |
window_size[0], window_size[1],
|
| 822 |
+
attention_chunk,
|
| 823 |
has_softcap,
|
| 824 |
num_splits,
|
| 825 |
pack_gqa,
|