Skip to content

Commit 903b5fc

Browse files
committed
remove yarn and add entrypoint
Signed-off-by: ruit <ruit@nvidia.com>
1 parent 0b9806a commit 903b5fc

2 files changed

Lines changed: 121 additions & 11 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Long-context (128K) SFT example for Qwen3-600M.
17+
18+
Parallelism: TP=1, CP=8 (8 GPUs minimum)
19+
Sequence length: 131072 (128K tokens)
20+
21+
Usage
22+
-----
23+
Minimal (mock data):
24+
torchrun --nproc_per_node=8 examples/long_contex/finetune_qwen3_600m_128k.py
25+
26+
With a blend dataset (JSON):
27+
torchrun --nproc_per_node=8 examples/long_contex/finetune_qwen3_600m_128k.py \\
28+
--per-split-data-args-path /path/to/data.json
29+
30+
With a pretrained checkpoint:
31+
torchrun --nproc_per_node=8 examples/long_contex/finetune_qwen3_600m_128k.py \\
32+
--per-split-data-args-path /path/to/data.json \\
33+
checkpoint.pretrained_checkpoint=/path/to/pretrained
34+
35+
Additional Hydra-style dot-notation overrides:
36+
torchrun --nproc_per_node=8 examples/long_contex/finetune_qwen3_600m_128k.py \\
37+
--per-split-data-args-path /path/to/data.json \\
38+
train.train_samples=10000 \\
39+
logger.wandb_project=my_project
40+
"""
41+
42+
import argparse
43+
import logging
44+
import os
45+
import sys
46+
from typing import Tuple
47+
48+
import torch
49+
from omegaconf import OmegaConf
50+
51+
from megatron.bridge.recipes.qwen.qwen3 import qwen3_600m_sft_128k_config
52+
from megatron.bridge.training.config import ConfigContainer
53+
from megatron.bridge.training.finetune import finetune
54+
from megatron.bridge.training.gpt_step import forward_step
55+
from megatron.bridge.training.utils.omegaconf_utils import (
56+
apply_overrides,
57+
create_omegaconf_dict_config,
58+
parse_hydra_overrides,
59+
)
60+
61+
62+
logger: logging.Logger = logging.getLogger(__name__)
63+
64+
65+
def parse_cli_args() -> Tuple[argparse.Namespace, list[str]]:
66+
"""Parse command line arguments, separating known script args from OmegaConf overrides."""
67+
parser = argparse.ArgumentParser(
68+
description="Long-context (128K) SFT for Qwen3-600M using Megatron-Bridge",
69+
formatter_class=argparse.RawTextHelpFormatter,
70+
)
71+
parser.add_argument(
72+
"--config-file",
73+
type=str,
74+
help="Path to a YAML OmegaConf override file.",
75+
)
76+
77+
# Remaining args are treated as Hydra-style dot-notation overrides
78+
args, cli_dotlist_overrides = parser.parse_known_args()
79+
return args, cli_dotlist_overrides
80+
81+
82+
def main() -> None:
83+
"""Entry point for Qwen3-600M 128K long-context SFT."""
84+
args, cli_overrides = parse_cli_args()
85+
86+
cfg: ConfigContainer = qwen3_600m_sft_128k_config()
87+
88+
# Convert to OmegaConf DictConfig for merging
89+
merged_omega_conf, excluded_fields = create_omegaconf_dict_config(cfg)
90+
91+
# Load and merge YAML overrides if a config file is provided
92+
if args.config_file:
93+
logger.debug(f"Loading YAML overrides from: {args.config_file}")
94+
if not os.path.exists(args.config_file):
95+
logger.error(f"Override YAML file not found: {args.config_file}")
96+
sys.exit(1)
97+
yaml_overrides_omega = OmegaConf.load(args.config_file)
98+
merged_omega_conf = OmegaConf.merge(merged_omega_conf, yaml_overrides_omega)
99+
logger.debug("YAML overrides merged successfully.")
100+
101+
# Apply command-line Hydra-style overrides
102+
if cli_overrides:
103+
logger.debug(f"Applying Hydra-style command-line overrides: {cli_overrides}")
104+
merged_omega_conf = parse_hydra_overrides(merged_omega_conf, cli_overrides)
105+
logger.debug("Hydra-style command-line overrides applied successfully.")
106+
107+
# Apply the final merged config back to the ConfigContainer
108+
final_overrides_as_dict = OmegaConf.to_container(merged_omega_conf, resolve=True)
109+
apply_overrides(cfg, final_overrides_as_dict, excluded_fields)
110+
111+
# Start training
112+
logger.debug("Starting long-context SFT...")
113+
finetune(config=cfg, forward_step_func=forward_step)
114+
115+
if torch.distributed.is_initialized():
116+
torch.distributed.destroy_process_group()
117+
118+
119+
if __name__ == "__main__":
120+
main()

‎src/megatron/bridge/recipes/qwen/qwen3.py‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -619,19 +619,9 @@ def qwen3_600m_sft_128k_config() -> ConfigContainer:
619619
cfg.dataset.packed_sequence_specs.pad_seq_to_mult = cfg.model.context_parallel_size * 2
620620

621621
# Batch sizes (micro_batch_size must be 1 with packed sequence)
622-
cfg.train.global_batch_size = 8
622+
cfg.train.global_batch_size = 2
623623
cfg.train.micro_batch_size = 1
624624

625-
# YaRN RoPE scaling (factor=3.2 extends 40960 → ~128K context)
626-
cfg.model.position_embedding_type = "yarn"
627-
cfg.model.yarn_rotary_scaling_factor = 3.2
628-
cfg.model.yarn_original_max_position_embeddings = 40960
629-
cfg.model.yarn_beta_fast = 32.0
630-
cfg.model.yarn_beta_slow = 1.0
631-
cfg.model.yarn_mscale = 1.0
632-
cfg.model.yarn_mscale_all_dim = 0.0
633-
cfg.model.yarn_correction_range_round_to_int = True # truncate=true
634-
635625
# Training config
636626
cfg.validation.eval_interval = 30
637627
cfg.train.manual_gc = False

0 commit comments

Comments
 (0)