-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy path_msgpack_utils.py
More file actions
222 lines (186 loc) · 8.68 KB
/
_msgpack_utils.py
File metadata and controls
222 lines (186 loc) · 8.68 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
#######################################################################
# Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org>
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#######################################################################
from __future__ import annotations
import builtins
import inspect
import linecache
import textwrap
from dataclasses import asdict
import numpy as np
from msgpack import ExtType, packb, unpackb
from blosc2 import blosc2_ext
from blosc2.dsl_kernel import DSLKernel
from blosc2.ref import Ref
# Msgpack extension type codes are application-defined. Reserve code 42 in
# python-blosc2 for values serialized as Blosc2 CFrames via ``to_cframe()`` and
# reconstructed with ``blosc2.from_cframe()``. Keep this stable for backward
# compatibility with persisted msgpack payloads produced by this package.
_BLOSC2_EXT_CODE = 42
# Reserve code 43 for structured Blosc2 reference objects that are not naturally
# serialized as CFrames. The payload is a msgpack-encoded mapping with a
# stable ``kind`` and ``version`` envelope.
_BLOSC2_STRUCTURED_EXT_CODE = 43
_BLOSC2_STRUCTURED_VERSION = 1
_BLOSC2_DSL_VERSION = 1
def _encode_operand_reference(obj):
return Ref.from_object(obj).to_dict()
def _encode_structured_reference(obj):
import blosc2
if isinstance(obj, blosc2.Ref):
payload = {"kind": "ref", "version": _BLOSC2_STRUCTURED_VERSION, "ref": obj.to_dict()}
return ExtType(_BLOSC2_STRUCTURED_EXT_CODE, packb(payload, use_bin_type=True))
if isinstance(obj, blosc2.C2Array):
payload = _encode_operand_reference(obj)
return ExtType(_BLOSC2_STRUCTURED_EXT_CODE, packb(payload, use_bin_type=True))
if isinstance(obj, blosc2.LazyExpr):
expression = obj.expression_tosave if hasattr(obj, "expression_tosave") else obj.expression
operands = obj.operands_tosave if hasattr(obj, "operands_tosave") else obj.operands
payload = {
"kind": "lazyexpr",
"version": _BLOSC2_STRUCTURED_VERSION,
"expression": expression,
"operands": {key: _encode_operand_reference(value) for key, value in operands.items()},
}
return ExtType(_BLOSC2_STRUCTURED_EXT_CODE, packb(payload, use_bin_type=True))
if isinstance(obj, blosc2.LazyUDF):
if not isinstance(obj.func, DSLKernel):
raise TypeError("Structured Blosc2 msgpack payload only supports LazyUDF backed by DSLKernel")
udf_func = obj.func.func
udf_name = getattr(udf_func, "__name__", obj.func.__name__)
try:
udf_source = textwrap.dedent(inspect.getsource(udf_func)).lstrip()
except Exception:
udf_source = obj.func.dsl_source
if udf_source is None:
raise ValueError("Structured LazyUDF msgpack payload requires recoverable DSL kernel source")
kwargs = {}
for key, value in obj.kwargs.items():
if key in {"dtype", "shape"}:
continue
if isinstance(value, blosc2.CParams | blosc2.DParams):
kwargs[key] = asdict(value)
else:
kwargs[key] = value
# Keep both source forms:
# - udf_source recreates the executable Python function object
# - dsl_source preserves the DSLKernel's normalized DSL metadata so the
# reconstructed function can keep its DSL identity and fast-path hints
payload = {
"kind": "lazyudf",
"version": _BLOSC2_STRUCTURED_VERSION,
"function_kind": "dsl",
"dsl_version": _BLOSC2_DSL_VERSION,
"name": udf_name,
"udf_source": udf_source,
"dsl_source": obj.func.dsl_source,
"dtype": np.dtype(obj.dtype).str,
"shape": list(obj.shape),
"operands": {f"o{i}": _encode_operand_reference(value) for i, value in enumerate(obj.inputs)},
"kwargs": kwargs,
}
return ExtType(_BLOSC2_STRUCTURED_EXT_CODE, packb(payload, use_bin_type=True))
return None
def _decode_operand_reference(payload):
return Ref.from_dict(payload).open()
def _decode_structured_reference(data):
payload = unpackb(data)
if not isinstance(payload, dict):
raise TypeError("Structured Blosc2 msgpack payload must decode to a mapping")
version = payload.get("version")
if version != _BLOSC2_STRUCTURED_VERSION:
raise ValueError(f"Unsupported structured Blosc2 msgpack payload version: {version!r}")
kind = payload.get("kind")
if kind == "ref":
ref_payload = payload.get("ref")
return Ref.from_dict(ref_payload)
if kind == "c2array":
return _decode_operand_reference(payload)
if kind == "lazyexpr":
return _decode_structured_lazyexpr(payload)
if kind == "lazyudf":
return _decode_structured_lazyudf(payload)
raise ValueError(f"Unsupported structured Blosc2 msgpack payload kind: {kind!r}")
def _decode_structured_lazyexpr(payload):
import blosc2
expression = payload.get("expression")
if not isinstance(expression, str):
raise TypeError("Structured LazyExpr msgpack payload requires a string 'expression'")
operands_payload = payload.get("operands")
if not isinstance(operands_payload, dict):
raise TypeError("Structured LazyExpr msgpack payload requires a mapping 'operands'")
operands = {key: _decode_operand_reference(value) for key, value in operands_payload.items()}
return blosc2.lazyexpr(expression, operands=operands)
def _decode_structured_lazyudf(payload):
import blosc2
function_kind = payload.get("function_kind")
if function_kind != "dsl":
raise ValueError(f"Unsupported structured LazyUDF function kind: {function_kind!r}")
dsl_version = payload.get("dsl_version")
if dsl_version != _BLOSC2_DSL_VERSION:
raise ValueError(f"Unsupported structured LazyUDF DSL version: {dsl_version!r}")
udf_source = payload.get("udf_source")
if not isinstance(udf_source, str):
raise TypeError("Structured LazyUDF msgpack payload requires a string 'udf_source'")
name = payload.get("name")
if not isinstance(name, str):
raise TypeError("Structured LazyUDF msgpack payload requires a string 'name'")
dtype = payload.get("dtype")
if not isinstance(dtype, str):
raise TypeError("Structured LazyUDF msgpack payload requires a string 'dtype'")
shape_payload = payload.get("shape")
if not isinstance(shape_payload, list):
raise TypeError("Structured LazyUDF msgpack payload requires a list 'shape'")
operands_payload = payload.get("operands")
if not isinstance(operands_payload, dict):
raise TypeError("Structured LazyUDF msgpack payload requires a mapping 'operands'")
kwargs = payload.get("kwargs", {})
if not isinstance(kwargs, dict):
raise TypeError("Structured LazyUDF msgpack payload requires a mapping 'kwargs'")
local_ns = {}
filename = f"<{name}>"
safe_globals = {
"__builtins__": {k: v for k, v in builtins.__dict__.items() if k != "__import__"},
"np": np,
"blosc2": blosc2,
}
linecache.cache[filename] = (len(udf_source), None, udf_source.splitlines(True), filename)
exec(compile(udf_source, filename, "exec"), safe_globals, local_ns)
func = local_ns[name]
if not isinstance(func, DSLKernel):
func = DSLKernel(func)
dsl_source = payload.get("dsl_source")
if dsl_source is not None and func.dsl_source is None:
func.dsl_source = dsl_source
operands = tuple(
_decode_operand_reference(operands_payload[f"o{n}"]) for n in range(len(operands_payload))
)
return blosc2.lazyudf(func, operands, dtype=np.dtype(dtype), shape=tuple(shape_payload), **kwargs)
def _encode_msgpack_ext(obj):
import blosc2
if isinstance(
obj, blosc2.NDArray | blosc2.SChunk | blosc2.VLArray | blosc2.BatchStore | blosc2.EmbedStore
):
return ExtType(_BLOSC2_EXT_CODE, obj.to_cframe())
structured = _encode_structured_reference(obj)
if structured is not None:
return structured
return blosc2_ext.encode_tuple(obj)
def msgpack_packb(value):
return packb(value, default=_encode_msgpack_ext, strict_types=True, use_bin_type=True)
def decode_tuple_list_hook(obj):
if obj and isinstance(obj[0], str) and obj[0] == "__tuple__":
return tuple(obj[1:])
return obj
def _decode_msgpack_ext(code, data):
import blosc2
if code == _BLOSC2_EXT_CODE:
return blosc2.from_cframe(data, copy=True)
if code == _BLOSC2_STRUCTURED_EXT_CODE:
return _decode_structured_reference(data)
return ExtType(code, data)
def msgpack_unpackb(payload):
return unpackb(payload, list_hook=decode_tuple_list_hook, ext_hook=_decode_msgpack_ext)