Translated ['src/AI/AI-llm-architecture/2.-data-sampling.md'] to zh

This commit is contained in:
Translator 2025-08-04 12:31:18 +00:00
parent aa79b6daf7
commit 10ccc62d92

View File

@ -4,10 +4,10 @@
## **数据采样**
**数据采样**是为训练大型语言模型LLMs如GPT准备数据的关键过程。它涉及将文本数据组织成输入和目标序列,模型利用这些序列学习如何根据前面的单词预测下一个单词(或标记)。适当的数据采样确保模型有效捕捉语言模式和依赖关系。
**数据采样**是为训练大型语言模型LLMs如GPT准备数据的关键过程。它涉及将文本数据组织成模型用于学习如何根据前面的单词预测下一个单词(或标记)的输入和目标序列。适当的数据采样确保模型有效捕捉语言模式和依赖关系。
> [!TIP]
> 第二阶段的目标非常简单:**对输入数据进行采样,并为训练阶段准备,通常通过将数据集分成特定长度的句子,并生成预期的响应。**
> 这个第二阶段的目标非常简单:**对输入数据进行采样,并为训练阶段准备,通常通过将数据集分成特定长度的句子,并生成预期的响应。**
### **为什么数据采样很重要**
@ -28,9 +28,9 @@
```arduino
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
```
**分词**
**Tokenization**
假设我们使用一个**基本分词器**,将文本拆分为单词和标点符号:
假设我们使用一个**基本的分词器**,将文本分割成单词和标点符号:
```vbnet
Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit."]
```
@ -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 2040 % 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. OpenAIs 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. OpenAIs 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}}