code-HFD_lzk / tests /test_full_duplex_continuous_cache.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
db172f6 verified
Raw History Blame Contribute Delete
5.05 kB
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))