2024-03-22 03:18:23 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import mlx.core as mx
|
|
|
|
from mlx_lm.sample_utils import top_p_sampling
|
|
|
|
|
|
|
|
|
2024-03-26 22:56:01 +08:00
|
|
|
class TestSamplingUtils(unittest.TestCase):
|
2024-08-16 02:29:09 +08:00
|
|
|
def test_top_p_sampling(self):
|
|
|
|
probs = mx.array([0.9, 0.0, 0.0, 0.1])[None]
|
|
|
|
logits = mx.log(probs)
|
2024-03-22 03:18:23 +08:00
|
|
|
temperature = 1.0
|
2024-08-16 02:29:09 +08:00
|
|
|
|
|
|
|
token = top_p_sampling(logits, 0.3, temperature).item()
|
|
|
|
self.assertEqual(token, 0)
|
|
|
|
|
|
|
|
token = top_p_sampling(logits, 0.95, temperature).item()
|
|
|
|
self.assertTrue(token in (0, 3))
|
|
|
|
|
|
|
|
probs = mx.array([0.0, 0.5, 0.4, 0.1])[None]
|
|
|
|
logits = mx.log(probs)
|
|
|
|
|
|
|
|
token = top_p_sampling(logits, 0.4, temperature).item()
|
|
|
|
self.assertEqual(token, 1)
|
|
|
|
|
|
|
|
token = top_p_sampling(logits, 0.6, temperature).item()
|
|
|
|
self.assertTrue(token in (1, 2))
|
|
|
|
|
|
|
|
token = top_p_sampling(logits, 0.95, temperature).item()
|
|
|
|
self.assertTrue(token in (1, 2, 3))
|
2024-03-22 03:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|