|
19 | 19 | This script demonstrates how to convert models between HuggingFace and Megatron formats |
20 | 20 | using the AutoBridge import_ckpt and export_ckpt methods. |
21 | 21 |
|
22 | | -Features: |
23 | | -- Import HuggingFace models to Megatron checkpoint format |
24 | | -- Export Megatron checkpoints to HuggingFace format |
25 | | -- Support for various model architectures (GPT, Llama, etc.) |
26 | | -- Configurable model and conversion settings |
27 | | -
|
28 | 22 | Usage examples: |
| 23 | + # Download the HF checkpoint locally |
| 24 | + huggingface-cli download Wan-AI/Wan2.1-T2V-1.3B-Diffusers \ |
| 25 | + --local-dir /root/.cache/huggingface/wan2.1 \ |
| 26 | + --local-dir-use-symlinks False |
| 27 | +
|
29 | 28 | # Import a HuggingFace model to Megatron format |
30 | | - python examples/conversion/convert_checkpoints.py import \ |
31 | | - --hf-model meta-llama/Llama-3.2-1B \ |
32 | | - --megatron-path ./checkpoints/llama3_2_1b |
| 29 | + python examples/megatron/recipes/wan/conversion/convert_checkpoints.py import \ |
| 30 | + --hf-model /root/.cache/huggingface/wan2.1 \ |
| 31 | + --megatron-path /workspace/checkpoints/megatron_checkpoints/wan_1_3b |
33 | 32 |
|
34 | 33 | # Export a Megatron checkpoint to HuggingFace format |
35 | | - python examples/conversion/convert_checkpoints.py export \ |
36 | | - --hf-model meta-llama/Llama-3.2-1B \ |
37 | | - --megatron-path ./checkpoints/llama3_2_1b \ |
38 | | - --hf-path ./exports/llama3_2_1b_hf |
39 | | -
|
40 | | - # Import with custom settings |
41 | | - python examples/conversion/convert_checkpoints.py import \ |
42 | | - --hf-model ./local_model \ |
43 | | - --megatron-path ./checkpoints/custom_model \ |
44 | | - --torch-dtype bfloat16 \ |
45 | | - --device-map auto |
46 | | -
|
47 | | - # Export without progress bar (useful for scripting) |
48 | | - python examples/conversion/convert_checkpoints.py export \ |
49 | | - --hf-model ./local_model \ |
50 | | - --megatron-path ./checkpoints/custom_model \ |
51 | | - --hf-path ./exports/custom_model_hf \ |
52 | | - --no-progress |
| 34 | + python examples/megatron/recipes/wan/conversion/convert_checkpoints.py export \ |
| 35 | + --hf-model /root/.cache/huggingface/wan2.1 \ |
| 36 | + --megatron-path /workspace/checkpoints/megatron_checkpoints/wan_1_3b/iter_0000000 \ |
| 37 | + --hf-path /workspace/checkpoints/hf_checkpoints/wan_1_3b_hf |
| 38 | +
|
| 39 | + NOTE: The converted checkpoint /workspace/checkpoints/hf_checkpoints/wan_1_3b_hf |
| 40 | + only contains the DiT model transformer weights. You still need other components in |
| 41 | + the diffusion pipeline (VAE, text encoders, etc.) to run inference. To do so, you can |
| 42 | + duplicate the original HF checkpoint directory /root/.cache/huggingface/wan2.1 (which |
| 43 | + contains VAE, text encoders, etc.), and replace ./transformer with |
| 44 | + /workspace/checkpoints/hf_checkpoints/wan_1_3b_hf/transformer. |
| 45 | +
|
53 | 46 | """ |
54 | 47 |
|
55 | 48 | import argparse |
| 49 | +import os |
| 50 | +import random |
56 | 51 | import sys |
57 | 52 | from pathlib import Path |
58 | 53 | from typing import Optional |
59 | 54 |
|
60 | 55 | import torch |
61 | | - |
62 | 56 | from megatron.bridge import AutoBridge |
63 | 57 | from megatron.bridge.models.hf_pretrained.wan import PreTrainedWAN |
64 | 58 | from megatron.bridge.models.wan.wan_bridge import WanBridge |
65 | | -from megatron.bridge.training.model_load_save import save_megatron_model |
66 | | -from megatron.bridge.training.model_load_save import load_megatron_model, temporary_distributed_context |
67 | | -import os |
68 | | -import random |
| 59 | +from megatron.bridge.training.model_load_save import ( |
| 60 | + load_megatron_model, |
| 61 | + save_megatron_model, |
| 62 | + temporary_distributed_context, |
| 63 | +) |
69 | 64 |
|
70 | 65 |
|
71 | 66 | def validate_path(path: str, must_exist: bool = False) -> Path: |
|
0 commit comments