File size: 7,826 Bytes
5d9f970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from __future__ import annotations

import csv
import json
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace

import numpy as np

from psi.data.seed.motion_frame_index import read_motion_frame_count, valid_sample_count
from psi.data.seed.seed_audio_motion_dataset import SeedAudioMotionDataset
from psi.data.seed.seed_motion_dataset import SeedMotionDataset
from psi.config.data_seed_text_audio_video_mix import SeedTextAudioVideoMixDataConfig


def _write_csv(path: Path, columns: int, frames: int) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", newline="", encoding="utf-8") as handle:
        writer = csv.writer(handle)
        writer.writerow([f"c{column}" for column in range(columns)])
        for frame in range(frames):
            writer.writerow([float(frame + column) for column in range(columns)])


def _write_motion(path: Path, frames: int) -> None:
    _write_csv(path / "joint_pos.csv", 29, frames)
    _write_csv(path / "body_pos.csv", 3, frames)
    _write_csv(path / "body_quat.csv", 4, frames)


class MotionFrameIndexTest(unittest.TestCase):
    def test_real_motion_rows_override_text_metadata(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            motion_dir = root / "g1" / "csv_sonic" / "example"
            _write_motion(motion_dir, frames=4)
            metadata_path = root / "metadata.csv"
            with metadata_path.open("w", newline="", encoding="utf-8") as handle:
                writer = csv.DictWriter(
                    handle,
                    fieldnames=["move_g1_path", "move_name", "move_duration_frames", "prompt"],
                )
                writer.writeheader()
                writer.writerow(
                    {
                        "move_g1_path": "g1/csv/example.csv",
                        "move_name": "example",
                        "move_duration_frames": 10,
                        "prompt": "move",
                    }
                )

            dataset = SeedMotionDataset(
                root_dir=str(root),
                metadata_csv=str(metadata_path),
                split="all",
                action_chunk_size=2,
                use_delta_actions=False,
                text_fields=["prompt"],
                records_cache_dir=str(root / "cache"),
            )

            self.assertEqual(dataset.motion_records[0]["n_frames"], 4)
            self.assertEqual(dataset.motion_records[0]["declared_n_frames"], 10)
            # Three starts (0, 1, 2) have a real next-frame target; start 3 is
            # intentionally absent instead of becoming an all-zero-loss item.
            self.assertEqual(len(dataset), 3)
            self.assertGreater(float(dataset[2]["action_mask"].sum()), 0.0)

            rtc_dataset = SeedMotionDataset(
                root_dir=str(root),
                metadata_csv=str(metadata_path),
                split="all",
                action_chunk_size=2,
                use_delta_actions=False,
                supervision_prefix_frames=1,
                text_fields=["prompt"],
                records_cache_dir=str(root / "cache"),
            )
            self.assertEqual(len(rtc_dataset), 2)
            self.assertGreater(float(rtc_dataset[1]["action_mask"][1:].sum()), 0.0)

    def test_real_motion_rows_override_audio_manifest(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            motion_dir = root / "motion"
            _write_motion(motion_dir, frames=4)
            np.save(root / "audio.npy", np.zeros(160, dtype=np.float32))
            manifest_path = root / "manifest.jsonl"
            manifest_path.write_text(
                json.dumps(
                    {
                        "id": "example",
                        "converted_path": "motion",
                        "audio_path": "audio.npy",
                        "n_frames": 10,
                    }
                )
                + "\n",
                encoding="utf-8",
            )

            dataset = SeedAudioMotionDataset(
                dataset_root=str(root),
                metadata_jsonl="manifest.jsonl",
                split="all",
                action_chunk_size=2,
                use_delta_actions=False,
                full_audio=True,
                audio_path_key="audio_path",
                source_name="test_audio",
                records_cache_dir=str(root / "cache"),
            )

            self.assertEqual(dataset.motion_records[0]["n_frames"], 4)
            self.assertEqual(dataset.motion_records[0]["declared_n_frames"], 10)
            self.assertEqual(len(dataset), 3)
            self.assertGreater(float(dataset[2]["action_mask"].sum()), 0.0)

            rtc_dataset = SeedAudioMotionDataset(
                dataset_root=str(root),
                metadata_jsonl="manifest.jsonl",
                split="all",
                action_chunk_size=2,
                use_delta_actions=False,
                supervision_prefix_frames=1,
                full_audio=True,
                audio_path_key="audio_path",
                source_name="test_audio",
                records_cache_dir=str(root / "cache"),
            )
            self.assertEqual(len(rtc_dataset), 2)
            self.assertGreater(float(rtc_dataset[1]["action_mask"][1:].sum()), 0.0)

    def test_motion_csv_lengths_must_agree(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            motion_dir = Path(tmp)
            _write_motion(motion_dir, frames=4)
            _write_csv(motion_dir / "body_pos.csv", 3, frames=3)
            with self.assertRaisesRegex(ValueError, "Frame length mismatch"):
                read_motion_frame_count(motion_dir)

    def test_non_delta_index_never_adds_zero_target_start(self) -> None:
        self.assertEqual(
            valid_sample_count(
                4,
                index_step_frames=1,
                action_step_frames=1,
                use_delta_actions=False,
            ),
            3,
        )
        self.assertEqual(
            valid_sample_count(
                4,
                index_step_frames=1,
                action_step_frames=1,
                use_delta_actions=True,
            ),
            3,
        )

    def test_rtc_prefix_requires_a_real_postfix_target(self) -> None:
        # With a ten-token condition prefix, the first supervised token targets
        # start+11.  Four starts (0..3) are valid in a 15-frame motion.
        for use_delta_actions in (False, True):
            self.assertEqual(
                valid_sample_count(
                    15,
                    index_step_frames=1,
                    action_step_frames=1,
                    use_delta_actions=use_delta_actions,
                    supervision_prefix_frames=10,
                ),
                4,
            )
        with self.assertRaisesRegex(ValueError, "non-negative"):
            valid_sample_count(
                15,
                index_step_frames=1,
                action_step_frames=1,
                use_delta_actions=False,
                supervision_prefix_frames=-1,
            )

    def test_data_mix_reads_rtc_prefix_from_repack_config(self) -> None:
        config = SimpleNamespace(
            transform=SimpleNamespace(
                repack=SimpleNamespace(
                    action_chunk_size=30,
                    use_delta_actions=False,
                    num_past_frames=0,
                    rtc_prefix_frames=10,
                )
            )
        )
        self.assertEqual(
            SeedTextAudioVideoMixDataConfig._repack_args(config),
            (30, False, 0, 10),
        )


if __name__ == "__main__":
    unittest.main()