Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
24 changes: 6 additions & 18 deletions benchmarks/ops/benchmark_kda.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import os

import torch
import triton
Expand Down Expand Up @@ -38,21 +37,11 @@ def benchmark(T, provider):
dtype = torch.bfloat16
B, H, D = 1, 16, 128

# Set TMA environment variable based on provider
original_tma_env = os.environ.get('FLA_USE_TMA', '0')

if provider.endswith('_no_tma'):
os.environ['FLA_USE_TMA'] = '0'
provider_base = provider.replace('_no_tma', '')
else:
os.environ['FLA_USE_TMA'] = '1'
provider_base = provider

quantiles = [0.5, 0.2, 0.8]
results = 0, 0, 0

do = torch.randn(B, T, H, D, dtype=dtype, device=device)
if provider_base == 'gdn':
if provider == 'gdn':
q = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
k = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
v = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
Expand All @@ -69,7 +58,7 @@ def benchmark(T, provider):
)[0].backward(do),
quantiles=quantiles,
)
elif provider_base == 'attn':
elif provider == 'attn':
q = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
k = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
v = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
Expand All @@ -81,7 +70,7 @@ def benchmark(T, provider):
).backward(do),
quantiles=quantiles,
)
elif provider_base == 'comba':
elif provider == 'comba':
q = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
k = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
p = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
Expand All @@ -100,7 +89,7 @@ def benchmark(T, provider):
)[0].backward(do),
quantiles=quantiles,
)
elif provider_base == 'kda':
elif provider == 'kda':
q = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
k = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
v = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
Expand All @@ -114,10 +103,11 @@ def benchmark(T, provider):
g=g,
beta=beta,
use_qk_l2norm_in_kernel=True,
safe_gate=True, # Plese Carefully read doc strings
Comment thread
zhiyuan1i marked this conversation as resolved.
)[0].backward(do),
quantiles=quantiles,
)
elif provider_base == 'dplr':
elif provider == 'dplr':
q = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
k = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
a = torch.randn(B, T, H, D, dtype=dtype, device=device).requires_grad_(True)
Expand All @@ -137,8 +127,6 @@ def benchmark(T, provider):
quantiles=quantiles,
)

# Restore original TMA environment variable
os.environ['FLA_USE_TMA'] = original_tma_env
return results


Expand Down
20 changes: 10 additions & 10 deletions fla/ops/common/chunk_delta_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def chunk_gated_delta_rule_fwd_kernel_h_blockdim64(
m_t = (i_t * BT + tl.arange(0, BT)) < T
b_g_last = tl.load(g + bos * H + last_idx * H + i_h)
p_g = tl.make_block_ptr(g + bos * H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
b_g = tl.load(p_g, boundary_check=(0,))
b_g = tl.load(p_g, boundary_check=(0,)).to(tl.float32)
if USE_EXP2:
b_v = b_v * tl.where(m_t, exp2(b_g_last - b_g), 0)[:, None]
b_g_last = exp2(b_g_last)
Expand All @@ -163,28 +163,28 @@ def chunk_gated_delta_rule_fwd_kernel_h_blockdim64(

if USE_GK:
o_k1 = tl.arange(0, 64)
b_gk_last1 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k1, mask=(o_k1 < K), other=0.)
b_gk_last1 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k1, mask=(o_k1 < K), other=0.).to(tl.float32)
if USE_EXP2:
b_h1 *= exp2(b_gk_last1)[:, None]
else:
b_h1 *= exp(b_gk_last1)[:, None]
if K > 64:
o_k2 = 64 + o_k1
b_gk_last2 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k2, mask=(o_k2 < K), other=0.)
b_gk_last2 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k2, mask=(o_k2 < K), other=0.).to(tl.float32)
if USE_EXP2:
b_h2 *= exp2(b_gk_last2)[:, None]
else:
b_h2 *= exp(b_gk_last2)[:, None]
if K > 128:
o_k3 = 128 + o_k1
b_gk_last3 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k3, mask=(o_k3 < K), other=0.)
b_gk_last3 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k3, mask=(o_k3 < K), other=0.).to(tl.float32)
if USE_EXP2:
b_h3 *= exp2(b_gk_last3)[:, None]
else:
b_h3 *= exp(b_gk_last3)[:, None]
if K > 192:
o_k4 = 192 + o_k1
b_gk_last4 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k4, mask=(o_k4 < K), other=0.)
b_gk_last4 = tl.load(gk + (bos + last_idx) * H*K + i_h * K + o_k4, mask=(o_k4 < K), other=0.).to(tl.float32)
if USE_EXP2:
b_h4 *= exp2(b_gk_last4)[:, None]
else:
Expand Down Expand Up @@ -335,7 +335,7 @@ def chunk_gated_delta_rule_bwd_kernel_dhu_blockdim64(
if USE_G:
bg_last = tl.load(g + (bos + last_idx) * H + i_h)
p_g = tl.make_block_ptr(g + bos * H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
b_g = tl.load(p_g, boundary_check=(0,))
b_g = tl.load(p_g, boundary_check=(0,)).to(tl.float32)
if USE_EXP2:
bg_last_exp = exp2(bg_last)
b_g_exp = exp2(b_g)
Expand All @@ -354,31 +354,31 @@ def chunk_gated_delta_rule_bwd_kernel_dhu_blockdim64(
b_k = tl.load(p_k, boundary_check=(0, 1))
if USE_GK:
o_k1 = tl.arange(0, 64)
b_gk_last1 = tl.load(gk + last_idx * H*K + o_k1, mask=(o_k1 < K), other=0.)
b_gk_last1 = tl.load(gk + last_idx * H*K + o_k1, mask=(o_k1 < K), other=0.).to(tl.float32)
b_dv = tl.dot(b_k, b_dh1.to(b_k.dtype))

if K > 64:
p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 64), (BT, 64), (1, 0))
b_k = tl.load(p_k, boundary_check=(0, 1))
if USE_GK:
o_k2 = 64 + o_k1
b_gk_last2 = tl.load(gk + last_idx * H*K + o_k2, mask=(o_k2 < K), other=0.)
b_gk_last2 = tl.load(gk + last_idx * H*K + o_k2, mask=(o_k2 < K), other=0.).to(tl.float32)
b_dv += tl.dot(b_k, b_dh2.to(b_k.dtype))

if K > 128:
p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 128), (BT, 64), (1, 0))
b_k = tl.load(p_k, boundary_check=(0, 1))
if USE_GK:
o_k3 = 128 + o_k1
b_gk_last3 = tl.load(gk + last_idx * H*K + o_k3, mask=(o_k3 < K), other=0.)
b_gk_last3 = tl.load(gk + last_idx * H*K + o_k3, mask=(o_k3 < K), other=0.).to(tl.float32)
b_dv += tl.dot(b_k, b_dh3.to(b_k.dtype))

if K > 192:
p_k = tl.make_block_ptr(k, (T, K), (H*K, 1), (i_t * BT, 192), (BT, 64), (1, 0))
b_k = tl.load(p_k, boundary_check=(0, 1))
if USE_GK:
o_k4 = 192 + o_k1
b_gk_last4 = tl.load(gk + last_idx * H*K + o_k4, mask=(o_k4 < K), other=0.)
b_gk_last4 = tl.load(gk + last_idx * H*K + o_k4, mask=(o_k4 < K), other=0.).to(tl.float32)
b_dv += tl.dot(b_k, b_dh4.to(b_k.dtype))

if USE_G:
Expand Down
2 changes: 1 addition & 1 deletion fla/ops/gla/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def chunk_gla_fwd_kernel_o(
# [BT, BK]
b_q = tl.load(p_q, boundary_check=(0, 1))
# [BT, BK]
b_g = tl.load(p_g, boundary_check=(0, 1))
b_g = tl.load(p_g, boundary_check=(0, 1)).to(tl.float32)
# [BT, BK]
if USE_EXP2:
b_qg = (b_q * exp2(b_g)).to(b_q.dtype)
Expand Down
Loading