mirror of
https://github.com/ml-explore/mlx-examples.git
synced 2025-06-24 09:21:18 +08:00
Faster sampling with mx.compile
(#937)
* faster sampling with compile * fix test
This commit is contained in:
parent
95840f32e2
commit
9b83004631
@ -1,6 +1,11 @@
|
|||||||
|
# Copyright © 2023-2024 Apple Inc.
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
import mlx.core as mx
|
import mlx.core as mx
|
||||||
|
|
||||||
|
|
||||||
|
@partial(mx.compile, inputs=mx.random.state, outputs=mx.random.state)
|
||||||
def top_p_sampling(logits: mx.array, top_p: float, temperature: float) -> mx.array:
|
def top_p_sampling(logits: mx.array, top_p: float, temperature: float) -> mx.array:
|
||||||
"""
|
"""
|
||||||
Apply top-p (nucleus) sampling to logits.
|
Apply top-p (nucleus) sampling to logits.
|
||||||
@ -13,7 +18,7 @@ def top_p_sampling(logits: mx.array, top_p: float, temperature: float) -> mx.arr
|
|||||||
token selected based on the top-p criterion.
|
token selected based on the top-p criterion.
|
||||||
"""
|
"""
|
||||||
# referenced implementation from https://github.com/huggingface/transformers/blob/main/src/transformers/generation/logits_process.py#L449-L460
|
# referenced implementation from https://github.com/huggingface/transformers/blob/main/src/transformers/generation/logits_process.py#L449-L460
|
||||||
probs = mx.softmax(logits / temperature, axis=-1)
|
probs = mx.softmax(logits * (1 / temperature), axis=-1)
|
||||||
|
|
||||||
# sort probs in ascending order
|
# sort probs in ascending order
|
||||||
sorted_indices = mx.argsort(probs, axis=-1)
|
sorted_indices = mx.argsort(probs, axis=-1)
|
||||||
@ -25,10 +30,15 @@ def top_p_sampling(logits: mx.array, top_p: float, temperature: float) -> mx.arr
|
|||||||
top_probs = mx.where(
|
top_probs = mx.where(
|
||||||
cumulative_probs > 1 - top_p,
|
cumulative_probs > 1 - top_p,
|
||||||
sorted_probs,
|
sorted_probs,
|
||||||
mx.zeros_like(sorted_probs),
|
0,
|
||||||
)
|
)
|
||||||
|
|
||||||
sorted_token = mx.random.categorical(mx.log(top_probs))
|
sorted_token = mx.random.categorical(mx.log(top_probs))
|
||||||
token = sorted_indices.squeeze(0)[sorted_token]
|
token = sorted_indices.squeeze(0)[sorted_token]
|
||||||
|
|
||||||
return token
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
@partial(mx.compile, inputs=mx.random.state, outputs=mx.random.state)
|
||||||
|
def categorical_sampling(logits, temp):
|
||||||
|
return mx.random.categorical(logits * (1 / temp))
|
||||||
|
@ -20,7 +20,7 @@ from transformers import PreTrainedTokenizer
|
|||||||
|
|
||||||
# Local imports
|
# Local imports
|
||||||
from .models.base import KVCache
|
from .models.base import KVCache
|
||||||
from .sample_utils import top_p_sampling
|
from .sample_utils import categorical_sampling, top_p_sampling
|
||||||
from .tokenizer_utils import TokenizerWrapper, load_tokenizer
|
from .tokenizer_utils import TokenizerWrapper, load_tokenizer
|
||||||
from .tuner.utils import apply_lora_layers
|
from .tuner.utils import apply_lora_layers
|
||||||
from .tuner.utils import dequantize as dequantize_model
|
from .tuner.utils import dequantize as dequantize_model
|
||||||
@ -169,7 +169,7 @@ def generate_step(
|
|||||||
if top_p > 0 and top_p < 1.0:
|
if top_p > 0 and top_p < 1.0:
|
||||||
token = top_p_sampling(logits, top_p, temp)
|
token = top_p_sampling(logits, top_p, temp)
|
||||||
else:
|
else:
|
||||||
token = mx.random.categorical(logits * (1 / temp))
|
token = categorical_sampling(logits, temp)
|
||||||
|
|
||||||
return token, logprobs
|
return token, logprobs
|
||||||
|
|
||||||
|
@ -1,38 +1,32 @@
|
|||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
import mlx.core as mx
|
import mlx.core as mx
|
||||||
from mlx_lm.sample_utils import top_p_sampling
|
from mlx_lm.sample_utils import top_p_sampling
|
||||||
|
|
||||||
|
|
||||||
class TestSamplingUtils(unittest.TestCase):
|
class TestSamplingUtils(unittest.TestCase):
|
||||||
@patch("mlx.core.random.categorical")
|
def test_top_p_sampling(self):
|
||||||
def test_top_p_sampling(self, mock_categorical):
|
probs = mx.array([0.9, 0.0, 0.0, 0.1])[None]
|
||||||
logits = mx.array([[1.0, 2.0, 3.0, 4.0]])
|
logits = mx.log(probs)
|
||||||
top_p = 0.3
|
|
||||||
temperature = 1.0
|
temperature = 1.0
|
||||||
expected_token = mx.array([3])
|
|
||||||
mock_categorical.return_value = expected_token
|
|
||||||
|
|
||||||
token = top_p_sampling(logits, top_p, temperature)
|
token = top_p_sampling(logits, 0.3, temperature).item()
|
||||||
expected_top_probs = mx.array([[0.0, 0.0, 0.0, 0.643914]])
|
self.assertEqual(token, 0)
|
||||||
self.assertTrue(mx.allclose(token, expected_token))
|
|
||||||
args, _ = mock_categorical.call_args
|
|
||||||
self.assertTrue(args[0].shape == expected_top_probs.shape)
|
|
||||||
self.assertTrue(mx.allclose(args[0], mx.log(expected_top_probs)))
|
|
||||||
|
|
||||||
logits = mx.array([[1.0, 2.0, 3.0, 4.0]])
|
token = top_p_sampling(logits, 0.95, temperature).item()
|
||||||
top_p = 0.9
|
self.assertTrue(token in (0, 3))
|
||||||
temperature = 1.0
|
|
||||||
expected_token = mx.array([3])
|
|
||||||
mock_categorical.return_value = expected_token
|
|
||||||
|
|
||||||
token = top_p_sampling(logits, top_p, temperature)
|
probs = mx.array([0.0, 0.5, 0.4, 0.1])[None]
|
||||||
expected_top_probs = mx.array([[0.0, 0.0871443, 0.236883, 0.643914]])
|
logits = mx.log(probs)
|
||||||
self.assertTrue(mx.allclose(token, expected_token))
|
|
||||||
args, _ = mock_categorical.call_args
|
token = top_p_sampling(logits, 0.4, temperature).item()
|
||||||
self.assertTrue(args[0].shape == expected_top_probs.shape)
|
self.assertEqual(token, 1)
|
||||||
self.assertTrue(mx.allclose(args[0], mx.log(expected_top_probs)))
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user