Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(data): soup data validate counts the rows the loader skipped as invalid rows (#1908)

ndablizovd2581306918f
diff --git a/changelog.d/0.76.0/1908.fixed.md b/changelog.d/0.76.0/1908.fixed.md
new file mode 100644
index 00000000..f6b9be08
--- /dev/null
+++ b/changelog.d/0.76.0/1908.fixed.md
@@ -0,0 +1 @@
+- `soup data validate` counts every row the loader skipped (not a JSON object, or not valid JSON) as an invalid row in its `N/M rows valid` line, in `--min-valid-fraction` and in the exit code: a file of only junk rows exits 2 ("no usable rows remain") with `--format` and 3 without one, and `[good, 42]` is `1/2 rows valid` and fails `--min-valid-fraction 1.0`. A file that is not empty but has no object row says how many rows were skipped instead of "Dataset is empty" (#1887 by @ndablizov in #1908)
diff --git a/docs/data.md b/docs/data.md
index 75e1c280..62d910ea 100644
--- a/docs/data.md
+++ b/docs/data.md
@@ -669,8 +669,11 @@ where a key is absent (#1807):
JSONL dataset rows must be objects; JSON dataset files must contain an array of
objects. Non-object rows are skipped with a warning naming the physical JSONL
-line or JSON array row. `soup data validate` also lists these warnings as issues;
-its row statistics describe the retained objects. `soup data review` applies the
+line or JSON array row. `soup data validate` lists these warnings as issues (the
+first three, then `... and N more`), and every skipped row, whether not an object
+or not valid JSON, counts as an invalid row in its `N/M rows valid` line, in
+`--min-valid-fraction` and in the exit code; its other statistics describe the
+retained objects. `soup data review` applies the
same object check before displaying a pair. Rejected row contents are not printed. `soup data canary insert` retains its first-row refusal and preserves later rows when rewriting the file.
Soup supports these formats (auto-detected). Files can be JSONL, JSON, CSV, Parquet, or TXT.
@@ -1051,8 +1054,11 @@ soup data clean ./data/train.jsonl -o ./data/clean.jsonl --dry-run
`soup data validate` exits with code `0` when at least one row is usable and the
optional minimum valid fraction is met. It exits with code `3` for input errors,
such as a missing file or an undetectable format, and code `2` when a non-empty
-dataset has no usable rows or falls below `--min-valid-fraction`. A partially valid
-dataset still exits with code `0` when no minimum is specified.
+dataset has no usable rows or falls below `--min-valid-fraction`. A file whose
+every row was skipped is not empty: it exits with code `2` ("no usable rows
+remain") when `--format` names the format, and `3` without one, since nothing is
+left to detect a format from. A partially valid dataset still exits with code `0`
+when no minimum is specified.
Training loads a dataset through the same converters, and a row they reject is
dropped rather than stopping the run, so one bad line does not abort a load. The
@@ -1060,7 +1066,9 @@ drop is reported: `soup train`, and every other command that loads a dataset,
prints `Warning: N of M rows dropped` with the first row's index and the
converter's reason. For a local file it also prints the `soup data validate`
command that lists them all. The
-count agrees with `soup data validate` for the same file. Before #1181 the rows
+count agrees with `soup data validate` for the same file, except that the training
+drop report counts retained rows only, so the rows the loader skipped before
+conversion appear in `soup data validate` alone. Before #1181 the rows
were dropped without a word. If a load ends with zero training rows, `soup train`
stops with exit 1 before loading the model, `--dry-run` included, naming the format
the rows were read as and the first row's drop reason (#1217).
diff --git a/src/soup_cli/commands/data.py b/src/soup_cli/commands/data.py
index 24d599f1..4508cdb3 100644
--- a/src/soup_cli/commands/data.py
+++ b/src/soup_cli/commands/data.py
@@ -111,6 +111,11 @@ def validate(
load_issues: list[str] = []
data = load_raw_data(file_path, issues=load_issues)
+ # #1887: every row the loader skipped (not an object, or not JSON) is an
+ # invalid row for the N/M line, for --min-valid-fraction and for the exit
+ # code. The statistics below still describe the retained objects.
+ skipped = len(load_issues)
+ none_remain = f"no object rows remain: {skipped} row(s) skipped (listed above)"
# Auto-detect format if not specified
if fmt == "auto":
@@ -120,10 +125,16 @@ def validate(
fmt = detect_format(data)
console.print(f"[dim]Auto-detected format: {for_terminal(fmt)}[/]")
except ValueError as exc:
- console.print(f"[red]{for_terminal(str(exc))}[/]")
+ message = str(exc)
+ if not data and skipped:
+ message = f"{none_remain}; cannot detect a format. Pass --format to validate."
+ console.print(f"[red]{for_terminal(message)}[/]")
raise typer.Exit(EXIT_USAGE_ERROR)
result = validate_and_stats(data, expected_format=fmt)
+ if not data and skipped:
+ result["issues"] = [none_remain if i == "Dataset is empty" else i
+ for i in result["issues"]]
load_issue_samples = load_issues[:_MAX_REASON_SAMPLES]
if len(load_issues) > _MAX_REASON_SAMPLES:
load_issue_samples.append(f"... and {len(load_issues) - _MAX_REASON_SAMPLES} more")
@@ -137,7 +148,7 @@ def validate(
console.print("[bold green]Dataset is valid![/]")
valid = result["valid_rows"]
- total = result["total"]
+ total = result["total"] + skipped
console.print(f"\n[green]{valid}/{total} rows valid for {for_terminal(fmt)} format[/]")
if total > 0 and valid == 0:
diff --git a/src/soup_cli/data/loader.py b/src/soup_cli/data/loader.py
index 0dac7163..c0597ab2 100644
--- a/src/soup_cli/data/loader.py
+++ b/src/soup_cli/data/loader.py
@@ -126,7 +126,12 @@ def _load_jsonl(
continue
data.append(row)
except json.JSONDecodeError as e:
- console.print(f"[yellow]Warning: invalid JSON on line {i + 1}: {e}[/]")
+ issue = f"invalid JSON on line {i + 1}: {e}"
+ console.print(f"[yellow]Warning: {issue}[/]")
+ if issues is not None:
+ # #1887: a line that is not JSON is a skipped row too, and
+ # `soup data validate` counts it like a non-object row.
+ issues.append(issue)
return data
diff --git a/tests/test_data_validate_skipped_rows_gate.py b/tests/test_data_validate_skipped_rows_gate.py
new file mode 100644
index 00000000..08552bc7
--- /dev/null
+++ b/tests/test_data_validate_skipped_rows_gate.py
@@ -0,0 +1,161 @@
+"""#1887 — ``soup data validate`` counts the rows the loader skipped as invalid rows.
+
+#1873 made the loader skip every row that is not a JSON object (and it already
+skipped lines that are not valid JSON). ``validate`` listed those skips but its
+numbers never saw them: ``total`` and ``valid`` came from the retained objects
+only, so a file of junk rows exited 0 at "0/0 rows valid" and ``[good, 42]``
+passed ``--min-valid-fraction 1.0`` at "1/1 rows valid". The rule now: a skipped
+row is an invalid row for the ``N/M rows valid`` line, for ``--min-valid-fraction``
+and for the exit code. The listing and the retained-object statistics are unchanged.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+from typer.testing import CliRunner
+
+from soup_cli.commands.data import app
+from tests.conftest import strip_ansi
+
+GOOD = {"instruction": "A sufficiently long question", "output": "A useful answer"}
+GOOD2 = {**GOOD, "instruction": "Another sufficiently long question"}
+JUNK = [42, "abc", [1, 2], None]
+
+pytestmark = pytest.mark.parametrize("extension", ["jsonl", "json"])
+
+
+def _plain(text: str) -> str:
+ return " ".join(strip_ansi(text).split())
+
+
+def _write(tmp_path: Path, extension: str, rows: list, *, raw_lines: list[str] | None = None):
+ path = tmp_path / f"data.{extension}"
+ if extension == "jsonl":
+ lines = [json.dumps(row) for row in rows] + list(raw_lines or [])
+ path.write_text("\n".join(lines) + "\n", encoding="utf-8")
+ else:
+ assert raw_lines is None, "a .json array cannot hold an invalid line"
+ path.write_text(json.dumps(rows), encoding="utf-8")
+ return path
+
+
+def _validate(path: Path, *extra: str):
+ result = CliRunner().invoke(app, ["validate", str(path), *extra])
+ return result.exit_code, _plain(result.output)
+
+
+# ---------------------------------------------------------------------------
+# The issue's reproductions
+# ---------------------------------------------------------------------------
+
+
+def test_a_file_of_only_junk_rows_fails_the_gate(tmp_path, extension):
+ path = _write(tmp_path, extension, JUNK)
+ code, out = _validate(path, "--format", "alpaca")
+ assert code == 2, out
+ assert "0/4 rows valid" in out
+ assert "no usable rows remain" in out
+ assert "Dataset is valid!" not in out
+
+
+def test_good_plus_junk_fails_a_minimum_of_one(tmp_path, extension):
+ path = _write(tmp_path, extension, [GOOD, 42])
+ code, out = _validate(path, "--format", "alpaca", "--min-valid-fraction", "1.0")
+ assert code == 2, out
+ assert "1/2 rows valid" in out
+ assert "valid fraction 0.500 is below --min-valid-fraction 1.000" in out
+
+
+def test_the_minimum_is_strict_so_half_passes_at_half(tmp_path, extension):
+ path = _write(tmp_path, extension, [GOOD, 42])
+ code, out = _validate(path, "--format", "alpaca", "--min-valid-fraction", "0.5")
+ assert code == 0, out
+ assert "1/2 rows valid" in out
+
+
+# ---------------------------------------------------------------------------
+# Controls: what must keep passing
+# ---------------------------------------------------------------------------
+
+
+def test_control_a_clean_file_passes_a_minimum_of_one(tmp_path, extension):
+ path = _write(tmp_path, extension, [GOOD, GOOD2])
+ code, out = _validate(path, "--format", "alpaca", "--min-valid-fraction", "1.0")
+ assert code == 0, out
+ assert "2/2 rows valid" in out
+ assert "Dataset is valid!" in out
+
+
+def test_a_partial_drop_still_passes_without_a_minimum(tmp_path, extension):
+ path = _write(tmp_path, extension, [GOOD, 42])
+ code, out = _validate(path, "--format", "alpaca")
+ assert code == 0, out
+ assert "1/2 rows valid" in out
+
+
+def test_the_listing_cap_and_the_count_agree(tmp_path, extension):
+ path = _write(tmp_path, extension, [GOOD] + [42] * 6)
+ code, out = _validate(path, "--format", "alpaca")
+ assert code == 0, out
+ assert "1/7 rows valid" in out
+ assert "... and 3 more" in out
+
+
+# ---------------------------------------------------------------------------
+# The "0 rows" cases keep their codes
+# ---------------------------------------------------------------------------
+
+
+def test_a_zero_byte_file_is_still_zero_of_zero(tmp_path, extension):
+ path = tmp_path / f"empty.{extension}"
+ path.write_text("" if extension == "jsonl" else "[]", encoding="utf-8")
+ code, out = _validate(path, "--format", "alpaca")
+ assert code == 0, out
+ assert "0/0 rows valid" in out
+
+
+def test_an_all_junk_file_without_a_format_is_still_a_usage_error(tmp_path, extension):
+ path = _write(tmp_path, extension, JUNK)
+ code, out = _validate(path)
+ assert code == 3, out
+ # The refusal says what happened rather than calling a 4-row file empty.
+ assert "no object rows remain" in out
+ assert "4 row(s) skipped" in out
+ assert "--format" in out
+
+
+def test_the_all_junk_issue_line_names_the_skips_not_an_empty_dataset(tmp_path, extension):
+ path = _write(tmp_path, extension, JUNK)
+ code, out = _validate(path, "--format", "alpaca")
+ assert code == 2, out
+ assert "no object rows remain: 4 row(s) skipped" in out
+ assert "Dataset is empty" not in out
+
+
+# ---------------------------------------------------------------------------
+# Lines that are not JSON at all count the same way (.jsonl only)
+# ---------------------------------------------------------------------------
+
+
+def test_an_invalid_json_line_is_an_invalid_row(tmp_path, extension):
+ if extension == "json":
+ pytest.skip("a .json array is parsed whole; an invalid file is a load error")
+ path = _write(tmp_path, extension, [GOOD], raw_lines=["{not json"])
+ code, out = _validate(path, "--format", "alpaca", "--min-valid-fraction", "1.0")
+ assert code == 2, out
+ assert "1/2 rows valid" in out
+ assert "invalid JSON on line 2" in out
+ assert "Dataset is valid!" not in out
+
+
+def test_an_invalid_json_line_is_listed_once(tmp_path, extension):
+ if extension == "json":
+ pytest.skip("a .json array is parsed whole; an invalid file is a load error")
+ path = _write(tmp_path, extension, [GOOD], raw_lines=["{not json"])
+ code, out = _validate(path, "--format", "alpaca")
+ assert code == 0, out
+ listing = out.split("Issues found:", 1)[1]
+ assert listing.count("invalid JSON on line 2") == 1