Views
No views yet
Qwen3-Embedding-0.6B using bitsandbytes.BitsAndBytesConfig(load_in_8bit=True) following the current recommended Hugging Face API (quantization_config argument).bitsandbytes (bnb)safetensors1import torch
2from torch import Tensor
3import torch.nn.functional as F
4from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
5
6def last_token_pool(last_hidden_states: Tensor,
7 attention_mask: Tensor) -> Tensor:
8 left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
9 if left_padding:
10 return last_hidden_states[:, -1]
11 else:
12 sequence_lengths = attention_mask.sum(dim=1) - 1
13 batch_size = last_hidden_states.shape[0]
14 return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
15
16bnb_config = BitsAndBytesConfig(
17 load_in_4bit=True, # this enables 8-bit quantization
18 llm_int8_threshold=6.0, # defaults; safe values
19 llm_int8_has_fp16_weight=False
20)
21
22model_id = "ManiKumarAdapala/Qwen3-Embedding-0.6B-Q8_0-Safetensors"
23
24tokenizer = AutoTokenizer.from_pretrained(model_id, padding_side='left')
25model = AutoModel.from_pretrained(model_id, quantization_config = bnb_config)
26
27max_length = 1024 #fixed
28
29input_texts = "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun."
30
31# Tokenize the input texts
32batch_dict = tokenizer(
33 input_texts,
34 padding=True,
35 truncation=True,
36 max_length=max_length,
37 return_tensors="pt",
38)
39batch_dict.to(model.device)
40outputs = model(**batch_dict)
41embeddings_ = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
42
43# normalize embeddings
44embeddings = F.normalize(embeddings_, p=2, dim=1)
45
46print(embeddings, len(embeddings[0]))| Model Type | Disk Size | Runtime RAM |
|---|---|---|
| FP16 | ~1.2 GB | ~2.3 GB |
| 8-bit (this model) | ~600 MB | ~0.9–1.1 GB |
@article{qwen3embedding,
title={Qwen3 Embedding: Advancing Text Embedding and Reranking Through Foundation Models},
author={Zhang, Yanzhao and Li, Mingxin and Long, Dingkun and Zhang, Xin and Lin, Huan and Yang, Baosong and Xie, Pengjun and Yang, An and Liu, Dayiheng and Lin, Junyang and Huang, Fei and Zhou, Jingren},
journal={arXiv preprint arXiv:2506.05176},
year={2025}
}