xoscar 0.8.0__cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.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.
Potentially problematic release.
This version of xoscar might be problematic. Click here for more details.
- xoscar/__init__.py +61 -0
- xoscar/_utils.cpython-313-x86_64-linux-gnu.so +0 -0
- xoscar/_utils.pxd +36 -0
- xoscar/_utils.pyx +246 -0
- xoscar/_version.py +693 -0
- xoscar/aio/__init__.py +16 -0
- xoscar/aio/base.py +86 -0
- xoscar/aio/file.py +59 -0
- xoscar/aio/lru.py +228 -0
- xoscar/aio/parallelism.py +39 -0
- xoscar/api.py +527 -0
- xoscar/backend.py +67 -0
- xoscar/backends/__init__.py +14 -0
- xoscar/backends/allocate_strategy.py +160 -0
- xoscar/backends/communication/__init__.py +30 -0
- xoscar/backends/communication/base.py +315 -0
- xoscar/backends/communication/core.py +69 -0
- xoscar/backends/communication/dummy.py +253 -0
- xoscar/backends/communication/errors.py +20 -0
- xoscar/backends/communication/socket.py +444 -0
- xoscar/backends/communication/ucx.py +538 -0
- xoscar/backends/communication/utils.py +97 -0
- xoscar/backends/config.py +157 -0
- xoscar/backends/context.py +437 -0
- xoscar/backends/core.py +352 -0
- xoscar/backends/indigen/__init__.py +16 -0
- xoscar/backends/indigen/__main__.py +19 -0
- xoscar/backends/indigen/backend.py +51 -0
- xoscar/backends/indigen/driver.py +26 -0
- xoscar/backends/indigen/fate_sharing.py +221 -0
- xoscar/backends/indigen/pool.py +509 -0
- xoscar/backends/indigen/shared_memory.py +548 -0
- xoscar/backends/message.cpython-313-x86_64-linux-gnu.so +0 -0
- xoscar/backends/message.pyi +255 -0
- xoscar/backends/message.pyx +646 -0
- xoscar/backends/pool.py +1630 -0
- xoscar/backends/router.py +285 -0
- xoscar/backends/test/__init__.py +16 -0
- xoscar/backends/test/backend.py +38 -0
- xoscar/backends/test/pool.py +233 -0
- xoscar/batch.py +256 -0
- xoscar/collective/__init__.py +27 -0
- xoscar/collective/backend/__init__.py +13 -0
- xoscar/collective/backend/nccl_backend.py +160 -0
- xoscar/collective/common.py +102 -0
- xoscar/collective/core.py +737 -0
- xoscar/collective/process_group.py +687 -0
- xoscar/collective/utils.py +41 -0
- xoscar/collective/xoscar_pygloo.cpython-313-x86_64-linux-gnu.so +0 -0
- xoscar/collective/xoscar_pygloo.pyi +239 -0
- xoscar/constants.py +23 -0
- xoscar/context.cpython-313-x86_64-linux-gnu.so +0 -0
- xoscar/context.pxd +21 -0
- xoscar/context.pyx +368 -0
- xoscar/core.cpython-313-x86_64-linux-gnu.so +0 -0
- xoscar/core.pxd +51 -0
- xoscar/core.pyx +664 -0
- xoscar/debug.py +188 -0
- xoscar/driver.py +42 -0
- xoscar/errors.py +63 -0
- xoscar/libcpp.pxd +31 -0
- xoscar/metrics/__init__.py +21 -0
- xoscar/metrics/api.py +288 -0
- xoscar/metrics/backends/__init__.py +13 -0
- xoscar/metrics/backends/console/__init__.py +13 -0
- xoscar/metrics/backends/console/console_metric.py +82 -0
- xoscar/metrics/backends/metric.py +149 -0
- xoscar/metrics/backends/prometheus/__init__.py +13 -0
- xoscar/metrics/backends/prometheus/prometheus_metric.py +70 -0
- xoscar/nvutils.py +717 -0
- xoscar/profiling.py +260 -0
- xoscar/serialization/__init__.py +20 -0
- xoscar/serialization/aio.py +142 -0
- xoscar/serialization/core.cpython-313-x86_64-linux-gnu.so +0 -0
- xoscar/serialization/core.pxd +28 -0
- xoscar/serialization/core.pyi +57 -0
- xoscar/serialization/core.pyx +944 -0
- xoscar/serialization/cuda.py +111 -0
- xoscar/serialization/exception.py +48 -0
- xoscar/serialization/mlx.py +67 -0
- xoscar/serialization/numpy.py +82 -0
- xoscar/serialization/pyfury.py +37 -0
- xoscar/serialization/scipy.py +72 -0
- xoscar/utils.py +522 -0
- xoscar/virtualenv/__init__.py +34 -0
- xoscar/virtualenv/core.py +268 -0
- xoscar/virtualenv/platform.py +56 -0
- xoscar/virtualenv/utils.py +100 -0
- xoscar/virtualenv/uv.py +321 -0
- xoscar-0.8.0.dist-info/METADATA +229 -0
- xoscar-0.8.0.dist-info/RECORD +93 -0
- xoscar-0.8.0.dist-info/WHEEL +6 -0
- xoscar-0.8.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Copyright 2022-2023 XProbe Inc.
|
|
2
|
+
# derived from copyright 1999-2021 Alibaba Group Holding Ltd.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
from typing import Any, Dict, List, Tuple
|
|
17
|
+
|
|
18
|
+
import pandas as pd
|
|
19
|
+
|
|
20
|
+
from ..utils import lazy_import
|
|
21
|
+
from .core import Serializer, buffered
|
|
22
|
+
|
|
23
|
+
cupy = lazy_import("cupy")
|
|
24
|
+
cudf = lazy_import("cudf")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class CupySerializer(Serializer):
|
|
28
|
+
@buffered
|
|
29
|
+
def serial(self, obj: Any, context: Dict):
|
|
30
|
+
if not (obj.flags["C_CONTIGUOUS"] or obj.flags["F_CONTIGUOUS"]):
|
|
31
|
+
obj = cupy.array(obj, copy=True)
|
|
32
|
+
|
|
33
|
+
header = obj.__cuda_array_interface__.copy()
|
|
34
|
+
header["strides"] = tuple(obj.strides)
|
|
35
|
+
header["lengths"] = [obj.nbytes]
|
|
36
|
+
buffer = cupy.ndarray(
|
|
37
|
+
shape=(obj.nbytes,), dtype=cupy.dtype("u1"), memptr=obj.data, strides=(1,)
|
|
38
|
+
)
|
|
39
|
+
return (header,), [buffer], True
|
|
40
|
+
|
|
41
|
+
def deserial(self, serialized: Tuple, context: Dict, subs: List):
|
|
42
|
+
(header,) = serialized
|
|
43
|
+
return cupy.ndarray(
|
|
44
|
+
shape=header["shape"],
|
|
45
|
+
dtype=header["typestr"],
|
|
46
|
+
memptr=cupy.asarray(subs[0]).data,
|
|
47
|
+
strides=header["strides"],
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class CudfSerializer(Serializer):
|
|
52
|
+
@staticmethod
|
|
53
|
+
def _get_ext_index_type(index_obj):
|
|
54
|
+
import cudf
|
|
55
|
+
|
|
56
|
+
multi_index_type = None
|
|
57
|
+
if isinstance(index_obj, pd.MultiIndex):
|
|
58
|
+
multi_index_type = "pandas"
|
|
59
|
+
elif isinstance(index_obj, cudf.MultiIndex):
|
|
60
|
+
multi_index_type = "cudf"
|
|
61
|
+
|
|
62
|
+
if multi_index_type is None:
|
|
63
|
+
return None
|
|
64
|
+
return {
|
|
65
|
+
"index_type": multi_index_type,
|
|
66
|
+
"names": list(index_obj.names),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def _apply_index_type(obj, attr, header):
|
|
71
|
+
import cudf
|
|
72
|
+
|
|
73
|
+
multi_index_cls = (
|
|
74
|
+
pd.MultiIndex if header["index_type"] == "pandas" else cudf.MultiIndex
|
|
75
|
+
)
|
|
76
|
+
original_index = getattr(obj, attr)
|
|
77
|
+
if isinstance(original_index, (pd.MultiIndex, cudf.MultiIndex)):
|
|
78
|
+
return
|
|
79
|
+
new_index = multi_index_cls.from_tuples(original_index, names=header["names"])
|
|
80
|
+
setattr(obj, attr, new_index)
|
|
81
|
+
|
|
82
|
+
def serial(self, obj: Any, context: Dict):
|
|
83
|
+
header, buffers = obj.device_serialize()
|
|
84
|
+
if hasattr(obj, "columns"):
|
|
85
|
+
header["_ext_columns"] = self._get_ext_index_type(obj.columns)
|
|
86
|
+
if hasattr(obj, "index"):
|
|
87
|
+
header["_ext_index"] = self._get_ext_index_type(obj.index)
|
|
88
|
+
return (header,), buffers, True
|
|
89
|
+
|
|
90
|
+
def deserial(self, serialized: Tuple, context: Dict, buffers: List):
|
|
91
|
+
from cudf.core.abc import Serializable
|
|
92
|
+
|
|
93
|
+
(header,) = serialized
|
|
94
|
+
col_header = header.pop("_ext_columns", None)
|
|
95
|
+
index_header = header.pop("_ext_index", None)
|
|
96
|
+
|
|
97
|
+
result = Serializable.device_deserialize(header, buffers)
|
|
98
|
+
|
|
99
|
+
if col_header is not None:
|
|
100
|
+
self._apply_index_type(result, "columns", col_header)
|
|
101
|
+
if index_header is not None:
|
|
102
|
+
self._apply_index_type(result, "index", index_header)
|
|
103
|
+
return result
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
if cupy is not None:
|
|
107
|
+
CupySerializer.register("cupy.ndarray")
|
|
108
|
+
if cudf is not None:
|
|
109
|
+
CudfSerializer.register("cudf.DataFrame")
|
|
110
|
+
CudfSerializer.register("cudf.Series")
|
|
111
|
+
CudfSerializer.register("cudf.Index")
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Copyright 2022-2023 XProbe Inc.
|
|
2
|
+
# derived from copyright 1999-2021 Alibaba Group Holding Ltd.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import pickle # nosec # pylint: disable=import_pickle
|
|
19
|
+
|
|
20
|
+
from .core import Serializer, buffered, pickle_buffers, unpickle_buffers
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class UnpickleableError(Exception):
|
|
24
|
+
def __init__(self, raw_error: str | Exception):
|
|
25
|
+
if isinstance(raw_error, str):
|
|
26
|
+
super().__init__(raw_error)
|
|
27
|
+
else:
|
|
28
|
+
super().__init__(
|
|
29
|
+
f"Error cannot be pickled, "
|
|
30
|
+
f"error type: {type(raw_error)}, "
|
|
31
|
+
f"raw error:\n{raw_error}"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class ExceptionSerializer(Serializer):
|
|
36
|
+
@buffered
|
|
37
|
+
def serial(self, obj: Exception, context: dict):
|
|
38
|
+
try:
|
|
39
|
+
buffers = pickle_buffers(obj)
|
|
40
|
+
except (TypeError, pickle.PicklingError):
|
|
41
|
+
buffers = pickle_buffers(UnpickleableError(obj))
|
|
42
|
+
return (), buffers, True
|
|
43
|
+
|
|
44
|
+
def deserial(self, serialized: tuple, context: dict, subs: list):
|
|
45
|
+
return unpickle_buffers(subs)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
ExceptionSerializer.register(Exception)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Copyright 2022-2025 XProbe Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from typing import Any, List
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
from ..utils import lazy_import
|
|
20
|
+
from .core import Serializer, buffered
|
|
21
|
+
|
|
22
|
+
mx = lazy_import("mlx.core")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
dtype_map = {
|
|
26
|
+
"b": np.int8,
|
|
27
|
+
"B": np.uint8,
|
|
28
|
+
"h": np.int16,
|
|
29
|
+
"H": np.uint16,
|
|
30
|
+
"i": np.int32,
|
|
31
|
+
"I": np.uint32,
|
|
32
|
+
"q": np.int64,
|
|
33
|
+
"Q": np.uint64,
|
|
34
|
+
"e": np.float16,
|
|
35
|
+
"f": np.float32,
|
|
36
|
+
"d": np.float64,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class MLXSerislizer(Serializer):
|
|
41
|
+
@buffered
|
|
42
|
+
def serial(self, obj: "mx.array", context: dict): # type: ignore
|
|
43
|
+
ravel_obj = obj.reshape(-1).view(mx.uint8)
|
|
44
|
+
mv = memoryview(ravel_obj)
|
|
45
|
+
header = dict(
|
|
46
|
+
shape=obj.shape, format=mv.format, dtype=str(obj.dtype).rsplit(".", 1)[-1]
|
|
47
|
+
)
|
|
48
|
+
if not mv.c_contiguous:
|
|
49
|
+
# NOTE: we only consider c contiguous here,
|
|
50
|
+
# MLX has no way to create f contiguous arrays.
|
|
51
|
+
mv = memoryview(bytes(mv))
|
|
52
|
+
return (header,), [mv], True
|
|
53
|
+
|
|
54
|
+
def deserial(self, serialized: tuple, context: dict, subs: List[Any]):
|
|
55
|
+
header = serialized[0]
|
|
56
|
+
shape, format, dtype = header["shape"], header["format"], header["dtype"]
|
|
57
|
+
mv = memoryview(subs[0])
|
|
58
|
+
if mv.format != format:
|
|
59
|
+
dtype = dtype_map.get(format, np.uint8)
|
|
60
|
+
np_arr = np.frombuffer(mv, dtype=dtype).reshape(shape) # parse
|
|
61
|
+
mv = memoryview(np_arr) # recreate memoryview
|
|
62
|
+
ravel_array = mx.array(mv)
|
|
63
|
+
return ravel_array.view(getattr(mx, dtype)).reshape(shape)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if mx is not None:
|
|
67
|
+
MLXSerislizer.register(mx.array)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Copyright 2022-2023 XProbe Inc.
|
|
2
|
+
# derived from copyright 1999-2021 Alibaba Group Holding Ltd.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
from typing import Any, Dict, List, Tuple
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
from .core import Serializer, buffered, pickle_buffers, unpickle_buffers
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class NDArraySerializer(Serializer):
|
|
24
|
+
@buffered
|
|
25
|
+
def serial(self, obj: np.ndarray, context: Dict):
|
|
26
|
+
header: dict = {}
|
|
27
|
+
if obj.dtype.hasobject:
|
|
28
|
+
header["pickle"] = True
|
|
29
|
+
buffers = pickle_buffers(obj)
|
|
30
|
+
return (header,), buffers, True
|
|
31
|
+
|
|
32
|
+
order = "C"
|
|
33
|
+
if obj.flags.f_contiguous:
|
|
34
|
+
order = "F"
|
|
35
|
+
elif not obj.flags.c_contiguous:
|
|
36
|
+
obj = np.ascontiguousarray(obj)
|
|
37
|
+
try:
|
|
38
|
+
desc = np.lib.format.dtype_to_descr(obj.dtype)
|
|
39
|
+
dtype_new_order = None
|
|
40
|
+
except ValueError:
|
|
41
|
+
# for structured dtype, array[[field2, field1]] will create a view,
|
|
42
|
+
# and dtype_to_desc will fail due to the order
|
|
43
|
+
fields = obj.dtype.fields
|
|
44
|
+
new_fields = sorted(fields, key=lambda k: fields[k][1])
|
|
45
|
+
desc = np.lib.format.dtype_to_descr(obj.dtype[new_fields])
|
|
46
|
+
dtype_new_order = list(fields)
|
|
47
|
+
header.update(
|
|
48
|
+
dict(
|
|
49
|
+
pickle=False,
|
|
50
|
+
descr=desc,
|
|
51
|
+
dtype_new_order=dtype_new_order,
|
|
52
|
+
shape=list(obj.shape),
|
|
53
|
+
strides=list(obj.strides),
|
|
54
|
+
order=order,
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
return (header,), [memoryview(obj.ravel(order=order).view("uint8").data)], True # type: ignore
|
|
58
|
+
|
|
59
|
+
def deserial(self, serialized: Tuple, context: Dict, subs: List[Any]):
|
|
60
|
+
header = serialized[0]
|
|
61
|
+
if header["pickle"]:
|
|
62
|
+
return unpickle_buffers(subs)
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
dtype = np.lib.format.descr_to_dtype(header["descr"])
|
|
66
|
+
except AttributeError: # pragma: no cover
|
|
67
|
+
# for older numpy versions, descr_to_dtype is not implemented
|
|
68
|
+
dtype = np.dtype(header["descr"])
|
|
69
|
+
|
|
70
|
+
dtype_new_order = header["dtype_new_order"]
|
|
71
|
+
if dtype_new_order:
|
|
72
|
+
dtype = dtype[dtype_new_order]
|
|
73
|
+
return np.ndarray(
|
|
74
|
+
shape=tuple(header["shape"]),
|
|
75
|
+
dtype=dtype,
|
|
76
|
+
buffer=subs[0],
|
|
77
|
+
strides=tuple(header["strides"]),
|
|
78
|
+
order=header["order"],
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
NDArraySerializer.register(np.ndarray)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import threading
|
|
3
|
+
|
|
4
|
+
_fury = threading.local()
|
|
5
|
+
_fury_not_installed = object()
|
|
6
|
+
_register_class_list = set()
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def register_classes(*args):
|
|
10
|
+
instance = get_fury()
|
|
11
|
+
if instance is not None:
|
|
12
|
+
_register_class_list.update(args)
|
|
13
|
+
for c in _register_class_list:
|
|
14
|
+
instance.register_class(c)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_fury():
|
|
18
|
+
if os.environ.get("USE_FURY") in ("1", "true", "True"):
|
|
19
|
+
instance = getattr(_fury, "instance", None)
|
|
20
|
+
if instance is _fury_not_installed: # pragma: no cover
|
|
21
|
+
return None
|
|
22
|
+
if instance is not None:
|
|
23
|
+
return instance
|
|
24
|
+
else:
|
|
25
|
+
try:
|
|
26
|
+
import pyfury
|
|
27
|
+
|
|
28
|
+
_fury.instance = instance = pyfury.Fury(
|
|
29
|
+
language=pyfury.Language.PYTHON, require_class_registration=False
|
|
30
|
+
)
|
|
31
|
+
for c in _register_class_list: # pragma: no cover
|
|
32
|
+
instance.register_class(c)
|
|
33
|
+
print("pyfury is enabled.")
|
|
34
|
+
except ImportError: # pragma: no cover
|
|
35
|
+
print("pyfury is not installed.")
|
|
36
|
+
_fury.instance = _fury_not_installed
|
|
37
|
+
return instance
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Copyright 2022-2023 XProbe Inc.
|
|
2
|
+
# derived from copyright 1999-2021 Alibaba Group Holding Ltd.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
from typing import Any, Dict, List, Tuple
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
import scipy.sparse as sps
|
|
22
|
+
except ImportError: # pragma: no cover
|
|
23
|
+
sps = None
|
|
24
|
+
|
|
25
|
+
from .core import Serializer, buffered, deserialize, serialize
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class CsrMatrixSerializer(Serializer):
|
|
29
|
+
@buffered
|
|
30
|
+
def serial(self, obj: Any, context: Dict):
|
|
31
|
+
data_header, data_buffers = serialize(obj.data)
|
|
32
|
+
idx_header, idx_buffers = serialize(obj.indices)
|
|
33
|
+
indptr_header, indptr_buffers = serialize(obj.indptr)
|
|
34
|
+
header = (
|
|
35
|
+
data_header, # data_header
|
|
36
|
+
len(data_buffers), # data_buf_num
|
|
37
|
+
idx_header, # idx_header
|
|
38
|
+
len(idx_buffers), # idx_buf_num
|
|
39
|
+
indptr_header, # indptr_header
|
|
40
|
+
obj.shape, # shape
|
|
41
|
+
)
|
|
42
|
+
return header, data_buffers + idx_buffers + indptr_buffers, True
|
|
43
|
+
|
|
44
|
+
def deserial(self, serialized: Tuple, context: Dict, subs: List):
|
|
45
|
+
(
|
|
46
|
+
data_header,
|
|
47
|
+
data_buf_num,
|
|
48
|
+
idx_header,
|
|
49
|
+
idx_buf_num,
|
|
50
|
+
indptr_header,
|
|
51
|
+
shape,
|
|
52
|
+
) = serialized
|
|
53
|
+
data_buffers = subs[:data_buf_num]
|
|
54
|
+
idx_buffers = subs[data_buf_num : data_buf_num + idx_buf_num]
|
|
55
|
+
indptr_buffers = subs[data_buf_num + idx_buf_num :]
|
|
56
|
+
|
|
57
|
+
data = deserialize(data_header, data_buffers)
|
|
58
|
+
indices = deserialize(idx_header, idx_buffers)
|
|
59
|
+
indptr = deserialize(indptr_header, indptr_buffers)
|
|
60
|
+
shape = tuple(shape)
|
|
61
|
+
|
|
62
|
+
empty_arr = np.zeros(0, dtype=data.dtype)
|
|
63
|
+
|
|
64
|
+
target_csr = sps.coo_matrix(
|
|
65
|
+
(empty_arr, (empty_arr,) * 2), dtype=data.dtype, shape=shape
|
|
66
|
+
).tocsr()
|
|
67
|
+
target_csr.data, target_csr.indices, target_csr.indptr = data, indices, indptr
|
|
68
|
+
return target_csr
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if sps: # pragma: no branch
|
|
72
|
+
CsrMatrixSerializer.register(sps.csr_matrix)
|