File size: 6,003 Bytes
7d67b48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Download and restore the Wizard pre-encoded training data into one root folder (the $WIZARD_DATA of the training code).

    python restore_precoded.py --root /path/to/wizard_data                    # download + extract + fix paths
    python restore_precoded.py --root /path/to/wizard_data --verify           # also check every file's sha256 against the manifests
    python restore_precoded.py --root /path/to/wizard_data --dirs wan_v7,wan_mix --keep-tars   # a subset, keep the downloaded shards

What it does:
  1. downloads precoded/<dir>/shard-*.tar(.tsv) and precoded_meta/ from yuzhewu207/wizard-precoded (huggingface_hub; resumable);
  2. extracts every shard into --root (members are paths relative to the data root, e.g. wan_v7/cond7/clip_<key>.pt);
  3. copies precoded_meta/* over it (row files and text tables without the WildWorld rows);
  4. the data was built under /data/yuzhewu/datasets: absolute symlinks (wan_mix5/clips/*, wan_v7/gbuf2, wan_v7/shield) and the absolute
     paths inside the row files (*.jsonl, *.json, *.txt at the top of each folder) are rewritten to --root;
  5. --verify: sha256 of every extracted file against the shard manifests.
Needs ~0.85 TB free (+0.8 TB while a shard is downloaded and extracted unless --stream is used, which deletes each tar after extraction; default).
With access to the private part (WildWorld windows) pass --private-repo yuzhewu207/wizard-repro: its v2/precoded_ww shards and the full row files are added.
"""
import argparse, hashlib, json, os, sys, tarfile, glob, shutil

OLD = "/data/yuzhewu/datasets"


def sha256(p, bs=1 << 22):
    h = hashlib.sha256()
    with open(p, "rb") as f:
        for b in iter(lambda: f.read(bs), b""): h.update(b)
    return h.hexdigest()


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--root", required=True); ap.add_argument("--repo", default="yuzhewu207/wizard-precoded")
    ap.add_argument("--private-repo", default=""); ap.add_argument("--dirs", default="", help="comma list of folders (default: all)")
    ap.add_argument("--dl", default="", help="download dir (default: <root>/_download)"); ap.add_argument("--keep-tars", action="store_true")
    ap.add_argument("--verify", action="store_true"); ap.add_argument("--local", default="", help="use already-downloaded repo folder instead of downloading")
    a = ap.parse_args()
    from huggingface_hub import HfApi, hf_hub_download, snapshot_download
    root = os.path.abspath(a.root); os.makedirs(root, exist_ok=True); dl = a.local or a.dl or os.path.join(root, "_download")
    want = set(x for x in a.dirs.split(",") if x)
    srcs = [(a.repo, "precoded/", "precoded_meta/")] + ([(a.private_repo, "v2/precoded_ww/", "v2/precoded_meta_full/")] if a.private_repo else [])
    for repo, prefix, meta in srcs:
        files = HfApi().list_repo_files(repo, repo_type="dataset") if not a.local else \
            [os.path.relpath(p, dl) for p in glob.glob(f"{dl}/{prefix}**/*", recursive=True) + glob.glob(f"{dl}/{meta}**/*", recursive=True) if os.path.isfile(p)]
        tars = sorted(f for f in files if f.startswith(prefix) and f.endswith(".tar") and (not want or f.split("/")[-2] in want))
        print(f"{repo}: {len(tars)} shards", flush=True)
        for i, t in enumerate(tars):
            mark = os.path.join(root, "_restored", t.replace("/", "__") + ".done")
            if os.path.exists(mark): continue
            p = os.path.join(dl, t) if a.local else hf_hub_download(repo, t, repo_type="dataset", local_dir=dl)
            m = os.path.join(dl, t + ".tsv") if a.local else hf_hub_download(repo, t + ".tsv", repo_type="dataset", local_dir=dl)
            with tarfile.open(p) as tf: tf.extractall(root, filter="tar") if sys.version_info >= (3, 12) else tf.extractall(root)
            os.makedirs(os.path.dirname(mark), exist_ok=True); shutil.copy(m, mark)
            if not a.keep_tars and not a.local: os.remove(p)
            print(f"  [{i + 1}/{len(tars)}] {t}", flush=True)
        metas = [f for f in files if f.startswith(meta)]
        for f in metas:
            p = os.path.join(dl, f) if a.local else hf_hub_download(repo, f, repo_type="dataset", local_dir=dl)
            dst = os.path.join(root, f[len(meta):]); os.makedirs(os.path.dirname(dst), exist_ok=True); shutil.copy(p, dst)
        print(f"  meta: {len(metas)} files from {meta}", flush=True)
    # absolute paths -> root
    nl = nt = 0
    for d in sorted(os.listdir(root)):
        dd = os.path.join(root, d)
        if not os.path.isdir(dd) or d.startswith("_"): continue
        for base, dirs, fs in os.walk(dd):
            for x in fs + dirs:
                q = os.path.join(base, x)
                if os.path.islink(q) and os.readlink(q).startswith(OLD):
                    t = root + os.readlink(q)[len(OLD):]; os.remove(q); os.symlink(t, q); nl += 1
        for f in glob.glob(f"{dd}/*.jsonl") + glob.glob(f"{dd}/*.json") + glob.glob(f"{dd}/*.txt"):
            s = open(f, encoding="utf-8").read()
            if OLD in s: open(f, "w", encoding="utf-8").write(s.replace(OLD, root)); nt += 1
    print(f"rewrote {nl} symlinks and {nt} row/index files to {root}", flush=True)
    if a.verify:
        bad = n = 0
        for m in glob.glob(os.path.join(root, "_restored", "*.done")):
            for line in open(m):
                size, h, rel = line.rstrip("\n").split("\t", 2)
                if h.startswith("symlink->"): continue
                n += 1; p = os.path.join(root, rel)
                if not os.path.exists(p) or sha256(p) != h: bad += 1; print("BAD", rel)
        print(f"verified {n} files, {bad} bad")
    dangling = [q for q in glob.glob(f"{root}/wan_mix5/clips/*") if os.path.islink(q) and not os.path.exists(q)]
    print(f"dangling wan_mix5/clips links: {len(dangling)}" + (" (expected: the WildWorld clips are not in the public part)" if dangling and not a.private_repo else ""))
    print(f"done. export WIZARD_DATA={root}")


if __name__ == "__main__":
    main()