File size: 5,483 Bytes
2af6df2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05fecb0
 
 
 
 
 
 
 
 
 
 
2af6df2
 
 
05fecb0
 
 
 
 
2af6df2
 
 
05fecb0
 
 
 
 
 
 
2af6df2
05fecb0
 
 
 
 
 
 
2af6df2
 
 
05fecb0
2af6df2
 
 
05fecb0
2af6df2
05fecb0
2af6df2
 
 
 
 
bcedfe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared HF auth + repo/shard helpers for all pipeline stages.

Token resolution order: HF_TOKEN env var (set by the notebook from a Colab
secret) -> already-cached huggingface_hub login. Importing this module logs in
and constructs the HfApi client once.
"""
import os
import re
from pathlib import Path

from huggingface_hub import HfApi, hf_hub_download, login

import config


def get_token():
    tok = os.environ.get("HF_TOKEN")
    if not tok:
        # Fall back to any cached credential; raises later if a write is attempted
        # without one. The notebook is expected to export HF_TOKEN.
        tok = None
    return tok


HF_TOKEN = get_token()
if HF_TOKEN:
    login(token=HF_TOKEN, add_to_git_credential=False)
api = HfApi(token=HF_TOKEN)


def ensure_dst_repo():
    """Create the destination dataset repo if it does not exist yet (no-op after)."""
    api.create_repo(repo_id=config.DST_REPO, repo_type="dataset", exist_ok=True, private=False)


def list_repo_files_safe(repo_id):
    try:
        return api.list_repo_files(repo_id=repo_id, repo_type="dataset")
    except Exception as exc:
        print(f"Could not list {repo_id}: {exc}")
        return []


_SHARD_RE = re.compile(r"(^|.*/)shard_\d+\.parquet$")


def source_shards(repo_id=None, prefix="", root_only=False):
    """List shard paths in a repo.

    - prefix="foo": only shards under `foo/` (e.g. the stripped layer).
    - root_only=True: only shards at the repo root (no `/` in the path). Use this
      for the clean source so a stray subfolder (e.g. `stripped/`) is never
      mistaken for input.
    Defaults to the FILTER's input (config.SRC_REPO). Returns repo-relative paths.
    """
    repo_id = repo_id or config.SRC_REPO
    files = list_repo_files_safe(repo_id)
    shards = sorted(p for p in files if _SHARD_RE.match(p))
    if not shards:
        shards = sorted(p for p in files if p.endswith(".parquet"))
    if prefix:
        pref = prefix.rstrip("/") + "/"
        shards = [p for p in shards if p.startswith(pref)]
    elif root_only:
        shards = [p for p in shards if "/" not in p]
    return shards


def destination_shards(prefix="", root_only=False):
    """Set of shard paths already present in DST_REPO.

    - prefix="foo": only shards under `foo/` (e.g. the stripped layer).
    - root_only=True: only shards at the repo root (the final filter outputs), so
      the stripped layer is never counted as a completed final shard.
    """
    files = list_repo_files_safe(config.DST_REPO)
    shards = set(p for p in files if _SHARD_RE.match(p))
    if prefix:
        pref = prefix.rstrip("/") + "/"
        shards = {p for p in shards if p.startswith(pref)}
    elif root_only:
        shards = {p for p in shards if "/" not in p}
    return shards


def output_path_for_source(path_in_repo):
    """Basename of a shard, dropping any prefix -- final output shards are at root."""
    return Path(path_in_repo).name


def download_source_shard(path_in_repo, repo_id=None):
    return hf_hub_download(
        repo_id=repo_id or config.SRC_REPO,
        repo_type="dataset",
        filename=path_in_repo,
        token=HF_TOKEN,
        local_dir=config.SRC_CACHE,
    )


class BatchCommitter:
    """Accumulate per-shard CommitOperationAdd lists and push them to HF in one
    commit per BATCH_SIZE shards, instead of a commit per shard.

    HF commit latency (~40s) dominates per-shard time, so batching N shards into
    one commit roughly divides the upload cost by N. Crash-safety: buffered ops
    for shards not yet flushed are simply lost and those shards get reprocessed on
    resume (they were never committed), so the "is this shard in the repo?" resume
    check stays correct.

    Usage:
        bc = BatchCommitter(repo_id=..., batch_size=config.BATCH_SIZE)
        for shard in todo:
            ops = process_shard(shard)          # returns list[CommitOperationAdd]
            bc.add(ops, label=shard_name)       # commits automatically at batch_size
        bc.flush()                              # push the final partial batch
    """

    def __init__(self, repo_id, batch_size, repo_type="dataset", message_prefix="update"):
        self.repo_id = repo_id
        self.batch_size = max(1, batch_size)
        self.repo_type = repo_type
        self.message_prefix = message_prefix
        self._ops = []
        self._labels = []
        self.n_committed = 0
        self.n_commits = 0

    def add(self, ops, label=""):
        """Buffer one shard's operations; auto-flush when batch_size is reached.
        Returns True if a commit happened as a result of this add."""
        self._ops.extend(ops)
        self._labels.append(label)
        if len(self._labels) >= self.batch_size:
            self.flush()
            return True
        return False

    def flush(self):
        """Commit whatever is buffered (a no-op if nothing is buffered)."""
        if not self._ops:
            return
        n = len(self._labels)
        first, last = self._labels[0], self._labels[-1]
        span = first if n == 1 else f"{first}..{last}"
        api.create_commit(
            repo_id=self.repo_id,
            repo_type=self.repo_type,
            operations=self._ops,
            commit_message=f"{self.message_prefix} {n} shard(s): {span}",
        )
        self.n_committed += n
        self.n_commits += 1
        self._ops = []
        self._labels = []

    @property
    def pending(self):
        return len(self._labels)