mlx-examples/llms/tests/test_sample_utils.py

99 lines
3.6 KiB
Python
Raw Normal View History

import unittest
import mlx.core as mx
2025-03-09 03:08:55 +08:00
from mlx_lm.sample_utils import apply_min_p, apply_top_k, apply_top_p
class TestSampleUtils(unittest.TestCase):
2025-03-09 03:08:55 +08:00
def test_apply_top_p(self):
probs = mx.array([0.9, 0.0, 0.0, 0.1])[None]
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_top_p(logits, 0.3)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
2025-03-08 21:55:49 +08:00
self.assertEqual(actual_probs.tolist(), [1.0, 0.0, 0.0, 0.0])
2025-03-09 03:08:55 +08:00
new_logits = apply_top_p(logits, 0.95)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
2025-03-08 21:55:49 +08:00
self.assertEqual(probs.squeeze().tolist(), actual_probs.tolist())
probs = mx.array([0.0, 0.5, 0.4, 0.1])[None]
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_top_p(logits, 0.4)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
2025-03-08 21:55:49 +08:00
self.assertEqual(actual_probs.tolist(), [0.0, 1.0, 0.0, 0.0])
2025-03-09 03:08:55 +08:00
new_logits = apply_top_p(logits, 0.6)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
2025-03-08 21:55:49 +08:00
self.assertEqual(
[round(p, 4) for p in actual_probs.tolist()], [0.0, 0.5556, 0.4444, 0.0]
)
2025-03-09 03:08:55 +08:00
new_logits = apply_top_p(logits, 0.95)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
2025-03-08 21:55:49 +08:00
actual_rounded = [round(p, 4) for p in actual_probs.tolist()]
expected_rounded = [0.0, 0.5, 0.4, 0.1]
self.assertEqual(actual_rounded, expected_rounded)
self.assertAlmostEqual(sum(actual_probs.tolist()), 1.0)
# Batch mode works
2025-03-08 21:55:49 +08:00
probs = mx.array([[0.9, 0.0, 0.0, 0.1], [0.0, 0.8, 0.1, 0.1]])
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_top_p(logits, 0.5)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits, axis=-1)
2025-03-08 21:55:49 +08:00
self.assertEqual(
actual_probs.tolist(), [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]]
)
2025-03-09 03:08:55 +08:00
def test_apply_min_p(self):
probs = mx.array([0.9, 0.0, 0.0, 0.1])[None]
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_min_p(logits, 0.8)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
self.assertEqual(actual_probs.tolist(), [1.0, 0.0, 0.0, 0.0])
probs = mx.array([0.9, 0.0, 0.0, 0.1])[None]
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_min_p(logits, 0.05)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
self.assertEqual(actual_probs.tolist(), mx.squeeze(probs).tolist())
# Batch mode works
probs = mx.array([[0.9, 0.0, 0.0, 0.1], [0.0, 0.8, 0.0, 0.1]])
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_min_p(logits, 0.7)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits, axis=-1)
self.assertEqual(
actual_probs.tolist(), [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]]
)
2025-03-09 03:08:55 +08:00
def test_apply_top_k(self):
probs = mx.array([0.9, 0.0, 0.0, 0.1])[None]
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_top_k(logits, 1)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
self.assertEqual(actual_probs.tolist(), [1.0, 0.0, 0.0, 0.0])
2025-03-08 23:12:28 +08:00
probs = mx.array([0.6, 0.0, 0.1, 0.3])[None]
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_top_k(logits, 2)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits.squeeze())
self.assertEqual(
[round(p, 4) for p in actual_probs.tolist()], [0.6667, 0.0, 0.0, 0.3333]
)
# Batch mode works
probs = mx.array([[0.9, 0.0, 0.0, 0.1], [0.0, 0.8, 0.0, 0.1]])
logits = mx.log(probs)
2025-03-09 03:08:55 +08:00
new_logits = apply_top_k(logits, 1)
2025-03-08 23:12:28 +08:00
actual_probs = mx.softmax(new_logits, axis=-1)
self.assertEqual(
actual_probs.tolist(), [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]]
)
if __name__ == "__main__":
unittest.main()