loaderx 1.9.6__tar.gz → 1.9.7__tar.gz
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.
- {loaderx-1.9.6/loaderx.egg-info → loaderx-1.9.7}/PKG-INFO +1 -1
- {loaderx-1.9.6 → loaderx-1.9.7}/build.zig +4 -3
- {loaderx-1.9.6 → loaderx-1.9.7}/build.zig.zon +1 -1
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx/__init__.py +1 -1
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx/_store.py +68 -38
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx/dataloader.py +10 -2
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx/utils.py +23 -9
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx/zrecord.py +13 -52
- {loaderx-1.9.6 → loaderx-1.9.7/loaderx.egg-info}/PKG-INFO +1 -1
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx.egg-info/SOURCES.txt +1 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/scripts/test_loaderx.py +135 -165
- {loaderx-1.9.6 → loaderx-1.9.7}/src/record/engine.zig +135 -336
- loaderx-1.9.7/src/record/executor.zig +60 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/src/store.zig +21 -38
- {loaderx-1.9.6 → loaderx-1.9.7}/src/zsampler.zig +32 -18
- {loaderx-1.9.6 → loaderx-1.9.7}/LICENSE +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/MANIFEST.in +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/README.md +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx/_lib.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx/zsampler.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx.egg-info/dependency_links.txt +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx.egg-info/requires.txt +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/loaderx.egg-info/top_level.txt +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/pyproject.toml +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/scripts/_bench_common.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/scripts/bench.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/scripts/bench_dense.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/scripts/bench_ragged.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/scripts/build_wheels.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/scripts/prepare_tokens.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/setup.cfg +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/setup.py +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/src/record/storage.zig +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/src/zstd/c.zig +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/COPYING +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/LICENSE +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/allocations.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/bits.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/bitstream.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/compiler.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/cpu.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/debug.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/debug.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/entropy_common.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/error_private.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/error_private.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/fse.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/fse_decompress.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/huf.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/mem.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/pool.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/pool.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/portability_macros.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/threading.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/threading.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/xxhash.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/xxhash.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/zstd_common.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/zstd_deps.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/zstd_internal.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/common/zstd_trace.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/clevels.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/fse_compress.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/hist.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/hist.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/huf_compress.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress_internal.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress_literals.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress_literals.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress_sequences.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress_sequences.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress_superblock.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_compress_superblock.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_cwksp.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_double_fast.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_double_fast.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_fast.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_fast.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_lazy.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_lazy.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_ldm.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_ldm.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_ldm_geartab.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_opt.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstd_opt.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstdmt_compress.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/compress/zstdmt_compress.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/decompress/huf_decompress.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/decompress/zstd_ddict.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/decompress/zstd_ddict.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/decompress/zstd_decompress.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/decompress/zstd_decompress_block.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/decompress/zstd_decompress_block.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/decompress/zstd_decompress_internal.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/dictBuilder/cover.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/dictBuilder/cover.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/dictBuilder/divsufsort.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/dictBuilder/divsufsort.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/dictBuilder/fastcover.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/dictBuilder/zdict.c +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/zdict.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/zstd.h +0 -0
- {loaderx-1.9.6 → loaderx-1.9.7}/vendor/zstd/lib/zstd_errors.h +0 -0
|
@@ -157,7 +157,7 @@ pub fn build(b: *std.Build) void {
|
|
|
157
157
|
// Throughput is not measured here — it is measured through the binding that
|
|
158
158
|
// clients actually use, by the scripts/bench_*.py entry points.
|
|
159
159
|
const test_step = b.step("test", "Run native tests");
|
|
160
|
-
for ([_][]const u8{ "src/record/engine.zig", "src/
|
|
160
|
+
for ([_][]const u8{ "src/record/engine.zig", "src/zsampler.zig" }) |src| {
|
|
161
161
|
for ([_]std.builtin.OptimizeMode{ .Debug, .ReleaseFast }) |mode| {
|
|
162
162
|
const tests = b.addTest(.{
|
|
163
163
|
.root_module = b.createModule(.{
|
|
@@ -167,8 +167,9 @@ pub fn build(b: *std.Build) void {
|
|
|
167
167
|
.link_libc = true,
|
|
168
168
|
}),
|
|
169
169
|
});
|
|
170
|
-
// The
|
|
171
|
-
if (!std.mem.eql(u8, src, "src/zsampler.zig"))
|
|
170
|
+
// The record engine tests call zstd and need the C objects linked.
|
|
171
|
+
if (!std.mem.eql(u8, src, "src/zsampler.zig"))
|
|
172
|
+
attachZstd(b, tests);
|
|
172
173
|
test_step.dependOn(&b.addRunArtifact(tests).step);
|
|
173
174
|
}
|
|
174
175
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
"""Shared CFFI
|
|
1
|
+
"""Shared CFFI support for the geometry-specific store bindings."""
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
|
+
import threading
|
|
4
5
|
|
|
5
6
|
import numpy as np
|
|
6
7
|
from cffi import FFI
|
|
@@ -10,10 +11,15 @@ from . import _lib
|
|
|
10
11
|
ffi = FFI()
|
|
11
12
|
|
|
12
13
|
ffi.cdef("""
|
|
14
|
+
typedef struct Executor Executor;
|
|
13
15
|
typedef struct Store Store;
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
Executor* executor_create(int32_t*);
|
|
18
|
+
void executor_close(Executor*);
|
|
19
|
+
|
|
20
|
+
Store* store_create(Executor*, const char*, size_t, uint8_t,
|
|
21
|
+
const uint8_t*, size_t, int32_t*);
|
|
22
|
+
Store* store_open(Executor*, const char*, size_t, int32_t*);
|
|
17
23
|
void store_close(Store*);
|
|
18
24
|
uint64_t store_len(Store*);
|
|
19
25
|
uint8_t store_codec(Store*);
|
|
@@ -76,25 +82,73 @@ def _ptr(ctype, array):
|
|
|
76
82
|
return ffi.cast(ctype, array.ctypes.data)
|
|
77
83
|
|
|
78
84
|
|
|
85
|
+
class _Executor:
|
|
86
|
+
def __init__(self):
|
|
87
|
+
code = ffi.new("int32_t*", -2)
|
|
88
|
+
self._handle = lib.executor_create(code)
|
|
89
|
+
if self._handle == ffi.NULL:
|
|
90
|
+
raise ZrecordError(code[0])
|
|
91
|
+
|
|
92
|
+
def close(self):
|
|
93
|
+
handle = getattr(self, "_handle", ffi.NULL)
|
|
94
|
+
if handle != ffi.NULL:
|
|
95
|
+
lib.executor_close(handle)
|
|
96
|
+
self._handle = ffi.NULL
|
|
97
|
+
|
|
98
|
+
def __del__(self):
|
|
99
|
+
self.close()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
_executor = None
|
|
103
|
+
_executor_lock = threading.Lock()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _reset_executor_after_fork():
|
|
107
|
+
global _executor, _executor_lock
|
|
108
|
+
if _executor is not None:
|
|
109
|
+
# The child has no copies of the parent's worker threads, so the copied
|
|
110
|
+
# native pool cannot be safely drained or destroyed.
|
|
111
|
+
_executor._handle = ffi.NULL
|
|
112
|
+
_executor = None
|
|
113
|
+
_executor_lock = threading.Lock()
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
if hasattr(os, "register_at_fork"):
|
|
117
|
+
os.register_at_fork(after_in_child=_reset_executor_after_fork)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _get_executor():
|
|
121
|
+
global _executor
|
|
122
|
+
if _executor is None:
|
|
123
|
+
with _executor_lock:
|
|
124
|
+
if _executor is None:
|
|
125
|
+
_executor = _Executor()
|
|
126
|
+
return _executor
|
|
127
|
+
|
|
128
|
+
|
|
79
129
|
class _Store:
|
|
80
130
|
"""Common binding behavior over the native RecordEngine handle."""
|
|
81
131
|
|
|
82
132
|
@classmethod
|
|
83
|
-
def
|
|
133
|
+
def open(cls, path):
|
|
84
134
|
self = cls.__new__(cls)
|
|
135
|
+
self._executor = _get_executor()
|
|
85
136
|
raw_path = _path_bytes(path)
|
|
86
137
|
code = ffi.new("int32_t*", -2)
|
|
87
|
-
self._finish_handle(lib.store_open(
|
|
138
|
+
self._finish_handle(lib.store_open(
|
|
139
|
+
self._executor._handle, raw_path, len(raw_path), code,
|
|
140
|
+
), code)
|
|
88
141
|
return self
|
|
89
142
|
|
|
90
143
|
@classmethod
|
|
91
|
-
def
|
|
144
|
+
def create(cls, path, codec, dict_bytes):
|
|
92
145
|
self = cls.__new__(cls)
|
|
146
|
+
self._executor = _get_executor()
|
|
93
147
|
raw_path = _path_bytes(path)
|
|
94
148
|
raw_dict = np.frombuffer(dict_bytes or b"", dtype=np.uint8)
|
|
95
149
|
code = ffi.new("int32_t*", -2)
|
|
96
150
|
handle = lib.store_create(
|
|
97
|
-
raw_path, len(raw_path), CODECS[codec],
|
|
151
|
+
self._executor._handle, raw_path, len(raw_path), CODECS[codec],
|
|
98
152
|
_ptr("const uint8_t*", raw_dict), raw_dict.size, code,
|
|
99
153
|
)
|
|
100
154
|
self._finish_handle(handle, code)
|
|
@@ -141,47 +195,23 @@ class _Store:
|
|
|
141
195
|
if handle != ffi.NULL:
|
|
142
196
|
lib.store_close(handle)
|
|
143
197
|
self._handle = ffi.NULL
|
|
198
|
+
self._executor = None
|
|
144
199
|
|
|
145
200
|
def __del__(self):
|
|
146
201
|
self.close()
|
|
147
202
|
|
|
148
|
-
|
|
149
|
-
class _DenseStore(_Store):
|
|
150
|
-
@classmethod
|
|
151
|
-
def open(cls, path, record_bytes):
|
|
152
|
-
self = cls._open(path)
|
|
153
|
-
self.record_bytes = record_bytes
|
|
154
|
-
return self
|
|
155
|
-
|
|
156
|
-
@classmethod
|
|
157
|
-
def create(cls, path, *, codec="zstd", record_bytes, dict_bytes=None):
|
|
158
|
-
self = cls._create(path, codec, dict_bytes)
|
|
159
|
-
self.record_bytes = record_bytes
|
|
160
|
-
return self
|
|
161
|
-
|
|
162
|
-
def gather(self, idxs, dst):
|
|
203
|
+
def dense_gather(self, idxs, dst, record_bytes):
|
|
163
204
|
_check(lib.dense_store_gather(
|
|
164
205
|
self._handle, _ptr("const uint64_t*", idxs), idxs.size,
|
|
165
|
-
_ptr("uint8_t*", dst), dst.nbytes,
|
|
206
|
+
_ptr("uint8_t*", dst), dst.nbytes, record_bytes,
|
|
166
207
|
))
|
|
167
208
|
|
|
168
|
-
def
|
|
209
|
+
def dense_append(self, src, record_bytes):
|
|
169
210
|
return _check(lib.dense_store_append(
|
|
170
|
-
self._handle, _ptr("const uint8_t*", src), src.nbytes,
|
|
171
|
-
self.record_bytes,
|
|
211
|
+
self._handle, _ptr("const uint8_t*", src), src.nbytes, record_bytes,
|
|
172
212
|
))
|
|
173
213
|
|
|
174
|
-
|
|
175
|
-
class _RaggedStore(_Store):
|
|
176
|
-
@classmethod
|
|
177
|
-
def open(cls, path):
|
|
178
|
-
return cls._open(path)
|
|
179
|
-
|
|
180
|
-
@classmethod
|
|
181
|
-
def create(cls, path, *, codec="zstd", dict_bytes=None):
|
|
182
|
-
return cls._create(path, codec, dict_bytes)
|
|
183
|
-
|
|
184
|
-
def gather(self, idxs, dst, offsets):
|
|
214
|
+
def ragged_gather(self, idxs, dst, offsets):
|
|
185
215
|
_check(lib.ragged_store_gather(
|
|
186
216
|
self._handle, _ptr("const uint64_t*", idxs), idxs.size,
|
|
187
217
|
_ptr("uint8_t*", dst), dst.nbytes,
|
|
@@ -196,7 +226,7 @@ class _RaggedStore(_Store):
|
|
|
196
226
|
))
|
|
197
227
|
return lengths
|
|
198
228
|
|
|
199
|
-
def
|
|
229
|
+
def ragged_append(self, src, offsets):
|
|
200
230
|
return _check(lib.ragged_store_append(
|
|
201
231
|
self._handle, _ptr("const uint8_t*", src), src.nbytes,
|
|
202
232
|
_ptr("const uint64_t*", offsets), offsets.size,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import operator
|
|
1
2
|
import threading
|
|
2
3
|
from queue import Queue, Empty, Full
|
|
3
4
|
|
|
@@ -49,8 +50,9 @@ class DataLoader:
|
|
|
49
50
|
the collate step (reshaping, casting, ...). Defaults to identity.
|
|
50
51
|
|
|
51
52
|
Raises:
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
TypeError: If ``prefetch_size`` is not an integer.
|
|
54
|
+
ValueError: If ``streams`` is empty, the streams differ in record
|
|
55
|
+
count, or ``prefetch_size`` is not positive.
|
|
54
56
|
"""
|
|
55
57
|
self.streams = dict(streams)
|
|
56
58
|
if not self.streams:
|
|
@@ -63,6 +65,12 @@ class DataLoader:
|
|
|
63
65
|
length = next(iter(lengths.values()))
|
|
64
66
|
|
|
65
67
|
transform_workers = max(1, int(transform_workers))
|
|
68
|
+
try:
|
|
69
|
+
prefetch_size = operator.index(prefetch_size)
|
|
70
|
+
except TypeError:
|
|
71
|
+
raise TypeError("prefetch_size must be an integer") from None
|
|
72
|
+
if prefetch_size < 1:
|
|
73
|
+
raise ValueError("prefetch_size must be positive")
|
|
66
74
|
|
|
67
75
|
# Transform workers themselves hold the in-flight batches. One waiting
|
|
68
76
|
# raw batch is enough to hand off work without duplicating the final
|
|
@@ -11,17 +11,31 @@ import math
|
|
|
11
11
|
|
|
12
12
|
import numpy as np
|
|
13
13
|
|
|
14
|
-
from .zrecord import DenseStore, RaggedStore
|
|
14
|
+
from .zrecord import DICT_TIERS, DenseStore, RaggedStore
|
|
15
15
|
from ._store import _build_dict
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
def
|
|
18
|
+
def _dict_tier(tier):
|
|
19
|
+
tier = "balanced" if tier is None else tier
|
|
20
|
+
if tier not in DICT_TIERS:
|
|
21
|
+
raise ValueError(
|
|
22
|
+
f"unknown dict tier {tier!r}, expected one of {sorted(DICT_TIERS)}"
|
|
23
|
+
)
|
|
24
|
+
return DICT_TIERS[tier]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _sample_count(total, item_bytes, sample_budget):
|
|
28
|
+
per = max(item_bytes, 1)
|
|
29
|
+
return max(1, min(total, max(1, sample_budget // per)))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _pack_samples(source, sample_budget):
|
|
19
33
|
"""Pack a training sample of `source` into concatenated bytes and per-record
|
|
20
34
|
sizes. `source` is a stacked numpy array, an iterable of records, or a
|
|
21
|
-
DenseStore/RaggedStore; the sample is
|
|
35
|
+
DenseStore/RaggedStore; the sample is bounded by `sample_budget` bytes."""
|
|
22
36
|
if isinstance(source, np.ndarray):
|
|
23
37
|
itemsize = math.prod(source.shape[1:]) * np.dtype(source.dtype).itemsize
|
|
24
|
-
k = _sample_count(len(source),
|
|
38
|
+
k = _sample_count(len(source), itemsize, sample_budget)
|
|
25
39
|
idx = np.unique(np.linspace(0, len(source) - 1, k).astype(np.int64))
|
|
26
40
|
sample = np.ascontiguousarray(source[idx])
|
|
27
41
|
row = math.prod(sample.shape[1:]) * sample.dtype.itemsize
|
|
@@ -35,7 +49,7 @@ def _pack_samples(source, tier=None):
|
|
|
35
49
|
else:
|
|
36
50
|
preview = source.__getitems__(np.arange(min(n, 32)))
|
|
37
51
|
item = int(np.mean([record.nbytes for record in preview])) or 1
|
|
38
|
-
k = _sample_count(n, item,
|
|
52
|
+
k = _sample_count(n, item, sample_budget)
|
|
39
53
|
idx = np.unique(np.linspace(0, n - 1, min(k, n)).astype(np.int64))
|
|
40
54
|
sample = source.__getitems__(idx)
|
|
41
55
|
if isinstance(source, DenseStore):
|
|
@@ -45,7 +59,6 @@ def _pack_samples(source, tier=None):
|
|
|
45
59
|
sizes = np.fromiter((len(record) for record in packed), dtype=np.uintp)
|
|
46
60
|
return b"".join(packed), sizes
|
|
47
61
|
# iterable of records: buffer up to the tier's sample budget
|
|
48
|
-
budget = _dict_tier(tier)["samples"]
|
|
49
62
|
buf, total = [], 0
|
|
50
63
|
for record in source:
|
|
51
64
|
if isinstance(record, (bytes, bytearray)):
|
|
@@ -54,7 +67,7 @@ def _pack_samples(source, tier=None):
|
|
|
54
67
|
b = np.ascontiguousarray(np.asarray(record)).tobytes()
|
|
55
68
|
buf.append(b)
|
|
56
69
|
total += len(b)
|
|
57
|
-
if total >=
|
|
70
|
+
if total >= sample_budget:
|
|
58
71
|
break
|
|
59
72
|
if not buf:
|
|
60
73
|
raise ValueError("train_dict needs at least one record")
|
|
@@ -79,8 +92,9 @@ def train_dict(source, tier=None, path=None):
|
|
|
79
92
|
Returns:
|
|
80
93
|
bytes: the trained dictionary.
|
|
81
94
|
"""
|
|
82
|
-
|
|
83
|
-
|
|
95
|
+
preset = _dict_tier(tier)
|
|
96
|
+
samples, sizes = _pack_samples(source, preset["samples"])
|
|
97
|
+
d = _build_dict(samples, sizes, preset["size"])
|
|
84
98
|
if path is not None:
|
|
85
99
|
Path(path).write_bytes(d)
|
|
86
100
|
return d
|
|
@@ -39,9 +39,7 @@ import threading
|
|
|
39
39
|
import msgpack
|
|
40
40
|
import numpy as np
|
|
41
41
|
|
|
42
|
-
from ._store import
|
|
43
|
-
MAX_RECORD_LEN, ZrecordError, _DenseStore, _RaggedStore,
|
|
44
|
-
)
|
|
42
|
+
from ._store import MAX_RECORD_LEN, ZrecordError, _Store
|
|
45
43
|
|
|
46
44
|
__all__ = ["DenseStore", "RaggedStore", "ZrecordError", "DICT_TIERS"]
|
|
47
45
|
|
|
@@ -62,33 +60,6 @@ DICT_TIERS = {
|
|
|
62
60
|
"balanced": {"size": 128 << 10, "samples": 16 << 20},
|
|
63
61
|
"max": {"size": 1 << 20, "samples": 64 << 20},
|
|
64
62
|
}
|
|
65
|
-
DICT_TIER_DEFAULT = "balanced"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def _dict_tier(tier):
|
|
69
|
-
"""Resolve a tier name to its `{size, samples}` preset.
|
|
70
|
-
|
|
71
|
-
`None` picks the default tier; anything else must name a tier in
|
|
72
|
-
`DICT_TIERS`. There is deliberately no raw-byte escape hatch: the three
|
|
73
|
-
presets are the whole API, so a caller never has to think about dictionary
|
|
74
|
-
sizing.
|
|
75
|
-
"""
|
|
76
|
-
if tier is None:
|
|
77
|
-
tier = DICT_TIER_DEFAULT
|
|
78
|
-
if tier not in DICT_TIERS:
|
|
79
|
-
raise ValueError(
|
|
80
|
-
f"unknown dict tier {tier!r}, expected one of {sorted(DICT_TIERS)}"
|
|
81
|
-
)
|
|
82
|
-
return DICT_TIERS[tier]
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def _sample_count(total, item_bytes, tier=None):
|
|
86
|
-
"""Records to train a dictionary on: the tier's sample byte budget, in
|
|
87
|
-
records. Sized by bytes so a tier costs the same training time whatever the
|
|
88
|
-
record size, and capped by the store's record count."""
|
|
89
|
-
samples = _dict_tier(tier)["samples"]
|
|
90
|
-
per = max(item_bytes, 1)
|
|
91
|
-
return max(1, min(total, max(1, samples // per)))
|
|
92
63
|
|
|
93
64
|
|
|
94
65
|
def _decode_identity(raw, dense):
|
|
@@ -242,9 +213,7 @@ class _Base:
|
|
|
242
213
|
self._row_bytes = math.prod(self.item_shape) * self.dtype.itemsize
|
|
243
214
|
if self._row_bytes <= 0 or self._row_bytes > MAX_RECORD_LEN:
|
|
244
215
|
raise ValueError("invalid dense record width in type identity")
|
|
245
|
-
|
|
246
|
-
else:
|
|
247
|
-
self._store = _RaggedStore.open(self.path)
|
|
216
|
+
self._store = _Store.open(self.path)
|
|
248
217
|
try:
|
|
249
218
|
self.codec = self._store.codec()
|
|
250
219
|
except BaseException:
|
|
@@ -281,14 +250,7 @@ class _Base:
|
|
|
281
250
|
raise ValueError(
|
|
282
251
|
f"dense record width {self._row_bytes} is outside the native limit"
|
|
283
252
|
)
|
|
284
|
-
|
|
285
|
-
self.path, codec=self.codec, record_bytes=self._row_bytes,
|
|
286
|
-
dict_bytes=dict_bytes,
|
|
287
|
-
)
|
|
288
|
-
else:
|
|
289
|
-
self._store = _RaggedStore.create(
|
|
290
|
-
self.path, codec=self.codec, dict_bytes=dict_bytes,
|
|
291
|
-
)
|
|
253
|
+
self._store = _Store.create(self.path, self.codec, dict_bytes)
|
|
292
254
|
try:
|
|
293
255
|
_write_identity(self.path, encoded_identity)
|
|
294
256
|
except BaseException:
|
|
@@ -450,11 +412,11 @@ class DenseStore(_Base):
|
|
|
450
412
|
"""Append one ``(batch, *item_shape)`` dense array."""
|
|
451
413
|
with self._gate.shared():
|
|
452
414
|
src = self._prepare_dense(records)
|
|
453
|
-
return self._store.
|
|
415
|
+
return self._store.dense_append(src, self._row_bytes)
|
|
454
416
|
|
|
455
417
|
def _gather(self, idxs):
|
|
456
418
|
values = np.empty((idxs.size, *self.item_shape), dtype=self.dtype)
|
|
457
|
-
self._store.
|
|
419
|
+
self._store.dense_gather(idxs, values, self._row_bytes)
|
|
458
420
|
return values
|
|
459
421
|
|
|
460
422
|
|
|
@@ -492,10 +454,11 @@ class RaggedStore(_Base):
|
|
|
492
454
|
"""Append shape-prefixed records under one native commit."""
|
|
493
455
|
with self._gate.shared():
|
|
494
456
|
src, offsets = self._prepare_batch(records)
|
|
495
|
-
return self._store.
|
|
457
|
+
return self._store.ragged_append(src, offsets)
|
|
496
458
|
|
|
497
459
|
def _prepare_batch(self, records):
|
|
498
|
-
encoded,
|
|
460
|
+
encoded, offsets = [], []
|
|
461
|
+
total = 0
|
|
499
462
|
for record in records:
|
|
500
463
|
if isinstance(record, (bytes, bytearray)):
|
|
501
464
|
if self.dtype != np.dtype(np.uint8):
|
|
@@ -524,15 +487,13 @@ class RaggedStore(_Base):
|
|
|
524
487
|
f"encoded record of {encoded_bytes} bytes exceeds the "
|
|
525
488
|
f"{MAX_RECORD_LEN} byte record limit"
|
|
526
489
|
)
|
|
490
|
+
offsets.append(total)
|
|
491
|
+
total += encoded_bytes
|
|
527
492
|
encoded.extend((prefix, b))
|
|
528
|
-
|
|
529
|
-
if not sizes:
|
|
493
|
+
if not offsets:
|
|
530
494
|
raise ValueError("append needs at least one record")
|
|
531
495
|
src = np.concatenate(encoded)
|
|
532
|
-
|
|
533
|
-
if len(sizes) > 1:
|
|
534
|
-
offsets[1:] = np.cumsum(np.asarray(sizes, dtype=np.uint64)[:-1])
|
|
535
|
-
return src, offsets
|
|
496
|
+
return src, np.asarray(offsets, dtype=np.uint64)
|
|
536
497
|
|
|
537
498
|
def _gather(self, idxs):
|
|
538
499
|
lengths = self._store.record_lengths(idxs)
|
|
@@ -543,7 +504,7 @@ class RaggedStore(_Base):
|
|
|
543
504
|
total += int(length)
|
|
544
505
|
offsets = np.asarray(py_offsets, dtype=np.uint64)
|
|
545
506
|
buf = np.empty(total, dtype=np.uint8)
|
|
546
|
-
self._store.
|
|
507
|
+
self._store.ragged_gather(idxs, buf, offsets)
|
|
547
508
|
|
|
548
509
|
out = []
|
|
549
510
|
for offset, length in zip(offsets, lengths):
|