File size: 14,673 Bytes
fb4d72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285e72f
fb4d72b
 
 
 
285e72f
5bccf5c
c83d887
 
5bccf5c
285e72f
 
 
 
 
 
 
 
 
 
c83d887
 
 
 
 
 
 
 
5bccf5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c83d887
 
 
 
 
 
 
 
5bccf5c
c83d887
 
 
285e72f
c83d887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285e72f
 
 
 
c83d887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285e72f
 
 
 
 
fb4d72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285e72f
fb4d72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acdad44
285e72f
fb4d72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285e72f
 
 
 
 
 
 
 
 
fb4d72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285e72f
 
 
 
fb4d72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Studio episode/media stages on the LeRobot #4892 execution contracts.

The CPU controller keeps its shared billing guard and provider semaphores in
process. Only opaque in-process handles enter operational plans; tokens and
provider objects are never serialized. Scientific identities exclude handles.
"""

import hashlib
import json
import shutil
import tarfile
import uuid
from dataclasses import asdict
from pathlib import Path

from lerobot.annotations.runtime.configs import RuntimeConfig, StageConfig
from lerobot.annotations.runtime.pipeline import run_pipeline
from lerobot.annotations.runtime.types import (
    Artifact,
    DatasetRef,
    InputItem,
    ItemResult,
    ModuleSpec,
    Outcome,
    fingerprint,
)
from lerobot.annotations.runtime.worker import accepted_items

CONTEXTS = {}


def unpack(bundle, directory):
    with tarfile.open(bundle) as archive:
        for member in archive.getmembers():
            path = Path(member.name)
            if not member.isfile() or path.is_absolute() or ".." in path.parts:
                raise ValueError("Invalid Studio checkpoint bundle")
            target = directory / path
            target.parent.mkdir(parents=True, exist_ok=True)
            target.write_bytes(archive.extractfile(member).read())


class StudioStage:
    def __init__(self, handle, phase, scientific):
        self.handle, self.phase, self.scientific = handle, phase, scientific
        self.spec = ModuleSpec("studio_" + phase, "1", "episode", {"bundle": None})

    def scientific_config(self):
        return {"phase": self.phase, **self.scientific}

    def setup(self, context):
        self.values = CONTEXTS[self.handle]

    def teardown(self):
        pass

    def process_batch(self, items, context):
        import worker

        from lerobot.annotations.steerable_pipeline.staging import EpisodeStaging

        record, row, info, root, manifest, provider, media = self.values
        results = []
        for item in items:
            directory = context.scratch / item.item_id
            directory.mkdir(parents=True, exist_ok=True)
            staging_dir = directory / "staging"
            if self.phase == "language":
                result = worker._annotate_episode(
                    record, row, info, root, manifest, provider, staging_dir, None
                )
            else:
                artifact = Artifact(**item.payload["upstream"])
                if not context.store.verify(artifact):
                    raise ValueError("Upstream checkpoint checksum mismatch")
                bundle = context.store.download(artifact, directory / "language.tar.gz")
                unpack(bundle, directory)
                bundle.unlink()
                result = json.loads((directory / "result.json").read_text())
            if self.phase == "hands":
                import math
                import time

                import av
                import pyarrow.parquet as pq
                from subset import trim_video

                from lerobot.annotations.hands.writer import attach_actions

                if worker.HAND_ANNOTATOR is None:
                    raise RuntimeError("The HF Jobs hand worker was not initialized")
                native = directory / "hand-native"
                native.mkdir(parents=True, exist_ok=True)
                video = native / "episode.mp4"
                started = time.perf_counter()
                print(
                    "HAND_RUNTIME "
                    + json.dumps(
                        {"event": "episode_video_prepare_start", "source_episode": record.episode_index}
                    ),
                    flush=True,
                )
                source_video = root / worker.video_path(row, info, manifest["camera"])
                offset = float(row[f"videos/{manifest['camera']}/from_timestamp"])
                # Standalone raw episode videos need no RGB re-encode. The
                # native writer independently verifies decoded frame count/FPS.
                with av.open(str(source_video)) as probe:
                    stream = probe.streams.video[0]
                    complete_episode = (
                        len(probe.streams.video) == 1
                        and offset == 0
                        and stream.start_time == 0
                        and stream.frames == int(row["length"])
                        and math.isclose(float(stream.average_rate or 0), float(info["fps"]), rel_tol=1e-6)
                    )
                if complete_episode:
                    video = source_video
                else:
                    trim_video(
                        source_video, video, offset, int(row["length"]), float(info["fps"]), lossless=True
                    )
                prepared = time.perf_counter()
                print(
                    "HAND_RUNTIME "
                    + json.dumps(
                        {
                            "event": "native_annotation_start",
                            "source_episode": record.episode_index,
                            "video_prepare_seconds": prepared - started,
                            "input_mode": "whole-episode-video" if complete_episode else "lossless-trim",
                        }
                    ),
                    flush=True,
                )
                try:
                    native_report = worker.HAND_ANNOTATOR.annotate(
                        video,
                        manifest["hand_actions"]["calibration"],
                        native / "work",
                        orientation=manifest["hand_actions"].get("orientation", "sensor"),
                    )
                except Exception:
                    # Native inference is offline and has no Hub credentials.
                    import logging

                    logging.exception(
                        "Native hand annotation failed for source episode %s", record.episode_index
                    )
                    raise
                inferred = time.perf_counter()
                table = pq.read_table(
                    record.data_path, filters=[("episode_index", "=", record.episode_index)]
                )
                table, summary = attach_actions(table, native_report, directory=native, fps=info["fps"])
                summary["timing_seconds"] = {
                    "source_video_prepare": prepared - started,
                    "native_annotation": inferred - prepared,
                    "native_scope": "intrinsics, rectification, camera, hands and native export; includes coordinator wait and first-use model initialization; excludes source preparation and final dataset video copying",
                }
                summary["native_components"] = {
                    key: native_report[key]
                    for key in ("camera_statistics", "hand_components")
                    if key in native_report
                }
                print(
                    "HAND_RUNTIME "
                    + json.dumps(
                        {
                            "event": "native_annotation_finished",
                            "source_episode": record.episode_index,
                            "native_seconds": inferred - prepared,
                            "valid_frames": summary["valid_frames"],
                        }
                    ),
                    flush=True,
                )
                pq.write_table(table, directory / "hand_actions.parquet")
                result["hand_actions"] = summary
                # Model assets and RGB/prediction caches never enter dataset uploads or checkpoints.
                shutil.rmtree(native)
            elif self.phase == "human_video":
                staging = EpisodeStaging(staging_dir, record.episode_index)
                frames = worker.SourceFrames(row, info, root, manifest["camera"])
                full = worker.SourceFrames(row, info, root, manifest["camera"], max_size=None)
                try:
                    entries = worker.generate_human_videos(
                        record,
                        staging,
                        provider,
                        frames,
                        full,
                        media,
                        directory / "human_videos" / f"episode_{record.episode_index:06d}",
                        manifest["human_video"]["segments_per_episode"],
                    )
                except worker.BudgetExceededError as exc:
                    entries = [
                        {
                            "source_episode_index": record.episode_index,
                            "status": "time_stopped"
                            if isinstance(exc, worker.DeadlineExceededError)
                            else "budget_stopped",
                            "files": {},
                        }
                    ]
                for entry in entries:
                    entry["files"] = {
                        kind: str(Path(path).relative_to(directory))
                        for kind, path in entry.get("files", {}).items()
                    }
                result["human_videos"] = worker.human_video_summary(entries)
                result["human_video_entries"] = entries
            (directory / "result.json").write_text(json.dumps(result))
            bundle = context.scratch / (item.item_id + ".tar.gz")
            with tarfile.open(bundle, "w:gz") as archive:
                for path in sorted(directory.rglob("*")):
                    if path.is_file():
                        archive.add(path, arcname=str(path.relative_to(directory)))
            artifact = context.write_asset(item, "bundle", bundle)
            results.append(ItemResult(item.item_id, Outcome.COMPLETED, (artifact,)))
        return results


class EpisodeSource:
    def __init__(self, record, row, info, manifest):
        self.record, self.row = record, row
        self.identity = {
            "row": row,
            "fps": info["fps"],
            "task": record.episode_task,
            "source": manifest.get("dataset"),
            "revision": manifest.get("dataset_revision"),
        }
        revision = manifest.get("dataset_revision") or fingerprint(self.identity)
        self.dataset_ref = DatasetRef(manifest.get("dataset", "studio-local"), revision)

    def discover(self, stage, store, upstream):
        payload = {}
        identity = dict(self.identity)
        if upstream:
            plan, _ = next(iter(upstream.values()))
            _, result = next(accepted_items(store, plan))
            artifact = result.artifacts[0]
            payload["upstream"] = asdict(artifact)
            identity["upstream"] = {key: value for key, value in asdict(artifact).items() if key != "path"}
        yield InputItem(str(self.record.episode_index), payload, content_digest=fingerprint(identity))


def run_episode(record, row, info, root, manifest, provider, staging_dir, media=None):
    handle = uuid.uuid4().hex
    CONTEXTS[handle] = (record, row, info, root, manifest, provider, media)
    folder = Path(__file__).parent
    scientific = {
        "options": {
            key: manifest.get(key)
            for key in (
                "features",
                "camera",
                "vqa_hz",
                "prompts",
                "model",
                "generation_options",
                "human_video",
                "captioner",
                "captioner_profile",
                "flora_execution",
                "hand_actions",
            )
        },
        "code": {
            path.name: hashlib.sha256(path.read_bytes()).hexdigest()
            for pattern in ("*.py", "*.txt", "pipeline-version.json")
            for path in sorted(folder.glob(pattern))
            if not path.name.startswith("test_")
        },
    }
    stages = [
        StageConfig(
            "language",
            "studio_runtime:StudioStage",
            {"handle": handle, "phase": "language", "scientific": scientific},
        )
    ]
    if media is not None:
        stages.append(
            StageConfig(
                "human_video",
                "studio_runtime:StudioStage",
                {"handle": handle, "phase": "human_video", "scientific": scientific},
                ("language",),
            )
        )
    if manifest.get("hand_actions"):
        stages.append(
            StageConfig(
                "hands",
                "studio_runtime:StudioStage",
                {"handle": handle, "phase": "hands", "scientific": scientific},
                (stages[-1].id,),
            )
        )
    # Existing Studio threads own concurrency; one in-process runtime worker
    # shares the same request budget. Accepted media is never automatically rebilled.
    runtime = RuntimeConfig(
        run_uri=str(staging_dir.parent / "runtime" / f"episode_{record.episode_index:06d}"),
        workers=1,
        batch_size=1,
        shard_size=1,
        max_retries=0,
    )
    try:
        store, completed = run_pipeline(EpisodeSource(record, row, info, manifest), stages, runtime)
        plan, _ = completed[stages[-1].id]
        _, result = next(accepted_items(store, plan))
        directory = staging_dir.parent / "restored" / handle
        directory.mkdir(parents=True, exist_ok=True)
        bundle = store.download(result.artifacts[0], directory / "result.tar.gz")
        unpack(bundle, directory)
        result = json.loads((directory / "result.json").read_text())
        if (directory / "hand_actions.parquet").exists():
            target = staging_dir.parent / "hand_actions" / f"episode_{record.episode_index:06d}.parquet"
            target.parent.mkdir(parents=True, exist_ok=True)
            shutil.copyfile(directory / "hand_actions.parquet", target)
        if (directory / "staging").exists():
            shutil.rmtree(staging_dir / f"episode_{record.episode_index:06d}", ignore_errors=True)
            shutil.copytree(directory / "staging", staging_dir, dirs_exist_ok=True)
        for entry in result.get("human_video_entries", []):
            for kind, relative in entry.get("files", {}).items():
                source = directory / relative
                target = staging_dir.parent / relative
                target.parent.mkdir(parents=True, exist_ok=True)
                shutil.copyfile(source, target)
                entry["files"][kind] = str(target)
        result["runtime"] = {name: summary.plan_id for name, (_, summary) in completed.items()}
        shutil.rmtree(directory)
        return result
    finally:
        CONTEXTS.pop(handle, None)