|
| 1 | +# Copyright 2026 The ODML Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""CLI subprocess smoke test for the Lighteval framework integration. |
| 16 | +
|
| 17 | +Verifies that the `ai-edge-eval list-tasks --framework lighteval` entry point |
| 18 | +correctly loads and reports tasks from the internal allowlist, catching |
| 19 | +import-order regressions and CLI registration bugs in fresh interpreter |
| 20 | +instances. |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import shutil |
| 25 | +import subprocess |
| 26 | + |
| 27 | +from absl.testing import absltest |
| 28 | + |
| 29 | +try: |
| 30 | + from google3.third_party.bazel_rules.rules_python.python.runfiles import runfiles # pylint: disable=g-import-not-at-top |
| 31 | +except ImportError: |
| 32 | + runfiles = None |
| 33 | + |
| 34 | + |
| 35 | +def _resolve_cli_bin() -> str: |
| 36 | + """Find the ai-edge-eval entry point via Blaze runfiles or pip install.""" |
| 37 | + if runfiles: |
| 38 | + r = runfiles.Create() |
| 39 | + if r: |
| 40 | + found = r.Rlocation("google3/third_party/py/ai_edge_eval/cli") |
| 41 | + if found and os.path.exists(found): |
| 42 | + return found |
| 43 | + |
| 44 | + found = shutil.which("ai-edge-eval") |
| 45 | + if found and os.path.exists(found): |
| 46 | + return found |
| 47 | + |
| 48 | + raise absltest.SkipTest("ai-edge-eval CLI binary not found via Bazel or PATH") |
| 49 | + |
| 50 | + |
| 51 | +class CliLightevalSmokeTest(absltest.TestCase): |
| 52 | + |
| 53 | + def setUp(self): |
| 54 | + super().setUp() |
| 55 | + self.bin = _resolve_cli_bin() |
| 56 | + # Don't inherit a noisy parent env that could mask import errors. |
| 57 | + self.env = {**os.environ, "TRANSFORMERS_VERBOSITY": "error"} |
| 58 | + |
| 59 | + def _run(self, *args: str) -> subprocess.CompletedProcess[str]: |
| 60 | + """Executes the `ai-edge-eval` CLI binary with arguments in an isolated subprocess. |
| 61 | +
|
| 62 | + Captures stdout and stderr, enforces text-mode decoding, explicitly disables |
| 63 | + strict returncode checking (`check=False`), and applies a 60-second timeout. |
| 64 | +
|
| 65 | + Args: |
| 66 | + *args: Command-line arguments and flags to forward to the executable. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + A `subprocess.CompletedProcess[str]` instance representing the finished |
| 70 | + invocation. |
| 71 | + """ |
| 72 | + return subprocess.run( |
| 73 | + [self.bin, *args], |
| 74 | + capture_output=True, |
| 75 | + text=True, |
| 76 | + env=self.env, |
| 77 | + timeout=60, |
| 78 | + check=False, |
| 79 | + ) |
| 80 | + |
| 81 | + def test_list_tasks_lighteval(self): |
| 82 | + """list-tasks --framework lighteval returns the yaml allowlist and exits 0.""" |
| 83 | + p = self._run("list-tasks", "--framework", "lighteval") |
| 84 | + self.assertEqual(p.returncode, 0, msg=p.stderr) |
| 85 | + out = p.stdout |
| 86 | + # Known tasks from model_eval/config/tasks.yaml (lighteval section). |
| 87 | + for task in ("arc:easy", "arc:challenge", "mmlu", "winogrande", "ifeval"): |
| 88 | + self.assertIn( |
| 89 | + task, out, msg=f"task {task!r} missing from list-tasks output" |
| 90 | + ) |
| 91 | + |
| 92 | + def test_list_runners_lighteval(self): |
| 93 | + """list-runners --framework lighteval includes both runner paths and exits 0.""" |
| 94 | + p = self._run("list-runners", "--framework", "lighteval") |
| 95 | + self.assertEqual(p.returncode, 0, msg=p.stderr) |
| 96 | + out = p.stdout |
| 97 | + for runner in ("litert-lm", "accelerate"): |
| 98 | + self.assertIn( |
| 99 | + runner, out, msg=f"runner {runner!r} missing from list-runners output" |
| 100 | + ) |
| 101 | + |
| 102 | + def test_list_args_lighteval(self): |
| 103 | + """list-args --framework lighteval surfaces lighteval PipelineParameters fields.""" |
| 104 | + p = self._run("list-args", "--framework", "lighteval") |
| 105 | + self.assertEqual(p.returncode, 0, msg=p.stderr) |
| 106 | + out = p.stdout |
| 107 | + # 'max_samples' is the field the adapter maps --limit onto; if this is |
| 108 | + # missing, the introspection path is broken. |
| 109 | + self.assertIn("max_samples", out) |
| 110 | + |
| 111 | + def test_unknown_framework_is_handled(self): |
| 112 | + """An unknown --framework value exits non-zero with a clear error. |
| 113 | +
|
| 114 | + This guards against silent fall-through that could mask a real |
| 115 | + misregistration of the lighteval framework type. |
| 116 | + """ |
| 117 | + p = self._run("list-tasks", "--framework", "definitely-not-a-framework") |
| 118 | + self.assertNotEqual(p.returncode, 0) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + absltest.main() |
0 commit comments