tenso 0.1.0__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.
- tenso-0.1.0/PKG-INFO +6 -0
- tenso-0.1.0/pyproject.toml +21 -0
- tenso-0.1.0/src/tenso/__init__.py +3 -0
- tenso-0.1.0/src/tenso/core.py +56 -0
tenso-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "tenso" # The install name (must be unique on PyPI)
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "High-performance zero-copy tensor serialization"
|
|
5
|
+
dependencies = [
|
|
6
|
+
"numpy",
|
|
7
|
+
]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
|
|
10
|
+
[tool.setuptools.packages.find]
|
|
11
|
+
where = ["src"]
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["uv_build>=0.8.14,<0.9.0"]
|
|
15
|
+
build-backend = "uv_build"
|
|
16
|
+
|
|
17
|
+
[dependency-groups]
|
|
18
|
+
dev = [
|
|
19
|
+
"build>=1.3.0",
|
|
20
|
+
"pytest>=9.0.1",
|
|
21
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import struct
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
# --- The Tenso Protocol ---
|
|
5
|
+
_MAGIC = b'TNSO' # Magic bytes for file identification
|
|
6
|
+
_VERSION = 1
|
|
7
|
+
|
|
8
|
+
_DTYPE_MAP = {
|
|
9
|
+
np.dtype('float32'): 1,
|
|
10
|
+
np.dtype('int32'): 2,
|
|
11
|
+
np.dtype('float64'): 3,
|
|
12
|
+
np.dtype('int64'): 4,
|
|
13
|
+
}
|
|
14
|
+
_REV_DTYPE_MAP = {v: k for k, v in _DTYPE_MAP.items()}
|
|
15
|
+
|
|
16
|
+
def dumps(tensor: np.ndarray) -> bytes:
|
|
17
|
+
"""Serialize a numpy array into bytes (Zero-Copy)."""
|
|
18
|
+
# 1. Validation
|
|
19
|
+
if tensor.dtype not in _DTYPE_MAP:
|
|
20
|
+
raise ValueError(f"Unsupported dtype: {tensor.dtype}")
|
|
21
|
+
|
|
22
|
+
dtype_code = _DTYPE_MAP[tensor.dtype]
|
|
23
|
+
shape = tensor.shape
|
|
24
|
+
ndim = len(shape)
|
|
25
|
+
|
|
26
|
+
# 2. Header (8 Bytes): Magic + Ver + Flags + Dtype + Ndim
|
|
27
|
+
header = struct.pack('<4sBBBB', _MAGIC, _VERSION, 0, dtype_code, ndim)
|
|
28
|
+
|
|
29
|
+
# 3. Shape Block (Variable): Ndim * uint32
|
|
30
|
+
shape_block = struct.pack(f'<{ndim}I', *shape)
|
|
31
|
+
|
|
32
|
+
# 4. Body (Zero-Copy): Raw memory dump
|
|
33
|
+
return header + shape_block + tensor.tobytes()
|
|
34
|
+
|
|
35
|
+
def loads(data: bytes) -> np.ndarray:
|
|
36
|
+
"""Deserialize bytes back into a numpy array."""
|
|
37
|
+
# 1. Parse Header
|
|
38
|
+
magic, ver, flags, dtype_code, ndim = struct.unpack('<4sBBBB', data[:8])
|
|
39
|
+
|
|
40
|
+
if magic != _MAGIC:
|
|
41
|
+
raise ValueError("Invalid tenso packet (Magic bytes mismatch)")
|
|
42
|
+
|
|
43
|
+
# 2. Parse Shape
|
|
44
|
+
shape_end = 8 + (ndim * 4)
|
|
45
|
+
shape = struct.unpack(f'<{ndim}I', data[8:shape_end])
|
|
46
|
+
|
|
47
|
+
# 3. Parse Body
|
|
48
|
+
dtype = _REV_DTYPE_MAP[dtype_code]
|
|
49
|
+
return np.frombuffer(data, dtype=dtype, offset=shape_end).reshape(shape)
|
|
50
|
+
|
|
51
|
+
# File I/O Helpers
|
|
52
|
+
def dump(tensor: np.ndarray, fp) -> None:
|
|
53
|
+
fp.write(dumps(tensor))
|
|
54
|
+
|
|
55
|
+
def load(fp) -> np.ndarray:
|
|
56
|
+
return loads(fp.read())
|