Repository navigation
verify: run on a laptop without OVL_DEVICE; clearer help and errors #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryoari
wants to merge
2
commits into
main
Choose a base branch
from
ux/verify-on-a-laptop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−23
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """The command line on a computer without a GPU: what runs, what stops early, and the messages.""" | ||
| import argparse | ||
| import os | ||
| from pathlib import Path | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| os.environ["OVL_DEVICE"] = "cpu" | ||
| os.environ["OVL_TIMESTAMP"] = "0" | ||
| ROOT = Path(__file__).resolve().parents[2] | ||
| sys.path.insert(0, str(ROOT / "simple")) | ||
| import ovl # noqa: E402 | ||
|
|
||
|
|
||
| def verify_args(**kw): | ||
| base = dict(run=None, data=None, segment=None, random=0, all=False, weights=None, anchor=None, | ||
| no_anchor=True, no_timestamps=False) | ||
| return argparse.Namespace(**{**base, **kw}) | ||
|
|
||
|
|
||
| class RequireGpuTests(unittest.TestCase): | ||
| def test_stops_on_cpu_when_no_device_was_chosen(self): | ||
| with mock.patch.object(ovl, "DEV", "cpu"), mock.patch.dict(os.environ, clear=False) as env: | ||
| env.pop("OVL_DEVICE", None) | ||
| with self.assertRaisesRegex(SystemExit, "Training needs an NVIDIA GPU.*OVL_DEVICE=cpu"): | ||
| ovl.require_gpu("Training") | ||
|
|
||
| def test_an_empty_device_setting_is_not_a_cpu_choice(self): | ||
| with mock.patch.object(ovl, "DEV", "cpu"), mock.patch.dict(os.environ, {"OVL_DEVICE": ""}): | ||
| with self.assertRaisesRegex(SystemExit, "Training needs an NVIDIA GPU"): | ||
| ovl.require_gpu("Training") | ||
|
|
||
| def test_runs_on_cpu_when_asked_explicitly(self): | ||
| with mock.patch.object(ovl, "DEV", "cpu"), mock.patch.dict(os.environ, {"OVL_DEVICE": "cpu"}): | ||
| ovl.require_gpu("Training") | ||
|
|
||
| def test_runs_on_a_gpu(self): | ||
| with mock.patch.object(ovl, "DEV", "cuda"), mock.patch.dict(os.environ, clear=False) as env: | ||
| env.pop("OVL_DEVICE", None) | ||
| ovl.require_gpu("Training") | ||
|
|
||
| def test_segment_check_without_a_gpu_stops_before_reading_anything(self): | ||
| with mock.patch.object(ovl, "DEV", "cpu"), mock.patch.dict(os.environ, clear=False) as env: | ||
| env.pop("OVL_DEVICE", None) | ||
| with self.assertRaisesRegex(SystemExit, "Recomputing a segment needs an NVIDIA GPU"): | ||
| ovl.cmd_verify(verify_args(run="/nonexistent", data="/nonexistent", random=1)) | ||
|
|
||
|
|
||
| class DeviceDefaultTests(unittest.TestCase): | ||
| def test_falls_back_to_cpu_without_a_gpu(self): | ||
| env = {k: v for k, v in os.environ.items() if k != "OVL_DEVICE"} | ||
| code = "import sys; sys.path.insert(0, 'simple'); import torch; torch.cuda.is_available = lambda: False; import ovl; print(ovl.DEV)" | ||
| out = subprocess.run([sys.executable, "-c", code], cwd=ROOT, env=env, capture_output=True, text=True, check=True) | ||
| self.assertEqual(out.stdout.strip(), "cpu") | ||
|
|
||
|
|
||
| class MissingInputTests(unittest.TestCase): | ||
| def setUp(self): | ||
| self.tmp = tempfile.TemporaryDirectory() | ||
| self.addCleanup(self.tmp.cleanup) | ||
| self.run_dir = Path(self.tmp.name) | ||
| (self.run_dir / "chain.jsonl").write_text('{"index": 0, "kind": "initial", "state": "s", "model": "m", "previous": null}\n') | ||
|
|
||
| def test_missing_weights_file_is_reported(self): | ||
| with self.assertRaisesRegex(SystemExit, "weights file .*nope.pt not found"): | ||
| ovl.cmd_verify(verify_args(run=str(self.run_dir), weights=str(self.run_dir / "nope.pt"))) | ||
|
|
||
| def test_data_directory_without_streams_is_reported(self): | ||
| with self.assertRaisesRegex(SystemExit, "is not a prepared data directory"): | ||
| ovl.cmd_verify(verify_args(run=str(self.run_dir), data=str(self.run_dir))) | ||
|
|
||
| def test_data_directory_without_conversation_stream_is_reported(self): | ||
| (self.run_dir / "wikipedia").mkdir() | ||
| (self.run_dir / "wikipedia" / "stream.json").write_text("{}") | ||
| with self.assertRaisesRegex(SystemExit, "conversation/stream.json not found"): | ||
| ovl.cmd_verify(verify_args(run=str(self.run_dir), data=str(self.run_dir))) | ||
|
|
||
| def test_data_directory_is_checked_before_the_anchor(self): | ||
| # An anchor that would fail (and, with timestamps on, go online) must not be reached first. | ||
| (self.run_dir / "anchor.json").write_text("not json") | ||
| with self.assertRaisesRegex(SystemExit, "is not a prepared data directory"): | ||
| ovl.cmd_verify(verify_args(run=str(self.run_dir), data=str(self.run_dir), no_anchor=False)) | ||
|
|
||
|
|
||
| class HelpTests(unittest.TestCase): | ||
| def test_verify_options_are_described(self): | ||
| out = subprocess.run([sys.executable, str(ROOT / "simple" / "ovl.py"), "verify", "--help"], | ||
| capture_output=True, text=True, check=True).stdout | ||
| for words in ("the run directory", "recompute every segment", "this model file"): | ||
| self.assertIn(words, out) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add passing and tampered data cases.
These tests only check missing manifests. Add an offline CPU test that verifies matching manifests, then modifies one manifest and verifies failure.
As per path instructions, “Verification tests need both a passing and a tampered (failing) case.”
Source: Path instructions