ajinkyamulay's picture
v9: all eight domains plus 13 Reasoning Gym families
440d35c verified
Raw History Blame Contribute Delete
1.83 kB
import inspect
import os
import random
import re
from . import finance, industrial, mathematics, media, office, science, security, swe
REGISTRY = {}
for _module in (swe, industrial, science, office, finance, mathematics, security, media):
REGISTRY.update(_module.TASKS)
if os.path.isdir(os.environ.get("WB_REPO_LIBS", "/app/repo_libs")): # Repo Repair needs vendored libraries
from . import repo
REGISTRY.update(repo.TASKS)
try: # v9: Reasoning Gym problems, when the library is installed
from . import rgym
REGISTRY.update(rgym.TASKS)
except ImportError:
pass
# A task id is a family, optionally with a difficulty level: "fin-loan-balance@2" or "-l2".
LEVELS = (1, 2)
TASK_IDS = list(REGISTRY)
def _max_level(family):
return getattr(REGISTRY[family], "max_level", max(LEVELS))
ALL_TASK_IDS = [f"{t}@{lv}" for t in REGISTRY for lv in LEVELS if lv <= _max_level(t)]
def parse_task_id(task_id: str):
m = re.fullmatch(r"(.+)-l(\d)", task_id) # "fin-loan-balance-l2" is "fin-loan-balance@2"
if m and m.group(1) in REGISTRY:
task_id = f"{m.group(1)}@{m.group(2)}"
family, _, level = task_id.partition("@")
if family not in REGISTRY or level not in ("",) + tuple(str(i) for i in range(1, _max_level(family) + 1)):
raise KeyError(task_id)
return family, int(level or 1)
def is_valid(task_id: str) -> bool:
try:
family, level = parse_task_id(task_id)
except KeyError:
return False
return 1 <= level <= _max_level(family)
def build_task(task_id: str, seed: int):
family, level = parse_task_id(task_id)
fn = REGISTRY[family]
rng = random.Random(f"{family}@{level}:{seed}" if level > 1 else f"{family}:{seed}")
if "level" in inspect.signature(fn).parameters:
return fn(rng, level=level)
return fn(rng)