-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaching.py
289 lines (238 loc) · 10.4 KB
/
caching.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Code taken from
# Source: https://github.com/Charl-AI/stochastic-caching/blob/main/stocaching/__init__.py
"""Stocaching, a tiny library for stochastic dataset caching in PyTorch."""
import ctypes
import multiprocessing as mp
import os
from enum import Enum
import numpy as np
import torch
__all__ = ["SharedCache", "get_shm_size"]
BYTES_PER_GIB = 1024**3
C_DTYPES = {
torch.bool: ctypes.c_bool,
torch.uint8: ctypes.c_uint8,
torch.int8: ctypes.c_int8,
torch.int16: ctypes.c_int16,
torch.int32: ctypes.c_int32,
torch.int64: ctypes.c_int64,
torch.float32: ctypes.c_float,
torch.float64: ctypes.c_double,
}
class SlotState(Enum):
EMPTY = 0
SET = 1
# in this context, OOB means outside the range of the cache,
# but inside the range of the full dataset
OOB = 2
class SharedCache:
"""A simple shared memory cache for use in PyTorch datasets.
You can set a size limit for the cache to take. If your dataset
exceeds this size, the cache will only allocate slots for the first N samples.
This allows you to speed up training by caching only a subset of your dataset.
When applied to a large, shuffled dataset, we call this 'stochastic caching'.
You may interact with the cache directly as if it were a list of slots,
with one slot per sample. Get and set with `x = cache[0]` and `cache[0] = x`.
Using the getter and setter directly can be fiddly if you are only caching part
of the dataset. We expose two convenience methods (`get_slot` and `set_slot`),
which simplify usage by allowing you to treat the cache as if it were the same
size as the full dataset.
You may access the underlying pytorch array with the `underlying_array property`.
Example usage:
```python
import torch
from stocaching import SharedCache
from torch.utils.data import Dataset
class MyDataset(Dataset):
def __init__(self):
super().__init__()
... # set up dataset
dataset_len = N # number of samples in the full dataset
data_dims = (3, 32, 32) # data dims (not including batch)
# initialize the cache
self.cache = SharedCache(
size_limit_gib=32,
dataset_len=dataset_len,
data_dims=data_dims,
dtype=torch.uint8
)
def __getitem__(self, idx):
# retrieve data from cache if it's there
x = self.cache.get_slot(idx)
# x will be None if the cache slot was empty or OOB
if x is None:
x = ... # load data to uint8 tensor from disk
# try to cache x, no-op if idx is OOB of the cache
self.cache.set_slot(idx, x)
return x
```
"""
def __init__(
self,
size_limit_gib: int,
dataset_len: int,
data_dims: tuple[int, ...],
dtype: torch.dtype = torch.uint8,
) -> None:
"""
Args:
size_limit_gib (int): Maximum size of the cache in GiB.
dataset_len (int): Length (number of samples) of the full dataset.
data_dims (tuple[int, ...]): Dimensions of the data to be stored in the
cache. E.g. (C, H, W) for 2D image data. Does not include batch dim.
dtype (torch.dtype, optional): Torch data type of the dataset to cache.
Must be in the subset of torch dtypes with corresponding ctypes:
bool, uint8, int8, int16, int32, int64, float32, float64.
Defaults to torch.uint8 (this is usually best for jpg images).
"""
dtype_bytes = dtype.itemsize
slot_bytes = np.prod(data_dims) * dtype_bytes
dataset_bytes = slot_bytes * dataset_len
size_limit_bytes = size_limit_gib * BYTES_PER_GIB
# we allocate a flat 8-bit array to keep track of which samples are cached,
# which are not cached yet, and which are out of bounds of the cache
aux_bytes = dataset_len * torch.uint8.itemsize
ds_and_aux_bytes = dataset_bytes + aux_bytes
if ds_and_aux_bytes > size_limit_bytes:
cache_len = int((size_limit_bytes - aux_bytes) / slot_bytes)
print(
f"Dataset size ({ds_and_aux_bytes / BYTES_PER_GIB:.1f} GiB)"
+ f" exceeds cache limit ({size_limit_gib} GiB)."
+ f" Allocating space to cache {cache_len} / {dataset_len} samples."
)
else:
cache_len = dataset_len
print(
f"Dataset size ({ds_and_aux_bytes / BYTES_PER_GIB:.1f} GiB)"
+ f" fits in cache limit ({size_limit_gib} GiB)."
+ f" Allocating space to cache all {cache_len} samples."
)
shared_array_base = mp.Array(
C_DTYPES[dtype], int(np.prod(data_dims) * cache_len)
)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape((cache_len, *data_dims))
self._shm = torch.from_numpy(shared_array)
self._shm *= 0
shared_aux_base = mp.Array(C_DTYPES[torch.uint8], dataset_len)
shared_aux = np.ctypeslib.as_array(shared_aux_base.get_obj())
self._aux = torch.from_numpy(shared_aux)
self._aux *= 0
# only cache the first cache_len samples by index
self._aux[cache_len:] = SlotState.OOB.value
@property
def array(self) -> torch.Tensor:
"""Access the full underlying cache (just a tensor backed by shared memory).
Returns a torch tensor of shape (cache_len, *data_dims).
The dtype is whatever you specified when constructing the cache."""
return self._shm
@property
def aux_array(self) -> torch.Tensor:
"""Access the auxiliary array (just a tensor backed by shared memory).
The auxiliary array keeps track of which samples from the full dataset have been
cached, which samples are yet to be cached, and which are OOB.
Returns a shared memory torch uint8 tensor, shape (dataset_len,).
`self.aux_array[idx] == 0` means sample idx is not cached.
`self.aux_array[idx] == 1` means sample idx is cached.
`self.aux_array[idx] == 2` means sample idx is OOB.
"""
return self._aux
def __getitem__(self, idx: int):
return self.array[idx]
def __setitem__(self, idx: int, value: torch.Tensor):
self.array[idx] = value
def __len__(self):
return len(self.array)
def _slot_state(self, idx: int) -> SlotState:
"""Get the state of a slot in the cache. Raises an error if idx is outside
the range of the full dataset."""
if idx < 0 or idx >= len(self.aux_array):
raise IndexError(
f"Index {idx} out of bounds for dataset of length {len(self.aux_array)}"
)
return SlotState(self._aux[idx].item())
def set_slot(
self,
idx: int,
value: torch.Tensor,
allow_oob_idx: bool = True,
allow_overwrite: bool = False,
) -> None:
"""Set a slot in the cache to a value.
The main reason to use this method over __setitem__ is that we
allow you to call this method when idx is out of bounds of the
cache, but within the range of the full dataset.
In this case the method is a no-op when idx is out of bounds.
Args:
idx (int): Index of the slot to set.
value (torch.Tensor): Value to set the slot to.
allow_oob_idx (bool, optional): When False, raises an error if
idx is out of bounds of the cache. Defaults to True.
allow_overwrite (bool, optional): When False, raises an error if
the slot has any existing non-zero elements. Defaults to False.
"""
slot_state = self._slot_state(idx)
if slot_state == SlotState.OOB:
if not allow_oob_idx:
raise IndexError(
f"Index {idx} out of bounds of SharedCache of length {len(self)}"
)
return # no-op
if slot_state == SlotState.SET and not allow_overwrite:
raise RuntimeError(
f"Tried to overwrite non-empty slot {idx=} in SharedCache."
)
self[idx] = value
self.aux_array[idx] = SlotState.SET.value
def get_slot(
self,
idx: int,
allow_oob_idx: bool = True,
allow_empty_slot: bool = True,
) -> torch.Tensor | None:
"""Get the value of a slot in the cache.
The main reason to use this method over __getitem__ is that we
allow you to call this method when idx is out of bounds of the
cache, but within the range of the dataset.
In this case the method returns None when idx is out of bounds.
Args:
idx (int): Index of the slot to get.
allow_oob_idx (bool, optional): When False, raises an error if
idx is out of bounds of the cache. Defaults to True.
allow_empty_slot (bool, optional): When True, returns
None if the slot is empty. Otherwise, raises
an exception. Defaults to True.
"""
slot_state = self._slot_state(idx)
if slot_state == SlotState.OOB:
if not allow_oob_idx:
raise IndexError(
f"Index {idx} out of bounds of SharedCache of length {len(self)}"
)
return None
if slot_state == SlotState.EMPTY:
if allow_empty_slot:
return None
else:
raise RuntimeError(
f"Tried to read from an empty slot {idx=} in SharedCache."
)
return self[idx]
def clear(self) -> None:
"""Clear all slots in the cache."""
self._shm *= 0
self._aux *= 0
self._aux[len(self) :] = SlotState.OOB.value # noqa
def get_shm_size() -> int:
"""Get size of /dev/shm. The size limit of the shared memory cache
should not exceed this.
N.B. You may check the size of /dev/shm on the command line with `df -h`.
A simple way to (temporarily) change it is to run:
`mount -o remount,size=128G /dev/shm` (change to 128 GiB, for example).
Returns:
(int) Size of /dev/shm in GiB
"""
stats = os.statvfs("/dev/shm")
shm_bytes = stats.f_bsize * stats.f_blocks
shm_size = shm_bytes / BYTES_PER_GIB
return int(shm_size)