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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
# Copyright 2024 MAGI Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import sys
import argparse
import csv
import subprocess
from pathlib import Path

import multiprocessing as mp

# Constants
DEFAULT_BASE_PORT = 29510
PHYSICSIQ_FPS = 24


def resolve_gpu_ids(gpus_config) -> list[int]:
    """Resolve explicit GPU IDs or auto-detect all currently visible GPUs."""
    if isinstance(gpus_config, int):
        return [gpus_config]

    gpus_text = str(gpus_config).strip()
    if not gpus_text:
        raise ValueError("'gpus' must not be empty")

    if gpus_text.lower() not in {"all", "auto"}:
        return [int(item.strip()) for item in gpus_text.split(",") if item.strip()]

    visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES")
    if visible_devices:
        visible = [item.strip() for item in visible_devices.split(",") if item.strip()]
        if visible and all(item.isdigit() for item in visible):
            return [int(item) for item in visible]

    try:
        output = subprocess.check_output(
            ["nvidia-smi", "--query-gpu=index", "--format=csv,noheader,nounits"],
            text=True,
            timeout=10,
        )
        gpu_ids = [int(line.strip()) for line in output.splitlines() if line.strip()]
        if gpu_ids:
            return gpu_ids
    except Exception:
        pass

    try:
        import torch

        count = torch.cuda.device_count()
        if count > 0:
            return list(range(count))
    except Exception:
        pass

    raise RuntimeError("No CUDA GPUs detected for gpus: all")


def load_yaml_config(yaml_path: str) -> dict:
    """Load configuration from YAML file."""
    import yaml

    with open(yaml_path, "r") as f:
        return yaml.safe_load(f)


def apply_slice(items: list, start: int | None, end: int | None) -> list:
    """Apply start/end slice to a list with bounds checking."""
    if start is None and end is None:
        return items

    slice_start = max(0, start if start is not None else 0)
    slice_end = min(end if end is not None else len(items), len(items))
    slice_end = max(slice_start, slice_end)

    return items[slice_start:slice_end]


def configure_teacache(transport, config: dict) -> None:
    """Configure TeaCache reuse strategy on SampleTransport."""
    from inference.pipeline.teacache import setup_teacache

    setup_teacache(
        rel_l1_thresh=config["rel_l1_thresh"],
        warmup_steps=config["warmup_steps"],
        log=config.get("log", False),
    )


def configure_kv_cache(transport, config: dict) -> None:
    """Configure KV cache compression if enabled."""
    if not config.get("compress_kv_cache", False):
        transport.compress_kv_cache = False
        return

    print("KV cache compression is enabled.")
    transport.compress_kv_cache = True

    assert config.get("total_cache_chunk_nums") is not None

    compression_config = {
        "method_config": {
            "compress_strategy": config["compress_strategy"],
            "mix_lambda": config["mix_lambda"],
            "query_granularity": config["query_granularity"],
            "score_weighting_method": config.get("score_weighting_method") or "no_weight",
            "power": config.get("power", 3),
        },
    }

    from inference.pipeline.kvcompress import replace_magi

    replace_magi(compression_config)


def configure_flowcache(transport, config: dict) -> None:
    """Configure FlowCache reuse strategy on SampleTransport."""
    from inference.pipeline.flowcache import setup_flowcache

    configure_kv_cache(transport, config)

    setup_flowcache(
        rel_l1_thresh=config["rel_l1_thresh"],
        warmup_steps=config["warmup_steps"],
        discard_nearly_clean_chunk=config.get("discard_nearly_clean_chunk", False),
        log=config.get("log", False),
        total_cache_chunk_nums=config.get("total_cache_chunk_nums", 5),
        compress_kv_cache=config.get("compress_kv_cache", False),
    )


def configure_reuse_strategy(config: dict) -> None:
    """Configure the appropriate reuse strategy on SampleTransport."""
    from inference.pipeline.video_generate import SampleTransport

    strategy = config["reuse_strategy"]

    if strategy == "original":
        return
    if strategy == "all":
        configure_teacache(SampleTransport, config)
    elif strategy == "chunkwise":
        configure_flowcache(SampleTransport, config)
    else:
        raise ValueError(f"Unknown reuse strategy: {strategy}")


def setup_environment(gpu_id: int) -> None:
    """Set up environment variables for a GPU worker process."""
    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    os.environ["WORLD_SIZE"] = "1"
    os.environ["RANK"] = "0"
    os.environ["LOCAL_RANK"] = "0"
    os.environ["MASTER_ADDR"] = "localhost"
    os.environ["MASTER_PORT"] = str(DEFAULT_BASE_PORT + gpu_id)

    # Enable pdb terminal debugging
    sys.stdin = open(0)


def filter_existing_samples(samples: list, config: dict) -> list:
    """Filter out samples whose output files already exist."""
    if config["benchmark"] == "vbench":
        return [
            sample
            for sample in samples
            if not os.path.exists(os.path.abspath(os.path.join(config["save_path"], f"{sample}-0.mp4")))
        ]
    else:  # physicsiq
        return [
            sample for sample in samples if not os.path.exists(sample["output_path"])
        ]


def assign_samples_to_gpu(
    samples: list, gpu_id: int, rank: int, num_gpus: int
) -> list:
    """Divide samples across GPUs and return the subset for this GPU."""
    samples_per_gpu = (len(samples) + num_gpus - 1) // num_gpus
    start_idx = rank * samples_per_gpu
    end_idx = min(start_idx + samples_per_gpu, len(samples))
    return samples[start_idx:end_idx]


def process_vbench_sample(pipeline, prompt: str, config: dict, gpu_id: int) -> None:
    """Process a single vbench text-to-video sample."""
    output_path = os.path.abspath(os.path.join(config["save_path"], f"{prompt}-0.mp4"))

    if os.path.exists(output_path):
        print(f"[SKIP GPU {gpu_id}] Already exists: {output_path}")
        return

    print(f"[GPU {gpu_id}] Generating T2V: '{prompt}' -> {output_path}")
    pipeline.run_text_to_video(prompt=prompt, output_path=output_path)
    print(f"[DONE GPU {gpu_id}] Saved: {output_path}")


def process_physicsiq_sample(pipeline, sample: dict, gpu_id: int) -> None:
    """Process a single PhysicsIQ video-to-video sample."""
    prompt = sample["description"]
    prefix_video_path = sample["prefix_video_path"]
    output_path = sample["output_path"]

    if not os.path.exists(prefix_video_path):
        print(f"[WARN GPU {gpu_id}] Conditioning video not found: {prefix_video_path}")
        return

    if os.path.exists(output_path):
        print(f"[SKIP GPU {gpu_id}] Already exists: {output_path}")
        return

    print(f"[GPU {gpu_id}] Generating V2V: '{prompt}'")
    print(f"   Input:  {prefix_video_path}")
    print(f"   Output: {output_path}")

    pipeline.run_video_to_video(
        prompt=prompt,
        prefix_video_path=prefix_video_path,
        output_path=output_path,
    )
    print(f"[DONE GPU {gpu_id}] Saved: {output_path}")


def worker_process(gpu_id: int, rank: int, config: dict, all_samples: list) -> None:
    """Independent worker running on each GPU."""
    setup_environment(gpu_id)
    configure_reuse_strategy(config)

    try:
        magi_root = subprocess.check_output(
            ["git", "rev-parse", "--show-toplevel"]
        ).decode().strip()
        os.environ["MAGI_ROOT"] = magi_root
        os.environ["PYTHONPATH"] = f"{magi_root}:{os.environ.get('PYTHONPATH', '')}"
    except Exception as e:
        print(f"[GPU {gpu_id}] Failed to set MAGI_ROOT: {e}")
        return

    filtered_samples = filter_existing_samples(all_samples, config)

    if not filtered_samples:
        print(f"[GPU {gpu_id}] No samples need to be generated.")
        return

    print(f"Processing {len(filtered_samples)} samples.")

    my_samples = assign_samples_to_gpu(
        filtered_samples, gpu_id, rank, config["num_gpus"]
    )

    if not my_samples:
        print(f"[GPU {gpu_id}] No samples assigned.")
        return

    print(f"[GPU {gpu_id}] Assigned {len(my_samples)} samples")

    from inference.pipeline.entry import MagiPipeline

    print(f"[GPU {gpu_id}] Loading model...")
    pipeline = MagiPipeline(config["config_file"])
    print(f"[GPU {gpu_id}] Model loaded.")

    process_func = (
        process_vbench_sample if config["benchmark"] == "vbench" else process_physicsiq_sample
    )

    for sample in my_samples:
        process_func(pipeline, sample, config, gpu_id)

    print(f"[GPU {gpu_id}] Completed.")


def build_conditioning_video_path(
    data_root: str, vid_id: str, scenario: str, fps: int
) -> str:
    """Construct the path to the conditioning video file."""
    conditioning_dir = os.path.join(
        data_root, "physics-IQ-benchmark", "split-videos", "conditioning", f"{fps}FPS"
    )
    match_suffix = re.search(r"_(.*)", scenario)
    suffix = match_suffix.group(1) if match_suffix else ""
    filename = f"{vid_id}_conditioning-videos_{fps}FPS_{suffix}"
    return os.path.join(conditioning_dir, filename)


def load_physicsiq_samples(config: dict) -> list[dict]:
    """Load sample list from PhysicsIQ dataset."""
    data_root = config["physicsiq_data_dir"]
    descriptions_csv = os.path.join(data_root, "descriptions", "descriptions.csv")
    output_dir = config["save_path"]

    if not os.path.exists(descriptions_csv):
        raise FileNotFoundError(f"descriptions.csv not found at {descriptions_csv}")

    os.makedirs(output_dir, exist_ok=True)

    samples = []
    with open(descriptions_csv, mode="r") as f:
        reader = csv.DictReader(f)
        for row in reader:
            scenario = row["scenario"].strip()
            match_id = re.match(r"^(\d+)_", scenario)

            if not match_id:
                print(f"Cannot extract ID from scenario: {scenario}")
                continue

            vid_id = match_id.group(1).zfill(4)
            description = row["description"]
            generated_video_name = row["generated_video_name"]
            prefix_video_path = build_conditioning_video_path(
                data_root, vid_id, scenario, PHYSICSIQ_FPS
            )
            output_path = os.path.join(output_dir, generated_video_name)

            os.makedirs(os.path.dirname(output_path), exist_ok=True)

            samples.append({
                "vid_id": vid_id,
                "scenario": scenario,
                "description": description,
                "generated_video_name": generated_video_name,
                "prefix_video_path": prefix_video_path,
                "output_path": output_path,
            })

    # PhysicsIQ samples are duplicated; take only the first half
    unique_count = len(samples) // 2
    samples = samples[:unique_count]

    print(f"Loaded {unique_count} PhysicsIQ samples.")

    return apply_slice(samples, config.get("start"), config.get("end"))


def load_vbench_samples(config: dict) -> list[str]:
    """Load prompt list from vbench dimension file."""
    prompt_dir = config["vbench_prompt_dir"]
    dimension = config.get("dimension")

    if not dimension:
        raise ValueError("For vbench, 'dimension' must be specified in config")

    prompt_file = os.path.join(prompt_dir, f"{dimension}.txt")

    if not os.path.exists(prompt_file):
        raise FileNotFoundError(f"Prompt file not found: {prompt_file}")

    with open(prompt_file, "r") as f:
        prompts = [line.strip() for line in f if line.strip()]

    return apply_slice(prompts, config.get("start"), config.get("end"))


def setup_save_path(config: dict) -> None:
    """Configure the output save path based on benchmark type."""
    base_path = config["base_save_path"]

    if config["benchmark"] == "vbench":
        dimension = config.get("dimension")
        videos_dir = os.path.join(base_path, "videos", dimension) if dimension else None
        config["save_path"] = videos_dir if videos_dir else os.path.join(base_path, "videos")
    elif config["benchmark"] == "physicsiq":
        config["save_path"] = os.path.join(base_path, "videos")

    os.makedirs(config["save_path"], exist_ok=True)


def main() -> None:
    """Entry point for video sampling script."""
    parser = argparse.ArgumentParser(
        description="Video sampling script using YAML configuration"
    )
    parser.add_argument("yaml_config", type=str, help="Path to YAML configuration file")
    args = parser.parse_args()

    config = load_yaml_config(args.yaml_config)
    print(f"Loaded configuration from: {args.yaml_config}")

    setup_save_path(config)

    gpu_ids = resolve_gpu_ids(config["gpus"])
    config["num_gpus"] = len(gpu_ids)

    benchmark = config["benchmark"]
    if benchmark == "vbench":
        all_samples = load_vbench_samples(config)
    elif benchmark == "physicsiq":
        data_root = config["physicsiq_data_dir"]
        if not os.path.exists(data_root):
            raise FileNotFoundError(f"Data directory not found: {data_root}")
        all_samples = load_physicsiq_samples(config)
    else:
        raise ValueError(f"Invalid benchmark: {benchmark}")

    print(f"Total samples: {len(all_samples)}")
    print(f"GPUs: {gpu_ids}")
    print(f"Output: {config['save_path']}")
    print(f"Config: {config['config_file']}")

    processes = []
    for rank, gpu_id in enumerate(gpu_ids):
        p = mp.Process(target=worker_process, args=(gpu_id, rank, config, all_samples))
        p.start()
        processes.append(p)

    for p in processes:
        p.join()

    failed = [p.exitcode for p in processes if p.exitcode != 0]
    if failed:
        raise RuntimeError(f"{len(failed)} worker process(es) failed with exit codes: {failed}")


if __name__ == "__main__":
    main()