Views
No views yet
1from sentence_transformers import SentenceTransformer
2
3model = SentenceTransformer("codefuse-ai/F2LLM-v2-8B-Preview", device="cuda:0", model_kwargs={"torch_dtype": "bfloat16"})
4
5# Some sample query and documents
6query = "What is F2LLM used for?"
7documents = [
8 'We present F2LLM, a family of fully open embedding LLMs that achieve a strong balance between model size, training data, and embedding performance.',
9 'F2LLM is a model for computing text embeddings that can be used for various NLP tasks such as information retrieval, semantic search, and text classification.',
10 'F2LLM 是 CodeFuse 开源的系列嵌入模型。',
11 'F2LLM — это модель вычисления встраивания текста, которую можно использовать для различных задач НЛП, таких как поиск информации, семантический поиск и классификация текста.'
12]
13
14# Encode the query and documents
15query_embedding = model.encode(query)
16document_embeddings = model.encode(documents)
17print(query_embedding.shape, document_embeddings.shape)
18# (4096,) (4, 4096)
19
20# Compute cosine similarity between the query and documents
21similarity = model.similarity(query_embedding, document_embeddings)
22print(similarity)
23# tensor([[0.6329, 0.8003, 0.6361, 0.8267]])1from transformers import AutoModel, AutoTokenizer
2import torch
3import torch.nn.functional as F
4
5
6model_path = "codefuse-ai/F2LLM-v2-8B-Preview"
7tokenizer = AutoTokenizer.from_pretrained(model_path)
8model = AutoModel.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map={'': 0})
9
10query = "What is F2LLM used for?"
11
12documents = [
13 'We present F2LLM, a family of fully open embedding LLMs that achieve a strong balance between model size, training data, and embedding performance.',
14 'F2LLM is a model for computing text embeddings that can be used for various NLP tasks such as information retrieval, semantic search, and text classification.',
15 'F2LLM 是 CodeFuse 开源的系列嵌入模型。',
16 'F2LLM — это модель вычисления встраивания текста, которую можно использовать для различных задач НЛП, таких как поиск информации, семантический поиск и классификация текста.'
17]
18
19def encode(sentences):
20 batch_size = len(sentences)
21 # the tokenizer will automatically add eos token
22 tokenized_inputs = tokenizer(sentences, padding=True, return_tensors='pt').to(model.device)
23 last_hidden_state = model(**tokenized_inputs).last_hidden_state
24 eos_positions = tokenized_inputs.attention_mask.sum(dim=1) - 1
25 embeddings = last_hidden_state[torch.arange(batch_size, device=model.device), eos_positions]
26 embeddings = F.normalize(embeddings, p=2, dim=1)
27 return embeddings
28
29# Encode the query and documents
30query_embedding = encode([query])
31document_embeddings = encode(documents)
32print(query_embedding.shape, document_embeddings.shape)
33# torch.Size([1, 4096]) torch.Size([4, 4096])
34
35# Compute cosine similarity between the query and documents
36similarity = query_embedding @ document_embeddings.T
37print(similarity)
38# tensor([[0.6328, 0.8008, 0.6328, 0.8242]], device='cuda:0',
39# dtype=torch.bfloat16, grad_fn=<MmBackward0>)intermediate_checkpoints branch.@misc{f2llm-v2,
title={F2LLM-v2: Inclusive, Performant, and Efficient Embeddings for a Multilingual World},
author={Ziyin Zhang and Zihan Liao and Hang Yu and Peng Di and Rui Wang},
year={2026},
eprint={2603.19223},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2603.19223},
}