File size: 3,712 Bytes
76b7b25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | """CPU checks for launch semantics and DCP export compatibility."""
import json
import os
from pathlib import Path
import subprocess
import sys
import pytest
import torch
import torch.distributed.checkpoint as dcp
import yaml
ROOT = Path(__file__).resolve().parents[1]
RECIPES = json.loads((ROOT/'configs/recipes.json').read_text())
@pytest.mark.parametrize('name', sorted(RECIPES))
def test_recipe_dry_run(name, tmp_path):
recipe = RECIPES[name]
output = tmp_path/'output'
args = [sys.executable, str(ROOT/'scripts/launch.py'), name, '--base','/base',
'--gemma','/gemma','--prompts','/prompts','--latents','/latents',
'--output',str(output),'--dry-run']
if recipe['parent_artifact']:
args += ['--resume' if recipe['parent_requires_full_resume'] else '--init','/parent']
result = subprocess.run(args,capture_output=True,text=True,check=True)
config = yaml.safe_load(result.stdout.split('Command:')[0])
assert config['output_path'] == str(tmp_path)
assert config['stop_file_path'] == str(output/'STOP_REQUESTED')
assert config['resume_training_state'] == recipe['parent_requires_full_resume']
assert config['require_full_resume_state'] == recipe['parent_requires_full_resume']
assert config['student_benchmark_use_cfg'] is False
assert not output.exists()
def test_missing_parent_is_rejected(tmp_path):
result = subprocess.run([sys.executable,str(ROOT/'scripts/launch.py'),'rcm_from_scm500',
'--base','/base','--gemma','/gemma','--prompts','/prompts','--latents','/latents',
'--output',str(tmp_path/'output'),'--dry-run'],capture_output=True,text=True)
assert result.returncode == 2
assert 'requires its parent' in result.stderr
def test_dcp_generator_export(tmp_path):
cp=tmp_path/'checkpoint'
cp.mkdir()
generator={'model.weight':torch.arange(12,dtype=torch.bfloat16).reshape(3,4),
'model.bias':torch.arange(3,dtype=torch.float32)}
dcp.save({'generator':generator,'critic':{'model.weight':torch.ones(3,4)}},
checkpoint_id=str(cp/'sharded'))
torch.save({'completed_step':7,'next_step':8},cp/'metadata.pth')
output=tmp_path/'export.pth'
subprocess.run([sys.executable,str(ROOT/'scripts/export_generator.py'),str(cp),str(output)],check=True)
state=torch.load(output,map_location='cpu',weights_only=True)
assert set(state['generator']) == set(generator)
for key,value in generator.items():
assert torch.equal(state['generator'][key],value)
assert 'critic' not in state and not state['has_generator_optimizer']
assert state['completed_step'] == 7 and state['next_step'] == 8
def test_launcher_uses_exact_output_directory(tmp_path, monkeypatch):
import importlib.util
spec=importlib.util.spec_from_file_location('release_launch',ROOT/'scripts/launch.py')
module=importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for name in ['base','gemma','prompts','latents']:
(tmp_path/name).touch()
output=tmp_path/'exact_run'
monkeypatch.setattr(sys,'argv',['launch.py','dcm_beta0','--base',str(tmp_path/'base'),
'--gemma',str(tmp_path/'gemma'),'--prompts',str(tmp_path/'prompts'),
'--latents',str(tmp_path/'latents'),'--output',str(output)])
observed={}
def fake_call(cmd, cwd, env):
observed.update(cmd=cmd,cwd=cwd,env=env)
return 0
monkeypatch.setattr(module.subprocess,'call',fake_call)
with pytest.raises(SystemExit) as raised:
module.main()
assert raised.value.code == 0
assert observed['env']['LTX_RUN_DIR_NAME'] == 'exact_run'
assert output.joinpath('launch_config.yaml').exists()
|