HAKO upload: hako/sources/corpus_cache.py
Browse files- hako/sources/corpus_cache.py +48 -0
hako/sources/corpus_cache.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Corpus cache extractor: materialize a small JSONL sample of the giant
|
| 2 |
+
single-row-group parquet (BrunoN-Dev/corpus-ptbr-v1) in an ISOLATED process.
|
| 3 |
+
|
| 4 |
+
Why: the shard has ONE row group of 500k rows (~1.2 GiB decompressed text).
|
| 5 |
+
Any in-process read (datasets streaming, iter_batches) inflates the resident
|
| 6 |
+
set of the trainer beyond the RAM cap. The P-SRC/P-MEM fix: run this module
|
| 7 |
+
as a STANDALONE python process (no torch, no onnx loaded), read the row
|
| 8 |
+
group once, write N docs to a JSONL cache, exit. The trainer then streams
|
| 9 |
+
from the ~10 MiB cache file.
|
| 10 |
+
|
| 11 |
+
Usage: python3 -m hako.sources.corpus_cache <out_jsonl> <n_docs>
|
| 12 |
+
"""
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
import sys
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def main(out_path: str, n_docs: int = 6000) -> int:
|
| 20 |
+
import json
|
| 21 |
+
import resource
|
| 22 |
+
|
| 23 |
+
import polars as pl
|
| 24 |
+
|
| 25 |
+
# Polars' Rust parquet reader decodes page-by-page with slice pushdown,
|
| 26 |
+
# so head(N) never materializes the 1-row-group column (~3 GiB in
|
| 27 |
+
# pyarrow). Peak RSS measured: ~2.4 GiB for 6000 docs.
|
| 28 |
+
uri = ("https://huggingface.co/datasets/BrunoN-Dev/corpus-ptbr-v1/"
|
| 29 |
+
"resolve/main/data/train-00000-of-00018.parquet")
|
| 30 |
+
df = pl.scan_parquet(uri).select("text").head(n_docs).collect(
|
| 31 |
+
engine="streaming")
|
| 32 |
+
texts = []
|
| 33 |
+
for t in df["text"].to_list():
|
| 34 |
+
t = str(t)[:2000].strip()
|
| 35 |
+
if len(t) > 40:
|
| 36 |
+
texts.append(t)
|
| 37 |
+
del df
|
| 38 |
+
with open(out_path, "w", encoding="utf-8") as fh:
|
| 39 |
+
for t in texts:
|
| 40 |
+
fh.write(json.dumps({"text": t}, ensure_ascii=True) + "\n")
|
| 41 |
+
rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024
|
| 42 |
+
print(f"cache written: {len(texts)} docs -> {out_path} "
|
| 43 |
+
f"(peak RSS {rss:.0f} MiB)")
|
| 44 |
+
return 0
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
sys.exit(main(sys.argv[1], int(sys.argv[2]) if len(sys.argv) > 2 else 6000))
|