# TurMix & HinMix: Exact Reproduction Guide This document provides a complete guide to reproducing the Turkish (TurMix) and Hindi (HinMix) pretraining data pipelines. ## Table of Contents 1. [Overview](#overview) 2. [Environment Setup](#environment-setup) 3. [Project Structure](#project-structure) 4. [Data Sources](#data-sources) 5. [Pipeline Stages](#pipeline-stages) 6. [Stage 1: Download](#stage-1-download) 7. [Stage 2: Quality Filtering](#stage-2-quality-filtering) 8. [Stage 3: MinHash Deduplication](#stage-3-minhash-deduplication) 9. [Stage 4: Consensus Subset Construction](#stage-4-consensus-subset-construction) 10. [Stage 5: Upload to HuggingFace](#stage-5-upload-to-huggingface) 11. [Final Statistics](#final-statistics) --- ## Overview The pipeline processes web crawl data through the following stages: 1. **Download**: Fetch data from HuggingFace using `huggingface-cli` 2. **Quality Filter**: Apply language-specific quality filters using DataTrove 3. **MinHash Dedup**: Remove near-duplicate documents within each source 4. **Consensus**: Identify documents appearing in 2+ sources (exact text match) ### Key Design Decisions - Use `huggingface-cli download` with `--include` patterns for efficient selective downloads - Process each source separately to avoid parquet schema conflicts - Use 54 workers (CPU cores - 2) for parallel processing - MinHash deduplication within each source (not cross-source) - Consensus detection via exact text hash matching across sources --- ## Environment Setup ### Prerequisites ```bash # Python 3.10+ conda create -n pretraining python=3.10 conda activate pretraining # Install dependencies pip install datasets datatrove pyarrow huggingface_hub pip install fasttext-langdetect # For language detection ``` ### HuggingFace Authentication ```bash huggingface-cli login # Enter your HuggingFace token ``` --- ## Project Structure ``` arabic-pretraining-mix-other-languages/ ├── run_pipeline.py # Main unified pipeline runner ├── build_consensus_v2.py # Consensus subset builder (memory-efficient) ├── filter_c4.py # C4 JSON filtering script ├── fix_hplt2_filter.py # Turkish HPLT2 filtering (5 subfolders) ├── fix_hindi_hplt2_filter.py # Hindi HPLT2 filtering ├── src/ │ ├── config/ │ │ ├── common.py # Shared configs (paths, workers, MinHash params) │ │ ├── datasets_tr.py # Turkish dataset definitions │ │ └── datasets_hi.py # Hindi dataset definitions │ ├── filters/ │ │ ├── base_quality.py # Base quality filter class │ │ ├── tr_quality.py # Turkish quality filter │ │ ├── hi_quality.py # Hindi quality filter │ │ └── lang_config.py # Language-specific constants │ └── dedup/ │ └── __init__.py # MinHash deduplication wrappers └── data/ ├── hi/ # Hindi data │ ├── downloads/ # Raw downloaded data │ ├── filtered/ # Quality-filtered data │ ├── deduped/ # MinHash-deduplicated data │ ├── consensus/ # Consensus subset │ └── minhash_signatures/ # MinHash signatures (preserved) └── tr/ # Turkish data (same structure) ``` --- ## Data Sources ### Hindi Sources (6) | Source | HuggingFace Path | Subset/Config | Download Command | |--------|------------------|---------------|------------------| | HPLT-2 | `HPLT/HPLT2.0_cleaned` | `hin_Deva` | `huggingface-cli download HPLT/HPLT2.0_cleaned --include "hin_Deva/*" --local-dir ./data/hi/hplt2 --repo-type dataset` | | Fineweb-2 | `HuggingFaceFW/fineweb-2` | `hin_Deva` | `huggingface-cli download HuggingFaceFW/fineweb-2 --include "data/hin_Deva/*" --local-dir ./data/hi/fineweb2 --repo-type dataset` | | CulturaX | `uonlp/CulturaX` | `hi` | `huggingface-cli download uonlp/CulturaX --include "hi/*" --local-dir ./data/hi/culturax --repo-type dataset` | | mC4 | `allenai/c4` | `hi` | `huggingface-cli download allenai/c4 --include "multilingual/c4-hi*" --local-dir ./data/hi/c4 --repo-type dataset` | | Sangraha (verified) | `ai4bharat/sangraha` | `verified/hin` | `huggingface-cli download ai4bharat/sangraha --include "verified/hin/*" --local-dir ./data/hi/sangraha_verified --repo-type dataset` | | Sangraha (unverified) | `ai4bharat/sangraha` | `unverified/hin` | `huggingface-cli download ai4bharat/sangraha --include "unverified/hin/*" --local-dir ./data/hi/sangraha_unverified --repo-type dataset` | ### Turkish Sources (5) | Source | HuggingFace Path | Subset/Config | Download Command | |--------|------------------|---------------|------------------| | HPLT-2 | `HPLT/HPLT2.0_cleaned` | `tur_Latn` | `huggingface-cli download HPLT/HPLT2.0_cleaned --include "tur_Latn*/*" --local-dir ./data/tr/hplt2 --repo-type dataset` | | Fineweb-2 | `HuggingFaceFW/fineweb-2` | `tur_Latn` | `huggingface-cli download HuggingFaceFW/fineweb-2 --include "data/tur_Latn/*" --local-dir ./data/tr/fineweb2 --repo-type dataset` | | CulturaX | `uonlp/CulturaX` | `tr` | `huggingface-cli download uonlp/CulturaX --include "tr/*" --local-dir ./data/tr/culturax --repo-type dataset` | | mC4 | `allenai/c4` | `tr` | `huggingface-cli download allenai/c4 --include "multilingual/c4-tr*" --local-dir ./data/tr/c4 --repo-type dataset` | | VNGRS | `vngrs-ai/vngrs-web-corpus` | N/A | `huggingface-cli download vngrs-ai/vngrs-web-corpus --local-dir ./data/tr/vngrs --repo-type dataset` | **Note**: Turkish HPLT-2 is split into 5 subfolders: `tur_Latn_1` through `tur_Latn_5`. --- ## Pipeline Stages ### Stage 1: Download Downloads are performed using `huggingface-cli download` with `--include` patterns: ```bash # Example: Download Hindi CulturaX huggingface-cli download uonlp/CulturaX \ --include "hi/*" \ --local-dir ./data/hi/culturax \ --repo-type dataset ``` The `run_pipeline.py` script automates this: ```bash python run_pipeline.py --language hi --stage download python run_pipeline.py --language tr --stage download ``` --- ### Stage 2: Quality Filtering Quality filtering uses DataTrove with custom language-specific filters. #### Filter Configuration (from Fineweb-2) **Hindi Filter Thresholds** (`src/filters/hi_quality.py`): ```python HINDI_FILTER_CONFIG = { "min_script_ratio": 0.5, # Devanagari script ratio "lang_score_threshold": 0.692, "dup_line_frac": 0.206, "new_line_ratio": 0.316, "min_avg_word_length": 2, "max_avg_word_length": 21, "line_punct_thr": 0.091, "non_alpha_words_ratio": 0.837, "top_5_gram_frac": 0.135, "top_10_gram_frac": 0.090, } ``` **Turkish Filter Thresholds** (`src/filters/tr_quality.py`): ```python TURKISH_FILTER_CONFIG = { "min_script_ratio": 0.65, # Turkish Latin script ratio "lang_score_threshold": 0.875, "dup_line_frac": 0.272, "new_line_ratio": 0.222, "min_avg_word_length": 3, "max_avg_word_length": 21, "line_punct_thr": 0.091, "non_alpha_words_ratio": 0.773, "top_5_gram_frac": 0.154, "top_10_gram_frac": 0.103, } ``` #### Output Schema Normalization All filtered output uses a unified schema: ```python OUTPUT_SCHEMA = pa.schema([ ("text", pa.string()), ("id", pa.string()), ("metadata", pa.struct([ ("source", pa.string()), ])), ]) ``` #### Running Filtering ```bash # Main parquet datasets (culturax, fineweb2, sangraha, vngrs) python run_pipeline.py --language hi --stage filter python run_pipeline.py --language tr --stage filter # C4 JSON files (requires separate script due to JSON format) python filter_c4.py --language hi python filter_c4.py --language tr # HPLT2 (requires separate handling due to nested structure) python fix_hindi_hplt2_filter.py python fix_hplt2_filter.py # Turkish - 5 subfolders ``` --- ### Stage 3: MinHash Deduplication MinHash deduplication removes near-duplicate documents within each source. #### MinHash Configuration ```python MINHASH_CONFIG = { "n_grams": 5, "num_buckets": 14, "hashes_per_bucket": 8, "similarity_threshold": 0.8, } ``` #### MinHash Stages (via DataTrove) 1. **Stage 1 - Signatures**: Generate MinHash signatures for each document 2. **Stage 2 - Buckets**: Group documents by LSH buckets to find candidates 3. **Stage 3 - Cluster**: Cluster similar documents together 4. **Stage 4 - Filter**: Keep one representative per cluster, write deduped output #### Running MinHash ```bash python run_pipeline.py --language hi --stage minhash python run_pipeline.py --language tr --stage minhash ``` **Runtime**: Hindi ~17 hours, Turkish ~30 hours (on 56-core machine) --- ### Stage 4: Consensus Subset Construction The consensus subset identifies documents that appear in 2+ sources using exact text hash matching. #### Algorithm (Two-Pass, Memory-Efficient) **Pass 1**: Build hash-to-sources index ```python # For each document, compute MD5 hash of normalized text # Store only: hash -> set of sources (not full text) def compute_text_hash(text: str) -> str: normalized = ' '.join(text.lower().split()) return hashlib.md5(normalized.encode('utf-8')).hexdigest() # Pass 1: hash_to_sources[hash].add(source) ``` **Pass 2**: Extract documents with multi-source hashes ```python # Re-read data, collect documents where hash appears in 2+ sources # Store full document with sources list ``` #### Consensus Output Schema ```python schema = pa.schema([ ('text', pa.string()), ('id', pa.string()), ('sources', pa.list_(pa.string())), # e.g., ["c4", "culturax"] ('all_ids', pa.list_(pa.string())), # e.g., ["c4:url1", "culturax:url2"] ('metadata', pa.struct([ ('source', pa.string()), # "consensus" ])), ]) ``` #### Running Consensus Builder ```bash python build_consensus_v2.py --language hi python build_consensus_v2.py --language tr ``` **Runtime**: Hindi ~2 hours, Turkish ~7 hours --- ### Stage 5: Upload to HuggingFace #### Create Repositories ```bash huggingface-cli repo create HinMix --organization AdaMLLab --type dataset huggingface-cli repo create TurMix --organization AdaMLLab --type dataset ``` #### Staging Directory Structure ``` hf_staging/HinMix/ ├── README.md ├── minhash_deduped/ │ ├── c4/*.parquet │ ├── culturax/*.parquet │ ├── fineweb2/*.parquet │ ├── hplt2/*.parquet │ ├── sangraha_unverified/*.parquet │ └── sangraha_verified/*.parquet ├── quality_filtered/ │ └── (same structure as minhash_deduped) └── consensus/ └── consensus.parquet ``` #### Upload Command ```bash # Use upload-large-folder for large datasets hf upload-large-folder AdaMLLab/HinMix hf_staging/HinMix --repo-type dataset --num-workers 8 hf upload-large-folder AdaMLLab/TurMix hf_staging/TurMix --repo-type dataset --num-workers 8 ``` --- ## Final Statistics ### Hindi (HinMix) | Stage | Documents | Size | Notes | |-------|-----------|------|-------| | **Quality Filtered** | ~99M | 231GB | All 6 sources combined | | **MinHash Deduped** | ~60M | 136GB | 40% reduction | | **Consensus** | 1.92M | 3.7GB | Docs in 2+ sources | **Consensus Source Participation**: - fineweb2: 1,602,172 - hplt2: 1,194,132 - sangraha_unverified: 600,851 - culturax: 277,990 - sangraha_verified: 153,060 - c4: 71,462 ### Turkish (TurMix) | Stage | Documents | Size | Notes | |-------|-----------|------|-------| | **Quality Filtered** | ~49M | 658GB | All 5 sources combined | | **MinHash Deduped** | ~27M | 359GB | 46% reduction | | **Consensus** | 7.84M | 13GB | Docs in 2+ sources | **Consensus Source Participation**: - fineweb2: 7,217,270 - hplt2: 7,075,189 - culturax: 686,152 - c4: 419,307 - vngrs: 402,760 --- ## Key Scripts ### run_pipeline.py (Main Entry Point) ```python #!/usr/bin/env python3 """ Usage: python run_pipeline.py --language hi --stage download python run_pipeline.py --language hi --stage filter python run_pipeline.py --language hi --stage minhash python run_pipeline.py --language tr --stage all """ ``` ### build_consensus_v2.py (Consensus Builder) ```python #!/usr/bin/env python3 """ Memory-efficient two-pass consensus builder. Usage: python build_consensus_v2.py --language hi python build_consensus_v2.py --language tr """ ``` ### filter_c4.py (C4 JSON Filtering) ```python #!/usr/bin/env python3 """ Filters C4 JSON files (not parquet) with schema normalization. Usage: python filter_c4.py --language hi python filter_c4.py --language tr """ ``` --- ## Troubleshooting ### Common Issues 1. **Storage Full During MinHash** - MinHash signatures can grow to 300+ GB - Ensure at least 500GB free space before starting - If interrupted, clean `minhash_signatures/`, `minhash_buckets/`, `minhash_clusters/` and restart 2. **Memory Issues During Consensus** - Use `build_consensus_v2.py` (two-pass, memory-efficient) - Original `build_consensus.py` requires 100+ GB RAM 3. **C4 Schema Mismatch** - C4 is JSON, not parquet - Use `filter_c4.py` with `JsonlReader` and schema adapter 4. **HPLT2 Nested Folders** - Turkish HPLT2 has 5 subfolders (`tur_Latn_1` to `tur_Latn_5`) - Use `fix_hplt2_filter.py` which handles all subfolders ### Recovery Commands ```bash # Check running processes ps aux | grep "run_pipeline\|build_consensus" | grep -v grep # Check disk usage df -h /home/alrashsm/Documents/Github/arabic-pretraining-mix-other-languages/data/ # Check MinHash progress ls data/hi/logs/minhash_sig/completions/ | wc -l # Should reach 54 tail -20 data/hi/minhash.log ``` --- ## License This pipeline and resulting datasets are released under CC-BY-4.0. Individual source datasets have their own licenses - refer to original sources. --- ## Citation ```bibtex @dataset{hinmix_turmix_2024, title={HinMix and TurMix: Hindi and Turkish Pretraining Data Mixes}, author={AdaMLLab}, year={2024}, publisher={Hugging Face}, url={https://huggingface.co/AdaMLLab} } ```