mahiyama commited on
Commit
592aa0a
·
verified ·
1 Parent(s): 1ef92dc

force text modality, drop usage section, remove self source_datasets, style cleanup

Browse files
Files changed (1) hide show
  1. README.md +368 -418
README.md CHANGED
@@ -1,418 +1,368 @@
1
- ---
2
- language:
3
- - ja
4
- license: cc-by-sa-4.0
5
- size_categories:
6
- - 1M<n<10M
7
- task_categories:
8
- - sentence-similarity
9
- - text-retrieval
10
- task_ids:
11
- - document-retrieval
12
- pretty_name: Auto Wiki QA (Pairs / Triplets / N-tuples + distillation top-K)
13
- source_datasets:
14
- - hpprc/emb
15
- - mahiyama/auto-wiki-qa
16
- tags:
17
- - japanese
18
- - wikipedia
19
- - question-answering
20
- - retrieval
21
- - embedding
22
- - triplet
23
- - hard-negative-mining
24
- - contrastive-learning
25
- - knowledge-distillation
26
- - cross-encoder
27
- configs:
28
- - config_name: pairs
29
- data_files:
30
- - split: train
31
- path: pairs/train-*
32
- - config_name: triplets
33
- data_files:
34
- - split: train
35
- path: triplets/train-*
36
- - config_name: n-tuples
37
- data_files:
38
- - split: train
39
- path: n-tuples/train-*
40
- - config_name: n-tuples-100k
41
- data_files:
42
- - split: train
43
- path: n-tuples-100k/train-*
44
- - config_name: n-tuples-250k
45
- data_files:
46
- - split: train
47
- path: n-tuples-250k/train-*
48
- - config_name: n-tuples-500k
49
- data_files:
50
- - split: train
51
- path: n-tuples-500k/train-*
52
- - config_name: n-tuples-1m
53
- data_files:
54
- - split: train
55
- path: n-tuples-1m/train-*
56
- default_config: triplets
57
- ---
58
-
59
- # auto-wiki-qa
60
-
61
- 日本語 Wikipedia ベースの自動生成 QA データセット
62
- ([hpprc/emb](https://huggingface.co/datasets/hpprc/emb) の auto-wiki-qa-dataset /
63
- auto-wiki-qa-collection) を元に、Hard Negative Mining と
64
- Cross-Encoder 蒸留スコア付与を施した日本語 retrieval 学習用データセットの **v2 版**。
65
-
66
- `pairs` / `triplets` / `n-tuples` の 3 形式と、`n-tuples` を `quality_score` で
67
- ソートした top-K サブセット (100k / 250k / 500k / 1m) を提供する。
68
- Dense Retriever / Cross-Encoder / SPLADE などの日本語検索モデル学習および
69
- KL Divergence 蒸留に使える。
70
-
71
- > **v2 (2026-05) の主な変更点**
72
- > - HNM の retriever を [`cl-nagoya/ruri-v3-310m`](https://huggingface.co/cl-nagoya/ruri-v3-310m) (Apache 2.0、商用可)、
73
- > 蒸留教師を [`cl-nagoya/ruri-v3-reranker-310m`](https://huggingface.co/cl-nagoya/ruri-v3-reranker-310m) (Apache 2.0、商用可) に刷新。
74
- > - Hard Negative 選定を **単一 margin filter** (`pos − neg ≥ 4.0`、σ で 0.98 相当の分離) に変更。
75
- > 同テーマ別エントリ・同回答別記事などの **false negative リーク** を構造的に排除した。
76
- > - dense retriever のスコアは hard negative 選定に**使わず**、教師リランカーの生 logit のみで判定する。
77
-
78
- ## Configs
79
-
80
- | config | rows | columns | 総トークン数 (cl100k_base) | 説明 |
81
- |---|---:|---|---:|---|
82
- | pairs | 2,376,503 | query, answer | 562,891,158 (562.89M) | 元の (query, answer) ペア。`mahiyama/auto-wiki-qa` (v1) からそのままミラー。 |
83
- | triplets | 2,149,821 | query, positive, negative | 1,017,027,276 (1.02B) | Hard Negative Mining (v3) を通過した全行。クエリごとに 1 件の hard negative を付けた triplet。 |
84
- | n-tuples | 2,149,821 | query, positive, negative_1〜negative_5, label | 3,055,829,114 (3.06B) | 同じ採用行に対しクエリごとに 5 件の hard negative + Cross-Encoder の生 logit を `label` に付与。 |
85
- | n-tuples-100k | 100,000 | query, positive, negative_1〜negative_5, label | 138,618,253 (138.62M) | `n-tuples` のうち `quality_score` の valid を通過した行を降順で並べ、**上位 10 万行**を抽出 (デバッグ・早期実験向け)。 |
86
- | n-tuples-250k | 250,000 | query, positive, negative_1〜negative_5, label | 349,362,397 (349.36M) | 同じ valid 行から `quality_score` 上位 25 万行 (バランス重視)。 |
87
- | n-tuples-500k | 500,000 | query, positive, negative_1〜negative_5, label | 701,461,481 (701.46M) | 同じ valid 行から `quality_score` 上位 50 万行。 |
88
- | n-tuples-1m | 1,000,000 | query, positive, negative_1〜negative_5, label | 1,410,424,585 (1.41B) | 同じ valid 行から `quality_score` 上位 100 万行 (採用行数が 1M に満たない場合は全採用行)。 |
89
-
90
- > トークン数は OpenAI の tiktoken `cl100k_base` (GPT-4 系) で計測。`label` カラムはトークン数集計の対象外 (浮動小数の数値ベクトル)。
91
-
92
- すべての config の split は `train`。`n-tuples-*` の top-K サブセットは
93
- `quality_score` 降順で並んでいるため、行順序は元の `n-tuples` と一致しない。
94
-
95
- ### label カラムの形式 (n-tuples / n-tuples-* 共通)
96
-
97
- ```python
98
- label: list[float] # 長さ 6 = [score(query, positive), score(query, neg_1), …, score(query, neg_5)]
99
- ```
100
-
101
- `label[0]` が positive、`label[1:]` が `negative_1` 〜 `negative_5` に 1:1 で順序通り対応する。
102
- スコアは Cross-Encoder の **生 logit (sigmoid 適用前)**。
103
-
104
- `pos − max(neg_1..neg_5) ≥ 4.0` および `pos ≥ 2.0` が全行で構造的に成立している
105
- (下記 Hard Negative Mining セクション参照)。
106
-
107
- ## 使い方
108
-
109
- ### pairs (最もシンプルな (query, answer) 形式)
110
-
111
- ```python
112
- from datasets import load_dataset
113
-
114
- pairs = load_dataset("mahiyama/auto-wiki-qa", "pairs", split="train")
115
- print(pairs)
116
- # Dataset({
117
- # features: ['query', 'answer'],
118
- # num_rows: 2376503
119
- # })
120
- print(pairs[0])
121
- # {"query": "...", "answer": "..."}
122
- ```
123
-
124
- ### triplets (1 hard negative / query)
125
-
126
- ```python
127
- triplets = load_dataset("mahiyama/auto-wiki-qa", "triplets", split="train")
128
- print(triplets[0])
129
- # {"query": "...", "positive": "...", "negative": "..."}
130
- ```
131
-
132
- ### n-tuples (5 hard negatives / query + 蒸留 logit)
133
-
134
- ```python
135
- ntuples = load_dataset("mahiyama/auto-wiki-qa", "n-tuples", split="train")
136
- print(ntuples[0])
137
- # {
138
- # "query": "...",
139
- # "positive": "...",
140
- # "negative_1": "...", "negative_2": "...", ..., "negative_5": "...",
141
- # "label": [12.34, 1.05, -4.21, -2.85, -5.13, -6.02], # 生 logit
142
- # }
143
- ```
144
-
145
- ### top-K 高品質サブセット (KL 蒸留学習向け)
146
-
147
- ```python
148
- # 最高品質 10 万行 (デバッグ・早期実験に十分強い)
149
- ds = load_dataset("mahiyama/auto-wiki-qa", "n-tuples-100k", split="train")
150
-
151
- # バランス重視 25 万 / 50 万行
152
- ds = load_dataset("mahiyama/auto-wiki-qa", "n-tuples-250k", split="train")
153
- ds = load_dataset("mahiyama/auto-wiki-qa", "n-tuples-500k", split="train")
154
-
155
- # 採用行のほぼ全量 (上位 100 万行 or 全採用行)
156
- ds = load_dataset("mahiyama/auto-wiki-qa", "n-tuples-1m", split="train")
157
- ```
158
-
159
- ## Hard Negative Mining の方法 (v3)
160
-
161
- 各クエリ q_i (元データ行 i) に対して以下を実施:
162
-
163
- 1. **クエリ埋め込み** —
164
- [`cl-nagoya/ruri-v3-310m`](https://huggingface.co/cl-nagoya/ruri-v3-310m)
165
- (prefix `"検索クエリ: "`) で 768 次元にエンコード。
166
- 2. **文書埋め込み** — 同モデル (prefix `"検索文書: "`) で全 positive (約 240 万件、重複除去後)
167
- を 768 次元にエンコードし、L2 正規化して
168
- [faiss-cpu](https://github.com/facebookresearch/faiss) の `IndexFlatIP`
169
- (内積 = コサイン類似度) を構築。索引は `faiss_index.bin` として再利用可能。
170
- 3. **kNN 検索** — 各クエリで上位 25 件を取得し、gold positive と一致する `doc_id` を除外して
171
- **top-15** を negative 候補プールとする。
172
- 4. **リランカー蒸留スコア付与** —
173
- [`cl-nagoya/ruri-v3-reranker-310m`](https://huggingface.co/cl-nagoya/ruri-v3-reranker-310m)
174
- で `(query, positive)` と `(query, kNN candidate × 15)` の合計 **16 ペア**に
175
- **生 logit (sigmoid 適用前)** を付与。
176
- 5. **Hard Negative 選定 (v3、単一 margin filter)**
177
- - `pos_score < 2.0` の行は **DROP** (positive が弱い)
178
- - 15 候補のうち `pos_score - neg_score >= 4.0` を満たすもののみ採用候補
179
- - 採用候補が 5 件未満の行は **DROP** (フォールバックなし)
180
- - 採用候補をリランカースコア降順で並べ、**上位 5 件**を `negative_1..5` とする
181
- - `negative_1` = 採用候補のうちスコア最大 (= 最も hard だが MIN_DIFF を満たす)
182
- - `negative_5` = 採用候補のうちスコア最小
183
-
184
- これにより、採用行は以下の条件を構造的に満たす:
185
-
186
- - `pos >= 2.0` (sigmoid 0.88 以上の確信ある positive)
187
- - `pos - neg_i >= 4.0` (i=1..5、すべての negative に対し σ で 0.98 相当の分離)
188
- - `margin = pos - max(neg_1..neg_5) >= 4.0`
189
-
190
- ### なぜ単一 margin filter か (kNN/リランカー top-K 除外をしない理由)
191
-
192
- NV-Retriever / SFR-Embedding 系の標準レシピでは「kNN 上位 K 件除外」「リランカー上位 K 件除外」
193
- を組み合わせるが、`pos - neg >= 4.0` (σ で 0.98 相当の分離) のような**厳しい絶対 margin filter**
194
- を使うと、これらの構造的除外は**冗長**になる:
195
-
196
- - false negative リーク (= reranker が高 logit を付ける候補) は margin filter で必ず弾かれる
197
- - 一方、kNN/reranker top-K 除外を残すと「ぎりぎり通過する硬い negative」が拾えなくなる
198
-
199
- → v3 では **margin filter 1 本に統合**することで、量とハードさのバランスを取った。
200
-
201
- ### v1 (旧 `mahiyama/auto-wiki-qa`) との違い
202
-
203
- | 項目 | v1 (旧) | v2 (v3 HNM、本リポジトリ) |
204
- |---|---|---|
205
- | Retriever | `jinaai/jina-embeddings-v5-text-small` (非商用) | `cl-nagoya/ruri-v3-310m` (Apache 2.0、商用可) |
206
- | Reranker | `cl-nagoya/ruri-reranker-large` | `cl-nagoya/ruri-v3-reranker-310m` |
207
- | HNM フィルタ | dense score の `< pos × 0.95` (positive-aware filter) | reranker logit の `pos − neg ≥ 4.0` (絶対 margin) |
208
- | False negative leak 対策 | なし (定義系・同テーマ別エントリで leak が頻発) | 構造的に排除 |
209
- | ANN | Elasticsearch HNSW | `faiss-cpu IndexFlatIP` |
210
- | 採用行数 | 入力行数と同じ (フォールバックで埋める) | リーク行は **DROP** (採用率 90.5%) |
211
- | 商用利用 | 制限あり (Jina) | 制限なし (両モデルとも Apache 2.0) |
212
-
213
- 代償として行数は減るが、KL 蒸留損失はラベル雑音にとても敏感なため、量を多少削ってでも
214
- クリーンな negative を確保する方が学習効率は明確に良い��
215
-
216
- ### 採用統計
217
-
218
- | 指標 | 値 |
219
- |---|---:|
220
- | 入力行数 (リランカーで採点した行) | 2,376,503 |
221
- | 採用行数 (kept: HNM 通過) | 2,149,821 (90.5%) |
222
- | Drop: 弱 positive (`pos < 2.0`) | 194,350 (8.2%) |
223
- | Drop: 候補不足 (filter 通過 < 5 件) | 32,332 (1.4%) |
224
- | quality_score の valid 通過 (採用行に対して) | 2,149,821 (100.00%) |
225
-
226
- `triplets` / `n-tuples` には採用行のみが入る。
227
- `n-tuples-{100k, 250k, 500k, 1m}` は採用行のうち `quality_score` の valid を通過した行を
228
- 降順で並べた top-K サブセット。v3 HNM が `pos>=2.0` と `margin>=4.0` を保証するため、
229
- valid 通過率は実質 100% になる (透明性のためチェックは入れている)。
230
-
231
- ## 蒸留スコア付けの方法
232
-
233
- `n-tuples` 配下の `label` カラムおよび `n-tuples-*` の top-K サブセットは、
234
- 教師モデル `cl-nagoya/ruri-v3-reranker-310m` (Japanese ModernBERT Cross-Encoder、Apache 2.0)
235
- の **生 logit** を各 (query, doc) ペア (positive 1 件 + negative 5 件) に付与したもの。
236
-
237
- ### スコア付けの設定
238
-
239
- | 項目 | 値 |
240
- |---|---|
241
- | Teacher | `cl-nagoya/ruri-v3-reranker-310m` |
242
- | Max length | 512 |
243
- | 推論精度 | FP16 |
244
- | Batch size | 128 |
245
- | 入力フォーマット | 単一文ペア `(query, document)` |
246
- | 出力 | 生 logit (sigmoid 適用前)。観測 logit レンジ おおむね ±15 (採用行の positive 平均 ≈ 8.80) |
247
-
248
- KL 蒸留損失 (`SparseDistillKLDivLoss` / `DistillKLDivLoss` 等) は
249
- `softmax(label / T)` を target distribution として使うため、
250
- sigmoid 後の `[0, 1]` 値だと softmax target が一様分布に潰れて ranking 情報が失われる。
251
- これを防ぐため、教師の最終活性化を明示的に `Identity` に置き換えてスコアを出力した:
252
-
253
- ```python
254
- import torch.nn as nn
255
- from sentence_transformers import CrossEncoder
256
-
257
- teacher = CrossEncoder(
258
- "cl-nagoya/ruri-v3-reranker-310m",
259
- max_length=512,
260
- device="cuda",
261
- )
262
- teacher.model.eval()
263
- teacher.default_activation_function = nn.Identity() # sigmoid を外す
264
-
265
- scores = teacher.predict(
266
- [("query A", "passage A"), ("query A", "passage B")],
267
- batch_size=128,
268
- convert_to_numpy=True,
269
- activation_fct=nn.Identity(), # 念のため predict() でも明示
270
- )
271
- # scores: array([12.34, -3.56], dtype=float32)
272
- assert abs(scores).max() > 1.0 # |score|max > 1 で sigmoid 混入チェック
273
- ```
274
-
275
- logit のスケール感:
276
-
277
- | logit | sigmoid(logit) | 解釈 |
278
- | ----: | --------------: | ------------------------------------- |
279
- | +8 | 0.9997 | 確信度高く relevant |
280
- | +5 | 0.993 | 強く relevant |
281
- | +2 | 0.881 | relevant 寄り |
282
- | 0 | 0.5 | 不確か |
283
- | -2 | 0.119 | irrelevant 寄り |
284
- | -5 | 0.007 | 強く irrelevant |
285
- | -8 | 0.0003 | 確信度高く irrelevant |
286
-
287
- ### quality_score: top-K サブセットのソート指標
288
-
289
- `n-tuples-100k / -250k / -500k / -1m` は、`n-tuples` を以下の手順で構築する:
290
-
291
- 1. `quality_score` の **valid マスク** を計算 (distillation_score.md 準拠)
292
- 2. valid を通過した行を `quality_score` 降順で並べる
293
- 3. 上位 K 行を抽出 (採用行数が K に満たない場合は全採用行)
294
-
295
- v3 HNM が `pos >= 2.0` と全 neg について `pos - neg >= 4.0` を保証するため、
296
- valid マスクの 3 条件 (pos>=2.0, margin>0, margin>=0.5) は全て自動的に成立し、
297
- valid 通過率は実質 100% (上記**採用統計**セクション参照)。
298
- それでもチェックを入れているのは、品質スコア計算ロジックの**形式的整合性**のため。
299
-
300
- #### 定義 (実装通り)
301
-
302
- ```python
303
- import numpy as np
304
-
305
- POS_MIN = 2.0 # σ(2.0) ≈ 0.88: 確信のある positive のみ採用
306
- MARGIN_MIN = 0.5 # 誤差レベルの分離は雑音と見なす
307
- MARGIN_PENALTY = 0.1 # 易しすぎる行を弱く後回しにする係数
308
-
309
- def quality_score_vec(labels: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
310
- """
311
- Args:
312
- labels: (N, 6) float32. 生 logit。 [p, n1, n2, n3, n4, n5].
313
- Returns:
314
- scores: (N,) float. valid 落ちした行は -inf。
315
- valid: (N,) bool. pos>=2.0 ∧ margin>0 ∧ margin>=0.5
316
- """
317
- p = labels[:, 0]
318
- neg = labels[:, 1:]
319
- max_n = neg.max(axis=1)
320
- mean_n = neg.mean(axis=1)
321
- margin = p - max_n
322
-
323
- fn_mask = margin <= 0
324
- weak_mask = (~fn_mask) & (p < POS_MIN)
325
- border_mask = (~fn_mask) & (~weak_mask) & (margin < MARGIN_MIN)
326
- valid_mask = (~fn_mask) & (~weak_mask) & (~border_mask)
327
-
328
- raw = mean_n - MARGIN_PENALTY * margin
329
- scores = np.where(valid_mask, raw, -np.inf)
330
- return scores, valid_mask
331
- ```
332
-
333
- - **`mean(neg)` を主軸にする理由** — KL 蒸留は `softmax(label / T)` を target にするた��、
334
- negative 全体の logit 分布が target 形状を決める。`mean(neg)` が高い行ほど target が
335
- 滑らかな分布になり、ranking 学習が効く。
336
- - **`margin` に弱い負ペナルティを掛ける理由** — `margin` が極端に大きい「楽勝行」を
337
- タイブレークで後回しにするため。
338
-
339
- #### Hard Negative の観測レンジ (採用行に対して)
340
-
341
- | 項目 | 観測値 |
342
- | ----- | -----: |
343
- | positive logit (平均) | 8.80 |
344
- | positive logit (5%-tile / 95%-tile) | 3.68 / 12.02 |
345
- | neg_1 logit (平均、最硬の negative) | -1.50 |
346
- | neg_5 logit (平均、5 番目の negative) | -5.35 |
347
- | margin = pos − max(neg) (平均) | 10.30 |
348
- | margin = pos − max(neg) (最小) | 4.00 |
349
-
350
- 採用行の最小 margin が `MIN_DIFF_FROM_POS = 4.0` を下回らないことが構造的に保証される
351
- (Hard Negative 選定段階で `pos - neg >= 4.0` を全 5 件に対して要求しているため)。
352
-
353
- ### KL 蒸留損失への組み込み (PyTorch 例)
354
-
355
- ```python
356
- import torch.nn.functional as F
357
-
358
- def kl_distill_loss(student_logits, teacher_label, T=2.0):
359
- """
360
- Args:
361
- student_logits: (B, 6) student の (query, doc_i) スコア (生 logit)
362
- teacher_label: (B, 6) label カラムをそのまま (生 logit)
363
- T: distillation temperature (推奨 1.0〜4.0、生 logit レンジが広めなので 2.0 前後が扱いやすい)
364
- """
365
- log_p_student = F.log_softmax(student_logits / T, dim=-1)
366
- p_teacher = F.softmax(teacher_label / T, dim=-1)
367
- return F.kl_div(log_p_student, p_teacher, reduction="batchmean") * (T * T)
368
- ```
369
-
370
- sentence-transformers の `SparseDistillKLDivLoss` / `DistillKLDivLoss` を使う場合は、
371
- `label` カラムをそのまま teacher score として渡せばよい。
372
-
373
- ## 変更履歴
374
-
375
- | バージョン | リポジトリ | 日付 | 概要 |
376
- |---|---|---|---|
377
- | v2 (= 本リポジトリ) | `mahiyama/auto-wiki-qa` (Public) | 2026-05 | HNM retriever を `cl-nagoya/ruri-v3-310m` に、教師 reranker を `cl-nagoya/ruri-v3-reranker-310m` に刷新。`faiss-cpu IndexFlatIP` ベース。Hard Negative 選定を **単一 margin filter** (`pos − neg ≥ 4.0`) に変更し、false negative リークを構造的に排除。両モデルとも Apache 2.0 で商用利用可。 |
378
- | v1 | `mahiyama/auto-wiki-qa` (Public) | 2024 | 初版。HNM retriever = `jinaai/jina-embeddings-v5-text-small`、教師 reranker = `cl-nagoya/ruri-reranker-large`。フィルタは `< pos × 0.95` の positive-aware filter のみ。 |
379
-
380
- ## License
381
-
382
- - Passage 本文は日本語版 Wikipedia 由来であり、**CC BY-SA 4.0**
383
- (Wikipedia コンテンツの表示・継承ライセンス) を継承する。
384
- - クエリは `hpprc/emb` の auto-wiki-qa 由来。利用・再配布時は元データセットの
385
- ライセンスと利用条件にも従うこと。
386
- - HNM retriever / 蒸留教師 (`cl-nagoya/ruri-v3-310m` / `ruri-v3-reranker-310m`) は
387
- **Apache 2.0** ライセンスで、商用利用に制限はない。
388
-
389
- ## Citation
390
-
391
- 元データセットを引用してください:
392
-
393
- ```bibtex
394
- @misc{hpprc_emb,
395
- author = {Hayato Tsukagoshi},
396
- title = {hpprc/emb: Japanese text embedding training datasets},
397
- year = {2024},
398
- url = {https://huggingface.co/datasets/hpprc/emb},
399
- }
400
- ```
401
-
402
- 教師モデルの引用:
403
-
404
- ```bibtex
405
- @misc{ruri_v3,
406
- author = {Tsukagoshi, Hayato and Sasano, Ryohei},
407
- title = {Ruri v3: Japanese General Text Embeddings},
408
- year = {2025},
409
- url = {https://huggingface.co/cl-nagoya/ruri-v3-310m},
410
- }
411
- ```
412
-
413
- ## Acknowledgements
414
-
415
- - 元データ: [hpprc/emb — auto-wiki-qa](https://huggingface.co/datasets/hpprc/emb)
416
- - ベース passage コーパス: [Japanese Wikipedia](https://ja.wikipedia.org/)
417
- - HNM 用 retriever: [`cl-nagoya/ruri-v3-310m`](https://huggingface.co/cl-nagoya/ruri-v3-310m)
418
- - 蒸留教師: [`cl-nagoya/ruri-v3-reranker-310m`](https://huggingface.co/cl-nagoya/ruri-v3-reranker-310m)
 
1
+ ---
2
+ language:
3
+ - ja
4
+ license: cc-by-sa-4.0
5
+ size_categories:
6
+ - 1M<n<10M
7
+ task_categories:
8
+ - sentence-similarity
9
+ - text-retrieval
10
+ task_ids:
11
+ - document-retrieval
12
+ pretty_name: Auto Wiki QA (Pairs / Triplets / N-tuples + distillation top-K)
13
+ source_datasets:
14
+ - hpprc/emb
15
+ tags:
16
+ - text
17
+ - japanese
18
+ - wikipedia
19
+ - question-answering
20
+ - retrieval
21
+ - embedding
22
+ - triplet
23
+ - hard-negative-mining
24
+ - contrastive-learning
25
+ - knowledge-distillation
26
+ - cross-encoder
27
+ configs:
28
+ - config_name: pairs
29
+ data_files:
30
+ - split: train
31
+ path: pairs/train-*
32
+ - config_name: triplets
33
+ data_files:
34
+ - split: train
35
+ path: triplets/train-*
36
+ - config_name: n-tuples
37
+ data_files:
38
+ - split: train
39
+ path: n-tuples/train-*
40
+ - config_name: n-tuples-100k
41
+ data_files:
42
+ - split: train
43
+ path: n-tuples-100k/train-*
44
+ - config_name: n-tuples-250k
45
+ data_files:
46
+ - split: train
47
+ path: n-tuples-250k/train-*
48
+ - config_name: n-tuples-500k
49
+ data_files:
50
+ - split: train
51
+ path: n-tuples-500k/train-*
52
+ - config_name: n-tuples-1m
53
+ data_files:
54
+ - split: train
55
+ path: n-tuples-1m/train-*
56
+ default_config: triplets
57
+ ---
58
+
59
+ # auto-wiki-qa
60
+
61
+ 日本語 Wikipedia ベースの自動生成 QA データセット
62
+ ([hpprc/emb](https://huggingface.co/datasets/hpprc/emb) の auto-wiki-qa-dataset /
63
+ auto-wiki-qa-collection) を元に、Hard Negative Mining と
64
+ Cross-Encoder 蒸留スコア付与を施した日本語 retrieval 学習用データセット。
65
+
66
+ pairs / triplets / n-tuples の 3 形式と、n-tuples を quality_score で
67
+ ソートした top-K サブセット (100k / 250k / 500k / 1m) を提供する。
68
+ Dense Retriever / Cross-Encoder / SPLADE などの日本語検索モデル学習および
69
+ KL Divergence 蒸留に使える。
70
+
71
+ > v2 (2026-05) の主な変更点
72
+ > - HNM の retriever を [cl-nagoya/ruri-v3-310m](https://huggingface.co/cl-nagoya/ruri-v3-310m) (Apache 2.0、商用可)、
73
+ > 蒸留教師を [cl-nagoya/ruri-v3-reranker-310m](https://huggingface.co/cl-nagoya/ruri-v3-reranker-310m) (Apache 2.0、商用可) に刷新。
74
+ > - Hard Negative 選定を 単一 margin filter (pos − neg ≥ 4.0、σ で 0.98 相当の分離) に変更。
75
+ > 同テーマ別エントリ・同回答別記事などの false negative リーク を構造的に排除した。
76
+ > - dense retriever のスコアは hard negative 選定に使わず、教師リランカーの生 logit のみで判定する。
77
+
78
+ ## Configs
79
+
80
+ | config | rows | columns | 総トークン数 (cl100k_base) | 説明 |
81
+ |---|---:|---|---:|---|
82
+ | pairs | 2,376,503 | query, answer | 562,891,158 (562.89M) | 元の (query, answer) ペア。mahiyama/auto-wiki-qa (v1) からそのままミラー。 |
83
+ | triplets | 2,149,821 | query, positive, negative | 1,017,027,276 (1.02B) | Hard Negative Mining (v3) を通過した全行。クエリごとに 1 件の hard negative を付けた triplet。 |
84
+ | n-tuples | 2,149,821 | query, positive, negative_1〜negative_5, label | 3,055,829,114 (3.06B) | 同じ採用行に対しクエリごとに 5 件の hard negative + Cross-Encoder の生 logit を label に付与。 |
85
+ | n-tuples-100k | 100,000 | query, positive, negative_1〜negative_5, label | 138,618,253 (138.62M) | n-tuples のうち quality_score の valid を通過した行を降順で並べ、上位 10 万行を抽出 (デバッグ・早期実験向け)。 |
86
+ | n-tuples-250k | 250,000 | query, positive, negative_1〜negative_5, label | 349,362,397 (349.36M) | 同じ valid 行から quality_score 上位 25 万行 (バランス重視)。 |
87
+ | n-tuples-500k | 500,000 | query, positive, negative_1〜negative_5, label | 701,461,481 (701.46M) | 同じ valid 行から quality_score 上位 50 万行。 |
88
+ | n-tuples-1m | 1,000,000 | query, positive, negative_1〜negative_5, label | 1,410,424,585 (1.41B) | 同じ valid 行から quality_score 上位 100 万行 (採用行数が 1M に満たない場合は全採用行)。 |
89
+
90
+ > トークン数は OpenAI の tiktoken cl100k_base (GPT-4 系) で計測。label カラムはトークン数集計の対象外 (浮動小数の数値ベクトル)。
91
+
92
+ すべての config の split は train。n-tuples-* の top-K サブセットは
93
+ quality_score 降順で並んでいるため、行順序は元の n-tuples と一致しない。
94
+
95
+ ### label カラムの形式 (n-tuples / n-tuples-* 共通)
96
+
97
+ ```python
98
+ label: list[float] # 長さ 6 = [score(query, positive), score(query, neg_1), …, score(query, neg_5)]
99
+ ```
100
+
101
+ label[0] が positive、label[1:] が negative_1 〜 negative_5 に 1:1 で順序通り対応する。
102
+ スコアは Cross-Encoder の 生 logit (sigmoid 適用前)。
103
+
104
+ pos − max(neg_1..neg_5) ≥ 4.0 および pos ≥ 2.0 が全行で構造的に成立している
105
+ (下記 Hard Negative Mining セクション参照)。
106
+
107
+ ## Hard Negative Mining の方法 (v3)
108
+
109
+ 各クエリ q_i (元データ行 i) に対して以下を実施:
110
+
111
+ 1. クエリ埋め込み —
112
+ [cl-nagoya/ruri-v3-310m](https://huggingface.co/cl-nagoya/ruri-v3-310m)
113
+ (prefix "検索クエリ: ") で 768 次元にエンコード。
114
+ 2. 文書埋め込み — 同モデル (prefix "検索文書: ") で全 positive (約 240 万件、重複除去後)
115
+ を 768 次元にエンコードし、L2 正規化して
116
+ [faiss-cpu](https://github.com/facebookresearch/faiss) の IndexFlatIP
117
+ (内積 = コサイン類似度) を構築。索引は faiss_index.bin として再利用可能。
118
+ 3. kNN 検索 — 各クエリで上位 25 件を取得し、gold positive と一致する doc_id を除外して
119
+ top-15 を negative 候補プールとする。
120
+ 4. リランカー蒸留スコア付与 —
121
+ [cl-nagoya/ruri-v3-reranker-310m](https://huggingface.co/cl-nagoya/ruri-v3-reranker-310m)
122
+ で (query, positive) と (query, kNN candidate × 15) の合計 16 ペアに
123
+ 生 logit (sigmoid 適用前) を付与。
124
+ 5. Hard Negative 選定 (v3、単一 margin filter)
125
+ - pos_score < 2.0 の行は DROP (positive が弱い)
126
+ - 15 候補のうち pos_score - neg_score >= 4.0 を満たすもののみ採用候補
127
+ - 採用候補が 5 件未満の行は DROP (フォールバックなし)
128
+ - 採用候補をリランカースコア降順で並べ、上位 5 件を negative_1..5 とする
129
+ - negative_1 = 採用候補のうちスコア最大 (= 最も hard だが MIN_DIFF を満たす)
130
+ - negative_5 = 採用候補のうちスコア最小
131
+
132
+ これにより、採用行は以下の条件を構造的に満たす:
133
+
134
+ - pos >= 2.0 (sigmoid 0.88 以上の確信ある positive)
135
+ - pos - neg_i >= 4.0 (i=1..5、すべての negative に対し σ で 0.98 相当の分離)
136
+ - margin = pos - max(neg_1..neg_5) >= 4.0
137
+
138
+ ### なぜ単一 margin filter か (kNN/リランカー top-K 除外をしない理由)
139
+
140
+ NV-Retriever / SFR-Embedding 系の標準レシピでは「kNN 上位 K 件除外」「リランカー上位 K 件除外」
141
+ を組み合わせるが、pos - neg >= 4.0 (σ で 0.98 相当の分離) のような厳しい絶対 margin filter
142
+ を使うと、これらの構造的除外は冗長になる:
143
+
144
+ - false negative リーク (= reranker が高 logit を付ける候補) は margin filter で必ず弾かれる
145
+ - 一方、kNN/reranker top-K 除外を残すと「ぎりぎり通過する硬い negative」が拾えなくなる
146
+
147
+ → v3 では margin filter 1 本に統合することで、量とハードさのバランスを取った。
148
+
149
+ ### v1 (旧 mahiyama/auto-wiki-qa) との違い
150
+
151
+ | 項目 | v1 (旧) | v2 (v3 HNM、本リポジトリ) |
152
+ |---|---|---|
153
+ | Retriever | jinaai/jina-embeddings-v5-text-small (非商用) | cl-nagoya/ruri-v3-310m (Apache 2.0、商用可) |
154
+ | Reranker | cl-nagoya/ruri-reranker-large | cl-nagoya/ruri-v3-reranker-310m |
155
+ | HNM フィルタ | dense score の < pos × 0.95 (positive-aware filter) | reranker logit の pos − neg ≥ 4.0 (絶対 margin) |
156
+ | False negative leak 対策 | なし (定義系・同テーマ別エントリで leak が頻発) | 構造的に排除 |
157
+ | ANN | Elasticsearch HNSW | faiss-cpu IndexFlatIP |
158
+ | 採用行数 | 入力行数と同じ (フォールバックで埋める) | リーク行は DROP (採用率 90.5%) |
159
+ | 商用利用 | 制限あり (Jina) | 制限なし (両モデルとも Apache 2.0) |
160
+
161
+ 代償として行数は減るが、KL 蒸留損失はラベル雑音にとても敏感なため、量を多少削ってでも
162
+ クリーンな negative を確保する方が学習効率は明確に良い。
163
+
164
+ ### 採用統計
165
+
166
+ | 指標 | 値 |
167
+ |---|---:|
168
+ | 入力行数 (リランカーで採点した行) | 2,376,503 |
169
+ | 採用行数 (kept: HNM 通過) | 2,149,821 (90.5%) |
170
+ | Drop: 弱 positive (pos < 2.0) | 194,350 (8.2%) |
171
+ | Drop: 候補不足 (filter 通過 < 5 件) | 32,332 (1.4%) |
172
+ | quality_score の valid 通過 (採用行に対して) | 2,149,821 (100.00%) |
173
+
174
+ triplets / n-tuples には採用行のみが入る。
175
+ n-tuples-{100k, 250k, 500k, 1m} は採用行のうち quality_score の valid を通過した行を
176
+ 降順で並べた top-K サブセット。v3 HNM が pos>=2.0 と margin>=4.0 を保証するため、
177
+ valid 通過率は実質 100% になる (透明性のためチェックは入れている)。
178
+
179
+ ## 蒸留スコア付けの方法
180
+
181
+ n-tuples 配下の label カラムおよび n-tuples-* の top-K サブセットは、
182
+ 教師モデル cl-nagoya/ruri-v3-reranker-310m (Japanese ModernBERT Cross-Encoder、Apache 2.0)
183
+ の 生 logit を各 (query, doc) ペア (positive 1 件 + negative 5 件) に付与したもの。
184
+
185
+ ### スコア付けの設定
186
+
187
+ | 項目 | 値 |
188
+ |---|---|
189
+ | Teacher | cl-nagoya/ruri-v3-reranker-310m |
190
+ | Max length | 512 |
191
+ | 推論精度 | FP16 |
192
+ | Batch size | 128 |
193
+ | 入力フォーマット | 単一文ペア (query, document) |
194
+ | 出力 | 生 logit (sigmoid 適用前)。観測 logit レンジ おおむね ±15 (採用行の positive 平均 ≈ 8.80) |
195
+
196
+ KL 蒸留損失 (SparseDistillKLDivLoss / DistillKLDivLoss 等) は
197
+ softmax(label / T) を target distribution として使うため、
198
+ sigmoid 後の [0, 1] 値だと softmax target が一様分布に潰れて ranking 情報が失われる。
199
+ これを防ぐため、教師の最終活性化を明示的に Identity に置き換えてスコアを出力した:
200
+
201
+ ```python
202
+ import torch.nn as nn
203
+ from sentence_transformers import CrossEncoder
204
+
205
+ teacher = CrossEncoder(
206
+ "cl-nagoya/ruri-v3-reranker-310m",
207
+ max_length=512,
208
+ device="cuda",
209
+ )
210
+ teacher.model.eval()
211
+ teacher.default_activation_function = nn.Identity() # sigmoid を外す
212
+
213
+ scores = teacher.predict(
214
+ [("query A", "passage A"), ("query A", "passage B")],
215
+ batch_size=128,
216
+ convert_to_numpy=True,
217
+ activation_fct=nn.Identity(), # 念のため predict() でも明示
218
+ )
219
+ # scores: array([12.34, -3.56], dtype=float32)
220
+ assert abs(scores).max() > 1.0 # |score|max > 1 で sigmoid 混入チェック
221
+ ```
222
+
223
+ logit のスケール感:
224
+
225
+ | logit | sigmoid(logit) | 解釈 |
226
+ | ----: | --------------: | ------------------------------------- |
227
+ | +8 | 0.9997 | 確信度高く relevant |
228
+ | +5 | 0.993 | 強く relevant |
229
+ | +2 | 0.881 | relevant 寄り |
230
+ | 0 | 0.5 | 不確か |
231
+ | -2 | 0.119 | irrelevant 寄り |
232
+ | -5 | 0.007 | 強く irrelevant |
233
+ | -8 | 0.0003 | 確信度高く irrelevant |
234
+
235
+ ### quality_score: top-K サブセットのソート指標
236
+
237
+ n-tuples-100k / -250k / -500k / -1m は、n-tuples を以下の手順で構築する:
238
+
239
+ 1. quality_score の valid マスク を計算 (distillation_score.md 準拠)
240
+ 2. valid を通過した行を quality_score 降順で並べる
241
+ 3. 上位 K 行を抽出 (採用行数が K に満たない場合は全採用行)
242
+
243
+ v3 HNM が pos >= 2.0 と全 neg について pos - neg >= 4.0 を保証するため、
244
+ valid マスクの 3 条件 (pos>=2.0, margin>0, margin>=0.5) は全て自動的に成立し、
245
+ valid 通過率は実質 100% (上記採用統計セクション参照)。
246
+ それでもチェックを入れているのは、品質スコア計算ロジックの形式的整合性のため。
247
+
248
+ #### 定義 (実装通り)
249
+
250
+ ```python
251
+ import numpy as np
252
+
253
+ POS_MIN = 2.0 # σ(2.0) ≈ 0.88: 確信のある positive のみ採用
254
+ MARGIN_MIN = 0.5 # 誤差レベルの分離は雑音と見なす
255
+ MARGIN_PENALTY = 0.1 # 易しすぎる行を弱く後回しにする係数
256
+
257
+ def quality_score_vec(labels: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
258
+ """
259
+ Args:
260
+ labels: (N, 6) float32. 生 logit。 [p, n1, n2, n3, n4, n5].
261
+ Returns:
262
+ scores: (N,) float. valid 落ちした行は -inf。
263
+ valid: (N,) bool. pos>=2.0 ∧ margin>0 ∧ margin>=0.5
264
+ """
265
+ p = labels[:, 0]
266
+ neg = labels[:, 1:]
267
+ max_n = neg.max(axis=1)
268
+ mean_n = neg.mean(axis=1)
269
+ margin = p - max_n
270
+
271
+ fn_mask = margin <= 0
272
+ weak_mask = (~fn_mask) & (p < POS_MIN)
273
+ border_mask = (~fn_mask) & (~weak_mask) & (margin < MARGIN_MIN)
274
+ valid_mask = (~fn_mask) & (~weak_mask) & (~border_mask)
275
+
276
+ raw = mean_n - MARGIN_PENALTY * margin
277
+ scores = np.where(valid_mask, raw, -np.inf)
278
+ return scores, valid_mask
279
+ ```
280
+
281
+ - mean(neg) を主軸にする理由 — KL 蒸留は softmax(label / T) を target にするため、
282
+ negative 全体の logit 分布が target 形状を決める。mean(neg) が高い行ほど target が
283
+ 滑らかな分布になり、ranking 学習が効く。
284
+ - margin に弱い負ペナルティを掛ける理由 — margin が極端に大きい「楽勝行」を
285
+ タイブレークで後回しにするため。
286
+
287
+ #### Hard Negative の観測レンジ (採用行に対して)
288
+
289
+ | 項目 | 観測値 |
290
+ | ----- | -----: |
291
+ | positive logit (平均) | 8.80 |
292
+ | positive logit (5%-tile / 95%-tile) | 3.68 / 12.02 |
293
+ | neg_1 logit (平均、最硬の negative) | -1.50 |
294
+ | neg_5 logit (平均、5 番目の negative) | -5.35 |
295
+ | margin = pos − max(neg) (平均) | 10.30 |
296
+ | margin = pos − max(neg) (最小) | 4.00 |
297
+
298
+ 採用行の最小 margin が MIN_DIFF_FROM_POS = 4.0 を下回らないことが構造的に保証される
299
+ (Hard Negative 選定段階で pos - neg >= 4.0 を全 5 件に対して要求しているため)。
300
+
301
+ ### KL 蒸留損失への組み込み (PyTorch 例)
302
+
303
+ ```python
304
+ import torch.nn.functional as F
305
+
306
+ def kl_distill_loss(student_logits, teacher_label, T=2.0):
307
+ """
308
+ Args:
309
+ student_logits: (B, 6) student の (query, doc_i) スコア (生 logit)
310
+ teacher_label: (B, 6) label カラムをそのまま (生 logit)
311
+ T: distillation temperature (推奨 1.0〜4.0、生 logit レンジが広めなので 2.0 前後が扱いやすい)
312
+ """
313
+ log_p_student = F.log_softmax(student_logits / T, dim=-1)
314
+ p_teacher = F.softmax(teacher_label / T, dim=-1)
315
+ return F.kl_div(log_p_student, p_teacher, reduction="batchmean") * (T * T)
316
+ ```
317
+
318
+ sentence-transformers の SparseDistillKLDivLoss / DistillKLDivLoss を使う場合は、
319
+ label カラムをそのまま teacher score として渡せばよい。
320
+
321
+ ## 変更履歴
322
+
323
+ | バージョン | リポジトリ | 日付 | 概要 |
324
+ |---|---|---|---|
325
+ | v2 (= 本リポジトリ) | mahiyama/auto-wiki-qa (Public) | 2026-05 | HNM retriever を cl-nagoya/ruri-v3-310m に、教師 reranker を cl-nagoya/ruri-v3-reranker-310m に刷新。faiss-cpu IndexFlatIP ベース。Hard Negative 選定を 単一 margin filter (pos − neg ≥ 4.0) に変更し、false negative リークを構造的に排除。両モデルとも Apache 2.0 で商用利用可。 |
326
+ | v1 | mahiyama/auto-wiki-qa (Public) | 2024 | 初版。HNM retriever = jinaai/jina-embeddings-v5-text-small、教師 reranker = cl-nagoya/ruri-reranker-large。フィルタは < pos × 0.95 の positive-aware filter のみ。 |
327
+
328
+ ## License
329
+
330
+ CC BY-SA 4.0
331
+
332
+ - Passage 本文は��本語版 Wikipedia 由来であり、CC BY-SA 4.0
333
+ (Wikipedia コンテンツの表示・継承ライセンス) を継承する。
334
+ - クエリは hpprc/emb の auto-wiki-qa 由来。利用・再配布時は元データセットの
335
+ ライセンスと利用条件にも従うこと。
336
+ - HNM retriever / 蒸留教師 (cl-nagoya/ruri-v3-310m / ruri-v3-reranker-310m) は
337
+ Apache 2.0 ライセンスで、商用利用に制限はない。
338
+
339
+ ## Citation
340
+
341
+ 元データセットを引用してください:
342
+
343
+ ```bibtex
344
+ @misc{hpprc_emb,
345
+ author = {Hayato Tsukagoshi},
346
+ title = {hpprc/emb: Japanese text embedding training datasets},
347
+ year = {2024},
348
+ url = {https://huggingface.co/datasets/hpprc/emb},
349
+ }
350
+ ```
351
+
352
+ 教師モデルの引用:
353
+
354
+ ```bibtex
355
+ @misc{ruri_v3,
356
+ author = {Tsukagoshi, Hayato and Sasano, Ryohei},
357
+ title = {Ruri v3: Japanese General Text Embeddings},
358
+ year = {2025},
359
+ url = {https://huggingface.co/cl-nagoya/ruri-v3-310m},
360
+ }
361
+ ```
362
+
363
+ ## Acknowledgements
364
+
365
+ - 元データ: [hpprc/emb — auto-wiki-qa](https://huggingface.co/datasets/hpprc/emb)
366
+ - ベース passage コーパス: [Japanese Wikipedia](https://ja.wikipedia.org/)
367
+ - HNM 用 retriever: [cl-nagoya/ruri-v3-310m](https://huggingface.co/cl-nagoya/ruri-v3-310m)
368
+ - 蒸留教師: [cl-nagoya/ruri-v3-reranker-310m](https://huggingface.co/cl-nagoya/ruri-v3-reranker-310m)