"""Fixed new-run precision, with explicit historical checkpoint compatibility.""" from types import SimpleNamespace import pytest import torch from psi.models.psi0 import ActionProjectionIn, Psi0Model from psi.models.relative_instruction_memory import architecture_contract def projection(): torch.manual_seed(17) return ActionProjectionIn(30, 39, 64) def test_default_matches_legacy_under_autocast_exactly(): module = projection() x = torch.randn(2, 30, 39) with torch.autocast('cpu', dtype=torch.bfloat16): expected = module.ac_proj(x) + module.dec_pos.unsqueeze(0) actual = module(x) torch.testing.assert_close(actual, expected, rtol=0, atol=0) def test_fp32_input_matches_fp32_reference_and_backward(): module = projection() original_keys = tuple(module.state_dict()) x = torch.randn(2, 20, 39, requires_grad=True) expected = module(x, position_offset=10) module.force_fp32 = True with torch.autocast('cpu', dtype=torch.bfloat16): actual = module(x, position_offset=10) torch.testing.assert_close(actual, expected, rtol=0, atol=0) actual.square().mean().backward() assert x.grad is not None and torch.isfinite(x.grad).all() assert all(p.grad is not None and torch.isfinite(p.grad).all() for p in module.parameters()) assert tuple(module.state_dict()) == original_keys def test_rejects_downcast_master_weights_and_invalid_rtc_positions(): module = projection() module.force_fp32 = True with pytest.raises(ValueError, match='positions'): module(torch.randn(1, 20, 39), position_offset=11) module.bfloat16() with pytest.raises(RuntimeError, match='master weights'): module(torch.randn(1, 30, 39)) def test_precision_policy_is_fixed_in_recipe_and_versioned(): from psi.config.model_full_duplex_psi0 import FullDuplexPsi0ModelConfig from psi.config.hfd_current_recipe import FIELDS assert FullDuplexPsi0ModelConfig.model_fields['action_input_projection_fp32'].default is False assert FIELDS['model.action-input-projection-fp32'] == (True, None) assert architecture_contract(SimpleNamespace()) == {} assert architecture_contract(SimpleNamespace(action_input_projection_fp32=True)) == { 'action_input_projection_precision_v1': 'fp32_input_mlp'} def test_launcher_default_is_saved_in_model_config(tmp_path, monkeypatch): import tyro from pathlib import Path from psi.hfd_training_launcher import resolve, to_args from psi.config.train.posttrain_full_duplex_audio_command_psi0_config import DynamicLaunchConfig root = Path(__file__).resolve().parents[1] options, _ = resolve(root, {}) config = tyro.cli(DynamicLaunchConfig, config=(tyro.conf.ConsolidateSubcommandArgs,), args=to_args(options)[1:]) assert config.model.action_input_projection_fp32 loaded = DynamicLaunchConfig.model_validate_json(config.model_dump_json()) assert loaded.model.action_input_projection_fp32 # Actual unified inference configuration loader, not a hand-picked flag. from scripts.infer.unified_overlay import cfg import sys monkeypatch.setattr(sys, 'argv', list(sys.argv)) (tmp_path/'run_config.json').write_text(config.model_dump_json()) (tmp_path/'argv.txt').write_text('\n'.join(['scripts/train.py', *to_args(options)])+'\n') assert cfg(tmp_path).model.action_input_projection_fp32 def test_new_run_cannot_disable_precision(): from pathlib import Path from psi.hfd_training_launcher import resolve root = Path(__file__).resolve().parents[1] with pytest.raises(ValueError, match='fixed in production'): resolve(root, {'HFD_ACTION_INPUT_FP32': '0'}) with pytest.raises(ValueError, match='requires FP32'): resolve(root, {}, ['--model.no-action-input-projection-fp32']) def test_both_real_shell_entries_use_fixed_fp32(): import json, os, subprocess from pathlib import Path root = Path(__file__).resolve().parents[1] env = {k: v for k, v in os.environ.items() if not k.startswith('HFD_')} env.update(HFD_ENV_FILE='/nonexistent', HFD_CONFIG_ONLY='1', HFD_TRAIN_BATCH_SIZE='8', HFD_RUN_TIMESTAMP='test-no-run-created') for entry in ('posttrain-full-duplex-audio-command.sh', 'train_dlc.sh'): output = subprocess.check_output(['bash', str(root/entry), '--print-config'], cwd=root, env=env, text=True, timeout=60) config = json.loads(output) assert config['model.action-input-projection-fp32'] is True assert config['model.interaction-architecture'] == 'a1_c_deferred' assert config['model.action-representation'] == 'root40_v1' assert config['model.action-dim'] == config['model.odim'] == 40 assert config['data.transform.repack.root40-actions'] is True def test_historical_resume_does_not_silently_change_precision(tmp_path): import json from pathlib import Path from psi.hfd_training_launcher import CONFIG_MODULE, resolve, to_args root = Path(__file__).resolve().parents[1] saved, _ = resolve(root, {}) saved.pop('model.action-input-projection-fp32') saved.pop('model.action-representation') saved.pop('data.transform.repack.root40-actions') for key in ('model.action-dim', 'model.odim', 'data.transform.repack.pad-action-dim', 'data.transform.repack.pad-state-dim', 'data.transform.field.pad-action-dim', 'data.transform.field.pad-state-dim'): saved[key] = 39 checkpoint = tmp_path/'checkpoints'/'ckpt_10' checkpoint.mkdir(parents=True) (tmp_path/'run_config.json').write_text(json.dumps({ 'train': {'stage': 0, 'full_duplex_semantics_version': 24}})) (tmp_path/'argv.txt').write_text('\n'.join(to_args(saved))+'\n') resumed, _ = resolve(root, {'HFD_RESUME_FROM_CHECKPOINT': str(checkpoint), 'HFD_RESUME_LOAD_FULL_STATE': '1'}) assert 'model.action-input-projection-fp32' not in resumed def test_constructor_and_checkpoint_preserve_policy_without_new_parameters(): config = SimpleNamespace(action_chunk_size=30, action_dim=39, action_exec_horizon=20, max_delay=11, rtc=True, use_dit=False, nhead=2, hidden_dim=16, num_blocks=2, resnet_store_path='', odim=39, view_feature_dim=8, use_film=False, combined_temb=False, action_input_projection_fp32=True) enabled = Psi0Model(config, None) assert enabled.action_header.action_proj_in.force_fp32 config.action_input_projection_fp32 = False legacy = Psi0Model(config, None) assert not legacy.action_header.action_proj_in.force_fp32 assert list(enabled.state_dict()) == list(legacy.state_dict()) enabled.load_state_dict(legacy.state_dict(), strict=True) assert enabled.action_header.action_proj_in.force_fp32