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
Optimized friendly-error HTTP 401/403 patterns (#1594)
John Coplin333f2b8e4174
diff --git a/changelog.d/0.75.1/1594.fixed.md b/changelog.d/0.75.1/1594.fixed.mdnew file mode 100644index 00000000..2a831389--- /dev/null+++ b/changelog.d/0.75.1/1594.fixed.md@@ -0,0 +1 @@+- **The pattern used to match HTTP 401/403 errors in the `format_friendly_error` function in `errors.py` no longer backtracks quadratically if handed an exception which contains the characters "HTTP" followed by whitespace with no status code (#1367 by @johncoplin in #1594).** The old pattern had two places in which whitespace could go if the separator was missing from an exception, meaning the function would have to try every possible combination of whitespace before being able to print the message for unmatched errors. This pattern has now been replaced with one that only has two spots for whitespace if the separator exists, preventing quadratic backtracking and meaning error-messages print faster overall for these kinds of exceptions.\ No newline at end of filediff --git a/src/soup_cli/utils/errors.py b/src/soup_cli/utils/errors.pyindex 6d66cb2f..5ff50d58 100644--- a/src/soup_cli/utils/errors.py+++ b/src/soup_cli/utils/errors.py@@ -261,10 +261,10 @@ _AUTH_HINTS = {# size, path component, or hash slice cannot match._AUTH_STATUS_TEXT = ((re.compile(r"401\s+(?:Unauthorized|Client Error)"), 401),- (re.compile(r"HTTP\s+(?:Error\s+)?[:=]?\s*401\b"), 401),+ (re.compile(r"HTTP\s+(?:Error\s+)?(?:[:=]\s*)?401\b"), 401),(re.compile(r"Error code:\s*401\b"), 401),(re.compile(r"403\s+(?:Forbidden|Client Error)"), 403),- (re.compile(r"HTTP\s+(?:Error\s+)?[:=]?\s*403\b"), 403),+ (re.compile(r"HTTP\s+(?:Error\s+)?(?:[:=]\s*)?403\b"), 403),(re.compile(r"Error code:\s*403\b"), 403),)diff --git a/tests/test_issue1367_quadratic_backtracking.py b/tests/test_issue1367_quadratic_backtracking.pynew file mode 100644index 00000000..aa3a5660--- /dev/null+++ b/tests/test_issue1367_quadratic_backtracking.py@@ -0,0 +1,67 @@+"""#1367 — friendly-error 401/403 patterns no longer backtrack quadratically++If an HTTP error is followed by whitespace and has no separator or status code, the old pattern for+401 and 403 errors would try every possible way of splitting the whitespace between two places to+make the error match. Checking this old pattern had a time complexity of O(n²), causing quadratic+backtracking the more whitespace an error like this had.++By changing the pattern so the second whitespace area is only considered if the separator is+present, the time complexity for checking the pattern becomes O(n), removing the quadratic+backtracking.++The tests below check that the current pattern doesn't contain quadratic backtracking,+and that the current pattern matches to exactly the same phrasings as the old one (ensuring that+it is only a version of the pattern which is quicker to check and not a different pattern entirely).+"""+import re+import time++import pytest++from soup_cli.utils.errors import _AUTH_STATUS_TEXT, format_friendly_error++phrasings = ["HTTP 401",+ "HTTP 401",+ "HTTP Error 401",+ "HTTP Error 401: Unauthorized",+ "HTTP =401",+ "HTTP = 401",+ "HTTP: 401",+ "HTTP Error: 401",+ "HTTP 4010",+ "HTTP401",+ "HTTP Error = 401",+ "HTTP\t401",+ "HTTP Error\n401",+ "Got HTTP 401 from server",+ "HTTP : 401"+ ]++_AUTH_STATUS_TEXT_OLD = (+ (re.compile(r"HTTP\s+(?:Error\s+)?[:=]?\s*401\b"), 401),+ (re.compile(r"HTTP\s+(?:Error\s+)?[:=]?\s*403\b"), 403),+)+++def test_quadratic_backtracking_is_not_present() -> None:+ test_input = ValueError("HTTP" + " " * 50_000)+ start_time = time.perf_counter()+ format_friendly_error(test_input)+ end_time = time.perf_counter()+ total_time = end_time - start_time+ time_bound = 0.5+ assert total_time < time_bound, "Quadratic backtracking present in one or more error messages."++[email protected]("error_code", ["401", "403"])[email protected]("test_input", phrasings)+def test_new_pattern_agrees_with_old_pattern(error_code: str, test_input: str) -> None:+ index_old = 0+ index_current = 1+ if error_code == "403":+ test_input = test_input.replace("401", error_code)+ index_old = 1+ index_current = 4+ old_output = _AUTH_STATUS_TEXT_OLD[index_old][0].search(test_input)+ current_output = _AUTH_STATUS_TEXT[index_current][0].search(test_input)+ assert (old_output is None) == (current_output is None), "Error message patterns do not match."