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

Handle bfloat16 weights in disk offload without adding memory overhead #461

Merged
merged 1 commit into from
Jun 22, 2022
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
10 changes: 5 additions & 5 deletions src/accelerate/utils/offload.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def offload_weight(weight, weight_name, offload_folder, index=None):
dtype = None
# Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
if str(weight.dtype) == "torch.bfloat16":
# Need to convert to FP32 since NumPy does not handle bfloat16s.
weight = weight.float()
# Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
weight = weight.view(torch.int16)
dtype = "bfloat16"
array = weight.numpy()
tensor_file = os.path.join(offload_folder, f"{weight_name}.dat")
Expand All @@ -50,16 +50,16 @@ def load_offloaded_weight(weight_file, weight_info):

dtype = weight_info["dtype"]
if dtype == "bfloat16":
# NumPy does not support bfloat16 so this was saved as a float32
dtype = "float32"
# NumPy does not support bfloat16 so this was saved as a int16
dtype = "int16"

weight = np.memmap(weight_file, dtype=dtype, shape=shape, mode="r")

if len(weight_info["shape"]) == 0:
weight = weight[0]
weight = torch.tensor(weight)
if weight_info["dtype"] == "bfloat16":
weight = weight.to(torch.bfloat16)
weight = weight.view(torch.bfloat16)

return weight

Expand Down