-
-
Notifications
You must be signed in to change notification settings - Fork 11k
[Feature] Add support for naver/splade-v3 (BERT-based sparse embedding model) #26339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DarkLight1337
merged 10 commits into
vllm-project:main
from
gjgjos:feat/splade-sparse-embedding
Oct 12, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3827c27
feat(models): add SPLADE sparse embedding model and register in registry
gjgjos 657860b
Add SPLADE sparse embedding model and tests- Removed unnecessary torc…
gjgjos 693c658
fix(bert): v1 SPLADE pooler (prompt_lens/split, valid_mask)
gjgjos 415137d
Update vllm/model_executor/models/bert.py
gjgjos 0c22312
Update vllm/model_executor/models/bert.py
gjgjos 3276ca4
Update vllm/model_executor/models/bert.py
gjgjos b220766
Merge branch 'main' into feat/splade-sparse-embedding
gjgjos 706a735
feat(bert): simplify load_weights
gjgjos 06392ab
Merge branch 'vllm-project:main' into feat/splade-sparse-embedding
gjgjos 893b570
feat: refactor SPLADESparsePooler to remove pad/mask & per-seq pooling
gjgjos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
tests/models/language/pooling/test_splade_sparse_pooler.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import types | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| from vllm.model_executor.models.bert import ( | ||
| BertMLMHead, | ||
| SPLADESparsePooler, | ||
| ) | ||
|
|
||
| # --------------------------------------------------------------------- | ||
| # 1) Functional test: SPLADE formula correctness (no HF download needed) | ||
| # --------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("B,T,H,V", [(2, 3, 5, 7)]) | ||
| def test_splade_pooler_matches_reference_formula(B, T, H, V): | ||
| """Ensure SPLADESparsePooler forward() matches the mathematical formula: | ||
| log1p(relu(logits)) -> max over sequence length (after masking).""" | ||
| torch.manual_seed(0) | ||
|
|
||
| # Prepare [B] sequences of shape [T, H] | ||
| hs_list = [torch.randn(T, H) for _ in range(B)] | ||
|
|
||
| # Simulate PoolingMetadata (only required fields) | ||
| prompt_lens = [T, T - 1] | ||
| token_ids = torch.tensor( | ||
| [ | ||
| [101, 5, 102], # Batch 0: [CLS], token, [SEP] | ||
| [101, 6, 6], # Batch 1: [CLS], token, token (last token ignored) | ||
| ], | ||
| dtype=torch.long, | ||
| ) | ||
| meta = types.SimpleNamespace(prompt_lens=prompt_lens, prompt_token_ids=token_ids) | ||
|
|
||
| # MLM head (prefer BertMLMHead, fallback to Linear if unavailable) | ||
| try: | ||
| mlm_head = BertMLMHead(hidden_size=H, vocab_size=V, layer_norm_eps=1e-12) | ||
| except Exception: | ||
| mlm_head = nn.Linear(H, V, bias=True) | ||
|
|
||
| # Forward pass through SPLADE pooler | ||
| pooler = SPLADESparsePooler(mlm_head=mlm_head, pooling="max", remove_cls_sep=True) | ||
| pooled = pooler(hidden_states=hs_list, pooling_metadata=meta) # list of [V] | ||
|
|
||
| # Basic output checks | ||
| assert isinstance(pooled, list) and len(pooled) == B | ||
| for vec in pooled: | ||
| assert vec.shape == (V,) | ||
| assert torch.isfinite(vec).all() | ||
| assert (vec >= 0).all(), "SPLADE outputs must be non-negative." | ||
|
|
||
| # Reference implementation for comparison | ||
| def ref_one(hs: torch.Tensor, L: int, tid_row: torch.Tensor) -> torch.Tensor: | ||
| keep = torch.ones(L, dtype=torch.bool) | ||
| if L > 0 and tid_row[0].item() == 101: # remove CLS | ||
| keep[0] = False | ||
| if L > 0 and tid_row[L - 1].item() == 102: # remove SEP | ||
| keep[L - 1] = False | ||
|
|
||
| valid = hs[:L][keep[:L]] | ||
| if valid.numel() == 0: | ||
| return torch.zeros(V, dtype=torch.float32) | ||
|
|
||
| logits = mlm_head(valid) # [L', V] | ||
| scores = torch.log1p(torch.relu(logits)) # [L', V] | ||
| return scores.max(dim=0).values.to(torch.float32) | ||
|
|
||
| torch.testing.assert_close( | ||
| pooled[0], | ||
| ref_one(hs_list[0], prompt_lens[0], token_ids[0]), | ||
| rtol=1e-4, | ||
| atol=1e-4, | ||
| ) | ||
| torch.testing.assert_close( | ||
| pooled[1], | ||
| ref_one(hs_list[1], prompt_lens[1], token_ids[1]), | ||
| rtol=1e-4, | ||
| atol=1e-4, | ||
| ) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------- | ||
| # 2) Integration smoke test: end-to-end embedding path wiring | ||
| # --------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @pytest.mark.cpu_model | ||
| def test_bert_splade_sparse_embed_smoke(vllm_runner, monkeypatch): | ||
| """Ensure BertSpladeSparseEmbeddingModel loads and produces sparse embeddings.""" | ||
| from transformers import AutoTokenizer | ||
|
|
||
| MODEL_ID = "hf-internal-testing/tiny-random-bert" | ||
| hf_overrides = {"architectures": ["BertSpladeSparseEmbeddingModel"]} | ||
|
|
||
| # Enforce CPU-only execution (optional) | ||
| monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "") | ||
| monkeypatch.setenv("VLLM_USE_TRITON_FLASH_ATTN", "False") | ||
|
|
||
| tok = AutoTokenizer.from_pretrained(MODEL_ID) | ||
| vocab_size = tok.vocab_size | ||
|
|
||
| # The embed path should route through SPLADESparsePooler | ||
| with vllm_runner( | ||
| MODEL_ID, | ||
| runner="pooling", | ||
| max_model_len=64, | ||
| hf_overrides=hf_overrides, | ||
| ) as vm: | ||
| outs = vm.embed(["hello world", "splade sparse test"]) | ||
|
|
||
| # Basic sanity checks | ||
| assert len(outs) == 2 | ||
| assert outs[0].shape[0] == vocab_size | ||
| assert outs[1].shape[0] == vocab_size | ||
| assert np.isfinite(outs[0]).all() and (outs[0] >= 0).all() | ||
| assert np.isfinite(outs[1]).all() and (outs[1] >= 0).all() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.