solarian-diffusion 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffusion_policy/__init__.py +0 -0
- diffusion_policy/codecs/__init__.py +0 -0
- diffusion_policy/codecs/imagecodecs_numcodecs.py +1408 -0
- diffusion_policy/common/__init__.py +0 -0
- diffusion_policy/common/checkpoint_util.py +64 -0
- diffusion_policy/common/json_logger.py +117 -0
- diffusion_policy/common/normalize_util.py +251 -0
- diffusion_policy/common/pose_repr_util.py +112 -0
- diffusion_policy/common/pytorch_util.py +82 -0
- diffusion_policy/common/q_critic_util.py +135 -0
- diffusion_policy/common/replay_buffer.py +672 -0
- diffusion_policy/common/sampler.py +204 -0
- diffusion_policy/config/legacy/task/blockpush_lowdim_seed.yaml +34 -0
- diffusion_policy/config/legacy/task/blockpush_lowdim_seed_abs.yaml +34 -0
- diffusion_policy/config/legacy/task/can_image.yaml +64 -0
- diffusion_policy/config/legacy/task/can_image_abs.yaml +64 -0
- diffusion_policy/config/legacy/task/can_lowdim.yaml +45 -0
- diffusion_policy/config/legacy/task/can_lowdim_abs.yaml +46 -0
- diffusion_policy/config/legacy/task/kitchen_lowdim.yaml +33 -0
- diffusion_policy/config/legacy/task/kitchen_lowdim_abs.yaml +38 -0
- diffusion_policy/config/legacy/task/lift_abs.yaml +72 -0
- diffusion_policy/config/legacy/task/lift_image.yaml +64 -0
- diffusion_policy/config/legacy/task/lift_image_abs.yaml +63 -0
- diffusion_policy/config/legacy/task/lift_lowdim.yaml +46 -0
- diffusion_policy/config/legacy/task/lift_lowdim_abs.yaml +47 -0
- diffusion_policy/config/legacy/task/pusht_image.yaml +40 -0
- diffusion_policy/config/legacy/task/pusht_lowdim.yaml +34 -0
- diffusion_policy/config/legacy/task/real_pusht_image.yaml +47 -0
- diffusion_policy/config/legacy/task/square_abs.yaml +72 -0
- diffusion_policy/config/legacy/task/square_image.yaml +64 -0
- diffusion_policy/config/legacy/task/square_image_abs.yaml +64 -0
- diffusion_policy/config/legacy/task/square_lowdim.yaml +46 -0
- diffusion_policy/config/legacy/task/square_lowdim_abs.yaml +47 -0
- diffusion_policy/config/legacy/task/tool_hang_abs.yaml +71 -0
- diffusion_policy/config/legacy/task/tool_hang_image.yaml +63 -0
- diffusion_policy/config/legacy/task/tool_hang_image_abs.yaml +63 -0
- diffusion_policy/config/legacy/task/tool_hang_lowdim.yaml +45 -0
- diffusion_policy/config/legacy/task/tool_hang_lowdim_abs.yaml +46 -0
- diffusion_policy/config/legacy/task/transport_image.yaml +75 -0
- diffusion_policy/config/legacy/task/transport_image_abs.yaml +75 -0
- diffusion_policy/config/legacy/task/transport_lowdim.yaml +49 -0
- diffusion_policy/config/legacy/task/transport_lowdim_abs.yaml +49 -0
- diffusion_policy/config/legacy/task/umi.yaml +45 -0
- diffusion_policy/config/legacy/task/umi_image.yaml +36 -0
- diffusion_policy/config/legacy/task/umi_teleop_image.yaml +41 -0
- diffusion_policy/config/legacy/train_bet_lowdim_workspace.yaml +132 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_kitchen_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_pusht_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_real_hybrid_workspace.yaml +144 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_umi_teleop_hybrid_workspace.yaml +156 -0
- diffusion_policy/config/legacy/train_diffusion_unet_ddim_hybrid_workspace.yaml +140 -0
- diffusion_policy/config/legacy/train_diffusion_unet_ddim_lowdim_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_unet_hybrid_workspace.yaml +137 -0
- diffusion_policy/config/legacy/train_diffusion_unet_image_pretrained_workspace.yaml +149 -0
- diffusion_policy/config/legacy/train_diffusion_unet_image_workspace.yaml +150 -0
- diffusion_policy/config/legacy/train_diffusion_unet_lowdim_workspace.yaml +143 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_image_workspace.yaml +152 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_pretrained_workspace.yaml +152 -0
- diffusion_policy/config/legacy/train_diffusion_unet_timm_workspace.yaml +145 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_teleop_hybrid_workspace.yaml +153 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_timm_workspace.yaml +145 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_workspace.yaml +150 -0
- diffusion_policy/config/legacy/train_diffusion_unet_video_workspace.yaml +128 -0
- diffusion_policy/config/legacy/train_ibc_dfo_hybrid_workspace.yaml +111 -0
- diffusion_policy/config/legacy/train_ibc_dfo_lowdim_workspace.yaml +109 -0
- diffusion_policy/config/legacy/train_ibc_dfo_real_hybrid_workspace.yaml +111 -0
- diffusion_policy/config/legacy/train_robomimic_image_workspace.yaml +92 -0
- diffusion_policy/config/legacy/train_robomimic_lowdim_workspace.yaml +92 -0
- diffusion_policy/config/legacy/train_robomimic_real_image_workspace.yaml +92 -0
- diffusion_policy/config/rl_annotate.yaml +265 -0
- diffusion_policy/config/rl_annotate_per_frame.yaml +162 -0
- diffusion_policy/config/rl_annotate_per_frame_v4.yaml +162 -0
- diffusion_policy/config/rl_annotate_v2.yaml +141 -0
- diffusion_policy/config/rl_annotate_v3.yaml +165 -0
- diffusion_policy/config/task/lift.yaml +89 -0
- diffusion_policy/config/task/square.yaml +92 -0
- diffusion_policy/config/task/tool_hang.yaml +93 -0
- diffusion_policy/config/task/tool_hang_abs.yaml +71 -0
- diffusion_policy/config/task/umi.yaml +105 -0
- diffusion_policy/config/task/umi_bimanual.yaml +135 -0
- diffusion_policy/config/task/umi_cup_wild.yaml +106 -0
- diffusion_policy/config/task/umi_image.yaml +36 -0
- diffusion_policy/config/task/umi_pick_lego.yaml +106 -0
- diffusion_policy/config/task/umi_pick_single_lego_175.yaml +106 -0
- diffusion_policy/config/task/umi_pick_single_lego_bbox.yaml +135 -0
- diffusion_policy/config/task/umi_teleop_image.yaml +41 -0
- diffusion_policy/config/train_diffusion_transformer_umi_bimanual_workspace.yaml +185 -0
- diffusion_policy/config/train_diffusion_transformer_umi_workspace.yaml +185 -0
- diffusion_policy/config/train_diffusion_unet_image_workspace.yaml +152 -0
- diffusion_policy/config/train_diffusion_unet_timm_robomimic_workspace.yaml +152 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_cup_wild.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_awr.yaml +324 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_parl.yaml +399 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego.yaml +258 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_125.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_175.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_two_legos_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace.yaml +224 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego.yaml +213 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego_resume.yaml +213 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_pick_lego.yaml +254 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_red_cup.yaml +219 -0
- diffusion_policy/config/train_diffusion_unet_umi_bimanual_workspace.yaml +203 -0
- diffusion_policy/config/train_iql_umi.yaml +186 -0
- diffusion_policy/config/train_iql_umi_v2.yaml +212 -0
- diffusion_policy/config/train_iql_umi_v3.yaml +212 -0
- diffusion_policy/dataset/__init__.py +0 -0
- diffusion_policy/dataset/base_dataset.py +76 -0
- diffusion_policy/dataset/umi_dataset.py +652 -0
- diffusion_policy/env_runner/__init__.py +0 -0
- diffusion_policy/env_runner/base_image_runner.py +9 -0
- diffusion_policy/env_runner/real_pusht_image_runner.py +10 -0
- diffusion_policy/model/__init__.py +0 -0
- diffusion_policy/model/common/__init__.py +0 -0
- diffusion_policy/model/common/dict_of_tensor_mixin.py +38 -0
- diffusion_policy/model/common/lr_scheduler.py +46 -0
- diffusion_policy/model/common/module_attr_mixin.py +14 -0
- diffusion_policy/model/common/normalizer.py +353 -0
- diffusion_policy/model/common/tensor_util.py +960 -0
- diffusion_policy/model/diffusion/__init__.py +0 -0
- diffusion_policy/model/diffusion/conditional_unet1d.py +238 -0
- diffusion_policy/model/diffusion/conv1d_components.py +46 -0
- diffusion_policy/model/diffusion/ema_model.py +88 -0
- diffusion_policy/model/diffusion/mask_generator.py +221 -0
- diffusion_policy/model/diffusion/positional_embedding.py +17 -0
- diffusion_policy/model/vision/__init__.py +0 -0
- diffusion_policy/model/vision/crop_randomizer.py +288 -0
- diffusion_policy/model/vision/multi_image_obs_encoder.py +195 -0
- diffusion_policy/model/vision/timm_obs_encoder.py +525 -0
- diffusion_policy/policy/__init__.py +0 -0
- diffusion_policy/policy/base_image_policy.py +27 -0
- diffusion_policy/policy/diffusion_unet_image_policy.py +264 -0
- diffusion_policy/policy/diffusion_unet_timm_policy.py +607 -0
- diffusion_policy/policy/parl_action_optimizer.py +675 -0
- diffusion_policy/workspace/__init__.py +0 -0
- diffusion_policy/workspace/base_workspace.py +146 -0
- diffusion_policy/workspace/train_diffusion_unet_image_workspace.py +1585 -0
- solarian_diffusion/__init__.py +32 -0
- solarian_diffusion/config.py +31 -0
- solarian_diffusion/policy_server.py +316 -0
- solarian_diffusion/rtc.py +272 -0
- solarian_diffusion/service.py +16 -0
- solarian_diffusion/trainer.py +213 -0
- solarian_diffusion-0.1.0.dist-info/METADATA +126 -0
- solarian_diffusion-0.1.0.dist-info/RECORD +159 -0
- solarian_diffusion-0.1.0.dist-info/WHEEL +5 -0
- solarian_diffusion-0.1.0.dist-info/entry_points.txt +8 -0
- solarian_diffusion-0.1.0.dist-info/licenses/LICENSE +201 -0
- solarian_diffusion-0.1.0.dist-info/licenses/NOTICE +45 -0
- solarian_diffusion-0.1.0.dist-info/top_level.txt +3 -0
- umi/__init__.py +0 -0
- umi/common/__init__.py +0 -0
- umi/common/pose_util.py +115 -0
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
from typing import Union, Dict, Optional
|
|
2
|
+
import os
|
|
3
|
+
import math
|
|
4
|
+
import numbers
|
|
5
|
+
import signal
|
|
6
|
+
import threading
|
|
7
|
+
import zarr
|
|
8
|
+
import numcodecs
|
|
9
|
+
import numpy as np
|
|
10
|
+
from functools import cached_property
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class _DeferKeyboardInterrupt:
|
|
14
|
+
"""Context manager that defers SIGINT until the critical section exits.
|
|
15
|
+
|
|
16
|
+
Used to make `add_episode` / `drop_episode` atomic with respect to
|
|
17
|
+
Ctrl-C so we cannot leave the on-disk zarr in a state where the
|
|
18
|
+
per-key `data/*` arrays and `meta/episode_ends` disagree.
|
|
19
|
+
|
|
20
|
+
Only effective in the main thread (Python only delivers signals
|
|
21
|
+
there). Outside the main thread it's a no-op. The deferred signal,
|
|
22
|
+
if any, is re-raised by invoking the previous handler after the
|
|
23
|
+
section completes, so callers' KeyboardInterrupt logic still runs.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, label: str = 'ReplayBuffer'):
|
|
27
|
+
self._label = label
|
|
28
|
+
self._is_main = threading.current_thread() is threading.main_thread()
|
|
29
|
+
self._received = None
|
|
30
|
+
self._prev_handler = None
|
|
31
|
+
self._warned = False
|
|
32
|
+
|
|
33
|
+
def _handle(self, signum, frame):
|
|
34
|
+
if not self._warned:
|
|
35
|
+
print(
|
|
36
|
+
f'[{self._label}] Ctrl-C received; deferring until current '
|
|
37
|
+
f'zarr write finishes to avoid corrupting the replay buffer. '
|
|
38
|
+
f'Press again only if you accept losing this episode.',
|
|
39
|
+
flush=True,
|
|
40
|
+
)
|
|
41
|
+
self._warned = True
|
|
42
|
+
self._received = (signum, frame)
|
|
43
|
+
|
|
44
|
+
def __enter__(self):
|
|
45
|
+
if self._is_main:
|
|
46
|
+
self._prev_handler = signal.signal(signal.SIGINT, self._handle)
|
|
47
|
+
return self
|
|
48
|
+
|
|
49
|
+
def __exit__(self, exc_type, exc, tb):
|
|
50
|
+
if not self._is_main:
|
|
51
|
+
return False
|
|
52
|
+
signal.signal(signal.SIGINT, self._prev_handler)
|
|
53
|
+
if self._received is not None:
|
|
54
|
+
prev = self._prev_handler
|
|
55
|
+
signum, frame = self._received
|
|
56
|
+
if callable(prev) and prev not in (signal.SIG_IGN, signal.SIG_DFL):
|
|
57
|
+
prev(signum, frame)
|
|
58
|
+
else:
|
|
59
|
+
raise KeyboardInterrupt()
|
|
60
|
+
return False
|
|
61
|
+
|
|
62
|
+
def check_chunks_compatible(chunks: tuple, shape: tuple):
|
|
63
|
+
assert len(shape) == len(chunks)
|
|
64
|
+
for c in chunks:
|
|
65
|
+
assert isinstance(c, numbers.Integral)
|
|
66
|
+
assert c > 0
|
|
67
|
+
|
|
68
|
+
def rechunk_recompress_array(group, name,
|
|
69
|
+
chunks=None, chunk_length=None,
|
|
70
|
+
compressor=None, tmp_key='_temp'):
|
|
71
|
+
old_arr = group[name]
|
|
72
|
+
if chunks is None:
|
|
73
|
+
if chunk_length is not None:
|
|
74
|
+
chunks = (chunk_length,) + old_arr.chunks[1:]
|
|
75
|
+
else:
|
|
76
|
+
chunks = old_arr.chunks
|
|
77
|
+
check_chunks_compatible(chunks, old_arr.shape)
|
|
78
|
+
|
|
79
|
+
if compressor is None:
|
|
80
|
+
compressor = old_arr.compressor
|
|
81
|
+
|
|
82
|
+
if (chunks == old_arr.chunks) and (compressor == old_arr.compressor):
|
|
83
|
+
# no change
|
|
84
|
+
return old_arr
|
|
85
|
+
|
|
86
|
+
# rechunk recompress
|
|
87
|
+
group.move(name, tmp_key)
|
|
88
|
+
old_arr = group[tmp_key]
|
|
89
|
+
n_copied, n_skipped, n_bytes_copied = zarr.copy(
|
|
90
|
+
source=old_arr,
|
|
91
|
+
dest=group,
|
|
92
|
+
name=name,
|
|
93
|
+
chunks=chunks,
|
|
94
|
+
compressor=compressor,
|
|
95
|
+
)
|
|
96
|
+
del group[tmp_key]
|
|
97
|
+
arr = group[name]
|
|
98
|
+
return arr
|
|
99
|
+
|
|
100
|
+
def get_optimal_chunks(shape, dtype,
|
|
101
|
+
target_chunk_bytes=2e6,
|
|
102
|
+
max_chunk_length=None):
|
|
103
|
+
"""
|
|
104
|
+
Common shapes
|
|
105
|
+
T,D
|
|
106
|
+
T,N,D
|
|
107
|
+
T,H,W,C
|
|
108
|
+
T,N,H,W,C
|
|
109
|
+
"""
|
|
110
|
+
itemsize = np.dtype(dtype).itemsize
|
|
111
|
+
# reversed
|
|
112
|
+
rshape = list(shape[::-1])
|
|
113
|
+
if max_chunk_length is not None:
|
|
114
|
+
rshape[-1] = int(max_chunk_length)
|
|
115
|
+
split_idx = len(shape)-1
|
|
116
|
+
for i in range(len(shape)-1):
|
|
117
|
+
this_chunk_bytes = itemsize * np.prod(rshape[:i])
|
|
118
|
+
next_chunk_bytes = itemsize * np.prod(rshape[:i+1])
|
|
119
|
+
if this_chunk_bytes <= target_chunk_bytes \
|
|
120
|
+
and next_chunk_bytes > target_chunk_bytes:
|
|
121
|
+
split_idx = i
|
|
122
|
+
|
|
123
|
+
rchunks = rshape[:split_idx]
|
|
124
|
+
item_chunk_bytes = itemsize * np.prod(rshape[:split_idx])
|
|
125
|
+
this_max_chunk_length = rshape[split_idx]
|
|
126
|
+
next_chunk_length = min(this_max_chunk_length, math.ceil(
|
|
127
|
+
target_chunk_bytes / item_chunk_bytes))
|
|
128
|
+
rchunks.append(next_chunk_length)
|
|
129
|
+
len_diff = len(shape) - len(rchunks)
|
|
130
|
+
rchunks.extend([1] * len_diff)
|
|
131
|
+
chunks = tuple(rchunks[::-1])
|
|
132
|
+
# print(np.prod(chunks) * itemsize / target_chunk_bytes)
|
|
133
|
+
return chunks
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class ReplayBuffer:
|
|
137
|
+
"""
|
|
138
|
+
Zarr-based temporal datastructure.
|
|
139
|
+
Assumes first dimension to be time. Only chunk in time dimension.
|
|
140
|
+
"""
|
|
141
|
+
def __init__(self,
|
|
142
|
+
root: Union[zarr.Group,
|
|
143
|
+
Dict[str,dict]]):
|
|
144
|
+
"""
|
|
145
|
+
Dummy constructor. Use copy_from* and create_from* class methods instead.
|
|
146
|
+
"""
|
|
147
|
+
assert('data' in root)
|
|
148
|
+
assert('meta' in root)
|
|
149
|
+
assert('episode_ends' in root['meta'])
|
|
150
|
+
for key, value in root['data'].items():
|
|
151
|
+
assert(value.shape[0] == root['meta']['episode_ends'][-1])
|
|
152
|
+
self.root = root
|
|
153
|
+
|
|
154
|
+
# ============= create constructors ===============
|
|
155
|
+
@classmethod
|
|
156
|
+
def create_empty_zarr(cls, storage=None, root=None):
|
|
157
|
+
if root is None:
|
|
158
|
+
if storage is None:
|
|
159
|
+
storage = zarr.MemoryStore()
|
|
160
|
+
root = zarr.group(store=storage)
|
|
161
|
+
data = root.require_group('data', overwrite=False)
|
|
162
|
+
meta = root.require_group('meta', overwrite=False)
|
|
163
|
+
if 'episode_ends' not in meta:
|
|
164
|
+
episode_ends = meta.zeros('episode_ends', shape=(0,), dtype=np.int64,
|
|
165
|
+
compressor=None, overwrite=False)
|
|
166
|
+
return cls(root=root)
|
|
167
|
+
|
|
168
|
+
@classmethod
|
|
169
|
+
def create_empty_numpy(cls):
|
|
170
|
+
root = {
|
|
171
|
+
'data': dict(),
|
|
172
|
+
'meta': {
|
|
173
|
+
'episode_ends': np.zeros((0,), dtype=np.int64)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return cls(root=root)
|
|
177
|
+
|
|
178
|
+
@classmethod
|
|
179
|
+
def create_from_group(cls, group, **kwargs):
|
|
180
|
+
if 'data' not in group:
|
|
181
|
+
# create from stratch
|
|
182
|
+
buffer = cls.create_empty_zarr(root=group, **kwargs)
|
|
183
|
+
else:
|
|
184
|
+
# already exist
|
|
185
|
+
buffer = cls(root=group, **kwargs)
|
|
186
|
+
return buffer
|
|
187
|
+
|
|
188
|
+
@classmethod
|
|
189
|
+
def create_from_path(cls, zarr_path, mode='r', **kwargs):
|
|
190
|
+
"""
|
|
191
|
+
Open a on-disk zarr directly (for dataset larger than memory).
|
|
192
|
+
Slower.
|
|
193
|
+
"""
|
|
194
|
+
group = zarr.open(os.path.expanduser(zarr_path), mode)
|
|
195
|
+
return cls.create_from_group(group, **kwargs)
|
|
196
|
+
|
|
197
|
+
# ============= copy constructors ===============
|
|
198
|
+
@classmethod
|
|
199
|
+
def copy_from_store(cls, src_store, store=None, keys=None,
|
|
200
|
+
chunks: Dict[str,tuple]=dict(),
|
|
201
|
+
compressors: Union[dict, str, numcodecs.abc.Codec]=dict(),
|
|
202
|
+
if_exists='replace',
|
|
203
|
+
**kwargs):
|
|
204
|
+
"""
|
|
205
|
+
Load to memory.
|
|
206
|
+
"""
|
|
207
|
+
src_root = zarr.group(src_store)
|
|
208
|
+
root = None
|
|
209
|
+
if store is None:
|
|
210
|
+
# numpy backend
|
|
211
|
+
meta = dict()
|
|
212
|
+
for key, value in src_root['meta'].items():
|
|
213
|
+
if len(value.shape) == 0:
|
|
214
|
+
meta[key] = np.array(value)
|
|
215
|
+
else:
|
|
216
|
+
meta[key] = value[:]
|
|
217
|
+
|
|
218
|
+
if keys is None:
|
|
219
|
+
keys = src_root['data'].keys()
|
|
220
|
+
data = dict()
|
|
221
|
+
for key in keys:
|
|
222
|
+
arr = src_root['data'][key]
|
|
223
|
+
data[key] = arr[:]
|
|
224
|
+
|
|
225
|
+
root = {
|
|
226
|
+
'meta': meta,
|
|
227
|
+
'data': data
|
|
228
|
+
}
|
|
229
|
+
else:
|
|
230
|
+
root = zarr.group(store=store)
|
|
231
|
+
# copy without recompression
|
|
232
|
+
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(source=src_store, dest=store,
|
|
233
|
+
source_path='/meta', dest_path='/meta', if_exists=if_exists)
|
|
234
|
+
data_group = root.create_group('data', overwrite=True)
|
|
235
|
+
if keys is None:
|
|
236
|
+
keys = src_root['data'].keys()
|
|
237
|
+
for key in keys:
|
|
238
|
+
value = src_root['data'][key]
|
|
239
|
+
cks = cls._resolve_array_chunks(
|
|
240
|
+
chunks=chunks, key=key, array=value)
|
|
241
|
+
cpr = cls._resolve_array_compressor(
|
|
242
|
+
compressors=compressors, key=key, array=value)
|
|
243
|
+
if cks == value.chunks and cpr == value.compressor:
|
|
244
|
+
# copy without recompression
|
|
245
|
+
this_path = '/data/' + key
|
|
246
|
+
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(
|
|
247
|
+
source=src_store, dest=store,
|
|
248
|
+
source_path=this_path, dest_path=this_path,
|
|
249
|
+
if_exists=if_exists
|
|
250
|
+
)
|
|
251
|
+
else:
|
|
252
|
+
# copy with recompression
|
|
253
|
+
n_copied, n_skipped, n_bytes_copied = zarr.copy(
|
|
254
|
+
source=value, dest=data_group, name=key,
|
|
255
|
+
chunks=cks, compressor=cpr, if_exists=if_exists
|
|
256
|
+
)
|
|
257
|
+
buffer = cls(root=root)
|
|
258
|
+
return buffer
|
|
259
|
+
|
|
260
|
+
@classmethod
|
|
261
|
+
def copy_from_path(cls, zarr_path, backend=None, store=None, keys=None,
|
|
262
|
+
chunks: Dict[str,tuple]=dict(),
|
|
263
|
+
compressors: Union[dict, str, numcodecs.abc.Codec]=dict(),
|
|
264
|
+
if_exists='replace',
|
|
265
|
+
**kwargs):
|
|
266
|
+
"""
|
|
267
|
+
Copy a on-disk zarr to in-memory compressed.
|
|
268
|
+
Recommended
|
|
269
|
+
"""
|
|
270
|
+
if backend == 'numpy':
|
|
271
|
+
print('backend argument is deprecated!')
|
|
272
|
+
store = None
|
|
273
|
+
group = zarr.open(os.path.expanduser(zarr_path), 'r')
|
|
274
|
+
return cls.copy_from_store(src_store=group.store, store=store,
|
|
275
|
+
keys=keys, chunks=chunks, compressors=compressors,
|
|
276
|
+
if_exists=if_exists, **kwargs)
|
|
277
|
+
|
|
278
|
+
# ============= save methods ===============
|
|
279
|
+
def save_to_store(self, store,
|
|
280
|
+
chunks: Optional[Dict[str,tuple]]=dict(),
|
|
281
|
+
compressors: Union[str, numcodecs.abc.Codec, dict]=dict(),
|
|
282
|
+
if_exists='replace',
|
|
283
|
+
**kwargs):
|
|
284
|
+
|
|
285
|
+
root = zarr.group(store)
|
|
286
|
+
if self.backend == 'zarr':
|
|
287
|
+
# recompression free copy
|
|
288
|
+
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(
|
|
289
|
+
source=self.root.store, dest=store,
|
|
290
|
+
source_path='/meta', dest_path='/meta', if_exists=if_exists)
|
|
291
|
+
else:
|
|
292
|
+
meta_group = root.create_group('meta', overwrite=True)
|
|
293
|
+
# save meta, no chunking
|
|
294
|
+
for key, value in self.root['meta'].items():
|
|
295
|
+
_ = meta_group.array(
|
|
296
|
+
name=key,
|
|
297
|
+
data=value,
|
|
298
|
+
shape=value.shape,
|
|
299
|
+
chunks=value.shape)
|
|
300
|
+
|
|
301
|
+
# save data, chunk
|
|
302
|
+
data_group = root.create_group('data', overwrite=True)
|
|
303
|
+
for key, value in self.root['data'].items():
|
|
304
|
+
cks = self._resolve_array_chunks(
|
|
305
|
+
chunks=chunks, key=key, array=value)
|
|
306
|
+
cpr = self._resolve_array_compressor(
|
|
307
|
+
compressors=compressors, key=key, array=value)
|
|
308
|
+
if isinstance(value, zarr.Array):
|
|
309
|
+
if cks == value.chunks and cpr == value.compressor:
|
|
310
|
+
# copy without recompression
|
|
311
|
+
this_path = '/data/' + key
|
|
312
|
+
n_copied, n_skipped, n_bytes_copied = zarr.copy_store(
|
|
313
|
+
source=self.root.store, dest=store,
|
|
314
|
+
source_path=this_path, dest_path=this_path, if_exists=if_exists)
|
|
315
|
+
else:
|
|
316
|
+
# copy with recompression
|
|
317
|
+
n_copied, n_skipped, n_bytes_copied = zarr.copy(
|
|
318
|
+
source=value, dest=data_group, name=key,
|
|
319
|
+
chunks=cks, compressor=cpr, if_exists=if_exists
|
|
320
|
+
)
|
|
321
|
+
else:
|
|
322
|
+
# numpy
|
|
323
|
+
_ = data_group.array(
|
|
324
|
+
name=key,
|
|
325
|
+
data=value,
|
|
326
|
+
chunks=cks,
|
|
327
|
+
compressor=cpr
|
|
328
|
+
)
|
|
329
|
+
return store
|
|
330
|
+
|
|
331
|
+
def save_to_path(self, zarr_path,
|
|
332
|
+
chunks: Optional[Dict[str,tuple]]=dict(),
|
|
333
|
+
compressors: Union[str, numcodecs.abc.Codec, dict]=dict(),
|
|
334
|
+
if_exists='replace',
|
|
335
|
+
**kwargs):
|
|
336
|
+
store = zarr.DirectoryStore(os.path.expanduser(zarr_path))
|
|
337
|
+
return self.save_to_store(store, chunks=chunks,
|
|
338
|
+
compressors=compressors, if_exists=if_exists, **kwargs)
|
|
339
|
+
|
|
340
|
+
@staticmethod
|
|
341
|
+
def resolve_compressor(compressor='default'):
|
|
342
|
+
if compressor == 'default':
|
|
343
|
+
compressor = numcodecs.Blosc(cname='lz4', clevel=5,
|
|
344
|
+
shuffle=numcodecs.Blosc.NOSHUFFLE)
|
|
345
|
+
elif compressor == 'disk':
|
|
346
|
+
compressor = numcodecs.Blosc('zstd', clevel=5,
|
|
347
|
+
shuffle=numcodecs.Blosc.BITSHUFFLE)
|
|
348
|
+
return compressor
|
|
349
|
+
|
|
350
|
+
@classmethod
|
|
351
|
+
def _resolve_array_compressor(cls,
|
|
352
|
+
compressors: Union[dict, str, numcodecs.abc.Codec], key, array):
|
|
353
|
+
# allows compressor to be explicitly set to None
|
|
354
|
+
cpr = 'nil'
|
|
355
|
+
if isinstance(compressors, dict):
|
|
356
|
+
if key in compressors:
|
|
357
|
+
cpr = cls.resolve_compressor(compressors[key])
|
|
358
|
+
elif isinstance(array, zarr.Array):
|
|
359
|
+
cpr = array.compressor
|
|
360
|
+
else:
|
|
361
|
+
cpr = cls.resolve_compressor(compressors)
|
|
362
|
+
# backup default
|
|
363
|
+
if cpr == 'nil':
|
|
364
|
+
cpr = cls.resolve_compressor('default')
|
|
365
|
+
return cpr
|
|
366
|
+
|
|
367
|
+
@classmethod
|
|
368
|
+
def _resolve_array_chunks(cls,
|
|
369
|
+
chunks: Union[dict, tuple], key, array):
|
|
370
|
+
cks = None
|
|
371
|
+
if isinstance(chunks, dict):
|
|
372
|
+
if key in chunks:
|
|
373
|
+
cks = chunks[key]
|
|
374
|
+
elif isinstance(array, zarr.Array):
|
|
375
|
+
cks = array.chunks
|
|
376
|
+
elif isinstance(chunks, tuple):
|
|
377
|
+
cks = chunks
|
|
378
|
+
else:
|
|
379
|
+
raise TypeError(f"Unsupported chunks type {type(chunks)}")
|
|
380
|
+
# backup default
|
|
381
|
+
if cks is None:
|
|
382
|
+
cks = get_optimal_chunks(shape=array.shape, dtype=array.dtype)
|
|
383
|
+
# check
|
|
384
|
+
check_chunks_compatible(chunks=cks, shape=array.shape)
|
|
385
|
+
return cks
|
|
386
|
+
|
|
387
|
+
# ============= properties =================
|
|
388
|
+
@cached_property
|
|
389
|
+
def data(self):
|
|
390
|
+
return self.root['data']
|
|
391
|
+
|
|
392
|
+
@cached_property
|
|
393
|
+
def meta(self):
|
|
394
|
+
return self.root['meta']
|
|
395
|
+
|
|
396
|
+
def update_meta(self, data):
|
|
397
|
+
# sanitize data
|
|
398
|
+
np_data = dict()
|
|
399
|
+
for key, value in data.items():
|
|
400
|
+
if isinstance(value, np.ndarray):
|
|
401
|
+
np_data[key] = value
|
|
402
|
+
else:
|
|
403
|
+
arr = np.array(value)
|
|
404
|
+
if arr.dtype == object:
|
|
405
|
+
raise TypeError(f"Invalid value type {type(value)}")
|
|
406
|
+
np_data[key] = arr
|
|
407
|
+
|
|
408
|
+
meta_group = self.meta
|
|
409
|
+
if self.backend == 'zarr':
|
|
410
|
+
for key, value in np_data.items():
|
|
411
|
+
_ = meta_group.array(
|
|
412
|
+
name=key,
|
|
413
|
+
data=value,
|
|
414
|
+
shape=value.shape,
|
|
415
|
+
chunks=value.shape,
|
|
416
|
+
overwrite=True)
|
|
417
|
+
else:
|
|
418
|
+
meta_group.update(np_data)
|
|
419
|
+
|
|
420
|
+
return meta_group
|
|
421
|
+
|
|
422
|
+
@property
|
|
423
|
+
def episode_ends(self):
|
|
424
|
+
return self.meta['episode_ends']
|
|
425
|
+
|
|
426
|
+
def get_episode_idxs(self):
|
|
427
|
+
import numba
|
|
428
|
+
numba.jit(nopython=True)
|
|
429
|
+
def _get_episode_idxs(episode_ends):
|
|
430
|
+
result = np.zeros((episode_ends[-1],), dtype=np.int64)
|
|
431
|
+
for i in range(len(episode_ends)):
|
|
432
|
+
start = 0
|
|
433
|
+
if i > 0:
|
|
434
|
+
start = episode_ends[i-1]
|
|
435
|
+
end = episode_ends[i]
|
|
436
|
+
for idx in range(start, end):
|
|
437
|
+
result[idx] = i
|
|
438
|
+
return result
|
|
439
|
+
return _get_episode_idxs(self.episode_ends)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
@property
|
|
443
|
+
def backend(self):
|
|
444
|
+
backend = 'numpy'
|
|
445
|
+
if isinstance(self.root, zarr.Group):
|
|
446
|
+
backend = 'zarr'
|
|
447
|
+
return backend
|
|
448
|
+
|
|
449
|
+
# =========== dict-like API ==============
|
|
450
|
+
def __repr__(self) -> str:
|
|
451
|
+
if self.backend == 'zarr':
|
|
452
|
+
return str(self.root.tree())
|
|
453
|
+
else:
|
|
454
|
+
return super().__repr__()
|
|
455
|
+
|
|
456
|
+
def keys(self):
|
|
457
|
+
return self.data.keys()
|
|
458
|
+
|
|
459
|
+
def values(self):
|
|
460
|
+
return self.data.values()
|
|
461
|
+
|
|
462
|
+
def items(self):
|
|
463
|
+
return self.data.items()
|
|
464
|
+
|
|
465
|
+
def __getitem__(self, key):
|
|
466
|
+
return self.data[key]
|
|
467
|
+
|
|
468
|
+
def __contains__(self, key):
|
|
469
|
+
return key in self.data
|
|
470
|
+
|
|
471
|
+
# =========== our API ==============
|
|
472
|
+
@property
|
|
473
|
+
def n_steps(self):
|
|
474
|
+
if len(self.episode_ends) == 0:
|
|
475
|
+
return 0
|
|
476
|
+
return self.episode_ends[-1]
|
|
477
|
+
|
|
478
|
+
@property
|
|
479
|
+
def n_episodes(self):
|
|
480
|
+
return len(self.episode_ends)
|
|
481
|
+
|
|
482
|
+
@property
|
|
483
|
+
def chunk_size(self):
|
|
484
|
+
if self.backend == 'zarr':
|
|
485
|
+
return next(iter(self.data.arrays()))[-1].chunks[0]
|
|
486
|
+
return None
|
|
487
|
+
|
|
488
|
+
@property
|
|
489
|
+
def episode_lengths(self):
|
|
490
|
+
ends = self.episode_ends[:]
|
|
491
|
+
ends = np.insert(ends, 0, 0)
|
|
492
|
+
lengths = np.diff(ends)
|
|
493
|
+
return lengths
|
|
494
|
+
|
|
495
|
+
def add_episode(self,
|
|
496
|
+
data: Dict[str, np.ndarray],
|
|
497
|
+
chunks: Optional[Dict[str,tuple]]=dict(),
|
|
498
|
+
compressors: Union[str, numcodecs.abc.Codec, dict]=dict()):
|
|
499
|
+
assert(len(data) > 0)
|
|
500
|
+
is_zarr = (self.backend == 'zarr')
|
|
501
|
+
|
|
502
|
+
curr_len = self.n_steps
|
|
503
|
+
episode_length = None
|
|
504
|
+
for key, value in data.items():
|
|
505
|
+
assert(len(value.shape) >= 1)
|
|
506
|
+
if episode_length is None:
|
|
507
|
+
episode_length = len(value)
|
|
508
|
+
else:
|
|
509
|
+
assert(episode_length == len(value))
|
|
510
|
+
new_len = curr_len + episode_length
|
|
511
|
+
|
|
512
|
+
# Schema-divergence guard: every prior episode wrote a known
|
|
513
|
+
# set of keys (the keys currently present in `self.data`). If
|
|
514
|
+
# this episode is missing any of them, appending would grow
|
|
515
|
+
# `episode_ends` without growing those columns, leaving the
|
|
516
|
+
# zarr in a state where `value.shape[0] != episode_ends[-1]`
|
|
517
|
+
# for the missing keys. Refuse to write instead of silently
|
|
518
|
+
# corrupting the buffer.
|
|
519
|
+
if curr_len > 0:
|
|
520
|
+
existing_keys = set(self.data.keys())
|
|
521
|
+
new_keys = set(data.keys())
|
|
522
|
+
missing = existing_keys - new_keys
|
|
523
|
+
if missing:
|
|
524
|
+
raise ValueError(
|
|
525
|
+
f'add_episode schema mismatch: replay buffer already '
|
|
526
|
+
f'contains key(s) {sorted(missing)} from previous '
|
|
527
|
+
f'episode(s) but this episode does not provide them. '
|
|
528
|
+
f'Refusing to write to avoid desyncing episode_ends '
|
|
529
|
+
f'from data arrays. Provide all existing keys (use a '
|
|
530
|
+
f'sentinel array if needed) or start a fresh '
|
|
531
|
+
f'replay_buffer.zarr.'
|
|
532
|
+
)
|
|
533
|
+
|
|
534
|
+
# The block below is the on-disk commit. It MUST run to
|
|
535
|
+
# completion atomically w.r.t. Ctrl-C, otherwise we get the
|
|
536
|
+
# exact "value.shape[0] == episode_ends[-1]" assertion failure
|
|
537
|
+
# at next open. SIGINT is captured and re-delivered after the
|
|
538
|
+
# commit so the caller's KeyboardInterrupt handling still runs.
|
|
539
|
+
with _DeferKeyboardInterrupt():
|
|
540
|
+
for key, value in data.items():
|
|
541
|
+
new_shape = (new_len,) + value.shape[1:]
|
|
542
|
+
# create array
|
|
543
|
+
if key not in self.data:
|
|
544
|
+
if is_zarr:
|
|
545
|
+
cks = self._resolve_array_chunks(
|
|
546
|
+
chunks=chunks, key=key, array=value)
|
|
547
|
+
cpr = self._resolve_array_compressor(
|
|
548
|
+
compressors=compressors, key=key, array=value)
|
|
549
|
+
arr = self.data.zeros(name=key,
|
|
550
|
+
shape=new_shape,
|
|
551
|
+
chunks=cks,
|
|
552
|
+
dtype=value.dtype,
|
|
553
|
+
compressor=cpr)
|
|
554
|
+
else:
|
|
555
|
+
# copy data to prevent modify
|
|
556
|
+
arr = np.zeros(shape=new_shape, dtype=value.dtype)
|
|
557
|
+
self.data[key] = arr
|
|
558
|
+
else:
|
|
559
|
+
arr = self.data[key]
|
|
560
|
+
assert(value.shape[1:] == arr.shape[1:])
|
|
561
|
+
# same method for both zarr and numpy
|
|
562
|
+
if is_zarr:
|
|
563
|
+
arr.resize(new_shape)
|
|
564
|
+
else:
|
|
565
|
+
arr.resize(new_shape, refcheck=False)
|
|
566
|
+
# copy data
|
|
567
|
+
arr[-value.shape[0]:] = value
|
|
568
|
+
|
|
569
|
+
# append to episode ends
|
|
570
|
+
episode_ends = self.episode_ends
|
|
571
|
+
if is_zarr:
|
|
572
|
+
episode_ends.resize(episode_ends.shape[0] + 1)
|
|
573
|
+
else:
|
|
574
|
+
episode_ends.resize(episode_ends.shape[0] + 1, refcheck=False)
|
|
575
|
+
episode_ends[-1] = new_len
|
|
576
|
+
|
|
577
|
+
# rechunk
|
|
578
|
+
if is_zarr:
|
|
579
|
+
if episode_ends.chunks[0] < episode_ends.shape[0]:
|
|
580
|
+
rechunk_recompress_array(self.meta, 'episode_ends',
|
|
581
|
+
chunk_length=int(episode_ends.shape[0] * 1.5))
|
|
582
|
+
|
|
583
|
+
def drop_episode(self):
|
|
584
|
+
is_zarr = (self.backend == 'zarr')
|
|
585
|
+
episode_ends = self.episode_ends[:].copy()
|
|
586
|
+
assert(len(episode_ends) > 0)
|
|
587
|
+
start_idx = 0
|
|
588
|
+
if len(episode_ends) > 1:
|
|
589
|
+
start_idx = episode_ends[-2]
|
|
590
|
+
# Same atomicity requirement as add_episode: a SIGINT between
|
|
591
|
+
# the per-key resize and the episode_ends shrink would leave
|
|
592
|
+
# `value.shape[0] > episode_ends[-1]` on disk.
|
|
593
|
+
with _DeferKeyboardInterrupt():
|
|
594
|
+
for key, value in self.data.items():
|
|
595
|
+
new_shape = (start_idx,) + value.shape[1:]
|
|
596
|
+
if is_zarr:
|
|
597
|
+
value.resize(new_shape)
|
|
598
|
+
else:
|
|
599
|
+
value.resize(new_shape, refcheck=False)
|
|
600
|
+
if is_zarr:
|
|
601
|
+
self.episode_ends.resize(len(episode_ends)-1)
|
|
602
|
+
else:
|
|
603
|
+
self.episode_ends.resize(len(episode_ends)-1, refcheck=False)
|
|
604
|
+
|
|
605
|
+
def pop_episode(self):
|
|
606
|
+
assert(self.n_episodes > 0)
|
|
607
|
+
episode = self.get_episode(self.n_episodes-1, copy=True)
|
|
608
|
+
self.drop_episode()
|
|
609
|
+
return episode
|
|
610
|
+
|
|
611
|
+
def extend(self, data):
|
|
612
|
+
self.add_episode(data)
|
|
613
|
+
|
|
614
|
+
def get_episode(self, idx, copy=False):
|
|
615
|
+
idx = list(range(len(self.episode_ends)))[idx]
|
|
616
|
+
start_idx = 0
|
|
617
|
+
if idx > 0:
|
|
618
|
+
start_idx = self.episode_ends[idx-1]
|
|
619
|
+
end_idx = self.episode_ends[idx]
|
|
620
|
+
result = self.get_steps_slice(start_idx, end_idx, copy=copy)
|
|
621
|
+
return result
|
|
622
|
+
|
|
623
|
+
def get_episode_slice(self, idx):
|
|
624
|
+
start_idx = 0
|
|
625
|
+
if idx > 0:
|
|
626
|
+
start_idx = self.episode_ends[idx-1]
|
|
627
|
+
end_idx = self.episode_ends[idx]
|
|
628
|
+
return slice(start_idx, end_idx)
|
|
629
|
+
|
|
630
|
+
def get_steps_slice(self, start, stop, step=None, copy=False):
|
|
631
|
+
_slice = slice(start, stop, step)
|
|
632
|
+
|
|
633
|
+
result = dict()
|
|
634
|
+
for key, value in self.data.items():
|
|
635
|
+
x = value[_slice]
|
|
636
|
+
if copy and isinstance(value, np.ndarray):
|
|
637
|
+
x = x.copy()
|
|
638
|
+
result[key] = x
|
|
639
|
+
return result
|
|
640
|
+
|
|
641
|
+
# =========== chunking =============
|
|
642
|
+
def get_chunks(self) -> dict:
|
|
643
|
+
assert self.backend == 'zarr'
|
|
644
|
+
chunks = dict()
|
|
645
|
+
for key, value in self.data.items():
|
|
646
|
+
chunks[key] = value.chunks
|
|
647
|
+
return chunks
|
|
648
|
+
|
|
649
|
+
def set_chunks(self, chunks: dict):
|
|
650
|
+
assert self.backend == 'zarr'
|
|
651
|
+
for key, value in chunks.items():
|
|
652
|
+
if key in self.data:
|
|
653
|
+
arr = self.data[key]
|
|
654
|
+
if value != arr.chunks:
|
|
655
|
+
check_chunks_compatible(chunks=value, shape=arr.shape)
|
|
656
|
+
rechunk_recompress_array(self.data, key, chunks=value)
|
|
657
|
+
|
|
658
|
+
def get_compressors(self) -> dict:
|
|
659
|
+
assert self.backend == 'zarr'
|
|
660
|
+
compressors = dict()
|
|
661
|
+
for key, value in self.data.items():
|
|
662
|
+
compressors[key] = value.compressor
|
|
663
|
+
return compressors
|
|
664
|
+
|
|
665
|
+
def set_compressors(self, compressors: dict):
|
|
666
|
+
assert self.backend == 'zarr'
|
|
667
|
+
for key, value in compressors.items():
|
|
668
|
+
if key in self.data:
|
|
669
|
+
arr = self.data[key]
|
|
670
|
+
compressor = self.resolve_compressor(value)
|
|
671
|
+
if compressor != arr.compressor:
|
|
672
|
+
rechunk_recompress_array(self.data, key, compressor=compressor)
|