forked from real-stanford/diffusion_policy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay_buffer.py
588 lines (529 loc) · 20 KB
/
replay_buffer.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
from typing import Union, Dict, Optional
import os
import math
import numbers
import zarr
import numcodecs
import numpy as np
from functools import cached_property
def check_chunks_compatible(chunks: tuple, shape: tuple):
assert len(shape) == len(chunks)
for c in chunks:
assert isinstance(c, numbers.Integral)
assert c > 0
def rechunk_recompress_array(group, name,
chunks=None, chunk_length=None,
compressor=None, tmp_key='_temp'):
old_arr = group[name]
if chunks is None:
if chunk_length is not None:
chunks = (chunk_length,) + old_arr.chunks[1:]
else:
chunks = old_arr.chunks
check_chunks_compatible(chunks, old_arr.shape)
if compressor is None:
compressor = old_arr.compressor
if (chunks == old_arr.chunks) and (compressor == old_arr.compressor):
# no change
return old_arr
# rechunk recompress
group.move(name, tmp_key)
old_arr = group[tmp_key]
n_copied, n_skipped, n_bytes_copied = zarr.copy(
source=old_arr,
dest=group,
name=name,
chunks=chunks,
compressor=compressor,
)
del group[tmp_key]
arr = group[name]
return arr
def get_optimal_chunks(shape, dtype,
target_chunk_bytes=2e6,
max_chunk_length=None):
"""
Common shapes
T,D
T,N,D
T,H,W,C
T,N,H,W,C
"""
itemsize = np.dtype(dtype).itemsize
# reversed
rshape = list(shape[::-1])
if max_chunk_length is not None:
rshape[-1] = int(max_chunk_length)
split_idx = len(shape)-1
for i in range(len(shape)-1):
this_chunk_bytes = itemsize * np.prod(rshape[:i])
next_chunk_bytes = itemsize * np.prod(rshape[:i+1])
if this_chunk_bytes <= target_chunk_bytes \
and next_chunk_bytes > target_chunk_bytes:
split_idx = i
rchunks = rshape[:split_idx]
item_chunk_bytes = itemsize * np.prod(rshape[:split_idx])
this_max_chunk_length = rshape[split_idx]
next_chunk_length = min(this_max_chunk_length, math.ceil(
target_chunk_bytes / item_chunk_bytes))
rchunks.append(next_chunk_length)
len_diff = len(shape) - len(rchunks)
rchunks.extend([1] * len_diff)
chunks = tuple(rchunks[::-1])
# print(np.prod(chunks) * itemsize / target_chunk_bytes)
return chunks
class ReplayBuffer:
"""
Zarr-based temporal datastructure.
Assumes first dimension to be time. Only chunk in time dimension.
"""
def __init__(self,
root: Union[zarr.Group,
Dict[str,dict]]):
"""
Dummy constructor. Use copy_from* and create_from* class methods instead.
"""
assert('data' in root)
assert('meta' in root)
assert('episode_ends' in root['meta'])
for key, value in root['data'].items():
assert(value.shape[0] == root['meta']['episode_ends'][-1])
self.root = root
# ============= create constructors ===============
@classmethod
def create_empty_zarr(cls, storage=None, root=None):
if root is None:
if storage is None:
storage = zarr.MemoryStore()
root = zarr.group(store=storage)
data = root.require_group('data', overwrite=False)
meta = root.require_group('meta', overwrite=False)
if 'episode_ends' not in meta:
episode_ends = meta.zeros('episode_ends', shape=(0,), dtype=np.int64,
compressor=None, overwrite=False)
return cls(root=root)
@classmethod
def create_empty_numpy(cls):
root = {
'data': dict(),
'meta': {
'episode_ends': np.zeros((0,), dtype=np.int64)
}
}
return cls(root=root)
@classmethod
def create_from_group(cls, group, **kwargs):
if 'data' not in group:
# create from stratch
buffer = cls.create_empty_zarr(root=group, **kwargs)
else:
# already exist
buffer = cls(root=group, **kwargs)
return buffer
@classmethod
def create_from_path(cls, zarr_path, mode='r', **kwargs):
"""
Open a on-disk zarr directly (for dataset larger than memory).
Slower.
"""
group = zarr.open(os.path.expanduser(zarr_path), mode)
return cls.create_from_group(group, **kwargs)
# ============= copy constructors ===============
@classmethod
def copy_from_store(cls, src_store, store=None, keys=None,
chunks: Dict[str,tuple]=dict(),
compressors: Union[dict, str, numcodecs.abc.Codec]=dict(),
if_exists='replace',
**kwargs):
"""
Load to memory.
"""
src_root = zarr.group(src_store)
root = None
if store is None:
# numpy backend
meta = dict()
for key, value in src_root['meta'].items():
if len(value.shape) == 0:
meta[key] = np.array(value)
else:
meta[key] = value[:]
if keys is None:
keys = src_root['data'].keys()
data = dict()
for key in keys:
arr = src_root['data'][key]
data[key] = arr[:]
root = {
'meta': meta,
'data': data
}
else:
root = zarr.group(store=store)
# copy without recompression
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(source=src_store, dest=store,
source_path='/meta', dest_path='/meta', if_exists=if_exists)
data_group = root.create_group('data', overwrite=True)
if keys is None:
keys = src_root['data'].keys()
for key in keys:
value = src_root['data'][key]
cks = cls._resolve_array_chunks(
chunks=chunks, key=key, array=value)
cpr = cls._resolve_array_compressor(
compressors=compressors, key=key, array=value)
if cks == value.chunks and cpr == value.compressor:
# copy without recompression
this_path = '/data/' + key
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(
source=src_store, dest=store,
source_path=this_path, dest_path=this_path,
if_exists=if_exists
)
else:
# copy with recompression
n_copied, n_skipped, n_bytes_copied = zarr.copy(
source=value, dest=data_group, name=key,
chunks=cks, compressor=cpr, if_exists=if_exists
)
buffer = cls(root=root)
return buffer
@classmethod
def copy_from_path(cls, zarr_path, backend=None, store=None, keys=None,
chunks: Dict[str,tuple]=dict(),
compressors: Union[dict, str, numcodecs.abc.Codec]=dict(),
if_exists='replace',
**kwargs):
"""
Copy a on-disk zarr to in-memory compressed.
Recommended
"""
if backend == 'numpy':
print('backend argument is deprecated!')
store = None
group = zarr.open(os.path.expanduser(zarr_path), 'r')
return cls.copy_from_store(src_store=group.store, store=store,
keys=keys, chunks=chunks, compressors=compressors,
if_exists=if_exists, **kwargs)
# ============= save methods ===============
def save_to_store(self, store,
chunks: Optional[Dict[str,tuple]]=dict(),
compressors: Union[str, numcodecs.abc.Codec, dict]=dict(),
if_exists='replace',
**kwargs):
root = zarr.group(store)
if self.backend == 'zarr':
# recompression free copy
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(
source=self.root.store, dest=store,
source_path='/meta', dest_path='/meta', if_exists=if_exists)
else:
meta_group = root.create_group('meta', overwrite=True)
# save meta, no chunking
for key, value in self.root['meta'].items():
_ = meta_group.array(
name=key,
data=value,
shape=value.shape,
chunks=value.shape)
# save data, chunk
data_group = root.create_group('data', overwrite=True)
for key, value in self.root['data'].items():
cks = self._resolve_array_chunks(
chunks=chunks, key=key, array=value)
cpr = self._resolve_array_compressor(
compressors=compressors, key=key, array=value)
if isinstance(value, zarr.Array):
if cks == value.chunks and cpr == value.compressor:
# copy without recompression
this_path = '/data/' + key
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(
source=self.root.store, dest=store,
source_path=this_path, dest_path=this_path, if_exists=if_exists)
else:
# copy with recompression
n_copied, n_skipped, n_bytes_copied = zarr.copy(
source=value, dest=data_group, name=key,
chunks=cks, compressor=cpr, if_exists=if_exists
)
else:
# numpy
_ = data_group.array(
name=key,
data=value,
chunks=cks,
compressor=cpr
)
return store
def save_to_path(self, zarr_path,
chunks: Optional[Dict[str,tuple]]=dict(),
compressors: Union[str, numcodecs.abc.Codec, dict]=dict(),
if_exists='replace',
**kwargs):
store = zarr.DirectoryStore(os.path.expanduser(zarr_path))
return self.save_to_store(store, chunks=chunks,
compressors=compressors, if_exists=if_exists, **kwargs)
@staticmethod
def resolve_compressor(compressor='default'):
if compressor == 'default':
compressor = numcodecs.Blosc(cname='lz4', clevel=5,
shuffle=numcodecs.Blosc.NOSHUFFLE)
elif compressor == 'disk':
compressor = numcodecs.Blosc('zstd', clevel=5,
shuffle=numcodecs.Blosc.BITSHUFFLE)
return compressor
@classmethod
def _resolve_array_compressor(cls,
compressors: Union[dict, str, numcodecs.abc.Codec], key, array):
# allows compressor to be explicitly set to None
cpr = 'nil'
if isinstance(compressors, dict):
if key in compressors:
cpr = cls.resolve_compressor(compressors[key])
elif isinstance(array, zarr.Array):
cpr = array.compressor
else:
cpr = cls.resolve_compressor(compressors)
# backup default
if cpr == 'nil':
cpr = cls.resolve_compressor('default')
return cpr
@classmethod
def _resolve_array_chunks(cls,
chunks: Union[dict, tuple], key, array):
cks = None
if isinstance(chunks, dict):
if key in chunks:
cks = chunks[key]
elif isinstance(array, zarr.Array):
cks = array.chunks
elif isinstance(chunks, tuple):
cks = chunks
else:
raise TypeError(f"Unsupported chunks type {type(chunks)}")
# backup default
if cks is None:
cks = get_optimal_chunks(shape=array.shape, dtype=array.dtype)
# check
check_chunks_compatible(chunks=cks, shape=array.shape)
return cks
# ============= properties =================
@cached_property
def data(self):
return self.root['data']
@cached_property
def meta(self):
return self.root['meta']
def update_meta(self, data):
# sanitize data
np_data = dict()
for key, value in data.items():
if isinstance(value, np.ndarray):
np_data[key] = value
else:
arr = np.array(value)
if arr.dtype == object:
raise TypeError(f"Invalid value type {type(value)}")
np_data[key] = arr
meta_group = self.meta
if self.backend == 'zarr':
for key, value in np_data.items():
_ = meta_group.array(
name=key,
data=value,
shape=value.shape,
chunks=value.shape,
overwrite=True)
else:
meta_group.update(np_data)
return meta_group
@property
def episode_ends(self):
return self.meta['episode_ends']
def get_episode_idxs(self):
import numba
numba.jit(nopython=True)
def _get_episode_idxs(episode_ends):
result = np.zeros((episode_ends[-1],), dtype=np.int64)
for i in range(len(episode_ends)):
start = 0
if i > 0:
start = episode_ends[i-1]
end = episode_ends[i]
for idx in range(start, end):
result[idx] = i
return result
return _get_episode_idxs(self.episode_ends)
@property
def backend(self):
backend = 'numpy'
if isinstance(self.root, zarr.Group):
backend = 'zarr'
return backend
# =========== dict-like API ==============
def __repr__(self) -> str:
if self.backend == 'zarr':
return str(self.root.tree())
else:
return super().__repr__()
def keys(self):
return self.data.keys()
def values(self):
return self.data.values()
def items(self):
return self.data.items()
def __getitem__(self, key):
return self.data[key]
def __contains__(self, key):
return key in self.data
# =========== our API ==============
@property
def n_steps(self):
if len(self.episode_ends) == 0:
return 0
return self.episode_ends[-1]
@property
def n_episodes(self):
return len(self.episode_ends)
@property
def chunk_size(self):
if self.backend == 'zarr':
return next(iter(self.data.arrays()))[-1].chunks[0]
return None
@property
def episode_lengths(self):
ends = self.episode_ends[:]
ends = np.insert(ends, 0, 0)
lengths = np.diff(ends)
return lengths
def add_episode(self,
data: Dict[str, np.ndarray],
chunks: Optional[Dict[str,tuple]]=dict(),
compressors: Union[str, numcodecs.abc.Codec, dict]=dict()):
assert(len(data) > 0)
is_zarr = (self.backend == 'zarr')
curr_len = self.n_steps
episode_length = None
for key, value in data.items():
assert(len(value.shape) >= 1)
if episode_length is None:
episode_length = len(value)
else:
assert(episode_length == len(value))
new_len = curr_len + episode_length
for key, value in data.items():
new_shape = (new_len,) + value.shape[1:]
# create array
if key not in self.data:
if is_zarr:
cks = self._resolve_array_chunks(
chunks=chunks, key=key, array=value)
cpr = self._resolve_array_compressor(
compressors=compressors, key=key, array=value)
arr = self.data.zeros(name=key,
shape=new_shape,
chunks=cks,
dtype=value.dtype,
compressor=cpr)
else:
# copy data to prevent modify
arr = np.zeros(shape=new_shape, dtype=value.dtype)
self.data[key] = arr
else:
arr = self.data[key]
assert(value.shape[1:] == arr.shape[1:])
# same method for both zarr and numpy
if is_zarr:
arr.resize(new_shape)
else:
arr.resize(new_shape, refcheck=False)
# copy data
arr[-value.shape[0]:] = value
# append to episode ends
episode_ends = self.episode_ends
if is_zarr:
episode_ends.resize(episode_ends.shape[0] + 1)
else:
episode_ends.resize(episode_ends.shape[0] + 1, refcheck=False)
episode_ends[-1] = new_len
# rechunk
if is_zarr:
if episode_ends.chunks[0] < episode_ends.shape[0]:
rechunk_recompress_array(self.meta, 'episode_ends',
chunk_length=int(episode_ends.shape[0] * 1.5))
def drop_episode(self):
is_zarr = (self.backend == 'zarr')
episode_ends = self.episode_ends[:].copy()
assert(len(episode_ends) > 0)
start_idx = 0
if len(episode_ends) > 1:
start_idx = episode_ends[-2]
for key, value in self.data.items():
new_shape = (start_idx,) + value.shape[1:]
if is_zarr:
value.resize(new_shape)
else:
value.resize(new_shape, refcheck=False)
if is_zarr:
self.episode_ends.resize(len(episode_ends)-1)
else:
self.episode_ends.resize(len(episode_ends)-1, refcheck=False)
def pop_episode(self):
assert(self.n_episodes > 0)
episode = self.get_episode(self.n_episodes-1, copy=True)
self.drop_episode()
return episode
def extend(self, data):
self.add_episode(data)
def get_episode(self, idx, copy=False):
idx = list(range(len(self.episode_ends)))[idx]
start_idx = 0
if idx > 0:
start_idx = self.episode_ends[idx-1]
end_idx = self.episode_ends[idx]
result = self.get_steps_slice(start_idx, end_idx, copy=copy)
return result
def get_episode_slice(self, idx):
start_idx = 0
if idx > 0:
start_idx = self.episode_ends[idx-1]
end_idx = self.episode_ends[idx]
return slice(start_idx, end_idx)
def get_steps_slice(self, start, stop, step=None, copy=False):
_slice = slice(start, stop, step)
result = dict()
for key, value in self.data.items():
x = value[_slice]
if copy and isinstance(value, np.ndarray):
x = x.copy()
result[key] = x
return result
# =========== chunking =============
def get_chunks(self) -> dict:
assert self.backend == 'zarr'
chunks = dict()
for key, value in self.data.items():
chunks[key] = value.chunks
return chunks
def set_chunks(self, chunks: dict):
assert self.backend == 'zarr'
for key, value in chunks.items():
if key in self.data:
arr = self.data[key]
if value != arr.chunks:
check_chunks_compatible(chunks=value, shape=arr.shape)
rechunk_recompress_array(self.data, key, chunks=value)
def get_compressors(self) -> dict:
assert self.backend == 'zarr'
compressors = dict()
for key, value in self.data.items():
compressors[key] = value.compressor
return compressors
def set_compressors(self, compressors: dict):
assert self.backend == 'zarr'
for key, value in compressors.items():
if key in self.data:
arr = self.data[key]
compressor = self.resolve_compressor(value)
if compressor != arr.compressor:
rechunk_recompress_array(self.data, key, compressor=compressor)