|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Unified Qwen3.5 HF <-> Megatron checkpoint converter. |
| 3 | +
|
| 4 | +Supports both dense and MoE models. Model type is auto-detected from the |
| 5 | +input checkpoint / YAML; users do not need to specify it. |
| 6 | +
|
| 7 | +Examples: |
| 8 | + # HF -> Megatron |
| 9 | + python convert_qwen35.py --direction hf2meg \ |
| 10 | + --hf-path /path/to/hf \ |
| 11 | + --meg-path /path/to/output \ |
| 12 | + --yaml /path/to/train.yaml \ |
| 13 | + [--ref-path /path/to/ref] |
| 14 | +
|
| 15 | + # Megatron -> HF |
| 16 | + python convert_qwen35.py --direction meg2hf \ |
| 17 | + --meg-path /path/to/meg/checkpoint \ |
| 18 | + --hf-path /path/to/output \ |
| 19 | + --yaml /path/to/train.yaml \ |
| 20 | + [--ref-path /path/to/hf/ref] |
| 21 | +""" |
| 22 | + |
| 23 | +import argparse |
| 24 | +import sys |
| 25 | + |
| 26 | +from qwen35.config import Config, detect_model_type |
| 27 | +from qwen35.constants import LN_ADJUSTMENT |
| 28 | +from qwen35.converter import DenseConverter, MoEConverter |
| 29 | + |
| 30 | + |
| 31 | +def parse_args(): |
| 32 | + p = argparse.ArgumentParser(description="Unified Qwen3.5 checkpoint converter") |
| 33 | + p.add_argument( |
| 34 | + "--direction", |
| 35 | + required=True, |
| 36 | + choices=["hf2meg", "meg2hf"], |
| 37 | + help="Conversion direction", |
| 38 | + ) |
| 39 | + p.add_argument("--hf-path", required=True, help="Path to HF checkpoint directory") |
| 40 | + p.add_argument( |
| 41 | + "--meg-path", |
| 42 | + required=True, |
| 43 | + help="For hf2meg: output Megatron checkpoint directory. " |
| 44 | + "For meg2hf: input Megatron checkpoint directory.", |
| 45 | + ) |
| 46 | + p.add_argument( |
| 47 | + "--yaml", |
| 48 | + required=True, |
| 49 | + help="Path to training YAML config (provides TP/PP/EP and model shapes)", |
| 50 | + ) |
| 51 | + p.add_argument( |
| 52 | + "--ref-path", |
| 53 | + default=None, |
| 54 | + help="Reference checkpoint path for validation (Megatron ref for hf2meg, " |
| 55 | + "HF ref for meg2hf).", |
| 56 | + ) |
| 57 | + p.add_argument( |
| 58 | + "--adjust-embedding", |
| 59 | + action="store_true", |
| 60 | + help="Adjust embedding vocab size to match reference checkpoint (hf2meg only)", |
| 61 | + ) |
| 62 | + p.add_argument( |
| 63 | + "--adjust-ln", |
| 64 | + action="store_true", |
| 65 | + help="Enable legacy layer norm adjustment (add/subtract 1.0). " |
| 66 | + "Only use this if the model stores raw gamma values instead of " |
| 67 | + "zero-centered weights (default: disabled for Qwen3.5).", |
| 68 | + ) |
| 69 | + p.add_argument( |
| 70 | + "--no-adjust-ln", |
| 71 | + action="store_true", |
| 72 | + help=argparse.SUPPRESS, |
| 73 | + ) |
| 74 | + p.add_argument( |
| 75 | + "--tp", |
| 76 | + type=int, |
| 77 | + default=None, |
| 78 | + help="Override tensor model parallel size from YAML", |
| 79 | + ) |
| 80 | + p.add_argument( |
| 81 | + "--pp", |
| 82 | + type=int, |
| 83 | + default=None, |
| 84 | + help="Override pipeline model parallel size from YAML", |
| 85 | + ) |
| 86 | + p.add_argument( |
| 87 | + "--ep", |
| 88 | + type=int, |
| 89 | + default=None, |
| 90 | + help="Override expert model parallel size from YAML (MoE only)", |
| 91 | + ) |
| 92 | + return p.parse_args() |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + args = parse_args() |
| 97 | + |
| 98 | + # Apply CLI override for layer norm adjustment |
| 99 | + if args.adjust_ln: |
| 100 | + import qwen35.constants |
| 101 | + |
| 102 | + qwen35.constants.LN_ADJUSTMENT = True |
| 103 | + if args.no_adjust_ln: |
| 104 | + import qwen35.constants |
| 105 | + |
| 106 | + qwen35.constants.LN_ADJUSTMENT = False |
| 107 | + |
| 108 | + cfg = Config(args.yaml) |
| 109 | + if args.tp is not None: |
| 110 | + cfg.tp = args.tp |
| 111 | + if args.pp is not None: |
| 112 | + cfg.pp = args.pp |
| 113 | + if args.ep is not None: |
| 114 | + cfg.ep = args.ep |
| 115 | + |
| 116 | + # Auto-detect model type from whichever input is available |
| 117 | + hf_input = args.hf_path if args.direction == "hf2meg" else None |
| 118 | + meg_input = args.meg_path if args.direction == "meg2hf" else None |
| 119 | + model_type = detect_model_type( |
| 120 | + hf_dir=hf_input, |
| 121 | + meg_dir=meg_input, |
| 122 | + yaml_path=args.yaml, |
| 123 | + ) |
| 124 | + converter_cls = MoEConverter if model_type == "moe" else DenseConverter |
| 125 | + converter = converter_cls(cfg, adjust_embedding=args.adjust_embedding) |
| 126 | + |
| 127 | + print(f"Direction: {args.direction}") |
| 128 | + print(f"Model type: {model_type}") |
| 129 | + print(f"TP={cfg.tp}, PP={cfg.pp}, EP={cfg.ep}") |
| 130 | + print(f"Layers={cfg.num_layers}, hidden={cfg.hidden_size}") |
| 131 | + print(f"LN adjustment: {LN_ADJUSTMENT}") |
| 132 | + |
| 133 | + if args.direction == "hf2meg": |
| 134 | + success = converter.run_hf2meg(args.hf_path, args.meg_path, args.ref_path) |
| 135 | + else: |
| 136 | + success = converter.run_meg2hf(args.meg_path, args.hf_path, args.ref_path) |
| 137 | + |
| 138 | + sys.exit(0 if success else 1) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + main() |
0 commit comments