Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add initial generative support * make generation context_length independend * remove kwargs * last positional embeddings for CLS * typo * fix mask len * add comment * remove unused args * simpler logic for input shorter than context length Co-authored-by: gpucce <g.puccetti@gmail.com>
- Loading branch information
Showing
3 changed files
with
118 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from math import ceil | ||
import torch | ||
from torch import nn | ||
import torch.nn.functional as F | ||
|
||
def exists(val): | ||
return val is not None | ||
|
||
# nucleus | ||
|
||
def top_p(logits, thres = 0.9): | ||
sorted_logits, sorted_indices = torch.sort(logits, descending=True) | ||
cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1) | ||
|
||
sorted_indices_to_remove = cum_probs > (1 - thres) | ||
sorted_indices_to_remove[:, 1:] = sorted_indices_to_remove[:, :-1].clone() | ||
sorted_indices_to_remove[:, 0] = 0 | ||
|
||
sorted_logits[sorted_indices_to_remove] = float('-inf') | ||
return sorted_logits.scatter(1, sorted_indices, sorted_logits) | ||
|
||
# topk | ||
|
||
def top_k(logits, thres = 0.9): | ||
k = ceil((1 - thres) * logits.shape[-1]) | ||
val, ind = torch.topk(logits, k) | ||
probs = torch.full_like(logits, float('-inf')) | ||
probs.scatter_(1, ind, val) | ||
return probs | ||
|
||
# top_a | ||
|
||
def top_a(logits, min_p_pow=2.0, min_p_ratio=0.02): | ||
probs = F.softmax(logits, dim=-1) | ||
limit = torch.pow(torch.max(probs), min_p_pow) * min_p_ratio | ||
logits[probs < limit] = float('-inf') | ||
logits[probs >= limit] = 1 | ||
return logits |
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