Code view:

A painting is a person's account of this edit as text, recorded independently of the node mapping - a rendering often has several equally defensible answers where the mapping has one. Deletions and insertions carry no position on the opposite side in a painting, so that panel draws no caret for them.

Before

…lines 1–256 unchanged (256 lines)
257 window_size_right: int | None,
258 seqused_k: torch.Tensor | None,
259 out: torch.Tensor | None = None,
260 q_descale: torch.Tensor | None = None,
261 k_descale: torch.Tensor | None = None,
262 v_descale: torch.Tensor | None = None,
263) -> tuple[torch.Tensor, torch.Tensor]:
264 """
265 Run the FA3 forward pass by calling the C++ kernel directly.
266 """
267 if _FA3_CUDA_FWD is None:
268 raise RuntimeError("FA3 not registered")
269 # Ensure contiguous in the last dimension
…lines 270–276 unchanged (7 lines)
277 else _maybe_contiguous(value)
278 )
279
280 cu_seqlens_q = _maybe_contiguous(cu_seq_q)
281 cu_seqlens_k = _maybe_contiguous(cu_seq_k)
282 seqused_k = _maybe_contiguous(seqused_k)
283
284 out, softmax_lse, out_accum, softmax_lse_accum = _FA3_CUDA_FWD(
285 q,
286 k,
287 v,
288 None, # k_new
289 None, # v_new
290 None, # qv
291 out, # out_ (pre-allocated output)
292 cu_seqlens_q, # cu_seqlens_q
293 cu_seqlens_k, # cu_seqlens_k
294 None, # cu_seqlens_k_new
295 None, # seqused_q
296 seqused_k, # seqused_k
297 max_q, # max_seqlen_q
298 max_k, # max_seqlen_k
299 None, # page_table,
300 None, # kv_batch_idx,
301 None, # leftpad_k,
302 None, # rotary_cos,
303 None, # rotary_sin,
304 None, # seqlens_rotary,
305 q_descale, # q_descale,
…lines 306–396 unchanged (91 lines)
397 scale: float | None = None,
398 window_size_left: int = -1,
399 window_size_right: int = -1,
400 seqused_k: torch.Tensor | None = None,
401 alibi_slopes: torch.Tensor | None = None,
402 out: torch.Tensor | None = None,
403):
404 error = _fa3_forward_support_error(
405 query,
406 key,
407 value,
408 dropout_p,
409 return_debug_mask,
…lines 410–429 unchanged (20 lines)
430 window_size_right,
431 seqused_k,
432 out,
433 q_descale,
434 k_descale,
435 v_descale,
436 )
437 rng_state = torch.zeros((2,), dtype=torch.uint64, device=query.device)
438 philox_offset = torch.zeros((), dtype=torch.uint64, device=query.device)
439 debug_mask = torch.empty(0, dtype=query.dtype, device=query.device)
440 return out, lse, rng_state, philox_offset, debug_mask
441
442
…lines 443–453 unchanged (11 lines)
454 *,
455 scale: float | None = None,
456 window_size_left: int = -1,
457 window_size_right: int = -1,
458 seqused_k: torch.Tensor | None = None,
459 alibi_slopes: torch.Tensor | None = None,
460 out: torch.Tensor | None = None,
461):
462 return _fa3_flash_attention_forward_impl(
463 query,
464 key,
465 value,
466 cum_seq_q,
…lines 467–475 unchanged (9 lines)
476 scale=scale,
477 window_size_left=window_size_left,
478 window_size_right=window_size_right,
479 seqused_k=seqused_k,
480 alibi_slopes=alibi_slopes,
481 out=out,
482 )
483
484
485def _fa3_flash_attention_backward_impl(
486 grad_out: torch.Tensor,
487 query: torch.Tensor,
488 key: torch.Tensor,
…lines 489–693 unchanged (205 lines)

After

…lines 1–256 unchanged (256 lines)
257 window_size_right: int | None,
258 seqused_k: torch.Tensor | None,
259 out: torch.Tensor | None = None,
260 q_descale: torch.Tensor | None = None,
261 k_descale: torch.Tensor | None = None,
262 v_descale: torch.Tensor | None = None,
263 block_table: torch.Tensor | None = None,
264) -> tuple[torch.Tensor, torch.Tensor]:
265 """
266 Run the FA3 forward pass by calling the C++ kernel directly.
267 """
268 if _FA3_CUDA_FWD is None:
269 raise RuntimeError("FA3 not registered")
…lines 270–277 unchanged (8 lines)
278 else _maybe_contiguous(value)
279 )
280
281 cu_seqlens_q = _maybe_contiguous(cu_seq_q)
282 cu_seqlens_k = _maybe_contiguous(cu_seq_k)
283 seqused_k = _maybe_contiguous(seqused_k)
284 block_table = _maybe_contiguous(block_table)
285
286 out, softmax_lse, out_accum, softmax_lse_accum = _FA3_CUDA_FWD(
287 q,
288 k,
289 v,
290 None, # k_new
…lines 291–294 unchanged (4 lines)
295 cu_seqlens_k, # cu_seqlens_k
296 None, # cu_seqlens_k_new
297 None, # seqused_q
298 seqused_k, # seqused_k
299 max_q, # max_seqlen_q
300 max_k, # max_seqlen_k
301 block_table, # block_table,
302 None, # kv_batch_idx,
303 None, # leftpad_k,
304 None, # rotary_cos,
305 None, # rotary_sin,
306 None, # seqlens_rotary,
307 q_descale, # q_descale,
…lines 308–398 unchanged (91 lines)
399 scale: float | None = None,
400 window_size_left: int = -1,
401 window_size_right: int = -1,
402 seqused_k: torch.Tensor | None = None,
403 alibi_slopes: torch.Tensor | None = None,
404 out: torch.Tensor | None = None,
405 block_table: torch.Tensor | None = None,
406):
407 error = _fa3_forward_support_error(
408 query,
409 key,
410 value,
411 dropout_p,
…lines 412–432 unchanged (21 lines)
433 window_size_right,
434 seqused_k,
435 out,
436 q_descale,
437 k_descale,
438 v_descale,
439 block_table,
440 )
441 rng_state = torch.zeros((2,), dtype=torch.uint64, device=query.device)
442 philox_offset = torch.zeros((), dtype=torch.uint64, device=query.device)
443 debug_mask = torch.empty(0, dtype=query.dtype, device=query.device)
444 return out, lse, rng_state, philox_offset, debug_mask
445
…lines 446–457 unchanged (12 lines)
458 *,
459 scale: float | None = None,
460 window_size_left: int = -1,
461 window_size_right: int = -1,
462 seqused_k: torch.Tensor | None = None,
463 alibi_slopes: torch.Tensor | None = None,
464 block_table: torch.Tensor | None = None,
465 out: torch.Tensor | None = None,
466):
467 return _fa3_flash_attention_forward_impl(
468 query,
469 key,
470 value,
…lines 471–480 unchanged (10 lines)
481 scale=scale,
482 window_size_left=window_size_left,
483 window_size_right=window_size_right,
484 seqused_k=seqused_k,
485 alibi_slopes=alibi_slopes,
486 out=out,
487 block_table=block_table,
488 )
489
490
491def _fa3_flash_attention_backward_impl(
492 grad_out: torch.Tensor,
493 query: torch.Tensor,
…lines 494–699 unchanged (206 lines)

Before

module
expression_statement
string
string_start "\"\"\""
string_content "\nPROTOTYPE!\nFlash Attention 3 implementation.\nFor fp8: only "...
string_end "\"\"\""
comment "# mypy: allow-untyped-defs"
future_import_statement
from "from"
__future__ "__future__"
import "import"
dotted_name
identifier "annotations"
import_statement
import "import"
dotted_name
identifier "importlib"
import_statement
import "import"
dotted_name
identifier "warnings"
import_from_statement
from "from"
dotted_name
identifier "typing"
import "import"
dotted_name
identifier "TYPE_CHECKING"
if_statement
if "if"
identifier "TYPE_CHECKING"
: ":"
block
import_from_statement
from "from"
dotted_name
identifier "collections"
. "."
identifier "abc"
import "import"
dotted_name
identifier "Callable"
import_from_statement
from "from"
dotted_name
identifier "dataclasses"
import "import"
dotted_name
identifier "dataclass"
import_from_statement
from "from"
dotted_name
identifier "functools"
import "import"
dotted_name
identifier "cache"
import_from_statement
from "from"
dotted_name
identifier "typing_extensions"
import "import"
dotted_name
identifier "TypeVarTuple"
, ","
dotted_name
identifier "Unpack"
import_statement
import "import"
dotted_name
identifier "torch"
import_from_statement
from "from"
dotted_name
identifier "torch"
. "."
identifier "library"
import "import"
dotted_name
identifier "Library"
import_from_statement
from "from"
relative_import
import_prefix
. "."
import "import"
dotted_name
identifier "_registry"
expression_statement
assignment
identifier "__all__"
= "="
list
[ "["
string
string_start "\""
string_content "register_flash_attention_fa3"
string_end "\""
, ","
] "]"
expression_statement
assignment
identifier "_FA3_CUDA_FWD"
: ":"
type
binary_operator
identifier "Callable"
| "|"
none "None"
= "="
none "None"
comment "# Cache for torch.ops.flash_attn_3.fwd"
expression_statement
assignment
identifier "_FA3_CUDA_BWD"
: ":"
type
binary_operator
identifier "Callable"
| "|"
none "None"
= "="
none "None"
comment "# Cache for torch.ops.flash_attn_3.bwd"
decorated_definition (+52 nodes collapsed)
decorated_definition (+45 nodes collapsed)
function_definition (+60 nodes collapsed)
function_definition (+173 nodes collapsed)
function_definition (+161 nodes collapsed)
function_definition (+333 nodes collapsed)
function_definition (+359 nodes collapsed)
function_definition (+301 nodes collapsed)
expression_statement
assignment
identifier "Ts"
= "="
call
identifier "TypeVarTuple"
argument_list
( "("
string
string_start "\""
string_content "Ts"
string_end "\""
) ")"
function_definition (+60 nodes collapsed)
function_definition (+69 nodes collapsed)
function_definition
def "def"
identifier "_fa3_run_forward"
parameters
( "("
typed_parameter
identifier "query"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "key"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "value"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "cu_seq_q"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "cu_seq_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "max_q"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "max_k"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "scale"
: ":"
type
binary_operator
identifier "float"
| "|"
none "None"
, ","
typed_parameter
identifier "is_causal"
: ":"
type
identifier "bool"
, ","
typed_parameter
identifier "window_size_left"
: ":"
type
binary_operator
identifier "int"
| "|"
none "None"
, ","
typed_parameter
identifier "window_size_right"
: ":"
type
binary_operator
identifier "int"
| "|"
none "None"
, ","
typed_parameter
identifier "seqused_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_default_parameter
identifier "out"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "q_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "k_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "v_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
) ")"
-> "->"
type
generic_type
identifier "tuple"
type_parameter
[ "["
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
type
attribute
identifier "torch"
. "."
identifier "Tensor"
] "]"
: ":"
block
expression_statement
string
string_start "\"\"\""
string_content "\n Run the FA3 forward pass by calling the C++ kernel dire"...
string_end "\"\"\""
if_statement
if "if"
comparison_operator
identifier "_FA3_CUDA_FWD"
is "is"
none "None"
: ":"
block
raise_statement
raise "raise"
call
identifier "RuntimeError"
argument_list
( "("
string
string_start "\""
string_content "FA3 not registered"
string_end "\""
) ")"
comment "# Ensure contiguous in the last dimension"
expression_statement
assignment
identifier "q"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "query"
) ")"
expression_statement
assignment
identifier "k"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "key"
) ")"
expression_statement (+66 nodes collapsed)
expression_statement
assignment
identifier "cu_seqlens_q"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "cu_seq_q"
) ")"
expression_statement
assignment
identifier "cu_seqlens_k"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "cu_seq_k"
) ")"
expression_statement
assignment
identifier "seqused_k"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "seqused_k"
) ")"
expression_statement
assignment
pattern_list
identifier "out"
, ","
identifier "softmax_lse"
, ","
identifier "out_accum"
, ","
identifier "softmax_lse_accum"
= "="
call
identifier "_FA3_CUDA_FWD"
argument_list
( "("
identifier "q"
, ","
identifier "k"
, ","
identifier "v"
, ","
none "None"
, ","
comment "# k_new"
none "None"
, ","
comment "# v_new"
none "None"
, ","
comment "# qv"
identifier "out"
, ","
comment "# out_ (pre-allocated output)"
identifier "cu_seqlens_q"
, ","
comment "# cu_seqlens_q"
identifier "cu_seqlens_k"
, ","
comment "# cu_seqlens_k"
none "None"
, ","
comment "# cu_seqlens_k_new"
none "None"
, ","
comment "# seqused_q"
identifier "seqused_k"
, ","
comment "# seqused_k"
identifier "max_q"
, ","
comment "# max_seqlen_q"
identifier "max_k"
, ","
comment "# max_seqlen_k"
none "None"
, ","
comment "# page_table,"
none "None"
, ","
comment "# kv_batch_idx,"
none "None"
, ","
comment "# leftpad_k,"
none "None"
, ","
comment "# rotary_cos,"
none "None"
, ","
comment "# rotary_sin,"
none "None"
, ","
comment "# seqlens_rotary,"
identifier "q_descale"
, ","
comment "# q_descale,"
identifier "k_descale"
, ","
comment "# k_descale,"
identifier "v_descale"
, ","
comment "# v_descale,"
identifier "scale"
, ","
comment "# softmax_scale,"
identifier "is_causal"
, ","
comment "# causal,"
conditional_expression
identifier "window_size_left"
if "if"
comparison_operator
identifier "window_size_left"
is not
is "is"
not "not"
none "None"
else "else"
unary_operator
- "-"
integer "1"
, ","
comment "# window_size_left"
conditional_expression
identifier "window_size_right"
if "if"
comparison_operator
identifier "window_size_right"
is not
is "is"
not "not"
none "None"
else "else"
unary_operator
- "-"
integer "1"
, ","
comment "# window_size_right"
integer "0"
, ","
comment "# attention_chunk,"
float "0.0"
, ","
comment "# softcap,"
true "True"
, ","
comment "# rotary_interleaved,"
none "None"
, ","
comment "# scheduler_metadata,"
conditional_expression
integer "1"
if "if"
call
attribute
identifier "torch"
. "."
identifier "are_deterministic_algorithms_enabled"
argument_list
( "("
) ")"
else "else"
integer "0"
, ","
comment "# num_splits,"
none "None"
, ","
comment "# pack_gqa,"
boolean_operator
call
attribute
attribute
identifier "torch"
. "."
identifier "_C"
. "."
identifier "_get_sm_carveout_experimental"
argument_list
( "("
) ")"
or "or"
integer "0"
, ","
comment "# sm_margin,"
) ")"
return_statement
return "return"
expression_list
identifier "out"
, ","
call
attribute
identifier "softmax_lse"
. "."
identifier "contiguous"
argument_list
( "("
) ")"
function_definition (+414 nodes collapsed)
function_definition
def "def"
identifier "_fa3_flash_attention_forward_impl"
parameters
( "("
typed_parameter
identifier "query"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "key"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "value"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "cum_seq_q"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "cum_seq_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "max_q"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "max_k"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "dropout_p"
: ":"
type
identifier "float"
, ","
typed_parameter
identifier "is_causal"
: ":"
type
identifier "bool"
, ","
typed_parameter
identifier "return_debug_mask"
: ":"
type
identifier "bool"
, ","
typed_default_parameter
identifier "q_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "k_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "v_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
keyword_separator
* "*"
, ","
typed_default_parameter
identifier "scale"
: ":"
type
binary_operator
identifier "float"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "window_size_left"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "window_size_right"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "seqused_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "alibi_slopes"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "out"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
) ")"
: ":"
block
expression_statement (+31 nodes collapsed)
if_statement (+25 nodes collapsed)
expression_statement
assignment
pattern_list
identifier "out"
, ","
identifier "lse"
= "="
call
identifier "_fa3_run_forward"
argument_list
( "("
identifier "query"
, ","
identifier "key"
, ","
identifier "value"
, ","
identifier "cum_seq_q"
, ","
identifier "cum_seq_k"
, ","
identifier "max_q"
, ","
identifier "max_k"
, ","
identifier "scale"
, ","
identifier "is_causal"
, ","
identifier "window_size_left"
, ","
identifier "window_size_right"
, ","
identifier "seqused_k"
, ","
identifier "out"
, ","
identifier "q_descale"
, ","
identifier "k_descale"
, ","
identifier "v_descale"
, ","
) ")"
expression_statement (+33 nodes collapsed)
expression_statement (+31 nodes collapsed)
expression_statement (+29 nodes collapsed)
return_statement
return "return"
expression_list
identifier "out"
, ","
identifier "lse"
, ","
identifier "rng_state"
, ","
identifier "philox_offset"
, ","
identifier "debug_mask"
function_definition
def "def"
identifier "_fa3_flash_attention_forward_impl_default"
parameters
( "("
typed_parameter
identifier "query"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "key"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "value"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "cum_seq_q"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "cum_seq_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "max_q"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "max_k"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "dropout_p"
: ":"
type
identifier "float"
, ","
typed_parameter
identifier "is_causal"
: ":"
type
identifier "bool"
, ","
typed_parameter
identifier "return_debug_mask"
: ":"
type
identifier "bool"
, ","
keyword_separator
* "*"
, ","
typed_default_parameter
identifier "scale"
: ":"
type
binary_operator
identifier "float"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "window_size_left"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "window_size_right"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "seqused_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "alibi_slopes"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "out"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
) ")"
: ":"
block
return_statement
return "return"
call
identifier "_fa3_flash_attention_forward_impl"
argument_list
( "("
identifier "query"
, ","
identifier "key"
, ","
identifier "value"
, ","
identifier "cum_seq_q"
, ","
identifier "cum_seq_k"
, ","
identifier "max_q"
, ","
identifier "max_k"
, ","
identifier "dropout_p"
, ","
identifier "is_causal"
, ","
identifier "return_debug_mask"
, ","
none "None"
, ","
none "None"
, ","
none "None"
, ","
keyword_argument
identifier "scale"
= "="
identifier "scale"
, ","
keyword_argument
identifier "window_size_left"
= "="
identifier "window_size_left"
, ","
keyword_argument
identifier "window_size_right"
= "="
identifier "window_size_right"
, ","
keyword_argument
identifier "seqused_k"
= "="
identifier "seqused_k"
, ","
keyword_argument
identifier "alibi_slopes"
= "="
identifier "alibi_slopes"
, ","
keyword_argument
identifier "out"
= "="
identifier "out"
, ","
) ")"
function_definition (+311 nodes collapsed)
function_definition (+389 nodes collapsed)
function_definition (+103 nodes collapsed)
function_definition (+307 nodes collapsed)
expression_statement
call
attribute
identifier "_registry"
. "."
identifier "register_flash_attention_impl"
argument_list
( "("
string
string_start "\""
string_content "FA3"
string_end "\""
, ","
keyword_argument
identifier "register_fn"
= "="
identifier "register_flash_attention_fa3"
) ")"

After

module
expression_statement
string
string_start "\"\"\""
string_content "\nPROTOTYPE!\nFlash Attention 3 implementation.\nFor fp8: only "...
string_end "\"\"\""
comment "# mypy: allow-untyped-defs"
future_import_statement
from "from"
__future__ "__future__"
import "import"
dotted_name
identifier "annotations"
import_statement
import "import"
dotted_name
identifier "importlib"
import_statement
import "import"
dotted_name
identifier "warnings"
import_from_statement
from "from"
dotted_name
identifier "typing"
import "import"
dotted_name
identifier "TYPE_CHECKING"
if_statement
if "if"
identifier "TYPE_CHECKING"
: ":"
block
import_from_statement
from "from"
dotted_name
identifier "collections"
. "."
identifier "abc"
import "import"
dotted_name
identifier "Callable"
import_from_statement
from "from"
dotted_name
identifier "dataclasses"
import "import"
dotted_name
identifier "dataclass"
import_from_statement
from "from"
dotted_name
identifier "functools"
import "import"
dotted_name
identifier "cache"
import_from_statement
from "from"
dotted_name
identifier "typing_extensions"
import "import"
dotted_name
identifier "TypeVarTuple"
, ","
dotted_name
identifier "Unpack"
import_statement
import "import"
dotted_name
identifier "torch"
import_from_statement
from "from"
dotted_name
identifier "torch"
. "."
identifier "library"
import "import"
dotted_name
identifier "Library"
import_from_statement
from "from"
relative_import
import_prefix
. "."
import "import"
dotted_name
identifier "_registry"
expression_statement
assignment
identifier "__all__"
= "="
list
[ "["
string
string_start "\""
string_content "register_flash_attention_fa3"
string_end "\""
, ","
] "]"
expression_statement
assignment
identifier "_FA3_CUDA_FWD"
: ":"
type
binary_operator
identifier "Callable"
| "|"
none "None"
= "="
none "None"
comment "# Cache for torch.ops.flash_attn_3.fwd"
expression_statement
assignment
identifier "_FA3_CUDA_BWD"
: ":"
type
binary_operator
identifier "Callable"
| "|"
none "None"
= "="
none "None"
comment "# Cache for torch.ops.flash_attn_3.bwd"
decorated_definition (+52 nodes collapsed)
decorated_definition (+45 nodes collapsed)
function_definition (+60 nodes collapsed)
function_definition (+173 nodes collapsed)
function_definition (+161 nodes collapsed)
function_definition (+333 nodes collapsed)
function_definition (+359 nodes collapsed)
function_definition (+301 nodes collapsed)
expression_statement
assignment
identifier "Ts"
= "="
call
identifier "TypeVarTuple"
argument_list
( "("
string
string_start "\""
string_content "Ts"
string_end "\""
) ")"
function_definition (+60 nodes collapsed)
function_definition (+69 nodes collapsed)
function_definition
def "def"
identifier "_fa3_run_forward"
parameters
( "("
typed_parameter
identifier "query"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "key"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "value"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "cu_seq_q"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "cu_seq_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "max_q"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "max_k"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "scale"
: ":"
type
binary_operator
identifier "float"
| "|"
none "None"
, ","
typed_parameter
identifier "is_causal"
: ":"
type
identifier "bool"
, ","
typed_parameter
identifier "window_size_left"
: ":"
type
binary_operator
identifier "int"
| "|"
none "None"
, ","
typed_parameter
identifier "window_size_right"
: ":"
type
binary_operator
identifier "int"
| "|"
none "None"
, ","
typed_parameter
identifier "seqused_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_default_parameter
identifier "out"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "q_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "k_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "v_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "block_table"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
) ")"
-> "->"
type
generic_type
identifier "tuple"
type_parameter
[ "["
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
type
attribute
identifier "torch"
. "."
identifier "Tensor"
] "]"
: ":"
block
expression_statement
string
string_start "\"\"\""
string_content "\n Run the FA3 forward pass by calling the C++ kernel dire"...
string_end "\"\"\""
if_statement
if "if"
comparison_operator
identifier "_FA3_CUDA_FWD"
is "is"
none "None"
: ":"
block
raise_statement
raise "raise"
call
identifier "RuntimeError"
argument_list
( "("
string
string_start "\""
string_content "FA3 not registered"
string_end "\""
) ")"
comment "# Ensure contiguous in the last dimension"
expression_statement
assignment
identifier "q"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "query"
) ")"
expression_statement
assignment
identifier "k"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "key"
) ")"
expression_statement (+66 nodes collapsed)
expression_statement
assignment
identifier "cu_seqlens_q"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "cu_seq_q"
) ")"
expression_statement
assignment
identifier "cu_seqlens_k"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "cu_seq_k"
) ")"
expression_statement
assignment
identifier "seqused_k"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "seqused_k"
) ")"
expression_statement
assignment
identifier "block_table"
= "="
call
identifier "_maybe_contiguous"
argument_list
( "("
identifier "block_table"
) ")"
expression_statement
assignment
pattern_list
identifier "out"
, ","
identifier "softmax_lse"
, ","
identifier "out_accum"
, ","
identifier "softmax_lse_accum"
= "="
call
identifier "_FA3_CUDA_FWD"
argument_list
( "("
identifier "q"
, ","
identifier "k"
, ","
identifier "v"
, ","
none "None"
, ","
comment "# k_new"
none "None"
, ","
comment "# v_new"
none "None"
, ","
comment "# qv"
identifier "out"
, ","
comment "# out_ (pre-allocated output)"
identifier "cu_seqlens_q"
, ","
comment "# cu_seqlens_q"
identifier "cu_seqlens_k"
, ","
comment "# cu_seqlens_k"
none "None"
, ","
comment "# cu_seqlens_k_new"
none "None"
, ","
comment "# seqused_q"
identifier "seqused_k"
, ","
comment "# seqused_k"
identifier "max_q"
, ","
comment "# max_seqlen_q"
identifier "max_k"
, ","
comment "# max_seqlen_k"
identifier "block_table"
, ","
comment "# block_table,"
none "None"
, ","
comment "# kv_batch_idx,"
none "None"
, ","
comment "# leftpad_k,"
none "None"
, ","
comment "# rotary_cos,"
none "None"
, ","
comment "# rotary_sin,"
none "None"
, ","
comment "# seqlens_rotary,"
identifier "q_descale"
, ","
comment "# q_descale,"
identifier "k_descale"
, ","
comment "# k_descale,"
identifier "v_descale"
, ","
comment "# v_descale,"
identifier "scale"
, ","
comment "# softmax_scale,"
identifier "is_causal"
, ","
comment "# causal,"
conditional_expression
identifier "window_size_left"
if "if"
comparison_operator
identifier "window_size_left"
is not
is "is"
not "not"
none "None"
else "else"
unary_operator
- "-"
integer "1"
, ","
comment "# window_size_left"
conditional_expression
identifier "window_size_right"
if "if"
comparison_operator
identifier "window_size_right"
is not
is "is"
not "not"
none "None"
else "else"
unary_operator
- "-"
integer "1"
, ","
comment "# window_size_right"
integer "0"
, ","
comment "# attention_chunk,"
float "0.0"
, ","
comment "# softcap,"
true "True"
, ","
comment "# rotary_interleaved,"
none "None"
, ","
comment "# scheduler_metadata,"
conditional_expression
integer "1"
if "if"
call
attribute
identifier "torch"
. "."
identifier "are_deterministic_algorithms_enabled"
argument_list
( "("
) ")"
else "else"
integer "0"
, ","
comment "# num_splits,"
none "None"
, ","
comment "# pack_gqa,"
boolean_operator
call
attribute
attribute
identifier "torch"
. "."
identifier "_C"
. "."
identifier "_get_sm_carveout_experimental"
argument_list
( "("
) ")"
or "or"
integer "0"
, ","
comment "# sm_margin,"
) ")"
return_statement
return "return"
expression_list
identifier "out"
, ","
call
attribute
identifier "softmax_lse"
. "."
identifier "contiguous"
argument_list
( "("
) ")"
function_definition (+414 nodes collapsed)
function_definition
def "def"
identifier "_fa3_flash_attention_forward_impl"
parameters
( "("
typed_parameter
identifier "query"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "key"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "value"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "cum_seq_q"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "cum_seq_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "max_q"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "max_k"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "dropout_p"
: ":"
type
identifier "float"
, ","
typed_parameter
identifier "is_causal"
: ":"
type
identifier "bool"
, ","
typed_parameter
identifier "return_debug_mask"
: ":"
type
identifier "bool"
, ","
typed_default_parameter
identifier "q_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "k_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "v_descale"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
keyword_separator
* "*"
, ","
typed_default_parameter
identifier "scale"
: ":"
type
binary_operator
identifier "float"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "window_size_left"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "window_size_right"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "seqused_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "alibi_slopes"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "out"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "block_table"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
) ")"
: ":"
block
expression_statement (+31 nodes collapsed)
if_statement (+25 nodes collapsed)
expression_statement
assignment
pattern_list
identifier "out"
, ","
identifier "lse"
= "="
call
identifier "_fa3_run_forward"
argument_list
( "("
identifier "query"
, ","
identifier "key"
, ","
identifier "value"
, ","
identifier "cum_seq_q"
, ","
identifier "cum_seq_k"
, ","
identifier "max_q"
, ","
identifier "max_k"
, ","
identifier "scale"
, ","
identifier "is_causal"
, ","
identifier "window_size_left"
, ","
identifier "window_size_right"
, ","
identifier "seqused_k"
, ","
identifier "out"
, ","
identifier "q_descale"
, ","
identifier "k_descale"
, ","
identifier "v_descale"
, ","
identifier "block_table"
, ","
) ")"
expression_statement (+33 nodes collapsed)
expression_statement (+31 nodes collapsed)
expression_statement (+29 nodes collapsed)
return_statement
return "return"
expression_list
identifier "out"
, ","
identifier "lse"
, ","
identifier "rng_state"
, ","
identifier "philox_offset"
, ","
identifier "debug_mask"
function_definition
def "def"
identifier "_fa3_flash_attention_forward_impl_default"
parameters
( "("
typed_parameter
identifier "query"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "key"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "value"
: ":"
type
attribute
identifier "torch"
. "."
identifier "Tensor"
, ","
typed_parameter
identifier "cum_seq_q"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "cum_seq_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
, ","
typed_parameter
identifier "max_q"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "max_k"
: ":"
type
identifier "int"
, ","
typed_parameter
identifier "dropout_p"
: ":"
type
identifier "float"
, ","
typed_parameter
identifier "is_causal"
: ":"
type
identifier "bool"
, ","
typed_parameter
identifier "return_debug_mask"
: ":"
type
identifier "bool"
, ","
keyword_separator
* "*"
, ","
typed_default_parameter
identifier "scale"
: ":"
type
binary_operator
identifier "float"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "window_size_left"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "window_size_right"
: ":"
type
identifier "int"
= "="
unary_operator
- "-"
integer "1"
, ","
typed_default_parameter
identifier "seqused_k"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "alibi_slopes"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "block_table"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
typed_default_parameter
identifier "out"
: ":"
type
binary_operator
attribute
identifier "torch"
. "."
identifier "Tensor"
| "|"
none "None"
= "="
none "None"
, ","
) ")"
: ":"
block
return_statement
return "return"
call
identifier "_fa3_flash_attention_forward_impl"
argument_list
( "("
identifier "query"
, ","
identifier "key"
, ","
identifier "value"
, ","
identifier "cum_seq_q"
, ","
identifier "cum_seq_k"
, ","
identifier "max_q"
, ","
identifier "max_k"
, ","
identifier "dropout_p"
, ","
identifier "is_causal"
, ","
identifier "return_debug_mask"
, ","
none "None"
, ","
none "None"
, ","
none "None"
, ","
keyword_argument
identifier "scale"
= "="
identifier "scale"
, ","
keyword_argument
identifier "window_size_left"
= "="
identifier "window_size_left"
, ","
keyword_argument
identifier "window_size_right"
= "="
identifier "window_size_right"
, ","
keyword_argument
identifier "seqused_k"
= "="
identifier "seqused_k"
, ","
keyword_argument
identifier "alibi_slopes"
= "="
identifier "alibi_slopes"
, ","
keyword_argument
identifier "out"
= "="
identifier "out"
, ","
keyword_argument
identifier "block_table"
= "="
identifier "block_table"
, ","
) ")"
function_definition (+311 nodes collapsed)
function_definition (+389 nodes collapsed)
function_definition (+103 nodes collapsed)
function_definition (+307 nodes collapsed)
expression_statement
call
attribute
identifier "_registry"
. "."
identifier "register_flash_attention_impl"
argument_list
( "("
string
string_start "\""
string_content "FA3"
string_end "\""
, ","
keyword_argument
identifier "register_fn"
= "="
identifier "register_flash_attention_fa3"
) ")"