Skip to content

Fix sliding window size for bidirectional Gemma 3 - #480

Open
g-andrade wants to merge 1 commit into
elixir-nx:mainfrom
g-andrade:gemma3-bidirectional-window
Open

g-andrade wants to merge 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

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>
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.

1 participant