Skip to content

Commit 220f48b

Browse files
frt03The gemma Authors
authored andcommitted
Code update
PiperOrigin-RevId: 948315473
1 parent 09e7b48 commit 220f48b

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

gemma/gm/text/_sampler_loop.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ def attention_mask_for_step(self) -> Bool['B cache_length']: # pyrefly: ignore[
9090
cache_length = self.full_attention_mask.shape[-1]
9191

9292
# +1 because the current step can be self-attended too.
93-
step_mask = jnp.arange(cache_length) < self.used_cache_length + 1
93+
used_cache_len = self.used_cache_length
94+
if used_cache_len.ndim > 0:
95+
used_cache_len = used_cache_len[..., None]
96+
step_mask = jnp.arange(cache_length) < used_cache_len + 1
9497
attention_mask = self.full_attention_mask * step_mask
9598
return attention_mask
9699

gemma/gm/utils/_cache_helper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ def at(self) -> _CacheProxyAt:
7171
return _CacheProxyAt(self)
7272

7373
@property
74-
def end_index(self) -> Int['']: # pyrefly: ignore[not-a-type]
74+
def end_index(self) -> Int['*B']: # pyrefly: ignore[not-a-type]
7575
"""End index of the cache."""
7676
layer_data = next(iter(self.cache.values()))
77-
return layer_data['end_index'][0]
77+
return layer_data['end_index']
7878

7979
def set_end_index(self, value: int) -> Cache:
8080
"""Set the end index of the cache."""
@@ -86,7 +86,7 @@ def set_end_index(self, value: int) -> Cache:
8686
def is_full(self) -> Bool['']: # pyrefly: ignore[not-a-type]
8787
"""Returns whether the cache is full."""
8888
# Maybe will lose the last token.
89-
return self.end_index >= self.total_cache_length - 1
89+
return jnp.any(self.end_index >= self.total_cache_length - 1)
9090

9191

9292
@dataclasses.dataclass(frozen=True)

0 commit comments

Comments
 (0)