88from torchvision .transforms import Resize
99
1010
11- def transfer_and_resize_frame (frame ):
12- # This should be a no-op if the frame is already on the GPU .
13- frame = frame .to ("cuda:0" )
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 )
1414 frame = Resize ((256 , 256 ))(frame )
1515 return frame
1616
1717
18- def decode_full_video (video_path , device_string , do_gpu_preproc ):
19- decoder = torchcodec .decoders .SimpleVideoDecoder (
20- video_path , device = torch .device (device_string )
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
2125 )
2226 start_time = time .time ()
2327 frame_count = 0
24- for frame in decoder :
25- # You can do a resize to simulate extra preproc work that happens
26- # on the GPU by uncommenting the following line:
27- if do_gpu_preproc :
28- frame = transfer_and_resize_frame (frame )
29- frame_count += 1
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)
3040 end_time = time .time ()
3141 elapsed = end_time - start_time
3242 fps = frame_count / (end_time - start_time )
3343 print (
34- f"****** DECODED full video { device_string = } { frame_count = } { elapsed = } { fps = } "
44+ f"****** DECODED full video { decode_device = } { frame_count = } { elapsed = } { fps = } "
3545 )
3646 return frame_count , end_time - start_time
3747
@@ -59,15 +69,6 @@ def main():
5969 "to measure the cold start time."
6070 ),
6171 )
62- parser .add_argument (
63- "--do_gpu_preproc" ,
64- action = argparse .BooleanOptionalAction ,
65- default = True ,
66- help = (
67- "Do a transfer to GPU and resize operation after the decode to "
68- "simulate a real-world transform."
69- ),
70- )
7172 args = parser .parse_args ()
7273 video_path = args .video
7374
@@ -77,23 +78,17 @@ def main():
7778 decode_full_video (video_path , device )
7879 return
7980
80- label = "Decode"
81- if args .do_gpu_preproc :
82- label += " + GPU Preproc"
83- label += " Time"
84-
8581 results = []
8682 for device in args .devices .split ("," ):
8783 print ("device" , device )
8884 t = benchmark .Timer (
89- stmt = "decode_full_video(video_path, device, do_gpu_preproc )" ,
85+ stmt = "decode_full_video(video_path, device)" ,
9086 globals = {
9187 "device" : device ,
9288 "video_path" : video_path ,
9389 "decode_full_video" : decode_full_video ,
94- "do_gpu_preproc" : args .do_gpu_preproc ,
9590 },
96- label = label ,
91+ label = "Decode+Resize Time" ,
9792 sub_label = f"video={ os .path .basename (video_path )} " ,
9893 description = f"decode_device={ device } " ,
9994 ).blocked_autorange ()
0 commit comments