forked from NVIDIA-NeMo/Curator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
293 lines (258 loc) · 9.79 KB
/
main.py
File metadata and controls
293 lines (258 loc) · 9.79 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import time
import ray
from filters.heuristic_filters import (
ContainsThinkOpenTagFilter,
EmptyThinkTagsFilter,
MissingThinkCloseTagFilter,
MissingThinkOpenTagFilter,
NanoFilter,
ThinkingOnFilter,
malformed_filter,
)
from filters.model_filters import ApplyChatTemplate, CompletionTokenCountFilter, NonEnglishFilter, TokenCountFilter
from utils.jsonl_utils import interleave_datasets
from nemo_curator.core.client import RayClient
from nemo_curator.pipeline import Pipeline
from nemo_curator.stages.text.filters import ScoreFilter
from nemo_curator.stages.text.io.reader.jsonl import JsonlReader
from nemo_curator.stages.text.io.writer.jsonl import JsonlWriter
from nemo_curator.utils.file_utils import get_all_file_paths_under
from nemo_curator.utils.split_large_files import split_jsonl_file_by_size
def main(args: argparse.Namespace) -> None: # noqa: PLR0915
try:
os.makedirs(args.output_dir, exist_ok=False)
except FileExistsError as e:
msg = f"Output directory already exists: {args.output_dir}. Please delete or rename it and try again."
raise FileExistsError(msg) from e
# Initialize and start Ray client with the number of CPUs specified by the user
ray_client = RayClient(num_cpus=args.num_cpus)
ray_client.start()
# Initialize pipelines
pipeline_thinking_on = Pipeline(
name="curriculum_learning_thinking_on", description="Prepare dataset for curriculum learning with thinking ON."
)
pipeline_thinking_off = Pipeline(
name="curriculum_learning_thinking_off",
description="Prepare dataset for curriculum learning with thinking OFF.",
)
start_time = time.time()
# Handle input path
input_files = list(get_all_file_paths_under(args.input_dir, recurse_subdirectories=True, keep_extensions="jsonl"))
if args.filename_filter:
# Filter out files that don't contain any of the provided substrings
input_files = [filename for filename in input_files if any(s in filename for s in args.filename_filter)]
input_dir = os.path.join(args.output_dir, "input_data_shards")
os.makedirs(input_dir, exist_ok=False)
# Split into smaller files for parallel processing
ray.get(
[
split_jsonl_file_by_size.remote(
input_file=f,
output_path=input_dir,
target_size_mb=args.jsonl_blocksize_mb,
)
for f in input_files
]
)
# Read files for each pipeline
pipeline_thinking_on.add_stage(JsonlReader(file_paths=input_dir))
pipeline_thinking_off.add_stage(JsonlReader(file_paths=input_dir))
# Split pipelines into thinking ON and OFF
pipeline_thinking_on.add_stage(ScoreFilter(ThinkingOnFilter(), text_field="reasoning"))
pipeline_thinking_off.add_stage(ScoreFilter(ThinkingOnFilter(), text_field="reasoning", invert=True))
# Filter out samples based on various criteria
filter_steps = [
ScoreFilter(
NanoFilter(),
text_field="used_in_training",
),
ScoreFilter(
EmptyThinkTagsFilter(),
text_field="output",
),
malformed_filter,
ScoreFilter(
MissingThinkCloseTagFilter(),
text_field="output",
),
]
for filter_step in filter_steps:
pipeline_thinking_on.add_stage(filter_step)
pipeline_thinking_off.add_stage(filter_step)
# Filter out samples in thinking OFF that contain think tags
pipeline_thinking_off.add_stage(
ScoreFilter(
ContainsThinkOpenTagFilter(),
text_field="output",
)
)
# Filter out samples in thinking ON that do not contain think tags
pipeline_thinking_on.add_stage(
ScoreFilter(
MissingThinkOpenTagFilter(),
text_field="output",
)
)
# Filter out samples based on token count
tokenizer_steps = [
NonEnglishFilter(
tokenizer_identifier=args.tokenizer,
hf_token=args.hf_token,
lang_id_model_path=args.lang_id_model_path,
input_field="input",
output_field="output",
system_prompt_field="system_prompt",
),
TokenCountFilter(
tokenizer_identifier=args.tokenizer,
hf_token=args.hf_token,
max_token_count=args.max_token_count,
input_field="input",
output_field="output",
system_prompt_field="system_prompt",
),
CompletionTokenCountFilter(
tokenizer_identifier=args.tokenizer,
hf_token=args.hf_token,
max_completion_token_count=args.max_completion_token_count,
output_field="output",
),
ApplyChatTemplate(
tokenizer_identifier=args.tokenizer,
hf_token=args.hf_token,
input_field="input",
output_field="output",
system_prompt_field="system_prompt",
),
]
for tokenizer_step in tokenizer_steps:
pipeline_thinking_on.add_stage(tokenizer_step)
pipeline_thinking_off.add_stage(tokenizer_step)
if args.keep_columns:
keep_columns = args.keep_columns
# Always keep the completion_token_count column, so that we can sort the samples
if "completion_token_count" not in keep_columns:
keep_columns.append("completion_token_count")
else:
keep_columns = ["input", "output", "completion_token_count"]
# Save intermediate datasets
thinking_on_unsorted_path = os.path.join(args.output_dir, "thinking_on_unsorted")
thinking_off_unsorted_path = os.path.join(args.output_dir, "thinking_off_unsorted")
pipeline_thinking_on.add_stage(JsonlWriter(thinking_on_unsorted_path, fields=keep_columns))
pipeline_thinking_off.add_stage(JsonlWriter(thinking_off_unsorted_path, fields=keep_columns))
# Run pipelines
_thinking_on_output = pipeline_thinking_on.run()
_thinking_off_output = pipeline_thinking_off.run()
# Sort datasets
thinking_on_ds = ray.data.read_json(thinking_on_unsorted_path, lines=True)
thinking_on_ds = thinking_on_ds.sort("completion_token_count")
thinking_on_sorted_path = os.path.join(args.output_dir, "thinking_on_sorted")
thinking_on_ds.write_json(thinking_on_sorted_path, orient="records", lines=True)
thinking_off_ds = ray.data.read_json(thinking_off_unsorted_path, lines=True)
thinking_off_ds = thinking_off_ds.sort("completion_token_count")
thinking_off_sorted_path = os.path.join(args.output_dir, "thinking_off_sorted")
thinking_off_ds.write_json(thinking_off_sorted_path, orient="records", lines=True)
# Interleave datasets and combine into a single output file
interleave_datasets(
thinking_on_sorted_path,
thinking_off_sorted_path,
os.path.join(args.output_dir, "training.jsonl"),
chunk_size=args.chunk_size,
)
end_time = time.time()
print(f"Total time taken: {end_time - start_time} seconds")
ray_client.stop()
def attach_args() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
"Prepare dataset for curriculum learning.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--num-cpus",
type=int,
default=16,
help="Number of CPUs to use.",
)
parser.add_argument(
"--input-dir",
type=str,
help="Path to the input directory containing JSONL files.",
required=True,
)
parser.add_argument(
"--filename-filter",
nargs="+",
type=str,
help="If specified, only files with names containing one or more of the provided substrings will be processed.",
)
parser.add_argument(
"--jsonl-blocksize-mb",
type=int,
default=100,
help="Blocksize (in MB) to use for splitting the JSONL files.",
)
parser.add_argument(
"--tokenizer",
type=str,
default="meta-llama/Llama-3.1-8B-Instruct",
help="Hugging Face tokenizer",
)
parser.add_argument(
"--hf-token",
type=str,
help="Hugging Face token (if needed)",
)
parser.add_argument(
"--lang-id-model-path",
type=str,
help="Path to the FastText model",
required=True,
)
parser.add_argument(
"--max-token-count",
type=int,
default=16384,
help="Optional maximum token count. Rows exceeding this count will be filtered out.",
)
parser.add_argument(
"--max-completion-token-count",
type=int,
default=8192,
help="Optional maximum completion token count. Rows exceeding this count will be filtered out.",
)
parser.add_argument(
"--keep-columns",
nargs="+",
type=str,
help="Columns to keep when the dataset is written to disk.",
)
parser.add_argument(
"--chunk-size",
type=int,
default=1,
help="Chunk size to use for interleaving the datasets.",
)
parser.add_argument(
"--output-dir",
type=str,
help="Path to the output directory.",
required=True,
)
return parser
if __name__ == "__main__":
main(attach_args().parse_args())