diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..c874e913 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,58 @@ +# Bumblebee Examples + +This directory contains example scripts demonstrating how to use Bumblebee models. + +## Qwen3 Examples + +See the `qwen3/` subdirectory for comprehensive Qwen3 model examples: + +### Text Generation +```bash +elixir examples/qwen3/qwen3.exs +``` + +### Text Embeddings +```bash +elixir examples/qwen3/qwen3_embedding.exs +elixir examples/qwen3/qwen3_embedding_prompts.exs +``` + +### Document Reranking +```bash +elixir examples/qwen3/qwen3_reranker.exs +``` + +### Features Demonstrated + +**Text Generation** (`qwen3.exs`): +- Text completion +- Question answering +- Chat format +- Code generation + +**Embeddings** (`qwen3_embedding.exs`, `qwen3_embedding_prompts.exs`): +- 1024-dimensional text embeddings +- Semantic similarity computation +- Instruction-aware prompts (recommended by Qwen team) +- Multilingual support +- Code search + +**Reranking** (`qwen3_reranker.exs`): +- Query-document relevance scoring +- Custom task instructions +- Top-k result selection + +### Requirements + +- **Text Generation**: ~8GB disk space, ~10GB RAM +- **Embeddings**: ~1.5GB disk space, ~4GB RAM (0.6B model) +- **Reranking**: ~1.5GB disk space, ~4GB RAM (0.6B model) +- **Backend**: EXLA (CPU or GPU) + +### Documentation + +See `examples/qwen3/QWEN3_IEX_GUIDE.md` for interactive IEx usage examples. + +## Phoenix Examples + +See the `phoenix/` subdirectory for LiveView-based examples. diff --git a/lib/bumblebee.ex b/lib/bumblebee.ex index 51f2330f..a8ca88b0 100644 --- a/lib/bumblebee.ex +++ b/lib/bumblebee.ex @@ -178,6 +178,10 @@ defmodule Bumblebee do "Phi3ForCausalLM" => {Bumblebee.Text.Phi3, :for_causal_language_modeling}, "Phi3ForSequenceClassification" => {Bumblebee.Text.Phi3, :for_sequence_classification}, "Phi3ForTokenClassification" => {Bumblebee.Text.Phi3, :for_token_classification}, + "Qwen3Model" => {Bumblebee.Text.Qwen3, :base}, + "Qwen3ForCausalLM" => {Bumblebee.Text.Qwen3, :for_causal_language_modeling}, + "Qwen3ForSequenceClassification" => {Bumblebee.Text.Qwen3, :for_sequence_classification}, + "Qwen3ForEmbedding" => {Bumblebee.Text.Qwen3, :for_embedding}, "ResNetForImageClassification" => {Bumblebee.Vision.ResNet, :for_image_classification}, "ResNetModel" => {Bumblebee.Vision.ResNet, :base}, "RobertaForMaskedLM" => {Bumblebee.Text.Roberta, :for_masked_language_modeling}, @@ -253,6 +257,7 @@ defmodule Bumblebee do "mbart" => :mbart, "phi" => :code_gen, "phi3" => :llama, + "qwen3" => :qwen2, "roberta" => :roberta, "t5" => :t5, "whisper" => :whisper, diff --git a/lib/bumblebee/layers/transformer.ex b/lib/bumblebee/layers/transformer.ex index 6cf93cd6..a2332ef3 100644 --- a/lib/bumblebee/layers/transformer.ex +++ b/lib/bumblebee/layers/transformer.ex @@ -50,7 +50,9 @@ defmodule Bumblebee.Layers.Transformer do :block_type, :attention_window_size, :scale_attention_weights, - :rotary_embedding + :rotary_embedding, + :query_norm, + :key_norm ] opts = @@ -317,7 +319,9 @@ defmodule Bumblebee.Layers.Transformer do layer_norm: [], attention_window_size: nil, scale_attention_weights: true, - rotary_embedding: nil + rotary_embedding: nil, + query_norm: nil, + key_norm: nil ]) name = opts[:name] @@ -347,6 +351,8 @@ defmodule Bumblebee.Layers.Transformer do attention_window_size = opts[:attention_window_size] scale_attention_weights = opts[:scale_attention_weights] rotary_embedding = opts[:rotary_embedding] + query_norm = opts[:query_norm] + key_norm = opts[:key_norm] ffn_fun = case ffn do @@ -405,6 +411,8 @@ defmodule Bumblebee.Layers.Transformer do attention_window_size: attention_window_size, scale_attention_weights: scale_attention_weights, rotary_embedding: rotary_embedding, + query_norm: query_norm, + key_norm: key_norm, name: join(name, "self_attention") ) @@ -690,6 +698,14 @@ defmodule Bumblebee.Layers.Transformer do * `:max_positions` - the maximum number of distinct positions + * `:query_norm` - configuration for query normalization. If set, normalizes + the query projection before rotary embedding. Configured with the same + options as `:layer_norm` in the block function. Defaults to `nil` + + * `:key_norm` - configuration for key normalization. If set, normalizes + the key projection before rotary embedding. Configured with the same + options as `:layer_norm` in the block function. Defaults to `nil` + * `:name` - the prefix for layer names ## References @@ -721,7 +737,9 @@ defmodule Bumblebee.Layers.Transformer do key_use_bias: true, value_use_bias: true, output_use_bias: true, - rotary_embedding: nil + rotary_embedding: nil, + query_norm: nil, + key_norm: nil ]) attention_mask = opts[:attention_mask] @@ -739,6 +757,8 @@ defmodule Bumblebee.Layers.Transformer do scale_attention_weights = opts[:scale_attention_weights] dropout_rate = opts[:dropout_rate] rotary_embedding = opts[:rotary_embedding] + query_norm = opts[:query_norm] + key_norm = opts[:key_norm] query_use_bias = opts[:query_use_bias] key_use_bias = opts[:key_use_bias] @@ -778,6 +798,35 @@ defmodule Bumblebee.Layers.Transformer do ) |> Layers.split_heads(num_key_value_heads) + # Apply query and key normalization if configured (before rotary embedding) + query = + case query_norm do + opts when is_list(opts) -> + opts = Keyword.validate!(opts, epsilon: 1.0e-5) + # Normalize over the head dimension (channel_index: -1) + Layers.rms_norm(query, [epsilon: opts[:epsilon], channel_index: -1, name: join(name, "query_norm")]) + + fun when is_function(fun) -> + fun.(query, join(name, "query_norm")) + + nil -> + query + end + + key = + case key_norm do + opts when is_list(opts) -> + opts = Keyword.validate!(opts, epsilon: 1.0e-5) + # Normalize over the head dimension (channel_index: -1) + Layers.rms_norm(key, [epsilon: opts[:epsilon], channel_index: -1, name: join(name, "key_norm")]) + + fun when is_function(fun) -> + fun.(key, join(name, "key_norm")) + + nil -> + key + end + {query, key} = case rotary_embedding do opts when is_list(opts) -> diff --git a/lib/bumblebee/text.ex b/lib/bumblebee/text.ex index 770a2192..405a48ce 100644 --- a/lib/bumblebee/text.ex +++ b/lib/bumblebee/text.ex @@ -444,6 +444,48 @@ defmodule Bumblebee.Text do defdelegate text_embedding(model_info, tokenizer, opts \\ []), to: Bumblebee.Text.TextEmbedding + @type text_reranking_input :: {String.t(), String.t()} | [{String.t(), String.t()}] + @type text_reranking_output :: %{scores: text_reranking_score() | list(text_reranking_score())} + @type text_reranking_score :: %{score: number(), query: String.t(), document: String.t()} + + @doc """ + Builds a serving for text reranking. + + The serving expects input in one of the following formats: + + * `{query, document}` - a tuple with query and document text + * `[{query1, doc1}, {query2, doc2}, ...]` - a list of query-document pairs + + ## Options + + See `Bumblebee.Text.TextReranking.text_reranking/3` for available options. + + ## Examples + + {:ok, model_info} = Bumblebee.load_model({:hf, "Qwen/Qwen3-Reranker-0.6B"}, + architecture: :for_reranker) + {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "Qwen/Qwen3-Reranker-0.6B"}) + + serving = Bumblebee.Text.text_reranking(model_info, tokenizer) + + query = "What is the capital of France?" + documents = [ + "Paris is the capital of France.", + "Berlin is the capital of Germany." + ] + + pairs = Enum.map(documents, &{query, &1}) + Nx.Serving.run(serving, pairs) + + """ + @spec text_reranking( + Bumblebee.model_info(), + Bumblebee.Tokenizer.t(), + keyword() + ) :: Nx.Serving.t() + defdelegate text_reranking(model_info, tokenizer, opts \\ []), + to: Bumblebee.Text.TextReranking + @type fill_mask_input :: String.t() @type fill_mask_output :: %{predictions: list(fill_mask_prediction())} @type fill_mask_prediction :: %{score: number(), token: String.t()} diff --git a/lib/bumblebee/text/pre_trained_tokenizer.ex b/lib/bumblebee/text/pre_trained_tokenizer.ex index 59ab3468..faf57329 100644 --- a/lib/bumblebee/text/pre_trained_tokenizer.ex +++ b/lib/bumblebee/text/pre_trained_tokenizer.ex @@ -200,6 +200,12 @@ defmodule Bumblebee.Text.PreTrainedTokenizer do }, default_template_options: [language_token: "eng_Latn"] }, + qwen2: %{ + special_tokens: %{ + eos: "<|im_end|>", + pad: "<|endoftext|>" + } + }, roberta: %{ special_tokens: %{ bos: "", diff --git a/lib/bumblebee/text/qwen3.ex b/lib/bumblebee/text/qwen3.ex new file mode 100644 index 00000000..8a2b72f6 --- /dev/null +++ b/lib/bumblebee/text/qwen3.ex @@ -0,0 +1,552 @@ +defmodule Bumblebee.Text.Qwen3 do + alias Bumblebee.Shared + + options = + [ + vocab_size: [ + default: 151_936, + doc: """ + the vocabulary size of the token embedding. This corresponds to the number of distinct + tokens that can be represented in model input and output + """ + ], + max_positions: [ + default: 262_144, + doc: """ + the vocabulary size of the position embedding. This corresponds to the maximum sequence + length that this model can process. Typically this is set to a large value just in case, + such as 512, 1024 or 2048 + """ + ], + hidden_size: [ + default: 2560, + doc: "the dimensionality of hidden layers" + ], + intermediate_size: [ + default: 9728, + doc: "the dimensionality of intermediate layers" + ], + attention_head_size: [ + default: 128, + doc: """ + the size of the key, value, and query projection per attention head. + """ + ], + num_blocks: [ + default: 36, + doc: "the number of Transformer blocks in the model" + ], + num_attention_heads: [ + default: 32, + doc: "the number of attention heads for each attention layer in the model" + ], + num_key_value_heads: [ + default: 8, + doc: "the number of key value heads for each attention layer in the model" + ], + activation: [ + default: :silu, + doc: "the activation function" + ], + rotary_embedding_base: [ + default: 5_000_000, + doc: "base for computing rotary embedding frequency" + ], + rotary_embedding_scaling_strategy: [ + default: nil, + doc: """ + scaling configuration for rotary embedding. Currently the supported values are: + + * `%{type: :linear, factor: number()}` + + * `%{type: :dynamic, factor: number()}` + + For more details see https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases + """ + ], + layer_norm_epsilon: [ + default: 1.0e-6, + doc: "the epsilon used by RMS normalization layers" + ], + initializer_scale: [ + default: 0.02, + doc: + "the standard deviation of the normal initializer used for initializing kernel parameters" + ], + tie_word_embeddings: [ + default: true, + doc: "whether to tie input and output embedding weights" + ], + use_qk_norm: [ + default: true, + doc: "whether to use RMS normalization on query and key projections" + ] + ] ++ + Shared.common_options([:num_labels, :id_to_label]) ++ + Shared.token_options(pad_token_id: 151_643) + + @moduledoc """ + Qwen3 model family. + + ## Architectures + + * `:base` - plain Qwen3 without any head on top + + * `:for_causal_language_modeling` - Qwen3 with a language modeling + head. The head returns logits for each token in the original + sequence + + * `:for_sequence_classification` - Qwen3 with a sequence + classification head. The head returns logits corresponding to + possible classes + + * `:for_embedding` - Qwen3 with pooling to produce a single embedding + vector per sequence. The head pools the last attended token (based on + attention mask) and returns it as an embedding + + * `:for_reranker` - Qwen3 configured for binary relevance classification + (reranking). Returns logits at the last attended token position for + computing relevance scores between query-document pairs + + ## Inputs + + * `"input_ids"` - `{batch_size, sequence_length}` + + Indices of input sequence tokens in the vocabulary. + + * `"attention_mask"` - `{batch_size, sequence_length}` + + Mask indicating which tokens to attend to. This is used to ignore + padding tokens, which are added when processing a batch of sequences + with different length. + + * `"position_ids"` - `{batch_size, sequence_length}` + + Indices of positions of each input sequence tokens in the position + embeddings. + + * `"attention_head_mask"` - `{encoder_num_blocks, encoder_num_attention_heads}` + + Mask to nullify selected heads of the self-attention blocks in + the encoder. + + * `"input_embeddings"` - `{batch_size, sequence_length, hidden_size}` + + Embedded representation of `"input_ids"`, which can be specified + for more control over how `"input_ids"` are embedded than the + model's internal embedding lookup. If `"input_embeddings"` are present, + then `"input_ids"` will be ignored. + + * `"cache"` + + A container with cached layer results used to speed up sequential + decoding (autoregression). With cache, certain hidden states are + taken from the cache, rather than recomputed on every decoding + pass. The cache should be treated as opaque and initialized with + `Bumblebee.Text.Generation.init_cache/4`. + + ## Global layer options + + #{Shared.global_layer_options_doc([:output_hidden_states, :output_attentions])} + + ## Configuration + + #{Shared.options_doc(options)} + """ + + defstruct [architecture: :base] ++ Shared.option_defaults(options) + + @behaviour Bumblebee.ModelSpec + @behaviour Bumblebee.Configurable + @behaviour Bumblebee.Text.Generation + + import Bumblebee.Utils.Model, only: [join: 2] + + alias Bumblebee.Layers + + @impl true + def architectures(), + do: [ + :base, + :for_causal_language_modeling, + :for_sequence_classification, + :for_embedding, + :for_reranker + ] + + @impl true + def config(spec, opts) do + spec + |> Shared.put_config_attrs(opts) + |> Shared.validate_label_options() + end + + @impl true + def input_template(_spec) do + %{ + "input_ids" => Nx.template({1, 1}, :s64) + } + end + + @impl true + def init_cache(spec, batch_size, max_length, _inputs) do + Layers.Decoder.init_cache(batch_size, max_length, + hidden_size: spec.hidden_size, + attention_head_size: spec.attention_head_size, + decoder_num_attention_heads: spec.num_attention_heads, + decoder_num_blocks: spec.num_blocks + ) + end + + @impl true + def traverse_cache(_spec, cache, fun) do + Layers.Decoder.traverse_cache(cache, fun) + end + + @impl true + def model(%__MODULE__{architecture: :base} = spec) do + inputs = inputs(spec) + + inputs + |> core(spec) + |> Layers.output() + end + + def model(%__MODULE__{architecture: :for_causal_language_modeling} = spec) do + inputs = inputs(spec) + + outputs = core(inputs, spec) + logits = language_modeling_head(outputs.hidden_state, spec, name: "language_modeling_head") + + Layers.output(%{ + logits: logits, + hidden_states: outputs.hidden_states, + attentions: outputs.attentions, + cache: outputs.cache + }) + end + + def model(%__MODULE__{architecture: :for_sequence_classification} = spec) do + inputs = inputs(spec) + + outputs = core(inputs, spec) + + logits = + Axon.dense(outputs.hidden_state, spec.num_labels, + kernel_initializer: kernel_initializer(spec), + name: "sequence_classification_head.output", + use_bias: false + ) + + pooled_logits = + Layers.if_present inputs["input_ids"] do + Axon.layer( + fn logits, input_ids, _opts -> + indices = + input_ids + |> Nx.not_equal(spec.pad_token_id) + |> Nx.sum(axes: [-1]) + |> Nx.subtract(1) + |> Nx.as_type({:s, 64}) + + Bumblebee.Utils.Nx.batched_take(logits, indices) + end, + [logits, inputs["input_ids"]] + ) + else + Layers.take_token(logits, axis: 1, index: -1) + end + + Layers.output(%{ + logits: pooled_logits, + hidden_states: outputs.hidden_states, + attentions: outputs.attentions, + cache: outputs.cache + }) + end + + def model(%__MODULE__{architecture: :for_embedding} = spec) do + inputs = inputs(spec) + + outputs = core(inputs, spec) + + # Pool the last token using attention mask + # For Qwen3 embeddings, we need to find the last attended token based on + # the attention mask, not the pad_token_id. The EOS token (which matches + # pad_token_id) is actually part of the sequence and should be attended. + pooled_state = + Layers.if_present inputs["attention_mask"] do + Axon.layer( + fn hidden_state, attention_mask, _opts -> + # Find the last token with attention_mask = 1 (last attended token) + # This matches the behavior of the reference implementation + indices = + attention_mask + |> Nx.sum(axes: [-1]) + |> Nx.subtract(1) + |> Nx.as_type({:s, 64}) + + Bumblebee.Utils.Nx.batched_take(hidden_state, indices) + end, + [outputs.hidden_state, inputs["attention_mask"]] + ) + else + Layers.take_token(outputs.hidden_state, axis: 1, index: -1) + end + + Layers.output(%{ + embedding: pooled_state, + hidden_states: outputs.hidden_states, + attentions: outputs.attentions + }) + end + + def model(%__MODULE__{architecture: :for_reranker} = spec) do + inputs = inputs(spec) + + outputs = core(inputs, spec) + logits = language_modeling_head(outputs.hidden_state, spec, name: "language_modeling_head") + + # For reranker, we need to extract the logits at the last attended token position + # and return them for binary classification (relevant vs not relevant) + last_token_logits = + Layers.if_present inputs["attention_mask"] do + Axon.layer( + fn logits, attention_mask, _opts -> + # Find the last attended token position + indices = + attention_mask + |> Nx.sum(axes: [-1]) + |> Nx.subtract(1) + |> Nx.as_type({:s, 64}) + + Bumblebee.Utils.Nx.batched_take(logits, indices) + end, + [logits, inputs["attention_mask"]] + ) + else + Layers.take_token(logits, axis: 1, index: -1) + end + + Layers.output(%{ + logits: last_token_logits, + hidden_states: outputs.hidden_states, + attentions: outputs.attentions, + cache: outputs.cache + }) + end + + defp inputs(spec) do + shape = {nil, nil} + hidden_shape = {nil, nil, spec.hidden_size} + + attention_head_mask_shape = {spec.num_blocks, spec.num_attention_heads} + + Bumblebee.Utils.Model.inputs_to_map([ + Axon.input("input_ids", optional: true, shape: shape), + Axon.input("attention_mask", optional: true, shape: shape), + Axon.input("position_ids", optional: true, shape: shape), + Axon.input("attention_head_mask", optional: true, shape: attention_head_mask_shape), + Axon.input("input_embeddings", optional: true, shape: hidden_shape), + Axon.input("cache", optional: true) + ]) + end + + defp core(inputs, spec) do + embeddings = + embedder( + inputs["input_ids"], + inputs["input_embeddings"], + spec, + name: "embedder" + ) + + position_ids = + Layers.default inputs["position_ids"] do + Layers.default_position_ids(embeddings) + end + + decoder_outputs = + decoder( + embeddings, + position_ids, + inputs["attention_mask"], + inputs["attention_head_mask"], + inputs["cache"], + spec, + name: "decoder" + ) + + hidden_state = + Layers.rms_norm(decoder_outputs.hidden_state, + name: "output_norm", + epsilon: spec.layer_norm_epsilon + ) + + %{ + hidden_state: hidden_state, + hidden_states: Layers.append(decoder_outputs.hidden_states, hidden_state), + attentions: decoder_outputs.attentions, + cache: decoder_outputs.cache + } + end + + defp embedder(input_ids, input_embeddings, spec, opts) do + name = opts[:name] + + Layers.default input_embeddings do + Axon.embedding(input_ids, spec.vocab_size, spec.hidden_size, + kernel_initializer: kernel_initializer(spec), + name: join(name, "token_embedding") + ) + end + end + + defp decoder( + hidden_state, + position_ids, + attention_mask, + attention_head_mask, + cache, + spec, + opts + ) do + name = opts[:name] + + # Build query and key normalization configuration for Qwen3 + query_norm = if spec.use_qk_norm, do: [epsilon: spec.layer_norm_epsilon], else: nil + key_norm = if spec.use_qk_norm, do: [epsilon: spec.layer_norm_epsilon], else: nil + + # Use the generalized Layers.Transformer.blocks with QK normalization + Layers.Transformer.blocks(hidden_state, + num_blocks: spec.num_blocks, + num_attention_heads: spec.num_attention_heads, + num_key_value_heads: spec.num_key_value_heads, + hidden_size: spec.hidden_size, + attention_head_size: spec.attention_head_size, + kernel_initializer: kernel_initializer(spec), + query_use_bias: false, + key_use_bias: false, + value_use_bias: false, + output_use_bias: false, + block_type: :norm_first, + attention_mask: attention_mask, + attention_head_mask: attention_head_mask, + cache: cache, + causal: true, + layer_norm: &Layers.rms_norm(&1, epsilon: spec.layer_norm_epsilon, name: &2), + ffn: + &gated_ffn(&1, spec.intermediate_size, spec.hidden_size, + name: &2, + activation: spec.activation + ), + rotary_embedding: [ + position_ids: position_ids, + max_positions: spec.max_positions, + base: spec.rotary_embedding_base, + scaling_strategy: spec.rotary_embedding_scaling_strategy + ], + query_norm: query_norm, + key_norm: key_norm, + name: join(name, "blocks") + ) + end + + defp gated_ffn(hidden_state, intermediate_size, output_size, opts) do + name = opts[:name] + activation = opts[:activation] + + intermediate = + Axon.dense(hidden_state, intermediate_size, + name: join(name, "intermediate"), + use_bias: false + ) + + gate = Axon.dense(hidden_state, intermediate_size, name: join(name, "gate"), use_bias: false) + + hidden_state = Axon.multiply(intermediate, Axon.activation(gate, activation)) + + Axon.dense(hidden_state, output_size, name: join(name, "output"), use_bias: false) + end + + defp language_modeling_head(hidden_state, spec, opts) do + name = opts[:name] + + Layers.dense_transposed(hidden_state, spec.vocab_size, + kernel_initializer: kernel_initializer(spec), + name: join(name, "output") + ) + end + + defp kernel_initializer(spec) do + Axon.Initializers.normal(scale: spec.initializer_scale) + end + + defimpl Bumblebee.HuggingFace.Transformers.Config do + def load(spec, data) do + import Shared.Converters + + scaling_strategy_converter = fn _name, value -> + case value do + %{"type" => "linear", "factor" => factor} when is_number(factor) -> + {:ok, %{type: :linear, factor: factor}} + + %{"type" => "dynamic", "factor" => factor} when is_number(factor) -> + {:ok, %{type: :dynamic, factor: factor}} + + nil -> + {:ok, nil} + + _other -> + {:ok, nil} + end + end + + opts = + convert!(data, + vocab_size: {"vocab_size", number()}, + tie_word_embeddings: {"tie_word_embeddings", boolean()}, + max_positions: {"max_position_embeddings", number()}, + hidden_size: {"hidden_size", number()}, + num_blocks: {"num_hidden_layers", number()}, + num_attention_heads: {"num_attention_heads", number()}, + num_key_value_heads: {"num_key_value_heads", number()}, + attention_head_size: {"head_dim", number()}, + intermediate_size: {"intermediate_size", number()}, + activation: {"hidden_act", activation()}, + rotary_embedding_base: {"rope_theta", number()}, + rotary_embedding_scaling_strategy: + {"rope_scaling", optional(scaling_strategy_converter)}, + initializer_scale: {"initializer_range", number()}, + layer_norm_epsilon: {"rms_norm_eps", number()} + ) ++ Shared.common_options_from_transformers(data, spec) + + @for.config(spec, opts) + end + end + + defimpl Bumblebee.HuggingFace.Transformers.Model do + def params_mapping(spec) do + %{ + "embedder.token_embedding" => "model.embed_tokens", + "decoder.blocks.{n}.self_attention.query" => "model.layers.{n}.self_attn.q_proj", + "decoder.blocks.{n}.self_attention.key" => "model.layers.{n}.self_attn.k_proj", + "decoder.blocks.{n}.self_attention.value" => "model.layers.{n}.self_attn.v_proj", + "decoder.blocks.{n}.self_attention.output" => "model.layers.{n}.self_attn.o_proj", + "decoder.blocks.{n}.self_attention.query_norm" => "model.layers.{n}.self_attn.q_norm", + "decoder.blocks.{n}.self_attention.key_norm" => "model.layers.{n}.self_attn.k_norm", + "decoder.blocks.{n}.self_attention_norm" => "model.layers.{n}.input_layernorm", + "decoder.blocks.{n}.self_attention.rotary_embedding" => + "model.layers.{n}.self_attn.rotary_emb", + "decoder.blocks.{n}.ffn.gate" => "model.layers.{n}.mlp.gate_proj", + "decoder.blocks.{n}.ffn.intermediate" => "model.layers.{n}.mlp.up_proj", + "decoder.blocks.{n}.ffn.output" => "model.layers.{n}.mlp.down_proj", + "decoder.blocks.{n}.output_norm" => "model.layers.{n}.post_attention_layernorm", + "output_norm" => "model.norm", + "language_modeling_head.output" => + if(spec.tie_word_embeddings, do: "model.embed_tokens", else: "lm_head"), + "sequence_classification_head.output" => "score" + } + end + end +end diff --git a/lib/bumblebee/text/text_embedding.ex b/lib/bumblebee/text/text_embedding.ex index 34f41279..44011291 100644 --- a/lib/bumblebee/text/text_embedding.ex +++ b/lib/bumblebee/text/text_embedding.ex @@ -78,9 +78,19 @@ defmodule Bumblebee.Text.TextEmbedding do |> Nx.sum(axes: [1]) |> Nx.divide(Nx.sum(input_mask_expanded, axes: [1])) + :last_token_pooling -> + # Take the last non-padding token for each sequence + sequence_lengths = + inputs["attention_mask"] + |> Nx.sum(axes: [1]) + |> Nx.subtract(1) + |> Nx.as_type({:s, 64}) + + Bumblebee.Utils.Nx.batched_take(output, sequence_lengths) + other -> raise ArgumentError, - "expected :output_pool to be one of :cls_token_pooling, :mean_pooling or nil, got: #{inspect(other)}" + "expected :output_pool to be one of :cls_token_pooling, :mean_pooling, :last_token_pooling or nil, got: #{inspect(other)}" end output = diff --git a/lib/bumblebee/text/text_reranking.ex b/lib/bumblebee/text/text_reranking.ex new file mode 100644 index 00000000..974028ff --- /dev/null +++ b/lib/bumblebee/text/text_reranking.ex @@ -0,0 +1,243 @@ +defmodule Bumblebee.Text.TextReranking do + @moduledoc false + + alias Bumblebee.Shared + + @doc """ + Creates a serving for text reranking. + + The serving expects input in one of the following formats: + + * `{query, document}` - a tuple with query and document text + * `[{query1, doc1}, {query2, doc2}, ...]` - a list of query-document pairs + + ## Options + + * `:yes_token` - the token ID corresponding to "yes" for relevance scoring. + If not provided, will be inferred from the tokenizer + + * `:no_token` - the token ID corresponding to "no" for relevance scoring. + If not provided, will be inferred from the tokenizer + + * `:instruction_prefix` - the instruction prefix to use. Defaults to the + Qwen3 reranker format + + * `:instruction_suffix` - the instruction suffix to use. Defaults to the + Qwen3 reranker format + + * `:task_description` - the task description to include in prompts. Defaults + to "Given a web search query, retrieve relevant passages that answer the query" + + * `:compile` - compiles all computations for predefined input shapes + during serving initialization. Should be a keyword list with the + following keys: + + * `:batch_size` - the maximum batch size of the input. Inputs + are optionally padded to always match this batch size + + * `:sequence_length` - the maximum input sequence length. Input + sequences are always padded/truncated to match that length + + It is advised to set this option in production and also configure + a defn compiler using `:defn_options` to maximally reduce inference + time + + * `:defn_options` - the options for JIT compilation. Defaults to `[]` + + * `:preallocate_params` - when `true`, explicitly allocates params + on the device configured in `:defn_options`. You may want to set + this option when using partitioned models on the GPU + + ## Examples + + {:ok, model_info} = Bumblebee.load_model({:hf, "Qwen/Qwen3-Reranker-0.6B"}, + architecture: :for_reranker) + {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "Qwen/Qwen3-Reranker-0.6B"}) + + serving = Bumblebee.Text.TextReranking.text_reranking(model_info, tokenizer) + + query = "What is the capital of France?" + documents = [ + "Paris is the capital of France.", + "Berlin is the capital of Germany.", + "The Eiffel Tower is in Paris." + ] + + pairs = Enum.map(documents, &{query, &1}) + Nx.Serving.run(serving, pairs) + #=> %{ + #=> scores: [ + #=> %{score: 0.95, query: "What is the capital of France?", document: "Paris is the capital of France."}, + #=> %{score: 0.15, query: "What is the capital of France?", document: "Berlin is the capital of Germany."}, + #=> %{score: 0.72, query: "What is the capital of France?", document: "The Eiffel Tower is in Paris."} + #=> ] + #=> } + """ + def text_reranking(model_info, tokenizer, opts \\ []) do + %{model: model, params: params, spec: spec} = model_info + Shared.validate_architecture!(spec, :for_reranker) + + # Get yes/no token IDs + yes_token = + opts[:yes_token] || + get_token_id(tokenizer, "yes") + + no_token = + opts[:no_token] || + get_token_id(tokenizer, "no") + + # Default Qwen3 reranker format + instruction_prefix = + opts[:instruction_prefix] || + "<|im_start|>system\nJudge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be \"yes\" or \"no\".<|im_end|>\n<|im_start|>user\n" + + instruction_suffix = + opts[:instruction_suffix] || + "<|im_end|>\n<|im_start|>assistant\n\n\n\n\n" + + task_description = + opts[:task_description] || + "Given a web search query, retrieve relevant passages that answer the query" + + opts = + Keyword.validate!(opts, [ + :compile, + :yes_token, + :no_token, + :instruction_prefix, + :instruction_suffix, + :task_description, + defn_options: [], + preallocate_params: false + ]) + + preallocate_params = opts[:preallocate_params] + defn_options = opts[:defn_options] + + compile = + if compile = opts[:compile] do + compile + |> Keyword.validate!([:batch_size, :sequence_length]) + |> Shared.require_options!([:batch_size, :sequence_length]) + end + + batch_size = compile[:batch_size] + sequence_length = compile[:sequence_length] + + tokenizer = + Bumblebee.configure(tokenizer, + length: sequence_length, + return_token_type_ids: false + ) + + {_init_fun, predict_fun} = Axon.build(model) + + scores_fun = fn params, input -> + outputs = predict_fun.(params, input) + # outputs.logits has shape {batch_size, vocab_size} + # Extract logits for yes/no tokens + yes_logits = outputs.logits[[.., yes_token]] + no_logits = outputs.logits[[.., no_token]] + + # Stack and apply log_softmax + stacked = Nx.stack([no_logits, yes_logits], axis: 1) + log_probs = Axon.Activations.log_softmax(stacked, axis: 1) + + # Take exp of yes probability + scores = Nx.exp(log_probs[[.., 1]]) + scores + end + + batch_keys = Shared.sequence_batch_keys(sequence_length) + + Nx.Serving.new( + fn batch_key, defn_options -> + params = Shared.maybe_preallocate(params, preallocate_params, defn_options) + + scope = {:scores, batch_key} + + scores_fun = + Shared.compile_or_jit(scores_fun, scope, defn_options, compile != nil, fn -> + {:sequence_length, sequence_length} = batch_key + + inputs = %{ + "input_ids" => Nx.template({batch_size, sequence_length}, :u32), + "attention_mask" => Nx.template({batch_size, sequence_length}, :u32) + } + + [params, inputs] + end) + + fn inputs -> + inputs = Shared.maybe_pad(inputs, batch_size) + scores_fun.(params, inputs) |> Shared.serving_post_computation() + end + end, + defn_options + ) + |> Nx.Serving.batch_size(batch_size) + |> Nx.Serving.process_options(batch_keys: batch_keys) + |> Nx.Serving.client_preprocessing(fn input -> + {pairs, multi?} = validate_reranking_input!(input) + + # Format each query-document pair with the instruction template + texts = + Enum.map(pairs, fn {query, document} -> + content = format_instruction(task_description, query, document) + "#{instruction_prefix}#{content}#{instruction_suffix}" + end) + + inputs = + Nx.with_default_backend(Nx.BinaryBackend, fn -> + Bumblebee.apply_tokenizer(tokenizer, texts) + end) + + batch_key = Shared.sequence_batch_key_for_inputs(inputs, sequence_length) + batch = [inputs] |> Nx.Batch.concatenate() |> Nx.Batch.key(batch_key) + + {batch, {multi?, pairs}} + end) + |> Nx.Serving.client_postprocessing(fn {scores, _metadata}, {multi?, pairs} -> + results = + Enum.zip_with(Nx.to_list(scores), pairs, fn score, {query, document} -> + %{score: score, query: query, document: document} + end) + + output = %{scores: results} + if multi?, do: output, else: %{scores: hd(results)} + end) + end + + defp format_instruction(task, query, document) do + ": #{task}\n: #{query}\n: #{document}" + end + + defp get_token_id(tokenizer, token) do + encoded = Bumblebee.apply_tokenizer(tokenizer, token) + Nx.to_flat_list(encoded["input_ids"]) |> hd() + end + + defp validate_reranking_input!(input) do + case input do + {query, doc} when is_binary(query) and is_binary(doc) -> + {[{query, doc}], false} + + list when is_list(list) -> + pairs = + Enum.map(list, fn + {query, doc} when is_binary(query) and is_binary(doc) -> + {query, doc} + + other -> + raise ArgumentError, + "expected a query-document tuple {query, doc} where both are strings, got: #{inspect(other)}" + end) + + {pairs, true} + + other -> + raise ArgumentError, + "expected a query-document tuple {query, doc} or a list of such tuples, got: #{inspect(other)}" + end + end +end diff --git a/mix.lock b/mix.lock index 4e216574..8c977c37 100644 --- a/mix.lock +++ b/mix.lock @@ -1,37 +1,37 @@ %{ "axon": {:hex, :axon, "0.7.0", "2e2c6d93b4afcfa812566b8922204fa022b60081e86ebd411df4db7ea30f5457", [:mix], [{:kino, "~> 0.7", [hex: :kino, repo: "hexpm", optional: true]}, {:kino_vega_lite, "~> 0.1.7", [hex: :kino_vega_lite, repo: "hexpm", optional: true]}, {:nx, "~> 0.9", [hex: :nx, repo: "hexpm", optional: false]}, {:polaris, "~> 0.1", [hex: :polaris, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1.1", [hex: :table_rex, repo: "hexpm", optional: true]}], "hexpm", "ee9857a143c9486597ceff434e6ca833dc1241be6158b01025b8217757ed1036"}, "bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"}, - "castore": {:hex, :castore, "1.0.14", "4582dd7d630b48cf5e1ca8d3d42494db51e406b7ba704e81fbd401866366896a", [:mix], [], "hexpm", "7bc1b65249d31701393edaaac18ec8398d8974d52c647b7904d01b964137b9f4"}, - "cc_precompiler": {:hex, :cc_precompiler, "0.1.10", "47c9c08d8869cf09b41da36538f62bc1abd3e19e41701c2cea2675b53c704258", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "f6e046254e53cd6b41c6bacd70ae728011aa82b2742a80d6e2214855c6e06b22"}, + "castore": {:hex, :castore, "1.0.15", "8aa930c890fe18b6fe0a0cff27b27d0d4d231867897bd23ea772dee561f032a3", [:mix], [], "hexpm", "96ce4c69d7d5d7a0761420ef743e2f4096253931a3ba69e5ff8ef1844fe446d3"}, + "cc_precompiler": {:hex, :cc_precompiler, "0.1.11", "8c844d0b9fb98a3edea067f94f616b3f6b29b959b6b3bf25fee94ffe34364768", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "3427232caf0835f94680e5bcf082408a70b48ad68a5f5c0b02a3bea9f3a075b9"}, "complex": {:hex, :complex, "0.6.0", "b0130086a7a8c33574d293b2e0e250f4685580418eac52a5658a4bd148f3ccf1", [:mix], [], "hexpm", "0a5fa95580dcaf30fcd60fe1aaf24327c0fe401e98c24d892e172e79498269f9"}, - "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, + "cowboy": {:hex, :cowboy, "2.14.1", "031d338393e5a128a7de9613b4a0558aabc31b07082004abecb27cac790f5cd6", [:make, :rebar3], [{:cowlib, ">= 2.16.0 and < 3.0.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, ">= 1.8.0 and < 3.0.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "e5310d5afd478ba90b1fed4fcdbc0230082b4510009505c586725c30b44e356f"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, - "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, - "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, - "earmark_parser": {:hex, :earmark_parser, "1.4.41", "ab34711c9dc6212dda44fcd20ecb87ac3f3fce6f0ca2f28d4a00e4154f8cd599", [:mix], [], "hexpm", "a81a04c7e34b6617c2792e291b5a2e57ab316365c2644ddc553bb9ed863ebefa"}, + "cowlib": {:hex, :cowlib, "2.16.0", "54592074ebbbb92ee4746c8a8846e5605052f29309d3a873468d76cdf932076f", [:make, :rebar3], [], "hexpm", "7f478d80d66b747344f0ea7708c187645cfcc08b11aa424632f78e25bf05db51"}, + "decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"}, "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, - "ex_doc": {:hex, :ex_doc, "0.34.2", "13eedf3844ccdce25cfd837b99bea9ad92c4e511233199440488d217c92571e8", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "5ce5f16b41208a50106afed3de6a2ed34f4acfd65715b82a0b84b49d995f95c1"}, + "ex_doc": {:hex, :ex_doc, "0.38.4", "ab48dff7a8af84226bf23baddcdda329f467255d924380a0cf0cee97bb9a9ede", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "f7b62346408a83911c2580154e35613eb314e0278aeea72ed7fedef9c1f165b2"}, "exla": {:hex, :exla, "0.10.0", "93e7d75a774fbc06ce05b96de20c4b01bda413b315238cb3c727c09a05d2bc3a", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:fine, "~> 0.1.0", [hex: :fine, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:nx, "~> 0.10.0", [hex: :nx, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:xla, "~> 0.9.0", [hex: :xla, repo: "hexpm", optional: false]}], "hexpm", "16fffdb64667d7f0a3bc683fdcd2792b143a9b345e4b1f1d5cd50330c63d8119"}, - "fine": {:hex, :fine, "0.1.1", "df2ce44e438bed0061627e10c470873c69374ee7390a51bc612c2358ad37d556", [:mix], [], "hexpm", "41335526b82cf2c196d2588cd54d4504480e2e6ead24f2c07ae0c1cf40af61e5"}, + "fine": {:hex, :fine, "0.1.4", "b19a89c1476c7c57afb5f9314aed5960b5bc95d5277de4cb5ee8e1d1616ce379", [:mix], [], "hexpm", "be3324cc454a42d80951cf6023b9954e9ff27c6daa255483b3e8d608670303f5"}, "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, - "makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"}, - "makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"}, - "makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"}, - "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"}, - "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, + "makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"}, + "makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"}, + "makeup_erlang": {:hex, :makeup_erlang, "1.0.2", "03e1804074b3aa64d5fad7aa64601ed0fb395337b982d9bcf04029d68d51b6a7", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "af33ff7ef368d5893e4a267933e7744e46ce3cf1f61e2dccf53a111ed3aa3727"}, + "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, + "nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"}, "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, "nx": {:hex, :nx, "0.10.0", "128e4a094cb790f663e20e1334b127c1f2a4df54edfb8b13c22757ec33133b4f", [:mix], [{:complex, "~> 0.6", [hex: :complex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3db8892c124aeee091df0e6fbf8e5bf1b81f502eb0d4f5ba63e6378ebcae7da4"}, "nx_image": {:hex, :nx_image, "0.1.2", "0c6e3453c1dc30fc80c723a54861204304cebc8a89ed3b806b972c73ee5d119d", [:mix], [{:nx, "~> 0.4", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "9161863c42405ddccb6dbbbeae078ad23e30201509cc804b3b3a7c9e98764b81"}, "nx_signal": {:hex, :nx_signal, "0.2.0", "e1ca0318877b17c81ce8906329f5125f1e2361e4c4235a5baac8a95ee88ea98e", [:mix], [{:nx, "~> 0.6", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "7247e5e18a177a59c4cb5355952900c62fdeadeb2bad02a9a34237b68744e2bb"}, - "plug": {:hex, :plug, "1.14.2", "cff7d4ec45b4ae176a227acd94a7ab536d9b37b942c8e8fa6dfc0fff98ff4d80", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "842fc50187e13cf4ac3b253d47d9474ed6c296a8732752835ce4a86acdf68d13"}, - "plug_cowboy": {:hex, :plug_cowboy, "2.6.1", "9a3bbfceeb65eff5f39dab529e5cd79137ac36e913c02067dba3963a26efe9b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "de36e1a21f451a18b790f37765db198075c25875c64834bcc82d90b309eb6613"}, - "plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"}, + "plug": {:hex, :plug, "1.18.1", "5067f26f7745b7e31bc3368bc1a2b818b9779faa959b49c934c17730efc911cf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "57a57db70df2b422b564437d2d33cf8d33cd16339c1edb190cd11b1a3a546cc2"}, + "plug_cowboy": {:hex, :plug_cowboy, "2.7.4", "729c752d17cf364e2b8da5bdb34fb5804f56251e88bb602aff48ae0bd8673d11", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "9b85632bd7012615bae0a5d70084deb1b25d2bcbb32cab82d1e9a1e023168aa3"}, + "plug_crypto": {:hex, :plug_crypto, "2.1.1", "19bda8184399cb24afa10be734f84a16ea0a2bc65054e23a62bb10f06bc89491", [:mix], [], "hexpm", "6470bce6ffe41c8bd497612ffde1a7e4af67f36a15eea5f921af71cf3e11247c"}, "polaris": {:hex, :polaris, "0.1.0", "dca61b18e3e801ecdae6ac9f0eca5f19792b44a5cb4b8d63db50fc40fc038d22", [:mix], [{:nx, "~> 0.5", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "13ef2b166650e533cb24b10e2f3b8ab4f2f449ba4d63156e8c569527f206e2c2"}, "progress_bar": {:hex, :progress_bar, "3.0.0", "f54ff038c2ac540cfbb4c2bfe97c75e7116ead044f3c2b10c9f212452194b5cd", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "6981c2b25ab24aecc91a2dc46623658e1399c21a2ae24db986b90d678530f2b7"}, - "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, - "rustler_precompiled": {:hex, :rustler_precompiled, "0.8.2", "5f25cbe220a8fac3e7ad62e6f950fcdca5a5a5f8501835d2823e8c74bf4268d5", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "63d1bd5f8e23096d1ff851839923162096364bac8656a4a3c00d1fff8e83ee0a"}, + "ranch": {:hex, :ranch, "1.8.1", "208169e65292ac5d333d6cdbad49388c1ae198136e4697ae2f474697140f201c", [:make, :rebar3], [], "hexpm", "aed58910f4e21deea992a67bf51632b6d60114895eb03bb392bb733064594dd0"}, + "rustler_precompiled": {:hex, :rustler_precompiled, "0.8.3", "4e741024b0b097fe783add06e53ae9a6f23ddc78df1010f215df0c02915ef5a8", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "c23f5f33cb6608542de4d04faf0f0291458c352a4648e4d28d17ee1098cddcc4"}, "safetensors": {:hex, :safetensors, "0.1.3", "7ff3c22391e213289c713898481d492c9c28a49ab1d0705b72630fb8360426b2", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nx, "~> 0.5", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "fe50b53ea59fde4e723dd1a2e31cfdc6013e69343afac84c6be86d6d7c562c14"}, - "stb_image": {:hex, :stb_image, "0.6.9", "89e77998d4e6d5e2d05ab2d8b5a02e1e8df7bdf04c9dfb063f8b76b2c5870e1f", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.8", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:kino, "~> 0.7", [hex: :kino, repo: "hexpm", optional: true]}, {:nx, "~> 0.4", [hex: :nx, repo: "hexpm", optional: true]}], "hexpm", "02981332167659a0b99f7dfa71fdc9ba3bc3a9200e4952f36fe9ba2a2753f4fa"}, + "stb_image": {:hex, :stb_image, "0.6.10", "76975279e2a130f53dc670bf6f6b1cdc4fbd7ab6293053e88e7fb6a7eae0e836", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.8", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:kino, "~> 0.7", [hex: :kino, repo: "hexpm", optional: true]}, {:nx, "~> 0.4", [hex: :nx, repo: "hexpm", optional: true]}], "hexpm", "26125372cfeda209084d3670417fab6819cfccd0e66c657678ecc48314369e8d"}, "telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"}, "tokenizers": {:hex, :tokenizers, "0.5.1", "b0975d92b4ee5b18e8f47b5d65b9d5f1e583d9130189b1a2620401af4e7d4b35", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, ">= 0.0.0", [hex: :rustler, repo: "hexpm", optional: true]}, {:rustler_precompiled, "~> 0.6", [hex: :rustler_precompiled, repo: "hexpm", optional: false]}], "hexpm", "5f08d97cc7f2ed3d71d370d68120da6d3de010948ccf676c9c0eb591ba4bacc9"}, "torchx": {:hex, :torchx, "0.10.0", "81e583507cdb2bfca9fce0ab2f43da4505f19374bd9898d0cd50feec56b9035e", [:mix], [{:nx, "~> 0.10.0", [hex: :nx, repo: "hexpm", optional: false]}], "hexpm", "557f886c828b4d8d88307de076493e61c6152d4da5b3680e1874688bc7245610"}, diff --git a/notebooks/qwen3.livemd b/notebooks/qwen3.livemd new file mode 100644 index 00000000..50b82093 --- /dev/null +++ b/notebooks/qwen3.livemd @@ -0,0 +1,223 @@ +# Qwen3 + +```elixir +Mix.install([ + {:bumblebee, "~> 0.6.0"}, + {:nx, "~> 0.10.0"}, + {:exla, "~> 0.10.0"}, + {:kino, "~> 0.14.0"} +]) + +Nx.global_default_backend({EXLA.Backend, client: :host}) +``` + +## Introduction + +In this notebook we explore the [Qwen3](https://qwenlm.github.io/blog/qwen3/) model family from Alibaba Cloud. Qwen3 is a series of large language models that includes: + +* **Text Generation** - Instruction-tuned models for conversational AI +* **Embeddings** - Dense vector representations for semantic search +* **Rerankers** - Models to rerank search results for better relevance + + + +## Text Generation + +Let's start with the Qwen3 instruction model for conversational text generation. + +```elixir +repo = {:hf, "Qwen/Qwen3-4B-Instruct-2507"} + +{:ok, model_info} = Bumblebee.load_model(repo, type: :bf16, backend: EXLA.Backend) +{:ok, tokenizer} = Bumblebee.load_tokenizer(repo) +{:ok, generation_config} = Bumblebee.load_generation_config(repo) + +:ok +``` + +Configure the generation parameters and create a serving: + +```elixir +generation_config = + Bumblebee.configure(generation_config, + max_new_tokens: 256, + strategy: %{type: :multinomial_sampling, top_p: 0.8, top_k: 20, temperature: 0.7} + ) + +serving = + Bumblebee.Text.generation(model_info, tokenizer, generation_config, + compile: [batch_size: 1, sequence_length: 1024], + stream: true, + defn_options: [compiler: EXLA] + ) + +# Should be supervised +Kino.start_child({Nx.Serving, name: Qwen3, serving: serving}) +``` + +Create an input field and test the model: + +```elixir +user_input = Kino.Input.textarea("User prompt", default: "Explain quantum computing in simple terms") +``` + +```elixir +user = Kino.Input.read(user_input) + +# Qwen3 uses the <|im_start|> and <|im_end|> chat template format +prompt = """ +<|im_start|>system +You are a helpful assistant.<|im_end|> +<|im_start|>user +#{user}<|im_end|> +<|im_start|>assistant +""" + +Nx.Serving.batched_run(Qwen3, prompt) |> Enum.each(&IO.write/1) +``` + + + +## Embeddings + +Qwen3 embedding models convert text into dense vector representations, useful for semantic search and similarity tasks. + +```elixir +repo = {:hf, "Qwen/Qwen3-Embedding-0.6B"} + +{:ok, model_info} = Bumblebee.load_model(repo, type: :f32, backend: EXLA.Backend, architecture: :for_embedding) +{:ok, tokenizer} = Bumblebee.load_tokenizer(repo) + +serving = + Bumblebee.Text.TextEmbedding.text_embedding(model_info, tokenizer, + output_attribute: :embedding, + embedding_processor: :l2_norm, + compile: [batch_size: 2, sequence_length: 512], + defn_options: [compiler: EXLA] + ) + +Kino.start_child({Nx.Serving, name: Qwen3Embedding, serving: serving}) +``` + +Test the embedding model with some example texts. The Qwen3 embedding model uses an instruction format for better results: + +```elixir +query = "animals" + +texts = [ + "The quick brown fox jumps over the lazy dog", + "A fast auburn canine leaps above an idle hound", + "Python is a programming language" +] + +# Format texts with instruction prefix for Qwen3 embeddings +# Format: "Instruct: Given a query, retrieve relevant documents\nQuery: {query}\n{text}" +formatted_texts = + texts + |> Enum.map(fn text -> + "Instruct: Given a query, retrieve relevant documents\nQuery: #{query}\n#{text}" + end) + +# Get embeddings for all texts +embeddings = + formatted_texts + |> Enum.zip(texts) + |> Enum.map(fn {formatted_text, original_text} -> + %{embedding: embedding} = Nx.Serving.batched_run(Qwen3Embedding, formatted_text) + {original_text, embedding} + end) + +# Calculate cosine similarity between first two texts (similar meaning) +[{text1, emb1}, {text2, emb2}, {text3, emb3}] = embeddings + +similarity_1_2 = + Nx.dot(emb1, emb2) + |> Nx.to_number() + |> then(&Float.round(&1, 4)) + +similarity_1_3 = + Nx.dot(emb1, emb3) + |> Nx.to_number() + |> then(&Float.round(&1, 4)) + +IO.puts("Text 1: #{text1}") +IO.puts("Text 2: #{text2}") +IO.puts("Similarity: #{similarity_1_2}\n") + +IO.puts("Text 1: #{text1}") +IO.puts("Text 3: #{text3}") +IO.puts("Similarity: #{similarity_1_3}") +``` + +As expected, texts with similar meanings (sentences 1 and 2) have higher cosine similarity than texts with different meanings (sentences 1 and 3). + + + +## Reranking + +Reranking models take a query and a list of candidate documents, then score how relevant each document is to the query. This is useful for improving search results. + +```elixir +repo = {:hf, "Qwen/Qwen3-Reranker-0.6B"} + +{:ok, model_info} = + Bumblebee.load_model(repo, type: :f32, backend: EXLA.Backend, architecture: :for_reranker) + +{:ok, tokenizer} = Bumblebee.load_tokenizer(repo) + +serving = + Bumblebee.Text.text_reranking(model_info, tokenizer, + compile: [batch_size: 4, sequence_length: 512], + defn_options: [compiler: EXLA] + ) + +Kino.start_child({Nx.Serving, name: Qwen3Reranker, serving: serving}) +``` + +Test the reranker with a query and multiple candidate documents: + +```elixir +query = "What is machine learning?" + +documents = [ + "Machine learning is a subset of artificial intelligence that enables computers to learn from data.", + "The weather today is sunny with a high of 75 degrees.", + "Deep learning uses neural networks with multiple layers to learn complex patterns.", + "My favorite color is blue and I enjoy long walks on the beach." +] + +# Create query-document pairs +pairs = Enum.map(documents, fn doc -> {query, doc} end) + +# Get relevance scores +%{scores: results} = Nx.Serving.batched_run(Qwen3Reranker, pairs) + +# Sort by score descending +results = + results + |> Enum.sort_by(& &1.score, :desc) + |> Enum.map(fn result -> + {Float.round(result.score, 4), result.document} + end) + +IO.puts("Query: #{query}\n") +IO.puts("Ranked documents by relevance:\n") + +results +|> Enum.with_index(1) +|> Enum.each(fn {{score, doc}, idx} -> + IO.puts("#{idx}. [Score: #{score}] #{doc}") +end) +``` + +The reranker correctly identifies that documents about machine learning and deep learning are most relevant to the query, while the unrelated documents receive lower scores. + +## Summary + +This notebook demonstrated three key capabilities of the Qwen3 model family: + +1. **Text Generation** - Conversational AI using instruction-tuned models +2. **Embeddings** - Creating semantic vector representations for similarity search +3. **Reranking** - Scoring and ranking documents by relevance to a query + +All three models work seamlessly with Bumblebee and can be used for various NLP applications. diff --git a/test/bumblebee/text/qwen3_test.exs b/test/bumblebee/text/qwen3_test.exs new file mode 100644 index 00000000..44857869 --- /dev/null +++ b/test/bumblebee/text/qwen3_test.exs @@ -0,0 +1,107 @@ +defmodule Bumblebee.Text.Qwen3Test do + use ExUnit.Case, async: false + + import Bumblebee.TestHelpers + + @moduletag model_test_tags() + + test ":base" do + assert {:ok, %{model: model, params: params, spec: spec}} = + Bumblebee.load_model({:hf, "tiny-random/qwen3"}, architecture: :base) + + assert %Bumblebee.Text.Qwen3{architecture: :base} = spec + assert spec.use_qk_norm == true + + 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, 64} + + assert_all_close( + outputs.hidden_state[[.., 1..3, 1..3]], + Nx.tensor([ + [ + [0.0437, -0.0292, 0.6567], + [-0.0767, 0.0107, 0.2657], + [0.4693, -0.0452, 0.2521] + ] + ]), + atol: 1.0e-3 + ) + end + + test ":for_causal_language_modeling" do + assert {:ok, %{model: model, params: params, spec: spec}} = + Bumblebee.load_model({:hf, "tiny-random/qwen3"}) + + assert %Bumblebee.Text.Qwen3{architecture: :for_causal_language_modeling} = spec + assert spec.use_qk_norm == true + + 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.logits) == {1, 10, 151936} + + assert_all_close( + outputs.logits[[.., 1..3, 1..3]], + Nx.tensor([ + [ + [2.5975, 3.9118, -0.7135], + [1.8620, 0.6854, 2.3352], + [0.9874, -4.0238, -0.1917] + ] + ]), + atol: 1.0e-3 + ) + end + + test ":for_sequence_classification" do + assert {:ok, %{model: model, params: params, spec: spec}} = + Bumblebee.load_model({:hf, "tiny-random/qwen3"}, + architecture: :for_sequence_classification + ) + + assert %Bumblebee.Text.Qwen3{architecture: :for_sequence_classification} = 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) + + # Note: tiny-random model is missing sequence_classification_head parameters, + # so it uses random initialization. We only verify the shape is correct. + assert Nx.shape(outputs.logits) == {1, 2} + end + + test ":for_embedding" do + assert {:ok, %{model: model, params: params, spec: spec}} = + Bumblebee.load_model({:hf, "tiny-random/qwen3"}, architecture: :for_embedding) + + assert %Bumblebee.Text.Qwen3{architecture: :for_embedding} = 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.embedding) == {1, 64} + + assert_all_close( + outputs.embedding[[.., 1..3]], + Nx.tensor([[0.2217, -0.0037, -0.1757]]), + atol: 1.0e-3 + ) + end +end