Skip to content

Commit 0ad73c8

Browse files
authored
Use Zarr v2.13.0a2 (#129)
which include zarr-developers/zarr-python#934 Authors: - Mads R. B. Kristensen (https://github.com/madsbk) Approvers: - Benjamin Zaitlen (https://github.com/quasiben) URL: #129
1 parent f8f4063 commit 0ad73c8

File tree

4 files changed

+22
-100
lines changed

4 files changed

+22
-100
lines changed

python/benchmarks/single-node-io.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import argparse
55
import contextlib
6-
import functools
76
import os
87
import os.path
98
import pathlib
@@ -193,30 +192,30 @@ def run_posix(args):
193192
return read_time, write_time
194193

195194

196-
def run_zarr(store_type, args):
195+
def run_zarr(args):
197196
"""Use the Zarr API"""
198197

199198
import zarr
200-
import zarr.cupy
201199

202200
import kvikio.zarr
203201

202+
dir_path = args.dir / "zarr"
203+
204+
if not hasattr(zarr.Array, "meta_array"):
205+
raise RuntimeError("requires Zarr v2.13+")
206+
204207
a = cupy.arange(args.nbytes, dtype="uint8")
205208

206-
# Retrieve the store and compressor to use based on `store_type`
207-
shutil.rmtree(str(args.dir / store_type), ignore_errors=True)
208-
store, compressor = {
209-
"gds": (kvikio.zarr.GDSStore(args.dir / store_type), None),
210-
"posix": (
211-
zarr.DirectoryStore(args.dir / store_type),
212-
zarr.cupy.CuPyCPUCompressor(),
213-
),
214-
}[store_type]
209+
shutil.rmtree(str(dir_path), ignore_errors=True)
215210

216211
# Write
217212
t0 = clock()
218213
z = zarr.array(
219-
a, chunks=False, compressor=compressor, store=store, meta_array=cupy.empty(())
214+
a,
215+
chunks=False,
216+
compressor=None,
217+
store=kvikio.zarr.GDSStore(dir_path),
218+
meta_array=cupy.empty(()),
220219
)
221220
write_time = clock() - t0
222221

@@ -231,8 +230,7 @@ def run_zarr(store_type, args):
231230

232231
API = {
233232
"cufile": run_cufile,
234-
"zarr-gds": functools.partial(run_zarr, "gds"),
235-
"zarr-posix": functools.partial(run_zarr, "posix"),
233+
"zarr": run_zarr,
236234
"posix": run_posix,
237235
"cufile-mfma": run_cufile_multiple_files_multiple_arrays,
238236
"cufile-mf": run_cufile_multiple_files,

python/kvikio/zarr.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
22
# See file LICENSE for terms.
33

4-
import errno
54
import os
65
import os.path
7-
import shutil
8-
import uuid
96

107
import cupy
118
import zarr.storage
12-
from zarr.util import retry_call
139

1410
import kvikio
1511
from kvikio._lib.arr import asarray
@@ -55,59 +51,3 @@ def _tofile(self, a, fn):
5551
assert written == a.nbytes
5652
else:
5753
super()._tofile(a.obj, fn)
58-
59-
def __setitem__(self, key, value):
60-
"""
61-
We have to overwrite this because `DirectoryStore.__setitem__`
62-
converts `value` to a NumPy array always
63-
"""
64-
key = self._normalize_key(key)
65-
66-
# coerce to flat, contiguous buffer (ideally without copying)
67-
arr = asarray(value)
68-
if arr.contiguous:
69-
value = arr
70-
else:
71-
if arr.cuda:
72-
# value = cupy.ascontiguousarray(value)
73-
value = arr.reshape(-1, order="A")
74-
else:
75-
# can flatten without copy
76-
value = arr.reshape(-1, order="A")
77-
78-
# destination path for key
79-
file_path = os.path.join(self.path, key)
80-
81-
# ensure there is no directory in the way
82-
if os.path.isdir(file_path):
83-
shutil.rmtree(file_path)
84-
85-
# ensure containing directory exists
86-
dir_path, file_name = os.path.split(file_path)
87-
if os.path.isfile(dir_path):
88-
raise KeyError(key)
89-
if not os.path.exists(dir_path):
90-
try:
91-
os.makedirs(dir_path)
92-
except OSError as e:
93-
if e.errno != errno.EEXIST:
94-
raise KeyError(key)
95-
96-
# write to temporary file
97-
# note we're not using tempfile.NamedTemporaryFile to avoid
98-
# restrictive file permissions
99-
temp_name = file_name + "." + uuid.uuid4().hex + ".partial"
100-
temp_path = os.path.join(dir_path, temp_name)
101-
try:
102-
self._tofile(value, temp_path)
103-
104-
# move temporary file into place;
105-
# make several attempts at writing the temporary file to get past
106-
# potential antivirus file locking issues
107-
retry_call(
108-
os.replace, (temp_path, file_path), exceptions=(PermissionError,)
109-
)
110-
finally:
111-
# clean up if temp file still exists for whatever reason
112-
if os.path.exists(temp_path): # pragma: no cover
113-
os.remove(temp_path)

python/tests/test_benchmarks.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,16 @@
2121
"cufile-mfma",
2222
"cufile-mf",
2323
"cufile-ma",
24-
"zarr-gds",
25-
"zarr-posix",
24+
"zarr",
2625
],
2726
)
2827
def test_single_node_io(run_cmd, tmp_path, api):
2928
"""Test benchmarks/single-node-io.py"""
3029

3130
if "zarr" in api:
32-
pytest.importorskip(
33-
"zarr.cupy",
34-
reason=(
35-
"To use Zarr arrays with GDS directly, Zarr needs CuPy support: "
36-
"<https://github.com/zarr-developers/zarr-python/pull/934>"
37-
),
38-
)
31+
zarr = pytest.importorskip("zarr")
32+
if not hasattr(zarr.Array, "meta_array"):
33+
pytest.skip("requires Zarr v2.13+")
3934

4035
retcode = run_cmd(
4136
cmd=[

python/tests/test_zarr.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
zarr = pytest.importorskip("zarr")
99
GDSStore = pytest.importorskip("kvikio.zarr").GDSStore
1010

11+
# To support CuPy arrays, we need the `meta_array` argument introduced in
12+
# Zarr v2.13, see <https://github.com/zarr-developers/zarr-python/pull/934>
13+
if not hasattr(zarr.Array, "meta_array"):
14+
pytest.skip("requires Zarr v2.13+", allow_module_level=True)
15+
1116

1217
@pytest.fixture
1318
def store(tmp_path):
@@ -32,14 +37,6 @@ def test_direct_store_access(store, array_type):
3237
def test_array(store):
3338
"""Test Zarr array"""
3439

35-
pytest.importorskip(
36-
"zarr.cupy",
37-
reason=(
38-
"To use Zarr arrays with GDS directly, Zarr needs CuPy support: "
39-
"<https://github.com/zarr-developers/zarr-python/pull/934>"
40-
),
41-
)
42-
4340
a = cupy.arange(100)
4441
z = zarr.array(
4542
a, chunks=10, compressor=None, store=store, meta_array=cupy.empty(())
@@ -53,14 +50,6 @@ def test_array(store):
5350
def test_group(store):
5451
"""Test Zarr group"""
5552

56-
pytest.importorskip(
57-
"zarr.cupy",
58-
reason=(
59-
"To use Zarr arrays with GDS directly, Zarr needs CuPy support: "
60-
"<https://github.com/zarr-developers/zarr-python/pull/934>"
61-
),
62-
)
63-
6453
g = zarr.open_group(store, meta_array=cupy.empty(()))
6554
g.ones("data", shape=(10, 11), dtype=int, compressor=None)
6655
a = g["data"]

0 commit comments

Comments
 (0)