Skip to content

Commit 675052d

Browse files
committed
Expose sa_fuse_reciprocal and sa_use_base2_exp flags in MaxText
This change exposes two flags that are supported by the underlying tokamax SplashConfig: - sa_fuse_reciprocal: Maps to fuse_reciprocal in SplashConfig (defaults to True). - sa_use_base2_exp: Maps to use_base2_exp in SplashConfig (defaults to True). These are passed to tokamax_splash_kernel.SplashConfig. TAG=agy CONV=9b0f857c-78d2-4359-be96-73df07e21957 CHANGE_STEWARD=true
1 parent dc59ef9 commit 675052d

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/maxtext/configs/base.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,8 @@ sa_q_layout: "HEAD_DIM_MINOR"
11011101
sa_k_layout: "HEAD_DIM_MINOR"
11021102
sa_v_layout: "HEAD_DIM_MINOR"
11031103
use_splash_scheduler: false # to use tokamax splash attention scheduler.
1104+
sa_fuse_reciprocal: true # defaults to true in Tokamax
1105+
sa_use_base2_exp: true # defaults to true in Tokamax
11041106
# local_sa_* variants apply to local (sliding window) attention layers;
11051107
# if None, each inherits from the corresponding global sa_* flag.
11061108
local_sa_block_q: None # inherits from sa_block_q if None
@@ -1116,6 +1118,8 @@ local_sa_q_layout: None # inherits from sa_q_layout if None
11161118
local_sa_k_layout: None # inherits from sa_k_layout if None
11171119
local_sa_v_layout: None # inherits from sa_v_layout if None
11181120
local_use_splash_scheduler: None # inherits from use_splash_scheduler if None
1121+
local_sa_fuse_reciprocal: None # inherits from sa_fuse_reciprocal if None
1122+
local_sa_use_base2_exp: None # inherits from sa_use_base2_exp if None
11191123
use_max_logit_estimate: -1 # -1 means no estimate, any > 0 value will be used as max logit estimate
11201124
cost_estimate_flops_fwd: -1 # -1 means using splash default cost estmiation, any >= 0 value will be used as cost estmiation for splash to overlap for communication (forward)
11211125
cost_estimate_flops_bwd: -1 # -1 means using splash default cost estmiation, any >= 0 value will be used as cost estmiation for splash to overlap for communication (backward)

src/maxtext/configs/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ class SplashAttention(BaseModel):
700700
sa_k_layout: str = Field("HEAD_DIM_MINOR", description="Layout for K in splash attention.")
701701
sa_v_layout: str = Field("HEAD_DIM_MINOR", description="Layout for V in splash attention.")
702702
use_splash_scheduler: bool = Field(False, description="Use experimental splash attention scheduler.")
703+
sa_fuse_reciprocal: bool = Field(True, description="Maps to fuse_reciprocal in SplashConfig.")
704+
sa_use_base2_exp: bool = Field(True, description="Maps to use_base2_exp in SplashConfig.")
703705
# If None, each local_sa_* flag inherits from the corresponding sa_* flag.
704706
local_sa_block_q: int | None = Field(None, description="Block size for Q in local splash attention.")
705707
local_sa_block_kv: int | None = Field(None, description="Block size for KV in local splash attention.")
@@ -718,6 +720,8 @@ class SplashAttention(BaseModel):
718720
local_sa_k_layout: str | None = Field(None, description="Layout for K in local splash attention.")
719721
local_sa_v_layout: str | None = Field(None, description="Layout for V in local splash attention.")
720722
local_use_splash_scheduler: bool | None = Field(None, description="Use experimental local splash attention scheduler.")
723+
local_sa_fuse_reciprocal: bool | None = Field(None, description="Maps to local fuse_reciprocal in SplashConfig.")
724+
local_sa_use_base2_exp: bool | None = Field(None, description="Maps to local use_base2_exp in SplashConfig.")
721725
use_max_logit_estimate: int = Field(
722726
-1,
723727
description="-1 means no estimate, any > 0 value will be used as max logit estimate",
@@ -2984,6 +2988,10 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de
29842988
self.local_sa_v_layout = self.sa_v_layout
29852989
if self.local_use_splash_scheduler is None:
29862990
self.local_use_splash_scheduler = self.use_splash_scheduler
2991+
if self.local_sa_fuse_reciprocal is None:
2992+
self.local_sa_fuse_reciprocal = self.sa_fuse_reciprocal
2993+
if self.local_sa_use_base2_exp is None:
2994+
self.local_sa_use_base2_exp = self.sa_use_base2_exp
29872995

29882996
# I. RUN ALL CROSS-FIELD VALIDATIONS
29892997
if self.load_parameters_path and self.load_full_state_path:

src/maxtext/layers/attention_op.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ def __init__(
495495
self.k_layout = self.config.local_sa_k_layout
496496
self.v_layout = self.config.local_sa_v_layout
497497
self.use_splash_scheduler = self.config.local_use_splash_scheduler
498+
self.fuse_reciprocal = self.config.local_sa_fuse_reciprocal
499+
self.use_base2_exp = self.config.local_sa_use_base2_exp
498500
else:
499501
self.block_q = self.config.sa_block_q
500502
self.block_kv = self.config.sa_block_kv
@@ -509,6 +511,8 @@ def __init__(
509511
self.k_layout = self.config.sa_k_layout
510512
self.v_layout = self.config.sa_v_layout
511513
self.use_splash_scheduler = self.config.use_splash_scheduler
514+
self.fuse_reciprocal = self.config.sa_fuse_reciprocal
515+
self.use_base2_exp = self.config.sa_use_base2_exp
512516
self.attn_logits_soft_cap = attn_logits_soft_cap
513517
self.sliding_window_size = sliding_window_size
514518
self.chunk_attn_window_size = chunk_attn_window_size
@@ -1226,6 +1230,8 @@ def create_sa_config(config, query, key, attn_logits_soft_cap):
12261230
k_layout=tokamax_splash_kernel.QKVLayout[self.k_layout],
12271231
v_layout=tokamax_splash_kernel.QKVLayout[self.v_layout],
12281232
attn_logits_soft_cap=attn_logits_soft_cap,
1233+
fuse_reciprocal=self.fuse_reciprocal,
1234+
use_base2_exp=self.use_base2_exp,
12291235
residual_checkpoint_name="context",
12301236
fwd_cost_estimate=pl.CostEstimate(
12311237
flops=config.cost_estimate_flops_fwd,

0 commit comments

Comments
 (0)