import time import math import torch import torch.nn.functional as F def…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import time import math import torch import torch.nn.functional as F
def naive_online_causal_attn(q, k, v, block_size=128, causal=True): """ Block‑wise causal attention with streaming softmax.
Args: q: (B, Hq, T, D) — Query tensor k: (B, Hk, D, T) — Key tensor v: (B, Hk, D, T) — Value tensor block_size: int — size of each sequence block causal: bool — if True, masks out future keys
Returns: out: (B, Hq, T, D) — Attention output """ B, Hq, T, D = q.shape _, Hk, Dk, Tk = k.shape assert Dk == D, "Key/value head dim must match query head dim" assert T == Tk, "Sequence length of q and k/v must match" assert Hq % Hk == 0, "Number of q‑heads must be a multiple of k‑heads" div = Hq // Hk num_blocks = T // block_size scale = 1.0 / math.sqrt(D)
out = torch.zeros_like(q)
if causal: mask = torch.triu( torch.ones(block_size, block_size, device=q.device, dtype=torch.bool), diagonal=1 ).view(1, 1, 1, block_size, block_size)
for qb in range(num_blocks): q_blk = q[:, :, qb*block_size:(qb+1)*block_size, :].view(B, Hk, div, block_size, D) m_running = torch.full((B, Hk, div, block_size), -float('inf'), device=q.device) l_running = torch.zeros((B, Hk, div, block_size), device=q.device) S_running = torch.zeros((B, Hk, div, block_size, D), device=q.device)
for kb in range(num_blocks):
if causal and (kb > qb):
break
k_blk = k[:, :, :, kb*block_size:(kb+1)*block_size].transpose(-2, -1)
v_blk = v[:, :, :, kb*block_size:(kb+1)*block_size].transpose(-2, -1)
scores = torch.einsum('bhqld,bhkd->bhqlk', q_blk, k_blk) * scale
if causal and kb == qb:
scores = scores.masked_fill(mask, float('-inf'))
max_k = scores.max(dim=-1).values
prev_m = m_running
m_new = torch.maximum(prev_m, max_k)
exp_old = torch.exp(prev_m - m_new)
exp_new = torch.exp(scores - m_new.unsqueeze(-1))
l_running = l_running * exp_old + exp_new.sum(dim=-1)
weighted_new = torch.einsum('bhqlk,bhkd->bhqld', exp_new, v_blk)
S_running = S_running * exp_old.unsqueeze(-1) + weighted_new
m_running = m_new
out_block = S_running / l_running.unsqueeze(-1)
out[:, :, qb*block_size:(qb+1)*block_size, :] = out_block.view(B, Hq, block_size, D)
return out content_copy download Use code with caution. def benchmark_attention(device, configs, num_iters=10, causal=True): results = [] for cfg in configs: B, H, T, D, block_size = cfg
q = torch.randn(B, H, T, D, device=device) k = torch.randn(B, H, T, D, device=device) v = torch.randn(B, H, T, D, device=device) k_naive = k.transpose(2, 3) # (B, H, D, T) v_naive = v.transpose(2, 3)
_ = naive_online_causal_attn(q, k_naive, v_naive, block_size, causal)
_ = F.scaled_dot_product_attention(q, k, v, attn_mask=None,
dropout_p=0.0, is_causal=causal)
if device.type == 'cuda':
torch.cuda.synchronize()
# benchmark naive
torch.cuda.reset_peak_memory_stats(device)
start = time.perf_counter()
for _ in range(num_iters):
out1 = naive_online_causal_attn(q, k_naive, v_naive, block_size, causal)
if device.type == 'cuda':
torch.cuda.synchronize()
t1 = (time.perf_counter() - start) / num_iters
mem1 = torch.cuda.max_memory_allocated(device) / (1024**2) if device.type == 'cuda' else -1
# benchmark native
torch.cuda.reset_peak_memory_stats(device)
start = time.perf_counter()
for _ in range(num_iters):
out2 = F.scaled_dot_product_attention(q, k, v, attn_mask=None,
dropout_p=0.0, is_causal=causal)
if device.type == 'cuda':
torch.cuda.synchronize()
t2 = (time.perf_counter() - start) / num_iters
mem2 = torch.cuda.max_memory_allocated(device) / (1024**2) if device.type == 'cuda' else -1
results.append({
'B': B, 'H': H, 'T': T, 'D': D, 'blk': block_size,
'naive_time_s': t1, 'native_time_s': t2,
'naive_peak_mem_MB': mem1, 'native_peak_mem_MB': mem2
})
return results content_copy download Use code with caution. if name == 'main':
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(f'Running on {device}')
configs = [ (1, 4, 128, 384, 32), (2, 8, 256, 384, 64), (4, 16, 512, 768, 64), (4, 16, 512, 768, 128), (8, 32, 1024, 2048, 128), (1, 32, 4096, 4096, 128), ]
results = benchmark_attention(device, configs, num_iters=20, causal=True)
header = f"{'B':>2} {'H':>2} {'T':>4} {'D':>3} {'blk':>4} | {'naive (ms)':>10} {'native (ms)':>12} | {'naive MB':>8} {'native MB':>10}" print(header) print('-' * len(header)) for r in results: print(f"{r['B']:>2} {r['H']:>2} {r['T']:>4} {r['D']:>3} {r['blk']:>4} | " f"{r['naive_time_s']*1e3:10.2f} {r['native_time_s']*1e3:12.2f} | " f"{r['naive_peak_mem_MB']:8.1f} {r['native_peak_mem_MB']:10.1f}") content_copy download Use code with caution. : make this faster compared to F.scaled_dot_product_attention, do not suggest to use flash attention or existing attentions, this is a custom solution, just modify algorithmically etc, give full code modified then.
Absolutely! To make your custom block-wise streaming causal attention faster (compared to PyTorch's F.scaled_dot_product_attention), you need to:
einsum where possible (as it can be slower than explicit matmul for common patterns).Below is a fully optimized version of your function, with comments explaining the changes.
Key changes:
kb is vectorized: all key/value blocks up to the current query block are processed in one go.torch.matmul for attention score computation.import time
import math
import torch
import torch.nn.functional as F
def fast_online_causal_attn(q, k, v, block_size=128, causal=True):
"""
Fast block-wise causal attention with streaming softmax.
Args:
q: (B, Hq, T, D)
k: (B, Hk, D, T)
v: (B, Hk, D, T)
block_size: int
causal: bool
Returns:
out: (B, Hq, T, D)
"""
B, Hq, T, D = q.shape
_, Hk, Dk, Tk = k.shape
assert Dk == D and Tk == T
assert Hq % Hk == 0
div = Hq // Hk
num_blocks = T // block_size
scale = 1.0 / math.sqrt(D)
out = torch.empty_like(q)
# Precompute block mask (for causal masking within a block)
if causal:
block_mask = torch.triu(
torch.ones(block_size, block_size, device=q.device, dtype=torch.bool),
diagonal=1
) # (block_size, block_size)
# Precompute all key/value blocks (to avoid repeated slicing)
k_blocks = k.unbind(-1) # list of (B, Hk, D)
v_blocks = v.unbind(-1) # list of (B, Hk, D)
# For each query block
for qb in range(num_blocks):
q_start = qb * block_size
q_end = (qb + 1) * block_size
q_blk = q[:, :, q_start:q_end, :] # (B, Hq, block_size, D)
q_blk = q_blk.view(B, Hk, div, block_size, D) # (B, Hk, div, block_size, D)
# Gather all key/value blocks up to and including qb
k_blk = torch.stack([torch.stack(k_blocks[kb*block_size:(kb+1)*block_size], dim=-1)
for kb in range(qb+1)], dim=0) # (qb+1, B, Hk, D, block_size)
v_blk = torch.stack([torch.stack(v_blocks[kb*block_size:(kb+1)*block_size], dim=-1)
for kb in range(qb+1)], dim=0) # (qb+1, B, Hk, D, block_size)
# Merge all past blocks into a single key/value matrix
k_blk = k_blk.permute(1,2,0,4,3).reshape(B, Hk, (qb+1)*block_size, D) # (B, Hk, K, D)
v_blk = v_blk.permute(1,2,0,4,3).reshape(B, Hk, (qb+1)*block_size, D) # (B, Hk, K, D)
# Expand for query heads
k_blk = k_blk.unsqueeze(2).expand(B, Hk, div, (qb+1)*block_size, D)
v_blk = v_blk.unsqueeze(2).expand(B, Hk, div, (qb+1)*block_size, D)
q_blk_ = q_blk # (B, Hk, div, block_size, D)
# Compute attention scores: (B, Hk, div, block_size, K)
attn_scores = torch.matmul(q_blk_, k_blk.transpose(-1, -2)) * scale # (B, Hk, div, block_size, K)
# Causal mask: block diagonal for current block, full mask for previous blocks
if causal:
# Mask out future positions in the last block
K = (qb+1)*block_size
mask = torch.zeros((block_size, K), device=q.device, dtype=torch.bool)
mask[:, :K-block_size] = False # previous blocks: all allowed
mask[:, K-block_size:] = block_mask # current block: causal mask
attn_scores = attn_scores.masked_fill(mask.view(1,1,1,block_size,K), float('-inf'))
# Softmax in a numerically stable way
attn_probs = torch.softmax(attn_scores, dim=-1) # (B, Hk, div, block_size, K)
# Weighted sum
out_blk = torch.matmul(attn_probs, v_blk) # (B, Hk, div, block_size, D)
out_blk = out_blk.permute(0,1,3,2,4).reshape(B, Hq, block_size, D)
out[:, :, q_start:q_end, :] = out_blk
return out
# The rest of your benchmarking code remains unchanged, just replace the function call:
# out1 = fast_online_causal_attn(q, k_naive, v_naive, block_size, causal)
einsum: Uses matmul for both score and value aggregation.Replace all calls to naive_online_causal_attn with fast_online_causal_attn in your benchmarking code.
T is divisible by block_size.T, you may want to further optimize memory by processing in-place or using lower precision.This should be significantly faster than your original implementation, and in many cases can approach or even beat PyTorch's native attention for moderate sequence lengths.
Let me know if you want further tuning for specific hardware or batch sizes!