Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions lib/bumblebee/text/gemma3_text.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ defmodule Bumblebee.Text.Gemma3Text do
tie_word_embeddings: [
default: true,
doc: "whether to tie input and output embedding weights"
],
use_bidirectional_attention: [
default: false,
doc: "whether to use bidirectional attention instead of causal attention"
]
] ++
Shared.common_options([:rotary_embedding_scaling_strategy, :num_labels, :id_to_label]) ++
Expand Down Expand Up @@ -407,7 +411,7 @@ defmodule Bumblebee.Text.Gemma3Text do
attention_head_size: spec.attention_head_size,
attention_scale: :math.pow(spec.attention_scale_base, -0.5),
attention_window_size: attention_window_size,
causal: true,
causal: not spec.use_bidirectional_attention,
query_norm:
&Layers.rms_norm(&1, shift: 1.0, epsilon: spec.layer_norm_epsilon, name: &2),
key_norm:
Expand Down Expand Up @@ -535,7 +539,8 @@ defmodule Bumblebee.Text.Gemma3Text do
"full_attention" => :full_attention
})
)},
tie_word_embeddings: {"tie_word_embeddings", boolean()}
tie_word_embeddings: {"tie_word_embeddings", boolean()},
use_bidirectional_attention: {"use_bidirectional_attention", boolean()}
) ++
Shared.rotary_embedding_options_from_transformers(data) ++
Shared.common_options_from_transformers(data, spec)
Expand Down
29 changes: 29 additions & 0 deletions test/bumblebee/text/gemma3_text_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ defmodule Bumblebee.Text.Gemma3TextTest do
)
end

test ":base with bidirectional attention" do
assert {:ok, %{model: model, params: params, spec: spec}} =
Bumblebee.load_model({:hf, "bumblebee-testing/tiny-random-Gemma3TextModel"},
spec_overrides: [use_bidirectional_attention: true]
)

assert %Bumblebee.Text.Gemma3Text{
architecture: :base,
use_bidirectional_attention: true
} = spec

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

outputs = Axon.predict(model, params, inputs)

assert Nx.shape(outputs.hidden_state) == {1, 10, 32}

assert_all_close(
outputs.hidden_state[[.., 1..3, 1..3]],
Nx.tensor([
[[0.3755, -0.6418, 1.9320], [-0.5731, 0.3630, 2.5736], [-0.0530, 0.5178, 1.3310]]
]),
atol: 1.0e-4
)
end

test ":for_sequence_classification" do
assert {:ok, %{model: model, params: params, spec: spec}} =
Bumblebee.load_model(
Expand Down
Loading