Spaces:
Sleeping
Sleeping
PERDYPTO commited on
Update pipeline.py
Browse files- pipeline.py +2 -65
pipeline.py
CHANGED
|
@@ -17,16 +17,6 @@ import requests
|
|
| 17 |
import threading
|
| 18 |
from pathlib import Path
|
| 19 |
from queue import Empty
|
| 20 |
-
from functools import partial
|
| 21 |
-
|
| 22 |
-
# Force unbuffered stdout/stderr. Without this, print() output from
|
| 23 |
-
# worker subprocesses (spawned via multiprocessing) is fully
|
| 24 |
-
# block-buffered when stdout isn't a terminal — which is always true
|
| 25 |
-
# under Docker/uvicorn. That means "[WORKER] ... started" and every
|
| 26 |
-
# progress line can sit invisible in an internal buffer for minutes,
|
| 27 |
-
# even though the worker is alive and actively processing rows. This
|
| 28 |
-
# makes it look like workers never started when they actually did.
|
| 29 |
-
print = partial(print, flush=True)
|
| 30 |
|
| 31 |
import orjson
|
| 32 |
from datasets import load_dataset
|
|
@@ -68,18 +58,7 @@ REMOTE_STATE = f"{REMOTE_ROOT}/state"
|
|
| 68 |
REMOTE_FINAL = f"{REMOTE_ROOT}/final"
|
| 69 |
|
| 70 |
# ----------------------------------------------------------------------
|
| 71 |
-
# DATASETS
|
| 72 |
-
# swallowcode_v2 has been removed: its JSON shards contain a sparse
|
| 73 |
-
# error-reporting column (struct<type: string, message: string>) that
|
| 74 |
-
# is null in the first batches datasets' streaming JSON reader samples
|
| 75 |
-
# for schema inference, then populated with real struct data in later
|
| 76 |
-
# batches. Arrow can't cast a struct into a column it already committed
|
| 77 |
-
# to typing as null, so the whole shard iterator crashes with:
|
| 78 |
-
# TypeError: Couldn't cast array of type struct<type: string, message: string> to null
|
| 79 |
-
# This killed both worker processes immediately on startup and hung
|
| 80 |
-
# the coordinator forever since no task_done ever arrived for those
|
| 81 |
-
# tasks. Rather than patch around a dataset with an unstable schema,
|
| 82 |
-
# it has been dropped from the pool entirely.
|
| 83 |
# ----------------------------------------------------------------------
|
| 84 |
DATASETS = [
|
| 85 |
{
|
|
@@ -428,43 +407,6 @@ def load_stream(cfg):
|
|
| 428 |
# extract_record will pull only the fields we need from each row.
|
| 429 |
return load_dataset(cfg["dataset"], **kwargs)
|
| 430 |
|
| 431 |
-
# ----------------------------------------------------------------------
|
| 432 |
-
# RESILIENT ROW ITERATION — schema-mismatch-safe
|
| 433 |
-
# ----------------------------------------------------------------------
|
| 434 |
-
def safe_row_iter(ds, dataset_name, task_id, stats):
|
| 435 |
-
"""
|
| 436 |
-
datasets' streaming JSON reader infers an Arrow schema from the first
|
| 437 |
-
batch it samples. If a column is all-null in that batch but a later
|
| 438 |
-
batch has real (e.g. struct) data in that same column, Arrow raises
|
| 439 |
-
TypeError and kills the whole iterator — which previously took the
|
| 440 |
-
entire worker process down with it, and hung the coordinator forever
|
| 441 |
-
since no task_done ever arrived.
|
| 442 |
-
|
| 443 |
-
We can't fix the source dataset's schema from here, but we can stop
|
| 444 |
-
a single bad batch from killing the process: iterate manually with
|
| 445 |
-
next() instead of a for-loop (a for-loop can't recover from an
|
| 446 |
-
exception raised mid-iteration), catch the error, log it, and treat
|
| 447 |
-
the rest of this shard as exhausted rather than crash.
|
| 448 |
-
"""
|
| 449 |
-
it = iter(ds)
|
| 450 |
-
while True:
|
| 451 |
-
try:
|
| 452 |
-
row = next(it)
|
| 453 |
-
except StopIteration:
|
| 454 |
-
return
|
| 455 |
-
except TypeError as e:
|
| 456 |
-
# Arrow schema-cast failure inside datasets' JSON backend.
|
| 457 |
-
stats["other_reject"] += 1
|
| 458 |
-
print(f"[WARN] {dataset_name} T{task_id} schema-cast error, "
|
| 459 |
-
f"stopping this shard early: {e}")
|
| 460 |
-
return
|
| 461 |
-
except Exception as e:
|
| 462 |
-
stats["other_reject"] += 1
|
| 463 |
-
print(f"[WARN] {dataset_name} T{task_id} row iteration error: {e}")
|
| 464 |
-
return
|
| 465 |
-
else:
|
| 466 |
-
yield row
|
| 467 |
-
|
| 468 |
# ----------------------------------------------------------------------
|
| 469 |
# WORKER FUNCTION
|
| 470 |
# ----------------------------------------------------------------------
|
|
@@ -498,7 +440,7 @@ def worker_task(cfg, task_id, total_tasks, interpreters, result_queue):
|
|
| 498 |
result_queue.put(("accepted_batch", batch, stats.copy()))
|
| 499 |
batch.clear()
|
| 500 |
|
| 501 |
-
for row in
|
| 502 |
stats["processed"] += 1
|
| 503 |
|
| 504 |
try:
|
|
@@ -807,10 +749,5 @@ async def status():
|
|
| 807 |
# MAIN
|
| 808 |
# ----------------------------------------------------------------------
|
| 809 |
if __name__ == "__main__":
|
| 810 |
-
# Belt-and-suspenders: also force unbuffered mode at the interpreter
|
| 811 |
-
# level, in case this file is invoked before PYTHONUNBUFFERED=1 is
|
| 812 |
-
# set in the environment/Dockerfile. Covers C-extension and library
|
| 813 |
-
# writes to stdout/stderr, not just our own print() calls.
|
| 814 |
-
os.environ.setdefault("PYTHONUNBUFFERED", "1")
|
| 815 |
port = int(os.environ.get("PORT", 7860))
|
| 816 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 17 |
import threading
|
| 18 |
from pathlib import Path
|
| 19 |
from queue import Empty
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
import orjson
|
| 22 |
from datasets import load_dataset
|
|
|
|
| 58 |
REMOTE_FINAL = f"{REMOTE_ROOT}/final"
|
| 59 |
|
| 60 |
# ----------------------------------------------------------------------
|
| 61 |
+
# DATASETS (unchanged)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# ----------------------------------------------------------------------
|
| 63 |
DATASETS = [
|
| 64 |
{
|
|
|
|
| 407 |
# extract_record will pull only the fields we need from each row.
|
| 408 |
return load_dataset(cfg["dataset"], **kwargs)
|
| 409 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
# ----------------------------------------------------------------------
|
| 411 |
# WORKER FUNCTION
|
| 412 |
# ----------------------------------------------------------------------
|
|
|
|
| 440 |
result_queue.put(("accepted_batch", batch, stats.copy()))
|
| 441 |
batch.clear()
|
| 442 |
|
| 443 |
+
for row in ds:
|
| 444 |
stats["processed"] += 1
|
| 445 |
|
| 446 |
try:
|
|
|
|
| 749 |
# MAIN
|
| 750 |
# ----------------------------------------------------------------------
|
| 751 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 752 |
port = int(os.environ.get("PORT", 7860))
|
| 753 |
uvicorn.run(app, host="0.0.0.0", port=port)
|