cozyclay 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +59 -24
  2. package/THIRD_PARTY_NOTICES.md +31 -0
  3. package/bin/cozyclay.mjs +105 -11
  4. package/dist/CNAME +1 -0
  5. package/dist/app/index.html +57 -0
  6. package/dist/assets/app-Cgpk2hwX.js +4803 -0
  7. package/dist/assets/app-DgZvaAE1.css +1 -0
  8. package/dist/favicon.ico +0 -0
  9. package/dist/icons/apple-touch-icon.png +0 -0
  10. package/dist/icons/icon-192.png +0 -0
  11. package/dist/icons/icon-32.png +0 -0
  12. package/dist/icons/icon-512.png +0 -0
  13. package/dist/icons/icon-maskable-512.png +0 -0
  14. package/dist/index.html +406 -5
  15. package/dist/manifest.webmanifest +42 -0
  16. package/dist/media/cozyclay-demo-poster.jpg +0 -0
  17. package/dist/media/cozyclay-logo.png +0 -0
  18. package/dist/robots.txt +4 -0
  19. package/dist/sitemap.xml +9 -0
  20. package/dist/sw.js +127 -0
  21. package/package.json +15 -3
  22. package/src/App.jsx +1393 -648
  23. package/src/ardy/client.js +5 -4
  24. package/src/ardy/mp4-duration.js +102 -0
  25. package/src/ardy/timeline-coordinates.js +11 -0
  26. package/src/ardy/timeline.jsx +528 -67
  27. package/src/ardy/waypoints.js +14 -9
  28. package/src/camera-block.js +60 -0
  29. package/src/camera-follow.js +49 -10
  30. package/src/camera-rail-schedule.js +176 -0
  31. package/src/controls.jsx +16 -2
  32. package/src/cuts.js +184 -0
  33. package/src/generation/client.js +56 -0
  34. package/src/generation/session.js +47 -0
  35. package/src/generation/shot-spec.js +40 -0
  36. package/src/generation/use-generation.js +55 -0
  37. package/src/hierarchy-model.js +9 -17
  38. package/src/hierarchy-panel.jsx +170 -23
  39. package/src/locale-toggle.jsx +18 -0
  40. package/src/locale.js +34 -0
  41. package/src/main.jsx +5 -0
  42. package/src/object-catalog.jsx +32 -3
  43. package/src/planview.jsx +174 -66
  44. package/src/pose-extract/browser.js +42 -0
  45. package/src/pose-extract/fit.js +194 -0
  46. package/src/pose-extract/index.js +53 -0
  47. package/src/pose-extract/keyframes.js +74 -0
  48. package/src/pose-extract/landmarks.js +162 -0
  49. package/src/poses.js +114 -0
  50. package/src/posestudio.jsx +75 -13
  51. package/src/pwa.js +42 -0
  52. package/src/result-modal.jsx +124 -0
  53. package/src/scenes.js +223 -0
  54. package/src/shot-authoring.js +204 -77
  55. package/src/shot.js +8 -4
  56. package/src/styles.css +839 -38
  57. package/src/ui.jsx +54 -5
  58. package/tools/ardy/BRIDGE.md +70 -4
  59. package/tools/ardy/README.md +55 -0
  60. package/tools/ardy/bridge.mjs +89 -389
  61. package/tools/ardy/cclay_constrained_generate.py +449 -0
  62. package/tools/ardy/cclay_motion_edit.py +74 -9
  63. package/tools/ardy/cclay_sequence_generate.py +68 -7
  64. package/tools/ardy/cclay_worker.py +304 -0
  65. package/tools/ardy/merge-text-encoder.py +160 -0
  66. package/tools/ardy/run-local.mjs +233 -0
  67. package/tools/ardy/runners/index.mjs +23 -0
  68. package/tools/ardy/runners/local.mjs +452 -0
  69. package/tools/ardy/runners/proc.mjs +125 -0
  70. package/tools/ardy/runners/remote.mjs +246 -0
  71. package/tools/ardy/setup-local.mjs +189 -0
  72. package/tools/ardy/setup-text-encoder-on-box.sh +93 -0
  73. package/tools/ardy/setup-text-encoder.py +296 -0
  74. package/tools/generation/README.md +29 -0
  75. package/tools/generation/bridge.mjs +149 -0
  76. package/tools/generation/job-store.mjs +51 -0
  77. package/tools/generation/providers/kling.mjs +76 -0
  78. package/tools/generation/providers/runway.mjs +101 -0
  79. package/tools/generation/providers/seedance.mjs +87 -0
  80. package/tools/generation/providers/shared.mjs +49 -0
  81. package/tools/generation/providers/veo.mjs +84 -0
  82. package/tools/metrics/archive-traffic.mjs +138 -0
  83. package/dist/assets/index-BW_S1YRy.js +0 -4395
  84. package/dist/assets/index-nRmmJgap.css +0 -1
  85. package/tools/ardy/__pycache__/cclay_sequence_generate.cpython-313.pyc +0 -0
  86. package/tools/ardy/out/gen-1786443326924-c6019e-generated.npz +0 -0
  87. package/tools/ardy/out/gen-1786443609325-0053db-generated.npz +0 -0
  88. package/tools/ardy/out/gen-1786443835628-749ed1-generated.npz +0 -0
  89. package/tools/ardy/out/gen-1786462605247-03cb19-generated.npz +0 -0
  90. package/tools/qa-browser.mjs +0 -84
  91. package/tools/qa-crop3.mjs +0 -22
  92. package/tools/qa-playview.mjs +0 -49
  93. package/tools/qa-screenshot.mjs +0 -24
  94. package/tools/qa-visual.mjs +0 -51
@@ -0,0 +1,449 @@
1
+ # SPDX-License-Identifier: GPL-3.0-or-later
2
+ # SPDX-FileCopyrightText: CozyClay contributors
3
+ #
4
+ # cclay-owned script. Source of truth lives in the CozyClay repo at
5
+ # tools/ardy/cclay_constrained_generate.py. The local runner executes it in
6
+ # place; a box deployment may copy it to ~/ardy/scripts/. Do not edit a
7
+ # deployed copy.
8
+ """One-shot constrained text-to-motion generation for the cclay ARDY pipeline.
9
+
10
+ One full ``model(...)`` sampling call over the whole clip, optionally
11
+ conditioned on:
12
+
13
+ * ``--pose-from <npz> <src-frame> <dst-frame>`` (repeatable): pins the FULL
14
+ BODY pose stored in an ARDY motion npz (``local_rot_mats`` +
15
+ ``posed_joints``) onto a clip frame via ``FullBodyConstraintSet`` — joint
16
+ positions, joint rotations, root X/Y/Z and heading, exactly the semantics
17
+ BRIDGE.md documents for pose pinning.
18
+ * ``--root-2d FRAME X Z HEADING`` (repeatable): sparse root waypoints on the
19
+ X/Z ground plane via ``Root2DConstraintSet`` (HEADING in radians or the
20
+ literal ``none``), the same contract as cclay_sequence_generate.py.
21
+
22
+ With no constraints at all this is plain text-to-motion (the local twin of
23
+ ``scripts/generate.py``); a ``--base`` flag is accepted for grammar
24
+ compatibility with the remote script but ignored — constraint conditioning
25
+ here needs no unconstrained first pass.
26
+
27
+ The last stdout line is a single JSON object whose ``target_space`` field
28
+ marks it as a constrained-generation report (bridge.mjs keys on that):
29
+ {"target_space": "skeleton_joint_center", "frames": int, "fps": int,
30
+ "model": str,
31
+ "poses": [{"dst_frame", "root_error_m", "shape_mean_error_m",
32
+ "shape_max_error_m"}],
33
+ "waypoints": [{"frame", "requested_xz", "achieved_xz",
34
+ "achieved_error_m", "heading_rad"}]}
35
+ """
36
+
37
+ import argparse
38
+ import json
39
+ import os
40
+
41
+ import numpy as np
42
+ import torch
43
+
44
+ from ardy.model import DEFAULT_MODEL, load_model
45
+ from ardy.model.loading import get_env_var
46
+ from ardy.model.registry import resolve_model_name
47
+ from ardy.motion_rep.tools import length_to_mask
48
+ from ardy.postprocess import post_process_motion
49
+ from ardy.skeleton import SOMASkeleton30
50
+ from ardy.tools import seed_everything, to_numpy
51
+
52
+
53
+ def parse_args(argv=None):
54
+ parser = argparse.ArgumentParser(
55
+ description="One-shot constrained text-to-motion generation (cclay)"
56
+ )
57
+ parser.add_argument("--prompt", type=str, required=True, help="Text prompt.")
58
+ parser.add_argument("--duration", type=float, required=True, help="Clip length in seconds.")
59
+ parser.add_argument(
60
+ "--output",
61
+ type=str,
62
+ required=True,
63
+ help="Output npz path (.npz appended when missing; parent dirs created).",
64
+ )
65
+ parser.add_argument(
66
+ "--pose-from",
67
+ dest="pose_from",
68
+ action="append",
69
+ nargs=3,
70
+ metavar=("NPZ", "SRC_FRAME", "DST_FRAME"),
71
+ help="Repeatable: pin the full-body pose NPZ[SRC_FRAME] at clip frame DST_FRAME.",
72
+ )
73
+ parser.add_argument(
74
+ "--root-2d",
75
+ dest="root_2d",
76
+ action="append",
77
+ nargs=4,
78
+ metavar=("FRAME", "X", "Z", "HEADING"),
79
+ help="Repeatable: pin the root's ground position (meters) at a clip frame; HEADING radians or 'none'.",
80
+ )
81
+ parser.add_argument(
82
+ "--base",
83
+ type=str,
84
+ default=None,
85
+ help="Accepted for remote-grammar compatibility; unused locally.",
86
+ )
87
+ parser.add_argument("--seed", type=int, default=None, help="Seed for reproducible results.")
88
+ parser.add_argument(
89
+ "--model",
90
+ type=str,
91
+ default=DEFAULT_MODEL,
92
+ help="Model nickname or full folder name (default: %(default)s).",
93
+ )
94
+ parser.add_argument(
95
+ "--diffusion_steps",
96
+ type=int,
97
+ default=None,
98
+ help="Denoising steps, at most the model's num_base_steps (the default).",
99
+ )
100
+ parser.add_argument(
101
+ "--cfg_weight",
102
+ type=float,
103
+ nargs="+",
104
+ default=[2.0, 2.0],
105
+ help="CFG scale(s): one float (text) or two floats (text, constraint).",
106
+ )
107
+ parser.add_argument(
108
+ "--no-postprocess",
109
+ action="store_true",
110
+ help="Don't apply motion post-processing (foot-skate reduction).",
111
+ )
112
+ parser.add_argument(
113
+ "--checkpoints_dir",
114
+ type=str,
115
+ default=None,
116
+ help="Local dir holding released model folders (falls back to CHECKPOINTS_DIR env).",
117
+ )
118
+ return parser.parse_args(argv)
119
+
120
+
121
+ def cclay_pick_device():
122
+ """cuda > mps > cpu, overridable with CCLAY_ARDY_DEVICE (cpu|mps|cuda[:N]).
123
+
124
+ The env override is how the local runner forces CPU (--cpu) without
125
+ relying on CUDA_VISIBLE_DEVICES, which cannot hide an Apple GPU.
126
+ """
127
+ forced = os.environ.get("CCLAY_ARDY_DEVICE", "").strip().lower()
128
+ if forced:
129
+ return forced
130
+ if torch.cuda.is_available():
131
+ return "cuda:0"
132
+ mps_backend = getattr(torch.backends, "mps", None)
133
+ if mps_backend is not None and mps_backend.is_available():
134
+ return "mps"
135
+ return "cpu"
136
+
137
+
138
+ def cclay_mps_compat(device):
139
+ """Downcast float64 buffers to float32 at registration when running on MPS.
140
+
141
+ The MPS backend has no float64. ARDY's skeleton loads neutral_joints from
142
+ a float64 pickle and registers it as a buffer, which makes Ardy.__init__'s
143
+ .to("mps") fail; float32 is plenty for joint positions in meters. No-op on
144
+ cuda/cpu so remote-box behavior is untouched.
145
+ """
146
+ if not str(device).startswith("mps"):
147
+ return
148
+ original = torch.nn.Module.register_buffer
149
+ if getattr(original, "_cclay_f32", False):
150
+ return
151
+
152
+ def register_buffer_f32(self, name, tensor, persistent=True):
153
+ if tensor is not None and getattr(tensor, "dtype", None) == torch.float64:
154
+ tensor = tensor.float()
155
+ return original(self, name, tensor, persistent)
156
+
157
+ register_buffer_f32._cclay_f32 = True
158
+ torch.nn.Module.register_buffer = register_buffer_f32
159
+
160
+
161
+ def cclay_progress(iterable):
162
+ """Print one line per sampling step so the bridge can stream progress.
163
+
164
+ The model calls this where it would call tqdm; every line lands in the
165
+ UI as a status event, which is the only liveness signal the operator has
166
+ during a minutes-long generation.
167
+ """
168
+ total = len(iterable) if hasattr(iterable, "__len__") else None
169
+ for index, item in enumerate(iterable):
170
+ if total:
171
+ print(f"sampling step {index + 1}/{total}", flush=True)
172
+ else:
173
+ print(f"sampling step {index + 1}", flush=True)
174
+ yield item
175
+
176
+
177
+ def _default_history_frames(fps: float, gen_horizon_len: int, num_frames_per_token: int) -> int:
178
+ """Longest history that, with the generation horizon, fits the trained 10 s window."""
179
+ max_window_len = (int(10 * fps) // num_frames_per_token) * num_frames_per_token
180
+ return ((max_window_len - gen_horizon_len) // num_frames_per_token) * num_frames_per_token
181
+
182
+
183
+ def _single_file_path(path: str, ext: str) -> str:
184
+ if not path.endswith(ext):
185
+ path = path.rstrip(os.sep) + ext
186
+ parent = os.path.dirname(path)
187
+ if parent:
188
+ os.makedirs(parent, exist_ok=True)
189
+ return path
190
+
191
+
192
+ def save_motion_npz(path: str, motion_dict: dict, fps: float, text: str) -> None:
193
+ arrays = {k: np.asarray(v) for k, v in motion_dict.items()}
194
+ arrays["fps"] = np.asarray(fps)
195
+ arrays["text"] = np.asarray(text)
196
+ np.savez(path, **arrays)
197
+
198
+
199
+ def parse_root_waypoints(raw_waypoints, num_frames: int) -> list:
200
+ """Same contract as cclay_sequence_generate.py, frames in clip-local range."""
201
+ if not raw_waypoints:
202
+ return []
203
+ waypoints = []
204
+ seen = set()
205
+ for raw_frame, raw_x, raw_z, raw_heading in raw_waypoints:
206
+ try:
207
+ frame = int(raw_frame)
208
+ except ValueError:
209
+ raise ValueError(f"--root-2d frame must be an integer, got {raw_frame!r}.")
210
+ if not 0 <= frame < num_frames:
211
+ raise ValueError(f"--root-2d frame {frame} is outside the clip (0..{num_frames - 1}).")
212
+ if frame in seen:
213
+ raise ValueError(f"duplicate --root-2d for frame {frame}; one waypoint per frame.")
214
+ seen.add(frame)
215
+ try:
216
+ x, z = float(raw_x), float(raw_z)
217
+ except ValueError:
218
+ raise ValueError(f"--root-2d X Z must be numbers, got {(raw_x, raw_z)!r}.")
219
+ if str(raw_heading).lower() == "none":
220
+ heading = None
221
+ else:
222
+ try:
223
+ heading = float(raw_heading)
224
+ except ValueError:
225
+ raise ValueError(f"--root-2d HEADING must be a number or 'none', got {raw_heading!r}.")
226
+ waypoints.append({"frame": frame, "xz": [x, z], "heading": heading})
227
+ waypoints.sort(key=lambda entry: entry["frame"])
228
+ if len({entry["heading"] is None for entry in waypoints}) > 1:
229
+ raise ValueError(
230
+ "--root-2d heading must be given for every waypoint or for none of them; "
231
+ "ARDY conditions the whole waypoint set on one heading tensor."
232
+ )
233
+ return waypoints
234
+
235
+
236
+ def load_pose(path, skeleton, device):
237
+ """Pose npz -> (local_rot_mats [F,J,3,3], posed_joints [F,J,3]) tensors.
238
+
239
+ The npz stores per-joint LOCAL rotations; FullBodyConstraintSet wants
240
+ GLOBAL rotations and posed positions, so the caller runs skeleton.fk on
241
+ the selected frame — the same expansion cclay_motion_edit.py uses.
242
+ """
243
+ with np.load(path, allow_pickle=False) as data:
244
+ local = torch.from_numpy(np.asarray(data["local_rot_mats"])).float().to(device)
245
+ posed = torch.from_numpy(np.asarray(data["posed_joints"])).float().to(device)
246
+ return local, posed
247
+
248
+
249
+ def measure_waypoints(waypoints: list, generated_joints, skeleton) -> list:
250
+ report = []
251
+ root_index = skeleton.root_idx
252
+ for entry in waypoints:
253
+ root = np.asarray(generated_joints[entry["frame"], root_index], dtype=np.float64)
254
+ achieved = [float(root[0]), float(root[2])]
255
+ error = float(np.linalg.norm(np.asarray(achieved) - np.asarray(entry["xz"])))
256
+ report.append(
257
+ {
258
+ "frame": entry["frame"],
259
+ "requested_xz": [round(value, 4) for value in entry["xz"]],
260
+ "achieved_xz": [round(value, 4) for value in achieved],
261
+ "achieved_error_m": round(error, 4),
262
+ "heading_rad": entry["heading"],
263
+ }
264
+ )
265
+ return report
266
+
267
+
268
+ def main(argv=None, preloaded_model=None):
269
+ args = parse_args(argv)
270
+ device = cclay_pick_device()
271
+ cclay_mps_compat(device)
272
+ print(f"Using device: {device}")
273
+
274
+ prompt = args.prompt.strip()
275
+ if not prompt:
276
+ raise ValueError("--prompt must be non-empty.")
277
+ if args.duration <= 0:
278
+ raise ValueError(f"--duration must be > 0 seconds, got {args.duration}.")
279
+
280
+ if len(args.cfg_weight) == 1:
281
+ cfg_weight = float(args.cfg_weight[0])
282
+ elif len(args.cfg_weight) == 2:
283
+ cfg_weight = (float(args.cfg_weight[0]), float(args.cfg_weight[1]))
284
+ else:
285
+ raise ValueError("--cfg_weight expects one float (text) or two floats (text, constraint).")
286
+
287
+ checkpoints_dir = args.checkpoints_dir or get_env_var("CHECKPOINTS_DIR")
288
+ resolved_model = resolve_model_name(args.model, checkpoints_dir=checkpoints_dir)
289
+ model = (
290
+ preloaded_model
291
+ if preloaded_model is not None
292
+ else load_model(resolved_model, device=device, checkpoints_dir=checkpoints_dir)
293
+ )
294
+ model.eval()
295
+ skeleton = model.skeleton
296
+ fps = model.motion_rep.fps
297
+ patch = model.num_frames_per_token
298
+
299
+ num_frames = int(args.duration * fps)
300
+ if num_frames < 3:
301
+ raise ValueError(f"--duration {args.duration}s yields {num_frames} frames; the minimum is 3.")
302
+
303
+ num_base_steps = int(model.diffusion.num_base_steps)
304
+ diffusion_steps = args.diffusion_steps if args.diffusion_steps is not None else num_base_steps
305
+ if not 1 <= diffusion_steps <= num_base_steps:
306
+ raise ValueError(
307
+ f"--diffusion_steps must be between 1 and {num_base_steps}; got {diffusion_steps}."
308
+ )
309
+ history_frames = _default_history_frames(fps, model.gen_horizon_len, patch)
310
+
311
+ # --- constraints --------------------------------------------------------
312
+ constraint_lst = []
313
+ pose_targets = [] # for the report: (dst_frame, requested positions [J,3])
314
+ for npz_path, raw_src, raw_dst in args.pose_from or []:
315
+ try:
316
+ src_frame, dst_frame = int(raw_src), int(raw_dst)
317
+ except ValueError:
318
+ raise ValueError(f"--pose-from frames must be integers, got {(raw_src, raw_dst)!r}.")
319
+ if not 0 <= dst_frame < num_frames:
320
+ raise ValueError(f"--pose-from dst-frame {dst_frame} is outside the clip (0..{num_frames - 1}).")
321
+ local, posed = load_pose(npz_path, skeleton, device)
322
+ if not 0 <= src_frame < local.shape[0]:
323
+ raise ValueError(
324
+ f"--pose-from src-frame {src_frame} is outside {npz_path} (0..{local.shape[0] - 1})."
325
+ )
326
+ from ardy.constraints import FullBodyConstraintSet
327
+
328
+ root = posed[src_frame : src_frame + 1, skeleton.root_idx]
329
+ rotations, positions, _ = skeleton.fk(local[src_frame : src_frame + 1], root)
330
+ constraint_lst.append(
331
+ FullBodyConstraintSet(
332
+ skeleton,
333
+ torch.tensor([dst_frame]),
334
+ positions,
335
+ rotations,
336
+ )
337
+ )
338
+ pose_targets.append((dst_frame, positions[0].cpu().numpy()))
339
+ print(f"FullBodyConstraintSet: {npz_path}[{src_frame}] pinned at clip frame {dst_frame}")
340
+
341
+ waypoints = parse_root_waypoints(args.root_2d or [], num_frames)
342
+ if waypoints:
343
+ from ardy.constraints import Root2DConstraintSet
344
+
345
+ headings = [entry["heading"] for entry in waypoints]
346
+ constraint_lst.append(
347
+ Root2DConstraintSet(
348
+ skeleton,
349
+ frame_indices=torch.tensor([entry["frame"] for entry in waypoints]),
350
+ root_2d=torch.tensor(
351
+ [entry["xz"] for entry in waypoints], device=device, dtype=torch.float32
352
+ ),
353
+ global_root_heading=(
354
+ None
355
+ if headings[0] is None
356
+ else torch.tensor(headings, device=device, dtype=torch.float32)
357
+ ),
358
+ )
359
+ )
360
+ print(f"Root2DConstraintSet on clip frames {[entry['frame'] for entry in waypoints]}")
361
+
362
+ observed_motion = None
363
+ motion_mask = None
364
+ lengths = torch.tensor([num_frames], device=device)
365
+ if constraint_lst:
366
+ observed_motion, motion_mask = model.motion_rep.create_conditions_from_constraints_batched(
367
+ constraint_lst,
368
+ lengths,
369
+ to_normalize=True,
370
+ device=device,
371
+ )
372
+
373
+ # --- sampling -----------------------------------------------------------
374
+ if args.seed is not None:
375
+ seed_everything(args.seed)
376
+ print(f"Generating {num_frames} frames ({num_frames / fps:.2f}s at {fps} fps)")
377
+ with torch.no_grad():
378
+ motion = model(
379
+ [prompt],
380
+ num_frames,
381
+ num_denoising_steps=diffusion_steps,
382
+ pad_mask=length_to_mask(lengths),
383
+ first_heading_angle=torch.zeros(1, device=device),
384
+ motion_mask=motion_mask,
385
+ observed_motion=observed_motion,
386
+ cfg_weight=cfg_weight,
387
+ progress_bar=cclay_progress,
388
+ crop_history_length=history_frames,
389
+ )
390
+ output = model.motion_rep.inverse(motion, is_normalized=True)
391
+
392
+ use_postprocess = "g1" not in resolved_model.lower() and not args.no_postprocess
393
+ if use_postprocess:
394
+ corrected = post_process_motion(
395
+ output["local_rot_mats"],
396
+ output["root_positions"],
397
+ output["foot_contacts"],
398
+ skeleton,
399
+ constraint_lst=constraint_lst if constraint_lst else None,
400
+ )
401
+ output.update(corrected)
402
+
403
+ if isinstance(skeleton, SOMASkeleton30):
404
+ output = skeleton.output_to_SOMASkeleton77(output)
405
+
406
+ output = to_numpy(output)
407
+ motion_dict = {
408
+ k: (v[0] if hasattr(v, "shape") and len(v.shape) > 0 and v.shape[0] == 1 else v)
409
+ for k, v in output.items()
410
+ }
411
+
412
+ npz_path = _single_file_path(args.output, ".npz")
413
+ print(f"Saving the npz output to {npz_path}")
414
+ save_motion_npz(npz_path, motion_dict, fps, prompt)
415
+
416
+ # --- report (last stdout line, keyed on target_space by bridge.mjs) ------
417
+ generated_joints = np.asarray(motion_dict["posed_joints"])
418
+ root_index = skeleton.root_idx
419
+ pose_reports = []
420
+ for dst_frame, requested in pose_targets:
421
+ achieved = generated_joints[dst_frame]
422
+ root_error = float(np.linalg.norm(achieved[root_index] - requested[root_index]))
423
+ # Shape error compares body proportions with both roots removed, so a
424
+ # root miss is not double-counted against every joint.
425
+ requested_centered = requested - requested[root_index]
426
+ achieved_centered = achieved - achieved[root_index]
427
+ joint_errors = np.linalg.norm(achieved_centered - requested_centered, axis=-1)
428
+ pose_reports.append(
429
+ {
430
+ "dst_frame": dst_frame,
431
+ "root_error_m": round(root_error, 4),
432
+ "shape_mean_error_m": round(float(joint_errors.mean()), 4),
433
+ "shape_max_error_m": round(float(joint_errors.max()), 4),
434
+ }
435
+ )
436
+
437
+ report = {
438
+ "target_space": "skeleton_joint_center",
439
+ "frames": int(generated_joints.shape[0]),
440
+ "fps": int(fps),
441
+ "model": resolved_model,
442
+ "poses": pose_reports,
443
+ "waypoints": measure_waypoints(waypoints, generated_joints, skeleton),
444
+ }
445
+ print(json.dumps(report))
446
+
447
+
448
+ if __name__ == "__main__":
449
+ main()
@@ -48,7 +48,7 @@ TRACK_COMMIT_CHAINS = {
48
48
  }
49
49
 
50
50
 
51
- def parse_args():
51
+ def parse_args(argv=None):
52
52
  parser = argparse.ArgumentParser(description="Context-aware sparse ARDY motion edit")
53
53
  parser.add_argument("--source", required=True)
54
54
  parser.add_argument("--manifest", required=True)
@@ -57,13 +57,13 @@ def parse_args():
57
57
  parser.add_argument("--context-before", type=int, default=40)
58
58
  parser.add_argument("--context-after", type=int, default=20)
59
59
  parser.add_argument("--seed", type=int)
60
- return parser.parse_args()
60
+ return parser.parse_args(argv)
61
61
 
62
62
 
63
63
  class SparseJointConstraint:
64
64
  """Constraint only the named joints; deliberately does not touch root."""
65
65
 
66
- name = "cozyclay-sparse-joint"
66
+ name = "cozyclay-sparse-joint"
67
67
 
68
68
  def __init__(self, skeleton, frame_indices, positions, rotations, joint_names):
69
69
  import torch
@@ -101,7 +101,7 @@ class SparseJointConstraint:
101
101
  class RootTrackConstraint:
102
102
  """Root X/Y/Z and heading are a separate channel from body constraints."""
103
103
 
104
- name = "cozyclay-root-track"
104
+ name = "cozyclay-root-track"
105
105
 
106
106
  def __init__(self, skeleton, frame_indices, positions, headings):
107
107
  self.skeleton = skeleton
@@ -138,8 +138,68 @@ def heading_from_positions(positions, skeleton):
138
138
  return torch.stack([torch.cos(angle), torch.sin(angle)], dim=-1)
139
139
 
140
140
 
141
- def main():
142
- args = parse_args()
141
+ def cclay_pick_device():
142
+ """cuda > mps > cpu, overridable with CCLAY_ARDY_DEVICE (cpu|mps|cuda[:N]).
143
+
144
+ The env override is how the local runner forces CPU (--cpu) without
145
+ relying on CUDA_VISIBLE_DEVICES, which cannot hide an Apple GPU.
146
+ """
147
+ import torch
148
+
149
+ forced = os.environ.get("CCLAY_ARDY_DEVICE", "").strip().lower()
150
+ if forced:
151
+ return forced
152
+ if torch.cuda.is_available():
153
+ return "cuda:0"
154
+ mps_backend = getattr(torch.backends, "mps", None)
155
+ if mps_backend is not None and mps_backend.is_available():
156
+ return "mps"
157
+ return "cpu"
158
+
159
+
160
+ def cclay_mps_compat(device):
161
+ """Downcast float64 buffers to float32 at registration when running on MPS.
162
+
163
+ The MPS backend has no float64. ARDY's skeleton loads neutral_joints from
164
+ a float64 pickle and registers it as a buffer, which makes Ardy.__init__'s
165
+ .to("mps") fail; float32 is plenty for joint positions in meters. No-op on
166
+ cuda/cpu so remote-box behavior is untouched.
167
+ """
168
+ if not str(device).startswith("mps"):
169
+ return
170
+ import torch
171
+
172
+ original = torch.nn.Module.register_buffer
173
+ if getattr(original, "_cclay_f32", False):
174
+ return
175
+
176
+ def register_buffer_f32(self, name, tensor, persistent=True):
177
+ if tensor is not None and getattr(tensor, "dtype", None) == torch.float64:
178
+ tensor = tensor.float()
179
+ return original(self, name, tensor, persistent)
180
+
181
+ register_buffer_f32._cclay_f32 = True
182
+ torch.nn.Module.register_buffer = register_buffer_f32
183
+
184
+
185
+ def cclay_progress(iterable):
186
+ """Print one line per sampling step so the bridge can stream progress.
187
+
188
+ The model calls this where it would call tqdm; every line lands in the
189
+ UI as a status event, which is the only liveness signal the operator has
190
+ during a minutes-long generation.
191
+ """
192
+ total = len(iterable) if hasattr(iterable, "__len__") else None
193
+ for index, item in enumerate(iterable):
194
+ if total:
195
+ print(f"sampling step {index + 1}/{total}", flush=True)
196
+ else:
197
+ print(f"sampling step {index + 1}", flush=True)
198
+ yield item
199
+
200
+
201
+ def main(argv=None, preloaded_model=None):
202
+ args = parse_args(argv)
143
203
  import numpy as np
144
204
  import torch
145
205
  from ardy.constraints import FullBodyConstraintSet
@@ -150,10 +210,15 @@ def main():
150
210
  from ardy.motion_rep.tools import length_to_mask
151
211
  from ardy.tools import seed_everything, to_numpy
152
212
 
153
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
213
+ device = cclay_pick_device()
214
+ cclay_mps_compat(device)
154
215
  checkpoints_dir = get_env_var("CHECKPOINTS_DIR", None)
155
216
  resolved_model = resolve_model_name(DEFAULT_MODEL, checkpoints_dir=checkpoints_dir)
156
- model = load_model(resolved_model, device=device, checkpoints_dir=checkpoints_dir)
217
+ model = (
218
+ preloaded_model
219
+ if preloaded_model is not None
220
+ else load_model(resolved_model, device=device, checkpoints_dir=checkpoints_dir)
221
+ )
157
222
  model.eval()
158
223
  skeleton = model.skeleton
159
224
  fps = int(model.motion_rep.fps)
@@ -327,7 +392,7 @@ def main():
327
392
  motion_mask=motion_mask,
328
393
  observed_motion=observed_motion,
329
394
  cfg_weight=2.0,
330
- progress_bar=lambda iterable: iterable,
395
+ progress_bar=cclay_progress,
331
396
  init_history_sequence=history,
332
397
  )
333
398
  generated_motion = motion[:, history_frames:history_frames + generation_frames]
@@ -77,7 +77,7 @@ from ardy.tools import seed_everything, to_numpy
77
77
  _SEED_STRIDE = 9973
78
78
 
79
79
 
80
- def parse_args():
80
+ def parse_args(argv=None):
81
81
  parser = argparse.ArgumentParser(
82
82
  description="Segment-chained autoregressive text-to-motion generation (cclay)"
83
83
  )
@@ -180,7 +180,23 @@ def parse_args():
180
180
  default=None,
181
181
  help="Local dir holding released model folders (falls back to CHECKPOINTS_DIR env).",
182
182
  )
183
- return parser.parse_args()
183
+ return parser.parse_args(argv)
184
+
185
+
186
+ def cclay_progress(iterable):
187
+ """Print one line per sampling step so the bridge can stream progress.
188
+
189
+ The model calls this where it would call tqdm; every line lands in the
190
+ UI as a status event, which is the only liveness signal the operator has
191
+ during a minutes-long generation.
192
+ """
193
+ total = len(iterable) if hasattr(iterable, "__len__") else None
194
+ for index, item in enumerate(iterable):
195
+ if total:
196
+ print(f"sampling step {index + 1}/{total}", flush=True)
197
+ else:
198
+ print(f"sampling step {index + 1}", flush=True)
199
+ yield item
184
200
 
185
201
 
186
202
  def _default_history_frames(fps: float, gen_horizon_len: int, num_frames_per_token: int) -> int:
@@ -319,10 +335,51 @@ def _continuity_metrics(posed_joints: np.ndarray, boundaries: list) -> dict:
319
335
  }
320
336
 
321
337
 
322
- def main():
323
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
338
+ def cclay_pick_device():
339
+ """cuda > mps > cpu, overridable with CCLAY_ARDY_DEVICE (cpu|mps|cuda[:N]).
340
+
341
+ The env override is how the local runner forces CPU (--cpu) without
342
+ relying on CUDA_VISIBLE_DEVICES, which cannot hide an Apple GPU.
343
+ """
344
+ forced = os.environ.get("CCLAY_ARDY_DEVICE", "").strip().lower()
345
+ if forced:
346
+ return forced
347
+ if torch.cuda.is_available():
348
+ return "cuda:0"
349
+ mps_backend = getattr(torch.backends, "mps", None)
350
+ if mps_backend is not None and mps_backend.is_available():
351
+ return "mps"
352
+ return "cpu"
353
+
354
+
355
+ def cclay_mps_compat(device):
356
+ """Downcast float64 buffers to float32 at registration when running on MPS.
357
+
358
+ The MPS backend has no float64. ARDY's skeleton loads neutral_joints from
359
+ a float64 pickle and registers it as a buffer, which makes Ardy.__init__'s
360
+ .to("mps") fail; float32 is plenty for joint positions in meters. No-op on
361
+ cuda/cpu so remote-box behavior is untouched.
362
+ """
363
+ if not str(device).startswith("mps"):
364
+ return
365
+ original = torch.nn.Module.register_buffer
366
+ if getattr(original, "_cclay_f32", False):
367
+ return
368
+
369
+ def register_buffer_f32(self, name, tensor, persistent=True):
370
+ if tensor is not None and getattr(tensor, "dtype", None) == torch.float64:
371
+ tensor = tensor.float()
372
+ return original(self, name, tensor, persistent)
373
+
374
+ register_buffer_f32._cclay_f32 = True
375
+ torch.nn.Module.register_buffer = register_buffer_f32
376
+
377
+
378
+ def main(argv=None, preloaded_model=None):
379
+ device = cclay_pick_device()
380
+ cclay_mps_compat(device)
324
381
  print(f"Using device: {device}")
325
- args = parse_args()
382
+ args = parse_args(argv)
326
383
 
327
384
  segments = []
328
385
  for prompt, seconds_str in args.segment:
@@ -349,7 +406,11 @@ def main():
349
406
  # Load model (same path as scripts/generate.py)
350
407
  checkpoints_dir = args.checkpoints_dir or get_env_var("CHECKPOINTS_DIR")
351
408
  resolved_model = resolve_model_name(args.model, checkpoints_dir=checkpoints_dir)
352
- model = load_model(resolved_model, device=device, checkpoints_dir=checkpoints_dir)
409
+ model = (
410
+ preloaded_model
411
+ if preloaded_model is not None
412
+ else load_model(resolved_model, device=device, checkpoints_dir=checkpoints_dir)
413
+ )
353
414
  print(f"Loaded model: {resolved_model}")
354
415
 
355
416
  fps = model.motion_rep.fps
@@ -478,7 +539,7 @@ def main():
478
539
  motion_mask=seg_mask,
479
540
  observed_motion=seg_observed,
480
541
  cfg_weight=cfg_weight,
481
- progress_bar=lambda iterable: iterable,
542
+ progress_bar=cclay_progress,
482
543
  init_history_sequence=history,
483
544
  crop_history_length=history_budget if history is None else None,
484
545
  )