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.

test(autodistill): pin the vocabulary guards and shard non-retention (#1341) (#1400)

Maxibe3ee6533cdc
diff --git a/tests/test_issue580_autodistill_capture.py b/tests/test_issue580_autodistill_capture.py
index 38a8ff68..f579a3a8 100644
--- a/tests/test_issue580_autodistill_capture.py
+++ b/tests/test_issue580_autodistill_capture.py
@@ -174,3 +174,62 @@ def test_trajectory_rejects_missing_positions_and_undeclared_truncation():
max_sequence_length=1,
truncation="none",
)
+
+
[email protected]("bad_id", [4, -1])
+def test_capture_token_refuses_ids_outside_the_vocabulary(bad_id):
+ """#1341: the vocabulary guard in ``_validate_token_ids`` was never exercised.
+
+ The match is anchored on purpose: ``CaptureToken``'s own validator raises
+ the same phrase, but inside a pydantic ``ValidationError`` whose text starts
+ with "1 validation error". Anchoring is what makes these cases fail when the
+ capture guard is disabled, for -1 as well as for ``vocab_size``.
+ """
+ with pytest.raises(
+ ValueError, match=r"^target_token_id contains an id outside the vocabulary$"
+ ):
+ build_teacher_expert_capture_token(
+ example_id="example-1",
+ position=0,
+ context_token_ids=(1,),
+ target_token_id=bad_id,
+ teacher_logits=(4.0, 3.0, 2.0, -4.0),
+ vocab_size=4,
+ probability_policy=_policy(top_k=2),
+ )
+ with pytest.raises(
+ ValueError, match=r"^context_token_ids contains an id outside the vocabulary$"
+ ):
+ build_teacher_expert_capture_token(
+ example_id="example-1",
+ position=0,
+ context_token_ids=(1, bad_id),
+ target_token_id=2,
+ teacher_logits=(4.0, 3.0, 2.0, -4.0),
+ vocab_size=4,
+ probability_policy=_policy(top_k=2),
+ )
+
+
[email protected]("bad_id", [4, -1])
+def test_trajectory_capture_refuses_ids_outside_the_vocabulary(bad_id):
+ logits = ((4.0, 3.0, 2.0, -4.0),)
+ for field, prompt, targets in (
+ ("prompt_token_ids", (bad_id,), (1,)),
+ ("target_token_ids", (1,), (bad_id,)),
+ ):
+ with pytest.raises(
+ ValueError, match=rf"^{field} contains an id outside the vocabulary$"
+ ):
+ capture_teacher_expert_trajectory(
+ example=TeacherExpertExample(
+ example_id="example-1",
+ prompt_token_ids=prompt,
+ target_token_ids=targets,
+ ),
+ teacher_logits_by_position=logits,
+ vocab_size=4,
+ probability_policy=_policy(top_k=2),
+ max_sequence_length=16,
+ truncation="none",
+ )
diff --git a/tests/test_issue580_autodistill_fingerprints.py b/tests/test_issue580_autodistill_fingerprints.py
index 1f7f71de..399e59d9 100644
--- a/tests/test_issue580_autodistill_fingerprints.py
+++ b/tests/test_issue580_autodistill_fingerprints.py
@@ -143,6 +143,28 @@ def test_teacher_weight_hashing_uses_bounded_streaming_reads(tmp_path, monkeypat
assert set(read_sizes) == {4}
+def test_teacher_weight_hashing_does_not_retain_shard_bytes(tmp_path, monkeypatch):
+ """#1341: bounded reads alone don't stop the chunks being kept and joined."""
+ import soup_cli.autodistill.fingerprints as fingerprints
+
+ plan, teacher_root, _, _, _ = _local_plan(tmp_path)
+ weights_path = teacher_root / "model.safetensors"
+ original_stream_file = fingerprints._stream_file
+ retained: dict[Path, bytes] = {}
+
+ def recording_stream_file(path, *, retain_bytes):
+ size, digest, data = original_stream_file(path, retain_bytes=retain_bytes)
+ retained[Path(path)] = data
+ return size, digest, data
+
+ monkeypatch.setattr(fingerprints, "_HASH_CHUNK_BYTES", 4)
+ monkeypatch.setattr(fingerprints, "_stream_file", recording_stream_file)
+
+ verify_teacher_fingerprint(plan, teacher_root=teacher_root)
+
+ assert retained[weights_path] == b""
+
+
def test_tokenizer_requires_exact_files_template_and_renderer(tmp_path):
plan, _, tokenizer_root, _, template = _local_plan(tmp_path)
with pytest.raises(ArtifactCorruptionError, match="chat template"):
diff --git a/tests/test_issue580_autodistill_mlx_worker.py b/tests/test_issue580_autodistill_mlx_worker.py
index 04feeb94..700cdd6c 100644
--- a/tests/test_issue580_autodistill_mlx_worker.py
+++ b/tests/test_issue580_autodistill_mlx_worker.py
@@ -145,6 +145,7 @@ def _local_plan(
backend_version: str = _MLX_VERSION,
truncation: str = "none",
max_sequence_length: int = 16,
+ target_token_ids: tuple[int, ...] = (3, 4, 5),
) -> tuple[AutoDistillPlan, Path, Path, Path]:
teacher_root = tmp_path / "teacher"
tokenizer_root = tmp_path / "tokenizer"
@@ -156,10 +157,11 @@ def _local_plan(
weights = b"teacher-weights"
tokenizer = b'{"version":"1.0"}\n'
template = "{{ messages }}"
+ targets = ",".join(str(token_id) for token_id in target_token_ids).encode()
dataset = (
b'{"example_id":"example-1","prompt_token_ids":[1,2],'
b'"schema":"soup.autodistill.tokenized-teacher-example.v1",'
- b'"target_token_ids":[3,4,5]}\n'
+ b'"target_token_ids":[' + targets + b"]}\n"
)
(teacher_root / "config.json").write_bytes(config)
(teacher_root / "model.safetensors").write_bytes(weights)
@@ -652,6 +654,34 @@ def test_changed_bound_dataset_fails_before_model_load(monkeypatch, tmp_path):
assert not trace.exists()
+def test_out_of_vocabulary_dataset_id_fails_before_model_load(monkeypatch, tmp_path):
+ """#1341: the worker's own vocabulary guard, on a dataset that still
+ matches its fingerprint, so nothing earlier can refuse it first."""
+ plan, teacher_root, tokenizer_root, dataset_root = _local_plan(
+ tmp_path, target_token_ids=(3, 4, 8)
+ )
+ assert plan.capture.vocab_size == 8
+ fake_root = _write_fake_mlx_runtime(tmp_path)
+ trace = _runtime_environment(monkeypatch, tmp_path, fake_root)
+
+ with pytest.raises(
+ RuntimeError, match="teacher capture dataset contains an id outside the vocabulary"
+ ):
+ run_mlx_teacher_capture_process(
+ plan=plan,
+ teacher_root=teacher_root,
+ tokenizer_root=tokenizer_root,
+ dataset_root=dataset_root,
+ publication_root=tmp_path / "publication",
+ shard_id="shard-0001",
+ transaction_id="transaction-0001",
+ python_executable=sys.executable,
+ timeout_seconds=30,
+ )
+
+ assert not trace.exists()
+
+
def test_changed_bound_tokenizer_fails_before_model_load(monkeypatch, tmp_path):
plan, teacher_root, tokenizer_root, dataset_root = _local_plan(tmp_path)
fake_root = _write_fake_mlx_runtime(tmp_path)