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

Add bfp16 support to inhibit transforming to fp32 when using llama3 #31

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def forward(
self.apply_rotary(k, 0, attention_mask)
) # keys are cached so no offset

if self.cfg.dtype not in [torch.float32, torch.float64]:
if self.cfg.dtype not in [torch.float32, torch.float64, torch.bfloat16]:
# If using 16 bits, increase the precision to avoid numerical instabilities
q = q.to(torch.float32)
k = k.to(torch.float32)
Expand Down
2 changes: 1 addition & 1 deletion TransformerLens/transformer_lens/components/rms_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, cfg: Union[Dict, HookedTransformerConfig], length: Optional[i
def forward(
self, x: Float[torch.Tensor, "batch pos length"]
) -> Float[torch.Tensor, "batch pos length"]:
if self.cfg.dtype not in [torch.float32, torch.float64]:
if self.cfg.dtype not in [torch.float32, torch.float64, torch.bfloat16]:
x = x.to(torch.float32)
scale: Float[torch.Tensor, "batch pos 1"] = self.hook_scale(
(x.pow(2).mean(-1, keepdim=True) + self.eps).sqrt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, cfg: Union[Dict, HookedTransformerConfig]):
def forward(
self, x: Float[torch.Tensor, "batch pos length"]
) -> Float[torch.Tensor, "batch pos length"]:
if self.cfg.dtype not in [torch.float32, torch.float64]:
if self.cfg.dtype not in [torch.float32, torch.float64, torch.bfloat16]:
x = x.to(torch.float32)

scale: Float[torch.Tensor, "batch pos 1"] = self.hook_scale(
Expand Down