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 errors in hivemind.p2p and hivemind.compression #565

Merged
merged 2 commits into from
Apr 26, 2023
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
19 changes: 9 additions & 10 deletions hivemind/compression/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ def compress(self, tensor: torch.Tensor, info: CompressionInfo, allow_inplace: b
dtype_name = str(tensor.dtype).lstrip("torch.")
raw_data = tensor
if tensor.dtype == torch.bfloat16:
if USE_LEGACY_BFLOAT16:
if USE_LEGACY_BFLOAT16: # legacy mode: convert to fp32
raw_data = tensor.to(torch.float32)
else:
typed_storage = tensor.storage()
storage = typed_storage.untyped() if hasattr(typed_storage, "untyped") else typed_storage._untyped()
raw_data = torch.tensor(storage, dtype=torch.int8)
else: # efficient mode: send bfloat16 data directly
# reinterpret_cast to an arbitrary 2-byte type supported by numpy
raw_data = tensor.view(torch.int16)
mryab marked this conversation as resolved.
Show resolved Hide resolved

return runtime_pb2.Tensor(
compression=self.compression_type,
Expand All @@ -106,13 +105,13 @@ def extract(self, serialized_tensor: runtime_pb2.Tensor) -> torch.Tensor:
shape = torch.Size(serialized_tensor.size)
if serialized_tensor.dtype == "bfloat16":
numel = shape.numel()
if numel > 0 and len(serialized_tensor.buffer) // numel == 4: # legacy mode: convert to fp32
if numel > 0 and len(serialized_tensor.buffer) // numel == 4:
array = np.frombuffer(serialized_tensor.buffer, dtype=np.float32)
tensor = torch.as_tensor(array, dtype=torch.bfloat16)
else: # efficient mode: send bfloat16 data directly
storage_type = torch.TypedStorage if hasattr(torch, "TypedStorage") else torch._TypedStorage
storage = storage_type.from_buffer(serialized_tensor.buffer, byte_order="little", dtype=torch.bfloat16)
tensor = torch.as_tensor(storage, dtype=torch.bfloat16)
else:
array = np.frombuffer(serialized_tensor.buffer, dtype=np.int16)
# reinterpret_cast from an arbitrary 2-byte type supported by numpy
tensor = torch.as_tensor(array).view(torch.bfloat16)
mryab marked this conversation as resolved.
Show resolved Hide resolved
else:
array = np.frombuffer(serialized_tensor.buffer, dtype=np.dtype(serialized_tensor.dtype))
tensor = torch.as_tensor(array)
Expand Down
5 changes: 3 additions & 2 deletions hivemind/p2p/p2p_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,9 @@ def _terminate(self) -> None:

self._alive = False
if self._child is not None and self._child.returncode is None:
self._child.terminate()
logger.debug(f"Terminated p2pd with id = {self.peer_id}")
with suppress(ProcessLookupError):
self._child.terminate()
logger.debug(f"Terminated p2pd with id = {self.peer_id}")

with suppress(FileNotFoundError):
os.remove(self._daemon_listen_maddr["unix"])
Expand Down
3 changes: 2 additions & 1 deletion hivemind/p2p/p2p_daemon_bindings/p2pclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ async def create(
return client

def close(self) -> None:
self.control.close()
if self.control is not None:
self.control.close()

def __del__(self):
self.close()
Expand Down
Loading