-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemray-array.py
More file actions
383 lines (315 loc) · 12.4 KB
/
Copy pathmemray-array.py
File metadata and controls
383 lines (315 loc) · 12.4 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
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
import json
import os
import time
from pathlib import Path
import click
import memray
import numpy as np
@click.group()
def cli():
Path("profiles").mkdir(parents=True, exist_ok=True)
store_prefix_option = click.option(
"--store-prefix",
default="data",
help="Zarr store prefix, should be a local directory or an object store",
)
compress_option = click.option(
"--compress/--no-compress", is_flag=True, default=True, help="Enable compression"
)
library_option = click.option(
"--library",
default="fsspec",
type=click.Choice(
[
"fsspec",
"obstore",
"icechunk",
"zarrista",
"zarrista-obstore",
"zarrista-icechunk",
]
),
help="Library to use for file IO",
)
@cli.command()
@store_prefix_option
@compress_option
@library_option
def read(store_prefix, compress, library):
fs = filesystem(store_prefix)
compressed = "compressed" if compress else "uncompressed"
if library in ("zarrista", "zarrista-obstore"):
import zarrista
version = zarrista.__version__
store_kind = "obstore" if library == "zarrista-obstore" else "local"
label = f"read-{fs}-zarrista-{version}-{store_kind}-{compressed}"
path = f"/zarrista-{version}-{store_kind}-{compressed}.zarr"
profile = f"profiles/{label}.bin"
rm(profile)
record_versions(label, library)
if store_kind == "obstore":
# obstore is only supported by zarrista's async API.
import asyncio
from zarrista import AsyncArray
store = get_obstore_store(fs, store_prefix)
async def read_async():
z = await AsyncArray.open(store, path)
return (await z[:]).to_numpy()
with memray.Tracker(profile, native_traces=True):
arr = asyncio.run(read_async())
print(arr.shape)
del arr
time.sleep(1)
else:
from zarrista import Array
from zarrista.store import FilesystemStore
store = FilesystemStore(store_prefix)
with memray.Tracker(profile, native_traces=True):
z = Array.open(store, path)
arr = z[:].to_numpy()
print(arr.shape)
del arr
del z
time.sleep(1)
elif library == "zarrista-icechunk":
# Read-only: zarrista's icechunk bridge reconstructs a separate Rust
# session inside the extension, so writes never reach the Python repo.
# The data read here is written by the zarr-python + icechunk target.
import asyncio
import zarrista
from icechunk import Repository, Storage
from zarrista import AsyncArray
version = zarrista.__version__
label = f"read-{fs}-zarrista-{version}-icechunk-{compressed}"
profile = f"profiles/{label}.bin"
rm(profile)
record_versions(label, library)
icechunk_storage = Storage.new_local_filesystem(store_prefix)
repo = Repository.open(icechunk_storage)
session = repo.readonly_session("main")
async def read_async():
# The array is at the repo root, where the zarr+icechunk write puts it.
z = await AsyncArray.open(session)
return (await z[:]).to_numpy()
with memray.Tracker(profile, native_traces=True):
arr = asyncio.run(read_async())
print(arr.shape)
del arr
time.sleep(1)
elif library == "icechunk":
from icechunk import Repository, Storage
icechunk_storage = Storage.new_local_filesystem(store_prefix)
repo = Repository.open(icechunk_storage)
session = repo.readonly_session("main")
store = session.store
import zarr
zarr_version = find_zarr_version()
label = f"read-{fs}-zarr-{zarr_version}-{library}-{compressed}"
profile = f"profiles/{label}.bin"
rm(profile)
record_versions(label, library)
with memray.Tracker(profile, native_traces=True):
z = zarr.open(store, mode="r")
arr = z[:]
print(arr.shape)
else:
import zarr
zarr_version = find_zarr_version()
label = f"read-{fs}-zarr-{zarr_version}-{library}-{compressed}"
store = f"{store_prefix}/zarr-{zarr_version}-{library}-{compressed}.zarr"
store = get_zarr_store(fs, library, store)
profile = f"profiles/{label}.bin"
rm(profile)
record_versions(label, library)
with memray.Tracker(profile, native_traces=True):
z = zarr.open(store, mode="r")
arr = z[:]
print(arr.shape)
@cli.command()
@store_prefix_option
@compress_option
@library_option
def write(store_prefix, compress, library):
fs = filesystem(store_prefix)
compressed = "compressed" if compress else "uncompressed"
if library == "zarrista-icechunk":
# zarrista writes to a session it reconstructs internally, so they never
# reach the Python repo and `commit` reports "no changes made to the
# session". Read profiles for this library use data written by
# `write --library icechunk` instead.
raise click.ClickException(
"zarrista cannot write to icechunk yet (its session bridge is read-only). "
"Write the data with `--library icechunk`, then read it back with "
"`read --library zarrista-icechunk`."
)
if library in ("zarrista", "zarrista-obstore"):
import zarrista
from zarrista import ArrayBuilder, ChunkGrid, DataType, FillValue, codec
version = zarrista.__version__
store_kind = "obstore" if library == "zarrista-obstore" else "local"
label = f"write-{fs}-zarrista-{version}-{store_kind}-{compressed}"
path = f"/zarrista-{version}-{store_kind}-{compressed}.zarr"
profile = f"profiles/{label}.bin"
rm(profile)
record_versions(label, library)
def build(arr):
grid = ChunkGrid.regular(arr.shape, chunk_shape=arr.shape)
dtype = DataType.from_string(np.dtype(arr.dtype).name)
fill_value = FillValue(np.zeros((), dtype=arr.dtype).tobytes())
builder = ArrayBuilder(grid, dtype, fill_value)
if compress:
builder = builder.compressors([codec.zstd(3, checksum=False)])
return builder
if store_kind == "obstore":
# obstore is only supported by zarrista's async API.
import asyncio
store = get_obstore_store(fs, store_prefix)
async def write_async(arr):
z = await build(arr).create_async(store, path)
await z.store_array_subset(Ellipsis, arr)
with memray.Tracker(profile, native_traces=True):
rng = np.random.default_rng()
arr = rng.random((5000, 5000), dtype=np.float32) # 100MB
asyncio.run(write_async(arr))
del arr
time.sleep(1)
else:
from zarrista.store import FilesystemStore
store = FilesystemStore(store_prefix)
with memray.Tracker(profile, native_traces=True):
rng = np.random.default_rng()
arr = rng.random((5000, 5000), dtype=np.float32) # 100MB
z = build(arr).create(store, path)
z[:] = arr
del arr
del z
time.sleep(1)
elif library == "icechunk":
from icechunk import Repository, Storage
icechunk_storage = Storage.new_local_filesystem(store_prefix)
if Repository.exists(icechunk_storage):
repo = Repository.open(icechunk_storage)
else:
repo = Repository.create(icechunk_storage)
with repo.transaction(branch="main", message="create data") as store:
import zarr
zarr_version = find_zarr_version()
label = f"write-{fs}-zarr-{zarr_version}-{library}-{compressed}"
profile = f"profiles/{label}.bin"
rm(profile)
record_versions(label, library)
with memray.Tracker(profile, native_traces=True):
rng = np.random.default_rng()
arr = rng.random((5000, 5000), dtype=np.float32) # 100MB
kwargs = dict(config=dict(write_empty_chunks=True))
if not compress:
kwargs["compressors"] = None
z = zarr.create_array(
store=store,
shape=arr.shape,
dtype=arr.dtype,
chunks=arr.shape,
overwrite=True,
**kwargs,
)
z[:] = arr
else:
import zarr
zarr_version = find_zarr_version()
label = f"write-{fs}-zarr-{zarr_version}-{library}-{compressed}"
store = f"{store_prefix}/zarr-{zarr_version}-{library}-{compressed}.zarr"
store = get_zarr_store(fs, library, store)
profile = f"profiles/{label}.bin"
rm(profile)
record_versions(label, library)
with memray.Tracker(profile, native_traces=True):
rng = np.random.default_rng()
arr = rng.random((5000, 5000), dtype=np.float32) # 100MB
if zarr_version < "3":
kwargs = {}
if not compress:
kwargs["compressor"] = None
z = zarr.open(
store,
mode="w",
shape=arr.shape,
dtype=arr.dtype,
chunks=arr.shape,
**kwargs,
)
else:
kwargs = dict(config=dict(write_empty_chunks=True))
if not compress:
kwargs["compressors"] = None
z = zarr.create_array(
store=store,
shape=arr.shape,
dtype=arr.dtype,
chunks=arr.shape,
overwrite=True,
**kwargs,
)
z[:] = arr
def libraries_under_test(library):
"""Packages whose versions determine what a profile measures."""
if library.startswith("zarrista"):
packages = ["zarrista"]
else:
packages = ["zarr", "numcodecs"]
if library.endswith("obstore"):
packages.append("obstore")
elif library.endswith("icechunk"):
packages.append("icechunk")
return packages
def record_versions(label, library):
"""Write the library versions a profile exercised next to the profile itself.
The configs pin zarr/zarrista but leave the store libraries floating, so a
profile's versions are only knowable at run time. `generate_table.py` reads
these files to label the README tables.
"""
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as package_version
versions = {}
for package in libraries_under_test(library):
try:
versions[package] = package_version(package)
except PackageNotFoundError:
pass
Path(f"profiles/{label}.versions.json").write_text(json.dumps(versions, indent=2) + "\n")
def filesystem(store_prefix):
if store_prefix.startswith("s3://"):
return "s3"
return "local"
def find_zarr_version():
import zarr
if "dev" in zarr.__version__:
return "3-dev"
return zarr.__version__
def get_obstore_store(fs, store_prefix):
import obstore
if fs == "local":
return obstore.store.LocalStore(prefix=store_prefix, mkdir=True)
elif fs == "s3":
return obstore.store.S3Store.from_url(store_prefix)
else:
raise ValueError(f"unrecognised filesystem: {fs}")
def get_zarr_store(fs, library, store):
if library == "obstore":
import obstore
import zarr
if fs == "local":
local_store = obstore.store.LocalStore(prefix=store, mkdir=True)
return zarr.storage.ObjectStore(store=local_store)
elif fs == "s3":
s3_store = obstore.store.S3Store.from_url(store)
return zarr.storage.ObjectStore(store=s3_store)
else:
raise ValueError(f"unrecognised filesystem: {fs}")
return store
def rm(filename):
try:
os.remove(filename)
except OSError:
pass
if __name__ == "__main__":
cli()