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
17 changes: 4 additions & 13 deletions keras_hub/src/models/gemma3/gemma3_causal_lm_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,30 +682,25 @@ def generate_preprocess(
if isinstance(x, dict):
images = x.get("images", None)

# TODO: do we even need `responses` for generation? Makes sense for
# finetuning only (i.e., `call()`).
responses = x.get("responses", None)
prompts = x["prompts"]
else:
images = None
responses = None

prompts = x

# Find out if the input is batched/not batched. Uprank if not batched.
# In other preprocessors, we don't have to do this, but here, all
# the following logic (indices, etc.) uses tensors with a batch dim.
# We will squeeze these back at the end.
batched = True

batched = True
if isinstance(prompts, str):
batched = False
prompts = [prompts]
if responses is not None:
responses = [responses]
if isinstance(prompts, tf.Tensor) and len(prompts.shape) == 0:
batched = False
prompts = tf.expand_dims(prompts, axis=0)
if responses is not None:
responses = tf.expand_dims(responses, axis=0)

# We have the same 8 cases here, as in `call()`.
if self.text_only_model and images is not None:
Expand All @@ -729,11 +724,7 @@ def generate_preprocess(
# === Tokenization, padding, etc. ===
prompts = self.tokenizer(prompts)

if responses is not None:
responses = self.tokenizer(responses)
segments = (prompts, responses)
else:
segments = (prompts,)
segments = (prompts,)

# Padding.
token_ids, segment_ids = self.packer(
Expand Down
Loading