|
| 1 | +"""External interruptions are retried without accepting contaminated timings.""" |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +import tempfile |
| 5 | +from types import SimpleNamespace |
| 6 | +import unittest |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +from tools.gpu_job_retry import run_gpu_job |
| 10 | + |
| 11 | + |
| 12 | +class GpuJobRetryTests(unittest.TestCase): |
| 13 | + def test_competing_job_is_discarded_hashed_and_repeated_with_same_command(self): |
| 14 | + with tempfile.TemporaryDirectory() as temp: |
| 15 | + output=Path(temp)/'job.json'; log=Path(temp)/'job.log'; command=['worker','--requests','32'] |
| 16 | + env={'CUDA_VISIBLE_DEVICES':'7'}; seen=[] |
| 17 | + def run(cmd, env, stdout, stderr): |
| 18 | + seen.append((cmd,dict(env))) |
| 19 | + if len(seen)==1: |
| 20 | + output.write_text(json.dumps({'status':'failed','error':'GPU competition appeared during serving','samples':['contaminated']})) |
| 21 | + return SimpleNamespace(returncode=1) |
| 22 | + output.write_text(json.dumps({'status':'passed','samples':['fresh']})); return SimpleNamespace(returncode=0) |
| 23 | + with patch('tools.gpu_job_retry.subprocess.run',side_effect=run), patch('tools.gpu_job_retry.wait_idle') as idle: |
| 24 | + self.assertEqual(run_gpu_job(command,env=env,output=output,log=log).returncode,0) |
| 25 | + idle.assert_called_once() |
| 26 | + self.assertEqual(seen,[(command,env)]*2) |
| 27 | + self.assertEqual(json.loads(output.read_text())['samples'],['fresh']) |
| 28 | + retained=list(Path(temp).glob('job.interrupted-*.json')) |
| 29 | + self.assertEqual(len(retained),1) |
| 30 | + self.assertEqual(json.loads(retained[0].read_text())['samples'],['contaminated']) |
| 31 | + event=json.loads((Path(temp)/'job.interruptions.jsonl').read_text()) |
| 32 | + self.assertEqual(event['output'],str(retained[0]));self.assertEqual(len(event['sha256']),64) |
| 33 | + |
| 34 | + def test_algorithm_failure_does_not_turn_into_an_automatic_retry(self): |
| 35 | + with tempfile.TemporaryDirectory() as temp: |
| 36 | + output=Path(temp)/'job.json';log=Path(temp)/'job.log' |
| 37 | + def run(*args,**kwargs): |
| 38 | + output.write_text(json.dumps({'status':'failed','error':'decoded row bytes changed'})) |
| 39 | + return SimpleNamespace(returncode=1) |
| 40 | + with patch('tools.gpu_job_retry.subprocess.run',side_effect=run) as runner, patch('tools.gpu_job_retry.wait_idle') as idle: |
| 41 | + self.assertEqual(run_gpu_job(['worker'],env={'CUDA_VISIBLE_DEVICES':'0'},output=output,log=log).returncode,1) |
| 42 | + runner.assert_called_once();idle.assert_not_called() |
| 43 | + with self.assertRaisesRegex(ValueError,'needs diagnosis'): |
| 44 | + run_gpu_job(['worker'],env={'CUDA_VISIBLE_DEVICES':'0'},output=output,log=log) |
| 45 | + self.assertEqual(json.loads(output.read_text())['error'],'decoded row bytes changed') |
| 46 | + |
| 47 | + def test_a_passed_output_is_never_overwritten_on_resume(self): |
| 48 | + with tempfile.TemporaryDirectory() as temp: |
| 49 | + output=Path(temp)/'job.json';output.write_text('{"status":"passed","samples":[1,2,3]}') |
| 50 | + original=output.read_bytes() |
| 51 | + with patch('tools.gpu_job_retry.subprocess.run') as runner: |
| 52 | + self.assertEqual(run_gpu_job(['worker'],env={},output=output,log=Path(temp)/'log').returncode,0) |
| 53 | + runner.assert_not_called() |
| 54 | + self.assertEqual(output.read_bytes(),original) |
| 55 | + |
| 56 | + def test_resuming_an_interrupted_output_preserves_it(self): |
| 57 | + with tempfile.TemporaryDirectory() as temp: |
| 58 | + output=Path(temp)/'job.json';log=Path(temp)/'job.log' |
| 59 | + output.write_text('{"status":"failed","error":"GPU competition before model loading"}') |
| 60 | + def run(*args,**kwargs): |
| 61 | + self.assertFalse(output.exists()) |
| 62 | + output.write_text('{"status":"passed"}');return SimpleNamespace(returncode=0) |
| 63 | + with patch('tools.gpu_job_retry.subprocess.run',side_effect=run): |
| 64 | + self.assertEqual(run_gpu_job(['worker'],env={},output=output,log=log).returncode,0) |
| 65 | + self.assertEqual(len(list(Path(temp).glob('job.interrupted-*.json'))),1) |
| 66 | + |
| 67 | + |
| 68 | +if __name__=='__main__':unittest.main() |
0 commit comments