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 dump of bfloat16 torch tensor #7002

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
12 changes: 9 additions & 3 deletions src/datasets/utils/_dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,17 @@ def _save_torchTensor(pickler, obj):
import torch # type: ignore

# `torch.from_numpy` is not picklable in `torch>=1.11.0`
def create_torchTensor(np_array):
return torch.from_numpy(np_array)
def create_torchTensor(np_array, dtype=None):
tensor = torch.from_numpy(np_array)
if dtype:
tensor = tensor.type(torch.bfloat16)
return tensor
Comment on lines +165 to +169

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future compatibility? 😄

Suggested change
def create_torchTensor(np_array, dtype=None):
tensor = torch.from_numpy(np_array)
if dtype:
tensor = tensor.type(torch.bfloat16)
return tensor
def create_torchTensor(np_array, dtype=None):
tensor = torch.from_numpy(np_array)
if dtype:
tensor = tensor.type(dtype)
return tensor

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah good catch, I went too fast x)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed at #7003


log(pickler, f"To: {obj}")
args = (obj.detach().cpu().numpy(),)
if obj.dtype == torch.bfloat16:
args = (obj.detach().to(torch.float).cpu().numpy(), torch.bfloat16)
else:
args = (obj.detach().cpu().numpy(),)
pickler.save_reduce(create_torchTensor, args, obj=obj)
log(pickler, "# To")

Expand Down
Loading