-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathmain.py
More file actions
306 lines (274 loc) · 12.7 KB
/
main.py
File metadata and controls
306 lines (274 loc) · 12.7 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
294
295
296
297
298
299
300
301
302
303
304
305
306
# Adapted from https://github.com/tatsu-lab/stanford_alpaca/blob/3783d18/train.py
# Copyright 2023 Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li
#
# 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.
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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 dataclasses
import os
import torch
import transformers
from eagle_utils import (
EagleTrainerWithAccLog,
EagleTrainingPlot,
LoRAWarmupCallback,
make_speculative_data_module,
patch_ring_attention_for_ttt,
)
from rich.pretty import pprint
from transformers.trainer_utils import get_last_checkpoint
import modelopt.torch.opt as mto
import modelopt.torch.speculative as mtsp
from modelopt.recipe import load_recipe
from modelopt.recipe.config import (
ModelOptDFlashRecipe,
ModelOptEagleRecipe,
ModelOptMedusaRecipe,
ModelOptSpeculativeRecipeBase,
)
from modelopt.torch.speculative.plugins.hf_training_args import (
TrainingArguments as SpecTrainingArgs,
)
from modelopt.torch.speculative.utils import load_vlm_or_llm, patch_transformers5_params_loading
from modelopt.torch.utils import print_rank_0
from modelopt.torch.utils.distributed import is_master, local_rank
torch.manual_seed(0)
mto.enable_huggingface_checkpointing()
# HF-compatible TrainingArguments with our speculative-decoding extensions, auto-derived
# from :class:`SpecTrainingArgs` so its field set can't drift from the Pydantic recipe schema.
# Used at runtime as ``HfTrainingArguments(**recipe.training.model_dump())`` to obtain a
# ``transformers.Trainer``-compatible dataclass.
HfTrainingArguments = dataclasses.make_dataclass(
"HfTrainingArguments",
[
(name, fi.annotation, dataclasses.field(default=fi.default))
for name, fi in SpecTrainingArgs.model_fields.items()
],
bases=(transformers.TrainingArguments,),
)
def _parse_cli() -> tuple[str, bool, list[str]]:
"""Parse --config (required) and --dry_run from argv; return remaining args as dotlist overrides.
Extra positional args use dotlist syntax, e.g.
``model.model_name_or_path=meta-llama/Llama-3.2-1B training.output_dir=ckpts/test``.
"""
p = argparse.ArgumentParser(add_help=False)
p.add_argument(
"--config",
required=True,
help=(
"Path to a modelopt speculative-decoding recipe YAML "
"(speculative_eagle / speculative_dflash / speculative_medusa)."
),
)
p.add_argument(
"--dry_run",
action="store_true",
help="Skip training: load base + mtsp.convert + save_pretrained, then exit. "
"Produces a ModelOpt HF checkpoint with untrained draft-head weights, suitable "
"for end-to-end plumbing tests (e.g. running scripts/export_hf_checkpoint.py).",
)
args, overrides = p.parse_known_args()
return args.config, args.dry_run, overrides
def init_distributed_env(training_args: transformers.TrainingArguments) -> None:
"""Resolve dp_shard_size from the live env and attach a ParallelismConfig in-place.
Reads ``WORLD_SIZE`` / ``torch.cuda.device_count()`` and (when actually distributed)
builds an ``accelerate.ParallelismConfig`` on ``training_args``. Kept out of the
Pydantic schema so the recipe stays a pure declarative spec.
"""
if training_args.cp_size < 1:
raise ValueError(f"cp_size must be >= 1, got {training_args.cp_size}.")
world_size = int(os.environ.get("WORLD_SIZE", torch.cuda.device_count() or 1))
if training_args.dp_shard_size is None:
training_args.dp_shard_size = world_size // training_args.cp_size
if training_args.dp_shard_size < 1:
raise ValueError(
f"dp_shard_size resolved to {training_args.dp_shard_size}; "
f"WORLD_SIZE ({world_size}) must be >= cp_size ({training_args.cp_size})."
)
if training_args.cp_size > 1 or training_args.dp_shard_size > 1:
parallel_size = training_args.dp_shard_size * training_args.cp_size
if world_size % parallel_size != 0:
raise ValueError(
f"world_size ({world_size}) must be divisible by "
f"dp_shard_size ({training_args.dp_shard_size}) * "
f"cp_size ({training_args.cp_size}) = {parallel_size}"
)
try:
from accelerate import ParallelismConfig
except ImportError as e:
raise ImportError(
"cp_size>1 or dp_shard_size>1 requires `accelerate` for ParallelismConfig. "
"Install it via `pip install accelerate`."
) from e
training_args.parallelism_config = ParallelismConfig(
cp_size=training_args.cp_size,
dp_shard_size=training_args.dp_shard_size,
dp_replicate_size=world_size // parallel_size,
)
def train():
config_path, dry_run, overrides = _parse_cli()
recipe = load_recipe(config_path, overrides=overrides)
if not isinstance(recipe, ModelOptSpeculativeRecipeBase):
raise ValueError(
f"main.py expects a speculative-decoding recipe (eagle / dflash / medusa); "
f"got {type(recipe).__name__} from {config_path!r}."
)
# Pydantic-typed sections flow straight through as *_args; only TrainingArguments is
# reconstructed as an HF dataclass so it can be handed to transformers.Trainer.
training_args = HfTrainingArguments(**recipe.training.model_dump())
init_distributed_env(training_args)
if not dry_run and recipe.data.mode in ("online", "streaming") and not recipe.data.data_path:
raise ValueError(f"data.mode={recipe.data.mode!r} requires data.data_path.")
if training_args.cp_size > 1:
patch_ring_attention_for_ttt()
# Specific patch to accelerate 1.12.0. Removable after move to 1.13.0
training_args.parallelism_config.sp_backend = None
if is_master():
pprint(recipe)
# Detect checkpoint to resume from
last_checkpoint = (
get_last_checkpoint(training_args.output_dir)
if os.path.isdir(training_args.output_dir)
else None
)
if last_checkpoint:
print_rank_0(f"Last checkpoint detected: {last_checkpoint}")
checkpoint = training_args.resume_from_checkpoint or last_checkpoint
use_offline_training = recipe.data.mode != "online"
if checkpoint:
with patch_transformers5_params_loading():
model = load_vlm_or_llm(
checkpoint, dtype="auto", trust_remote_code=recipe.model.trust_remote_code
)
tokenizer = transformers.AutoTokenizer.from_pretrained(
checkpoint, trust_remote_code=recipe.model.trust_remote_code
)
else:
model_name_or_path = recipe.model.model_name_or_path
if model_name_or_path is None:
raise ValueError(
"model.model_name_or_path must be set in the recipe YAML or via a dotlist override."
)
# To avoid OOM for large models, we load and convert model on CPU first.
# Model will be moved to GPU during HF trainer.init().
model = load_vlm_or_llm(
model_name_or_path,
use_fake_base=recipe.model.use_fake_base_for_offline,
use_offline_training=use_offline_training,
dtype="auto",
device_map="cpu",
trust_remote_code=recipe.model.trust_remote_code,
)
tokenizer = transformers.AutoTokenizer.from_pretrained(
model_name_or_path,
model_max_length=training_args.training_seq_len,
trust_remote_code=recipe.model.trust_remote_code,
)
if isinstance(recipe, ModelOptMedusaRecipe):
medusa_cfg: dict = recipe.medusa.model_dump()
mtsp.convert(model, [("medusa", medusa_cfg)])
elif isinstance(recipe, ModelOptEagleRecipe):
eagle_cfg: dict = recipe.eagle.model_dump()
mtsp.convert(model, [("eagle", eagle_cfg)])
# Load draft vocab cache
mtsp.plugins.HFEagleModel.load_draft_vocab_cache(model, recipe.data.draft_vocab_cache)
elif isinstance(recipe, ModelOptDFlashRecipe):
# Fall back to tokenizer.mask_token_id when not set in the recipe; require one of the two.
if recipe.dflash.dflash_mask_token_id is None:
recipe.dflash.dflash_mask_token_id = getattr(tokenizer, "mask_token_id", None)
if recipe.dflash.dflash_mask_token_id is None:
raise ValueError(
"dflash.dflash_mask_token_id is required: set it in the recipe YAML "
"or use a tokenizer that defines mask_token_id."
)
dflash_cfg: dict = recipe.dflash.model_dump()
mtsp.convert(model, [("dflash", dflash_cfg)])
else:
raise ValueError(f"Unsupported speculative recipe type: {type(recipe).__name__}")
if dry_run:
# is_master() is unreliable here: we return before the HF Trainer inits torch.distributed,
# so use local_rank() (env-based) to keep a single writer to output_dir.
if local_rank() == 0:
os.makedirs(training_args.output_dir, exist_ok=True)
model.save_pretrained(training_args.output_dir)
tokenizer.save_pretrained(training_args.output_dir)
print_rank_0(
f"[dry-run] saved ModelOpt HF checkpoint (untrained draft head) to "
f"{training_args.output_dir}"
)
return
# Move any remaining CPU buffers to CUDA so DDP (NCCL-only) can broadcast
# them. We iterate named_buffers and reassign via the owning module to
# keep the module tree consistent. Parameters are left on CPU — the HF
# Trainer will move them during init.
if torch.cuda.is_available():
_target_dev = torch.device("cuda", 0)
for name, buf in list(model.named_buffers()):
if buf.device.type == "cpu":
parts = name.split(".")
mod = model
for p in parts[:-1]:
mod = getattr(mod, p)
setattr(mod, parts[-1], buf.to(_target_dev))
print_rank_0("Loading dataset...")
is_dflash = isinstance(recipe, ModelOptDFlashRecipe)
data_module = make_speculative_data_module(
tokenizer,
recipe.data,
train_len=training_args.training_seq_len,
answer_only_loss=training_args.answer_only_loss,
shift_labels=not is_dflash,
)
callbacks = [EagleTrainingPlot(training_args.ar_validate_steps, training_args.estimate_ar)]
if (
isinstance(recipe, ModelOptEagleRecipe)
and recipe.eagle.eagle_base_lora
and recipe.eagle.eagle_base_lora_warmup_steps > 0
):
callbacks.append(LoRAWarmupCallback(recipe.eagle.eagle_base_lora_warmup_steps))
# Leave training_args.ignore_data_skip at its default (False). The dataset is
# map-style, so HF Trainer's resume skips consumed indices at the batch-sampler
# level (accelerate.skip_first_batches) without re-fetching them, landing at the
# exact data position. Setting it True would restart the data order from the top.
trainer = EagleTrainerWithAccLog(
model=model,
processing_class=tokenizer,
args=training_args,
callbacks=callbacks,
**data_module,
)
# Manually enable this to return loss in eval
trainer.can_return_loss = True
# Make sure label_smoother is None
assert trainer.label_smoother is None, (
"label_smoother is not supported in speculative decoding!"
)
print_rank_0("Start training...")
trainer.train(resume_from_checkpoint=checkpoint)
trainer.save_state()
trainer.save_model(training_args.output_dir)
if __name__ == "__main__":
train()