Skip to content

Commit 311c536

Browse files
authored
Merge branch 'main' into fix/export-directory
2 parents cf3cf2f + aa4e088 commit 311c536

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-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+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2025 The AI Edge Torch Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
"""Test utils for generative layers."""
16+
from typing import Sequence
17+
from ai_edge_torch.generative.layers import kv_cache as kv_utils
18+
import torch
19+
20+
21+
def initialize_kv_cache_all_zeros(
22+
kv_shape: Sequence[int],
23+
layout: kv_utils.KVLayout = kv_utils.KV_LAYOUT_DEFAULT,
24+
) -> kv_utils.KVCacheEntry:
25+
return kv_utils.KVCacheEntry(
26+
k_cache=torch.zeros(kv_shape, dtype=torch.float32),
27+
v_cache=torch.zeros(kv_shape, dtype=torch.float32),
28+
kv_layout=layout,
29+
)

0 commit comments

Comments
 (0)