LHC88 bowang0911 commited on
Commit
8753d53
·
0 Parent(s):

Duplicate from perplexity-ai/pplx-embed-v1-4b

Browse files

Co-authored-by: Bo Wang <bowang0911@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ assets/diag.png filter=lfs diff=lfs merge=lfs -text
37
+ onnx/model.onnx_data filter=lfs diff=lfs merge=lfs -text
38
+ onnx/model.onnx_data_1 filter=lfs diff=lfs merge=lfs -text
39
+ onnx/model.onnx_data_2 filter=lfs diff=lfs merge=lfs -text
40
+ onnx/model.onnx_data_3 filter=lfs diff=lfs merge=lfs -text
41
+ onnx/model.onnx_data_4 filter=lfs diff=lfs merge=lfs -text
42
+ onnx/model.onnx_data_5 filter=lfs diff=lfs merge=lfs -text
43
+ onnx/model.onnx_data_6 filter=lfs diff=lfs merge=lfs -text
44
+ onnx/model.onnx_data_7 filter=lfs diff=lfs merge=lfs -text
1_Pooling/config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "word_embedding_dimension": 2560,
3
+ "pooling_mode_cls_token": false,
4
+ "pooling_mode_mean_tokens": true,
5
+ "pooling_mode_max_tokens": false,
6
+ "pooling_mode_mean_sqrt_len_tokens": false,
7
+ "pooling_mode_weightedmean_tokens": false,
8
+ "pooling_mode_lasttoken": false
9
+ }
README.md ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ pipeline_tag: feature-extraction
4
+ tags:
5
+ - feature-extraction
6
+ - sentence-similarity
7
+ - mteb
8
+ - sentence-transformers
9
+ language:
10
+ - multilingual
11
+ ---
12
+
13
+
14
+ <p align="center">
15
+ <img src="assets/logo.svg" alt="Perplexity Logo" width="400">
16
+ </p>
17
+
18
+ <p align="center">pplx-embed-v1: Diffusion-Pretrained Dense and Contextual Embeddings</p>
19
+
20
+ `pplx-embed-v1` and `pplx-embed-context-v1` are state-of-the-art text embedding models optimized for real-world, web-scale retrieval tasks.
21
+
22
+ - Use **`pplx-embed-v1`** for independent text embedding (queries, documents, semantic search)
23
+ - Use **`pplx-embed-context-v1`** for document chunks in RAG systems where surrounding context matters
24
+
25
+ > [!IMPORTANT]
26
+ > `pplx-embed-v1` and `pplx-embed-context-v1` natively produce *unnormalized* int8-quantized embeddings. Ensure that you compare them via *cosine similarity*.
27
+
28
+
29
+ ![diag.png](assets/diag.png)
30
+
31
+ ## Models
32
+
33
+ | Model | Dimensions | Context | MRL | Quantization | Instruction | Pooling |
34
+ |:-----:|:----------:|:-------:|:---:|:------------:|:-----------:|:-------:|
35
+ | `pplx-embed-v1-0.6B` | 1024 | 32K | Yes | INT8/BINARY | No | Mean |
36
+ | `pplx-embed-v1-4B` | 2560 | 32K | Yes | INT8/BINARY | No | Mean |
37
+ | `pplx-embed-context-v1-0.6B` | 1024 | 32K | Yes | INT8/BINARY | No | Mean |
38
+ | `pplx-embed-context-v1-4B` | 2560 | 32K | Yes | INT8/BINARY | No | Mean |
39
+
40
+ <sub>All models are built on diffusion continued pre-trained Qwen3 at Perplexity AI.</sub>
41
+
42
+ <sub>Many modern embedding models rely on instruction tuning, where users prepend an instruction string to the text being embedded. This can yield a 2%-3% lift on benchmarks, but it also introduces prompt-selection overhead and can make indexing pipelines brittle (small instruction changes can shift embedding space). We deliberately **avoid** this requirement: you can embed the text you want to index directly, without having to choose or maintain an instruction prefix.</sub>
43
+
44
+ ## Usage
45
+
46
+ <details>
47
+ <summary>Via API</summary>
48
+
49
+ ```bash
50
+ curl -X POST https://api.perplexity.ai/v1/embeddings \
51
+ -H "Authorization: Bearer YOUR_API_KEY" \
52
+ -H "Content-Type: application/json" \
53
+ -d '{
54
+ "input": [
55
+ "Scientists explore the universe driven by curiosity.",
56
+ "Children learn through curious exploration.",
57
+ "Historical discoveries began with curious questions.",
58
+ "Animals use curiosity to adapt and survive.",
59
+ "Philosophy examines the nature of curiosity."
60
+ ],
61
+ "model": "pplx-embed-v1-4b"
62
+ }'
63
+ ```
64
+
65
+ </details>
66
+
67
+
68
+ <details>
69
+ <summary>Using SentenceTransformers</summary>
70
+
71
+ ```python
72
+ from sentence_transformers import SentenceTransformer
73
+
74
+ model = SentenceTransformer(
75
+ "perplexity-ai/pplx-embed-v1-4B",
76
+ trust_remote_code=True
77
+ )
78
+
79
+ texts = [
80
+ "Scientists explore the universe driven by curiosity.",
81
+ "Children learn through curious exploration.",
82
+ "Historical discoveries began with curious questions.",
83
+ "Animals use curiosity to adapt and survive.",
84
+ "Philosophy examines the nature of curiosity.",
85
+ ]
86
+
87
+ embeddings = model.encode(texts) # Shape: (5, 2560), quantized to int8
88
+ embeddings = model.encode(texts, quantization="binary") # Shape: (5, 2560), quantized to binary
89
+ ```
90
+
91
+ </details>
92
+
93
+ <details>
94
+ <summary> Using ONNX models </summary>
95
+
96
+ ```python
97
+
98
+ import onnxruntime as ort
99
+ from transformers import AutoTokenizer
100
+ import numpy as np
101
+
102
+ tokenizer = AutoTokenizer.from_pretrained("perplexity-ai/pplx-embed-v1-4b", trust_remote_code=True)
103
+ session = ort.InferenceSession("onnx/model.onnx")
104
+
105
+
106
+ texts = [
107
+ "Scientists explore the universe driven by curiosity.",
108
+ "Children learn through curious exploration.",
109
+ "Historical discoveries began with curious questions.",
110
+ "Animals use curiosity to adapt and survive.",
111
+ "Philosophy examines the nature of curiosity.",
112
+ ]
113
+
114
+ tokenized = tokenizer(
115
+ texts,
116
+ padding=True,
117
+ truncation=True,
118
+ return_tensors="np"
119
+ )
120
+
121
+ onnx_inputs = {
122
+ "input_ids": tokenized["input_ids"].astype(np.int64),
123
+ "attention_mask": tokenized["attention_mask"].astype(np.int64),
124
+ }
125
+
126
+ # Run inference
127
+ onnx_embeddings = session.run([out.name for out in session.get_outputs()], onnx_inputs)
128
+
129
+ # ONNX produces both int8 and binary precision embeddings:
130
+ int8_embeddings = onnx_embeddings[2]
131
+ binary_embeddings = onnx_embeddings[3]
132
+ packed_embeddings = np.packbits(binary_embeddings != -1, axis=-1)
133
+ ```
134
+
135
+ </details>
136
+
137
+ <details>
138
+ <summary>Using Text Embeddings Inference (TEI)</summary>
139
+
140
+ > [!NOTE]
141
+ > Text Embeddings Inference v1.9.2+ is required.
142
+
143
+ > [!IMPORTANT]
144
+ > Currently, only int8-quantized embeddings are available via TEI. Remember to use cosine similarity with unnormalized int8 embeddings.
145
+
146
+ - CPU w/ Candle:
147
+
148
+ ```bash
149
+ docker run -p 8080:80 ghcr.io/huggingface/text-embeddings-inference:cpu-1.9 --model-id perplexity-ai/pplx-embed-v1-4B --dtype float32
150
+ ```
151
+
152
+ - CPU w/ ORT (ONNX Runtime):
153
+
154
+ ```bash
155
+ docker run -p 8080:80 ghcr.io/huggingface/text-embeddings-inference:cpu-1.9 --model-id onnx-community/pplx-embed-v1-4B --dtype float32
156
+ ```
157
+
158
+ - GPU w/ CUDA:
159
+
160
+ ```bash
161
+ docker run --gpus all --shm-size 1g -p 8080:80 ghcr.io/huggingface/text-embeddings-inference:cuda-1.9 --model-id perplexity-ai/pplx-embed-v1-4B --dtype float32
162
+ ```
163
+
164
+ > If you hit OOM during warmup, lower --max-batch-tokens and --max-client-batch-size. Set --max-batch-tokens to max_sequence_length × batch_size (e.g., 2048 tokens × 8 sequences = 16384).
165
+
166
+ > Alternatively, when running in CUDA you can use the architecture / compute capability specific
167
+ > container instead of the `cuda-1.9`, as that includes the binaries for Turing, Ampere, Hopper and
168
+ > Blackwell, so using a dedicated container will be lighter e.g., `ampere-1.9`.
169
+
170
+ And then you can send requests to it via cURL to `/embed`:
171
+
172
+ ```bash
173
+ curl http://0.0.0.0:8080/embed \
174
+ -H "Content-Type: application/json" \
175
+ -d '{
176
+ "inputs": [
177
+ "Scientists explore the universe driven by curiosity.",
178
+ "Children learn through curious exploration.",
179
+ "Historical discoveries began with curious questions.",
180
+ "Animals use curiosity to adapt and survive.",
181
+ "Philosophy examines the nature of curiosity."
182
+ ],
183
+ "normalize": false
184
+ }'
185
+ ```
186
+
187
+ </details>
188
+
189
+ ## Technical Details
190
+
191
+ For comprehensive technical details and evaluation results, see our paper on arXiv: https://arxiv.org/abs/2602.11151.
192
+
added_tokens.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "</think>": 151668,
3
+ "</tool_call>": 151658,
4
+ "</tool_response>": 151666,
5
+ "<think>": 151667,
6
+ "<tool_call>": 151657,
7
+ "<tool_response>": 151665,
8
+ "<|box_end|>": 151649,
9
+ "<|box_start|>": 151648,
10
+ "<|endoftext|>": 151643,
11
+ "<|file_sep|>": 151664,
12
+ "<|fim_middle|>": 151660,
13
+ "<|fim_pad|>": 151662,
14
+ "<|fim_prefix|>": 151659,
15
+ "<|fim_suffix|>": 151661,
16
+ "<|im_end|>": 151645,
17
+ "<|im_start|>": 151644,
18
+ "<|image_pad|>": 151655,
19
+ "<|object_ref_end|>": 151647,
20
+ "<|object_ref_start|>": 151646,
21
+ "<|quad_end|>": 151651,
22
+ "<|quad_start|>": 151650,
23
+ "<|repo_name|>": 151663,
24
+ "<|video_pad|>": 151656,
25
+ "<|vision_end|>": 151653,
26
+ "<|vision_pad|>": 151654,
27
+ "<|vision_start|>": 151652
28
+ }
assets/diag.png ADDED

Git LFS Details

  • SHA256: 63a6e460d1c0aab08674a944c2778403077c4ef27ccbe3a046057dfb4984d2ff
  • Pointer size: 132 Bytes
  • Size of remote file: 1.03 MB
assets/logo.svg ADDED
config.json ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "PPLXQwen3Model"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration.PPLXQwen3Config",
9
+ "AutoModel": "modeling.PPLXQwen3Model"
10
+ },
11
+ "bos_token_id": 151643,
12
+ "dtype": "float32",
13
+ "eos_token_id": 151643,
14
+ "head_dim": 128,
15
+ "hidden_act": "silu",
16
+ "hidden_size": 2560,
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 9728,
19
+ "layer_types": [
20
+ "full_attention",
21
+ "full_attention",
22
+ "full_attention",
23
+ "full_attention",
24
+ "full_attention",
25
+ "full_attention",
26
+ "full_attention",
27
+ "full_attention",
28
+ "full_attention",
29
+ "full_attention",
30
+ "full_attention",
31
+ "full_attention",
32
+ "full_attention",
33
+ "full_attention",
34
+ "full_attention",
35
+ "full_attention",
36
+ "full_attention",
37
+ "full_attention",
38
+ "full_attention",
39
+ "full_attention",
40
+ "full_attention",
41
+ "full_attention",
42
+ "full_attention",
43
+ "full_attention",
44
+ "full_attention",
45
+ "full_attention",
46
+ "full_attention",
47
+ "full_attention",
48
+ "full_attention",
49
+ "full_attention",
50
+ "full_attention",
51
+ "full_attention",
52
+ "full_attention",
53
+ "full_attention",
54
+ "full_attention",
55
+ "full_attention"
56
+ ],
57
+ "max_position_embeddings": 32768,
58
+ "max_window_layers": 36,
59
+ "model_type": "bidirectional_pplx_qwen3",
60
+ "num_attention_heads": 32,
61
+ "num_hidden_layers": 36,
62
+ "num_key_value_heads": 8,
63
+ "rms_norm_eps": 1e-06,
64
+ "rope_parameters": {
65
+ "rope_theta": 1000000,
66
+ "rope_type": "default"
67
+ },
68
+ "rope_theta": 1000000,
69
+ "sliding_window": null,
70
+ "tie_word_embeddings": true,
71
+ "transformers_version": "5.0.0.dev0",
72
+ "use_cache": false,
73
+ "use_sliding_window": false,
74
+ "vocab_size": 151936,
75
+ "attn_implementation": "sdpa",
76
+ "use_bidirectional_attention": true
77
+ }
configuration.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from transformers.models.qwen3.configuration_qwen3 import Qwen3Config
2
+
3
+
4
+ class PPLXQwen3Config(Qwen3Config):
5
+ model_type = "bidirectional_pplx_qwen3"
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:35f5ccd0a3c772e1a580d10056c294a960e5f2ee7c12e6997171541fbabf4a09
3
+ size 16089915848
modeling.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Callable
2
+ import torch
3
+ from transformers import Qwen3Model
4
+ from transformers.cache_utils import Cache
5
+ from transformers.masking_utils import create_causal_mask
6
+ from transformers.modeling_outputs import BaseModelOutputWithPooling
7
+ from transformers.processing_utils import Unpack
8
+ from transformers.utils import TransformersKwargs
9
+ from .configuration import PPLXQwen3Config
10
+
11
+
12
+ # From modeling_t5gemma.py
13
+ def bidirectional_mask_function(attention_mask: torch.Tensor | None) -> Callable:
14
+ """
15
+ This creates bidirectional attention mask.
16
+ """
17
+
18
+ def inner_mask(batch_idx: int, head_idx: int, q_idx: int, kv_idx: int) -> bool:
19
+ if attention_mask is None:
20
+ return torch.ones((), dtype=torch.bool)
21
+ return attention_mask[batch_idx, kv_idx].to(torch.bool)
22
+
23
+ return inner_mask
24
+
25
+
26
+ class PPLXQwen3Model(Qwen3Model):
27
+ _supports_flash_attn = True
28
+ _supports_sdpa = True
29
+
30
+ config_class = PPLXQwen3Config
31
+
32
+ def __init__(self, config):
33
+ super().__init__(config)
34
+ self.post_init()
35
+
36
+ def post_init(self):
37
+ super().post_init()
38
+ # Override to set all layers to non-causal attention. This'll work with attn_implementation="flash_attention_2" or "sdpa"
39
+ for layer in self.layers:
40
+ layer.self_attn.is_causal = False
41
+
42
+ def forward(
43
+ self,
44
+ input_ids: torch.LongTensor | None = None,
45
+ attention_mask: torch.Tensor | None = None,
46
+ position_ids: torch.LongTensor | None = None,
47
+ past_key_values: Cache | None = None,
48
+ inputs_embeds: torch.FloatTensor | None = None,
49
+ use_cache: bool | None = None,
50
+ cache_position: torch.LongTensor | None = None,
51
+ **kwargs: Unpack[TransformersKwargs],
52
+ ) -> BaseModelOutputWithPooling:
53
+ if inputs_embeds is None:
54
+ inputs_embeds = self.embed_tokens(input_ids)
55
+ input_ids = None
56
+
57
+ # We construct a dummy tensor imitating initial positions
58
+ dummy_cache_position = torch.arange(
59
+ inputs_embeds.shape[1], device=inputs_embeds.device, dtype=torch.long
60
+ )
61
+ attention_mask = {
62
+ "full_attention": create_causal_mask(
63
+ config=self.config,
64
+ input_embeds=inputs_embeds,
65
+ attention_mask=attention_mask,
66
+ cache_position=dummy_cache_position,
67
+ past_key_values=None,
68
+ position_ids=position_ids,
69
+ or_mask_function=bidirectional_mask_function(attention_mask),
70
+ )
71
+ }
72
+
73
+ outputs = super().forward(
74
+ input_ids=input_ids,
75
+ attention_mask=attention_mask,
76
+ position_ids=position_ids,
77
+ past_key_values=past_key_values,
78
+ inputs_embeds=inputs_embeds,
79
+ use_cache=use_cache,
80
+ cache_position=cache_position,
81
+ **kwargs,
82
+ )
83
+ return outputs
modules.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.models.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Pooling",
12
+ "type": "sentence_transformers.models.Pooling"
13
+ },
14
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "",
18
+ "type": "st_quantize.FlexibleQuantizer",
19
+ "kwargs": ["quantization"]
20
+ }
21
+ ]
onnx/model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4ad2fb365d833ff0f08109bb8babaff96987bc40d22d8afa55dd9733078d2414
3
+ size 667659
onnx/model.onnx_data ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:456f8f7269b87a1774ad05734c5437708d9bce8526a2bea2af3b4035499ae2bc
3
+ size 2081202176
onnx/model.onnx_data_1 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6055f3ec48c189b140e435014ea7a964c472a2a55d4d54fa2688700743c0111a
3
+ size 2018611200
onnx/model.onnx_data_2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4bfab8bf7dee4b9f4a154266b4cb2ae7b7af953345fbfdebec4b4d1312d3d3e4
3
+ size 2018611200
onnx/model.onnx_data_3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a5b008d0fb9797fe5fcc4f3b2f7a5e598c1af71d3f13556fb722578d60e9e00f
3
+ size 2018611200
onnx/model.onnx_data_4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5430d76ddafbdc644b6a5338c8dd01577644a54f8efb1312f921843b8e6d1871
3
+ size 2018611200
onnx/model.onnx_data_5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c66ace38112b8c1bea0643c8491a106f40ce171b6ecfcdaac6447484676b3854
3
+ size 2018611200
onnx/model.onnx_data_6 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:37642276adabd79d9c9d9c5b052e4a7974e95d634b1a5c47a4cbb4d07a3d0245
3
+ size 2018611200
onnx/model.onnx_data_7 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d452f4b217363fe0c641f522c03b1129e2077fd371e3040026ade2de87cc4a86
3
+ size 1913743360
special_tokens_map.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_start|>",
4
+ "<|im_end|>",
5
+ "<|object_ref_start|>",
6
+ "<|object_ref_end|>",
7
+ "<|box_start|>",
8
+ "<|box_end|>",
9
+ "<|quad_start|>",
10
+ "<|quad_end|>",
11
+ "<|vision_start|>",
12
+ "<|vision_end|>",
13
+ "<|vision_pad|>",
14
+ "<|image_pad|>",
15
+ "<|video_pad|>"
16
+ ],
17
+ "eos_token": {
18
+ "content": "<|endoftext|>",
19
+ "lstrip": false,
20
+ "normalized": false,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ },
24
+ "mask_token": {
25
+ "content": "â½Ĺ",
26
+ "lstrip": false,
27
+ "normalized": false,
28
+ "rstrip": false,
29
+ "single_word": false
30
+ },
31
+ "pad_token": {
32
+ "content": "<|endoftext|>",
33
+ "lstrip": false,
34
+ "normalized": false,
35
+ "rstrip": false,
36
+ "single_word": false
37
+ },
38
+ "sep_token": "<|endoftext|>"
39
+ }
st_quantize.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ from typing import Literal
4
+ from sentence_transformers.models import Module
5
+
6
+
7
+ class Quantizer(torch.nn.Module):
8
+ def __init__(self, hard: bool = True):
9
+ """
10
+ Args:
11
+ hard: Whether to use hard or soft quantization. Defaults to True.
12
+ """
13
+ super().__init__()
14
+ self._hard = hard
15
+
16
+ def _hard_quantize(self, x, *args, **kwargs) -> torch.Tensor:
17
+ raise NotImplementedError
18
+
19
+ def _soft_quantize(self, x, *args, **kwargs) -> torch.Tensor:
20
+ raise NotImplementedError
21
+
22
+ def forward(self, x, *args, **kwargs) -> torch.Tensor:
23
+ soft = self._soft_quantize(x, *args, **kwargs)
24
+
25
+ if not self._hard:
26
+ result = soft
27
+ else:
28
+ result = (
29
+ self._hard_quantize(x, *args, **kwargs).detach() + soft - soft.detach()
30
+ )
31
+
32
+ return result
33
+
34
+
35
+ class Int8TanhQuantizer(Quantizer):
36
+ def __init__(
37
+ self,
38
+ hard: bool = True,
39
+ ):
40
+ super().__init__(hard=hard)
41
+ self.qmin = -128
42
+ self.qmax = 127
43
+
44
+ def _soft_quantize(self, x, *args, **kwargs):
45
+ return torch.tanh(x)
46
+
47
+ def _hard_quantize(self, x, *args, **kwargs):
48
+ soft = self._soft_quantize(x)
49
+ int_x = torch.round(soft * self.qmax)
50
+ int_x = torch.clamp(int_x, self.qmin, self.qmax)
51
+ return int_x
52
+
53
+
54
+ class BinaryTanhQuantizer(Quantizer):
55
+ def __init__(
56
+ self,
57
+ hard: bool = True,
58
+ scale: float = 1.0,
59
+ ):
60
+ super().__init__(hard)
61
+ self._scale = scale
62
+
63
+ def _soft_quantize(self, x, *args, **kwargs):
64
+ return torch.tanh(self._scale * x)
65
+
66
+ def _hard_quantize(self, x, *args, **kwargs):
67
+ return torch.where(x >= 0, 1.0, -1.0)
68
+
69
+
70
+ class PackedBinaryQuantizer:
71
+ def __call__(self, x: torch.Tensor) -> torch.Tensor:
72
+ bits = np.where(x.cpu().numpy() >= 0, True, False)
73
+ packed = np.packbits(bits, axis=-1)
74
+ return torch.from_numpy(packed).to(x.device)
75
+
76
+
77
+ class FlexibleQuantizer(Module):
78
+ def __init__(self):
79
+ super().__init__()
80
+ self._int8_quantizer = Int8TanhQuantizer()
81
+ self._binary_quantizer = BinaryTanhQuantizer()
82
+ self._packed_binary_quantizer = PackedBinaryQuantizer()
83
+
84
+ def forward(
85
+ self,
86
+ features: dict[str, torch.Tensor],
87
+ quantization: Literal["int8", "binary", "ubinary"] = "int8",
88
+ **kwargs
89
+ ) -> dict[str, torch.Tensor]:
90
+ if quantization == "int8":
91
+ features["sentence_embedding"] = self._int8_quantizer(
92
+ features["sentence_embedding"]
93
+ )
94
+ elif quantization == "binary":
95
+ features["sentence_embedding"] = self._binary_quantizer(
96
+ features["sentence_embedding"]
97
+ )
98
+ elif quantization == "ubinary":
99
+ features["sentence_embedding"] = self._packed_binary_quantizer(
100
+ features["sentence_embedding"]
101
+ )
102
+ else:
103
+ raise ValueError(
104
+ f"Invalid quantization type: {quantization}. Must be 'binary', 'ubinary', or 'int8'."
105
+ )
106
+ return features
107
+
108
+ @classmethod
109
+ def load(
110
+ cls,
111
+ model_name_or_path: str,
112
+ subfolder: str = "",
113
+ token: bool | str | None = None,
114
+ cache_folder: str | None = None,
115
+ revision: str | None = None,
116
+ local_files_only: bool = False,
117
+ **kwargs,
118
+ ):
119
+ return cls()
120
+
121
+ def save(self, output_path: str, *args, **kwargs) -> None:
122
+ return
tokenizer_config.json ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "151642": {
6
+ "content": "â½Ĺ",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "151643": {
14
+ "content": "<|endoftext|>",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "151644": {
22
+ "content": "<|im_start|>",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ },
29
+ "151645": {
30
+ "content": "<|im_end|>",
31
+ "lstrip": false,
32
+ "normalized": false,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": true
36
+ },
37
+ "151646": {
38
+ "content": "<|object_ref_start|>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": true
44
+ },
45
+ "151647": {
46
+ "content": "<|object_ref_end|>",
47
+ "lstrip": false,
48
+ "normalized": false,
49
+ "rstrip": false,
50
+ "single_word": false,
51
+ "special": true
52
+ },
53
+ "151648": {
54
+ "content": "<|box_start|>",
55
+ "lstrip": false,
56
+ "normalized": false,
57
+ "rstrip": false,
58
+ "single_word": false,
59
+ "special": true
60
+ },
61
+ "151649": {
62
+ "content": "<|box_end|>",
63
+ "lstrip": false,
64
+ "normalized": false,
65
+ "rstrip": false,
66
+ "single_word": false,
67
+ "special": true
68
+ },
69
+ "151650": {
70
+ "content": "<|quad_start|>",
71
+ "lstrip": false,
72
+ "normalized": false,
73
+ "rstrip": false,
74
+ "single_word": false,
75
+ "special": true
76
+ },
77
+ "151651": {
78
+ "content": "<|quad_end|>",
79
+ "lstrip": false,
80
+ "normalized": false,
81
+ "rstrip": false,
82
+ "single_word": false,
83
+ "special": true
84
+ },
85
+ "151652": {
86
+ "content": "<|vision_start|>",
87
+ "lstrip": false,
88
+ "normalized": false,
89
+ "rstrip": false,
90
+ "single_word": false,
91
+ "special": true
92
+ },
93
+ "151653": {
94
+ "content": "<|vision_end|>",
95
+ "lstrip": false,
96
+ "normalized": false,
97
+ "rstrip": false,
98
+ "single_word": false,
99
+ "special": true
100
+ },
101
+ "151654": {
102
+ "content": "<|vision_pad|>",
103
+ "lstrip": false,
104
+ "normalized": false,
105
+ "rstrip": false,
106
+ "single_word": false,
107
+ "special": true
108
+ },
109
+ "151655": {
110
+ "content": "<|image_pad|>",
111
+ "lstrip": false,
112
+ "normalized": false,
113
+ "rstrip": false,
114
+ "single_word": false,
115
+ "special": true
116
+ },
117
+ "151656": {
118
+ "content": "<|video_pad|>",
119
+ "lstrip": false,
120
+ "normalized": false,
121
+ "rstrip": false,
122
+ "single_word": false,
123
+ "special": true
124
+ },
125
+ "151657": {
126
+ "content": "<tool_call>",
127
+ "lstrip": false,
128
+ "normalized": false,
129
+ "rstrip": false,
130
+ "single_word": false,
131
+ "special": false
132
+ },
133
+ "151658": {
134
+ "content": "</tool_call>",
135
+ "lstrip": false,
136
+ "normalized": false,
137
+ "rstrip": false,
138
+ "single_word": false,
139
+ "special": false
140
+ },
141
+ "151659": {
142
+ "content": "<|fim_prefix|>",
143
+ "lstrip": false,
144
+ "normalized": false,
145
+ "rstrip": false,
146
+ "single_word": false,
147
+ "special": false
148
+ },
149
+ "151660": {
150
+ "content": "<|fim_middle|>",
151
+ "lstrip": false,
152
+ "normalized": false,
153
+ "rstrip": false,
154
+ "single_word": false,
155
+ "special": false
156
+ },
157
+ "151661": {
158
+ "content": "<|fim_suffix|>",
159
+ "lstrip": false,
160
+ "normalized": false,
161
+ "rstrip": false,
162
+ "single_word": false,
163
+ "special": false
164
+ },
165
+ "151662": {
166
+ "content": "<|fim_pad|>",
167
+ "lstrip": false,
168
+ "normalized": false,
169
+ "rstrip": false,
170
+ "single_word": false,
171
+ "special": false
172
+ },
173
+ "151663": {
174
+ "content": "<|repo_name|>",
175
+ "lstrip": false,
176
+ "normalized": false,
177
+ "rstrip": false,
178
+ "single_word": false,
179
+ "special": false
180
+ },
181
+ "151664": {
182
+ "content": "<|file_sep|>",
183
+ "lstrip": false,
184
+ "normalized": false,
185
+ "rstrip": false,
186
+ "single_word": false,
187
+ "special": false
188
+ },
189
+ "151665": {
190
+ "content": "<tool_response>",
191
+ "lstrip": false,
192
+ "normalized": false,
193
+ "rstrip": false,
194
+ "single_word": false,
195
+ "special": false
196
+ },
197
+ "151666": {
198
+ "content": "</tool_response>",
199
+ "lstrip": false,
200
+ "normalized": false,
201
+ "rstrip": false,
202
+ "single_word": false,
203
+ "special": false
204
+ },
205
+ "151667": {
206
+ "content": "<think>",
207
+ "lstrip": false,
208
+ "normalized": false,
209
+ "rstrip": false,
210
+ "single_word": false,
211
+ "special": false
212
+ },
213
+ "151668": {
214
+ "content": "</think>",
215
+ "lstrip": false,
216
+ "normalized": false,
217
+ "rstrip": false,
218
+ "single_word": false,
219
+ "special": false
220
+ }
221
+ },
222
+ "additional_special_tokens": [
223
+ "<|im_start|>",
224
+ "<|im_end|>",
225
+ "<|object_ref_start|>",
226
+ "<|object_ref_end|>",
227
+ "<|box_start|>",
228
+ "<|box_end|>",
229
+ "<|quad_start|>",
230
+ "<|quad_end|>",
231
+ "<|vision_start|>",
232
+ "<|vision_end|>",
233
+ "<|vision_pad|>",
234
+ "<|image_pad|>",
235
+ "<|video_pad|>"
236
+ ],
237
+ "bos_token": null,
238
+ "clean_up_tokenization_spaces": false,
239
+ "eos_token": "<|endoftext|>",
240
+ "errors": "replace",
241
+ "extra_special_tokens": {},
242
+ "mask_token": "â½Ĺ",
243
+ "model_max_length": 131072,
244
+ "pad_token": "<|endoftext|>",
245
+ "sep_token": "<|endoftext|>",
246
+ "split_special_tokens": false,
247
+ "tokenizer_class": "Qwen2Tokenizer",
248
+ "unk_token": null
249
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff