|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import time |
| 4 | + |
| 5 | +import torch.utils.benchmark as benchmark |
| 6 | + |
| 7 | +import torchcodec |
| 8 | +from torchvision.transforms import Resize |
| 9 | + |
| 10 | + |
| 11 | +def transfer_and_resize_frame(frame, device): |
| 12 | + # This should be a no-op if the frame is already on the device. |
| 13 | + frame = frame.to(device) |
| 14 | + frame = Resize((256, 256))(frame) |
| 15 | + return frame |
| 16 | + |
| 17 | + |
| 18 | +def decode_full_video(video_path, decode_device): |
| 19 | + decoder = torchcodec.decoders._core.create_from_file(video_path) |
| 20 | + num_threads = None |
| 21 | + if "cuda" in decode_device: |
| 22 | + num_threads = 1 |
| 23 | + torchcodec.decoders._core.add_video_stream( |
| 24 | + decoder, stream_index=0, device_string=decode_device, num_threads=num_threads |
| 25 | + ) |
| 26 | + start_time = time.time() |
| 27 | + frame_count = 0 |
| 28 | + while True: |
| 29 | + try: |
| 30 | + frame, *_ = torchcodec.decoders._core.get_next_frame(decoder) |
| 31 | + # You can do a resize to simulate extra preproc work that happens |
| 32 | + # on the GPU by uncommenting the following line: |
| 33 | + # frame = transfer_and_resize_frame(frame, decode_device) |
| 34 | + |
| 35 | + frame_count += 1 |
| 36 | + except Exception as e: |
| 37 | + print("EXCEPTION", e) |
| 38 | + break |
| 39 | + # print(f"current {frame_count=}", flush=True) |
| 40 | + end_time = time.time() |
| 41 | + elapsed = end_time - start_time |
| 42 | + fps = frame_count / (end_time - start_time) |
| 43 | + print( |
| 44 | + f"****** DECODED full video {decode_device=} {frame_count=} {elapsed=} {fps=}" |
| 45 | + ) |
| 46 | + return frame_count, end_time - start_time |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + parser = argparse.ArgumentParser() |
| 51 | + parser.add_argument( |
| 52 | + "--devices", |
| 53 | + default="cuda:0,cpu", |
| 54 | + type=str, |
| 55 | + help="Comma-separated devices to test decoding on.", |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "--video", |
| 59 | + type=str, |
| 60 | + default=os.path.dirname(__file__) + "/../../test/resources/nasa_13013.mp4", |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "--use_torch_benchmark", |
| 64 | + action=argparse.BooleanOptionalAction, |
| 65 | + default=True, |
| 66 | + help=( |
| 67 | + "Use pytorch benchmark to measure decode time with warmup and " |
| 68 | + "autorange. Without this we just run one iteration without warmup " |
| 69 | + "to measure the cold start time." |
| 70 | + ), |
| 71 | + ) |
| 72 | + args = parser.parse_args() |
| 73 | + video_path = args.video |
| 74 | + |
| 75 | + if not args.use_torch_benchmark: |
| 76 | + for device in args.devices.split(","): |
| 77 | + print("Testing on", device) |
| 78 | + decode_full_video(video_path, device) |
| 79 | + return |
| 80 | + |
| 81 | + results = [] |
| 82 | + for device in args.devices.split(","): |
| 83 | + print("device", device) |
| 84 | + t = benchmark.Timer( |
| 85 | + stmt="decode_full_video(video_path, device)", |
| 86 | + globals={ |
| 87 | + "device": device, |
| 88 | + "video_path": video_path, |
| 89 | + "decode_full_video": decode_full_video, |
| 90 | + }, |
| 91 | + label="Decode+Resize Time", |
| 92 | + sub_label=f"video={os.path.basename(video_path)}", |
| 93 | + description=f"decode_device={device}", |
| 94 | + ).blocked_autorange() |
| 95 | + results.append(t) |
| 96 | + compare = benchmark.Compare(results) |
| 97 | + compare.print() |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
0 commit comments