forked from facebookresearch/spdl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_transfer_test.py
More file actions
256 lines (204 loc) · 9.5 KB
/
Copy pathbuffer_transfer_test.py
File metadata and controls
256 lines (204 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import annotations
import gc
import unittest
import numpy as np
import spdl.io
import spdl.io.utils
import torch
from parameterized import parameterized
from ..fixture import FFMPEG_CLI, get_sample
if not spdl.io.utils.built_with_cuda():
raise unittest.SkipTest("SPDL is not compiled with CUDA support")
DEFAULT_CUDA = 0
CMDS = {
"audio": f'{FFMPEG_CLI} -hide_banner -y -f lavfi -i "sine=frequency=1000:sample_rate=48000:duration=3" -c:a pcm_s16le sample.wav',
"video": f"{FFMPEG_CLI} -hide_banner -y -f lavfi -i testsrc -frames:v 1000 sample.mp4",
"image": f"{FFMPEG_CLI} -hide_banner -y -f lavfi -i color=0x000000,format=gray -frames:v 1 sample.png",
}
class TestTransferBufferToCuda(unittest.TestCase):
@parameterized.expand(
[
("audio",),
("video",),
("image",),
]
)
def test_transfer_buffer_to_cuda(self, media_type: str) -> None:
"""smoke test for transfer_buffer_to_cuda function"""
cmd = CMDS[media_type]
sample = get_sample(cmd)
demux_func = {
"audio": spdl.io.demux_audio,
"video": spdl.io.demux_video,
"image": spdl.io.demux_image,
}[media_type]
_ = torch.zeros([0], device=torch.device(f"cuda:{DEFAULT_CUDA}"))
packets = demux_func(sample.path)
# pyre-ignore[6]: Union type from demux_func is valid for decode_packets
frames = spdl.io.decode_packets(packets)
buffer = spdl.io.convert_frames(frames)
cpu_tensor = spdl.io.to_torch(buffer).clone()
cuda_tensor = spdl.io.to_torch(
spdl.io.transfer_buffer(
buffer,
device_config=spdl.io.cuda_config(
device_index=DEFAULT_CUDA,
),
)
)
self.assertTrue(cuda_tensor.is_cuda)
self.assertEqual(cuda_tensor.device, torch.device(f"cuda:{DEFAULT_CUDA}"))
self.assertTrue(torch.allclose(cpu_tensor, cuda_tensor.cpu()))
@parameterized.expand(
[
("audio",),
("video",),
("image",),
]
)
def test_transfer_buffer_to_cuda_with_pytorch_allocator(
self, media_type: str
) -> None:
"""smoke test for transfer_buffer_to_cuda function"""
cmd = CMDS[media_type]
sample = get_sample(cmd)
allocator_called, deleter_called = False, False
def allocator(size: int, device: int, stream: int) -> int:
print("Calling allocator", flush=True)
ptr = torch.cuda.caching_allocator_alloc(size, device, stream)
nonlocal allocator_called
allocator_called = True
return ptr
def deleter(ptr: int) -> None:
print("Calling deleter", flush=True)
torch.cuda.caching_allocator_delete(ptr)
nonlocal deleter_called
deleter_called = True
demux_func = {
"audio": spdl.io.demux_audio,
"video": spdl.io.demux_video,
"image": spdl.io.demux_image,
}[media_type]
def test() -> None:
packets = demux_func(sample.path)
frames = spdl.io.decode_packets(packets)
buffer = spdl.io.convert_frames(frames)
cpu_tensor = spdl.io.to_torch(buffer).clone()
print("Asserting allocator was not yet called")
self.assertFalse(allocator_called)
print("Transferring")
cuda_buffer = spdl.io.transfer_buffer(
buffer,
device_config=spdl.io.cuda_config(
device_index=DEFAULT_CUDA,
allocator=(allocator, deleter),
),
)
print("Asserting allocator was called")
self.assertTrue(allocator_called)
cuda_tensor = spdl.io.to_torch(cuda_buffer)
self.assertTrue(cuda_tensor.is_cuda)
self.assertEqual(cuda_tensor.device, torch.device(f"cuda:{DEFAULT_CUDA}"))
self.assertTrue(torch.allclose(cpu_tensor, cuda_tensor.cpu()))
print("Asserting deleter was not yet called")
self.assertFalse(deleter_called)
test()
print("Calling GC")
gc.collect()
print("Asserting deleter was called")
self.assertTrue(deleter_called)
class TestArrayTransfer(unittest.TestCase):
def test_array_transfer_numpy(self) -> None:
"""smoke test for transfer_buffer function"""
dtypes = {
np.dtype("uint8"): torch.uint8,
np.dtype("int32"): torch.int32,
np.dtype("int64"): torch.int64,
}
def test(array) -> None:
buffer = spdl.io.transfer_buffer(
array, device_config=spdl.io.cuda_config(device_index=DEFAULT_CUDA)
)
tensor = spdl.io.to_torch(buffer)
device = torch.device(f"cuda:{DEFAULT_CUDA}")
self.assertEqual(tensor.dtype, dtypes[array.dtype])
self.assertEqual(tensor.shape, torch.Size(array.shape))
self.assertEqual(tensor.device, torch.device(device))
self.assertTrue(torch.allclose(tensor, torch.from_numpy(array).to(device)))
for dtype in [np.uint8, np.int32, np.int64]:
max_val = np.iinfo(dtype).max
# pyrefly: ignore [no-matching-overload]
array = np.random.randint(0, max_val, size=(1, 128_000), dtype=dtype)
test(array)
def test_array_transfer_torch(self) -> None:
"""smoke test for transfer_buffer function"""
device_config = spdl.io.cuda_config(device_index=DEFAULT_CUDA)
def test(cpu_tensor) -> None:
buffer = spdl.io.transfer_buffer(cpu_tensor, device_config=device_config)
cuda_tensor = spdl.io.to_torch(buffer)
device = torch.device(f"cuda:{DEFAULT_CUDA}")
self.assertEqual(cuda_tensor.dtype, cpu_tensor.dtype)
self.assertEqual(cuda_tensor.shape, cpu_tensor.shape)
self.assertEqual(cuda_tensor.device, device)
self.assertTrue(torch.allclose(cuda_tensor, cpu_tensor.to(device)))
for dtype in [np.uint8, np.int32, np.int64]:
max_val = np.iinfo(dtype).max
# pyrefly: ignore [no-matching-overload]
array = np.random.randint(0, max_val, size=(1, 128_000), dtype=dtype)
test(torch.from_numpy(array))
def test_array_transfer_non_contiguous_torch(self) -> None:
"""passing noncontiguous array/tensor to transfer_buffer works."""
device_config = spdl.io.cuda_config(device_index=DEFAULT_CUDA)
cpu_tensor = torch.arange(24).reshape(6, 4).T[::2, :]
self.assertFalse(cpu_tensor.is_contiguous())
# pyre-ignore[6]: Tensor is a valid input type for transfer_buffer
buffer = spdl.io.transfer_buffer(cpu_tensor, device_config=device_config)
cuda_tensor = spdl.io.to_torch(buffer)
device = torch.device(f"cuda:{DEFAULT_CUDA}")
self.assertEqual(cuda_tensor.dtype, cpu_tensor.dtype)
self.assertEqual(cuda_tensor.shape, cpu_tensor.shape)
self.assertEqual(cuda_tensor.device, device)
self.assertTrue(torch.allclose(cuda_tensor, cpu_tensor.to(device)))
def test_array_transfer_non_contiguous_numpy(self) -> None:
"""passing noncontiguous array/tensor to transfer_buffer works"""
device_config = spdl.io.cuda_config(device_index=DEFAULT_CUDA)
array0 = np.arange(24)
self.assertTrue(array0.data.contiguous)
arr = array0.reshape(6, 4).T[:, ::2]
self.assertFalse(arr.data.contiguous)
# pyre-ignore[6]: ndarray is a valid input type for transfer_buffer
buffer = spdl.io.transfer_buffer(arr, device_config=device_config)
tensor = spdl.io.to_torch(buffer)
device = torch.device(f"cuda:{DEFAULT_CUDA}")
self.assertEqual(tensor.dtype, torch.int64)
self.assertEqual(tensor.shape, torch.Size(arr.shape))
self.assertEqual(tensor.device, torch.device(device))
self.assertTrue(torch.allclose(tensor, torch.from_numpy(arr).to(device)))
def test_array_transfer_smoke_test(self) -> None:
"""smoke test for transferring multiple arrays concurrently"""
# pyrefly: ignore [no-matching-overload]
array = np.random.randint(0, 256, size=(1, 64_000), dtype=np.uint8)
device_config = spdl.io.cuda_config(device_index=DEFAULT_CUDA)
for _ in range(100):
spdl.io.transfer_buffer(array, device_config=device_config)
class TestTransferCpu(unittest.TestCase):
def test_transfer_cpu(self) -> None:
"""smoke test for transfer_buffer function"""
for dtype in [np.uint8, np.int32, np.int64]:
max_val = np.iinfo(dtype).max
# pyrefly: ignore [no-matching-overload]
array = np.random.randint(0, max_val, size=(1, 128_000), dtype=dtype)
ref = torch.from_numpy(array)
cuda_tensor = ref.cuda(device=DEFAULT_CUDA)
# pyre-ignore[6]: Tensor is a valid input type for transfer_buffer_cpu
buffer = spdl.io.transfer_buffer_cpu(cuda_tensor)
cpu_tensor = spdl.io.to_torch(buffer)
self.assertEqual(ref.dtype, cpu_tensor.dtype)
self.assertEqual(ref.shape, cpu_tensor.shape)
self.assertEqual(ref.device, cpu_tensor.device)
self.assertTrue(torch.allclose(ref, cpu_tensor))