Skip to content

Commit

Permalink
Make is_torch_bf16_available_on_device more strict (huggingface#28796)
Browse files Browse the repository at this point in the history
fix

Co-authored-by: ydshieh <[email protected]>
  • Loading branch information
ydshieh and ydshieh authored Feb 1, 2024
1 parent 0d26abd commit eb8e7a0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/transformers/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ def is_torch_fp16_available_on_device(device):
try:
x = torch.zeros(2, 2, dtype=torch.float16).to(device)
_ = x @ x

# At this moment, let's be strict of the check: check if `LayerNorm` is also supported on device, because many
# models use this layer.
batch, sentence_length, embedding_dim = 3, 4, 5
embedding = torch.randn(batch, sentence_length, embedding_dim, dtype=torch.float16, device=device)
layer_norm = torch.nn.LayerNorm(embedding_dim, dtype=torch.float16, device=device)
_ = layer_norm(embedding)

except: # noqa: E722
# TODO: more precise exception matching, if possible.
# most backends should return `RuntimeError` however this is not guaranteed.
Expand Down

0 comments on commit eb8e7a0

Please sign in to comment.