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

Fix empty tensor deserialization #131

Merged
Merged
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
14 changes: 12 additions & 2 deletions src/litdata/streaming/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ def deserialize(self, data: bytes) -> torch.Tensor:
shape = []
for shape_idx in range(shape_size):
shape.append(np.frombuffer(data[8 + 4 * shape_idx : 8 + 4 * (shape_idx + 1)], np.uint32).item())
tensor = torch.frombuffer(data[8 + 4 * (shape_idx + 1) : len(data)], dtype=dtype)
idx_start = 8 + 4 * (shape_idx + 1)
idx_end = len(data)
if idx_end > idx_start:
tensor = torch.frombuffer(data[idx_start:idx_end], dtype=dtype)
else:
assert idx_start == idx_end, "The starting index should never be greater than end ending index."
tensor = torch.empty(shape, dtype=dtype)
shape = torch.Size(shape)
if tensor.shape == shape:
return tensor
Expand All @@ -211,7 +217,11 @@ def serialize(self, item: torch.Tensor) -> Tuple[bytes, Optional[str]]:

def deserialize(self, data: bytes) -> torch.Tensor:
assert self._dtype
return torch.frombuffer(data, dtype=self._dtype)
if len(data) > 0:
tensor = torch.frombuffer(data, dtype=self._dtype)
else:
tensor = torch.empty((0,), dtype=self._dtype)
return tensor

def can_serialize(self, item: torch.Tensor) -> bool:
return isinstance(item, torch.Tensor) and type(item) == torch.Tensor and len(item.shape) == 1
Expand Down
28 changes: 28 additions & 0 deletions tests/streaming/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,31 @@ class CustomSerializer(NoHeaderTensorSerializer):

assert isinstance(serializers["no_header_tensor"], CustomSerializer)
assert isinstance(serializers["custom"], CustomSerializer)


def test_deserialize_empty_tensor():
serializer = TensorSerializer()
t = torch.ones((0, 3)).int()
data, name = serializer.serialize(t)
new_t = serializer.deserialize(data)
assert torch.equal(t, new_t)

t = torch.ones((0, 3)).float()
data, name = serializer.serialize(t)
new_t = serializer.deserialize(data)
assert torch.equal(t, new_t)


def test_deserialize_empty_no_header_tensor():
serializer = NoHeaderTensorSerializer()
t = torch.ones((0,)).int()
data, name = serializer.serialize(t)
serializer.setup(name)
new_t = serializer.deserialize(data)
assert torch.equal(t, new_t)

t = torch.ones((0,)).float()
data, name = serializer.serialize(t)
serializer.setup(name)
new_t = serializer.deserialize(data)
assert torch.equal(t, new_t)
Loading