Skip to content

Fix sliding window size for bidirectional Gemma 3 - #480

Merged
jonatanklosko merged 1 commit into
elixir-nx:mainfrom
g-andrade:gemma3-bidirectional-window
Oct 26, 2026
Merged

jonatanklosko merged 1 commit into
elixir-nx:mainfrom
g-andrade:gemma3-bidirectional-window

Conversation

@g-andrade

@g-andrade g-andrade commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

With bidirectional attention, transformers halves the Gemma 3 sliding window (__post_init__), so EmbeddingGemma (sliding_window: 512) attends to 卤256 tokens. Before this proposed change, bumblebee used 卤511.

This is the window fix from #475, which was closed because of its size.

google/embeddinggemma-300m was checked against sentence-transformers on 110 texts of up to 1,353 tokens. Before, the minimum cosine was 0.987 on inputs over 512 tokens. Now it's 1.000000 on all of them.

Test reference values
from transformers import Gemma3TextConfig, Gemma3TextModel
import torch

repo = "bumblebee-testing/tiny-random-Gemma3TextModel"

config_dict = Gemma3TextConfig.from_pretrained(repo).to_dict()
config_dict.update(use_bidirectional_attention=True, sliding_window=4)
config = Gemma3TextConfig(**config_dict)

model = Gemma3TextModel.from_pretrained(repo, config=config)

inputs = {
    "input_ids": torch.tensor([[10, 20, 30, 40, 50, 60, 70, 80, 0, 0]]),
    "attention_mask": torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]]),
}

outputs = model(**inputs)
print(outputs.last_hidden_state[:, 1:4, 1:4])

馃 Generated with Claude Code

With bidirectional attention, transformers halves the configured
sliding window, so each token attends to half the window on each side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@jonatanklosko jonatanklosko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@jonatanklosko
jonatanklosko merged commit 25fa2dd into elixir-nx:main Oct 26, 2026
2 checks passed
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.

2 participants