Skip to content

Trim trailing zero-padded soft tokens in Gemma 4 ViT. - #1100

Open
copybara-service[bot] wants to merge 1 commit into
devfrom
test_1031133282
Open

copybara-service[bot] wants to merge 1 commit into
devfrom
test_1031133282

Conversation

@copybara-service

Copy link
Copy Markdown

Trim trailing zero-padded soft tokens in Gemma 4 ViT.

When an image's aspect ratio yields fewer than max_soft_tokens pooled tokens
(for example, 256 out of 280 for a square image), PrefillVitGemma4 left the
unused trailing rows zero-filled and returned all max_soft_tokens rows.
Because callers insert image_tokens.Rows() placeholders into the prompt,
those zero rows were passed into the language model instead of being stripped
as in the JAX and HuggingFace Gemma 4 implementations—polluting attention and
wasting prefill/KV-cache work (and distorting mean-pooled embeddings).

Resize image_tokens to the actual pooled token count via OverrideRows and
add a unit test covering partial-budget aspect ratios and buffer reuse.

When an image's aspect ratio yields fewer than `max_soft_tokens` pooled tokens
(for example, 256 out of 280 for a square image), `PrefillVitGemma4` left the
unused trailing rows zero-filled and returned all `max_soft_tokens` rows.
Because callers insert `image_tokens.Rows()` placeholders into the prompt,
those zero rows were passed into the language model instead of being stripped
as in the JAX and HuggingFace Gemma 4 implementations—polluting attention and
wasting prefill/KV-cache work (and distorting mean-pooled embeddings).

Resize `image_tokens` to the actual pooled token count via `OverrideRows` and
add a unit test covering partial-budget aspect ratios and buffer reuse.

PiperOrigin-RevId: 1031133282
@google-cla

google-cla Bot commented Nov 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants