| from __future__ import annotations |
|
|
| import argparse |
| import ast |
| import hashlib |
| import json |
| import random |
| from collections import Counter |
| from datetime import datetime, timezone |
| from pathlib import Path |
|
|
| import pyarrow as pa |
| import pyarrow.parquet as pq |
|
|
| from .families import FAMILIES |
|
|
| VERSION = "0.1.0" |
| LICENSE = "Apache-2.0" |
| DEFAULT_ROWS = 100_000 |
| GENERATOR_VERSION = "oellm-code-generators-0.1.0" |
| VERIFIER_VERSION = "oellm-code-stdio-contract-0.1.0" |
| DATASET_ID = "birgermoell/oellm-code-rlvr" |
|
|
| TEST_CASE = pa.struct([("input", pa.string()), ("output", pa.string()), ("type", pa.string())]) |
| SCHEMA = pa.schema([ |
| ("id", pa.string()), |
| ("messages", pa.list_(pa.struct([("role", pa.string()), ("content", pa.string())]))), |
| ("problem", pa.string()), |
| ("verification_info", pa.struct([("language", pa.string()), ("test_cases", pa.list_(TEST_CASE))])), |
| ("ground_truth", pa.string()), |
| ("public_tests", pa.list_(TEST_CASE)), |
| ("reference_solution", pa.string()), |
| ("dataset", pa.string()), |
| ("split", pa.string()), |
| ("programming_language", pa.string()), |
| ("prompt_language", pa.string()), |
| ("difficulty", pa.int8()), |
| ("difficulty_label", pa.string()), |
| ("generator_family", pa.string()), |
| ("concepts", pa.list_(pa.string())), |
| ("generator_version", pa.string()), |
| ("generation_seed", pa.int64()), |
| ("semantic_group_id", pa.string()), |
| ("parameters_json", pa.string()), |
| ("verifier_kind", pa.string()), |
| ("verifier_version", pa.string()), |
| ("time_limit_seconds", pa.int16()), |
| ("memory_limit_mb", pa.int16()), |
| ("hidden_test_count", pa.int16()), |
| ("mutation_score", pa.float32()), |
| ("source", pa.string()), |
| ("source_license", pa.string()), |
| ("prompt_sha256", pa.string()), |
| ("reference_sha256", pa.string()), |
| ("contamination_group", pa.string()), |
| ]) |
|
|
|
|
| def digest(text: str) -> str: |
| return hashlib.sha256(text.encode("utf-8")).hexdigest() |
|
|
|
|
| def stable_int(text: str) -> int: |
| return int.from_bytes(hashlib.sha256(text.encode()).digest()[:8], "big") & ((1 << 63) - 1) |
|
|
|
|
| def split_for(group_id: str) -> str: |
| bucket = stable_int("split:" + group_id) % 1000 |
| if bucket < 25: |
| return "test" |
| if bucket < 50: |
| return "validation" |
| return "train" |
|
|
|
|
| def difficulty_for(seed: int) -> int: |
| bucket = stable_int(f"difficulty:{seed}") % 100 |
| if bucket < 15: |
| return 1 |
| if bucket < 40: |
| return 2 |
| if bucket < 70: |
| return 3 |
| if bucket < 90: |
| return 4 |
| return 5 |
|
|
|
|
| def _typed(cases: list[dict[str, str]]) -> list[dict[str, str]]: |
| return [{"input": c["input"], "output": c["output"], "type": "stdin_stdout"} for c in cases] |
|
|
|
|
| def build_row(family: str, semantic_index: int) -> dict: |
| group_id = f"oellm-code-{family}-{semantic_index:09d}" |
| seed = stable_int(f"{VERSION}:{family}:{semantic_index}") |
| difficulty = difficulty_for(seed) |
| material = FAMILIES[family](random.Random(seed), difficulty) |
| if material.mutation_score < 2 / 3: |
| raise ValueError("insufficient mutation coverage") |
| ast.parse(material.reference_solution) |
| hidden = _typed(material.hidden_tests) |
| public = _typed(material.public_tests) |
| prompt_hash = digest(material.statement) |
| reference_hash = digest(material.reference_solution) |
| return { |
| "id": digest(group_id)[:24], |
| "messages": [{"role": "user", "content": material.statement}], |
| "problem": material.statement, |
| "verification_info": {"language": "python", "test_cases": hidden}, |
| "ground_truth": json.dumps(hidden, sort_keys=True, separators=(",", ":")), |
| "public_tests": public, |
| "reference_solution": material.reference_solution, |
| "dataset": "oellm-code-rlvr", |
| "split": split_for(group_id), |
| "programming_language": "python", |
| "prompt_language": "en", |
| "difficulty": difficulty, |
| "difficulty_label": ["", "introductory", "basic", "intermediate", "advanced", "challenge"][difficulty], |
| "generator_family": family, |
| "concepts": material.concepts, |
| "generator_version": GENERATOR_VERSION, |
| "generation_seed": seed, |
| "semantic_group_id": group_id, |
| "parameters_json": json.dumps(material.parameters, sort_keys=True, separators=(",", ":")), |
| "verifier_kind": "code_stdio", |
| "verifier_version": VERIFIER_VERSION, |
| "time_limit_seconds": 2, |
| "memory_limit_mb": 256, |
| "hidden_test_count": len(hidden), |
| "mutation_score": material.mutation_score, |
| "source": "deterministic_procedural_generation", |
| "source_license": LICENSE, |
| "prompt_sha256": prompt_hash, |
| "reference_sha256": reference_hash, |
| "contamination_group": f"generator:{family}:v0.1", |
| } |
|
|
|
|
| def file_sha256(path: Path) -> str: |
| result = hashlib.sha256() |
| with path.open("rb") as handle: |
| for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): |
| result.update(chunk) |
| return result.hexdigest() |
|
|
|
|
| class SplitWriters: |
| def __init__(self, root: Path): |
| root.mkdir(parents=True, exist_ok=True) |
| self.paths = {s: root / f"{s}-00000-of-00001.parquet" for s in ("train", "validation", "test")} |
| self.writers = {s: pq.ParquetWriter(p, SCHEMA, compression="zstd", compression_level=6) for s, p in self.paths.items()} |
| self.buffers = {s: [] for s in self.paths} |
| self.counts = Counter() |
|
|
| def add(self, row: dict) -> None: |
| split = row["split"] |
| self.buffers[split].append(row) |
| self.counts[split] += 1 |
| if len(self.buffers[split]) >= 2_000: |
| self.flush(split) |
|
|
| def flush(self, split: str) -> None: |
| if self.buffers[split]: |
| self.writers[split].write_table(pa.Table.from_pylist(self.buffers[split], schema=SCHEMA)) |
| self.buffers[split].clear() |
|
|
| def close(self) -> None: |
| for split in self.writers: |
| self.flush(split) |
| self.writers[split].close() |
|
|
|
|
| def build(output: Path, rows: int = DEFAULT_ROWS) -> dict: |
| writers = SplitWriters(output / "data") |
| families = list(FAMILIES) |
| accepted = Counter() |
| candidates = Counter() |
| family_target = {family: rows // len(families) for family in families} |
| for family in families[: rows % len(families)]: |
| family_target[family] += 1 |
| seen_prompts: set[str] = set() |
| difficulties, mutation_buckets, hidden_counts = Counter(), Counter(), Counter() |
|
|
| while sum(accepted.values()) < rows: |
| for family in families: |
| if accepted[family] >= family_target[family]: |
| continue |
| index = candidates[family] |
| candidates[family] += 1 |
| try: |
| row = build_row(family, index) |
| except ValueError: |
| continue |
| if row["prompt_sha256"] in seen_prompts: |
| continue |
| seen_prompts.add(row["prompt_sha256"]) |
| writers.add(row) |
| accepted[family] += 1 |
| difficulties[str(row["difficulty"])] += 1 |
| mutation_buckets[f"{row['mutation_score']:.2f}"] += 1 |
| hidden_counts[str(row["hidden_test_count"])] += 1 |
|
|
| writers.close() |
| files = { |
| split: {"path": f"data/{path.name}", "rows": writers.counts[split], "bytes": path.stat().st_size, "sha256": file_sha256(path)} |
| for split, path in writers.paths.items() |
| } |
| manifest = { |
| "dataset_id": DATASET_ID, |
| "version": VERSION, |
| "created_utc": datetime.now(timezone.utc).isoformat(), |
| "rows": rows, |
| "programming_languages": {"python": rows}, |
| "prompt_languages": {"en": rows}, |
| "generator_families": dict(sorted(accepted.items())), |
| "difficulty": dict(sorted(difficulties.items())), |
| "mutation_scores": dict(sorted(mutation_buckets.items())), |
| "hidden_test_counts": dict(sorted(hidden_counts.items())), |
| "files": files, |
| "license": LICENSE, |
| "generation": {"generator_version": GENERATOR_VERSION, "verifier_contract": VERIFIER_VERSION}, |
| } |
| (output / "manifest.json").write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n") |
| return manifest |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--output", type=Path, default=Path("release")) |
| parser.add_argument("--rows", type=int, default=DEFAULT_ROWS) |
| args = parser.parse_args() |
| print(json.dumps(build(args.output, args.rows), indent=2)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|