File size: 13,825 Bytes
a550c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""
Step (2) of evaluation pipeline.

This script recursively generates motions using Kimodo from a test suite folder tree.
"""

import argparse
import shutil
from pathlib import Path
from typing import Any

import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset
from tqdm.auto import tqdm

from kimodo.constraints import load_constraints_lst
from kimodo.meta import parse_prompts_from_meta
from kimodo.model import DEFAULT_MODEL, load_model
from kimodo.tools import load_json, seed_everything


def parse_args():
    parser = argparse.ArgumentParser(description="Recursively generate motions from a testsuite folder tree")
    parser.add_argument(
        "--benchmark",
        type=str,
        default="testsuite",
        help="Root folder containing subfolders with meta.json (default: testsuite)",
    )
    parser.add_argument(
        "--output",
        type=str,
        default=None,
        help="Output root; directory hierarchy is mirrored here. If omitted, motions are generated in-place inside the testsuite folder.",
    )
    parser.add_argument(
        "--batch_size",
        type=int,
        default=32,
        help="Batch size for generating motions (default: 32)",
    )
    parser.add_argument(
        "--num_workers",
        type=int,
        default=4,
        help="DataLoader workers for loading meta/constraints paths (default: 4)",
    )
    parser.add_argument(
        "--model",
        type=str,
        default=DEFAULT_MODEL,
        help="Name of the model (e.g. Kimodo-SOMA-RP-v1.1, kimodo-soma-rp, or SOMA).",
    )
    parser.add_argument(
        "--diffusion_steps",
        type=int,
        default=100,
        help="Number of diffusion steps (default: 100); overridden by meta.json if present",
    )
    parser.add_argument(
        "--postprocess",
        action="store_true",
        help="Apply motion post-processing to reduce foot skating",
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="Regenerate outputs even if motion.npz already exists",
    )
    parser.add_argument(
        "--text_encoder_fp32",
        action="store_true",
        help="Uses fp32 for instantiating the text encoder (if API is not already running) rather than default bfloat16.",
    )
    return parser.parse_args()


def discover_example_folders(root: Path) -> list[tuple[Path, Path]]:
    """Discover leaf directories that contain meta.json.

    Returns list of (src_dir, rel_path).
    """
    root = root.resolve()
    if not root.is_dir():
        raise FileNotFoundError(f"Testsuite folder does not exist: {root}")
    out: list[tuple[Path, Path]] = []
    for meta_path in root.rglob("meta.json"):
        src_dir = meta_path.parent
        rel = src_dir.relative_to(root)
        out.append((src_dir, rel))
    return sorted(out, key=lambda x: str(x[1]))


def copy_source_files(src_dir: Path, out_dir: Path) -> None:
    """Copy meta.json, constraints.json, and gt_motion.npz (if present) from src_dir to out_dir."""
    out_dir.mkdir(parents=True, exist_ok=True)
    for name in ("meta.json", "constraints.json", "gt_motion.npz"):
        src_file = src_dir / name
        if src_file.is_file():
            shutil.copy2(src_file, out_dir / name)


class EvalExampleDataset(Dataset):
    """Dataset of example folders: yields text, num_frame, constraints_path (and paths, meta).
    No torch/skeleton in workers so num_workers > 0 is safe with CUDA.
    """

    def __init__(
        self,
        examples: list[tuple[Path, Path]],
        testsuite_root: Path,
        generated_root: Path,
        fps: float,
    ):
        self.examples = examples
        self.testsuite_root = testsuite_root
        self.generated_root = generated_root
        self.fps = fps

    def __len__(self) -> int:
        return len(self.examples)

    def __getitem__(self, idx: int) -> dict[str, Any]:
        src_dir, rel_path = self.examples[idx]
        out_dir = self.generated_root / rel_path
        meta_path = src_dir / "meta.json"
        meta = load_json(str(meta_path))
        assert meta.get("num_samples", 1) == 1, "Expected num_samples to be absent or 1 in meta.json"
        texts, durations_sec = parse_prompts_from_meta(meta)
        assert len(texts) == 1, "Expected exactly one prompt (len(texts)==1) per example"
        num_frames = [int(float(d) * self.fps) for d in durations_sec]
        assert len(num_frames) == 1, "Expected exactly one duration per example"
        constraints_path = src_dir / "constraints.json"
        cpath = str(constraints_path) if constraints_path.is_file() else None
        return {
            "rel_path": rel_path,
            "src_dir": str(src_dir),
            "out_dir": str(out_dir),
            "meta": meta,
            "text": texts[0],
            "num_frame": num_frames[0],
            "constraints_path": cpath,
        }


def collate_examples(batch: list[dict]) -> dict[str, Any]:
    """Collate list of example dicts; keep list fields as lists (no stacking)."""
    if not batch:
        return {}
    keys = batch[0].keys()
    out: dict[str, Any] = {}
    for k in keys:
        vals = [b[k] for b in batch]
        out[k] = vals
    return out


def group_by_parent(
    examples: list[tuple[Path, Path]],
) -> list[list[tuple[Path, Path]]]:
    """Group (src_dir, rel_path) by parent directory of rel_path for folder-by-folder processing."""
    from itertools import groupby

    def parent_key(item: tuple[Path, Path]) -> Path:
        rel = item[1]
        return rel.parent if len(rel.parts) > 1 else Path(".")

    sorted_examples = sorted(examples, key=parent_key)
    groups: list[list[tuple[Path, Path]]] = []
    for _key, group in groupby(sorted_examples, key=parent_key):
        groups.append(list(group))
    return groups


def _slice_output_at(output: dict[str, Any], index: int) -> dict[str, Any]:
    """Slice a (possibly nested) output dict at batch index for one sample."""
    out: dict[str, Any] = {}
    for k, v in output.items():
        if isinstance(v, dict):
            out[k] = _slice_output_at(v, index)
        elif isinstance(v, np.ndarray) and v.ndim > 0:
            out[k] = v[index]
        else:
            out[k] = v
    return out


def _crop_output(output: dict[str, Any], num_frames: int) -> dict[str, Any]:
    """Crop a single-sample output dict along the time dimension (axis 0)."""
    out: dict[str, Any] = {}
    for k, v in output.items():
        if isinstance(v, dict):
            out[k] = _crop_output(v, num_frames)
        elif isinstance(v, np.ndarray) and v.ndim >= 1:
            out[k] = v[:num_frames]
        else:
            out[k] = v
    return out


def main():
    device = "cuda:0" if torch.cuda.is_available() else "cpu"
    print(f"Using device: {device}")

    args = parse_args()
    testsuite_root = Path(args.benchmark).resolve()
    if args.output is not None:
        generated_root = Path(args.output).resolve()
    else:
        generated_root = testsuite_root
    in_place = generated_root == testsuite_root

    examples = discover_example_folders(testsuite_root)
    if not examples:
        raise SystemExit(f"No folders with meta.json found under {testsuite_root}")
    print(f"Discovered {len(examples)} example folders.")

    model, resolved_name = load_model(
        args.model,
        device=device,
        default_family="Kimodo",
        return_resolved_name=True,
        text_encoder_fp32=args.text_encoder_fp32,
    )
    # v1.1 models are meant to be used for benchmark evaluation
    _deprecated_for_benchmark = {
        "kimodo-soma-rp-v1": "Kimodo-SOMA-RP-v1 was not trained to be compatible with the benchmark evaluation.",
        "kimodo-soma-seed-v1": "Kimodo-SOMA-SEED-v1 is not the latest model for benchmark evaluation.",
    }
    if resolved_name in _deprecated_for_benchmark:
        import warnings

        warnings.warn(
            f"Model '{args.model}' resolved to {resolved_name}: "
            f"{_deprecated_for_benchmark[resolved_name]} Consider using v1.1.",
            stacklevel=1,
        )
    print(f"Generating with model: {resolved_name}")
    fps = model.fps
    default_diffusion_steps = args.diffusion_steps

    groups = group_by_parent(examples)
    total_generated = 0
    total_skipped = 0

    total_examples = len(examples)
    for group in groups:
        rel_path_0 = group[0][1]
        if rel_path_0.parent != Path("."):
            folder_label = str(rel_path_0.parent)
        else:
            # Direct children of testsuite root: show root name (e.g. inbetweening)
            folder_label = testsuite_root.name
        num_in_folder = len(group)
        print(f"Generating folder: {folder_label} ({num_in_folder} motions)")

        dataset = EvalExampleDataset(
            group,
            testsuite_root,
            generated_root,
            fps=fps,
        )
        loader = DataLoader(
            dataset,
            batch_size=args.batch_size,
            shuffle=False,
            num_workers=args.num_workers,
            collate_fn=collate_examples,
        )

        folder_generated = 0
        folder_skipped = 0
        for batch_idx, batch in enumerate(loader):
            rel_paths = batch["rel_path"]
            src_dirs = batch["src_dir"]
            out_dirs = batch["out_dir"]
            metas = batch["meta"]
            batch_texts = batch["text"]
            batch_num_frames = batch["num_frame"]
            constraints_paths = batch["constraints_path"]

            # Filter out samples that are already generated (unless --overwrite).
            if args.overwrite:
                selected_indices = list(range(len(rel_paths)))
            else:
                selected_indices = []
                for i, out_dir_str in enumerate(out_dirs):
                    motion_path = Path(out_dir_str) / "motion.npz"
                    if motion_path.is_file():
                        folder_skipped += 1
                        total_skipped += 1
                        continue
                    selected_indices.append(i)

            if not selected_indices:
                print(
                    f"\r  Generated {folder_generated} / {num_in_folder} (skipped: {folder_skipped}) "
                    f"(total: {total_generated + total_skipped} / {total_examples})",
                    end="",
                    flush=True,
                )
                continue

            rel_paths = [rel_paths[i] for i in selected_indices]
            src_dirs = [src_dirs[i] for i in selected_indices]
            out_dirs = [out_dirs[i] for i in selected_indices]
            metas = [metas[i] for i in selected_indices]
            batch_texts = [batch_texts[i] for i in selected_indices]
            batch_num_frames = [batch_num_frames[i] for i in selected_indices]
            constraints_paths = [constraints_paths[i] for i in selected_indices]

            # Load constraints in main process on model device (no torch in workers)
            device_t = torch.device(device)
            batch_constraints_lst = [
                load_constraints_lst(cpath, model.skeleton, device=device_t) if cpath else []
                for cpath in constraints_paths
            ]

            if not in_place:
                for i in range(len(rel_paths)):
                    copy_source_files(Path(src_dirs[i]), Path(out_dirs[i]))

            # Use first example's diffusion_steps and seed for the whole batch
            diffusion_steps = metas[0].get("diffusion_steps", default_diffusion_steps)
            seed = metas[0].get("seed", None)
            if seed is not None:
                seed_everything(seed)
            else:
                print("Warning: No seed found in meta.json, not seeding this batch.")

            # Single model call for the entire batch (count in bar title, bar clears when done)
            bar_desc = (
                f"  Generated {folder_generated} / {num_in_folder} "
                f"(skipped: {folder_skipped}) (total: {total_generated + total_skipped} / {total_examples})"
            )
            output = model(
                batch_texts,
                batch_num_frames,
                constraint_lst=batch_constraints_lst,
                num_denoising_steps=diffusion_steps,
                multi_prompt=False,
                post_processing=args.postprocess,
                return_numpy=True,
                progress_bar=lambda x: tqdm(x, leave=False, desc=bar_desc),
            )

            # Save each sample to its output dir
            B = len(batch_texts)
            for b in range(B):
                out_dir = Path(out_dirs[b])
                sample_output = _slice_output_at(output, b)
                sample_output = _crop_output(sample_output, batch_num_frames[b])
                motion_path = out_dir / "motion.npz"
                np.savez(motion_path, **sample_output)
                total_generated += 1
                folder_generated += 1

            print(
                f"\r  Generated {folder_generated} / {num_in_folder} (skipped: {folder_skipped}) "
                f"(total: {total_generated + total_skipped} / {total_examples})",
                end="",
                flush=True,
            )

        print()
        print(
            f"  Finished folder {folder_label} ({num_in_folder} motions, "
            f"generated: {folder_generated}, skipped: {folder_skipped})."
        )

    if in_place:
        print(f"Generated {total_generated} motions in-place under {testsuite_root}.")
    else:
        print(f"Generated {total_generated} motions under {generated_root}.")


if __name__ == "__main__":
    main()