Integrate with Sentence Transformers

#1
by tomaarsen HF Staff - opened

Hello @wl1982 @yanwj @kevinke !

I've been integrating this model and its 9B sibling into the Sentence Transformers framework, although I've struggled as there's no recommended inference snippet, I can't be sure that it reproduces the expected outputs.

Pull Request overview

  • Add Sentence Transformers support for text, images, video, and interleaved inputs.
  • Document text retrieval and image-text similarity examples.

Details

I've added configuration files so the model can be loaded directly with SentenceTransformer("ATH-MaaS/Ovis-VL-Embedding-2B"). The integration loads the backbone from model/, applies last-token pooling, and L2-normalizes the resulting 2048-dimensional embeddings. Both query and document encoding use Represent the user's input. by default, with support for task-specific instructions.

I've based the inline embedding template on the qwen3_5_emb template selected in model/args.json. It reproduces the native formatting with an empty assistant thinking block and appends <|endoftext|>, following the suffix defined in MS-SWIFT: https://github.com/modelscope/ms-swift/blob/main/swift/template/templates/qwen.py.

I've verified loading and encoding with Sentence Transformers 5.4.0 and Transformers 5.12.1. Float32 checks cover text, images, both image/text orders, and video frames. Compared with a reference using the native processor template plus <|endoftext|>, the maximum embedding difference was 5.7e-7 and the maximum cosine similarity difference was 5.4e-7. These checks validate the integration against that reconstructed reference, but do not independently verify the extra token. I've also run the README example below with the default bfloat16 weights and checked image URLs, local paths, and custom instructions.

Install with pip install "sentence-transformers[image]>=5.4.0" "transformers>=5.12.1":

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("ATH-MaaS/Ovis-VL-Embedding-2B", revision="refs/pr/1")

query = "Which planet is known as the Red Planet?"
documents = [
    "Venus is often called Earth's twin because of its similar size and proximity.",
    "Mars, known for its reddish appearance, is often referred to as the Red Planet.",
    "Jupiter, the largest planet in our solar system, has a prominent red spot.",
    "Saturn, famous for its rings, is sometimes mistaken for the Red Planet.",
]

query_embedding = model.encode_query(query)
document_embeddings = model.encode_document(documents)
print(query_embedding.shape, document_embeddings.shape)
# (2048,) (4, 2048)

print(model.similarity(query_embedding, document_embeddings))
# tensor([[0.6870, 0.8412, 0.7255, 0.7707]])

# And a multimodal example:
text_embeddings = model.encode(["Ovis logo", "A green car", "children playing"])
image_embeddings = model.encode(
    "https://huggingface.co/ATH-MaaS/Ovis-VL-Embedding-2B/resolve/main/figures/ovis_logo.png"
)
print(text_embeddings.shape, image_embeddings.shape)
# (3, 2048) (2048,)

print(model.similarity(image_embeddings, text_embeddings))
# tensor([[0.6588, 0.4354, 0.4712]])

Note that the revision="refs/pr/1" allows you to run this code before even merging it, so you can compare against your reference implementation. Please check and let me know what your implementation produces, so I can make sure it matches.

I think it's such a shame that you've got this excellent model on the benchmarks, but users aren't sure how to use it (correctly).

  • Tom Aarsen
tomaarsen changed pull request status to open
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment