Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update simple_flash_attn_vit_3d.py #323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions vit_pytorch/simple_flash_attn_vit_3d.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from packaging import version
from collections import namedtuple

import torch
from torch import nn
import torch.nn.functional as F
from torch.nn import Module, ModuleList

from einops import rearrange
from einops import einsum, rearrange
from einops.layers.torch import Rearrange

# constants

Config = namedtuple('FlashAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient'])
Config = [
torch.nn.attention.SDPBackend.FLASH_ATTENTION,
torch.nn.attention.SDPBackend.MATH,
torch.nn.attention.SDPBackend.EFFICIENT_ATTENTION,
] # Flash Attention Config

# helpers

Expand Down Expand Up @@ -44,7 +47,7 @@ def posemb_sincos_3d(patches, temperature = 10000, dtype = torch.float32):
# main class

class Attend(Module):
def __init__(self, use_flash = False, config: Config = Config(True, True, True)):
def __init__(self, use_flash = False, config: list = Config):
super().__init__()
self.config = config
self.use_flash = use_flash
Expand All @@ -53,7 +56,7 @@ def __init__(self, use_flash = False, config: Config = Config(True, True, True))
def flash_attn(self, q, k, v):
# flash attention - https://arxiv.org/abs/2205.14135

with torch.backends.cuda.sdp_kernel(**self.config._asdict()):
with torch.nn.attention.sdpa_kernel(self.config):
out = F.scaled_dot_product_attention(q, k, v)

return out
Expand Down