MCPcopy
hub / github.com/huggingface/transformers / logits_to_probs

Function logits_to_probs

benchmark/benches/llama.py:147–155  ·  view source on GitHub ↗
(logits, temperature: float = 1.0, top_k: int | None = None)

Source from the content-addressed store, hash-verified

145 return torch.argmax(probs_sort / q, dim=-1, keepdim=True).to(dtype=torch.int)
146
147 def logits_to_probs(logits, temperature: float = 1.0, top_k: int | None = None):
148 logits = logits / max(temperature, 1e-5)
149
150 if top_k is not None:
151 v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
152 pivot = v.select(-1, -1).unsqueeze(-1)
153 logits = torch.where(logits < pivot, -float("Inf"), logits)
154 probs = torch.nn.functional.softmax(logits, dim=-1)
155 return probs
156
157 def sample(logits, temperature: float = 1.0, top_k: int | None = None):
158 probs = logits_to_probs(logits[0, -1], temperature, top_k)

Callers 1

sampleFunction · 0.85

Calls 2

sizeMethod · 0.45
unsqueezeMethod · 0.45

Tested by

no test coverage detected