-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add in equinox interface (aka shrek sampler)
- Loading branch information
Showing
3 changed files
with
34 additions
and
2 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
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,30 @@ | ||
# shrek llama | ||
|
||
import torch | ||
from math import sqrt | ||
|
||
from entropix.torch_sampler import sample, device | ||
from .textgen import TextGen, TextChat | ||
|
||
class ShrekMixin: | ||
def sample(self, tokens, **kwargs): | ||
head_dim = self.model.params['head_dim_kv'] | ||
n_layers = self.model.params['llama.block_count'] | ||
score_name = f'attn{n_layers-1}_pre_scores' | ||
|
||
tokens = torch.tensor(tokens, dtype=torch.int32, device=device) | ||
logits = self.logits(tokens) | ||
scores = self.model.get_named_node(score_name) / sqrt(head_dim) | ||
|
||
batch_tokens = tokens.unsqueeze(0) | ||
batch_logits = logits.unsqueeze(0) | ||
batch_scores = scores.unsqueeze(0) | ||
|
||
nexts = sample(batch_tokens, batch_logits, batch_scores, **kwargs) | ||
return nexts.squeeze(0).item() | ||
|
||
class ShrekGen(TextGen, ShrekMixin): | ||
pass | ||
|
||
class ShrekChat(TextChat, ShrekMixin): | ||
pass |