Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
fix(eval): make contains scoring linear again (#1528) (#1540)
Urooj Fatima Mukhtared50391a1711
diff --git a/changelog.d/0.75.1/1540.fixed.md b/changelog.d/0.75.1/1540.fixed.mdnew file mode 100644index 00000000..7391b05b--- /dev/null+++ b/changelog.d/0.75.1/1540.fixed.md@@ -0,0 +1 @@+- `soup eval`'s custom-task `contains` scorer is linear again in the length of the output and of `expected`. The whole-token match from #1490 restarted its search at every position, so a 400,000-character output against a 200,000-character `expected` took 68 s; it now takes milliseconds. Both sides are case-folded with `str.lower()`, as in v0.75.1, so `istanbul` no longer matches `İstanbul` and `s` no longer matches `ſ`; no other score changes (#1528 by @UroojFatima-052 in #1540).\ No newline at end of filediff --git a/docs/evaluation.md b/docs/evaluation.mdindex 8187dc83..91485d38 100644--- a/docs/evaluation.md+++ b/docs/evaluation.md@@ -827,7 +827,7 @@ How the string scorers read an output:| `scoring` | Scores | `expected: "4"` vs `The answer is 4.` | vs `14` ||---|---|---|---|| `answer` | The final answer each side states, read with the parser GRPO's `accuracy` reward uses (`#### 4`, `\boxed{4}`, `The answer is 4.`); numbers compare by value | match | no match |-| `contains` | `expected` as a whole alphanumeric-bounded token, case-insensitive | match | no match |+| `contains` | `expected` as a whole alphanumeric-bounded token, case-insensitive via `str.lower()` on both sides (so `istanbul` does not match `İstanbul`, nor `s` match `ſ`) | match | no match || `exact` | The whole stripped output, case-insensitive | no match | no match |`answer` refuses, at load time, an `expected` that states no single answer (a multi-linediff --git a/src/soup_cli/eval/custom.py b/src/soup_cli/eval/custom.pyindex 78a891bf..f01ff070 100644--- a/src/soup_cli/eval/custom.py+++ b/src/soup_cli/eval/custom.py@@ -163,15 +163,20 @@ def score_exact(output: str, expected: str) -> bool:def score_contains(output: str, expected: str) -> bool:"""True when ``expected`` appears in ``output`` as a whole alnum-bounded token.- Case-insensitive. ``14`` does not contain ``4``, and ``#### 420`` does not contain- ``42``. A token match cannot read negation (``not 42, so 41`` still contains ``42``);- use ``scoring: "answer"`` to compare the answer a model states.+ Case-insensitive via ``str.lower()`` on both sides. ``14`` does not contain ``4``,+ and ``#### 420`` does not contain ``42``. A token match cannot read negation+ (``not 42, so 41`` still contains ``42``); use ``scoring: "answer"`` to compare+ the answer a model states."""- needle = expected.strip()+ needle = expected.strip().lower()if not needle:return True- pattern = r"(?<![A-Za-z0-9])" + re.escape(needle) + r"(?![A-Za-z0-9])"- return re.search(pattern, output, re.IGNORECASE) is not None+ pattern = (+ re.escape(needle)+ + rf"(?<{{{len(needle)}}})" # the character before the match+ + r"(?![A-Za-z0-9])" # the character after it+ )+ return re.search(pattern, output.lower()) is not Nonedef score_answer(output: str, expected: str) -> bool:diff --git a/tests/test_issue1528_contains_is_linear.py b/tests/test_issue1528_contains_is_linear.pynew file mode 100644index 00000000..af452ca7--- /dev/null+++ b/tests/test_issue1528_contains_is_linear.py@@ -0,0 +1,86 @@+r"""#1528: the custom-eval ``contains`` scorer stays linear in output and expected length.++#1490 made ``contains`` match ``expected`` only as a whole token. Its pattern started with a+lookbehind and ran under ``re.IGNORECASE``, so ``re`` tried it at every position: quadratic in+``len(output) * len(expected)``. The time at 8N is compared with the time at N, as in+``test_issue1346_rewrites_are_linear.py``: a linear scan grows about 8x, a quadratic one about 64x.+"""++from __future__ import annotations++import random+import re+import time++import pytest++from soup_cli.eval.custom import score_contains++_SCALE_N = 2_500 # 5,000 output characters at N, 40,000 at 8N+_SCALE_LIMIT = 24.0 # the same 3x slack over linear as the #1346 growth test++SHAPES = {+ "word-starts-never-match": lambda k: ("a " * k, "a " * (k // 2) + "b"),+ "word-starts-match-at-end": lambda k: ("a " * k + "b", "a " * (k // 2) + "b"),+ "one-letter-run": lambda k: ("a" * (2 * k), "a" * k),+ "hyphen-pairs": lambda k: ("a-" * k, "-a" * (k // 2)),+}+++def _best_time(output: str, expected: str, repeats: int) -> float:+ best = float("inf")+ for _ in range(repeats):+ start = time.perf_counter()+ score_contains(output, expected)+ best = min(best, time.perf_counter() - start)+ return best++[email protected]("build", list(SHAPES.values()), ids=list(SHAPES))+def test_contains_grows_linearly(build):+ small, large = build(_SCALE_N), build(8 * _SCALE_N)+ t_small = max(_best_time(*small, repeats=5), 1e-7)+ t_large = float("inf")+ for _ in range(5): # stop as soon as the larger input is shown to be fast enough+ t_large = min(t_large, _best_time(*large, repeats=1))+ if t_large / t_small < _SCALE_LIMIT:+ break+ assert t_large / t_small < _SCALE_LIMIT, (t_small, t_large)+++def _contains_before_1528(output: str, expected: str) -> bool:+ """The #1490 scorer, kept as the reference the linear rewrite must agree with."""+ needle = expected.strip()+ if not needle:+ return True+ pattern = r"(?<![A-Za-z0-9])" + re.escape(needle) + r"(?![A-Za-z0-9])"+ return re.search(pattern, output, re.IGNORECASE) is not None+++_ALPHABET = "abAB01 .-#+(|"+++def test_contains_agrees_with_the_previous_regex_on_short_strings():+ rng = random.Random(1528)+ for _ in range(20_000):+ output = "".join(rng.choices(_ALPHABET, k=rng.randint(0, 12)))+ expected = "".join(rng.choices(_ALPHABET, k=rng.randint(0, 4)))+ assert score_contains(output, expected) == _contains_before_1528(output, expected), (+ output,+ expected,+ )+++ "output, expected",+ [("Welcome to İstanbul", "istanbul"), ("ſ", "s")],+)+def test_contains_folds_case_with_str_lower(output, expected):+ """``str.lower()`` and ``re.IGNORECASE`` disagree here; ``False`` matches v0.75.1."""+ assert score_contains(output, expected) is False+++def test_a_gold_token_past_60000_characters_still_scores():+ output = "x " * 30_000 + "42" + " " * 14+ assert len(output) == 60_016 and output.index("42") == 60_000+ assert score_contains(output, "42") is True