File size: 7,410 Bytes
2dd5f57 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | import argparse
import json
from pathlib import Path
import pytest
from datasets import Dataset
from scripts import infinity_parser2_prepare_data as prepare_module
class _FakeProcessor:
chat_template = "{{ messages }}"
def apply_chat_template(self, *args, **kwargs):
return ""
def decode(self, _tokens):
return "token"
def _raw_dataset(candidate_ranks: list[int]) -> Dataset:
return Dataset.from_list(
[
{
"id": f"v1.12:{source_index:010d}",
"conversations": [
{"role": "user", "content": "question"},
{"role": "assistant", "content": "answer"},
],
"provenance": {
"source_line_index": source_index,
"candidate_rank": candidate_rank,
"sample_manifest_sha256": "a" * 64,
"generation_config_sha256": "b" * 64,
},
}
for source_index, candidate_rank in enumerate(candidate_ranks)
]
)
def _patch_pipeline(
monkeypatch: pytest.MonkeyPatch,
raw: Dataset,
*,
dropped_ranks: set[int] | None = None,
fanout_by_rank: dict[int, int] | None = None,
) -> None:
dropped_ranks = dropped_ranks or set()
fanout_by_rank = fanout_by_rank or {}
def fake_build(dataset, *, preserve_columns, **_kwargs):
rows = [
dataset[index]
for index in range(len(dataset))
if dataset[index]["_candidate_rank"] not in dropped_ranks
for _ in range(fanout_by_rank.get(dataset[index]["_candidate_rank"], 1))
]
result = Dataset.from_dict(
{
"input_ids": [[10, 11] for _ in rows],
"loss_mask": [[0, 1] for _ in rows],
"seq_len": [2 for _ in rows],
**{
column: [row[column] for row in rows] for column in preserve_columns
},
}
)
result.set_format(type="torch")
return result
monkeypatch.setattr(
prepare_module,
"load_processor",
lambda *args, **kwargs: _FakeProcessor(),
)
monkeypatch.setattr(
prepare_module,
"load_raw_dataset",
lambda _path: (raw, None),
)
monkeypatch.setattr(
prepare_module,
"build_speculator_training_dataset",
fake_build,
)
def _args(tmp_path: Path, target_samples: int) -> argparse.Namespace:
return argparse.Namespace(
model="target-model",
data=tmp_path / "success-pool.jsonl",
output=tmp_path,
ranked_target_samples=target_samples,
seq_length=8,
token_freq_train_ratio=0.67,
render_endpoint="http://127.0.0.1:8000",
# Both are store_true flags, so False is their parser default. Rendering
# goes through the endpoint set above, which --local-render is the
# alternative to.
local_render=False,
drop_clipped_rows=False,
minimum_valid_tokens=None,
num_preprocessing_workers=1,
preprocessing_batch_size=2,
trust_remote_code=False,
)
def test_ranked_preprocessing_backfills_and_uses_train_prefix(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
):
_patch_pipeline(
monkeypatch,
_raw_dataset([4, 0, 3, 1, 2]),
dropped_ranks={1},
fanout_by_rank={0: 2, 2: 3},
)
captured = {}
def capture_frequency(*, dataset, output_path):
captured["dataset"] = dataset.with_format(None)
captured["output_path"] = output_path
monkeypatch.setattr(
prepare_module,
"save_token_frequency_distribution",
capture_frequency,
)
args = _args(tmp_path, 3)
dataset = prepare_module.prepare_ranked_dataset(args)
plain = dataset.with_format(None)
assert plain["candidate_rank"] == [0, 0, 2, 2, 2, 3]
assert captured["dataset"]["_candidate_rank"] == [0, 0, 2, 2, 2]
assert captured["output_path"] == tmp_path / "token_freq.pt"
metadata = json.loads(dataset.info.description)["ranked_preprocessing"]
assert metadata == {
"dataset_order": "candidate_rank_ascending_assistant_turn",
"eligible_records": 4,
"eligible_training_rows": 7,
"generation_config_sha256": "b" * 64,
"sample_manifest_sha256": "a" * 64,
"selected_records": 3,
"selected_training_rows": 6,
}
def test_ranked_preprocessing_checks_the_complete_reserve(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
):
_patch_pipeline(monkeypatch, _raw_dataset([0, 1, 2, 2]))
monkeypatch.setattr(
prepare_module,
"save_token_frequency_distribution",
lambda **kwargs: None,
)
with pytest.raises(ValueError, match="not unique"):
prepare_module.prepare_ranked_dataset(_args(tmp_path, 2))
def test_ranked_preprocessing_requires_enough_survivors(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
):
_patch_pipeline(monkeypatch, _raw_dataset([0, 1]))
monkeypatch.setattr(
prepare_module,
"save_token_frequency_distribution",
lambda **kwargs: None,
)
with pytest.raises(ValueError, match="below target"):
prepare_module.prepare_ranked_dataset(_args(tmp_path, 3))
def test_selection_manifest_is_minimal_and_published_last(tmp_path: Path):
output = tmp_path / "prepared"
output.mkdir()
dataset = Dataset.from_dict(
{
"input_ids": [[10, 11], [10, 12], [12, 13]],
"loss_mask": [[0, 1], [0, 1], [0, 1]],
"seq_len": [2, 2, 2],
"id": [
"v1.12:0000000003",
"v1.12:0000000003",
"v1.12:0000000001",
],
"source_line_index": [3, 3, 1],
"candidate_rank": [0, 0, 2],
}
)
dataset.info.description = json.dumps(
{
"ranked_preprocessing": {
"sample_manifest_sha256": "a" * 64,
"generation_config_sha256": "b" * 64,
"selected_records": 2,
"selected_training_rows": 3,
}
}
)
staged, manifest = prepare_module.stage_selection(
dataset,
output=output,
target_samples=2,
)
assert manifest == {
"complete": True,
"target_records": 2,
"training_rows": 3,
"sample_manifest_sha256": "a" * 64,
"generation_config_sha256": "b" * 64,
"selection": {
"path": prepare_module.SELECTION_NAME,
"records": 2,
},
}
assert not (output / prepare_module.MANIFEST_NAME).exists()
dataset.save_to_disk(output)
prepare_module.publish_selection(
output,
staged_selection=staged,
manifest=manifest,
)
rows = [
json.loads(line)
for line in (output / prepare_module.SELECTION_NAME).read_text().splitlines()
]
assert [row["source_line_index"] for row in rows] == [1, 3]
assert not list(output.glob("*.partial"))
args = argparse.Namespace(output=output, ranked_target_samples=2)
prepare_module.validate_existing_output(args)
args.ranked_target_samples = 3
with pytest.raises(ValueError, match="different settings"):
prepare_module.validate_existing_output(args)
|