Upload prepare_sft.py with huggingface_hub
Browse files- prepare_sft.py +90 -0
prepare_sft.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import Dataset
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
DRY_RUN: bool = os.getenv("DRY_RUN", "0") == "1"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
__doc__ = rf"""
|
| 11 |
+
A simple preprocessing script for preparing SFT data.
|
| 12 |
+
Usage:
|
| 13 |
+
python {sys.argv[0]} /path/to/VTCTrain # results will be saved to /path/to/VTCTrain/sft
|
| 14 |
+
DRY_RUN=1 python {sys.argv[0]} /path/to/VTCTrain # to inspect the output format without saving
|
| 15 |
+
|
| 16 |
+
Output arrow schema looks like:
|
| 17 |
+
```json
|
| 18 |
+
{{
|
| 19 |
+
'instruction': 'str',
|
| 20 |
+
'problem': 'str',
|
| 21 |
+
'answer': 'str',
|
| 22 |
+
}}
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
You may customize instruction templates to mitigate overfitting,
|
| 26 |
+
or adjust problem/answer mildly to avoid exact matches.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# normally this would be /path/to/VTCTrain
|
| 31 |
+
parent_folder = sys.argv[1]
|
| 32 |
+
assert "VTC" in parent_folder, "Please provide a VTC path."
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
RULER_INSTRUCTION = [
|
| 36 |
+
"sample instruction 1",
|
| 37 |
+
"sample instruction 2",
|
| 38 |
+
]
|
| 39 |
+
NOLIMA_INSTRUCTION = [
|
| 40 |
+
"sample instruction 1",
|
| 41 |
+
"sample instruction 2",
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def ruler_map(e: dict, idx: int) -> dict:
|
| 46 |
+
rng = np.random.default_rng(idx)
|
| 47 |
+
first_gt = e["answers"][0]
|
| 48 |
+
if isinstance(first_gt, str) and not first_gt.isdigit():
|
| 49 |
+
problem = f'What is one of the magic words for {e["problem"]}?'
|
| 50 |
+
else:
|
| 51 |
+
# isdigit or int, treat as number
|
| 52 |
+
assert isinstance(first_gt, (str, int))
|
| 53 |
+
problem = f'What is one of the magic numbers for {e["problem"]}?'
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"instruction": rng.choice(RULER_INSTRUCTION),
|
| 57 |
+
"problem": "<image>" * len(e["images"]) + problem,
|
| 58 |
+
"answer": ",".join(e["answers"]),
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def nolima_map(e: dict, idx: int) -> dict:
|
| 63 |
+
rng = np.random.default_rng(idx)
|
| 64 |
+
return {
|
| 65 |
+
"instruction": rng.choice(NOLIMA_INSTRUCTION),
|
| 66 |
+
"problem": "<image>" * len(e["images"]) + e["problem"],
|
| 67 |
+
"answer": e["answers"][0],
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
subfolders = ["NoLiMa", "RULER-MK", "RULER-MQ", "RULER-MV", "RULER-S"]
|
| 72 |
+
mapping_fns = [nolima_map, ruler_map, ruler_map, ruler_map, ruler_map]
|
| 73 |
+
max_samples = [6600, 2200, 2200, 2200, 2200]
|
| 74 |
+
|
| 75 |
+
for subfolder, mapping_fn, max_sample in zip(subfolders, mapping_fns, max_samples):
|
| 76 |
+
d = Dataset.load_from_disk(f"{parent_folder}/{subfolder}")
|
| 77 |
+
d = d.select(range(min(len(d), max_sample)))
|
| 78 |
+
d: Dataset = d.map(mapping_fn, with_indices=True, batch_size=100, writer_batch_size=100, num_proc=16)
|
| 79 |
+
sample = d[0]
|
| 80 |
+
print(
|
| 81 |
+
f"""
|
| 82 |
+
instruction: {sample["instruction"]}
|
| 83 |
+
problem: {sample["problem"]}
|
| 84 |
+
answer: {sample["answer"]}
|
| 85 |
+
"""
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if DRY_RUN:
|
| 89 |
+
continue
|
| 90 |
+
d.save_to_disk(f"{parent_folder}/sft/{subfolder}")
|