File size: 12,421 Bytes
6f4b258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6574926
 
6f4b258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6574926
6f4b258
 
 
 
 
ae83544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f4b258
 
 
ae83544
 
 
 
 
 
 
 
 
6f4b258
 
6574926
6f4b258
 
 
 
 
 
 
6574926
6f4b258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6574926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f4b258
6574926
 
6f4b258
6574926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f4b258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""

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 the intended dataset up
    _flatten_to_root(out_dir, preferred_name=out_dir.name)

    _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}")
    _flatten_to_root(out_dir, preferred_name=out_dir.name)
    _verify(out_dir)


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

def _nearest_existing_ancestor(path: Path):
    current = path
    while True:
        if current.exists():
            return current
        if current.parent == current:
            return None
        current = current.parent


def _discover_local_dataset(local_path: str):
    requested = Path(local_path)
    preferred_name = requested.name.lower()
    roots = []

    for candidate in [requested, _nearest_existing_ancestor(requested), Path.cwd(), Path("/workspace")]:
        if candidate is None:
            continue
        if candidate.exists():
            candidate = candidate.resolve()
        if candidate not in roots:
            roots.append(candidate)

    found = []
    seen = set()
    for root in roots:
        if not root.exists() or not root.is_dir():
            continue
        for index_path in root.glob("**/index.json"):
            parent = index_path.parent.resolve()
            if parent in seen:
                continue
            seen.add(parent)
            summary = _read_index_summary(index_path)
            if summary["chunks"] <= 0:
                continue
            name = parent.name.lower()
            exact = int(bool(preferred_name and name == preferred_name))
            contains = int(bool(preferred_name and preferred_name in name))
            found.append((exact, contains, summary["tokens"], summary["chunks"], parent, summary))

    if not found:
        return None, None

    found.sort(reverse=True, key=lambda item: (item[0], item[1], item[2], item[3]))
    best = found[0]
    return best[4], best[5]


def copy_local(local_path: str, out_dir: Path):
    src = Path(local_path)
    if not src.exists():
        discovered, summary = _discover_local_dataset(local_path)
        if discovered is None:
            raise FileNotFoundError(f"Local path not found: {src}")
        print(
            f"  Requested local path not found: {src}\n"
            f"  Auto-selected local dataset: {discovered} "
            f"({summary['chunks']} chunks, {summary['tokens']:,} tokens)"
        )
        src = discovered
    if out_dir.resolve() == src.resolve():
        print(f"  Source == destination, no copy needed.")
        _flatten_to_root(out_dir, preferred_name=out_dir.name)
        _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}")
    _flatten_to_root(out_dir, preferred_name=out_dir.name)
    _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 _read_index_summary(index_path: Path):
    try:
        with open(index_path, encoding="utf-8") as f:
            idx = json.load(f)
    except Exception:
        return {"chunks": 0, "tokens": 0}
    chunks = idx.get("chunks", [])
    total_tokens = sum(c.get("dim", 0) for c in chunks)
    return {"chunks": len(chunks), "tokens": total_tokens}


def _pick_dataset_subdir(candidates, preferred_name: str | None = None):
    """Pick the most likely dataset subdir from multiple nested index.json files.



    Priority:

    1. Parent folder name exactly matches preferred_name

    2. Parent folder name contains preferred_name

    3. Highest token count

    """
    ranked = []
    preferred_name = (preferred_name or "").lower()

    for index_path in candidates:
        parent = index_path.parent
        summary = _read_index_summary(index_path)
        name = parent.name.lower()
        exact = int(bool(preferred_name and name == preferred_name))
        contains = int(bool(preferred_name and preferred_name in name))
        ranked.append((exact, contains, summary["tokens"], summary["chunks"], parent, summary))

    ranked.sort(reverse=True, key=lambda item: (item[0], item[1], item[2], item[3]))
    return ranked[0][4], ranked[0][5]


def _flatten_to_root(data_dir: Path, preferred_name: str | None = None):
    """If index.json is nested, move the intended dataset subfolder up to data_dir."""
    if (data_dir / "index.json").exists():
        return

    candidates = list(data_dir.glob("**/index.json"))
    if not candidates:
        return

    if len(candidates) == 1:
        sub = candidates[0].parent
        summary = _read_index_summary(candidates[0])
    else:
        sub, summary = _pick_dataset_subdir(candidates, preferred_name=preferred_name)
        print(
            f"  Multiple nested datasets found; selected {sub.relative_to(data_dir)}/ "
            f"({summary['chunks']} chunks, {summary['tokens']:,} tokens)"
        )

    if sub == data_dir:
        return

    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)