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
def top_k_top_p_filter(logits, top_k: int = 0, top_p: float = 0.0): | |
if top_k > 0: | |
filter = torch.topk(logits, min(top_k, logits.size(-1)))[0] | |
logits[logits < filter[:, [-1]]] = float('-inf') | |
if top_p > 0.0: | |
sorted_logits, sorted_indices = torch.sort(logits, descending=True) | |
cumulative_probs = torch.cumsum( | |
F.softmax(sorted_logits, dim=-1), dim=-1) | |
filter = cumulative_probs > top_p | |
filter[..., 1:] = filter[..., :-1].clone() |
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
import torch.nn as nn | |
class MultiInputSequential(nn.Sequential): | |
def forward(self, *inputs): | |
for module in self._modules.values(): | |
if type(inputs) == tuple: | |
inputs = module(*inputs) | |
else: | |
inputs = module(inputs) | |
return inputs |