Fixing Dtype error: expected scalar type BFloat16 but found Float for Torch parts#59
Closed
nanowell wants to merge 1 commit intoxjdr-alt:mainfrom
Closed
Fixing Dtype error: expected scalar type BFloat16 but found Float for Torch parts#59nanowell wants to merge 1 commit intoxjdr-alt:mainfrom
nanowell wants to merge 1 commit intoxjdr-alt:mainfrom
Conversation
This PR will fix dtype errors: RuntimeError: expected scalar type BFloat16 but found Float
|
I fixed it with this diff --git a/entropix/torch_model.py b/entropix/torch_model.py
index 0ebb3e9..bda7629 100644
--- a/entropix/torch_model.py
+++ b/entropix/torch_model.py
@@ -25,7 +25,7 @@ from typing import Tuple, Optional
def rms_norm(x: torch.Tensor, w: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
return w * (x * torch.rsqrt(torch.pow(x, 2).mean(-1, keepdim=True) + eps))
-def apply_rotary_emb(xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor, dtype: torch.dtype = torch.float32) -> Tuple[torch.Tensor, torch.Tensor]:
+def apply_rotary_emb(xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor, dtype: torch.dtype = torch.bfloat16) -> Tuple[torch.Tensor, torch.Tensor]:
reshape_xq = xq.float().reshape(*xq.shape[:-1], -1, 2)
reshape_xk = xk.float().reshape(*xk.shape[:-1], -1, 2)
xq_ = torch.complex(reshape_xq[..., 0], reshape_xq[..., 1])
@@ -54,7 +54,7 @@ def attention(x: torch.Tensor, layer_weights: LayerWeights, model_params, cur_po
scores = scores + attn_mask
mask = torch.where(scores != 0.0, scores, DEFAULT_MASK_VALUE)
padded_logits = torch.where((mask >= DEFAULT_MASK_VALUE * 0.5), scores, DEFAULT_MASK_VALUE)
- scores = F.softmax(padded_logits, dim=-1).to(torch.float32)
+ scores = F.softmax(padded_logits, dim=-1).to(x.dtype)
output = torch.matmul(scores, values)
output = output.transpose(1, 2).reshape(xq.shape[0], xq.shape[2], -1)
out = F.linear(output, layer_weights.wo)alternatively you could pass edit: looks like same problem as #39 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR will fix dtype errors: RuntimeError: expected scalar type BFloat16 but found Float