|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import time |
| 5 | +from concurrent.futures import ThreadPoolExecutor |
| 6 | + |
| 7 | +import torch |
| 8 | + |
| 9 | +import torch.utils.benchmark as benchmark |
| 10 | + |
| 11 | +import torchcodec |
| 12 | +import torchvision.transforms.v2.functional as F |
| 13 | + |
| 14 | +RESIZED_WIDTH = 256 |
| 15 | +RESIZED_HEIGHT = 256 |
| 16 | + |
| 17 | + |
| 18 | +def transfer_and_resize_frame(frame, resize_device_string): |
| 19 | + # This should be a no-op if the frame is already on the target device. |
| 20 | + frame = frame.to(resize_device_string) |
| 21 | + frame = F.resize(frame, (RESIZED_HEIGHT, RESIZED_WIDTH)) |
| 22 | + return frame |
| 23 | + |
| 24 | + |
| 25 | +def decode_full_video(video_path, decode_device_string, resize_device_string): |
| 26 | + # We use the core API instead of SimpleVideoDecoder because the core API |
| 27 | + # allows us to natively resize as part of the decode step. |
| 28 | + print(f"{decode_device_string=} {resize_device_string=}") |
| 29 | + decoder = torchcodec.decoders._core.create_from_file(video_path) |
| 30 | + num_threads = None |
| 31 | + if "cuda" in decode_device_string: |
| 32 | + num_threads = 1 |
| 33 | + width = None |
| 34 | + height = None |
| 35 | + if "native" in resize_device_string: |
| 36 | + width = RESIZED_WIDTH |
| 37 | + height = RESIZED_HEIGHT |
| 38 | + torchcodec.decoders._core._add_video_stream( |
| 39 | + decoder, |
| 40 | + stream_index=-1, |
| 41 | + device=decode_device_string, |
| 42 | + num_threads=num_threads, |
| 43 | + width=width, |
| 44 | + height=height, |
| 45 | + ) |
| 46 | + |
| 47 | + start_time = time.time() |
| 48 | + frame_count = 0 |
| 49 | + while True: |
| 50 | + try: |
| 51 | + frame, *_ = torchcodec.decoders._core.get_next_frame(decoder) |
| 52 | + if resize_device_string != "none" and "native" not in resize_device_string: |
| 53 | + frame = transfer_and_resize_frame(frame, resize_device_string) |
| 54 | + |
| 55 | + frame_count += 1 |
| 56 | + except Exception as e: |
| 57 | + print("EXCEPTION", e) |
| 58 | + break |
| 59 | + |
| 60 | + end_time = time.time() |
| 61 | + elapsed = end_time - start_time |
| 62 | + fps = frame_count / (end_time - start_time) |
| 63 | + print( |
| 64 | + f"****** DECODED full video {decode_device_string=} {frame_count=} {elapsed=} {fps=}" |
| 65 | + ) |
| 66 | + return frame_count, end_time - start_time |
| 67 | + |
| 68 | + |
| 69 | +def decode_videos_using_threads( |
| 70 | + video_path, |
| 71 | + decode_device_string, |
| 72 | + resize_device_string, |
| 73 | + num_videos, |
| 74 | + num_threads, |
| 75 | + use_multiple_gpus, |
| 76 | +): |
| 77 | + executor = ThreadPoolExecutor(max_workers=num_threads) |
| 78 | + for i in range(num_videos): |
| 79 | + actual_decode_device = decode_device_string |
| 80 | + if "cuda" in decode_device_string and use_multiple_gpus: |
| 81 | + actual_decode_device = f"cuda:{i % torch.cuda.device_count()}" |
| 82 | + executor.submit( |
| 83 | + decode_full_video, video_path, actual_decode_device, resize_device_string |
| 84 | + ) |
| 85 | + executor.shutdown(wait=True) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + parser = argparse.ArgumentParser() |
| 90 | + parser.add_argument( |
| 91 | + "--devices", |
| 92 | + default="cuda:0,cpu", |
| 93 | + type=str, |
| 94 | + help="Comma-separated devices to test decoding on.", |
| 95 | + ) |
| 96 | + parser.add_argument( |
| 97 | + "--resize_devices", |
| 98 | + default="cuda:0,cpu,native,none", |
| 99 | + type=str, |
| 100 | + help="Comma-separated devices to test preroc (resize) on. Use 'none' to specify no resize.", |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "--video", |
| 104 | + type=str, |
| 105 | + default=str( |
| 106 | + pathlib.Path(__file__).parent / "../../test/resources/nasa_13013.mp4" |
| 107 | + ), |
| 108 | + ) |
| 109 | + parser.add_argument( |
| 110 | + "--use_torch_benchmark", |
| 111 | + action=argparse.BooleanOptionalAction, |
| 112 | + default=True, |
| 113 | + help=( |
| 114 | + "Use pytorch benchmark to measure decode time with warmup and " |
| 115 | + "autorange. Without this we just run one iteration without warmup " |
| 116 | + "to measure the cold start time." |
| 117 | + ), |
| 118 | + ) |
| 119 | + parser.add_argument( |
| 120 | + "--num_threads", |
| 121 | + type=int, |
| 122 | + default=1, |
| 123 | + help="Number of threads to use for decoding. Only used when --use_torch_benchmark is set.", |
| 124 | + ) |
| 125 | + parser.add_argument( |
| 126 | + "--num_videos", |
| 127 | + type=int, |
| 128 | + default=50, |
| 129 | + help="Number of videos to decode in parallel. Only used when --num_threads is set.", |
| 130 | + ) |
| 131 | + parser.add_argument( |
| 132 | + "--use_multiple_gpus", |
| 133 | + action=argparse.BooleanOptionalAction, |
| 134 | + default=True, |
| 135 | + help=("Use multiple GPUs to decode multiple videos in multi-threaded mode."), |
| 136 | + ) |
| 137 | + args = parser.parse_args() |
| 138 | + video_path = args.video |
| 139 | + |
| 140 | + if not args.use_torch_benchmark: |
| 141 | + for device in args.devices.split(","): |
| 142 | + print("Testing on", device) |
| 143 | + decode_full_video(video_path, device) |
| 144 | + return |
| 145 | + |
| 146 | + resize_devices = args.resize_devices.split(",") |
| 147 | + resize_devices = [d for d in resize_devices if d != ""] |
| 148 | + if len(resize_devices) == 0: |
| 149 | + resize_devices.append("none") |
| 150 | + |
| 151 | + label = "Decode+Resize Time" |
| 152 | + |
| 153 | + results = [] |
| 154 | + for decode_device_string in args.devices.split(","): |
| 155 | + for resize_device_string in resize_devices: |
| 156 | + decode_label = decode_device_string |
| 157 | + if "cuda" in decode_label: |
| 158 | + # Shorten "cuda:0" to "cuda" |
| 159 | + decode_label = "cuda" |
| 160 | + resize_label = resize_device_string |
| 161 | + if "cuda" in resize_device_string: |
| 162 | + # Shorten "cuda:0" to "cuda" |
| 163 | + resize_label = "cuda" |
| 164 | + print("decode_device", decode_device_string) |
| 165 | + print("resize_device", resize_device_string) |
| 166 | + if args.num_threads > 1: |
| 167 | + t = benchmark.Timer( |
| 168 | + stmt="decode_videos_using_threads(video_path, decode_device_string, resize_device_string, num_videos, num_threads, use_multiple_gpus)", |
| 169 | + globals={ |
| 170 | + "decode_device_string": decode_device_string, |
| 171 | + "video_path": video_path, |
| 172 | + "decode_full_video": decode_full_video, |
| 173 | + "decode_videos_using_threads": decode_videos_using_threads, |
| 174 | + "resize_device_string": resize_device_string, |
| 175 | + "num_videos": args.num_videos, |
| 176 | + "num_threads": args.num_threads, |
| 177 | + "use_multiple_gpus": args.use_multiple_gpus, |
| 178 | + }, |
| 179 | + label=label, |
| 180 | + description=f"threads={args.num_threads} work={args.num_videos} video={os.path.basename(video_path)}", |
| 181 | + sub_label=f"D={decode_label} R={resize_label} T={args.num_threads} W={args.num_videos}", |
| 182 | + ).blocked_autorange() |
| 183 | + results.append(t) |
| 184 | + else: |
| 185 | + t = benchmark.Timer( |
| 186 | + stmt="decode_full_video(video_path, decode_device_string, resize_device_string)", |
| 187 | + globals={ |
| 188 | + "decode_device_string": decode_device_string, |
| 189 | + "video_path": video_path, |
| 190 | + "decode_full_video": decode_full_video, |
| 191 | + "resize_device_string": resize_device_string, |
| 192 | + }, |
| 193 | + label=label, |
| 194 | + description=f"video={os.path.basename(video_path)}", |
| 195 | + sub_label=f"D={decode_label} R={resize_label}", |
| 196 | + ).blocked_autorange() |
| 197 | + results.append(t) |
| 198 | + compare = benchmark.Compare(results) |
| 199 | + compare.print() |
| 200 | + print("Key: D=Decode, R=Resize T=threads W=work (number of videos to decode)") |
| 201 | + print("Native resize is done as part of the decode step") |
| 202 | + print("none resize means there is no resize step -- native or otherwise") |
| 203 | + |
| 204 | + |
| 205 | +if __name__ == "__main__": |
| 206 | + main() |
0 commit comments