-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ensembling_layer
- Loading branch information
Showing
11 changed files
with
156 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
Submodule comparison-sweeps
added at
f4ed88
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import torch | ||
from torch import Tensor, nn | ||
|
||
|
||
class BurnsNorm(nn.Module): | ||
"""Burns et al. style normalization. Minimal changes from the original code.""" | ||
|
||
def __init__(self, scale: bool = True): | ||
super().__init__() | ||
self.scale: bool = scale | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
"""Normalizes per prompt template | ||
Args: | ||
x: input of dimension (n, v, c, d) or (n, v, d) | ||
Returns: | ||
x_normalized: normalized output | ||
""" | ||
num_elements = x.shape[0] | ||
x_normalized: Tensor = x - x.mean(dim=0) if num_elements > 1 else x | ||
|
||
if not self.scale: | ||
return x_normalized | ||
else: | ||
std = torch.linalg.norm(x_normalized, dim=0) / x_normalized.shape[0] ** 0.5 | ||
assert std.dim() == x.dim() - 1 | ||
|
||
# Compute the dimensions over which | ||
# we want to compute the mean standard deviation | ||
# exclude the first dimension v, | ||
# which is the template dimension | ||
dims = tuple(range(1, std.dim())) | ||
|
||
avg_norm = std.mean(dim=dims, keepdim=True) | ||
|
||
return x_normalized / avg_norm |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.