Add Sentence Transformers compatibility

#1
by tomaarsen HF Staff - opened

Hello!

Pull Request overview

  • Add Sentence Transformers compatibility
  • Add ST snippet to README

Details

I noticed that because this model doesn't use the adapter work like the main model, this model can actually be implemented without any trust_remote_code in Sentence Transformers. That might be pretty valuable for users. I just loaded the model with SentenceTransformer, saved it with save_pretrained("tmp"), updated the default pooling config with lasttoken pooling instead, and added a Normalize to the modules.json.

Try it out

from sentence_transformers import SentenceTransformer
import torch

model = SentenceTransformer(
    "jinaai/jina-embeddings-v5-text-small-clustering",
    model_kwargs={"dtype": torch.bfloat16},  # Recommended for GPUs
    config_kwargs={"_attn_implementation": "flash_attention_2"},  # Recommended but optional
    revision="refs/pr/1",
)
# Optional: set truncate_dim in encode() to control embedding size

texts = [
    "We propose a novel neural network architecture for image segmentation.",
    "This paper analyzes the effects of monetary policy on inflation.",
    "Our method achieves state-of-the-art results on object detection benchmarks.",
    "We study the relationship between interest rates and housing prices.",
    "A new attention mechanism is introduced for visual recognition tasks.",
]

# Encode query and documents
embeddings = model.encode(texts)
print(embeddings.shape)
# (5, 1024)

similarity = model.similarity(embeddings, embeddings)
print(similarity)
# tensor([[1.0000, 0.2303, 0.8060, 0.2309, 0.8487],
#         [0.2303, 1.0000, 0.2552, 0.7400, 0.2543],
#         [0.8060, 0.2552, 1.0000, 0.2344, 0.8542],
#         [0.2309, 0.7400, 0.2344, 1.0000, 0.2279],
#         [0.8487, 0.2543, 0.8542, 0.2279, 1.0000]])

Note the revision argument. I got this result with the script:

# tensor([[1.0000, 0.2303, 0.8060, 0.2309, 0.8487],
#         [0.2303, 1.0000, 0.2552, 0.7400, 0.2543],
#         [0.8060, 0.2552, 1.0000, 0.2344, 0.8542],
#         [0.2309, 0.7400, 0.2344, 1.0000, 0.2279],
#         [0.8487, 0.2543, 0.8542, 0.2279, 1.0000]])

Compared to this output when using the original model with the clustering task:

tensor([[1.0000, 0.2291, 0.8063, 0.2307, 0.8483],
        [0.2291, 1.0000, 0.2551, 0.7410, 0.2542],
        [0.8063, 0.2551, 1.0000, 0.2353, 0.8544],
        [0.2307, 0.7410, 0.2353, 1.0000, 0.2286],
        [0.8483, 0.2542, 0.8544, 0.2286, 1.0000]])

I believe the small difference is likely due to the adapter loading vs already being merged into the model.

  • Tom Aarsen
tomaarsen changed pull request status to open

I ran some more tests following my findings from https://huggingface.co/jinaai/jina-embeddings-v5-text-small/discussions/7. Here are the new results using fp32:

Main model using Transformers (fp32):

tensor([[1.0000, 0.2975, 0.8637, 0.3093, 0.9112],
        [0.2975, 1.0000, 0.3258, 0.8044, 0.3199],
        [0.8637, 0.3258, 1.0000, 0.3261, 0.9011],
        [0.3093, 0.8044, 0.3261, 1.0000, 0.3119],
        [0.9112, 0.3199, 0.9011, 0.3119, 1.0000]])

Main model using Sentence Transformers (fp32, after https://huggingface.co/jinaai/jina-embeddings-v5-text-small/discussions/7):

tensor([[1.0000, 0.2975, 0.8637, 0.3093, 0.9112],
        [0.2975, 1.0000, 0.3258, 0.8044, 0.3199],
        [0.8637, 0.3258, 1.0000, 0.3261, 0.9011],
        [0.3093, 0.8044, 0.3261, 1.0000, 0.3119],
        [0.9112, 0.3199, 0.9011, 0.3119, 1.0000]])

This PR (fp32, before updating default_prompt_name)

tensor([[1.0000, 0.2293, 0.8060, 0.2306, 0.8497],
        [0.2293, 1.0000, 0.2544, 0.7417, 0.2549],
        [0.8060, 0.2544, 1.0000, 0.2343, 0.8543],
        [0.2306, 0.7417, 0.2343, 1.0000, 0.2292],
        [0.8497, 0.2549, 0.8543, 0.2292, 1.0000]])

This PR (fp32, after updating default_prompt_name)

tensor([[1.0000, 0.2974, 0.8636, 0.3091, 0.9111],
        [0.2974, 1.0000, 0.3257, 0.8043, 0.3199],
        [0.8636, 0.3257, 1.0000, 0.3260, 0.9009],
        [0.3091, 0.8043, 0.3260, 1.0000, 0.3119],
        [0.9111, 0.3199, 0.9009, 0.3119, 1.0000]])

In short, the results after updating the default_prompt_name are very close, and I think this PR is now correct & ready.

  • Tom Aarsen
michael-guenther changed pull request status to merged

Sign up or log in to comment