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

Solve official flash-attention.py fails: typeError:'function' object is not subscriptable #541

Open
wants to merge 1 commit into
base: triton-mlir
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
20 changes: 18 additions & 2 deletions python/perf-kernels/flash-attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,14 @@ def forward(ctx, q, k, v, o, metadata):
ENABLE_DROPOUT=metadata.dropout_p > 0.0,
RETURN_ENCODED_SOFTMAX=metadata.return_encoded_softmax
)
## restore the grid for bwd kernel
best_config = attn_fwd.get_best_config()
block_m = int(best_config.__str__().split(",")[0].split("BLOCK_M:")[1])
grid = (
triton.cdiv(metadata.max_seqlens_q, block_m),
nheads_q,
batch
)

ctx.save_for_backward(q, k, v, o, M)
ctx.grid = grid
Expand Down Expand Up @@ -1134,6 +1142,12 @@ def test_op_bwd(Z, H, N_CTX, D_HEAD, dtype=torch.float16):
v = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0., std=0.5).requires_grad_()

sm_scale = 0.5
input_metadata = MetaData(sm_scale=sm_scale)
input_metadata.max_seqlens_q = N_CTX
input_metadata.max_seqlens_k = N_CTX
if causal:
input_metadata.need_causal()

split_kernel = True
dout = torch.randn_like(q)
# reference implementation
Expand All @@ -1148,7 +1162,9 @@ def test_op_bwd(Z, H, N_CTX, D_HEAD, dtype=torch.float16):
ref_dk, k.grad = k.grad.clone(), None
ref_dq, q.grad = q.grad.clone(), None
# # triton implementation
tri_out, _ = attention(q, k, v, causal, None, sm_scale, 0, False, True)
o = torch.empty_like(q)
tri_out, _ = attention(q, k, v, o, input_metadata)

tri_out.backward(dout)#dout)
tri_dv, v.grad = v.grad.clone(), None
tri_dk, k.grad = k.grad.clone(), None
Expand Down Expand Up @@ -1334,4 +1350,4 @@ def main():
run_benchmark(custom_config)

if __name__ == '__main__':
sys.exit(main())
sys.exit(main())