File size: 5,048 Bytes
aa02c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from __future__ import annotations

from pathlib import Path

import numpy as np
import torch

from psi.data.seed.full_duplex_audio_command_dataset import (
    AudioSource,
    FullDuplexAudioCommandDataset,
    HiddenSlice,
)
from psi.models.full_duplex_psi0 import StreamingCausalMemory


def _minimal_source_dataset(monkeypatch) -> FullDuplexAudioCommandDataset:
    dataset = FullDuplexAudioCommandDataset.__new__(FullDuplexAudioCommandDataset)
    dataset.records = [
        {
            "row_id": 3,
            "filename": "test_motion",
            "instruction": "test",
            "audio_dir": "seed_audio/000003",
            "motion_dir": Path("/unused/test_motion"),
            "declared_frames": 20,
        }
    ]
    dataset.audio_catalog = {
        "seed_audio/000003": (
            AudioSource(
                "seed_audio/000003/voice",
                (HiddenSlice(0, Path("/unused/0"), 0, 1),),
            ),
        )
    }
    dataset.white_noise_hidden = (torch.full((2, 8), 9.0),)
    dataset.fixed_voice_per_record = True
    dataset.action_chunk_size = 30
    dataset.action_exec_horizon = 20
    dataset.prefix_frames = 10
    dataset.max_motion_steps = 0
    dataset.initial_idle_max_steps = 1
    dataset.stationary_blend_frames = 20
    motion = np.zeros((20, 36), dtype=np.float32)
    motion[:, 32] = 1.0
    monkeypatch.setattr(dataset, "_load_motion", lambda path: motion)
    monkeypatch.setattr(dataset, "_load_slice", lambda item: torch.ones(2, 8))
    monkeypatch.setattr(
        dataset,
        "_sample_stationary_anchor",
        lambda task_start_pose: np.asarray(task_start_pose, dtype=np.float32),
    )
    monkeypatch.setattr(
        dataset,
        "_sample_stationary_sequence",
        lambda phase, frame_count, anchor_pose: (
            np.repeat(
                np.asarray(anchor_pose, dtype=np.float32)[None, :],
                frame_count,
                axis=0,
            ),
            f"test-{phase}",
        ),
    )
    monkeypatch.setattr(
        dataset,
        "_build_task_sequence",
        lambda task_motion, stationary_anchor: (
            np.asarray(task_motion, dtype=np.float32),
            1,
        ),
    )
    monkeypatch.setattr("random.randint", lambda low, high: 1)
    return dataset


def test_idle_listen_and_motion_keep_qwen_cadence_with_dense_actions(monkeypatch):
    source = _minimal_source_dataset(monkeypatch)[0]

    phases = [step["phase"] for step in source["steps"]]
    assert phases.count("idle") == 20
    assert phases.count("listen") == 20
    assert phases.count("motion") == 9
    assert [
        index
        for index, step in enumerate(source["steps"])
        if step["update_semantic_cache"]
    ] == [0, 20, 40]
    assert all(
        step["action_substep"] == 0
        for step in source["steps"]
        if step["update_semantic_cache"]
    )
    assert all(
        not step["update_semantic_cache"]
        for step in source["steps"]
        if step["action_substep"] != 0
    )


def test_continuous_cache_caps_each_source_without_padding_shrinkage():
    memory = StreamingCausalMemory(
        input_dim=8,
        hidden_dim=16,
        num_layers=1,
        num_heads=4,
        dropout=0.0,
        max_cache_tokens=3,
    ).eval()
    cache = memory.begin_source(["long-source", "finished-source"])

    for _ in range(3):
        _, cache, _ = memory(
            torch.randn(2, 8),
            cache,
            source_ids=["long-source", "finished-source"],
            update_cache=[True, True],
        )
    assert cache.sequence_lengths == (3, 3)

    # False denotes only a padded row.  Continuing the other source must retain
    # the finished source's newest three real observations under the shared cap.
    for _ in range(4):
        _, cache, _ = memory(
            torch.randn(2, 8),
            cache,
            source_ids=["long-source", "finished-source"],
            update_cache=[True, False],
        )
        assert cache.sequence_lengths == (3, 3)
        assert cache.valid_mask is not None
        assert cache.valid_mask.sum(dim=-1).tolist() == [3, 3]


def test_all_padding_rows_leave_cache_objects_unchanged():
    memory = StreamingCausalMemory(
        input_dim=8,
        hidden_dim=16,
        num_layers=2,
        num_heads=4,
        dropout=0.0,
        max_cache_tokens=4,
    ).eval()
    cache = memory.begin_source(["source-a", "source-b"])
    _, cache, _ = memory(
        torch.randn(2, 8),
        cache,
        source_ids=["source-a", "source-b"],
        update_cache=[True, True],
    )
    before_keys = cache.keys
    before_values = cache.values

    _, padded_cache, _ = memory(
        torch.randn(2, 8),
        cache,
        source_ids=["source-a", "source-b"],
        update_cache=[False, False],
    )

    assert padded_cache.sequence_lengths == (1, 1)
    assert all(before is after for before, after in zip(before_keys, padded_cache.keys))
    assert all(before is after for before, after in zip(before_values, padded_cache.values))