File size: 2,402 Bytes
fbfc91b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Materialize Kelp splits from the pinned upstream Arrow file and published index."""
import argparse
import hashlib
from pathlib import Path

import pyarrow as pa
from datasets import Dataset, DatasetDict, load_dataset
from huggingface_hub import hf_hub_download

INDEX = "ccccwx/kelp-length-splits"
UPSTREAM = "Alibaba-AAIG/StreamGuardBench"
REV = "7fa1203b2d9be5671d3b40f2c1df0035deeb098c"
FILE = "wildguard/qwen3_8b/train/data-00000-of-00001.arrow"
SHA = "a4bfd41c1b94578c61f870189c01f4908aee20b6abf6e4784836430ce24a2a1d"


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("config", choices=("natural", "length_balanced", "hard_length"))
    parser.add_argument("output", type=Path)
    parser.add_argument("--source", type=Path)
    parser.add_argument("--local-index", type=Path)
    args = parser.parse_args()
    if args.output.exists():
        parser.error("Choose a new output directory.")
    upstream = args.source or Path(hf_hub_download(UPSTREAM, FILE, revision=REV, repo_type="dataset"))
    with upstream.open("rb") as handle:
        if hashlib.file_digest(handle, "sha256").hexdigest() != SHA:
            raise ValueError("Unexpected upstream Arrow checksum.")
    if args.local_index:
        paths = {s: str(args.local_index / args.config / f"{s}.parquet")
                 for s in ("train", "validation", "test")}
        index = load_dataset("parquet", data_files=paths)
    else:
        index = load_dataset(INDEX, args.config)
    group_split, row_split = {}, {}
    result = {}
    with pa.memory_map(str(upstream)) as handle:
        table = pa.ipc.open_stream(handle).read_all().select(["prompt", "response"])
        for split, items in index.items():
            for row, group in zip(items["source_row"], items["group_id"], strict=True):
                if not 0 <= row < len(table) or row in row_split or group_split.setdefault(group, split) != split:
                    raise ValueError(f"Invalid or overlapping source row {row} in {split}")
                row_split[row] = split
            texts = table.take(list(items["source_row"]))
            result[split] = Dataset(texts.append_column("label", pa.array(list(items["label"]), type=pa.int64())))
        DatasetDict(result).save_to_disk(str(args.output))
    print({split: len(items) for split, items in result.items()})


if __name__ == "__main__":
    main()