8
8
from torchvision .transforms import Resize
9
9
10
10
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 )
14
14
frame = Resize ((256 , 256 ))(frame )
15
15
return frame
16
16
17
17
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
21
25
)
22
26
start_time = time .time ()
23
27
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)
30
40
end_time = time .time ()
31
41
elapsed = end_time - start_time
32
42
fps = frame_count / (end_time - start_time )
33
43
print (
34
- f"****** DECODED full video { device_string = } { frame_count = } { elapsed = } { fps = } "
44
+ f"****** DECODED full video { decode_device = } { frame_count = } { elapsed = } { fps = } "
35
45
)
36
46
return frame_count , end_time - start_time
37
47
@@ -59,15 +69,6 @@ def main():
59
69
"to measure the cold start time."
60
70
),
61
71
)
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
- )
71
72
args = parser .parse_args ()
72
73
video_path = args .video
73
74
@@ -77,23 +78,17 @@ def main():
77
78
decode_full_video (video_path , device )
78
79
return
79
80
80
- label = "Decode"
81
- if args .do_gpu_preproc :
82
- label += " + GPU Preproc"
83
- label += " Time"
84
-
85
81
results = []
86
82
for device in args .devices .split ("," ):
87
83
print ("device" , device )
88
84
t = benchmark .Timer (
89
- stmt = "decode_full_video(video_path, device, do_gpu_preproc )" ,
85
+ stmt = "decode_full_video(video_path, device)" ,
90
86
globals = {
91
87
"device" : device ,
92
88
"video_path" : video_path ,
93
89
"decode_full_video" : decode_full_video ,
94
- "do_gpu_preproc" : args .do_gpu_preproc ,
95
90
},
96
- label = label ,
91
+ label = "Decode+Resize Time" ,
97
92
sub_label = f"video={ os .path .basename (video_path )} " ,
98
93
description = f"decode_device={ device } " ,
99
94
).blocked_autorange ()
0 commit comments