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 tr
This commit is contained in:
parent
77853a7048
commit
3729359b9c
@ -37,18 +37,18 @@ Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing",
|
||||
**Parametreler**
|
||||
|
||||
- **Maksimum Dizi Uzunluğu (max_length):** 4 token
|
||||
- **Kaydırmalı Pencere Adımı:** 1 token
|
||||
- **Kaydırma Penceresi Adımı:** 1 token
|
||||
|
||||
**Girdi ve Hedef Dizileri Oluşturma**
|
||||
|
||||
1. **Kaydırmalı Pencere Yaklaşımı:**
|
||||
1. **Kaydırma Penceresi Yaklaşımı:**
|
||||
- **Girdi Dizileri:** Her girdi dizisi `max_length` token içerir.
|
||||
- **Hedef Dizileri:** Her hedef dizisi, ilgili girdi dizisini hemen takip eden token'ları içerir.
|
||||
2. **Dizileri Oluşturma:**
|
||||
|
||||
<table><thead><tr><th width="177">Pencere Pozisyonu</th><th>Girdi Dizisi</th><th>Hedef Dizisi</th></tr></thead><tbody><tr><td>1</td><td>["Lorem", "ipsum", "dolor", "sit"]</td><td>["ipsum", "dolor", "sit", "amet,"]</td></tr><tr><td>2</td><td>["ipsum", "dolor", "sit", "amet,"]</td><td>["dolor", "sit", "amet,", "consectetur"]</td></tr><tr><td>3</td><td>["dolor", "sit", "amet,", "consectetur"]</td><td>["sit", "amet,", "consectetur", "adipiscing"]</td></tr><tr><td>4</td><td>["sit", "amet,", "consectetur", "adipiscing"]</td><td>["amet,", "consectetur", "adipiscing", "elit."]</td></tr></tbody></table>
|
||||
|
||||
3. **Sonuç Girdi ve Hedef Dizileri:**
|
||||
3. **Elde Edilen Girdi ve Hedef Dizileri:**
|
||||
|
||||
- **Girdi:**
|
||||
|
||||
@ -72,18 +72,18 @@ Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing",
|
||||
]
|
||||
```
|
||||
|
||||
**Görsel Temsili**
|
||||
**Görsel Temsil**
|
||||
|
||||
<table><thead><tr><th width="222">Token Pozisyonu</th><th>Token</th></tr></thead><tbody><tr><td>1</td><td>Lorem</td></tr><tr><td>2</td><td>ipsum</td></tr><tr><td>3</td><td>dolor</td></tr><tr><td>4</td><td>sit</td></tr><tr><td>5</td><td>amet,</td></tr><tr><td>6</td><td>consectetur</td></tr><tr><td>7</td><td>adipiscing</td></tr><tr><td>8</td><td>elit.</td></tr></tbody></table>
|
||||
|
||||
**Adım 1 ile Kaydırmalı Pencere:**
|
||||
**Adım 1 ile Kaydırma Penceresi:**
|
||||
|
||||
- **İlk Pencere (Pozisyonlar 1-4):** \["Lorem", "ipsum", "dolor", "sit"] → **Hedef:** \["ipsum", "dolor", "sit", "amet,"]
|
||||
- **İkinci Pencere (Pozisyonlar 2-5):** \["ipsum", "dolor", "sit", "amet,"] → **Hedef:** \["dolor", "sit", "amet,", "consectetur"]
|
||||
- **Üçüncü Pencere (Pozisyonlar 3-6):** \["dolor", "sit", "amet,", "consectetur"] → **Hedef:** \["sit", "amet,", "consectetur", "adipiscing"]
|
||||
- **Dördüncü Pencere (Pozisyonlar 4-7):** \["sit", "amet,", "consectetur", "adipiscing"] → **Hedef:** \["amet,", "consectetur", "adipiscing", "elit."]
|
||||
|
||||
**Adım Anlama**
|
||||
**Adımı Anlama**
|
||||
|
||||
- **Adım 1:** Pencere her seferinde bir token ileri hareket eder, bu da yüksek oranda örtüşen dizilerle sonuçlanır. Bu, bağlamsal ilişkilerin daha iyi öğrenilmesine yol açabilir ancak benzer veri noktalarının tekrar edilmesi nedeniyle aşırı uyum riski artırabilir.
|
||||
- **Adım 2:** Pencere her seferinde iki token ileri hareket eder, örtüşmeyi azaltır. Bu, tekrarları ve hesaplama yükünü azaltır ancak bazı bağlamsal nüansları kaçırabilir.
|
||||
@ -91,7 +91,7 @@ Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing",
|
||||
|
||||
**Adım 2 ile Örnek:**
|
||||
|
||||
Aynı tokenleştirilmiş metni ve `max_length` 4'ü kullanarak:
|
||||
Aynı tokenleştirilmiş metni ve `max_length` değerini 4 kullanarak:
|
||||
|
||||
- **İlk Pencere (Pozisyonlar 1-4):** \["Lorem", "ipsum", "dolor", "sit"] → **Hedef:** \["ipsum", "dolor", "sit", "amet,"]
|
||||
- **İkinci Pencere (Pozisyonlar 3-6):** \["dolor", "sit", "amet,", "consectetur"] → **Hedef:** \["sit", "amet,", "consectetur", "adipiscing"]
|
||||
@ -99,7 +99,7 @@ Aynı tokenleştirilmiş metni ve `max_length` 4'ü kullanarak:
|
||||
|
||||
## Kod Örneği
|
||||
|
||||
Bunu [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb) adresinden bir kod örneği ile daha iyi anlayalım:
|
||||
Bunu daha iyi anlamak için [https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb](https://github.com/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb) adresinden bir kod örneğine bakalım:
|
||||
```python
|
||||
# Download the text to pre-train the LLM
|
||||
import urllib.request
|
||||
@ -230,9 +230,70 @@ tensor([[ 367, 2885, 1464, 1807],
|
||||
[ 3285, 326, 11, 287]])
|
||||
]
|
||||
```
|
||||
## Referanslar
|
||||
## Gelişmiş Örnekleme Stratejileri (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. Sıcaklık Tabanlı Karışım Ağırlığı
|
||||
En son teknoloji LLM'ler nadiren tek bir veri kümesi üzerinde eğitilir. Bunun yerine, çeşitli heterojen veri kaynaklarından (kod, web, akademik makaleler, forumlar…) örnekleme yaparlar. Her bir kaynağın göreli oranı, aşağı akış performansını güçlü bir şekilde etkileyebilir. Llama 2 gibi son açık kaynaklı modeller, *i* veri kümesinden bir belge çekme olasılığının
|
||||
```
|
||||
p(i) = \frac{w_i^{\alpha}}{\sum_j w_j^{\alpha}}
|
||||
```
|
||||
• *w<sub>i</sub>* – ham token yüzdesi corpus *i* için
|
||||
• *α* ("sıcaklık") – (0,1] aralığında bir değer. α < 1 dağılımı düzleştirir, daha küçük yüksek kaliteli corpuslara daha fazla ağırlık verir.
|
||||
|
||||
Llama 2, α = 0.7 kullandı ve α'nın azaltılmasının bilgi ağırlıklı görevlerde değerlendirme puanlarını artırdığını gösterdi, eğitim karışımını sabit tutarken. Aynı numara Mistral (2023) ve Claude 3 tarafından benimsenmiştir.
|
||||
```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