File size: 42,400 Bytes
bc29ee3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
#!/usr/bin/env python3
"""Evaluate trained one-block Predictors with an FPPF rollout against FFFF.

The evaluation uses the held-out offline prompt shards.  FFFF clean latents
are decoded once and cached as 8-bit RGB reference frames.  For every trained
Predictor, chunk 0 is generated with FFFF (there is no previous chunk), while
chunks 1..6 use Full-Predictor-Predictor-Full.  PSNR, Gaussian SSIM, and
AlexNet LPIPS are computed frame by frame against the matching FFFF video.
"""

from __future__ import annotations

import argparse
import csv
import json
import math
import os
import sys
import time
from pathlib import Path
from typing import Any


def _preparse_gpu() -> str:
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("--gpu", default="2")
    args, _ = parser.parse_known_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu)
    return str(args.gpu)


PHYSICAL_GPU = _preparse_gpu()

import lpips
import torch
import torch.nn.functional as F
from omegaconf import OmegaConf
from safetensors import safe_open
from safetensors.torch import load_file, save_file
from torchvision.io import write_video

REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from pipeline import CausalInferencePipeline
from predictor_training.offline_data import TOKENS_PER_CHUNK
from predictor_training.single_block import (
    SingleBlockPredictor,
    initialize_predictor_block,
)
from scripts.run_single_block_init_sweep import hidden_to_flow
from utils.misc import set_seed
from utils.wan_wrapper import WanDiffusionWrapper, WanVAEWrapper
from wan.modules.model import sinusoidal_embedding_1d


LATENT_CHANNELS = 16
LATENT_HEIGHT = 60
LATENT_WIDTH = 104
FRAMES_PER_CHUNK = 3
NUM_CHUNKS = 7
NUM_DENOISING_STEPS = 4
PIXEL_FRAMES_FIRST_CHUNK = 1 + 4 * (FRAMES_PER_CHUNK - 1)
DEFAULT_PROMPT_IDS = list(range(80, 100))


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--gpu", default=PHYSICAL_GPU)
    parser.add_argument(
        "--config_path",
        type=Path,
        default=Path("configs/self_forcing_sid.yaml"),
    )
    parser.add_argument(
        "--checkpoint_path",
        type=Path,
        default=Path("checkpoints/self_forcing_dmd.pt"),
    )
    parser.add_argument(
        "--dataset_root",
        type=Path,
        default=Path("outputs/predictor_offline_100_all_blocks"),
    )
    parser.add_argument(
        "--sweep_dir",
        type=Path,
        default=Path("outputs/single_block_init_sweep"),
    )
    parser.add_argument(
        "--output_dir",
        type=Path,
        default=Path("outputs/single_block_fppf_eval"),
    )
    parser.add_argument(
        "--prompt_ids", type=int, nargs="*", default=DEFAULT_PROMPT_IDS
    )
    parser.add_argument(
        "--experiments",
        nargs="*",
        default=None,
        help="Experiment directory names. Omit to evaluate all summary rows.",
    )
    parser.add_argument("--max_prompts", type=int, default=None)
    parser.add_argument("--max_experiments", type=int, default=None)
    parser.add_argument("--metric_batch_size", type=int, default=4)
    parser.add_argument("--generation_seed", type=int, default=0)
    parser.add_argument(
        "--verify_ffff",
        action=argparse.BooleanOptionalAction,
        default=True,
        help="Re-run FFFF once and compare its latent exactly to offline data.",
    )
    parser.add_argument(
        "--skip_lpips",
        action=argparse.BooleanOptionalAction,
        default=False,
        help="Only for quick diagnostics; formal evaluation should keep LPIPS.",
    )
    parser.add_argument(
        "--rebuild_references",
        action=argparse.BooleanOptionalAction,
        default=False,
    )
    parser.add_argument(
        "--save_videos",
        action=argparse.BooleanOptionalAction,
        default=False,
        help="Save each FPPF prediction as a 16-fps H.264 MP4 for VBench.",
    )
    args = parser.parse_args()
    if args.metric_batch_size < 1:
        parser.error("--metric_batch_size must be positive")
    if not args.prompt_ids:
        parser.error("At least one prompt ID is required")
    if any(value < 0 or value >= 100 for value in args.prompt_ids):
        parser.error("Prompt IDs must be in [0, 99]")
    return args


def resolve(path: Path) -> Path:
    path = path.expanduser()
    return path.resolve() if path.is_absolute() else (REPO_ROOT / path).resolve()


def atomic_json(path: Path, value: Any) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_suffix(path.suffix + ".tmp")
    temporary.write_text(
        json.dumps(value, indent=2, ensure_ascii=False, allow_nan=True) + "\n",
        encoding="utf-8",
    )
    os.replace(temporary, path)


def atomic_safetensors(
    path: Path, tensors: dict[str, torch.Tensor], metadata: dict[str, str]
) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_suffix(path.suffix + ".tmp")
    save_file(tensors, temporary, metadata=metadata)
    os.replace(temporary, path)


def load_prompt_metadata(dataset_root: Path, prompt_id: int) -> dict[str, Any]:
    path = dataset_root / f"prompt_{prompt_id:04d}" / "metadata.json"
    return json.loads(path.read_text(encoding="utf-8"))


def load_ffff_latent(dataset_root: Path, prompt_id: int) -> torch.Tensor:
    path = dataset_root / f"prompt_{prompt_id:04d}" / "trajectory.safetensors"
    with safe_open(path, framework="pt", device="cpu") as handle:
        chunks = [
            handle.get_tensor(f"chunk_{chunk:02d}_clean_latent")
            for chunk in range(NUM_CHUNKS)
        ]
    return torch.cat(chunks, dim=1).contiguous()


def pixels_to_u8(video: torch.Tensor) -> torch.Tensor:
    """Convert [1,T,3,H,W] pixels in [-1,1] to CPU uint8 frames."""
    return (
        ((video.squeeze(0).float() + 1.0) * 127.5)
        .round_()
        .clamp_(0, 255)
        .to(device="cpu", dtype=torch.uint8)
        .contiguous()
    )


def save_mp4(frames: torch.Tensor, path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    write_video(
        str(path), frames.permute(0, 2, 3, 1), fps=16,
        video_codec="libx264", options={"crf": "18"},
    )


@torch.inference_mode()
def prepare_reference_frames(
    *,
    vae: WanVAEWrapper,
    dataset_root: Path,
    output_dir: Path,
    prompt_ids: list[int],
    device: torch.device,
    rebuild: bool,
) -> None:
    reference_dir = output_dir / "ffff_reference_frames"
    reference_dir.mkdir(parents=True, exist_ok=True)
    for offset, prompt_id in enumerate(prompt_ids, start=1):
        destination = reference_dir / f"prompt_{prompt_id:04d}.safetensors"
        if destination.exists() and not rebuild:
            print(
                f"[reference] {offset}/{len(prompt_ids)} prompt={prompt_id} cached",
                flush=True,
            )
            continue
        latent = load_ffff_latent(dataset_root, prompt_id).to(
            device=device, dtype=torch.bfloat16
        )
        started = time.perf_counter()
        with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
            pixels = vae.decode_to_pixel(latent, use_cache=False)
        frames = pixels_to_u8(pixels)
        atomic_safetensors(
            destination,
            {"frames": frames},
            {
                "reference": "FFFF",
                "prompt_id": str(prompt_id),
                "range": "uint8_0_255",
                "layout": "TCHW",
            },
        )
        if hasattr(vae.model, "clear_cache"):
            vae.model.clear_cache()
        del latent, pixels, frames
        torch.cuda.empty_cache()
        print(
            f"[reference] {offset}/{len(prompt_ids)} prompt={prompt_id} "
            f"decoded={time.perf_counter() - started:.1f}s",
            flush=True,
        )


def load_reference_frames(output_dir: Path, prompt_id: int) -> torch.Tensor:
    path = output_dir / "ffff_reference_frames" / f"prompt_{prompt_id:04d}.safetensors"
    with safe_open(path, framework="pt", device="cpu") as handle:
        return handle.get_tensor("frames")


def discover_experiments(
    sweep_dir: Path,
    requested: list[str] | None,
    max_experiments: int | None,
) -> list[dict[str, Any]]:
    summary_path = sweep_dir / "summary.csv"
    with summary_path.open("r", encoding="utf-8", newline="") as handle:
        rows = list(csv.DictReader(handle))
    by_name = {row["name"]: row for row in rows}
    names = list(by_name) if requested is None else requested
    unknown = [name for name in names if name not in by_name]
    if unknown:
        raise KeyError(f"Unknown sweep experiments: {unknown}")
    if max_experiments is not None:
        names = names[:max_experiments]

    experiments = []
    for name in names:
        run_dir = sweep_dir / name
        config = json.loads((run_dir / "config.json").read_text(encoding="utf-8"))
        weights = run_dir / "predictor_final.safetensors"
        if not weights.exists():
            raise FileNotFoundError(weights)
        row = by_name[name]
        experiment = {
                "name": name,
                "initialization_method": config["initialization_method"],
                "source_layer": int(config["source_layer"]),
                "weights": weights,
                "gate_mode": config.get("gate_mode", "baseline"),
                "gate_hidden_dim": int(config.get("gate_hidden_dim", 128)),
                "gate_initial_bias": float(
                    config.get("gate_initial_bias", 4.6)
                ),
                "gate_floor": float(config.get("gate_floor", 0.0)),
                "constant_gate": float(config.get("constant_gate", 1.0)),
                "gate_override": config.get("gate_override"),
                "offline_final_val_flow_mse": float(row["final_val_flow_mse"]),
                "offline_final_val_hidden_mse": float(row["final_val_hidden_mse"]),
            }
        experiments.append(experiment)
        if experiment["gate_mode"] == "learned":
            training_metrics = json.loads(
                (run_dir / "metrics.json").read_text(encoding="utf-8")
            )
            gate_mean = float(training_metrics["evaluations"][-1]["gate_mean"])
            experiments.append(
                {
                    **experiment,
                    "name": f"{name}_constant_mean",
                    "gate_override": gate_mean,
                    "constant_gate": gate_mean,
                }
            )
    return experiments


def build_pipeline(
    config: Any,
    checkpoint_path: Path,
    vae: WanVAEWrapper,
    device: torch.device,
) -> CausalInferencePipeline:
    generator = WanDiffusionWrapper(
        **getattr(config, "model_kwargs", {}), is_causal=True
    )
    pipeline = CausalInferencePipeline(
        config,
        device=device,
        generator=generator,
        text_encoder=torch.nn.Identity(),
        vae=vae,
    )
    checkpoint = torch.load(
        checkpoint_path, map_location="cpu", weights_only=False, mmap=True
    )
    if set(checkpoint) != {"generator_ema"}:
        raise KeyError(f"Unexpected Teacher checkpoint keys: {sorted(checkpoint)}")
    pipeline.generator.load_state_dict(checkpoint["generator_ema"], strict=True)
    del checkpoint
    pipeline.to(dtype=torch.bfloat16)
    pipeline.generator.to(device=device)
    pipeline.eval()
    pipeline.generator.requires_grad_(False)
    return pipeline


def reset_kv_and_load_cross_cache(
    pipeline: CausalInferencePipeline,
    dataset_root: Path,
    prompt_id: int,
    device: torch.device,
) -> None:
    if pipeline.kv_cache1 is None:
        pipeline._initialize_kv_cache(1, torch.bfloat16, device)
        pipeline._initialize_crossattn_cache(1, torch.bfloat16, device)
    for cache in pipeline.kv_cache1:
        cache["global_end_index"].zero_()
        cache["local_end_index"].zero_()

    cross_path = (
        dataset_root / f"prompt_{prompt_id:04d}" / "cross_attention.safetensors"
    )
    with safe_open(cross_path, framework="pt", device="cpu") as handle:
        for layer, cache in enumerate(pipeline.crossattn_cache):
            cache["k"] = handle.get_tensor(f"block_{layer:02d}_k").to(
                device=device, dtype=torch.bfloat16
            )
            cache["v"] = handle.get_tensor(f"block_{layer:02d}_v").to(
                device=device, dtype=torch.bfloat16
            )
            cache["is_init"] = True


class FinalHiddenCapture:
    def __init__(self, teacher: torch.nn.Module) -> None:
        self.enabled = False
        self.value: torch.Tensor | None = None
        self.handle = teacher.head.register_forward_pre_hook(self._hook)

    def close(self) -> None:
        self.handle.remove()

    def _hook(
        self, _module: torch.nn.Module, inputs: tuple[torch.Tensor, ...]
    ) -> None:
        if self.enabled:
            if self.value is not None:
                raise RuntimeError("Teacher head was called twice in one Full step")
            self.value = inputs[0].detach()

    def start(self) -> None:
        self.value = None
        self.enabled = True

    def finish(self) -> torch.Tensor:
        self.enabled = False
        if self.value is None:
            raise RuntimeError("Teacher final hidden was not captured")
        value = self.value
        self.value = None
        return value


def load_predictor(
    teacher: torch.nn.Module,
    experiment: dict[str, Any],
    device: torch.device,
) -> SingleBlockPredictor:
    source_layer = int(experiment["source_layer"])
    block = initialize_predictor_block(
        teacher.blocks[source_layer], "teacher_full"
    )
    predictor = SingleBlockPredictor(
        block=block,
        dim=teacher.dim,
        gradient_checkpointing=False,
        input_variant=experiment.get("input_variant", "self_forcing"),
        gate_mode=experiment.get("gate_mode", "baseline"),
        gate_hidden_dim=int(experiment.get("gate_hidden_dim", 128)),
        gate_initial_bias=float(experiment.get("gate_initial_bias", 4.6)),
        gate_floor=float(experiment.get("gate_floor", 0.0)),
        constant_gate=float(experiment.get("constant_gate", 1.0)),
        atc_previous_scope=experiment.get("atc_previous_scope", "chunk"),
        atc_freq_dim=int(experiment.get("atc_freq_dim", 256)),
        atc_mlp_hidden_dim=int(experiment.get("atc_mlp_hidden_dim", 3072)),
        atc_gate_hidden_dim=int(experiment.get("atc_gate_hidden_dim", 512)),
        atc_transport_residual_scale=float(
            experiment.get("atc_transport_residual_scale", 0.1)
        ),
        atc_gate_initial_probability=float(
            experiment.get("atc_gate_initial_probability", 0.3)
        ),
        atc_collect_diagnostics=bool(
            experiment.get("atc_collect_diagnostics", False)
        ),
    )
    state = load_file(str(experiment["weights"]), device="cpu")
    predictor.load_state_dict(state, strict=True)
    if experiment.get("gate_override") is not None:
        predictor.fusion.gate_override = float(experiment["gate_override"])
    predictor.to(device=device)
    predictor.eval().requires_grad_(False)
    return predictor


@torch.inference_mode()
def predictor_step(
    *,
    predictor: SingleBlockPredictor,
    teacher: torch.nn.Module,
    noisy_input: torch.Tensor,
    timestep: torch.Tensor,
    anchor_hidden: torch.Tensor,
    previous_hidden: torch.Tensor,
    history_cache: dict[str, torch.Tensor],
    cross_cache: dict[str, torch.Tensor],
    current_start: int,
    anchor_timestep: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
        current_tokens = teacher.patch_embedding(
            noisy_input.permute(0, 2, 1, 3, 4)
        ).flatten(2).transpose(1, 2)
        time_embedding = teacher.time_embedding(
            sinusoidal_embedding_1d(
                teacher.freq_dim, timestep.flatten()
            ).type_as(current_tokens)
        )
        timestep_modulation = teacher.time_projection(
            time_embedding
        ).unflatten(1, (6, teacher.dim)).unflatten(
            dim=0, sizes=timestep.shape
        )
        head_embedding = time_embedding.unflatten(
            dim=0, sizes=timestep.shape
        ).unsqueeze(2)
        condition_per_frame = time_embedding.unflatten(
            dim=0, sizes=timestep.shape
        )
        condition_tokens = (
            condition_per_frame[:, :, None, :]
            .expand(
                timestep.shape[0],
                timestep.shape[1],
                30 * 52,
                teacher.dim,
            )
            .reshape(timestep.shape[0], -1, teacher.dim)
        )
        anchor_distance = None
        if predictor.input_variant == "atc":
            if anchor_timestep is None:
                raise ValueError("ATC inference requires anchor_timestep")
            anchor_distance = (
                timestep.float() - anchor_timestep.float()
            ).abs().mean(dim=1)
        grid_sizes = torch.tensor(
            [[FRAMES_PER_CHUNK, 30, 52]], dtype=torch.long, device="cpu"
        )
        history_length = int(history_cache["local_end_index"].item())
        pred_hidden = predictor(
            current_tokens=current_tokens,
            anchor_hidden=anchor_hidden,
            previous_hidden=previous_hidden,
            timestep_modulation=timestep_modulation,
            grid_sizes=grid_sizes,
            freqs=teacher.freqs,
            history_k=history_cache["k"][:, :history_length],
            history_v=history_cache["v"][:, :history_length],
            cross_k=cross_cache["k"],
            cross_v=cross_cache["v"],
            current_start=current_start,
            condition_tokens=condition_tokens,
            anchor_distance=anchor_distance,
        )
        pred_flow = hidden_to_flow(
            pred_hidden, head_embedding, grid_sizes, teacher
        )
    return pred_hidden, pred_flow, current_tokens


@torch.inference_mode()
def generate_rollout(
    *,
    pipeline: CausalInferencePipeline,
    dataset_root: Path,
    prompt_id: int,
    generation_seed: int,
    device: torch.device,
    predictor: SingleBlockPredictor | None,
    source_layer: int | None,
    schedule: str,
) -> tuple[torch.Tensor, dict[str, float | int]]:
    if schedule not in {"FFFF", "FPPF"}:
        raise ValueError(schedule)
    if schedule == "FPPF" and (predictor is None or source_layer is None):
        raise ValueError("FPPF requires a Predictor and source layer")

    reset_kv_and_load_cross_cache(pipeline, dataset_root, prompt_id, device)
    set_seed(generation_seed)
    noise = torch.randn(
        1,
        NUM_CHUNKS * FRAMES_PER_CHUNK,
        LATENT_CHANNELS,
        LATENT_HEIGHT,
        LATENT_WIDTH,
        dtype=torch.bfloat16,
        device=device,
    )
    teacher = pipeline.generator.model
    text_dim = int(teacher.text_embedding[0].in_features)
    conditional_dict = {
        "prompt_embeds": torch.zeros(
            1, 1, text_dim, dtype=torch.bfloat16, device=device
        )
    }
    timesteps = pipeline.denoising_step_list.to(device=device)
    output_chunks: list[torch.Tensor] = []
    previous_chunk_hidden: list[torch.Tensor | None] | None = None
    capture = FinalHiddenCapture(teacher)
    full_calls = 0
    predictor_calls = 0
    started = time.perf_counter()

    try:
        for chunk in range(NUM_CHUNKS):
            noisy_input = noise[
                :, chunk * FRAMES_PER_CHUNK : (chunk + 1) * FRAMES_PER_CHUNK
            ]
            current_hidden: list[torch.Tensor | None] = [None] * NUM_DENOISING_STEPS
            denoised_pred: torch.Tensor | None = None
            timestep: torch.Tensor | None = None
            for step, current_timestep in enumerate(timesteps):
                timestep = torch.ones(
                    [1, FRAMES_PER_CHUNK], dtype=torch.int64, device=device
                ) * current_timestep
                use_predictor = schedule == "FPPF" and chunk > 0 and step in {1, 2}

                if use_predictor:
                    anchor_hidden = current_hidden[step - 1]
                    assert anchor_hidden is not None
                    assert previous_chunk_hidden is not None
                    previous_hidden = previous_chunk_hidden[step]
                    assert previous_hidden is not None
                    history = pipeline.kv_cache1[int(source_layer)]
                    cross = pipeline.crossattn_cache[int(source_layer)]
                    pred_hidden, flow, _ = predictor_step(
                        predictor=predictor,
                        teacher=teacher,
                        noisy_input=noisy_input,
                        timestep=timestep,
                        anchor_hidden=anchor_hidden,
                        previous_hidden=previous_hidden,
                        history_cache=history,
                        cross_cache=cross,
                        current_start=chunk * TOKENS_PER_CHUNK,
                        anchor_timestep=(
                            torch.ones_like(timestep) * timesteps[step - 1]
                        ),
                    )
                    denoised_pred = pipeline.generator._convert_flow_pred_to_x0(
                        flow_pred=flow.flatten(0, 1),
                        xt=noisy_input.flatten(0, 1),
                        timestep=timestep.flatten(0, 1),
                    ).unflatten(0, flow.shape[:2])
                    current_hidden[step] = pred_hidden
                    predictor_calls += 1
                else:
                    capture.start()
                    _, denoised_pred = pipeline.generator(
                        noisy_image_or_video=noisy_input,
                        conditional_dict=conditional_dict,
                        timestep=timestep,
                        kv_cache=pipeline.kv_cache1,
                        crossattn_cache=pipeline.crossattn_cache,
                        current_start=chunk * TOKENS_PER_CHUNK,
                    )
                    current_hidden[step] = capture.finish()
                    full_calls += 1

                if step < NUM_DENOISING_STEPS - 1:
                    next_timestep = timesteps[step + 1]
                    denoised_flat = denoised_pred.flatten(0, 1)
                    noisy_input = pipeline.scheduler.add_noise(
                        denoised_flat,
                        torch.randn_like(denoised_flat),
                        next_timestep
                        * torch.ones(
                            [FRAMES_PER_CHUNK], dtype=torch.long, device=device
                        ),
                    ).unflatten(0, denoised_pred.shape[:2])

            if denoised_pred is None or timestep is None:
                raise RuntimeError("Denoising loop produced no clean latent")
            output_chunks.append(denoised_pred)

            context_timestep = torch.ones_like(timestep) * pipeline.args.context_noise
            pipeline.generator(
                noisy_image_or_video=denoised_pred,
                conditional_dict=conditional_dict,
                timestep=context_timestep,
                kv_cache=pipeline.kv_cache1,
                crossattn_cache=pipeline.crossattn_cache,
                current_start=chunk * TOKENS_PER_CHUNK,
            )
            previous_chunk_hidden = current_hidden
    finally:
        capture.close()

    torch.cuda.synchronize()
    return torch.cat(output_chunks, dim=1), {
        "generation_time_s": time.perf_counter() - started,
        "full_calls": full_calls,
        "predictor_calls": predictor_calls,
    }


def gaussian_kernel(
    device: torch.device, dtype: torch.dtype, channels: int = 3
) -> torch.Tensor:
    coordinates = torch.arange(11, device=device, dtype=dtype) - 5
    kernel_1d = torch.exp(-(coordinates.square()) / (2 * 1.5**2))
    kernel_1d /= kernel_1d.sum()
    kernel_2d = torch.outer(kernel_1d, kernel_1d)
    return kernel_2d.expand(channels, 1, 11, 11).contiguous()


def ssim_per_frame(
    reference: torch.Tensor, prediction: torch.Tensor, kernel: torch.Tensor
) -> torch.Tensor:
    channels = reference.shape[1]
    mu_x = F.conv2d(reference, kernel, groups=channels)
    mu_y = F.conv2d(prediction, kernel, groups=channels)
    mu_x2 = mu_x.square()
    mu_y2 = mu_y.square()
    mu_xy = mu_x * mu_y
    sigma_x2 = F.conv2d(reference.square(), kernel, groups=channels) - mu_x2
    sigma_y2 = F.conv2d(prediction.square(), kernel, groups=channels) - mu_y2
    sigma_xy = F.conv2d(reference * prediction, kernel, groups=channels) - mu_xy
    c1 = 0.01**2
    c2 = 0.03**2
    score = ((2 * mu_xy + c1) * (2 * sigma_xy + c2)) / (
        (mu_x2 + mu_y2 + c1) * (sigma_x2 + sigma_y2 + c2)
    )
    return score.mean(dim=(1, 2, 3))


@torch.inference_mode()
def frame_metrics(
    *,
    reference_u8: torch.Tensor,
    prediction_u8: torch.Tensor,
    lpips_model: torch.nn.Module | None,
    batch_size: int,
    device: torch.device,
) -> dict[str, Any]:
    if reference_u8.shape != prediction_u8.shape:
        raise ValueError(
            f"Reference/prediction shapes differ: {reference_u8.shape}, "
            f"{prediction_u8.shape}"
        )
    kernel = gaussian_kernel(device, torch.float32)
    psnr_values: list[float] = []
    mse_values: list[float] = []
    ssim_values: list[float] = []
    lpips_values: list[float] = []
    for start in range(0, reference_u8.shape[0], batch_size):
        end = min(start + batch_size, reference_u8.shape[0])
        reference = reference_u8[start:end].to(
            device=device, dtype=torch.float32
        ) / 255.0
        prediction = prediction_u8[start:end].to(
            device=device, dtype=torch.float32
        ) / 255.0
        mse = (reference - prediction).square().mean(dim=(1, 2, 3))
        psnr = -10.0 * torch.log10(mse.clamp_min(1e-12))
        ssim = ssim_per_frame(reference, prediction, kernel)
        mse_values.extend(float(value) for value in mse.cpu())
        psnr_values.extend(float(value) for value in psnr.cpu())
        ssim_values.extend(float(value) for value in ssim.cpu())
        if lpips_model is not None:
            distance = lpips_model(
                reference.mul(2).sub(1), prediction.mul(2).sub(1)
            ).flatten()
            lpips_values.extend(float(value) for value in distance.cpu())
        del reference, prediction, mse, psnr, ssim
    global_mse = sum(mse_values) / len(mse_values)
    rollout_mse = sum(mse_values[PIXEL_FRAMES_FIRST_CHUNK:]) / len(
        mse_values[PIXEL_FRAMES_FIRST_CHUNK:]
    )
    return {
        "mse_per_frame": mse_values,
        "psnr_per_frame": psnr_values,
        "ssim_per_frame": ssim_values,
        "lpips_per_frame": lpips_values,
        "pixel_mse": global_mse,
        "psnr": -10.0 * math.log10(max(global_mse, 1e-12)),
        "psnr_frame_mean": sum(psnr_values) / len(psnr_values),
        "ssim": sum(ssim_values) / len(ssim_values),
        "lpips": (
            sum(lpips_values) / len(lpips_values)
            if lpips_values
            else None
        ),
        "rollout_start_frame": PIXEL_FRAMES_FIRST_CHUNK,
        "rollout_pixel_mse": rollout_mse,
        "rollout_psnr": -10.0 * math.log10(max(rollout_mse, 1e-12)),
        "rollout_ssim": sum(ssim_values[PIXEL_FRAMES_FIRST_CHUNK:])
        / len(ssim_values[PIXEL_FRAMES_FIRST_CHUNK:]),
        "rollout_lpips": (
            sum(lpips_values[PIXEL_FRAMES_FIRST_CHUNK:])
            / len(lpips_values[PIXEL_FRAMES_FIRST_CHUNK:])
            if lpips_values
            else None
        ),
        "num_frames": len(psnr_values),
    }


def mean_std(values: list[float]) -> tuple[float, float]:
    mean = sum(values) / len(values)
    variance = sum((value - mean) ** 2 for value in values) / len(values)
    return mean, math.sqrt(variance)


def aggregate_prompt_results(
    experiment: dict[str, Any], prompt_results: list[dict[str, Any]]
) -> dict[str, Any]:
    mse_frames = [
        value
        for result in prompt_results
        for value in result["mse_per_frame"]
    ]
    psnr_frames = [
        value
        for result in prompt_results
        for value in result["psnr_per_frame"]
    ]
    ssim_frames = [
        value
        for result in prompt_results
        for value in result["ssim_per_frame"]
    ]
    lpips_frames = [
        value
        for result in prompt_results
        for value in result["lpips_per_frame"]
    ]
    rollout_mse_frames = [
        value
        for result in prompt_results
        for value in result["mse_per_frame"][PIXEL_FRAMES_FIRST_CHUNK:]
    ]
    rollout_ssim_frames = [
        value
        for result in prompt_results
        for value in result["ssim_per_frame"][PIXEL_FRAMES_FIRST_CHUNK:]
    ]
    rollout_lpips_frames = [
        value
        for result in prompt_results
        for value in result["lpips_per_frame"][PIXEL_FRAMES_FIRST_CHUNK:]
    ]
    pixel_mse = sum(mse_frames) / len(mse_frames)
    psnr_frame_mean, psnr_std = mean_std(psnr_frames)
    ssim, ssim_std = mean_std(ssim_frames)
    if lpips_frames:
        lpips_mean, lpips_std = mean_std(lpips_frames)
    else:
        lpips_mean, lpips_std = None, None
    rollout_pixel_mse = sum(rollout_mse_frames) / len(rollout_mse_frames)
    rollout_ssim = sum(rollout_ssim_frames) / len(rollout_ssim_frames)
    rollout_lpips = (
        sum(rollout_lpips_frames) / len(rollout_lpips_frames)
        if rollout_lpips_frames
        else None
    )
    return {
        "status": "complete",
        "name": experiment["name"],
        "initialization_method": experiment["initialization_method"],
        "source_layer": experiment["source_layer"],
        "offline_final_val_flow_mse": experiment["offline_final_val_flow_mse"],
        "offline_final_val_hidden_mse": experiment[
            "offline_final_val_hidden_mse"
        ],
        "schedule": "chunk0=FFFF; chunks1-6=FPPF",
        "reference": "matching FFFF, same prompt and seed",
        "pixel_domain": "VAE-decoded RGB, rounded to uint8",
        "aggregation": (
            "PSNR from global pixel MSE; SSIM/LPIPS mean over decoded frames"
        ),
        "num_prompts": len(prompt_results),
        "num_frames": len(psnr_frames),
        "pixel_mse": pixel_mse,
        "psnr": -10.0 * math.log10(max(pixel_mse, 1e-12)),
        "psnr_frame_mean": psnr_frame_mean,
        "psnr_frame_std": psnr_std,
        "ssim": ssim,
        "ssim_frame_std": ssim_std,
        "lpips": lpips_mean,
        "lpips_frame_std": lpips_std,
        "rollout_start_frame": PIXEL_FRAMES_FIRST_CHUNK,
        "rollout_num_frames": len(rollout_mse_frames),
        "rollout_pixel_mse": rollout_pixel_mse,
        "rollout_psnr": -10.0
        * math.log10(max(rollout_pixel_mse, 1e-12)),
        "rollout_ssim": rollout_ssim,
        "rollout_lpips": rollout_lpips,
        "mean_generation_time_s": sum(
            result["generation_time_s"] for result in prompt_results
        )
        / len(prompt_results),
        "full_calls_per_prompt": prompt_results[0]["full_calls"],
        "predictor_calls_per_prompt": prompt_results[0]["predictor_calls"],
        "prompt_ids": [result["prompt_id"] for result in prompt_results],
    }


def write_summary(
    output_dir: Path, experiments: list[dict[str, Any]]
) -> None:
    rows: list[dict[str, Any]] = []
    for experiment in experiments:
        path = output_dir / experiment["name"] / "metrics.json"
        if not path.exists():
            continue
        metrics = json.loads(path.read_text(encoding="utf-8"))
        if metrics.get("status") != "complete":
            continue
        rows.append(
            {
                "name": metrics["name"],
                "initialization_method": metrics["initialization_method"],
                "source_layer": metrics["source_layer"],
                "num_prompts": metrics["num_prompts"],
                "num_frames": metrics["num_frames"],
                "psnr": metrics["psnr"],
                "ssim": metrics["ssim"],
                "lpips": metrics["lpips"],
                "rollout_psnr": metrics["rollout_psnr"],
                "rollout_ssim": metrics["rollout_ssim"],
                "rollout_lpips": metrics["rollout_lpips"],
                "offline_final_val_flow_mse": metrics[
                    "offline_final_val_flow_mse"
                ],
                "mean_generation_time_s": metrics["mean_generation_time_s"],
            }
        )
    rows.sort(key=lambda row: float(row["lpips"] or math.inf))
    fields = [
        "name",
        "initialization_method",
        "source_layer",
        "num_prompts",
        "num_frames",
        "psnr",
        "ssim",
        "lpips",
        "rollout_psnr",
        "rollout_ssim",
        "rollout_lpips",
        "offline_final_val_flow_mse",
        "mean_generation_time_s",
    ]
    destination = output_dir / "summary.csv"
    temporary = destination.with_suffix(".csv.tmp")
    with temporary.open("w", encoding="utf-8", newline="") as handle:
        writer = csv.DictWriter(handle, fieldnames=fields)
        writer.writeheader()
        writer.writerows(rows)
    os.replace(temporary, destination)


def load_completed_prompt_results(
    run_dir: Path, prompt_ids: list[int]
) -> list[dict[str, Any]]:
    results = []
    for prompt_id in prompt_ids:
        path = run_dir / "per_prompt" / f"prompt_{prompt_id:04d}.json"
        if path.exists():
            results.append(json.loads(path.read_text(encoding="utf-8")))
    return results


def main() -> None:
    args = parse_args()
    args.config_path = resolve(args.config_path)
    args.checkpoint_path = resolve(args.checkpoint_path)
    args.dataset_root = resolve(args.dataset_root)
    args.sweep_dir = resolve(args.sweep_dir)
    args.output_dir = resolve(args.output_dir)
    args.output_dir.mkdir(parents=True, exist_ok=True)

    prompt_ids = sorted(set(args.prompt_ids))
    if args.max_prompts is not None:
        prompt_ids = prompt_ids[: args.max_prompts]
    experiments = discover_experiments(
        args.sweep_dir, args.experiments, args.max_experiments
    )
    device = torch.device("cuda")
    torch.set_grad_enabled(False)
    set_seed(args.generation_seed)

    config = OmegaConf.merge(
        OmegaConf.load(REPO_ROOT / "configs/default_config.yaml"),
        OmegaConf.load(args.config_path),
    )
    manifest = {
        "status": "running",
        "gpu": str(args.gpu),
        "config_path": str(args.config_path),
        "checkpoint_path": str(args.checkpoint_path),
        "dataset_root": str(args.dataset_root),
        "sweep_dir": str(args.sweep_dir),
        "prompt_ids": prompt_ids,
        "generation_seed_reset_per_prompt": args.generation_seed,
        "experiments": [item["name"] for item in experiments],
        "fppf_definition": "chunk0=FFFF; chunks1-6=FPPF",
        "reference": "offline FFFF clean latents from the same prompt/seed",
        "metrics": {
            "psnr": "RGB PSNR from global pixel MSE",
            "ssim": "11x11 Gaussian sigma=1.5 RGB SSIM, then frame mean",
            "lpips": "AlexNet LPIPS on RGB [-1,1], then frame mean",
            "pixel_quantization": "both inputs rounded to uint8",
            "rollout_only": (
                "also reported for decoded frames 9..80 after the FFFF-only "
                "first chunk"
            ),
        },
    }
    atomic_json(args.output_dir / "manifest.json", manifest)

    print("[setup] loading VAE and preparing FFFF reference frames", flush=True)
    vae = WanVAEWrapper().to(device=device, dtype=torch.bfloat16).eval()
    prepare_reference_frames(
        vae=vae,
        dataset_root=args.dataset_root,
        output_dir=args.output_dir,
        prompt_ids=prompt_ids,
        device=device,
        rebuild=args.rebuild_references,
    )

    print("[setup] loading frozen generator_ema", flush=True)
    pipeline = build_pipeline(config, args.checkpoint_path, vae, device)
    teacher = pipeline.generator.model
    lpips_model = None
    if not args.skip_lpips:
        print("[setup] loading AlexNet LPIPS", flush=True)
        lpips_model = lpips.LPIPS(net="alex", verbose=False).to(device).eval()
        lpips_model.requires_grad_(False)

    if args.verify_ffff:
        prompt_id = prompt_ids[0]
        print(f"[verify] reproducing FFFF prompt={prompt_id}", flush=True)
        reproduced, counts = generate_rollout(
            pipeline=pipeline,
            dataset_root=args.dataset_root,
            prompt_id=prompt_id,
            generation_seed=args.generation_seed,
            device=device,
            predictor=None,
            source_layer=None,
            schedule="FFFF",
        )
        expected = load_ffff_latent(args.dataset_root, prompt_id).to(
            device=device, dtype=torch.bfloat16
        )
        difference = reproduced.float() - expected.float()
        verification = {
            "prompt_id": prompt_id,
            "max_abs_latent_error": float(difference.abs().max()),
            "latent_mse": float(difference.square().mean()),
            **counts,
        }
        atomic_json(args.output_dir / "ffff_reproduction.json", verification)
        print(f"[verify] {verification}", flush=True)
        if verification["max_abs_latent_error"] > 1e-3:
            raise RuntimeError(
                "FFFF reproduction differs from offline reference; refusing "
                "to evaluate FPPF with unmatched randomness/caches"
            )
        del reproduced, expected, difference
        torch.cuda.empty_cache()

    for experiment_index, experiment in enumerate(experiments, start=1):
        run_dir = args.output_dir / experiment["name"]
        run_dir.mkdir(parents=True, exist_ok=True)
        metrics_path = run_dir / "metrics.json"
        if metrics_path.exists():
            existing = json.loads(metrics_path.read_text(encoding="utf-8"))
            if (
                existing.get("status") == "complete"
                and existing.get("prompt_ids") == prompt_ids
                and (args.skip_lpips or existing.get("lpips") is not None)
            ):
                print(
                    f"[run] {experiment_index}/{len(experiments)} "
                    f"skip complete {experiment['name']}",
                    flush=True,
                )
                continue

        print(
            f"[run] {experiment_index}/{len(experiments)} "
            f"{experiment['name']} source={experiment['source_layer']}",
            flush=True,
        )
        predictor = load_predictor(teacher, experiment, device)
        existing_results = {
            result["prompt_id"]: result
            for result in load_completed_prompt_results(run_dir, prompt_ids)
        }

        for prompt_index, prompt_id in enumerate(prompt_ids, start=1):
            video_path = run_dir / "videos" / f"prompt_{prompt_id:04d}.mp4"
            if prompt_id in existing_results and (
                not args.save_videos or video_path.exists()
            ):
                print(
                    f"[prompt] {experiment['name']} {prompt_index}/{len(prompt_ids)} "
                    f"id={prompt_id} cached",
                    flush=True,
                )
                continue
            started = time.perf_counter()
            latent, counts = generate_rollout(
                pipeline=pipeline,
                dataset_root=args.dataset_root,
                prompt_id=prompt_id,
                generation_seed=args.generation_seed,
                device=device,
                predictor=predictor,
                source_layer=experiment["source_layer"],
                schedule="FPPF",
            )
            with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
                pixels = vae.decode_to_pixel(latent, use_cache=False)
            prediction_u8 = pixels_to_u8(pixels)
            if args.save_videos:
                save_mp4(prediction_u8, video_path)
            reference_u8 = load_reference_frames(args.output_dir, prompt_id)
            metrics = frame_metrics(
                reference_u8=reference_u8,
                prediction_u8=prediction_u8,
                lpips_model=lpips_model,
                batch_size=args.metric_batch_size,
                device=device,
            )
            prompt_result = {
                "prompt_id": prompt_id,
                "prompt": load_prompt_metadata(args.dataset_root, prompt_id)[
                    "prompt"
                ],
                **counts,
                **metrics,
                "total_time_s": time.perf_counter() - started,
            }
            atomic_json(
                run_dir / "per_prompt" / f"prompt_{prompt_id:04d}.json",
                prompt_result,
            )
            existing_results[prompt_id] = prompt_result
            print(
                f"[prompt] {experiment['name']} {prompt_index}/{len(prompt_ids)} "
                f"id={prompt_id} psnr={metrics['psnr']:.4f} "
                f"ssim={metrics['ssim']:.6f} "
                f"lpips={metrics['lpips']} "
                f"time={prompt_result['total_time_s']:.1f}s",
                flush=True,
            )
            if hasattr(vae.model, "clear_cache"):
                vae.model.clear_cache()
            del latent, pixels, prediction_u8, reference_u8
            torch.cuda.empty_cache()

        prompt_results = [existing_results[prompt_id] for prompt_id in prompt_ids]
        aggregate = aggregate_prompt_results(experiment, prompt_results)
        atomic_json(metrics_path, aggregate)
        write_summary(args.output_dir, experiments)
        print(
            f"[result] {experiment['name']} psnr={aggregate['psnr']:.4f} "
            f"ssim={aggregate['ssim']:.6f} lpips={aggregate['lpips']}",
            flush=True,
        )
        del predictor
        torch.cuda.empty_cache()

    manifest["status"] = "complete"
    atomic_json(args.output_dir / "manifest.json", manifest)
    write_summary(args.output_dir, experiments)
    print(
        f"[complete] {len(experiments)} experiments -> "
        f"{args.output_dir / 'summary.csv'}",
        flush=True,
    )


if __name__ == "__main__":
    main()