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
16 changes: 4 additions & 12 deletions mlx_lm/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,14 +1202,8 @@ def filter(self, keep: List[int]):
for c in self.prompt_cache:
c.filter(keep)
self.tokens = [self.tokens[idx] for idx in keep]
if any(self.samplers):
self.samplers = [self.samplers[idx] for idx in keep]
else:
self.samplers = [None] * len(keep)
if any(self.logits_processors):
self.logits_processors = [self.logits_processors[idx] for idx in keep]
else:
self.logits_processors = [[]] * len(keep)
self.samplers = [self.samplers[idx] for idx in keep]
self.logits_processors = [self.logits_processors[idx] for idx in keep]
self.max_tokens = [self.max_tokens[idx] for idx in keep]
self.stop_matchers = [self.stop_matchers[idx] for idx in keep]

Expand Down Expand Up @@ -1479,10 +1473,8 @@ def filter(self, keep: List[int]):
for c in self.prompt_cache:
c.filter(keep)
self.tokens = [self.tokens[idx] for idx in keep]
if any(self.samplers):
self.samplers = [self.samplers[idx] for idx in keep]
if any(self.logits_processors):
self.logits_processors = [self.logits_processors[idx] for idx in keep]
self.samplers = [self.samplers[idx] for idx in keep]
self.logits_processors = [self.logits_processors[idx] for idx in keep]
self.max_tokens = [self.max_tokens[idx] for idx in keep]
self.stop_matchers = [self.stop_matchers[idx] for idx in keep]

Expand Down
39 changes: 28 additions & 11 deletions tests/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@
from mlx_lm.utils import load


class TestGenerateArgs(unittest.TestCase):

def test_prefill_step_size_default(self):
args = setup_arg_parser().parse_args([])
self.assertEqual(args.prefill_step_size, DEFAULT_PREFILL_STEP_SIZE)

def test_prefill_step_size_override(self):
args = setup_arg_parser().parse_args(["--prefill-step-size", "512"])
self.assertEqual(args.prefill_step_size, 512)


class TestGenerate(unittest.TestCase):

@classmethod
Expand Down Expand Up @@ -434,6 +423,34 @@ def processor(tokens, logits):
self.assertTrue(hasattr(seen[0], "shape"))
self.assertEqual(seen[0].tolist(), prompt)

def test_batch_processor_survive_a_request_without_it(self):
prompt = self.tokenizer.encode("hello")

def run(batch_gen, uid):
n = 0
while True:
for r in batch_gen.next_generated():
if r.uid == uid:
n += 1
if r.finish_reason is not None:
return n

batch_gen = BatchGenerator(self.model, max_tokens=3)
(uid,) = batch_gen.insert([prompt])
run(batch_gen, uid)

calls = []

def processor(tokens, logits):
calls.append(len(tokens))
return logits

(uid,) = batch_gen.insert([prompt], logits_processors=[[processor]])
n_tokens = run(batch_gen, uid)
# One call per generated token, plus the step that sampled the token
# after the last one returned
self.assertEqual(len(calls), n_tokens + 1)

def test_batch_generate_function_with_logits_processors(self):
"""Test that batch_generate function with logits_processors produces correct results."""
logit_bias = {0: 2000.0, 1: -2000.0}
Expand Down
Loading