mlboydaisuke commited on
Commit
2e0d7cb
Β·
verified Β·
1 Parent(s): b3f68a3

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +129 -0
README.md ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - executorch
5
+ - xnnpack
6
+ - pte
7
+ - on-device
8
+ - text-ranking
9
+ base_model:
10
+ - Qwen/Qwen3-Reranker-0.6B
11
+ ---
12
+ # Qwen3-Reranker-0.6B β€” ExecuTorch
13
+
14
+ A reranker that is not a cross-encoder. The other five on this shelf are BERTs with a
15
+ regression head; this one is a **causal language model asked a yes/no question**, and
16
+ the score is how much more it would answer "yes" than "no". Same job β€” read a query and
17
+ one document together and score the pair β€” with a different machine underneath.
18
+
19
+ ```
20
+ input_ids (1, 512) int64 the prompt, LEFT-padded
21
+ attention_mask (1, 512) int64
22
+ -> score (1, 1) fp32 log-odds: logit("yes") - logit("no")
23
+ ```
24
+
25
+ - **Source**: [Qwen/Qwen3-Reranker-0.6B](https://huggingface.co/Qwen/Qwen3-Reranker-0.6B) β€” 595.8M parameters, 28 Qwen3 layers, hidden 1024, 151,669 vocabulary
26
+ - **License**: apache-2.0
27
+ - **Output**: one number per pair. Higher is more relevant; `sigmoid(x)` maps it to 0..1
28
+ and does not change the ordering.
29
+
30
+ ## Variants
31
+
32
+ | build | file | size (MB) | worst score error vs eager | Mac median (ms)* |
33
+ |---|---|---|---|---|
34
+ | fp32 | `rerank_qwen3_0_6b_xnnpack_fp32.pte` | 2383.7 | **0.0000 logits** | 381.5 |
35
+ | fp16 | `rerank_qwen3_0_6b_xnnpack_fp16.pte` | 1192.5 | 0.0247 logits | 1058.0 |
36
+ | **Core ML (fp16, iOS)** | `rerank_qwen3_0_6b_coreml_all.pte` | 1195.2 | 0.0573 logits | **85.0** |
37
+
38
+ \*Mac arm64, median of 10, one 512-token pair β€” a reference point for relative cost, not
39
+ a device number. Torch eager fp32 on the same machine is 261 ms. **Core ML is the one to
40
+ use where it exists**: 4.5x faster than XNNPACK fp32 and 100% of the graph delegated,
41
+ where XNNPACK takes 72% across 172 subgraphs. A reranker earns its keep over a list of
42
+ fifty candidates, so that factor is the whole story.
43
+
44
+ Correlation cannot judge this model β€” the output is a single number, and the correlation
45
+ of a one-element vector is undefined. The gate is the score error in the model's own
46
+ units together with whether the ranking survives. Over six real pairs, **every shipped
47
+ build reproduces eager's order**, and the narrowest adjacent gap in that ranking is
48
+ 0.7433 logits.
49
+
50
+ ## Running it
51
+
52
+ **1. Build the prompt.** The model was trained to read one specific frame, and it is not
53
+ a chat model you can prompt freely:
54
+
55
+ ```python
56
+ PREFIX = ('<|im_start|>system\nJudge whether the Document meets the requirements '
57
+ 'based on the Query and the Instruct provided. Note that the answer can '
58
+ 'only be "yes" or "no".<|im_end|>\n<|im_start|>user\n')
59
+ SUFFIX = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
60
+ INSTRUCT = "Given a web search query, retrieve relevant passages that answer the query"
61
+
62
+ text = (f"{PREFIX}<Instruct>: {INSTRUCT}\n<Query>: {query}\n"
63
+ f"<Document>: {document}{SUFFIX}")
64
+ ```
65
+
66
+ `INSTRUCT` is a real input, not decoration: it names the retrieval task, and Qwen
67
+ reports it is worth a few points to write your own instead of the generic default.
68
+
69
+ **2. Tokenise with LEFT padding to exactly 512.**
70
+
71
+ ```python
72
+ tokenizer = AutoTokenizer.from_pretrained(repo, padding_side="left")
73
+ batch = tokenizer([text], padding="max_length", truncation=True, max_length=512,
74
+ return_tensors="pt")
75
+ assert batch["attention_mask"][0, -1] == 1
76
+ ```
77
+
78
+ **This is the one that will bite.** The graph reads position βˆ’1 unconditionally, which
79
+ is the last *real* token only when the padding is on the left. Right-padded input scores
80
+ a pad token and returns a confident-looking number that means nothing β€” nothing raises.
81
+ The assertion above is one line and catches it.
82
+
83
+ **3. Run it once per candidate** and sort by the score, descending.
84
+
85
+ ## The output projection is folded into one vector
86
+
87
+ The score needs two of the 151,669 logits, and the difference of two dot products is one
88
+ dot product against the difference of two rows:
89
+
90
+ ```
91
+ logit_yes - logit_no = h . W[yes] - h . W[no] = h . (W[yes] - W[no])
92
+ ```
93
+
94
+ So `lm_head` never runs. A 151,669-wide matmul per pair does not happen, and the delegate
95
+ does not take its own copy of a 621 MB table β€” which is exactly why this shelf's Whisper
96
+ decoders come out bigger than their weights.
97
+
98
+ The two vocabulary ids are read from the repo's own `1_LogitScore/config.json`
99
+ (**9693** for "yes", **2152** for "no") rather than looked up through the tokenizer,
100
+ where a leading-space variant is a different token.
101
+
102
+ **Checked against the reference, at matching precision.** sentence-transformers'
103
+ `CrossEncoder` scores these pairs at +6.6875 / βˆ’6.0625 / βˆ’7.2812 and the folded head at
104
+ +6.7271 / βˆ’6.1107 / βˆ’7.3159 β€” a gap of 4.8e-02 that has nothing to do with the fold.
105
+ The reference loads the checkpoint in its own **bfloat16**; run both arms in fp32 and the
106
+ folded head matches it to **4.673e-05**, while the reference's bf16 and fp32 arms differ
107
+ from each other by the full 4.820e-02.
108
+
109
+ ## It ranks across languages
110
+
111
+ The six test pairs include a Japanese passage that says what the query asks. It comes out
112
+ **first of six** at +7.470, above the English document carrying the actual number
113
+ (+6.727). The shelf's `ms-marco-MiniLM` rerankers are English-only and put it far lower β€”
114
+ which is the right answer for them, not a defect.
115
+
116
+ ## Not shipped: int8
117
+
118
+ `rerank_qwen3_0_6b_xnnpack_int8.pte` is **1064.0 MB** and runs at 327 ms β€” smaller and
119
+ faster than the fp32 build. It is withheld on the number that decides.
120
+
121
+ Dynamic int8 moves a pair score by **0.6456 logits**, against a narrowest adjacent gap of
122
+ **0.7433** in the ranking it has to preserve. It happens to keep the order on these six
123
+ pairs, but an error that is 87% of the distance between two neighbours is not a build
124
+ that ranks reliably β€” a slightly different candidate list would reorder.
125
+
126
+ The size was predictable before it was built: the token embedding table is
127
+ 151,669 x 1024 x 4 = 621 MB of the 2383.7 MB model, a **26.1%** share, and this shelf's
128
+ rule `int8/fp16 = 0.5 + 1.5 x (table share)` puts the file at 0.891 of fp16. It came out
129
+ at **0.892** β€” the same figure as Qwen3-Embedding-0.6B, which shares this backbone.