Datasets:
Download kernel_bench/restricted.py from code-roamer/gpu-kernel-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 4.14 kB
-
https://huggingface.co/datasets/code-roamer/gpu-kernel-benchmark/resolve/main/kernel_bench/restricted.py
- Command line
-
hf download hf://datasets/code-roamer/gpu-kernel-benchmark/kernel_bench/restricted.py
-
curl -L -o restricted.py https://huggingface.co/datasets/code-roamer/gpu-kernel-benchmark/resolve/main/kernel_bench/restricted.py
4.14 kB
| """Optional Linux host containment for grader workers; NOT a tamper-proof judge. | |
| The candidate still runs inside the Python measurement process. This restricts | |
| host files, credentials, networking, processes and visible device nodes, but does | |
| not make Python correctness/timing functions trustworthy against malicious code. | |
| """ | |
| from contextlib import contextmanager | |
| import os | |
| from pathlib import Path | |
| import shutil | |
| import sys | |
| import tempfile | |
| def worker_command(command, candidate, case, output, gpu, executable): | |
| if not executable: | |
| yield command | |
| return | |
| binary = shutil.which(executable) | |
| if not binary: | |
| raise FileNotFoundError("bubblewrap executable not found: " + executable) | |
| root = Path(__file__).resolve().parent | |
| with tempfile.TemporaryDirectory(prefix="kernelbench-restricted-") as temporary: | |
| stage = Path(temporary) | |
| package = stage / "grader/kernel_bench" | |
| package.mkdir(parents=True) | |
| for source in root.glob("*.py"): | |
| shutil.copyfile(source, package / source.name) | |
| shutil.copytree(root / "submissions", package / "submissions", ignore=shutil.ignore_patterns("__pycache__")) | |
| submission = stage / "submission" | |
| submission.mkdir() | |
| candidate = Path(candidate).resolve() | |
| for source in candidate.parent.iterdir(): | |
| if source.is_file() and source.suffix in {".py", ".cu", ".cpp", ".h", ".cuh"}: | |
| shutil.copyfile(source, submission / source.name) | |
| shutil.copyfile(case, stage / "case.json") | |
| results = stage / "results" | |
| results.mkdir() | |
| cmd = [binary, "--unshare-all", "--die-with-parent", "--new-session", | |
| "--cap-drop", "ALL", "--clearenv"] | |
| for name in ("/usr", "/lib", "/lib64", "/bin", "/sbin", "/sys"): | |
| if Path(name).exists(): | |
| cmd += ["--ro-bind", name, name] | |
| if Path("/etc/ld.so.cache").exists(): | |
| cmd += ["--ro-bind", "/etc/ld.so.cache", "/etc/ld.so.cache"] | |
| # Only this interpreter's venv, never its parent project or NAS mount. | |
| cmd += ["--ro-bind", sys.prefix, sys.prefix, "--proc", "/proc", "--dev", "/dev", | |
| "--tmpfs", "/tmp", "--dir", "/tmp/home"] | |
| for name in ("nvidiactl", "nvidia-uvm", "nvidia-uvm-tools", "nvidia" + gpu["index"]): | |
| node = "/dev/" + name | |
| if Path(node).exists(): | |
| cmd += ["--dev-bind", node, node] | |
| cmd += ["--ro-bind", str(stage / "grader"), "/work", | |
| "--ro-bind", str(submission), "/submission", | |
| "--ro-bind", str(stage / "case.json"), "/case.json", | |
| "--bind", str(results), "/results", "--chdir", "/work"] | |
| allowed_env = {"PATH": str(Path(sys.executable).parent) + ":/usr/local/bin:/usr/bin:/bin", | |
| "HOME": "/tmp/home", "TMPDIR": "/tmp", "CUDA_VISIBLE_DEVICES": gpu["uuid"], | |
| "OMP_NUM_THREADS": "1", "TORCHINDUCTOR_COMPILE_THREADS": "1", | |
| "PYTHONNOUSERSITE": "1", "PYTHONDONTWRITEBYTECODE": "1"} | |
| # Library paths only; do not pass API keys, SSH variables or arbitrary env. | |
| for key in ("LD_LIBRARY_PATH", "CUDA_HOME", "TRITON_PTXAS_PATH"): | |
| if os.environ.get(key): | |
| allowed_env[key] = os.environ[key] | |
| for key, value in allowed_env.items(): | |
| cmd += ["--setenv", key, value] | |
| replacements = {str(candidate): "/submission/" + candidate.name, | |
| str(Path(case)): "/case.json", str(Path(output)): "/results/result.json"} | |
| cmd += [replacements.get(str(part), str(part)) for part in command] | |
| try: | |
| yield cmd | |
| finally: | |
| produced = results / "result.json" | |
| # Copy bytes only, never follow a candidate-created symlink to host files. | |
| if produced.is_file() and not produced.is_symlink(): | |
| with produced.open("rb") as source: | |
| raw = source.read(16 * 1024 * 1024 + 1) | |
| if len(raw) <= 16 * 1024 * 1024: | |
| Path(output).write_bytes(raw) | |