Download grade.py from amphora/MathConstructOptimize-Envs: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/grade.py
- Command line
-
hf download hf://datasets/amphora/MathConstructOptimize-Envs/grade.py
-
curl -L -o grade.py https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/grade.py
1.42 kB
| """Grade an answer for a row of the flat dataset, with no Docker and no LLM. | |
| from grade import grade | |
| reward, details = grade(row, answer_dict) | |
| `row` is a dataset row (needs `family` and `instance`); `answer_dict` is the parsed JSON answer. | |
| Construct tasks return 1.0 for any valid witness, else 0.0. Optimize tasks return 0 if invalid, | |
| otherwise 0.1 + 0.9 * progress from a trivial baseline to the best known value (see graders/run.py). | |
| Requires python>=3.10 with numpy, scipy, sympy, networkx. | |
| """ | |
| import importlib.util | |
| import json | |
| import os | |
| import sys | |
| from pathlib import Path | |
| _HERE = Path(os.path.abspath(__file__)).parent / "graders" # abspath, not resolve(): HF cache files are symlinks | |
| _cache = {} | |
| def _load(name, path): | |
| spec = importlib.util.spec_from_file_location(name, path) | |
| mod = importlib.util.module_from_spec(spec) | |
| sys.modules[name] = mod | |
| spec.loader.exec_module(mod) | |
| return mod | |
| def _check_fn(family): | |
| if family not in _cache: | |
| if "run" not in sys.modules: | |
| _load("run", _HERE / "run.py") | |
| _cache[family] = _load(f"check_{family}", _HERE / family / "check.py").check | |
| return _cache[family] | |
| def grade(row, answer): | |
| inst = row["instance"] | |
| if isinstance(inst, str): | |
| inst = json.loads(inst) | |
| run = sys.modules.get("run") or _load("run", _HERE / "run.py") | |
| return run.grade(inst, answer, _check_fn(row["family"])) | |