Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mlx_lm/sample_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ def apply_xtc(
)

probs = mx.softmax(logits, -1)
mask = probs > mx.where(probs > xtc_threshold, probs, mx.inf).min()
mask = probs > mx.where(probs > xtc_threshold, probs, mx.inf).min(
axis=-1, keepdims=True
)
if xtc_special_tokens:
mask[..., xtc_special_tokens] = False

Expand Down
7 changes: 7 additions & 0 deletions tests/test_sample_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ def test_apply_xtc(self):
new_probs = mx.softmax(apply_xtc(mx.log(probs), 0, 0.1, [0]), -1)
self.assertTrue(mx.allclose(new_probs, probs))

# Test that the threshold is computed per row in a batch
probs = mx.array([[0.6, 0.25, 0.1, 0.05], [0.9, 0.05, 0.03, 0.02]])
batched = mx.softmax(apply_xtc(mx.log(probs), 1, 0.2, []), -1)
for i in range(probs.shape[0]):
alone = mx.softmax(apply_xtc(mx.log(probs[i : i + 1]), 1, 0.2, []), -1)
self.assertTrue(mx.allclose(batched[i : i + 1], alone))

def test_presence_penalty(self):
from mlx_lm.sample_utils import make_presence_penalty

Expand Down
Loading