import json import numpy as np from hades.data.pipeline import normalize_stage, sample_stage, tokenize_stage from hades.data.shards import ShardWriter, split_of, write_manifest, write_shards from hades.data.sources.base import Record from hades.tokenizer.export import export_hf BOS, EOS = 900, 901 def test_split_of_deterministic_and_rate(): assert split_of("CVE-2024-1") == split_of("CVE-2024-1") vals = sum(split_of(f"doc-{i}") == "val" for i in range(100_000)) assert 300 <= vals <= 700 def test_shards_roundtrip(tmp_path): docs = [[1, 2, 3], [4], [5, 6, 7, 8, 9], [10, 11]] shards = write_shards(docs, tmp_path, "train", bos_id=BOS, eos_id=EOS, shard_tokens=8) data = np.concatenate([np.fromfile(s["path"], dtype="= 2 first = np.fromfile(shards[0]["path"], dtype=" 0 def test_tokenize_refuses_without_eval_texts(tmp_path, trained_small): import pytest sources = {"gen": _FakeSource("gen", "general", ["Plain educational text about rivers and valleys. " * 10])} interim = tmp_path / "interim" normalize_stage(tmp_path / "raw", interim, sources) export_hf(trained_small, tmp_path / "tok.json") (tmp_path / "eval").mkdir() with pytest.raises(ValueError, match="decontamination"): tokenize_stage(interim, tmp_path / "out", tmp_path / "tok.json", tmp_path / "eval", sources) tokenize_stage(interim, tmp_path / "out", tmp_path / "tok.json", tmp_path / "eval", sources, allow_no_decontam=True) m = json.loads((tmp_path / "out" / "manifest.json").read_text()) assert m["decontamination"] == {"eval_texts": 0, "ngrams": 0, "eval_sha256": None} def test_manifest_records_decontamination(tmp_path, trained_small): sources = {"gen": _FakeSource("gen", "general", ["Plain educational text about rivers and valleys. " * 10])} interim = tmp_path / "interim" normalize_stage(tmp_path / "raw", interim, sources) export_hf(trained_small, tmp_path / "tok.json") (tmp_path / "eval").mkdir() (tmp_path / "eval" / "eval_text.jsonl").write_text(json.dumps({"text": "a b c d e f g h i j k l m n o p"}) + "\n") tokenize_stage(interim, tmp_path / "out", tmp_path / "tok.json", tmp_path / "eval", sources) d = json.loads((tmp_path / "out" / "manifest.json").read_text())["decontamination"] assert d["eval_texts"] == 1 and d["ngrams"] > 0 and len(d["eval_sha256"]) == 64