Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion Magpie/scripts/benchmark/magpie_bench_remote_compat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,62 @@ magpie_run_benchmark_serving_remote_direct() {
return "$rc"
}

###############################################################################
# magpie_eval_needs_unsafe_code
#
# lm-eval refuses HumanEval (and other tasks with unsafe_code: true) unless
# --confirm_run_unsafe_code is set. Enable that flag when MAGPIE_EVAL_TASKS
# contains humaneval*, or when MAGPIE_EVAL_CONFIRM_UNSAFE_CODE is true/1.
###############################################################################
magpie_eval_needs_unsafe_code() {
case "${MAGPIE_EVAL_CONFIRM_UNSAFE_CODE:-}" in
1|true|TRUE|yes|YES) return 0 ;;
esac
local tasks="${MAGPIE_EVAL_TASKS:-}"
[[ ",${tasks}," == *humaneval* ]]
}

###############################################################################
# magpie_prepare_inferencex_eval_env
#
# InferenceX run_lm_eval reads EVAL_TASKS_DIR and passes it to
# `lm_eval --tasks`. Crystal and Magpie callers send comma-separated builtin
Comment thread
haofrank marked this conversation as resolved.
Resolved
# task names on MAGPIE_EVAL_TASKS; without this mapping the local path keeps
# evaluating a single YAML such as utils/evals/gsm8k.yaml.
###############################################################################
magpie_prepare_inferencex_eval_env() {
if [[ -n "${MAGPIE_EVAL_TASKS:-}" ]]; then
export EVAL_TASKS_DIR="${MAGPIE_EVAL_TASKS// /}"
Comment thread
haofrank marked this conversation as resolved.
Resolved
echo "[magpie_bench_remote_compat] EVAL_TASKS_DIR <- MAGPIE_EVAL_TASKS=${EVAL_TASKS_DIR}" >&2
fi
if [[ -z "${EVAL_LIMIT:-}" && -n "${MAGPIE_EVAL_LIMIT:-}" ]]; then
export EVAL_LIMIT="$MAGPIE_EVAL_LIMIT"
fi
local include="${MAGPIE_EVAL_INCLUDE_PATH:-${MAGPIE_EVAL_TASK_PATH:-}}"
if [[ -n "$include" && "$include" != *","* ]]; then
if [[ -d "$include" ]]; then
export EVAL_INCLUDE_PATH="$include"
Comment thread
haofrank marked this conversation as resolved.
Resolved
elif [[ -f "$include" ]]; then
export EVAL_INCLUDE_PATH="$(cd "$(dirname "$include")" && pwd)"
fi
fi
}

###############################################################################
# magpie_python3_with_lm_eval_unsafe
#
# InferenceX run_lm_eval invokes `python3 -m lm_eval` in this shell and has no
# extra-args hook. Intercept that invocation to append the HumanEval flag
# without replacing InferenceX chat-completions / batched-concurrency logic.
###############################################################################
magpie_python3_with_lm_eval_unsafe() {
if [[ "${1:-}" == "-m" && "${2:-}" == "lm_eval" ]]; then
command python3 -m lm_eval --confirm_run_unsafe_code "${@:3}"
else
command python3 "$@"
fi
}

###############################################################################
# magpie_run_eval_remote_direct
#
Expand Down Expand Up @@ -158,6 +214,9 @@ magpie_run_eval_remote_direct() {
if [[ -n "${MAGPIE_EVAL_LIMIT:-}" ]]; then
cmd+=(--limit "$MAGPIE_EVAL_LIMIT")
fi
if magpie_eval_needs_unsafe_code; then
cmd+=(--confirm_run_unsafe_code)
Comment thread
haofrank marked this conversation as resolved.
Resolved
fi

echo "[magpie_bench_remote_compat] lm_eval cmd: ${cmd[*]}" >&2
set -x
Expand Down Expand Up @@ -297,13 +356,19 @@ PY
# Run InferenceX's local-server eval and copy its result artifacts into
# Magpie's mounted workspace. The source lm-eval directory is left untouched;
# Magpie owns only the copies under $RESULT_DIR/lm_eval.
#
# When MAGPIE_EVAL_TASKS is set, export it as EVAL_TASKS_DIR so InferenceX
# `lm_eval --tasks` receives comma-separated builtin task names. HumanEval
# additionally requires --confirm_run_unsafe_code.
###############################################################################
magpie_run_eval_persisted() {
if ! declare -F run_eval &>/dev/null; then
echo "[magpie_bench_remote_compat] ERROR run_eval is unavailable" >&2
return 1
fi

magpie_prepare_inferencex_eval_env

local result_dir="${RESULT_DIR:-${WORKSPACE_DIR:-/workspace}}"
local eval_dir="${result_dir%/}/lm_eval"
local raw_dir="${EVAL_RESULT_DIR:-}"
Expand All @@ -328,7 +393,13 @@ magpie_run_eval_persisted() {
# EVAL_RESULT_DIR. Run from raw_dir so both single and batched evaluations
# have one source tree that can be copied without modifying the artifacts.
cd "$raw_dir" || return 1
run_eval "$@" || eval_rc=$?
if magpie_eval_needs_unsafe_code; then
python3() { magpie_python3_with_lm_eval_unsafe "$@"; }
Comment thread
haofrank marked this conversation as resolved.
Resolved
run_eval "$@" || eval_rc=$?
unset -f python3
else
run_eval "$@" || eval_rc=$?
fi
cd "$caller_dir" || return 1

if [[ -n "${EVAL_BATCHED_CONCS:-}" ]]; then
Expand Down
124 changes: 124 additions & 0 deletions tests/test_eval_concurrency_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,130 @@ def test_persisted_eval_collects_batched_concurrency_results(tmp_path: Path):
assert summary["task"] == "gsm8k"


def test_persisted_eval_forwards_comma_separated_tasks_to_inferencex(tmp_path: Path):
compat = (
ROOT
/ "Magpie"
/ "scripts"
/ "benchmark"
/ "magpie_bench_remote_compat.sh"
)
shell = r'''
source "$MAGPIE_COMPAT"
run_eval() {
printf '%s\n' "$EVAL_TASKS_DIR" > "$EVAL_RESULT_DIR/tasks.txt"
printf '%s\n' "$EVAL_LIMIT" > "$EVAL_RESULT_DIR/limit.txt"
printf '{"results":{"gsm8k":{"exact_match,strict-match":0.9},"mmlu":{"acc,none":0.8}}}\n' \
> "$EVAL_RESULT_DIR/results.json"
}
_write_lm_eval_meta_json() {
printf '{"model":"test","conc":%s}\n' "$3" > "$1"
}
magpie_run_eval_persisted --framework lm-eval --port 8888
'''
source_dir = tmp_path / "source"
source_dir.mkdir()
env = {
**os.environ,
"MAGPIE_COMPAT": str(compat),
"RESULT_DIR": str(tmp_path),
"EVAL_RESULT_DIR": str(source_dir),
"EVAL_TASKS_DIR": "utils/evals/gsm8k.yaml",
"MAGPIE_EVAL_TASKS": "gsm8k, mmlu, hellaswag",
"MAGPIE_EVAL_LIMIT": "100",
"EVAL_CONCURRENT_REQUESTS": "8",
}
subprocess.run(["bash", "-c", shell], check=True, env=env)
assert (source_dir / "tasks.txt").read_text().strip() == (
"gsm8k,mmlu,hellaswag"
)
assert (source_dir / "limit.txt").read_text().strip() == "100"
summary = json.loads((tmp_path / "accuracy_report.json").read_text())
assert summary["status"] == "COMPLETED"
assert "gsm8k" in summary["tasks"]
assert "mmlu" in summary["tasks"]


def test_persisted_eval_adds_unsafe_flag_for_humaneval(tmp_path: Path):
compat = (
ROOT
/ "Magpie"
/ "scripts"
/ "benchmark"
/ "magpie_bench_remote_compat.sh"
)
shell = r'''
source "$MAGPIE_COMPAT"
run_eval() {
if declare -F python3 >/dev/null; then
declare -f python3 > "$EVAL_RESULT_DIR/python3.fn"
fi
printf '%s\n' "$EVAL_TASKS_DIR" > "$EVAL_RESULT_DIR/tasks.txt"
printf '{"results":{"humaneval_instruct":{"pass@1":0.7}}}\n' \
> "$EVAL_RESULT_DIR/results.json"
}
_write_lm_eval_meta_json() {
printf '{"model":"test","conc":8}\n' > "$1"
}
magpie_run_eval_persisted --framework lm-eval --port 8888
'''
source_dir = tmp_path / "source"
source_dir.mkdir()
env = {
**os.environ,
"MAGPIE_COMPAT": str(compat),
"RESULT_DIR": str(tmp_path),
"EVAL_RESULT_DIR": str(source_dir),
"MAGPIE_EVAL_TASKS": "gsm8k,humaneval_instruct",
"EVAL_CONCURRENT_REQUESTS": "8",
}
subprocess.run(["bash", "-c", shell], check=True, env=env)
wrapper = (source_dir / "python3.fn").read_text()
assert "magpie_python3_with_lm_eval_unsafe" in wrapper
assert (source_dir / "tasks.txt").read_text().strip() == (
"gsm8k,humaneval_instruct"
)


def test_remote_eval_adds_unsafe_flag_for_humaneval(tmp_path: Path):
compat = (
ROOT
/ "Magpie"
/ "scripts"
/ "benchmark"
/ "magpie_bench_remote_compat.sh"
)
python_stub = tmp_path / "python3"
python_stub.write_text(
"#!/usr/bin/env python3\n"
"import os\n"
"import sys\n"
"with open(os.environ['LM_EVAL_ARGS_FILE'], 'w', encoding='utf-8') as handle:\n"
" handle.write(' '.join(sys.argv[1:]) + '\\n')\n",
encoding="utf-8",
)
python_stub.chmod(0o755)
shell = r'''
source "$MAGPIE_COMPAT"
magpie_write_accuracy_result() { return 0; }
magpie_run_eval_remote_direct
'''
env = {
**os.environ,
"MAGPIE_COMPAT": str(compat),
"RESULT_DIR": str(tmp_path),
"BENCHMARK_BASE_URL": "http://127.0.0.1:8888",
"MAGPIE_EVAL_PYTHON": str(python_stub),
"MAGPIE_EVAL_TASKS": "gsm8k,humaneval_instruct",
"LM_EVAL_ARGS_FILE": str(tmp_path / "lm_eval.args"),
"MODEL": "test-model",
}
subprocess.run(["bash", "-c", shell], check=True, env=env)
args = (tmp_path / "lm_eval.args").read_text()
assert "--confirm_run_unsafe_code" in args
assert "--tasks gsm8k,humaneval_instruct" in args


def test_remote_eval_propagates_accuracy_report_failure(tmp_path: Path):
compat = (
ROOT
Expand Down