Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions gemma/gm/text/_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,24 @@ def sample(

# TODO(epot): Donate the `init_state`, `last_state`

# Build end_tokens conditionally: only include BEGIN_OF_TOOL_RESPONSE if
# the tokenizer supports it (Gemma3+), not Gemma2.
base_end_tokens = (
self.tokenizer.special_tokens.EOS,
self.tokenizer.special_tokens.END_OF_TURN,
)
# Only add tool token if the tokenizer defines it (Gemma3+ only).
tool_token = getattr(
self.tokenizer.special_tokens, 'BEGIN_OF_TOOL_RESPONSE', None
)
if tool_token is not None:
base_end_tokens = (*base_end_tokens, tool_token)
end_tokens = (*base_end_tokens, *self._normalized_stop_tokens)

sampler = _sampler_loop.SamplerLoop(
# Static attributes. Changing those will trigger a recompilation.
model=self.model,
end_tokens=(
self.tokenizer.special_tokens.EOS,
self.tokenizer.special_tokens.END_OF_TURN,
self.tokenizer.special_tokens.BEGIN_OF_TOOL_RESPONSE,
*self._normalized_stop_tokens,
),
end_tokens=end_tokens, # ← Changed from tuple literal to variable
forbidden_tokens=self._normalized_forbidden_tokens,
sampling=sampling,
cache_length=self.cache_length,
Expand Down