diff --git a/mlx_lm/generate.py b/mlx_lm/generate.py index c4c0e8e6a..4401edfcf 100644 --- a/mlx_lm/generate.py +++ b/mlx_lm/generate.py @@ -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] @@ -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] diff --git a/tests/test_generate.py b/tests/test_generate.py index 20c064353..2320c799b 100644 --- a/tests/test_generate.py +++ b/tests/test_generate.py @@ -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 @@ -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}