Skip to content

The best ChatGPT that $100 can buy.

Python

Fix generate() crash when top_k=0 (#467)

Harsh Gupta2e1772381752
diff --git a/nanochat/gpt.py b/nanochat/gpt.py
index d23a516..208acd1 100644
--- a/nanochat/gpt.py
+++ b/nanochat/gpt.py
@@ -440,7 +440,7 @@ class GPT(nn.Module):
for _ in range(max_tokens):
logits = self.forward(ids) # (B, T, vocab_size)
logits = logits[:, -1, :] # (B, vocab_size)
- if top_k is not None:
+ if top_k is not None and top_k > 0:
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
logits[logits < v[:, [-1]]] = -float('Inf')
if temperature > 0: