-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathconvert_checkpoints_multi_gpu.py
More file actions
362 lines (315 loc) · 13.8 KB
/
Copy pathconvert_checkpoints_multi_gpu.py
File metadata and controls
362 lines (315 loc) · 13.8 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python3
# Copyright (c) 2025, 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.
"""
Multi-GPU Checkpoint Conversion: Import (HF -> Megatron) and Export (Megatron -> HF).
This is the distributed counterpart of convert_checkpoints.py. It supports tensor,
pipeline, and expert parallelism for models that do not fit on a single GPU or
require a sharded Megatron checkpoint.
Usage examples:
# Import a HuggingFace MoE model to Megatron format with expert parallelism
uv run python -m torch.distributed.run --nproc_per_node=8 \
examples/conversion/convert_checkpoints_multi_gpu.py import \
--hf-model Qwen/Qwen3-30B-A3B \
--megatron-path ./checkpoints/qwen3_30b_a3b \
--tp 1 --ep 8
# Export a distributed Megatron checkpoint back to HuggingFace format
uv run python -m torch.distributed.run --nproc_per_node=8 \
examples/conversion/convert_checkpoints_multi_gpu.py export \
--hf-model Qwen/Qwen3-30B-A3B \
--megatron-path ./checkpoints/qwen3_30b_a3b \
--hf-path ./exports/qwen3_30b_a3b_hf \
--tp 1 --ep 8
# Import with pipeline parallelism
uv run python -m torch.distributed.run --nproc_per_node=4 \
examples/conversion/convert_checkpoints_multi_gpu.py import \
--hf-model meta-llama/Llama-3.1-70B \
--megatron-path ./checkpoints/llama31_70b \
--tp 2 --pp 2
# Multi-node import via Slurm srun
srun --ntasks-per-node=8 ... python \
examples/conversion/convert_checkpoints_multi_gpu.py import \
--hf-model moonshotai/Moonlight-16B-A3B \
--megatron-path ./checkpoints/moonlight \
--tp 2 --ep 8
"""
import argparse
import datetime
import os
import sys
import torch
from megatron.bridge import AutoBridge
from megatron.bridge.models.decorators import torchrun_main
from megatron.bridge.models.hf_pretrained.utils import is_safe_repo
from megatron.bridge.utils.common_utils import print_rank_0
DTYPE_MAP = {
"float32": torch.float32,
"float16": torch.float16,
"bfloat16": torch.bfloat16,
}
def _parse_dtype(name: str) -> torch.dtype:
if name not in DTYPE_MAP:
raise ValueError(f"Unsupported dtype '{name}'. Choose from {list(DTYPE_MAP)}.")
return DTYPE_MAP[name]
def _check_distributed():
if os.environ.get("WORLD_SIZE") is None:
print("This script must be launched with torchrun or srun. Example:")
print(f" torchrun --nproc_per_node <gpus> {sys.argv[0]} import --hf-model <id> --megatron-path <path>")
sys.exit(1)
def _ensure_distributed_initialized(timeout_minutes: int | None):
_check_distributed()
if timeout_minutes is None:
return
if torch.distributed.is_initialized():
return
torch.cuda.set_device(int(os.environ.get("LOCAL_RANK", "0")))
torch.distributed.init_process_group(
"nccl",
timeout=datetime.timedelta(minutes=timeout_minutes),
)
@torchrun_main
def import_hf_to_megatron(
hf_model: str,
megatron_path: str,
tp: int = 1,
pp: int = 1,
ep: int = 1,
etp: int = 1,
torch_dtype: str = "bfloat16",
trust_remote_code: bool = False,
distributed_timeout_minutes: int | None = None,
) -> None:
"""Import a HuggingFace model and save it as a distributed Megatron checkpoint."""
_ensure_distributed_initialized(distributed_timeout_minutes)
dtype = _parse_dtype(torch_dtype)
print_rank_0(f"Importing: {hf_model} -> {megatron_path}")
print_rank_0(f" TP={tp} PP={pp} EP={ep} ETP={etp} dtype={torch_dtype}")
bridge = AutoBridge.from_hf_pretrained(
hf_model,
trust_remote_code=is_safe_repo(trust_remote_code=trust_remote_code, hf_path=hf_model),
torch_dtype=dtype,
)
model_provider = bridge.to_megatron_provider(load_weights=True)
model_provider.tensor_model_parallel_size = tp
model_provider.pipeline_model_parallel_size = pp
model_provider.expert_model_parallel_size = ep
model_provider.expert_tensor_parallel_size = etp
model_provider.pipeline_dtype = dtype
model_provider.params_dtype = dtype
# Auto-generate pipeline layout for models that need it (e.g. DSv4 hash MoE)
if pp > 1 and hasattr(bridge._model_bridge, "generate_pipeline_layout"):
hf_config = bridge.hf_pretrained.config
num_layers = hf_config.num_hidden_layers
mtp = getattr(hf_config, "num_nextn_predict_layers", 0) or 0
model_provider.pipeline_model_parallel_layout = bridge._model_bridge.generate_pipeline_layout(
num_layers, pp, mtp
)
print_rank_0(f" Auto-generated pipeline layout for PP={pp} ({num_layers} layers, {mtp} MTP)")
model_provider.finalize()
model_provider.initialize_model_parallel(seed=0)
megatron_model = model_provider.provide_distributed_model(wrap_with_ddp=False)
hf_tokenizer_kwargs = {}
if hasattr(bridge._model_bridge, "get_hf_tokenizer_kwargs"):
hf_tokenizer_kwargs = bridge._model_bridge.get_hf_tokenizer_kwargs() or {}
if trust_remote_code:
hf_tokenizer_kwargs["trust_remote_code"] = True
print_rank_0(f"Saving Megatron checkpoint to: {megatron_path}")
bridge.save_megatron_model(
megatron_model,
megatron_path,
hf_tokenizer_path=hf_model,
hf_tokenizer_kwargs=hf_tokenizer_kwargs,
)
print_rank_0(f"Import complete: {megatron_path}")
@torchrun_main
def export_megatron_to_hf(
hf_model: str,
megatron_path: str,
hf_path: str,
tp: int = 1,
pp: int = 1,
ep: int = 1,
etp: int = 1,
torch_dtype: str = "bfloat16",
trust_remote_code: bool = False,
strict: bool = False,
show_progress: bool = True,
distributed_save: bool = False,
save_every_n_ranks: int = 1,
distributed_timeout_minutes: int | None = None,
) -> None:
"""Export a distributed Megatron checkpoint to HuggingFace format."""
_ensure_distributed_initialized(distributed_timeout_minutes)
dtype = _parse_dtype(torch_dtype)
print_rank_0(f"Exporting: {megatron_path} -> {hf_path}")
print_rank_0(f" TP={tp} PP={pp} EP={ep} ETP={etp} dtype={torch_dtype}")
print_rank_0(f" distributed_save={distributed_save} save_every_n_ranks={save_every_n_ranks}")
bridge = AutoBridge.from_hf_pretrained(
hf_model,
trust_remote_code=is_safe_repo(trust_remote_code=trust_remote_code, hf_path=hf_model),
torch_dtype=dtype,
)
model_provider = bridge.to_megatron_provider(load_weights=False)
model_provider.tensor_model_parallel_size = tp
model_provider.pipeline_model_parallel_size = pp
model_provider.expert_model_parallel_size = ep
model_provider.expert_tensor_parallel_size = etp
model_provider.pipeline_dtype = dtype
model_provider.params_dtype = dtype
# For PP > 1 export, read pipeline layout from checkpoint
if pp > 1:
from pathlib import Path
import yaml
ckpt_path = Path(megatron_path)
for candidate in [ckpt_path, *ckpt_path.glob("iter_*")]:
rc = candidate / "run_config.yaml"
if rc.exists():
with open(rc) as f:
cfg = yaml.safe_load(f)
saved_layout = cfg.get("model", {}).get("pipeline_model_parallel_layout")
if isinstance(saved_layout, list):
model_provider.pipeline_model_parallel_layout = saved_layout
print_rank_0(f" Read pipeline layout from checkpoint ({len(saved_layout)} stages)")
break
# run_config.yaml serializes an already-finalized layout as an object stub
# (the layout data is lost), so fall back to auto-generation like import does.
if not isinstance(getattr(model_provider, "pipeline_model_parallel_layout", None), list) and hasattr(
bridge._model_bridge, "generate_pipeline_layout"
):
hf_config = bridge.hf_pretrained.config
num_layers = hf_config.num_hidden_layers
mtp = getattr(hf_config, "num_nextn_predict_layers", 0) or 0
model_provider.pipeline_model_parallel_layout = bridge._model_bridge.generate_pipeline_layout(
num_layers, pp, mtp
)
print_rank_0(f" Auto-generated pipeline layout for PP={pp} ({num_layers} layers, {mtp} MTP)")
# Capture before finalize() turns the list into a PipelineParallelLayerLayout.
resolved_pp_layout = model_provider.pipeline_model_parallel_layout if pp > 1 else None
model_provider.finalize()
model_provider.initialize_model_parallel(seed=0)
mp_overrides = {
"tensor_model_parallel_size": tp,
"pipeline_model_parallel_size": pp,
"expert_model_parallel_size": ep,
"expert_tensor_parallel_size": etp,
"pipeline_dtype": dtype,
"params_dtype": dtype,
}
# load_megatron_model rebuilds the provider from the checkpoint's run_config,
# which lost the layout the same way — forward the resolved layout explicitly.
if isinstance(resolved_pp_layout, list):
mp_overrides["pipeline_model_parallel_layout"] = resolved_pp_layout
print_rank_0(f"Loading Megatron checkpoint from: {megatron_path}")
megatron_model = bridge.load_megatron_model(
megatron_path,
mp_overrides=mp_overrides,
wrap_with_ddp=False,
)
print_rank_0(f"Saving HuggingFace model to: {hf_path}")
bridge.save_hf_pretrained(
megatron_model,
hf_path,
show_progress=show_progress,
strict=strict,
distributed_save=distributed_save,
save_every_n_ranks=save_every_n_ranks,
)
print_rank_0(f"Export complete: {hf_path}")
def _add_common_args(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--hf-model", required=True, help="HuggingFace model ID or local path")
parser.add_argument("--tp", type=int, default=1, help="Tensor parallelism size")
parser.add_argument("--pp", type=int, default=1, help="Pipeline parallelism size")
parser.add_argument("--ep", type=int, default=1, help="Expert parallelism size")
parser.add_argument("--etp", type=int, default=1, help="Expert tensor parallelism size")
parser.add_argument(
"--torch-dtype",
choices=list(DTYPE_MAP),
default="bfloat16",
help="Model precision (default: bfloat16)",
)
parser.add_argument("--trust-remote-code", action="store_true", help="Allow custom model code execution")
parser.add_argument(
"--distributed-timeout-minutes",
type=int,
default=None,
help="Initialize the distributed process group with this timeout before model setup",
)
def main():
"""Parse CLI arguments and dispatch to import or export."""
parser = argparse.ArgumentParser(
description="Multi-GPU checkpoint conversion between HuggingFace and Megatron formats",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subparsers = parser.add_subparsers(dest="command", help="Conversion direction")
# Import: HF -> Megatron
import_parser = subparsers.add_parser("import", help="Import HuggingFace model to distributed Megatron checkpoint")
_add_common_args(import_parser)
import_parser.add_argument("--megatron-path", required=True, help="Directory to save the Megatron checkpoint")
# Export: Megatron -> HF
export_parser = subparsers.add_parser(
"export", help="Export distributed Megatron checkpoint to HuggingFace format"
)
_add_common_args(export_parser)
export_parser.add_argument("--megatron-path", required=True, help="Directory containing the Megatron checkpoint")
export_parser.add_argument("--hf-path", required=True, help="Directory to save the HuggingFace model")
export_parser.add_argument("--no-progress", action="store_true", help="Disable progress bar")
export_parser.add_argument(
"--not-strict", action="store_true", help="Allow source and target to have different keys"
)
export_parser.add_argument(
"--distributed-save",
action="store_true",
help="Each rank saves its assigned shards independently (reduces rank-0 memory pressure)",
)
export_parser.add_argument(
"--save-every-n-ranks",
type=int,
default=1,
help="Only every N-th rank writes files (reduces I/O, only with --distributed-save)",
)
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(1)
if args.command == "import":
import_hf_to_megatron(
hf_model=args.hf_model,
megatron_path=args.megatron_path,
tp=args.tp,
pp=args.pp,
ep=args.ep,
etp=args.etp,
torch_dtype=args.torch_dtype,
trust_remote_code=args.trust_remote_code,
distributed_timeout_minutes=args.distributed_timeout_minutes,
)
elif args.command == "export":
export_megatron_to_hf(
hf_model=args.hf_model,
megatron_path=args.megatron_path,
hf_path=args.hf_path,
tp=args.tp,
pp=args.pp,
ep=args.ep,
etp=args.etp,
torch_dtype=args.torch_dtype,
trust_remote_code=args.trust_remote_code,
strict=not args.not_strict,
show_progress=not args.no_progress,
distributed_save=args.distributed_save,
save_every_n_ranks=args.save_every_n_ranks,
distributed_timeout_minutes=args.distributed_timeout_minutes,
)
if __name__ == "__main__":
main()