Instructions to use JalalKhal/test-api with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use JalalKhal/test-api with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("JalalKhal/test-api", trust_remote_code=True) sentences = [ "That is a happy person", "That is a happy dog", "That is a very happy person", "Today is a sunny day" ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
test api
Browse files- model.safetensors +1 -1
- vllm_configuration_embedder.py +92 -0
- vllm_modeling_embedder.py +107 -0
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 565859400
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0f6e509537aa47a23abdd3cd174dc0a8f4398c603439041181e7ef804deb07f4
|
| 3 |
size 565859400
|
vllm_configuration_embedder.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TYPE_CHECKING
|
| 2 |
+
|
| 3 |
+
if TYPE_CHECKING:
|
| 4 |
+
from vllm.config import VllmConfig # ty: ignore[unresolved-import]
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
from vllm.model_executor.models.config import VerifyAndUpdateConfig # ty: ignore[unresolved-import]
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class EmbedderModelConfig(VerifyAndUpdateConfig):
|
| 11 |
+
@staticmethod
|
| 12 |
+
def verify_and_update_config(vllm_config: "VllmConfig") -> None:
|
| 13 |
+
from copy import deepcopy
|
| 14 |
+
|
| 15 |
+
from vllm.transformers_utils.config import set_default_rope_theta # ty: ignore[unresolved-import]
|
| 16 |
+
|
| 17 |
+
config = vllm_config.model_config.hf_config
|
| 18 |
+
assert config.__class__.__name__ == "EmbedderConfig" # nosec B101
|
| 19 |
+
assert config.activation_function in ["swiglu", "gelu"] # nosec B101
|
| 20 |
+
config.position_embedding_type = getattr(config, "position_embedding_type", "rope")
|
| 21 |
+
|
| 22 |
+
if config.activation_function == "swiglu":
|
| 23 |
+
config.hidden_act = "silu"
|
| 24 |
+
else:
|
| 25 |
+
config.hidden_act = config.activation_function
|
| 26 |
+
|
| 27 |
+
assert config.mlp_fc1_bias == config.mlp_fc2_bias == config.qkv_proj_bias # nosec B101
|
| 28 |
+
config.bias = config.qkv_proj_bias
|
| 29 |
+
|
| 30 |
+
assert config.rotary_emb_scale_base is None # nosec B101
|
| 31 |
+
assert not config.rotary_emb_interleaved # nosec B101
|
| 32 |
+
|
| 33 |
+
config.layer_norm_eps = config.layer_norm_epsilon
|
| 34 |
+
config.intermediate_size = config.n_inner
|
| 35 |
+
config.hidden_size = config.n_embd
|
| 36 |
+
config.num_hidden_layers = config.n_layer
|
| 37 |
+
|
| 38 |
+
head_dim = config.hidden_size // config.num_attention_heads
|
| 39 |
+
rotary_emb_dim = int(head_dim * config.rotary_emb_fraction)
|
| 40 |
+
max_trained_positions = getattr(config, "max_trained_positions", 2048)
|
| 41 |
+
|
| 42 |
+
set_default_rope_theta(config, default_theta=config.rotary_emb_base)
|
| 43 |
+
|
| 44 |
+
config.rotary_kwargs = {
|
| 45 |
+
"head_size": head_dim,
|
| 46 |
+
"rotary_dim": rotary_emb_dim,
|
| 47 |
+
"max_position": max_trained_positions,
|
| 48 |
+
"rope_parameters": config.rope_parameters,
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# we ignore config.rotary_scaling_factor so that for datasets shorter
|
| 52 |
+
# than max_trained_positions 2048, the results are consistent
|
| 53 |
+
# with SentenceTransformer.
|
| 54 |
+
# The context extension uses vllm style rope_theta and rope_parameters.
|
| 55 |
+
# See #17785 #18755
|
| 56 |
+
if not vllm_config.model_config.hf_overrides and vllm_config.model_config.original_max_model_len is None:
|
| 57 |
+
# Default
|
| 58 |
+
# Reset max_model_len to max_trained_positions.
|
| 59 |
+
# nomic-embed-text-v2-moe the length is set to 512
|
| 60 |
+
# by sentence_bert_config.json.
|
| 61 |
+
max_model_len = min(vllm_config.model_config.max_model_len, max_trained_positions) # type: ignore[unreachable]
|
| 62 |
+
|
| 63 |
+
vllm_config.recalculate_max_model_len(max_model_len)
|
| 64 |
+
|
| 65 |
+
else:
|
| 66 |
+
# We need to re-verify max_model_len to avoid lengths
|
| 67 |
+
# greater than position_embedding.
|
| 68 |
+
model_config = vllm_config.model_config
|
| 69 |
+
hf_text_config = model_config.hf_text_config
|
| 70 |
+
|
| 71 |
+
if isinstance(model_config.hf_overrides, dict):
|
| 72 |
+
# hf_overrides_kw
|
| 73 |
+
max_model_len = model_config.hf_overrides.get("max_model_len", vllm_config.model_config.max_model_len)
|
| 74 |
+
else:
|
| 75 |
+
# hf_overrides_fn
|
| 76 |
+
# This might be overridden by sentence_bert_config.json.
|
| 77 |
+
max_model_len = vllm_config.model_config.max_model_len
|
| 78 |
+
|
| 79 |
+
# reset hf_text_config for recalculate_max_model_len.
|
| 80 |
+
if hasattr(hf_text_config, "max_model_len"):
|
| 81 |
+
delattr(hf_text_config, "max_model_len")
|
| 82 |
+
hf_text_config.max_position_embeddings = max_trained_positions
|
| 83 |
+
hf_text_config.rope_parameters = config.rotary_kwargs["rope_parameters"]
|
| 84 |
+
|
| 85 |
+
# The priority of sentence_bert_config.json is higher
|
| 86 |
+
# than max_position_embeddings
|
| 87 |
+
encoder_config = deepcopy(model_config.encoder_config)
|
| 88 |
+
if encoder_config:
|
| 89 |
+
encoder_config.pop("max_seq_length", None)
|
| 90 |
+
model_config.encoder_config = encoder_config
|
| 91 |
+
|
| 92 |
+
vllm_config.recalculate_max_model_len(max_model_len)
|
vllm_modeling_embedder.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 2 |
+
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
| 3 |
+
from typing import cast
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
from vllm.compilation.decorators import support_torch_compile # ty: ignore[unresolved-import]
|
| 9 |
+
from vllm.config import VllmConfig # ty: ignore[unresolved-import]
|
| 10 |
+
from vllm.model_executor.models.bert_with_rope import NomicBertModel # ty: ignore[unresolved-import]
|
| 11 |
+
from vllm.model_executor.models.interfaces_base import default_pooling_type # ty: ignore[unresolved-import]
|
| 12 |
+
from vllm.model_executor.models.utils import WeightsMapper # ty: ignore[unresolved-import]
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class EncoderBlock(nn.Module):
|
| 16 |
+
def __init__(self, dim: int, hidden_dim: int, dropout: float):
|
| 17 |
+
super().__init__()
|
| 18 |
+
self.net = nn.Sequential(
|
| 19 |
+
nn.Linear(dim, hidden_dim),
|
| 20 |
+
nn.ReLU(),
|
| 21 |
+
)
|
| 22 |
+
self.norm = nn.LayerNorm(dim)
|
| 23 |
+
self.dropout = nn.Dropout(dropout)
|
| 24 |
+
self.proj = nn.Linear(hidden_dim, dim)
|
| 25 |
+
|
| 26 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 27 |
+
residual = x
|
| 28 |
+
x = self.net(x)
|
| 29 |
+
x = self.dropout(x)
|
| 30 |
+
x = self.proj(x)
|
| 31 |
+
return cast(torch.Tensor, self.norm(x + residual))
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class Head(nn.Module):
|
| 35 |
+
def __init__(self, dim: int, num_blocks: int = 1, dropout: float = 0):
|
| 36 |
+
super().__init__()
|
| 37 |
+
self.blocks = nn.Sequential(
|
| 38 |
+
*[EncoderBlock(dim=dim, hidden_dim=dim, dropout=dropout) for _ in range(num_blocks)]
|
| 39 |
+
)
|
| 40 |
+
self.proj = nn.Linear(dim, dim)
|
| 41 |
+
|
| 42 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 43 |
+
x = self.blocks(x)
|
| 44 |
+
x = self.proj(x)
|
| 45 |
+
return x
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@support_torch_compile
|
| 49 |
+
@default_pooling_type("CLS")
|
| 50 |
+
class EmbedderModel(nn.Module):
|
| 51 |
+
"""
|
| 52 |
+
vLLM wrapper for HF-trained EmbedderModel
|
| 53 |
+
(encoder + custom graph head)
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
# HF state_dict keys start with "model."
|
| 57 |
+
hf_to_vllm_mapper = WeightsMapper(orig_to_new_prefix={"model.": ""})
|
| 58 |
+
|
| 59 |
+
def __init__(
|
| 60 |
+
self,
|
| 61 |
+
*,
|
| 62 |
+
vllm_config: VllmConfig,
|
| 63 |
+
prefix: str = "",
|
| 64 |
+
):
|
| 65 |
+
super().__init__()
|
| 66 |
+
self.hf_config = vllm_config.model_config.hf_config
|
| 67 |
+
# --------------------------------------------------
|
| 68 |
+
# Base encoder (identical to training)
|
| 69 |
+
# --------------------------------------------------
|
| 70 |
+
self.encoder = NomicBertModel(
|
| 71 |
+
vllm_config=vllm_config,
|
| 72 |
+
prefix=f"{prefix}.encoder",
|
| 73 |
+
add_pooling_layer=False,
|
| 74 |
+
)
|
| 75 |
+
# --------------------------------------------------
|
| 76 |
+
# Custom head (must match HF exactly)
|
| 77 |
+
# --------------------------------------------------
|
| 78 |
+
self.head = Head(
|
| 79 |
+
dim=self.hf_config.hidden_size,
|
| 80 |
+
num_blocks=self.hf_config.num_blocks,
|
| 81 |
+
dropout=self.hf_config.dropout,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor:
|
| 85 |
+
return self.encoder.embed_input_ids(input_ids)
|
| 86 |
+
|
| 87 |
+
def forward(
|
| 88 |
+
self,
|
| 89 |
+
input_ids: torch.Tensor,
|
| 90 |
+
positions: torch.Tensor,
|
| 91 |
+
intermediate_tensors: torch.Tensor | None = None,
|
| 92 |
+
inputs_embeds: torch.Tensor | None = None,
|
| 93 |
+
token_type_ids: torch.Tensor | None = None,
|
| 94 |
+
) -> torch.Tensor:
|
| 95 |
+
# vLLM manages attention & KV internally
|
| 96 |
+
hidden_states = self.encoder(
|
| 97 |
+
input_ids=input_ids,
|
| 98 |
+
positions=positions,
|
| 99 |
+
inputs_embeds=inputs_embeds,
|
| 100 |
+
token_type_ids=token_type_ids,
|
| 101 |
+
)
|
| 102 |
+
emb = hidden_states
|
| 103 |
+
if not self.hf_config.encoder_only:
|
| 104 |
+
# Head + normalize (same as HF)
|
| 105 |
+
emb = self.head(hidden_states)
|
| 106 |
+
emb = F.normalize(emb, dim=-1)
|
| 107 |
+
return emb
|