Skip to content
Open
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
5 changes: 4 additions & 1 deletion mlx_lm/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ def _rstrip_until(s, untils):
l = len(s)
f = [s.find(u) for u in untils]
f = [l if x < 0 else x for x in f]
return s[: min(f)]
# A task may legitimately specify no stop sequences -- ifeval sets
# `until: []` because the completion is meant to run to max_gen_toks --
# and there is then nothing to truncate at.
return s[: min(f, default=l)]


def _lstrip(s, pattern):
Expand Down
22 changes: 21 additions & 1 deletion tests/test_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import mlx.core as mx

from mlx_lm.evaluate import MLXLM
from mlx_lm.evaluate import MLXLM, _rstrip_until


class TestMLXLM(unittest.TestCase):
Expand Down Expand Up @@ -55,5 +55,25 @@ def mock_score_fn(batch):
self.assertEqual(len(call_args_list[2][0][0]), 1) # Third batch: 1 item


class TestRstripUntil(unittest.TestCase):
def test_truncates_at_first_stop_sequence(self):
self.assertEqual(_rstrip_until("abc<eos>def", ["<eos>"]), "abc")

def test_truncates_at_the_earliest_of_several(self):
self.assertEqual(_rstrip_until("abc\n\nQ:", ["Q:", "\n\n"]), "abc")

def test_returns_the_whole_string_when_nothing_matches(self):
self.assertEqual(_rstrip_until("abc", ["<eos>"]), "abc")

def test_no_stop_sequences(self):
# Tasks may legitimately specify none: lm-eval's ifeval (and every one
# of its multilingual variants) sets `until: []` so the completion runs
# to max_gen_toks. There is then nothing to truncate at.
self.assertEqual(_rstrip_until("abc", []), "abc")

def test_no_stop_sequences_on_empty_string(self):
self.assertEqual(_rstrip_until("", []), "")


if __name__ == "__main__":
unittest.main()
Loading