Upload generation and checking scripts
Browse files
scripts/generate_hf_dataset.py
CHANGED
|
@@ -6,7 +6,8 @@ Run a small test with:
|
|
| 6 |
|
| 7 |
A full local generation uses the same format as the current dataset:
|
| 8 |
sage -python scripts/generate_hf_dataset.py --start 0 --end 1000000000 \
|
| 9 |
-
--workers 15 --out-dir generated/sum4cubes --overwrite
|
|
|
|
| 10 |
|
| 11 |
Resume from a shard boundary after an interrupted run:
|
| 12 |
sage -python scripts/generate_hf_dataset.py --start 80000000 \
|
|
@@ -103,6 +104,61 @@ def _shard_path(out_dir, shard_index):
|
|
| 103 |
return out_dir / f"cubes-{shard_index:02d}.txt"
|
| 104 |
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
class ShardWriter:
|
| 107 |
def __init__(self, out_dir, shard_size):
|
| 108 |
self.out_dir = out_dir
|
|
@@ -156,10 +212,11 @@ def _parse_row(line):
|
|
| 156 |
return n, xs
|
| 157 |
|
| 158 |
|
| 159 |
-
def verify_generated(out_dir, start, end, shard_size):
|
| 160 |
expected = start
|
| 161 |
total = 0
|
| 162 |
-
|
|
|
|
| 163 |
path = _shard_path(out_dir, shard_index)
|
| 164 |
if not path.exists():
|
| 165 |
raise RuntimeError(f"missing shard {path}")
|
|
@@ -201,6 +258,12 @@ def main():
|
|
| 201 |
help="keep existing shards and continue writing from --start",
|
| 202 |
)
|
| 203 |
parser.add_argument("--verify-output", action="store_true")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
parser.add_argument("--progress-every", type=int, default=100_000)
|
| 205 |
args = parser.parse_args()
|
| 206 |
|
|
@@ -312,8 +375,18 @@ def main():
|
|
| 312 |
flush=True,
|
| 313 |
)
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
if args.verify_output:
|
| 316 |
-
checked = verify_generated(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
print(f"VERIFY ok rows={checked}", flush=True)
|
| 318 |
|
| 319 |
|
|
|
|
| 6 |
|
| 7 |
A full local generation uses the same format as the current dataset:
|
| 8 |
sage -python scripts/generate_hf_dataset.py --start 0 --end 1000000000 \
|
| 9 |
+
--workers 15 --out-dir generated/sum4cubes --overwrite \
|
| 10 |
+
--merge-final-singleton
|
| 11 |
|
| 12 |
Resume from a shard boundary after an interrupted run:
|
| 13 |
sage -python scripts/generate_hf_dataset.py --start 80000000 \
|
|
|
|
| 104 |
return out_dir / f"cubes-{shard_index:02d}.txt"
|
| 105 |
|
| 106 |
|
| 107 |
+
def _last_shard_index(start, end, shard_size, merge_final_singleton):
|
| 108 |
+
if merge_final_singleton and end > start and end % shard_size == 0:
|
| 109 |
+
return (end - 1) // shard_size
|
| 110 |
+
return end // shard_size
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def _last_line(path):
|
| 114 |
+
with path.open("rb") as handle:
|
| 115 |
+
handle.seek(0, 2)
|
| 116 |
+
pos = handle.tell()
|
| 117 |
+
if pos == 0:
|
| 118 |
+
raise RuntimeError(f"empty shard {path}")
|
| 119 |
+
buf = bytearray()
|
| 120 |
+
while pos > 0:
|
| 121 |
+
step = min(8192, pos)
|
| 122 |
+
pos -= step
|
| 123 |
+
handle.seek(pos)
|
| 124 |
+
buf[:0] = handle.read(step)
|
| 125 |
+
lines = buf.splitlines()
|
| 126 |
+
if len(lines) >= 2 or pos == 0:
|
| 127 |
+
return lines[-1].decode("utf-8")
|
| 128 |
+
raise RuntimeError(f"could not read last line from {path}")
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def _merge_final_singleton(out_dir, start, end, shard_size):
|
| 132 |
+
if end <= start or end % shard_size != 0:
|
| 133 |
+
return False
|
| 134 |
+
final_index = end // shard_size
|
| 135 |
+
previous_index = final_index - 1
|
| 136 |
+
final_path = _shard_path(out_dir, final_index)
|
| 137 |
+
previous_path = _shard_path(out_dir, previous_index)
|
| 138 |
+
if not final_path.exists():
|
| 139 |
+
return False
|
| 140 |
+
if not previous_path.exists():
|
| 141 |
+
raise RuntimeError(f"missing previous shard {previous_path}")
|
| 142 |
+
|
| 143 |
+
text = final_path.read_text(encoding="utf-8")
|
| 144 |
+
lines = text.splitlines()
|
| 145 |
+
if len(lines) != 1:
|
| 146 |
+
raise RuntimeError(f"expected singleton shard {final_path}, found {len(lines)} rows")
|
| 147 |
+
n, _ = _parse_row(lines[0])
|
| 148 |
+
if n != end:
|
| 149 |
+
raise RuntimeError(f"expected final row n={end}, found n={n}")
|
| 150 |
+
|
| 151 |
+
prev_n, _ = _parse_row(_last_line(previous_path))
|
| 152 |
+
if prev_n == end:
|
| 153 |
+
final_path.unlink()
|
| 154 |
+
return True
|
| 155 |
+
|
| 156 |
+
with previous_path.open("a", encoding="utf-8") as handle:
|
| 157 |
+
handle.write(text if text.endswith("\n") else text + "\n")
|
| 158 |
+
final_path.unlink()
|
| 159 |
+
return True
|
| 160 |
+
|
| 161 |
+
|
| 162 |
class ShardWriter:
|
| 163 |
def __init__(self, out_dir, shard_size):
|
| 164 |
self.out_dir = out_dir
|
|
|
|
| 212 |
return n, xs
|
| 213 |
|
| 214 |
|
| 215 |
+
def verify_generated(out_dir, start, end, shard_size, merge_final_singleton=False):
|
| 216 |
expected = start
|
| 217 |
total = 0
|
| 218 |
+
last_shard = _last_shard_index(start, end, shard_size, merge_final_singleton)
|
| 219 |
+
for shard_index in range(start // shard_size, last_shard + 1):
|
| 220 |
path = _shard_path(out_dir, shard_index)
|
| 221 |
if not path.exists():
|
| 222 |
raise RuntimeError(f"missing shard {path}")
|
|
|
|
| 258 |
help="keep existing shards and continue writing from --start",
|
| 259 |
)
|
| 260 |
parser.add_argument("--verify-output", action="store_true")
|
| 261 |
+
parser.add_argument(
|
| 262 |
+
"--merge-final-singleton",
|
| 263 |
+
action="store_true",
|
| 264 |
+
help="if --end falls on a shard boundary, append that singleton row "
|
| 265 |
+
"to the previous shard",
|
| 266 |
+
)
|
| 267 |
parser.add_argument("--progress-every", type=int, default=100_000)
|
| 268 |
args = parser.parse_args()
|
| 269 |
|
|
|
|
| 375 |
flush=True,
|
| 376 |
)
|
| 377 |
|
| 378 |
+
if args.merge_final_singleton:
|
| 379 |
+
if _merge_final_singleton(out_dir, args.start, args.end, args.shard_size):
|
| 380 |
+
print("MERGE_FINAL_SINGLETON ok", flush=True)
|
| 381 |
+
|
| 382 |
if args.verify_output:
|
| 383 |
+
checked = verify_generated(
|
| 384 |
+
out_dir,
|
| 385 |
+
args.start,
|
| 386 |
+
args.end,
|
| 387 |
+
args.shard_size,
|
| 388 |
+
args.merge_final_singleton,
|
| 389 |
+
)
|
| 390 |
print(f"VERIFY ok rows={checked}", flush=True)
|
| 391 |
|
| 392 |
|