LLM Data Sampling
A skill for preparing and sampling text data for training large language models (LLMs). This covers tokenization, sequence generation, sliding windows, and advanced sampling strategies.
When to Use This Skill
Use this skill when the user needs to:
- Prepare text data for LLM training
- Create input/target token sequences
- Implement sliding window sampling
- Apply advanced sampling strategies (temperature weighting, sequence packing, deduplication)
- Optimize training data quality and security
- Create PyTorch datasets and dataloaders for LLM training
Core Concepts
1. Tokenization
Breaking text into smaller units (tokens) that the model processes. Common approaches:
- Word-level: Split by spaces
- Subword-level: BPE, WordPiece (used by GPT-2, BERT)
- Character-level: Individual characters
2. Sequence Length (max_length)
The number of tokens in each input sequence. Typical values:
- Small models: 256-512 tokens
- Medium models: 512-1024 tokens
- Large models: 1024-4096+ tokens
3. Sliding Window
A method to create overlapping input sequences by moving a window over tokenized text.
4. Stride
The number of tokens the sliding window moves forward. Key tradeoffs:
- Stride = 1: Maximum overlap, better context learning, higher overfitting risk
- Stride = max_length: No overlap, less redundancy, may miss dependencies
- Stride = 2-4×max_length: Recommended for most cases to balance context and efficiency
Step-by-Step Data Sampling
Basic Workflow
- Load and tokenize text
- Apply sliding window to create sequences
- Generate input/target pairs (target is input shifted by 1 token)
- Create dataset and dataloader for training
Example: Creating Input/Target Sequences
Given text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
With max_length=4 and stride=1:
| Window |
Input Sequence |
Target Sequence |
| 1 |
["Lorem", "ipsum", "dolor", "sit"] |
["ipsum", "dolor", "sit", "amet,"] |
| 2 |
["ipsum", "dolor", "sit", "amet,"] |
["dolor", "sit", "amet,", "consectetur"] |
| 3 |
["dolor", "sit", "amet,", "consectetur"] |
["sit", "amet,", "consectetur", "adipiscing"] |
Implementation Guide
Using the Sampling Script
The bundled script scripts/sample_data.py handles the complete data sampling pipeline:
# Basic usage
python scripts/sample_data.py \
--input "path/to/text.txt" \
--output "path/to/output.jsonl" \
--max-length 256 \
--stride 128 \
--batch-size 8
# With advanced options
python scripts/sample_data.py \
--input "data/" \
--output "processed/" \
--max-length 512 \
--stride 512 \
--temperature 0.7 \
--deduplicate \
--shuffle
Key Parameters
| Parameter |
Description |
Recommended Value |
max_length |
Sequence length in tokens |
256-1024 |
stride |
Window step size |
≥ max_length for most cases |
batch_size |
Samples per batch |
8-32 (depends on GPU) |
temperature |
Sampling temperature (α) |
0.7 for mixed corpora |
shuffle |
Randomize order |
True for training |
Advanced Sampling Strategies
1. Temperature-Based Mixture Weighting
When training on multiple data sources, use temperature weighting to balance corpus proportions:
p(i) = w_i^α / Σ(w_j^α)
w_i: Raw token percentage of corpus i
α (temperature): Value in (0,1]. Lower α flattens distribution, giving more weight to smaller high-quality corpora
- Llama 2 used α = 0.7 and showed improved evaluation scores
When to use: Training on heterogeneous data (code, web, academic papers, forums)
2. Sequence Packing / Dynamic Batching
Concatenate multiple shorter sequences until exact max_length is reached, with attention masks to prevent cross-segment attention.
Benefits:
- 20-40% throughput improvement
- No gradient change
- Reduces padding waste
Implementation: Use HuggingFace DataCollatorForLanguageModeling(pad_to_multiple_of=...) or PyTorch torchtext.experimental.agents.PackedBatch
3. Deduplication & Quality Filtering
Deduplication:
- MinHash/FAISS near-duplicate detection at document and n-gram level
- Llama 2 removed ~15% of CommonCrawl using 8-gram MinHash
- Target duplicate ratio: ≤0.04
Quality Filtering:
- Remove documents with perplexity > µ + 3σ (noisy OCR, garbled HTML)
- Block PII and sensitive content using regex & NER
- Filter by source quality scores
Security & Privacy Considerations
Data Poisoning / Backdoor Attacks
Risk: Inserting <1% backdoored sentences can create hidden triggers
Mitigations:
- Shuffled mixing: Ensure adjacent examples come from different sources
- Gradient similarity scoring: Remove outliers with high gradient divergence
- Dataset versioning: Freeze immutable tarballs, verify SHA-256 hashes
Membership Inference & Memorization
Risk: Long overlap between samples increases memorization of rare strings (phone numbers, keys)
Mitigations:
- Use stride ≥ max_length (except for <1B parameter models with scarce data)
- Random masking: Mask 1-3 tokens per window during training
- OpenAI 2024 finding: Raising stride from 1× to 4× max_length reduces verbatim leakage by ~50%
Best Practices
For Training Data Preparation
- Start with stride = max_length for most cases
- Use stride = 1 only for small models (<1B params) with limited data
- Apply deduplication before sampling (8-gram MinHash recommended)
- Filter low-quality documents using perplexity thresholds
- Version your datasets with SHA-256 hashes
- Shuffle across sources to prevent gradient alignment attacks
For Production Pipelines
- Use temperature weighting (α=0.7) for mixed corpora
- Implement sequence packing for 20-40% throughput gains
- Monitor duplicate ratios (target ≤0.04)
- Apply PII filtering before training
- Log sampling statistics for reproducibility
Common Issues & Solutions
| Issue |
Solution |
| GPU memory wasted on padding |
Use sequence packing with attention masks |
| Model overfitting to repeated patterns |
Increase stride, apply deduplication |
| Slow training throughput |
Use sequence packing, optimize batch size |
| Memorization of sensitive data |
Increase stride, add random masking |
| Poor performance on knowledge tasks |
Use temperature weighting (α=0.7) |
References
Next Steps
After preparing your data:
- Validate the sampled sequences with
scripts/validate_sampling.py
- Check for duplicates and quality issues
- Create a training dataloader with appropriate batch size
- Monitor for memorization during training
- Adjust stride and temperature based on validation performance
1---2name: llm-data-sampling3description: How to prepare and sample text data for training large language models. Use this skill whenever the user mentions data preparation, tokenization, sliding windows, sequence generation, training data, LLM datasets, or needs to create input/target pairs for model training. This includes tasks like chunking text, creating dataloaders, applying sampling strategies, or optimizing training data quality.4---56# LLM Data Sampling78A skill for preparing and sampling text data for training large language models (LLMs). This covers tokenization, sequence generation, sliding windows, and advanced sampling strategies.910## When to Use This Skill1112Use this skill when the user needs to:13- Prepare text data for LLM training14- Create input/target token sequences15- Implement sliding window sampling16- Apply advanced sampling strategies (temperature weighting, sequence packing, deduplication)17- Optimize training data quality and security18- Create PyTorch datasets and dataloaders for LLM training1920## Core Concepts2122### 1. Tokenization23Breaking text into smaller units (tokens) that the model processes. Common approaches:24- **Word-level**: Split by spaces25- **Subword-level**: BPE, WordPiece (used by GPT-2, BERT)26- **Character-level**: Individual characters2728### 2. Sequence Length (max_length)29The number of tokens in each input sequence. Typical values:30- Small models: 256-512 tokens31- Medium models: 512-1024 tokens32- Large models: 1024-4096+ tokens3334### 3. Sliding Window35A method to create overlapping input sequences by moving a window over tokenized text.3637### 4. Stride38The number of tokens the sliding window moves forward. Key tradeoffs:39- **Stride = 1**: Maximum overlap, better context learning, higher overfitting risk40- **Stride = max_length**: No overlap, less redundancy, may miss dependencies41- **Stride = 2-4×max_length**: Recommended for most cases to balance context and efficiency4243## Step-by-Step Data Sampling4445### Basic Workflow46471. **Load and tokenize text**482. **Apply sliding window** to create sequences493. **Generate input/target pairs** (target is input shifted by 1 token)504. **Create dataset and dataloader** for training5152### Example: Creating Input/Target Sequences5354Given text: `"Lorem ipsum dolor sit amet, consectetur adipiscing elit."`5556With `max_length=4` and `stride=1`:5758| Window | Input Sequence | Target Sequence |59|--------|----------------|------------------|60| 1 | ["Lorem", "ipsum", "dolor", "sit"] | ["ipsum", "dolor", "sit", "amet,"] |61| 2 | ["ipsum", "dolor", "sit", "amet,"] | ["dolor", "sit", "amet,", "consectetur"] |62| 3 | ["dolor", "sit", "amet,", "consectetur"] | ["sit", "amet,", "consectetur", "adipiscing"] |6364## Implementation Guide6566### Using the Sampling Script6768The bundled script `scripts/sample_data.py` handles the complete data sampling pipeline:6970```bash71# Basic usage72python scripts/sample_data.py \73 --input "path/to/text.txt" \74 --output "path/to/output.jsonl" \75 --max-length 256 \76 --stride 128 \77 --batch-size 87879# With advanced options80python scripts/sample_data.py \81 --input "data/" \82 --output "processed/" \83 --max-length 512 \84 --stride 512 \85 --temperature 0.7 \86 --deduplicate \87 --shuffle88```8990### Key Parameters9192| Parameter | Description | Recommended Value |93|-----------|-------------|-------------------|94| `max_length` | Sequence length in tokens | 256-1024 |95| `stride` | Window step size | ≥ max_length for most cases |96| `batch_size` | Samples per batch | 8-32 (depends on GPU) |97| `temperature` | Sampling temperature (α) | 0.7 for mixed corpora |98| `shuffle` | Randomize order | True for training |99100## Advanced Sampling Strategies101102### 1. Temperature-Based Mixture Weighting103104When training on multiple data sources, use temperature weighting to balance corpus proportions:105106```107p(i) = w_i^α / Σ(w_j^α)108```109110- `w_i`: Raw token percentage of corpus i111- `α` (temperature): Value in (0,1]. Lower α flattens distribution, giving more weight to smaller high-quality corpora112- **Llama 2 used α = 0.7** and showed improved evaluation scores113114**When to use**: Training on heterogeneous data (code, web, academic papers, forums)115116### 2. Sequence Packing / Dynamic Batching117118Concatenate multiple shorter sequences until exact `max_length` is reached, with attention masks to prevent cross-segment attention.119120**Benefits**:121- 20-40% throughput improvement122- No gradient change123- Reduces padding waste124125**Implementation**: Use HuggingFace `DataCollatorForLanguageModeling(pad_to_multiple_of=...)` or PyTorch `torchtext.experimental.agents.PackedBatch`126127### 3. Deduplication & Quality Filtering128129**Deduplication**:130- MinHash/FAISS near-duplicate detection at document and n-gram level131- Llama 2 removed ~15% of CommonCrawl using 8-gram MinHash132- Target duplicate ratio: ≤0.04133134**Quality Filtering**:135- Remove documents with perplexity > µ + 3σ (noisy OCR, garbled HTML)136- Block PII and sensitive content using regex & NER137- Filter by source quality scores138139## Security & Privacy Considerations140141### Data Poisoning / Backdoor Attacks142143**Risk**: Inserting <1% backdoored sentences can create hidden triggers144145**Mitigations**:1461. **Shuffled mixing**: Ensure adjacent examples come from different sources1472. **Gradient similarity scoring**: Remove outliers with high gradient divergence1483. **Dataset versioning**: Freeze immutable tarballs, verify SHA-256 hashes149150### Membership Inference & Memorization151152**Risk**: Long overlap between samples increases memorization of rare strings (phone numbers, keys)153154**Mitigations**:1551. **Use stride ≥ max_length** (except for <1B parameter models with scarce data)1562. **Random masking**: Mask 1-3 tokens per window during training1573. **OpenAI 2024 finding**: Raising stride from 1× to 4× max_length reduces verbatim leakage by ~50%158159## Best Practices160161### For Training Data Preparation1621631. **Start with stride = max_length** for most cases1642. **Use stride = 1** only for small models (<1B params) with limited data1653. **Apply deduplication** before sampling (8-gram MinHash recommended)1664. **Filter low-quality documents** using perplexity thresholds1675. **Version your datasets** with SHA-256 hashes1686. **Shuffle across sources** to prevent gradient alignment attacks169170### For Production Pipelines1711721. **Use temperature weighting** (α=0.7) for mixed corpora1732. **Implement sequence packing** for 20-40% throughput gains1743. **Monitor duplicate ratios** (target ≤0.04)1754. **Apply PII filtering** before training1765. **Log sampling statistics** for reproducibility177178## Common Issues & Solutions179180| Issue | Solution |181|-------|----------|182| GPU memory wasted on padding | Use sequence packing with attention masks |183| Model overfitting to repeated patterns | Increase stride, apply deduplication |184| Slow training throughput | Use sequence packing, optimize batch size |185| Memorization of sensitive data | Increase stride, add random masking |186| Poor performance on knowledge tasks | Use temperature weighting (α=0.7) |187188## References189190- [Build a Large Language Model from Scratch (Manning, 2024)](https://www.manning.com/books/build-a-large-language-model-from-scratch)191- [Llama 2: Open Foundation and Fine-Tuned Chat Models (2023)](https://arxiv.org/abs/2307.09288)192- [PoisonGPT: Assessing Backdoor Vulnerabilities (BlackHat EU 2023)](https://arxiv.org/abs/2308.12364)193- [OpenAI Deduplicate Everything (2024)](https://openai.com/research/deduplicate-everything)194195## Next Steps196197After preparing your data:1981. Validate the sampled sequences with `scripts/validate_sampling.py`1992. Check for duplicates and quality issues2003. Create a training dataloader with appropriate batch size2014. Monitor for memorization during training2025. Adjust stride and temperature based on validation performance