vimeml-zh-v2 / source /src /vimeml /tokenizer /encode_zh13.py
Voltline's picture
vimeml-zh-v2-20m (colloq-c40): FP32 bundle, Core ML INT8 stateless and KV v4 packages, evaluation summary, source e3fba3e (part 2)
4096d3a verified
Raw History Blame Contribute Delete
25.1 kB
"""vimeml-tiny-zh-v1 token stores with nested hash pools.
measure (vocabulary-independent)
Every web/review train document gets h = first 8 bytes of
sha256(pool_label + "\\0" + id) as an unsigned integer, and its paragraph
character count. q is the smallest h such that documents with h < q hold at
least p1_target_characters; P5 is h < q // 5 and P20 is h < q // 20, so
P20 is a subset of P5, which is a subset of P1. The memorization probe is
the memo_documents P20 documents with the smallest memo hash.
encode (per vocabulary)
Web store: only P1 documents, one sequence BOS + paragraph + EOS per
paragraph, in export order, with train.tiers.bin (uint8 per paragraph):
0 probe (in P20), 1 P20, 2 P5 only, 3 P1 only. validation holds every
validation document. Chat store: each LCCC utterance is one sequence.
Other selections (zh-800m, docs/zh-CN/training-zh-800m-plan.md) write web-only
stores without train.tiers.bin: "rest" keeps the train documents outside P1 (the
complement of the P1 store, same pools), "all" keeps every train document of an
export that has no pools (a later batch such as export-r2).
Paragraphs are the stripped nonempty lines of a document. A paragraph containing
U+2581 (SentencePiece meta space, decoded as a space) cannot roundtrip and is
dropped everywhere, in measure too, and counted. Every stored paragraph is
checked to decode to its exact text. The file layout and manifest are the
vimeml_sentence_tokens_v1 format read by TokenStore and SentenceWindowDataset.
pools.json and the store manifests hold no timing, so a rebuild from the same
export, tokenizer and sentencepiece version is byte-identical on any machine;
the wall time goes to run.json beside them, which nothing hashes.
"""
import hashlib
import json
import os
import time
from concurrent.futures import ProcessPoolExecutor, as_completed
from pathlib import Path
import numpy as np
from vimeml.tokenizer.zh13 import META_SPACE, paragraphs, sha256, write_json
SPECIAL = {"pad": 0, "unk": 1, "bos": 2, "eos": 3}
WEB_SOURCES = {"fineweb2-web": 0, "fineweb2-review": 1}
CHAT_SOURCES = {"lccc": 0}
FIRST_TEXT_ID = 7 # 0-3 special, 4-6 control (<CTX>/<READ>/<OUT>); 7 is "\n"
TIER_NAMES = ("probe", "p20", "p5", "p1")
ENCODE_BATCH = 50_000 # web paragraphs per SentencePiece call (whole documents, never split)
def doc_hash(label, doc_id):
return int.from_bytes(hashlib.sha256(f"{label}\0{doc_id}".encode("utf-8")).digest()[:8], "big")
def clean_paragraphs(text):
kept, dropped = [], 0
for line in paragraphs(text):
if META_SPACE in line:
dropped += 1
else:
kept.append(line)
return kept, dropped
def web_files(export):
train = Path(export) / "train"
return sorted(train.glob("web-*.parquet")) + sorted(train.glob("review-*.parquet"))
def source_of(path):
return WEB_SOURCES["fineweb2-review" if Path(path).name.startswith("review-") else "fineweb2-web"]
# ---- measure -------------------------------------------------------------------------------
def _measure_file(job):
import pyarrow.parquet as pq
path, label, memo_label, out, max_groups = job
handle = pq.ParquetFile(path)
hashes, memo, chars = [], [], []
dropped = 0
for group in range(min(handle.metadata.num_row_groups, max_groups or 1 << 30)):
table = handle.read_row_group(group, columns=["id", "text"])
for doc_id, text in zip(table["id"].to_pylist(), table["text"].to_pylist()):
kept, lost = clean_paragraphs(text)
dropped += lost
hashes.append(doc_hash(label, doc_id))
memo.append(doc_hash(memo_label, doc_id))
chars.append(sum(map(len, kept)))
np.savez(out, hash=np.array(hashes, dtype=np.uint64), memo=np.array(memo, dtype=np.uint64),
chars=np.array(chars, dtype=np.uint64))
return {"file": Path(path).name, "documents": len(hashes), "characters": int(sum(chars)),
"dropped_meta_space_paragraphs": dropped}
def measure(config, export, pools, export_identity, max_groups=0):
export, pools = Path(export), Path(pools)
if (pools / "pools.json").exists():
raise ValueError(f"{pools} already holds pools.json; use a new directory")
(pools / "measure").mkdir(parents=True, exist_ok=True)
files = web_files(export)
jobs = [(str(p), config["pool_label"], config["memo_label"], str(pools / "measure" / (p.stem + ".npz")), max_groups)
for p in files]
began = time.time()
with ProcessPoolExecutor(config["workers"]) as pool:
per_file = list(pool.map(_measure_file, jobs))
arrays = [np.load(job[3]) for job in jobs]
h = np.concatenate([a["hash"] for a in arrays])
m = np.concatenate([a["memo"] for a in arrays])
c = np.concatenate([a["chars"] for a in arrays])
order = np.argsort(h, kind="stable")
cumulative = np.cumsum(c[order])
target = config["p1_target_characters"]
if cumulative[-1] < target:
raise ValueError(f"Only {int(cumulative[-1])} characters; target {target}")
last = int(np.searchsorted(cumulative, target))
q = int(h[order[last]]) + 1 # documents with h < q reach the target
thresholds = {"p1": q, "p5": q // 5, "p20": q // 20}
in20 = h < thresholds["p20"]
probe_pool = np.flatnonzero(in20)
if len(probe_pool) < config["memo_documents"]:
raise ValueError("P20 is smaller than the memorization probe")
ranked = probe_pool[np.argsort(m[probe_pool], kind="stable")]
memo_threshold = int(m[ranked[config["memo_documents"] - 1]]) + 1
tiers = {}
probe = in20 & (m < memo_threshold)
masks = {"probe": probe, "p20": in20 & ~probe, "p5": (h < thresholds["p5"]) & ~in20,
"p1": (h < q) & ~(h < thresholds["p5"])}
for name, mask in masks.items():
tiers[name] = {"documents": int(mask.sum()), "characters": int(c[mask].sum())}
pools_by_name = {"P20": in20, "P5": h < thresholds["p5"], "P1": h < q}
result = {"format": "vimeml_zh13_pools_v1", "status": "complete", "export": export_identity,
"labels": {"pool": config["pool_label"], "memo": config["memo_label"]},
"hash": "first 8 bytes of sha256(label + NUL + id), big-endian unsigned",
"p1_target_characters": target, "thresholds": thresholds,
"memo_threshold": memo_threshold, "memo_documents": config["memo_documents"],
"pools": {k: {"documents": int(v.sum()), "characters": int(c[v].sum())} for k, v in pools_by_name.items()},
"tiers": tiers, "all_train": {"documents": int(len(h)), "characters": int(c.sum())},
"files": per_file, "max_row_groups": max_groups}
write_json(pools / "pools.json", result)
seconds = round(time.time() - began, 1)
write_json(pools / "run.json", {"seconds": seconds, "pools_sha256": sha256(pools / "pools.json")})
return {**result, "seconds": seconds}
# ---- encode --------------------------------------------------------------------------------
class PartWriter:
# Hand the bytes to the file system in large blocks: one small write per document
# costs more than the encoding itself on a network volume.
FLUSH_BYTES = 8 << 20
def __init__(self, prefix):
self.prefix = Path(prefix)
self.streams = {name: open(f"{self.prefix}.{name}", "wb") for name in ("tokens", "lengths", "sources", "tiers")}
self.buffers = {name: bytearray() for name in self.streams}
self.stats = {"sentences": 0, "stored_tokens": 0, "content_tokens": 0, "characters": 0,
"max_sequence_tokens": 0, "dropped_meta_space_paragraphs": 0, "documents": 0,
"tiers": {name: {"documents": 0, "sentences": 0, "characters": 0, "prediction_pairs": 0}
for name in TIER_NAMES}}
def _write(self, name, data):
buffer = self.buffers[name]
buffer += data
if len(buffer) >= self.FLUSH_BYTES:
self.streams[name].write(buffer)
buffer.clear()
def _flush(self):
for name, buffer in self.buffers.items():
if buffer:
self.streams[name].write(buffer)
buffer.clear()
def add(self, sp, texts, source, tier=None):
self.add_batch(sp, texts, source, None if tier is None else [tier] * len(texts))
def add_batch(self, sp, texts, source, tiers=None):
"""Store texts in order, one sequence each; tiers is None or one tier per text.
One SentencePiece call for the whole batch: per-call overhead, not encoding, dominates
when a call carries one document. Each text is still encoded on its own, so the bytes
equal those of one add() per document over the same texts.
"""
ids = sp.encode(texts)
decoded = sp.decode(ids) if ids else []
for text, back, row in zip(texts, decoded, ids):
if back != text or min(row) < FIRST_TEXT_ID:
raise ValueError(f"Roundtrip/reserved-ID failure in {self.prefix.name}: {text[:40]!r}")
lengths = np.fromiter((len(x) + 2 for x in ids), dtype=np.uint32, count=len(ids))
flat = []
for row in ids:
flat.append(SPECIAL["bos"])
flat.extend(row)
flat.append(SPECIAL["eos"])
self._write("tokens", np.array(flat, dtype="<u2").tobytes())
self._write("lengths", lengths.astype("<u4").tobytes())
self._write("sources", bytes([source]) * len(ids))
if tiers is not None:
self._write("tiers", bytes(tiers))
for text, length, tier in zip(texts, lengths.tolist(), tiers):
t = self.stats["tiers"][TIER_NAMES[tier]]
t["sentences"] += 1
t["characters"] += len(text)
t["prediction_pairs"] += length - 1
s = self.stats
s["sentences"] += len(ids)
s["stored_tokens"] += int(lengths.sum())
s["content_tokens"] += int(lengths.sum()) - 2 * len(ids)
s["characters"] += sum(map(len, texts))
if len(ids):
s["max_sequence_tokens"] = max(s["max_sequence_tokens"], int(lengths.max()))
def close(self):
self._flush()
for stream in self.streams.values():
stream.close()
return self.stats
def _tier(h, m, thresholds, memo_threshold):
if h >= thresholds["p1"]:
return None
if h < thresholds["p20"]:
return 0 if m < memo_threshold else 1
return 2 if h < thresholds["p5"] else 3
SELECTIONS = ("p1", "rest", "all")
def _keep(selection, pools, doc_id):
"""(kept, tier) for one train document under a selection."""
if selection == "all":
return True, None
h = doc_hash(pools["labels"]["pool"], doc_id)
if selection == "rest":
return h >= pools["thresholds"]["p1"], None
tier = _tier(h, doc_hash(pools["labels"]["memo"], doc_id), pools["thresholds"], pools["memo_threshold"])
return tier is not None, tier
def _encode_file(job):
import pyarrow.parquet as pq
import sentencepiece as spm
path, kind, model, prefix, pools, max_groups = job[:6]
selection = job[6] if len(job) > 6 else "p1"
sp = spm.SentencePieceProcessor(model_file=model)
writer = PartWriter(prefix)
handle = pq.ParquetFile(path)
groups = min(handle.metadata.num_row_groups, max_groups or 1 << 30)
try:
for group in range(groups):
if kind == "chat":
texts = handle.read_row_group(group, columns=["text"])["text"].to_pylist()
kept = [t.strip() for t in texts if t.strip() and META_SPACE not in t]
writer.stats["dropped_meta_space_paragraphs"] += sum(1 for t in texts if META_SPACE in t)
writer.stats["documents"] += len(kept)
for start in range(0, len(kept), 4096):
writer.add(sp, kept[start:start + 4096], CHAT_SOURCES["lccc"])
continue
table = handle.read_row_group(group, columns=["id", "text"])
texts, tiers = [], [] # pending paragraphs of this row group, in document order
for doc_id, text in zip(table["id"].to_pylist(), table["text"].to_pylist()):
tier = None
if kind == "train":
kept_doc, tier = _keep(selection, pools, doc_id)
if not kept_doc:
continue
kept, lost = clean_paragraphs(text)
writer.stats["dropped_meta_space_paragraphs"] += lost
writer.stats["documents"] += 1
if tier is not None:
writer.stats["tiers"][TIER_NAMES[tier]]["documents"] += 1
if kept:
if (tier is None) != (not tiers) and texts: # never mix tiered and untiered texts
writer.add_batch(sp, texts, source_of(path), tiers or None)
texts, tiers = [], []
texts.extend(kept)
if tier is not None:
tiers.extend([tier] * len(kept))
if len(texts) >= ENCODE_BATCH:
writer.add_batch(sp, texts, source_of(path), tiers or None)
texts, tiers = [], []
if texts:
writer.add_batch(sp, texts, source_of(path), tiers or None)
finally:
stats = writer.close()
return {"file": Path(path).name, "prefix": str(prefix), "row_groups": groups, **stats}
def _merge(parts, out, split, with_tiers):
"""Concatenate part files in export order; offsets from cumulative lengths."""
names = [f"{split}.tokens.bin", f"{split}.offsets.bin", f"{split}.sources.bin"] + ([f"{split}.tiers.bin"] if with_tiers else [])
streams = {name: open(out / name, "wb") for name in names}
total = 0
try:
streams[f"{split}.offsets.bin"].write(np.zeros(1, dtype="<u8").tobytes())
for part in parts:
prefix = part["prefix"]
with open(f"{prefix}.tokens", "rb") as source:
while block := source.read(1 << 24):
streams[f"{split}.tokens.bin"].write(block)
lengths = np.fromfile(f"{prefix}.lengths", dtype="<u4").astype(np.uint64)
streams[f"{split}.offsets.bin"].write((np.cumsum(lengths) + np.uint64(total)).astype("<u8").tobytes())
total += int(lengths.sum())
streams[f"{split}.sources.bin"].write(Path(f"{prefix}.sources").read_bytes())
if with_tiers:
streams[f"{split}.tiers.bin"].write(Path(f"{prefix}.tiers").read_bytes())
finally:
for stream in streams.values():
stream.close()
return names
def _split_stats(parts, tiers):
keys = ("sentences", "stored_tokens", "content_tokens", "characters", "documents", "dropped_meta_space_paragraphs")
stats = {k: sum(p[k] for p in parts) for k in keys}
stats["max_sequence_tokens"] = max(p["max_sequence_tokens"] for p in parts)
stats["prediction_pairs"] = stats["stored_tokens"] - stats["sentences"]
if tiers:
stats["tiers"] = {name: {k: sum(p["tiers"][name][k] for p in parts) for k in parts[0]["tiers"][name]}
for name in TIER_NAMES}
return stats
PART_FILES = ("tokens", "lengths", "sources", "tiers")
POLICIES = {"p1": ("BOS + stripped nonempty paragraph + EOS; P1 documents only in train, "
"train.tiers.bin per paragraph (0 probe, 1 P20, 2 P5, 3 P1)"),
"rest": "BOS + stripped nonempty paragraph + EOS; train documents outside P1 only, no tiers",
"all": "BOS + stripped nonempty paragraph + EOS; every train document, no pools, no tiers"}
def _done_path(prefix):
return Path(f"{prefix}.done.json")
def _reusable(job, key):
"""The finished record of a part from an interrupted run with the same inputs, or None.
A part counts as finished only when its .done.json exists (written after the worker
returned), names the same inputs and its four files still have the recorded sizes.
"""
path = _done_path(job[3])
if not path.exists():
return None
try:
done = json.loads(path.read_text("utf-8"))
except ValueError:
return None
if done.get("key") != key:
return None
for name, size in done.get("bytes", {}).items():
part = Path(f"{job[3]}.{name}")
if not part.exists() or part.stat().st_size != size:
return None
return done["record"]
def _mark_done(record, key):
prefix = record["prefix"]
data = {"key": key, "record": record,
"bytes": {name: Path(f"{prefix}.{name}").stat().st_size for name in PART_FILES}}
tmp = Path(f"{prefix}.done.json.tmp")
tmp.write_text(json.dumps(data, ensure_ascii=False), "utf-8")
os.replace(tmp, _done_path(prefix))
def encode(config, export, vocab, outputs, pools_path, export_identity, max_groups=0, resume=False,
selection="p1"):
"""outputs: {'web': token dir, 'chat': token dir, 'web_index': dir, 'chat_index': dir}.
resume=True continues an interrupted run in the same output directories: parts whose
completion record matches the current inputs are reused, every other file is encoded again.
selection "p1" (default) writes the tiered P1 web store and the chat store; "rest" and
"all" write only a web store (outputs need just 'web' and 'web_index'), see the module notes.
"""
if selection not in SELECTIONS:
raise ValueError(f"Unknown selection {selection!r}")
stores = ("web", "chat") if selection == "p1" else ("web",)
import sentencepiece
from vimeml.training.data import prepare_indexes
spm_version = sentencepiece.__version__
export = Path(export)
tokenizers = json.loads((Path(config["tokenizer_dir"]) / "manifest.json").read_text("utf-8"))
record = tokenizers["models"][str(vocab)]
model = Path(config["tokenizer_dir"]) / record["directory"] / "tokenizer.model"
if sha256(model) != record["tokenizer_model_sha256"]:
raise ValueError("Tokenizer differs from its manifest")
if selection == "all":
if pools_path is not None:
raise ValueError("selection 'all' takes no pools")
pools = None
else:
pools = json.loads(Path(pools_path).read_text("utf-8"))
if pools.get("status") != "complete" or pools["export"] != export_identity:
raise ValueError("Pools were measured on a different export")
for key in stores:
out = Path(outputs[key])
if (out / "manifest.json").exists():
raise ValueError(f"{out} is already complete; use a new directory")
if out.exists() and any(out.iterdir()):
if not resume:
raise ValueError(f"{out} is not empty; use a new directory, or resume the interrupted run")
if any(p.name != "parts" for p in out.iterdir()):
raise ValueError(f"{out} has files besides parts/; it stopped while merging, use a new directory")
(out / "parts").mkdir(parents=True, exist_ok=True)
pools_sha = sha256(pools_path) if pools_path is not None else None
run_key = {"vocab": vocab, "tokenizer_model_sha256": record["tokenizer_model_sha256"],
"pools_sha256": pools_sha, "export": export_identity,
"sentencepiece_version": spm_version, "max_row_groups": max_groups}
if selection != "p1":
run_key["selection"] = selection
began = time.time()
jobs = {store: {} for store in stores}
for split, kind, files in (("train", "train", web_files(export)),
("validation", "validation", sorted((export / "validation").glob("web-*.parquet"))
+ sorted((export / "validation").glob("review-*.parquet")))):
jobs["web"][split] = [(str(p), kind, str(model), str(Path(outputs["web"]) / "parts" / f"{split}-{p.stem}"),
pools, max_groups) + ((selection,) if selection != "p1" else ()) for p in files]
for split in ("train", "validation") if "chat" in stores else ():
path = export / split / f"chat-lccc-{split}.parquet"
jobs["chat"][split] = [(str(path), "chat", str(model), str(Path(outputs["chat"]) / "parts" / split), pools, max_groups)]
results = {}
def job_key(job):
return {**run_key, "file": Path(job[0]).name, "kind": job[1]}
with ProcessPoolExecutor(config["workers"]) as pool:
futures, reused = {}, {}
for store, by_split in jobs.items():
for split, split_jobs in by_split.items():
key = (store, split)
reused[key] = [_reusable(job, job_key(job)) if resume else None for job in split_jobs]
futures[key] = []
for job, old in zip(split_jobs, reused[key]):
future = None
if old is None:
future = pool.submit(_encode_file, job)
# mark the part finished as soon as it is, not when its split is reached
future.add_done_callback(
lambda f, k=job_key(job): f.exception() is None and _mark_done(f.result(), k))
futures[key].append(future)
for key, items in futures.items():
total = len(items)
ordered = list(reused[key])
done = sum(r is not None for r in ordered)
if done:
print(json.dumps({"reused": done, "of": total, "split": "/".join(key)}), flush=True)
# Progress per finished file: a whole split can take an hour, so waiting for the
# group before printing anything hides every sign of progress.
pending = [f for f in items if f is not None]
for future in as_completed(pending):
part = future.result()
ordered[items.index(future)] = part
done += 1
print(json.dumps({"file": part["file"], "sentences": part["sentences"],
"characters": part["characters"],
"done": done, "of": total, "split": "/".join(key)}), flush=True)
results[key] = ordered
print(json.dumps({"encoded": list(key), "files": total,
"sentences": sum(r["sentences"] for r in ordered)}), flush=True)
manifests = {}
for store, sources in (("web", WEB_SOURCES), ("chat", CHAT_SOURCES))[:len(stores)]:
out = Path(outputs[store])
splits, digests = {}, {}
for split in ("train", "validation"):
parts = results[(store, split)]
with_tiers = store == "web" and split == "train" and selection == "p1"
names = _merge(parts, out, split, with_tiers)
splits[split] = _split_stats(parts, with_tiers)
splits[split]["file_bytes"] = {n.split(".", 1)[1]: (out / n).stat().st_size for n in names}
splits[split]["primary_sources"] = {name: 0 for name in sources}
source_counts = np.bincount(np.fromfile(out / f"{split}.sources.bin", dtype=np.uint8), minlength=len(sources))
for name, index in sources.items():
splits[split]["primary_sources"][name] = int(source_counts[index])
for n in names:
digests[n] = sha256(out / n)
for part in (out / "parts").iterdir():
part.unlink()
(out / "parts").rmdir()
manifest = {"format": "vimeml_sentence_tokens_v1", "status": "complete", "vocab_size": vocab,
"token_dtype": "uint16_le", "offset_dtype": "uint64_le", "offset_unit": "tokens",
"special_ids": SPECIAL, "reserved_ids": record["reserved_ids"],
"tokenizer_model_sha256": record["tokenizer_model_sha256"],
"tokenizer_manifest_sha256": sha256(Path(config["tokenizer_dir"]) / "manifest.json"),
"source_ids": sources, "splits": splits, "available_splits": ["train", "validation"],
"sequence_policy": (POLICIES[selection] if store == "web"
else "BOS + stripped LCCC utterance + EOS"),
"export": export_identity, "pools_sha256": pools_sha, "output_sha256": digests,
"sentencepiece_version": spm_version,
"test_or_blind_read": False, "max_row_groups": max_groups}
if selection != "p1":
manifest["selection"] = selection
write_json(out / "manifest.json", manifest)
write_json(out / "run.json", {"seconds": round(time.time() - began, 1),
"manifest_sha256": sha256(out / "manifest.json")})
index = prepare_indexes(out, Path(outputs[f"{store}_index"]), config["context_length"], splits=("train", "validation"))
manifests[store] = {"tokens": manifest["splits"], "windows": {s: index["splits"][s]["windows"] for s in index["splits"]}}
return manifests