File size: 7,145 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 | from pathlib import Path
import pytest
from datasets import Dataset as HFDataset
from speculators.cli.prepare_data import assert_safe_to_overwrite
from speculators.data_generation import preprocessing as preprocessing_module
from speculators.data_generation.preprocessing import load_and_preprocess_dataset
def test_assert_safe_to_overwrite_allows_prepare_data_artifacts(tmp_path: Path):
output = tmp_path / "data"
output.mkdir()
(output / "data-00000-of-00001.arrow").touch()
(output / "dataset_info.json").touch()
token_freq_path = output / "token_freq.pt"
token_freq_path.touch()
assert_safe_to_overwrite(output, token_freq_path)
def test_assert_safe_to_overwrite_rejects_unknown_files(tmp_path: Path):
output = tmp_path / "data"
output.mkdir()
(output / "data-00000-of-00001.arrow").touch()
(output / "checkpoints").mkdir()
with pytest.raises(ValueError, match="would delete files"):
assert_safe_to_overwrite(output, output / "token_freq.pt")
def test_assert_safe_to_overwrite_honors_custom_token_freq_path(tmp_path: Path):
output = tmp_path / "data"
output.mkdir()
token_freq_path = output / "custom_freq.pt"
token_freq_path.touch()
assert_safe_to_overwrite(output, token_freq_path)
class _FakeProcessor:
"""Minimal processor stub that passes the chat-template precondition."""
chat_template = "{{ messages }}"
def apply_chat_template(self, *args, **kwargs):
return ""
class _NoChatTemplateProcessor:
chat_template = None
def _patch_empty_pipeline(monkeypatch: pytest.MonkeyPatch) -> None:
"""Make load_and_preprocess_dataset produce an empty dataset without GPU/network."""
empty = HFDataset.from_dict({"input_ids": [], "loss_mask": [], "seq_len": []})
monkeypatch.setattr(
preprocessing_module, "load_processor", lambda *a, **k: _FakeProcessor()
)
monkeypatch.setattr(
preprocessing_module,
"load_raw_dataset",
lambda _path: (HFDataset.from_dict({"conversations": []}), None),
)
monkeypatch.setattr(
preprocessing_module, "build_speculator_training_dataset", lambda *a, **k: empty
)
monkeypatch.setattr(
preprocessing_module, "save_token_frequency_distribution", lambda **k: None
)
def test_load_and_preprocess_raises_on_empty_output(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
_patch_empty_pipeline(monkeypatch)
with pytest.raises(ValueError, match="No samples remain"):
load_and_preprocess_dataset(
"target-model",
["sharegpt"],
seq_length=8,
token_freq_path=tmp_path / "token_freq.pt",
)
def test_load_and_preprocess_allows_empty_output_with_flag(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
_patch_empty_pipeline(monkeypatch)
dataset, processor = load_and_preprocess_dataset(
"target-model",
["sharegpt"],
seq_length=8,
token_freq_path=tmp_path / "token_freq.pt",
allow_empty_output=True,
)
assert len(dataset) == 0
assert isinstance(processor, _FakeProcessor)
def test_pretokenized_data_does_not_require_chat_template(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
raw = HFDataset.from_dict(
{
"input_ids": [[1, 2, 3]],
"loss_mask": [[0, 1, 1]],
}
)
monkeypatch.setattr(
preprocessing_module,
"load_processor",
lambda *a, **k: _NoChatTemplateProcessor(),
)
monkeypatch.setattr(
preprocessing_module, "load_raw_dataset", lambda _path: (raw, None)
)
processed = HFDataset.from_dict(
{
"input_ids": [[1, 2, 3]],
"loss_mask": [[0, 1, 1]],
"seq_len": [3],
}
)
processed.set_format(type="torch")
monkeypatch.setattr(
preprocessing_module,
"build_speculator_training_dataset",
lambda *a, **k: processed,
)
monkeypatch.setattr(
preprocessing_module, "save_token_frequency_distribution", lambda **k: None
)
monkeypatch.setattr(preprocessing_module, "_visualize_sample", lambda *a, **k: None)
dataset, _ = load_and_preprocess_dataset(
"custom-model",
["pretokenized.jsonl"],
seq_length=8,
build_dataset_num_proc=1,
token_freq_path=tmp_path / "token_freq.pt",
)
assert len(dataset) == 1
assert dataset[0]["input_ids"].tolist() == [1, 2, 3]
def test_conversation_data_still_requires_chat_template(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
"""Without a render endpoint, conversation data still fails fast when the
processor exposes no chat template."""
raw = HFDataset.from_dict(
{
"conversations": [
[
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "hi"},
]
]
}
)
monkeypatch.setattr(
preprocessing_module,
"load_processor",
lambda *a, **k: _NoChatTemplateProcessor(),
)
monkeypatch.setattr(
preprocessing_module, "load_raw_dataset", lambda _path: (raw, None)
)
with pytest.raises(ValueError, match="does not support chat templates"):
load_and_preprocess_dataset(
"custom-model",
["conversations.jsonl"],
seq_length=8,
build_dataset_num_proc=1,
token_freq_path=tmp_path / "token_freq.pt",
)
def test_render_endpoint_bypasses_chat_template_requirement(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
"""Server-side rendering needs no local chat template: the processor's
template is never used when a render endpoint is provided."""
raw = HFDataset.from_dict(
{
"conversations": [
[
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "hi"},
]
]
}
)
monkeypatch.setattr(
preprocessing_module,
"load_processor",
lambda *a, **k: _NoChatTemplateProcessor(),
)
monkeypatch.setattr(
preprocessing_module, "load_raw_dataset", lambda _path: (raw, None)
)
processed = HFDataset.from_dict(
{
"input_ids": [[1, 2, 3]],
"loss_mask": [[0, 1, 1]],
"seq_len": [3],
}
)
processed.set_format(type="torch")
monkeypatch.setattr(
preprocessing_module,
"build_speculator_training_dataset",
lambda *a, **k: processed,
)
monkeypatch.setattr(
preprocessing_module, "save_token_frequency_distribution", lambda **k: None
)
monkeypatch.setattr(preprocessing_module, "_visualize_sample", lambda *a, **k: None)
dataset, _ = load_and_preprocess_dataset(
"custom-model",
["conversations.jsonl"],
seq_length=8,
build_dataset_num_proc=1,
token_freq_path=tmp_path / "token_freq.pt",
render_endpoint="http://localhost:8000",
)
assert len(dataset) == 1
|