Skip to content

Commit aa4e088

Browse files
haozha111copybara-github
authored andcommitted
In ModelConfig, introduce a new field attention_patterns which could hold the repeated patterns for Local/Global Attention. Also implemented helper methods to check if a specific layer is global attention.
PiperOrigin-RevId: 764460147
1 parent aabc50e commit aa4e088

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

ai_edge_torch/generative/layers/model_config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ class ModelConfig:
268268
# export.
269269
use_mask_cache: bool = True
270270

271+
# An interleaved sequence of the attention types used in the model.
272+
# E.g. [AttentionType.LOCAL_SLIDING, AttentionType.LOCAL_SLIDING,
273+
# AttentionType.GLOBAL] means that the model has an attention pattern of 2
274+
# local attentions followed by a global attention in a repeated pattern.
275+
attention_patterns: Optional[Sequence[AttentionType]] = None
276+
271277
@property
272278
def kv_cache_max(self) -> int:
273279
if self.kv_cache_max_len > 0:
@@ -286,3 +292,19 @@ def block_config(self, idx: int) -> TransformerBlockConfig:
286292
@property
287293
def causal_mask_value(self) -> float:
288294
return self.block_config(0).attn_config.causal_mask_value
295+
296+
def check_if_global_attention_layer(self, layer_idx: int) -> bool:
297+
"""Returns True if the layer is a global attention layer."""
298+
if self.attention_patterns is None:
299+
# If attention_patterns is not set, we assume the model has global
300+
# attention.
301+
return True
302+
assert layer_idx >= 0 and layer_idx < self.num_layers, (
303+
"Layer index {layer_idx} is out of range for num_layers:"
304+
f" {self.num_layers}"
305+
)
306+
307+
return (
308+
self.block_config(layer_idx).attn_config.attn_type
309+
== AttentionType.GLOBAL
310+
)

0 commit comments

Comments
 (0)