File size: 8,350 Bytes
ad68b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486816d
ad68b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486816d
 
 
 
 
 
 
ad68b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486816d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
616615e
486816d
 
 
616615e
486816d
 
 
616615e
486816d
 
 
 
 
 
 
 
616615e
 
 
 
 
 
 
 
486816d
 
ad68b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
LUNA β€” Dataset Fetcher
======================
Downloads the tokenized litdata dataset from either:
  - HuggingFace Hub (recommended, free, fast)
  - Google Drive (direct link, requires gdown)

Usage:
    python fetch_data.py --source huggingface --hf_repo YourName/LUNA-pretrain-data --out_dir /workspace/data
    python fetch_data.py --source gdrive     --gdrive_id <FILE_OR_FOLDER_ID>       --out_dir /workspace/data
    python fetch_data.py --source local      --local_path Base/data/litdata_pretrain_final  --out_dir /workspace/data

After running, pass --data_path /workspace/data/litdata_pretrain_final to train.py
"""

import os
import sys
import json
import shutil
import argparse
from pathlib import Path


# ─── HuggingFace Download ─────────────────────────────────────────────────────

def download_huggingface(repo_id: str, out_dir: Path, hf_token: str = None):
    try:
        from huggingface_hub import snapshot_download
    except ImportError:
        print("  Installing huggingface_hub...")
        os.system(f"{sys.executable} -m pip install -q huggingface_hub")
        from huggingface_hub import snapshot_download

    print(f"  Downloading from HuggingFace: {repo_id}")
    out_dir.mkdir(parents=True, exist_ok=True)
    snapshot_download(
        repo_id=repo_id,
        repo_type="dataset",
        local_dir=str(out_dir),
        token=hf_token,
        ignore_patterns=["*.md", ".gitattributes"],
    )
    print(f"  Downloaded to: {out_dir}")

    # Auto-extract any zip files found in the download
    _extract_zips(out_dir)

    # If index.json landed in a subdirectory, move contents up
    _flatten_to_root(out_dir)

    _verify(out_dir)


# ─── Google Drive Download ────────────────────────────────────────────────────

def download_gdrive(gdrive_id: str, out_dir: Path):
    try:
        import gdown
    except ImportError:
        print("  Installing gdown...")
        os.system(f"{sys.executable} -m pip install -q gdown")
        import gdown

    out_dir.mkdir(parents=True, exist_ok=True)
    # Try as folder first, then single file
    url = f"https://drive.google.com/drive/folders/{gdrive_id}"
    print(f"  Attempting GDrive folder download: {gdrive_id}")
    try:
        gdown.download_folder(url=url, output=str(out_dir), quiet=False, use_cookies=False)
    except Exception as e:
        print(f"  Folder download failed ({e}), trying single file...")
        url = f"https://drive.google.com/uc?id={gdrive_id}"
        dest = out_dir / "data.zip"
        gdown.download(url, str(dest), quiet=False)
        if dest.suffix == ".zip":
            print("  Extracting zip...")
            import zipfile
            with zipfile.ZipFile(dest) as z:
                z.extractall(out_dir)
            dest.unlink()
    print(f"  Downloaded to: {out_dir}")
    _verify(out_dir)


# ─── Local Copy ───────────────────────────────────────────────────────────────

def copy_local(local_path: str, out_dir: Path):
    src = Path(local_path)
    if not src.exists():
        raise FileNotFoundError(f"Local path not found: {src}")
    if out_dir.resolve() == src.resolve():
        print(f"  Source == destination, no copy needed.")
        _verify(out_dir)
        return
    print(f"  Copying {src} β†’ {out_dir}")
    if out_dir.exists():
        shutil.rmtree(out_dir)
    shutil.copytree(src, out_dir)
    print(f"  Copied to: {out_dir}")
    _verify(out_dir)


# ─── Zip Extraction & Flattening ──────────────────────────────────────────────

def _extract_zips(data_dir: Path):
    """Find and extract all .zip files in data_dir, then delete the zips."""
    import zipfile
    zips = list(data_dir.glob("*.zip"))
    if not zips:
        return
    for zf in zips:
        print(f"  Extracting {zf.name} ...")
        with zipfile.ZipFile(zf) as z:
            z.extractall(data_dir)
        zf.unlink()
        print(f"  Removed {zf.name}")


def _flatten_to_root(data_dir: Path):
    """If index.json is nested (e.g. data_dir/a/b/index.json),
    move everything from that subfolder up to data_dir."""
    if (data_dir / "index.json").exists():
        return  # already at root
    candidates = list(data_dir.glob("**/index.json"))
    if len(candidates) != 1:
        return  # ambiguous or not found
    sub = candidates[0].parent
    print(f"  Moving contents from {sub.relative_to(data_dir)}/ up to {data_dir.name}/ ...")
    for item in sub.iterdir():
        dest = data_dir / item.name
        if dest.exists():
            if dest.is_dir():
                shutil.rmtree(dest)
            else:
                dest.unlink()
        shutil.move(str(item), str(dest))
    # Remove the now-empty nested directories
    # Walk up from sub to data_dir, removing empty dirs
    while sub != data_dir:
        try:
            sub.rmdir()
        except OSError:
            break
        sub = sub.parent


# ─── Verify ───────────────────────────────────────────────────────────────────

def _verify(data_dir: Path):
    idx_path = data_dir / "index.json"
    if not idx_path.exists():
        # Search one level deeper
        found = list(data_dir.glob("**/index.json"))
        if found:
            print(f"  Note: index.json found at {found[0]}, not root. Check your --out_dir.")
        else:
            print(f"  WARNING: index.json NOT found in {data_dir}")
        return

    with open(idx_path) as f:
        idx = json.load(f)
    chunks = idx.get("chunks", [])
    total_tokens = sum(c.get("dim", 0) for c in chunks)
    present = sum(1 for c in chunks if (data_dir / c["filename"]).exists())
    missing = len(chunks) - present

    print(f"\n  Dataset verified:")
    print(f"  Chunks declared : {len(chunks)}")
    print(f"  Chunks on disk  : {present}")
    print(f"  Missing chunks  : {missing}")
    print(f"  Total tokens    : {total_tokens:,}")
    if missing > 0:
        print(f"  WARNING: {missing} chunk(s) missing β€” training will error on those blocks!")
    else:
        print(f"  All chunks present. Ready to train.")


# ─── Args ─────────────────────────────────────────────────────────────────────

def parse_args():
    p = argparse.ArgumentParser(description="LUNA dataset fetcher")
    p.add_argument("--source", choices=["huggingface", "gdrive", "local"], required=True)
    p.add_argument("--out_dir", type=str, default="/workspace/data/litdata_pretrain_final",
                   help="Where to save the dataset")
    p.add_argument("--hf_repo", type=str, default="",
                   help="HuggingFace dataset repo ID (e.g. YourName/LUNA-pretrain-data)")
    p.add_argument("--hf_token", type=str, default=os.environ.get("HF_TOKEN", ""),
                   help="HuggingFace token (or set HF_TOKEN env var)")
    p.add_argument("--gdrive_id", type=str, default="",
                   help="Google Drive file/folder ID")
    p.add_argument("--local_path", type=str, default="Base/data/litdata_pretrain_final",
                   help="Local path to the dataset (for local source)")
    return p.parse_args()


if __name__ == "__main__":
    args = parse_args()
    out = Path(args.out_dir)

    if args.source == "huggingface":
        if not args.hf_repo:
            print("ERROR: --hf_repo required for HuggingFace source")
            sys.exit(1)
        download_huggingface(args.hf_repo, out, hf_token=args.hf_token or None)

    elif args.source == "gdrive":
        if not args.gdrive_id:
            print("ERROR: --gdrive_id required for GDrive source")
            sys.exit(1)
        download_gdrive(args.gdrive_id, out)

    elif args.source == "local":
        copy_local(args.local_path, out)