-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathprepare_data.py
More file actions
201 lines (179 loc) · 5.41 KB
/
Copy pathprepare_data.py
File metadata and controls
201 lines (179 loc) · 5.41 KB
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
#!/usr/bin/env python3
"""
Prepare data for speculator training
This script processes an input dataset and:
1. Applies chat template + tokenizes each sample
2. Produces a loss/assistant mask for each sample
3. Records token frequency statistics
The output of this script is:
1. Processed dataset ready for online training or offline datagen in output_dir
2. Token frequency statistics file at token_freq_path
Preprocessing will be skipped if the dataset already exists at the output directory.
Token frequencies are saved in the output directory by default.
Usage:
python prepare_data.py \
--model meta-llama/Llama-3.1-8B-Instruct \
--data sharegpt \
--output ./training_data \
--max-samples 5000
"""
import argparse
import glob
import logging
import shutil
import sys
from pathlib import Path
from speculators.data_generation.logging_utils import PipelineLogger
from speculators.data_generation.preprocessing import (
load_and_preprocess_dataset,
)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
log = PipelineLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser(description="Prepare data for speculator training")
# Model arguments
parser.add_argument(
"--model",
type=str,
required=True,
help="HuggingFace model ID or local path for target model",
)
parser.add_argument(
"--trust-remote-code",
action="store_true",
help=(
"Allow executing code from HF Hub when loading the target model's "
"processor."
),
)
# Data arguments
parser.add_argument(
"--data",
type=str,
action="append",
required=True,
help="Path to training data (same as used in preprocessing)",
)
parser.add_argument(
"--seq-length",
type=int,
default=8192,
help="Maximum sequence length for preprocessing and model (default: 8192)",
)
parser.add_argument(
"--max-samples",
type=int,
default=None,
help="Maximum number of samples to process (default: None, process all)",
)
parser.add_argument(
"--token-freq-path",
type=str,
default=None,
help=(
"Path to save token frequency distribution "
"(default: args.output / 'token_freq.pt')"
),
)
parser.add_argument(
"--assistant-pattern",
type=str,
default=None,
help=(
"Custom regex pattern for matching assistant responses. "
"If not provided, auto-detected from chat template."
),
)
parser.add_argument(
"--turn-dropout",
action="store_true",
help=(
"Enable turn dropout: randomly keeps first N consecutive turns "
"per conversation for data augmentation."
),
)
# Output arguments
parser.add_argument(
"--output",
type=str,
default="./output",
help="Directory to save output dataset (default: ./output)",
)
parser.add_argument(
"--overwrite",
action="store_true",
help=(
"Forcibly rerun `prepare_data.py`.Deletes existing content in output dir"
),
)
# Processing arguments
parser.add_argument(
"--seed",
type=int,
default=0,
help="Random seed (must match preprocessing seed, default: 0)",
)
parser.add_argument(
"--num-preprocessing-workers",
type=int,
default=8,
help="Number of CPU processes for dataset preprocessing (default: 8)",
)
parser.add_argument(
"--minimum-valid-tokens",
type=int,
default=None,
help=(
"Drop samples whose loss mask contains fewer than this many "
"trainable tokens."
),
)
return parser.parse_args()
def main():
args = parse_args()
log.section("Preparing data")
log.config(
{
"Target Model": args.model,
"Dataset": args.data,
"Output Dir": args.output,
}
)
output = Path(args.output)
if output.exists():
if not args.overwrite and glob.glob(str(output / "*.arrow")):
log.warning(
"Dataset files already exists in output directory, skipping "
"preprocessing. To existing overwrite files use --overwrite."
)
sys.exit(0)
if args.overwrite:
shutil.rmtree(output)
output.mkdir(parents=True)
else:
output.mkdir(parents=True)
token_freq_path = (
output / "token_freq.pt"
if args.token_freq_path is None
else Path(args.token_freq_path)
)
dataset, _ = load_and_preprocess_dataset(
target_model_path=args.model,
train_data_paths=args.data,
seq_length=args.seq_length,
build_dataset_num_proc=args.num_preprocessing_workers,
seed=args.seed,
max_samples=args.max_samples,
token_freq_path=token_freq_path,
assistant_pattern=args.assistant_pattern,
turn_dropout=args.turn_dropout,
minimum_valid_tokens=args.minimum_valid_tokens,
trust_remote_code=args.trust_remote_code,
)
log.info("Done preparing data")
log.section(f"Writing dataset to {args.output}")
dataset.save_to_disk(args.output)
if __name__ == "__main__":
main()