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: convert bfloat16 to float because of numpy incompatibility #53

Merged
merged 2 commits into from
Oct 3, 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: 11 additions & 1 deletion model2vec/distill/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@
"""
encodings = tokenizer(tokens, return_tensors="pt", padding=True, truncation=True).to(model.device)
encoded: BaseModelOutputWithPoolingAndCrossAttentions = model(**encodings)
out = encoded.last_hidden_state.cpu()
out: torch.Tensor = encoded.last_hidden_state.cpu()
# NOTE: If the dtype is bfloat 16, we convert to float32,
# because numpy does not suport bfloat16
# See here: https://github.com/numpy/numpy/issues/19808
if out.dtype == torch.bfloat16:
out = out.float()

Check warning on line 75 in model2vec/distill/inference.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/inference.py#L75

Added line #L75 was not covered by tests

mask = encodings["attention_mask"].cpu()
# NOTE: evil hack. For any batch, there will be a mask vector
Expand Down Expand Up @@ -124,6 +129,11 @@
with torch.no_grad():
encoded: BaseModelOutputWithPoolingAndCrossAttentions = model(input_ids=batch.to(device))
out: torch.Tensor = encoded.last_hidden_state
# NOTE: If the dtype is bfloat 16, we convert to float32,
# because numpy does not suport bfloat16
# See here: https://github.com/numpy/numpy/issues/19808
if out.dtype == torch.bfloat16:
out = out.float()

Check warning on line 136 in model2vec/distill/inference.py

View check run for this annotation

Codecov / codecov/patch

model2vec/distill/inference.py#L136

Added line #L136 was not covered by tests
intermediate_weights.append(out[:, 1].cpu().numpy())
out_weights = np.concatenate(intermediate_weights)

Expand Down