Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import time
|
| 4 |
import socket
|
| 5 |
import threading
|
| 6 |
import gc
|
| 7 |
-
import ctypes
|
| 8 |
-
import multiprocessing as mp
|
| 9 |
from pathlib import Path
|
| 10 |
import numpy as np
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
os.makedirs(OUT_DIR, exist_ok=True)
|
| 24 |
|
|
@@ -28,173 +29,89 @@ def serve():
|
|
| 28 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
| 29 |
s.bind(("0.0.0.0", 7860))
|
| 30 |
s.listen(5)
|
| 31 |
-
print(
|
| 32 |
while True:
|
| 33 |
conn, _ = s.accept()
|
| 34 |
conn.send(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
|
| 35 |
conn.close()
|
| 36 |
|
| 37 |
-
# ββ
|
| 38 |
-
def
|
| 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 |
-
tmp_path.rename(out_path) # β atomic, only visible when complete
|
| 112 |
-
print(f" β [{WORKER_ID}] {out_name} | {total_tokens:,} tokens")
|
| 113 |
-
return True, None
|
| 114 |
-
|
| 115 |
-
# ββ Force full memory flush βββββββββββββββοΏ½οΏ½οΏ½βββββββββββββββββββββββββββββββββββ
|
| 116 |
-
def flush_memory():
|
| 117 |
-
gc.collect()
|
| 118 |
-
try:
|
| 119 |
-
ctypes.CDLL("libc.so.6").malloc_trim(0)
|
| 120 |
-
except Exception:
|
| 121 |
-
pass
|
| 122 |
-
|
| 123 |
-
# ββ Worker loop βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 124 |
-
def worker_loop():
|
| 125 |
-
os.makedirs(OUT_DIR, exist_ok=True)
|
| 126 |
-
print(f"β [{WORKER_ID}] Loading tokenizer...")
|
| 127 |
-
tok = Tokenizer.from_file(TOK_PATH)
|
| 128 |
-
print(f"β [{WORKER_ID}] Tokenizer ready | vocab: {tok.get_vocab_size():,}")
|
| 129 |
-
del tok
|
| 130 |
-
flush_memory()
|
| 131 |
-
|
| 132 |
-
pool = mp.Pool(processes=2, initializer=init_worker, initargs=(TOK_PATH,))
|
| 133 |
-
print(f"β [{WORKER_ID}] Worker pool ready")
|
| 134 |
-
|
| 135 |
-
try:
|
| 136 |
-
while True:
|
| 137 |
-
if not os.path.exists(STATE_FILE):
|
| 138 |
-
print(f" [{WORKER_ID}] Waiting for state.json...")
|
| 139 |
-
time.sleep(POLL_INTERVAL)
|
| 140 |
-
continue
|
| 141 |
-
|
| 142 |
-
try:
|
| 143 |
-
state = load_state()
|
| 144 |
-
except Exception as e:
|
| 145 |
-
print(f" [{WORKER_ID}] State read error: {e}")
|
| 146 |
-
time.sleep(POLL_INTERVAL)
|
| 147 |
-
continue
|
| 148 |
-
|
| 149 |
-
total = len(state["shards"]) + len(state.get("queue", []))
|
| 150 |
-
done = sum(1 for v in state["shards"].values() if v["status"] == "done")
|
| 151 |
-
if total > 0 and done == total:
|
| 152 |
-
print(f" [{WORKER_ID}] All done. Sleeping.")
|
| 153 |
-
time.sleep(300)
|
| 154 |
-
continue
|
| 155 |
-
|
| 156 |
-
name, raw_path = claim_shard(state)
|
| 157 |
-
|
| 158 |
-
if not name:
|
| 159 |
-
print(f" [{WORKER_ID}] Nothing ready β polling in {POLL_INTERVAL}s")
|
| 160 |
-
time.sleep(POLL_INTERVAL)
|
| 161 |
-
continue
|
| 162 |
-
|
| 163 |
-
print(f" [{WORKER_ID}] Claimed: {name}")
|
| 164 |
-
success, error = process_shard(name, raw_path, pool)
|
| 165 |
-
|
| 166 |
-
try:
|
| 167 |
-
state = load_state()
|
| 168 |
-
except Exception:
|
| 169 |
-
pass
|
| 170 |
-
|
| 171 |
-
if success:
|
| 172 |
-
state["shards"][name]["status"] = "done"
|
| 173 |
-
state["shards"][name]["error"] = None
|
| 174 |
-
save_state(state)
|
| 175 |
-
try:
|
| 176 |
-
raw_path.unlink()
|
| 177 |
-
print(f" [{WORKER_ID}] Deleted: {raw_path.name}")
|
| 178 |
-
except Exception as e:
|
| 179 |
-
print(f" [{WORKER_ID}] Delete failed: {e}")
|
| 180 |
-
else:
|
| 181 |
-
state["shards"][name]["status"] = "pending"
|
| 182 |
-
state["shards"][name]["worker"] = None
|
| 183 |
-
state["shards"][name]["claimed_at"] = None
|
| 184 |
-
state["shards"][name]["error"] = error
|
| 185 |
-
save_state(state)
|
| 186 |
-
print(f" [{WORKER_ID}] Failed ({error}) β left on disk for retry: {name}")
|
| 187 |
-
|
| 188 |
-
flush_memory()
|
| 189 |
-
time.sleep(5)
|
| 190 |
-
|
| 191 |
-
finally:
|
| 192 |
-
pool.terminate()
|
| 193 |
-
pool.join()
|
| 194 |
|
| 195 |
# ββ Entry point βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 196 |
if __name__ == "__main__":
|
| 197 |
threading.Thread(target=serve, daemon=True).start()
|
| 198 |
-
|
| 199 |
while True:
|
| 200 |
-
time.sleep(60)
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import socket
|
| 4 |
import threading
|
| 5 |
import gc
|
|
|
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
import numpy as np
|
| 8 |
+
|
| 9 |
+
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 10 |
+
TOK_DIR = "/data/tokenized" # input: cc2025-05_000000.bin etc.
|
| 11 |
+
OUT_DIR = "/data/ready"
|
| 12 |
+
INDEX = "/data/index.json"
|
| 13 |
+
|
| 14 |
+
# Crawl ID β output file mapping
|
| 15 |
+
CRAWL_MAP = {
|
| 16 |
+
"2025-05": "fw-00001-of-00006.bin",
|
| 17 |
+
"2025-08": "fw-00002-of-00006.bin",
|
| 18 |
+
"2025-13": "fw-00003-of-00006.bin",
|
| 19 |
+
"2025-18": "fw-00004-of-00006.bin",
|
| 20 |
+
"2025-21": "fw-00005-of-00006.bin",
|
| 21 |
+
"2025-28": "fw-00006-of-00006.bin",
|
| 22 |
+
}
|
| 23 |
|
| 24 |
os.makedirs(OUT_DIR, exist_ok=True)
|
| 25 |
|
|
|
|
| 29 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
| 30 |
s.bind(("0.0.0.0", 7860))
|
| 31 |
s.listen(5)
|
| 32 |
+
print("β Listening on port 7860")
|
| 33 |
while True:
|
| 34 |
conn, _ = s.accept()
|
| 35 |
conn.send(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
|
| 36 |
conn.close()
|
| 37 |
|
| 38 |
+
# ββ Merge βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
+
def merge():
|
| 40 |
+
# Load existing index if resuming
|
| 41 |
+
if os.path.exists(INDEX):
|
| 42 |
+
with open(INDEX) as f:
|
| 43 |
+
index = json.load(f)
|
| 44 |
+
else:
|
| 45 |
+
index = {}
|
| 46 |
+
|
| 47 |
+
for crawl_id, out_name in CRAWL_MAP.items():
|
| 48 |
+
out_path = Path(OUT_DIR) / out_name
|
| 49 |
+
tmp_path = Path(OUT_DIR) / f"{out_name}.tmp"
|
| 50 |
+
|
| 51 |
+
if out_name in index:
|
| 52 |
+
print(f" SKIP {out_name} β already done")
|
| 53 |
+
continue
|
| 54 |
+
|
| 55 |
+
# Find all shard bins for this crawl
|
| 56 |
+
shards = sorted(Path(TOK_DIR).glob(f"cc{crawl_id}_*.bin"))
|
| 57 |
+
if not shards:
|
| 58 |
+
print(f" SKIP {out_name} β no shards found for {crawl_id}")
|
| 59 |
+
continue
|
| 60 |
+
|
| 61 |
+
print(f" Merging {len(shards)} shards β {out_name}")
|
| 62 |
+
total_tokens = 0
|
| 63 |
+
total_bytes = 0
|
| 64 |
+
|
| 65 |
+
with open(tmp_path, "wb") as out_f:
|
| 66 |
+
for shard in shards:
|
| 67 |
+
# Stream shard in 256MB chunks to stay under RAM limit
|
| 68 |
+
shard_size = shard.stat().st_size
|
| 69 |
+
chunk_bytes = 256 * 1024 * 1024 # 256MB
|
| 70 |
+
offset = 0
|
| 71 |
+
|
| 72 |
+
with open(shard, "rb") as in_f:
|
| 73 |
+
while offset < shard_size:
|
| 74 |
+
to_read = min(chunk_bytes, shard_size - offset)
|
| 75 |
+
data = in_f.read(to_read)
|
| 76 |
+
out_f.write(data)
|
| 77 |
+
n_toks = len(data) // 2 # uint16 = 2 bytes each
|
| 78 |
+
total_tokens += n_toks
|
| 79 |
+
total_bytes += len(data)
|
| 80 |
+
offset += len(data)
|
| 81 |
+
del data
|
| 82 |
+
gc.collect()
|
| 83 |
+
|
| 84 |
+
print(f" β {shard.name}")
|
| 85 |
+
|
| 86 |
+
tmp_path.rename(out_path)
|
| 87 |
+
|
| 88 |
+
index[out_name] = {
|
| 89 |
+
"crawl": crawl_id,
|
| 90 |
+
"total_tokens": total_tokens,
|
| 91 |
+
"total_bytes": total_bytes,
|
| 92 |
+
"dtype": "uint16",
|
| 93 |
+
"shards_merged": len(shards),
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
# Save index after each file β safe to resume
|
| 97 |
+
with open(INDEX, "w") as f:
|
| 98 |
+
json.dump(index, f, indent=2)
|
| 99 |
+
|
| 100 |
+
print(f" β {out_name} | {total_tokens:,} tokens")
|
| 101 |
+
gc.collect()
|
| 102 |
+
|
| 103 |
+
# Final summary
|
| 104 |
+
print("\nββ Index ββββββββββββββββββββββββββββββββββββββββ")
|
| 105 |
+
grand_total = 0
|
| 106 |
+
for name, info in index.items():
|
| 107 |
+
print(f" {name}: {info['total_tokens']:,} tokens")
|
| 108 |
+
grand_total += info["total_tokens"]
|
| 109 |
+
print(f" TOTAL: {grand_total:,} tokens")
|
| 110 |
+
print("β All done")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# ββ Entry point βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 113 |
if __name__ == "__main__":
|
| 114 |
threading.Thread(target=serve, daemon=True).start()
|
| 115 |
+
merge()
|
| 116 |
while True:
|
| 117 |
+
import time; time.sleep(60)
|