Views
No views yet
word_embedding layer, it immediately goes through a
Qwen3MLP (approximately 3 fully connected layers), then I calculate the compressed length, and finally use
adaptive_avg_pool1d to compress tokens to that length.1real_length = 1000 # Actual token count of the text
2length_threshold = 80 # Compress only if exceeding this threshold
3compression_ratio = 0.333
4if real_length <= length_threshold:
5 # No compression
6 pass
7else:
8 target_length = int(length_threshold + (real_length - length_threshold) * compression_ratio)modeling_qwen3_jasper.py file in this directory.1import torch
2from sentence_transformers import SentenceTransformer
3
4if __name__ == "__main__":
5 model_name_or_path = "infgrad/Jasper-Token-Compression-600M"
6 model = SentenceTransformer(
7 model_name_or_path,
8 model_kwargs={
9 "torch_dtype": torch.bfloat16,
10 "attn_implementation": "sdpa", # We support flash_attention_2; sdpa; eager
11 "trust_remote_code": True
12 },
13 trust_remote_code=True,
14 tokenizer_kwargs={"padding_side": "left"},
15 device="cpu",
16 )
17
18 queries = [
19 "What is photosynthesis?",
20 "Who invented the telephone?",
21 ]
22 documents = [
23 "Photosynthesis is the process by which green plants use sunlight, carbon dioxide, and water to produce glucose and oxygen",
24 "Alexander Graham Bell is credited with inventing the first practical telephone in 1876, receiving US patent number 174,465 for his device."
25 ]
26 # The smaller the compression_ratio parameter, the faster the speed, but the quality will correspondingly decrease.
27 # Based on our parameter settings during training and test results, we recommend a range between 0.3-0.8.
28 query_embeddings = model.encode(queries, prompt_name="query", normalize_embeddings=True, compression_ratio=0.3333)
29 document_embeddings = model.encode(documents, normalize_embeddings=True, compression_ratio=0.3333)
30
31 similarity = model.similarity(query_embeddings, document_embeddings)
32 print(similarity)
33
@misc{zhang2025jasperstelladistillationsota,
title={Jasper and Stella: distillation of SOTA embedding models},
author={Dun Zhang and Jiacheng Li and Ziyang Zeng and Fulong Wang},
year={2025},
eprint={2412.19048},
archivePrefix={arXiv},
primaryClass={cs.IR},
url={https://arxiv.org/abs/2412.19048},
}
@misc{zhang2025jaspertokencompression600mtechnicalreport,
title={Jasper-Token-Compression-600M Technical Report},
author={Dun Zhang and Ziyang Zeng and Yudong Zhou and Shuyang Lu},
year={2025},
eprint={2511.14405},
archivePrefix={arXiv},
primaryClass={cs.IR},
url={https://arxiv.org/abs/2511.14405},
}