mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
Translated ['src/AI/AI-llm-architecture/2.-data-sampling.md'] to ja
This commit is contained in:
parent
29db9c91cd
commit
402adc00fe
@ -4,7 +4,7 @@
|
||||
|
||||
## **データサンプリング**
|
||||
|
||||
**データサンプリング**は、GPTのような大規模言語モデル(LLM)のトレーニングのためにデータを準備する際の重要なプロセスです。これは、モデルが前の単語に基づいて次の単語(またはトークン)を予測する方法を学ぶために使用する入力とターゲットのシーケンスにテキストデータを整理することを含みます。適切なデータサンプリングは、モデルが言語パターンと依存関係を効果的に捉えることを保証します。
|
||||
**データサンプリング**は、GPTのような大規模言語モデル(LLM)のトレーニングのためにデータを準備する際の重要なプロセスです。これは、モデルが前の単語に基づいて次の単語(またはトークン)を予測する方法を学ぶために使用する入力とターゲットのシーケンスにテキストデータを整理することを含みます。適切なデータサンプリングは、モデルが言語パターンや依存関係を効果的に捉えることを保証します。
|
||||
|
||||
> [!TIP]
|
||||
> この第二段階の目標は非常にシンプルです:**入力データをサンプリングし、通常はデータセットを特定の長さの文に分け、期待される応答も生成してトレーニングフェーズのために準備します。**
|
||||
@ -13,16 +13,16 @@
|
||||
|
||||
GPTのようなLLMは、前の単語によって提供されるコンテキストを理解することによってテキストを生成または予測するようにトレーニングされています。これを達成するためには、トレーニングデータは、モデルが単語のシーケンスとその後の単語との関係を学べるように構造化されている必要があります。この構造化されたアプローチにより、モデルは一般化し、一貫性があり文脈に関連したテキストを生成することができます。
|
||||
|
||||
### **データサンプリングの重要な概念**
|
||||
### **データサンプリングの主要概念**
|
||||
|
||||
1. **トークン化:** テキストをトークン(例:単語、サブワード、または文字)と呼ばれる小さな単位に分解すること。
|
||||
2. **シーケンスの長さ (max_length):** 各入力シーケンス内のトークンの数。
|
||||
3. **スライディングウィンドウ:** トークン化されたテキストの上をウィンドウを移動させることによって重複する入力シーケンスを作成する方法。
|
||||
4. **ストライド:** スライディングウィンドウが次のシーケンスを作成するために前進するトークンの数。
|
||||
2. **シーケンスの長さ(max_length):** 各入力シーケンス内のトークンの数。
|
||||
3. **スライディングウィンドウ:** トークン化されたテキスト上でウィンドウを移動させることによって重複する入力シーケンスを作成する方法。
|
||||
4. **ストライド:** 次のシーケンスを作成するためにスライディングウィンドウが前方に移動するトークンの数。
|
||||
|
||||
### **ステップバイステップの例**
|
||||
|
||||
データサンプリングを説明するための例を見てみましょう。
|
||||
データサンプリングを説明するために、例を見ていきましょう。
|
||||
|
||||
**例文**
|
||||
```arduino
|
||||
@ -30,7 +30,7 @@ GPTのようなLLMは、前の単語によって提供されるコンテキス
|
||||
```
|
||||
**トークン化**
|
||||
|
||||
基本的な**トークナイザー**を使用して、テキストを単語と句読点に分割すると仮定します:
|
||||
基本的な**トークナイザー**を使用して、テキストを単語と句読点に分割すると仮定します:
|
||||
```vbnet
|
||||
Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit."]
|
||||
```
|
||||
@ -87,11 +87,11 @@ Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing",
|
||||
|
||||
- **ストライド1:** ウィンドウは毎回1トークン前進し、非常に重複したシーケンスを生成します。これにより、文脈関係の学習が向上する可能性がありますが、類似のデータポイントが繰り返されるため、過学習のリスクが高まる可能性があります。
|
||||
- **ストライド2:** ウィンドウは毎回2トークン前進し、重複を減らします。これにより冗長性と計算負荷が減少しますが、文脈のニュアンスを見逃す可能性があります。
|
||||
- **ストライドがmax_lengthに等しい:** ウィンドウは全体のウィンドウサイズ分前進し、重複のないシーケンスを生成します。これによりデータの冗長性が最小限に抑えられますが、シーケンス間の依存関係を学習するモデルの能力が制限される可能性があります。
|
||||
- **max_lengthに等しいストライド:** ウィンドウは全ウィンドウサイズ分前進し、重複のないシーケンスを生成します。これによりデータの冗長性が最小限に抑えられますが、シーケンス間の依存関係を学習するモデルの能力が制限される可能性があります。
|
||||
|
||||
**ストライド2の例:**
|
||||
|
||||
同じトークン化されたテキストと `max_length` が4の場合:
|
||||
同じトークン化されたテキストと `max_length` 4を使用して:
|
||||
|
||||
- **最初のウィンドウ (位置 1-4):** \["Lorem", "ipsum", "dolor", "sit"] → **ターゲット:** \["ipsum", "dolor", "sit", "amet,"]
|
||||
- **2番目のウィンドウ (位置 3-6):** \["dolor", "sit", "amet,", "consectetur"] → **ターゲット:** \["sit", "amet,", "consectetur", "adipiscing"]
|
||||
@ -230,9 +230,70 @@ tensor([[ 367, 2885, 1464, 1807],
|
||||
[ 3285, 326, 11, 287]])
|
||||
]
|
||||
```
|
||||
## 参考文献
|
||||
## 高度なサンプリング戦略 (2023-2025)
|
||||
|
||||
- [https://www.manning.com/books/build-a-large-language-model-from-scratch](https://www.manning.com/books/build-a-large-language-model-from-scratch)
|
||||
### 1. 温度ベースの混合重み付け
|
||||
最先端のLLMは、単一のコーパスでトレーニングされることはほとんどありません。代わりに、いくつかの異種データソース(コード、ウェブ、学術論文、フォーラムなど)からサンプリングします。各ソースの相対的な割合は、下流のパフォーマンスに強く影響を与える可能性があります。最近のオープンソースモデルであるLlama 2は、コーパス*i*から文書を引き出す確率が温度に基づくサンプリングスキームを導入しました。
|
||||
```
|
||||
p(i) = \frac{w_i^{\alpha}}{\sum_j w_j^{\alpha}}
|
||||
```
|
||||
• *w<sub>i</sub>* – コーパス *i* の生トークン割合
|
||||
• *α* ("温度") – (0,1] の値。 α < 1 は分布を平坦にし、より小さな高品質コーパスに重みを与えます。
|
||||
|
||||
Llama 2 は α = 0.7 を使用し、α を減少させることで知識重視のタスクで評価スコアが向上する一方で、トレーニングミックスを安定させることを示しました。同じ手法が Mistral (2023) と Claude 3 にも採用されています。
|
||||
```python
|
||||
from collections import Counter
|
||||
|
||||
def temperature_sample(corpus_ids, alpha=0.7):
|
||||
counts = Counter(corpus_ids) # number of tokens seen per corpus
|
||||
probs = {c: c_count**alpha for c, c_count in counts.items()}
|
||||
Z = sum(probs.values())
|
||||
probs = {c: p/Z for c, p in probs.items()}
|
||||
# Now draw according to probs to fill every batch
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
### 2. Sequence Packing / Dynamic Batching
|
||||
GPU memory is wasted when every sequence in a batch is padded to the longest example. "Packing" concatenates multiple shorter sequences until the **exact** `max_length` is reached and builds a parallel `attention_mask` so that tokens do not attend across segment boundaries. Packing can improve throughput by 20–40 % with no gradient change and is supported out-of-the-box in
|
||||
|
||||
* PyTorch `torchtext.experimental.agents.PackedBatch`
|
||||
* HuggingFace `DataCollatorForLanguageModeling(pad_to_multiple_of=…)`
|
||||
|
||||
Dynamic batching frameworks (e.g. FlashAttention 2, vLLM 2024) combine sequence packing with just-in-time kernel selection, enabling thousand-token context training at 400+ K tokens/s on A100-80G.
|
||||
|
||||
### 3. Deduplication & Quality Filtering
|
||||
Repeated passages cause memorization and provide an easy channel for data-poisoning. Modern pipelines therefore:
|
||||
|
||||
1. MinHash/FAISS near-duplicate detection at **document** and **128-gram** level.
|
||||
2. Filter documents whose perplexity under a small reference model is > µ + 3σ (noisy OCR, garbled HTML).
|
||||
3. Block-list documents that contain PII or CWE keywords using regex & spaCy NER.
|
||||
|
||||
The Llama 2 team deduplicated with 8-gram MinHash and removed ~15 % of CommonCrawl before sampling. OpenAI’s 2024 "Deduplicate Everything" paper demonstrates ≤0.04 duplicate ratio reduces over-fitting and speeds convergence.
|
||||
|
||||
## Security & Privacy Considerations During Sampling
|
||||
|
||||
### Data-Poisoning / Backdoor Attacks
|
||||
Researchers showed that inserting <1 % backdoored sentences can make a model obey a hidden trigger ("PoisonGPT", 2023). Recommended mitigations:
|
||||
|
||||
* **Shuffled mixing** – make sure adjacent training examples originate from different sources; this dilutes gradient alignment of malicious spans.
|
||||
* **Gradient similarity scoring** – compute cosine similarity of example gradient to batch average; outliers are candidates for removal.
|
||||
* **Dataset versioning & hashes** – freeze immutable tarballs and verify SHA-256 before each training run.
|
||||
|
||||
### Membership-Inference & Memorization
|
||||
Long overlap between sliding-window samples increases the chance that rare strings (telephone numbers, secret keys) are memorized. OpenAI’s 2024 study on ChatGPT memorization reports that raising stride from 1 × `max_length` to 4 × reduces verbatim leakage by ≈50 % with negligible loss in perplexity.
|
||||
|
||||
Practical recommendations:
|
||||
|
||||
* Use **stride ≥ max_length** except for <1B parameter models where data volume is scarce.
|
||||
* Add random masking of 1-3 tokens per window during training; this lowers memorization while preserving utility.
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- [Build a Large Language Model from Scratch (Manning, 2024)](https://www.manning.com/books/build-a-large-language-model-from-scratch)
|
||||
- [Llama 2: Open Foundation and Fine-Tuned Chat Models (2023)](https://arxiv.org/abs/2307.09288)
|
||||
- [PoisonGPT: Assessing Backdoor Vulnerabilities in Large Language Models (BlackHat EU 2023)](https://arxiv.org/abs/2308.12364)
|
||||
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user