# Copyright (c) 2025 SandAI. 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 argparse import gc import sys import torch from inference.pipeline import MagiPipeline def parse_arguments(): parser = argparse.ArgumentParser(description="Run MagiPipeline with different modes.") parser.add_argument('--config_file', type=str, help='Path to the configuration file.') parser.add_argument( '--mode', type=str, choices=['t2v', 'i2v', 'v2v'], required=True, help='Mode to run: t2v, i2v, or v2v.' ) parser.add_argument('--prompt', type=str, required=True, help='Prompt for the pipeline.') parser.add_argument('--image_path', type=str, help='Path to the image file (for i2v mode).') parser.add_argument('--prefix_video_path', type=str, help='Path to the prefix video file (for v2v mode).') parser.add_argument('--output_path', type=str, required=True, help='Path to save the output video.') parser.add_argument( '--residual_stats_path', type=str, help='Optional path to save per-chunk residual-difference norm stats as .json, .pt, or .pth.', ) parser.add_argument( '--l1_rel_stats_path', type=str, help='Optional path to save per-chunk relative L1 change stats as .json, .pt, or .pth.', ) parser.add_argument('--print_peak_memory', action='store_true', help='Print peak memory usage after pipeline completion.') return parser.parse_args() def main(): args = parse_arguments() if args.print_peak_memory: # Check if GPU is available and reset memory stats if torch.cuda.is_available(): torch.cuda.reset_peak_memory_stats() device = torch.cuda.current_device() print(f"Running on GPU: {torch.cuda.get_device_name(device)}") print(f"GPU Memory before pipeline: {torch.cuda.memory_allocated(device) / 1024**3:.2f} GB allocated") else: print("CUDA not available, running on CPU") pipeline = MagiPipeline( args.config_file, residual_stats_path=args.residual_stats_path, l1_rel_stats_path=args.l1_rel_stats_path, ) if args.mode == 't2v': pipeline.run_text_to_video(prompt=args.prompt, output_path=args.output_path) elif args.mode == 'i2v': if not args.image_path: print("Error: --image_path is required for i2v mode.") sys.exit(1) pipeline.run_image_to_video(prompt=args.prompt, image_path=args.image_path, output_path=args.output_path) elif args.mode == 'v2v': if not args.prefix_video_path: print("Error: --prefix_video_path is required for v2v mode.") sys.exit(1) pipeline.run_video_to_video(prompt=args.prompt, prefix_video_path=args.prefix_video_path, output_path=args.output_path) if args.print_peak_memory: # Print peak memory usage after pipeline completion if torch.cuda.is_available(): peak_memory = torch.cuda.max_memory_allocated(device) / 1024**3 current_memory = torch.cuda.memory_allocated(device) / 1024**3 cached_memory = torch.cuda.memory_reserved(device) / 1024**3 total_memory = torch.cuda.get_device_properties(device).total_memory / 1024**3 print("\n" + "="*50) print("GPU Memory Usage Summary:") print(f"Peak memory allocated: {peak_memory:.2f} GB") print(f"Current memory allocated: {current_memory:.2f} GB") print(f"Cached memory reserved: {cached_memory:.2f} GB") print(f"Total GPU memory: {total_memory:.2f} GB") print(f"Peak memory usage: {(peak_memory/total_memory)*100:.1f}%") print("="*50) # Clear cache and show final memory gc.collect() torch.cuda.empty_cache() final_memory = torch.cuda.memory_allocated(device) / 1024**3 print(f"Memory after cache cleanup: {final_memory:.2f} GB") if __name__ == "__main__": main()