Download code/tests/test_release_tools.py from luyu1021/turbot2va-ltx2: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/luyu1021/turbot2va-ltx2/resolve/main/code/tests/test_release_tools.py
- Command line
-
hf download hf://luyu1021/turbot2va-ltx2/code/tests/test_release_tools.py
-
curl -L -o test_release_tools.py https://huggingface.co/luyu1021/turbot2va-ltx2/resolve/main/code/tests/test_release_tools.py
3.71 kB
| """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()) | |
| 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() | |