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

This commit is contained in:
Translator 2025-08-04 12:31:57 +00:00
parent 7318106fa3
commit f7328f5a39

View File

@ -15,7 +15,7 @@ LLMs जैसे GPT को टेक्स्ट उत्पन्न कर
### **Key Concepts in Data Sampling**
1. **Tokenization:** टेक्स्ट को छोटे इकाइयों में तोड़ना जिन्हें टोकन कहा जाता है (जैसे, शब्द, उपशब्द, या वर्ण)।
1. **Tokenization:** टेक्स्ट को छोटे इकाइयों में तोड़ना जिन्हें टोकन कहा जाता है (जैसे, शब्द, उपशब्द, या अक्षर)।
2. **Sequence Length (max_length):** प्रत्येक इनपुट अनुक्रम में टोकनों की संख्या।
3. **Sliding Window:** एक विधि जो टोकनाइज्ड टेक्स्ट पर एक विंडो को आगे बढ़ाकर ओवरलैपिंग इनपुट अनुक्रम बनाने के लिए उपयोग की जाती है।
4. **Stride:** अगला अनुक्रम बनाने के लिए स्लाइडिंग विंडो द्वारा आगे बढ़ाए गए टोकनों की संख्या।
@ -30,7 +30,7 @@ LLMs जैसे GPT को टेक्स्ट उत्पन्न कर
```
**Tokenization**
मान लें कि हम एक **बुनियादी टोकनाइज़र** का उपयोग करते हैं जो पाठ को शब्दों और विराम चिह्नों में विभाजित करता है:
मान लीजिए कि हम एक **बुनियादी टोकनाइज़र** का उपयोग करते हैं जो पाठ को शब्दों और विराम चिह्नों में विभाजित करता है:
```vbnet
Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit."]
```
@ -87,7 +87,7 @@ Tokens: ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing",
- **स्ट्राइड 1:** विंडो हर बार एक टोकन आगे बढ़ती है, जिससे अत्यधिक ओवरलैपिंग अनुक्रम बनते हैं। यह संदर्भ संबंधों के बेहतर अध्ययन की ओर ले जा सकता है लेकिन ओवरफिटिंग का जोखिम बढ़ा सकता है क्योंकि समान डेटा बिंदु दोहराए जाते हैं।
- **स्ट्राइड 2:** विंडो हर बार दो टोकन आगे बढ़ती है, ओवरलैप को कम करती है। यह पुनरावृत्ति और गणनात्मक लोड को कम करता है लेकिन कुछ संदर्भ की बारीकियों को चूक सकता है।
- **स्ट्राइड अधिकतम_length के बराबर:** विंडो पूरी विंडो आकार द्वारा आगे बढ़ती है, जिससे गैर-ओवरलैपिंग अनुक्रम बनते हैं। यह डेटा पुनरावृत्ति को कम करता है लेकिन मॉडल की अनुक्रमों के बीच निर्भरताओं को सीखने की क्षमता को सीमित कर सकता है।
- **स्ट्राइड जो max_length के बराबर है:** विंडो पूरी विंडो आकार द्वारा आगे बढ़ती है, जिससे गैर-ओवरलैपिंग अनुक्रम बनते हैं। यह डेटा पुनरावृत्ति को कम करता है लेकिन मॉडल की अनुक्रमों के बीच निर्भरताओं को सीखने की क्षमता को सीमित कर सकता है।
**स्ट्राइड 2 के साथ उदाहरण:**
@ -230,9 +230,70 @@ tensor([[ 367, 2885, 1464, 1807],
[ 3285, 326, 11, 287]])
]
```
## संदर्भ
## Advanced Sampling Strategies (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. Temperature-Based Mixture Weighting
State-of-the-art LLMs को कभी-कभी एकल कॉर्पस पर प्रशिक्षित नहीं किया जाता है। इसके बजाय, वे कई विषम डेटा स्रोतों (कोड, वेब, शैक्षणिक पत्र, फोरम…) से सैंपल करते हैं। प्रत्येक स्रोत का सापेक्ष अनुपात डाउनस्ट्रीम प्रदर्शन को मजबूत रूप से प्रभावित कर सकता है। हाल के ओपन-सोर्स मॉडल जैसे Llama 2 ने एक **temperaturebased sampling scheme** पेश किया है जहाँ कॉर्पस *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}}