Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization support for np.void datatype #4437

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions flax/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ def _dtype_from_name(name: str):
"""Handle JAX bfloat16 dtype correctly."""
if name == b'bfloat16':
return jax.numpy.bfloat16
elif name[0:4] == b'void':
# decode voidNNNN, where NNNN is the length in bits
return np.void(int(name[4:]) // 8)
else:
return np.dtype(name)

Expand Down Expand Up @@ -343,6 +346,10 @@ def _np_convert_in_place(d):

def _chunk(arr) -> dict[str, Any]:
"""Convert array to a canonical dictionary of chunked arrays."""
if isinstance(arr.dtype, np.dtypes.VoidDType):
# The chunking strategy below doesn't work for a large scalar np.void.
# Implement an alternative strategy later if needed.
raise NotImplementedError('Chunking not implemented for np.void dtype')
chunksize = max(1, int(MAX_CHUNK_SIZE / arr.dtype.itemsize))
data = {'__msgpack_chunked_array__': True, 'shape': _tuple_to_dict(arr.shape)}
flatarr = arr.reshape(-1)
Expand Down
1 change: 1 addition & 0 deletions tests/serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def __call__(self):
'float',
'complex',
'bool',
'void',
])
def test_numpy_serialization(self, dtype):
np.random.seed(0)
Expand Down
Loading