Untitled

Anonymous
Python
10/09/2025 5:39 AM
5.5 KB
19
Indexable
Never
# Tao file inference_lora_repa.py

import argparse
import os
from pathlib import Path
import torch
from diffusers import CogVideoXPipeline
from finetrainers.models.cogvideox.repa_specification import REPACogVideoXTransformer3DModel
from safetensors.torch import load_file


def main():
    parser = argparse.ArgumentParser(description="Run inference with REPA LoRA + Projectors")
    parser.add_argument("--lora_path", type=str, required=True, help="Path to LoRA checkpoint directory")
    parser.add_argument("--prompt", type=str, required=True, help="Text prompt for generation")
    parser.add_argument("--output_file", type=str, required=True, help="Output video file (e.g., output.mp4)")
    parser.add_argument("--fps", type=int, default=8, help="Frames per second for output video")
    parser.add_argument("--num_inference_steps", type=int, default=50, help="Number of denoising steps")
    parser.add_argument("--lora_scale", type=float, default=0.75, help="LoRA weight scaling factor")
    parser.add_argument("--seed", type=int, default=42, help="Random seed")
    parser.add_argument("--height", type=int, default=480, help="Video height")
    parser.add_argument("--width", type=int, default=720, help="Video width")
    parser.add_argument("--num_frames", type=int, default=16, help="Number of video frames")
    parser.add_argument("--guidance_scale", type=float, default=6.0, help="Guidance scale for guidance-based video generation")
    parser.add_argument("--device", type=str, default="cuda", help="Device to run on")
    args = parser.parse_args()

    print(f"Loading pipeline from base model 'THUDM/CogVideoX-5b'...")
    pipe = CogVideoXPipeline.from_pretrained(
        "pretrained",
        torch_dtype=torch.bfloat16,
    ).to(args.device)
    import pdb;pdb.set_trace()
    # Load LoRA weights (adapter)
    lora_path = Path(args.lora_path)
    adapter_path = lora_path / "pytorch_lora_weights.safetensors"

    if adapter_path.exists():
        print(f"Loading LoRA weights from {adapter_path}...")
        pipe.load_lora_weights(str(lora_path), adapter_name="repa_lora")
        pipe.set_adapters(["repa_lora"], adapter_weights=[args.lora_scale])
    else:
        raise FileNotFoundError(f"LoRA adapter not found at {adapter_path}")

    # # Load REPA projectors into transformer
    # projector_path = lora_path / "projectors.safetensors"
    # if projector_path.exists():
    #     print(f"Loading REPA projectors from {projector_path}...")
    #     projector_state_dict = load_file(projector_path, device=args.device)
    #     # Patch the transformer with projectors
    #     new_transformer = REPACogVideoXTransformer3DModel.from_pretrained(
    #         "THUDM/CogVideoX-5b",
    #         subfolder="transformer",
    #         torch_dtype=torch.bfloat16,
    #         mlp_weights_path=str(projector_path),
    #     ).to(args.device)
    #     pipe.transformer = new_transformer
    # else:
    #     raise FileNotFoundError(f"REPA projectors not found at {projector_path}")

    # Optional: enable memory optimizations
    pipe.enable_model_cpu_offload()
    pipe.vae.enable_slicing()
    pipe.vae.enable_tiling()

    # Generate
    print(f"Generating video for prompt: '{args.prompt}'")
    generator = torch.Generator(device=args.device).manual_seed(args.seed)
    video_frames = pipe(
        prompt=args.prompt,
        height=args.height,
        width=args.width,
        num_frames=args.num_frames,
        num_inference_steps=args.num_inference_steps,
        guidance_scale=args.guidance_scale,
        generator=generator,
        output_type="pil",
    ).frames[0]

    # Save
    from diffusers.utils import export_to_video
    export_to_video(video_frames, args.output_file, fps=args.fps)
    print(f"Video saved to {args.output_file}")


if __name__ == "__main__":
    main()




## Tao file inference.sh
#!/bin/bash

# Simple script to run REPA LoRA inference
# Usage: ./run_inference_repa_lora.sh

set -e

# Configuration


lora_path="./results/ot_factorized1/lora_weights/002500"
prompt="OPENVID In a sprawling construction site, a robust yellow bulldozer, its powerful engine humming, navigates a vast expanse of debris-strewn ground. The camera captures the machine's relentless progress as it glides over the uneven terrain, its wide, flat blade pushing a mountain of rubble and broken concrete towards a towering dumpster. The scene is bathed in the warm glow of a golden-hour sun, casting dramatic shadows that accentuate the bulldozer's formidable presence. As the operator expertly maneuvers the machine, the camera remains steady, offering a panoramic view of the site, where a backdrop of skeletal buildings and a distant crane hints at the scale of the ongoing project. The bulldozer's rhythmic movements create a mesmerizing dance of progress, embodying the relentless spirit of construction."
output_file="output_repa_lora.mp4"
fps=25
num_inference_steps=50
lora_scale=0.75
height=512
width=768
num_frames=81
device="cuda:0"
guidance_scale=6.0


echo "Running REPA LoRA Inference..."
echo "LoRA Path: $lora_path"
echo "Prompt: $prompt"
echo "Output: $output_file"
echo "LoRA Scale: $lora_scale"

python inference_lora_repa.py \
    --lora_path "$lora_path" \
    --prompt "$prompt" \
    --output_file "$output_file" \
    --fps $fps \
    --num_inference_steps $num_inference_steps \
    --lora_scale $lora_scale \
    --height $height \
    --width $width \
    --num_frames $num_frames \
    --device $device \
    --guidance_scale $guidance_scale

echo "Inference completed! Video saved to: $output_file"





 
Editor is loading...
Leave a Comment