from types import SimpleNamespace import numpy as np import pytest import torch from torch import nn from psi.runtime.swa_rollout import ReplyDebounce, body_metrics, inplace_future_progress from psi.models.full_duplex_psi0 import QwenStreamingKVCache from psi.models.swa_qa_branch import BranchLoRALinear def test_inplace_phase_has_no_extra_transition_and_is_dense_stride_equivalent(): rows = inplace_future_progress(0, 686) assert rows[0, 0] == pytest.approx(1 / 685) # Dense training and deployment ask for the same future frames. np.testing.assert_array_equal(inplace_future_progress(20, 686)[:10], rows[20:]) np.testing.assert_allclose(rows[:, 2], rows[:, 0]) np.testing.assert_array_equal(inplace_future_progress(1000, 686)[:, 0], 1) def test_debounce_reuses_verified_two_window_rule(): trigger = ReplyDebounce() assert [trigger.update(p) for p in [.8, .2, .8, .9, .9, .1, .9, .9]] == [ False, False, False, True, False, False, False, True] def test_motion_scores_do_not_reward_freezing_or_hide_seams(): gt = np.zeros((60, 36)) gt[:, 32] = 1 gt[:, 0] = np.sin(np.arange(60) / 10) assert all(v == pytest.approx(0) for v in body_metrics(gt, gt).values()) frozen = gt.copy() frozen[:, 0] = 0 metrics = body_metrics(frozen, gt) assert metrics["joint_rmse_rad"] > 0 assert metrics["joint_acceleration_error_rad_s2"] > 0 jumped = gt.copy() jumped[20:, 0] += 1 assert body_metrics(jumped, gt)["joint_velocity_error_rad_s"] > 0 flipped = gt.copy() flipped[:, 32:36] *= -1 assert body_metrics(flipped, gt)["root_rotation_deg"] == pytest.approx(0) def test_private_cache_detaches_and_resets_only_selected_rows(): from transformers.cache_utils import DynamicCache key = torch.ones(2, 1, 3, 2, requires_grad=True) private = QwenStreamingKVCache( source_ids=("a", "b"), past_key_values=DynamicCache(ddp_cache_data=[(key * 2, key * 3)]), attention_mask=torch.ones(2, 3, dtype=torch.long), token_count=3, logical_window_counts=(1, 1), av_next_positions=(4, 5), ) main = QwenStreamingKVCache.empty(["a", "b"]) main.qa_cache = private main.detach() assert not private.past_key_values.layers[0].keys.requires_grad main.rebind_rows(["c", "b"], [True, False]) assert private.source_ids == ("c", "b") assert private.av_next_positions == (0, 5) assert private.attention_mask.tolist() == [[0, 0, 0], [1, 1, 1]] def test_new_adapter_starts_at_base_and_trains_without_thawing_base(): base = nn.Linear(3, 4, bias=False) x = torch.randn(2, 3) model = BranchLoRALinear(base) torch.testing.assert_close(model(x), base(x)) model(x).square().sum().backward() assert base.weight.grad is None assert model.up.weight.grad.abs().sum() > 0 down, up = torch.randn(16, 3), torch.randn(4, 16) with torch.no_grad(): model.down.weight.copy_(down) model.up.weight.copy_(up) torch.testing.assert_close(model(x), base(x) + (x @ down.T) @ up.T) def test_qa_loss_modifies_only_private_branch(): from psi.models.full_duplex_swa import FullDuplexSwaModel model = FullDuplexSwaModel.__new__(FullDuplexSwaModel) nn.Module.__init__(model) model.reply_head = nn.Linear(2, 2) class PrivateQa(nn.Linear): def qa_loss(self, cache, targets): assert cache == "private" return self(torch.ones(1, 2)).square().sum(), torch.tensor(0), torch.tensor(2) model.qa_model = PrivateQa(2, 2) nn.Module.train(model) loss, correct, count = model.qa_loss(SimpleNamespace(qa_cache="private"), ["answer"]) loss.backward() assert count == 2 and correct == 0 assert model.reply_head.weight.grad is None assert model.qa_model.weight.grad.abs().sum() > 0 def test_independent_reply_readout_does_not_update_body_summary(): """SWA readiness supervision must not perturb the action/Qwen body.""" from psi.models.full_duplex_swa import FullDuplexSwaModel model = FullDuplexSwaModel.__new__(FullDuplexSwaModel) nn.Module.__init__(model) model.reply_head = nn.Linear(3, 2) model.swa_reply_detach_body = True summary = torch.randn(2, 3, requires_grad=True) logits = model.reply_head(model._reply_summary_for_loss(summary)) torch.nn.functional.cross_entropy(logits, torch.tensor([0, 1])).backward() assert summary.grad is None assert model.reply_head.weight.grad is not None def test_legacy_reply_readout_keeps_legacy_gradient_path(): """The compatibility/shared branch is not silently changed.""" from psi.models.full_duplex_swa import FullDuplexSwaModel model = FullDuplexSwaModel.__new__(FullDuplexSwaModel) nn.Module.__init__(model) model.reply_head = nn.Linear(3, 2) model.swa_reply_detach_body = False summary = torch.randn(2, 3, requires_grad=True) torch.nn.functional.cross_entropy( model.reply_head(model._reply_summary_for_loss(summary)), torch.tensor([0, 1]), ).backward() assert summary.grad is not None def test_new_contract_is_explicit_and_legacy_contract_unchanged(): from psi.models.relative_instruction_memory import architecture_contract cfg = SimpleNamespace() assert architecture_contract(cfg) == {} cfg.swa_visual_qa_enabled = True cfg.swa_qa_mode = "independent_online_lora" contract = architecture_contract(cfg)["swa_qa_online_v1"] assert contract["initialization"] == "original_pretrained_qwen_zero_lora" assert contract["private_online_kv"] assert not contract["body_receives_qa_gradient"] def test_qa_clock_skips_silent_windows_and_survives_resume(): from copy import deepcopy from psi.trainers.swa_optimizer import prepare_qa_update, split_gradient_parameters, clip_qa_gradients body, qa = nn.Parameter(torch.ones(2)), nn.Parameter(torch.ones(2)) opt = torch.optim.AdamW([ dict(params=[body]), dict(params=[qa], group_name="swa_qa", qa_updates=0, qa_base_lr=5e-5, qa_warmup_updates=40), ]) b, q = split_gradient_parameters(opt) assert b == [body] and q == [qa] qa.grad = torch.ones(2) prepare_qa_update(opt, has_targets=True) clip_qa_gradients(q) opt.step() saved, weight = deepcopy(opt.state_dict()), qa.detach().clone() # Simulate a reducer manufacturing zero gradients on silent windows. for _ in range(19): qa.grad = torch.zeros_like(qa) prepare_qa_update(opt, has_targets=False) opt.step() torch.testing.assert_close(qa, weight, rtol=0, atol=0) assert opt.state[qa]["step"] == 1 opt.load_state_dict(saved) qa.grad = torch.ones(2) body.grad = torch.ones(2) prepare_qa_update(opt, has_targets=True) clip_qa_gradients(q) torch.testing.assert_close(body.grad, torch.ones(2)) assert opt.param_groups[1]["qa_updates"] == 2 assert opt.param_groups[1]["lr"] == pytest.approx(5e-5 * 2 / 40) def test_compact_checkpoint_keeps_frozen_qa_adapters_not_its_base(): from psi.models.swa_qa_branch import OnlineQaBranch from psi.trainers.posttrain_full_duplex import PosttrainFullDuplexTrainer model = nn.Module() model.control_head = nn.Linear(2, 2) model.vlm_model = nn.Linear(2, 2) private = OnlineQaBranch.__new__(OnlineQaBranch) nn.Module.__init__(private) private.vlm_model = nn.Module() private.vlm_model.projection = BranchLoRALinear(nn.Linear(2, 2)) model.qa_model = private model.requires_grad_(False) keys = PosttrainFullDuplexTrainer._hfdv2_delta_keys(model) assert {k for k in keys if k.startswith("qa_model.")} == { "qa_model.vlm_model.projection.down.weight", "qa_model.vlm_model.projection.up.weight"} assert {"control_head.weight", "control_head.bias"} <= keys