logic-puzzles / benchmark_overlap.py
jiteshkprasad's picture
Udgam Logic Puzzles v1: 92,507 puzzles, generator and checker
df805d1 verified
Raw History Blame Contribute Delete
3.02 kB
# Copyright 2026 Udgam Labs
# SPDX-License-Identifier: Apache-2.0
"""Overlap of Udgam Logic Puzzles with public reasoning benchmarks that use similar puzzle types.
python benchmark_overlap.py STAGE_DIR BENCH_DIR OUT.json
BENCH_DIR holds local copies of lukaemon/bbh (all 27 tasks), K-and-K/knights-and-knaves (test) and
allenai/ZebraLogicBench (grid_mode, mc_mode). For each of our questions we look for (a) an identical normalised text and
(b) any shared run of 13 words (the GPT-3 style contamination check); 8-word runs are counted for information.
"""
import collections
import glob
import json
import os
import re
import sys
import pyarrow.parquet as pq
def words(t):
return re.findall(r"[a-z0-9]+", t.lower())
def grams(ws, n):
return {" ".join(ws[i:i + n]) for i in range(len(ws) - n + 1)} if len(ws) >= n else {" ".join(ws)}
def bench_items(d):
for p in sorted(glob.glob(os.path.join(d, "bbh", "*", "*.parquet"))):
task = p.split(os.sep)[-2]
for r in pq.read_table(p).to_pylist():
yield f"bbh/{task}", r["input"]
for p in sorted(glob.glob(os.path.join(d, "kk", "test", "*.jsonl"))):
for line in open(p):
yield "knights-and-knaves", json.loads(line)["quiz"]
for p in sorted(glob.glob(os.path.join(d, "zebra", "*", "*.parquet"))):
for r in pq.read_table(p).to_pylist():
yield f"zebralogic/{p.split(os.sep)[-2]}", r["puzzle"]
def main(stage, bench, out):
ours = []
for split in ("train", "test"):
ours += [(split, r["id"], r["family"], r["question"]) for r in pq.read_table(os.path.join(stage, "data", f"{split}.parquet")).to_pylist()]
exact, g13, g8, n_items = {}, collections.defaultdict(set), collections.defaultdict(set), collections.Counter()
for name, text in bench_items(bench):
n_items[name.split("/")[0]] += 1
ws = words(text)
exact[" ".join(ws)] = name
for g in grams(ws, 13):
g13[g].add(name)
for g in grams(ws, 8):
g8[g].add(name)
hits13, hits8, hits_exact = collections.Counter(), collections.Counter(), 0
examples = collections.defaultdict(list)
for split, pid, fam, q in ours:
ws = words(q)
if " ".join(ws) in exact:
hits_exact += 1
shared = {g: g13[g] for g in grams(ws, 13) if g in g13}
if shared:
hits13[fam] += 1
if len(examples[fam]) < 3:
g, src = next(iter(shared.items()))
examples[fam].append(dict(id=pid, benchmark=sorted(src)[0], shared_13_words=g))
if any(g in g8 for g in grams(ws, 8)):
hits8[fam] += 1
res = dict(benchmark_items=dict(n_items), our_questions=len(ours), exact_matches=hits_exact,
questions_sharing_13_words=dict(hits13), questions_sharing_8_words=dict(hits8), examples_13=examples)
json.dump(res, open(out, "w"), indent=1)
print(json.dumps(res, indent=1)[:3000])
if __name__ == "__main__":
main(*sys.argv[1:4])