|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Functional smoke tests for LLaMA checkpointing.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import shutil |
| 19 | +import sys |
| 20 | + |
| 21 | +import pytest |
| 22 | +from torch.distributed.run import main as torchrun_main |
| 23 | + |
| 24 | +from megatron.bridge.recipes.llama import llama32_1b_pretrain_config |
| 25 | +from megatron.bridge.training.gpt_step import forward_step |
| 26 | +from megatron.bridge.training.pretrain import pretrain |
| 27 | + |
| 28 | + |
| 29 | +BASE_DIR = "/workspace/test_ckpts/llama32_1b" |
| 30 | +MBRIDGE_CKPT = f"{BASE_DIR}/mbridge" |
| 31 | +MCORE_CKPT = f"{BASE_DIR}/mcore" |
| 32 | +TB_DIR = f"{BASE_DIR}/tb" |
| 33 | + |
| 34 | + |
| 35 | +class TestLlama32Ckpt: |
| 36 | + """Test class for LLama checkpoint functional tests.""" |
| 37 | + |
| 38 | + @pytest.mark.run_only_on("GPU") |
| 39 | + def test_llama32_1B_ckpt_mbridge(self): |
| 40 | + """Functional test for LLama MBridge checkpoint.""" |
| 41 | + |
| 42 | + config = llama32_1b_pretrain_config() |
| 43 | + |
| 44 | + config.checkpoint.save = MBRIDGE_CKPT |
| 45 | + config.checkpoint.load = MCORE_CKPT if os.path.exists(MCORE_CKPT) else None |
| 46 | + config.checkpoint.load_optim = False |
| 47 | + |
| 48 | + config.model.seq_length = 8192 |
| 49 | + |
| 50 | + config.train.train_iters = 10 if config.checkpoint.load else 5 |
| 51 | + config.train.eval_iters = 5 |
| 52 | + config.train.save_interval = 5 |
| 53 | + config.train.global_batch_size = 8 |
| 54 | + config.train.micro_batch_size = 1 |
| 55 | + |
| 56 | + config.scheduler.lr_warmup_iters = 2 |
| 57 | + |
| 58 | + config.logger.log_interval = 1 |
| 59 | + |
| 60 | + pretrain(config=config, forward_step_func=forward_step) |
| 61 | + |
| 62 | + @pytest.mark.run_only_on("GPU") |
| 63 | + def test_llama32_1B_ckpt_core(self, monkeypatch): |
| 64 | + """Functional test for LLama MCore checkpoint.""" |
| 65 | + |
| 66 | + load_dir = MBRIDGE_CKPT if os.path.exists(MBRIDGE_CKPT) else None |
| 67 | + train_iters = 10 if load_dir else 5 |
| 68 | + |
| 69 | + # Set environment variables |
| 70 | + monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0,1") |
| 71 | + monkeypatch.setenv("CUDA_DEVICE_MAX_CONNECTIONS", "1") |
| 72 | + |
| 73 | + # Set MLM script |
| 74 | + monkeypatch.setattr( |
| 75 | + sys, |
| 76 | + "argv", |
| 77 | + [ |
| 78 | + "torchrun", |
| 79 | + "--nproc-per-node=2", |
| 80 | + "/opt/Megatron-Bridge/3rdparty/Megatron-LM/pretrain_gpt.py", |
| 81 | + "--load", |
| 82 | + "/workspace/test_ckpts/llama32_1b_mbridge", |
| 83 | + "--save", |
| 84 | + "/workspace/test_ckpts/llama32_1b_mcore", |
| 85 | + "--init-method-std", |
| 86 | + "0.014", |
| 87 | + "--disable-bias-linear", |
| 88 | + "--use-rope-scaling", |
| 89 | + "--swiglu", |
| 90 | + "--use-rotary-position-embeddings", |
| 91 | + "--num-layers", |
| 92 | + "16", |
| 93 | + "--hidden-size", |
| 94 | + "2048", |
| 95 | + "--num-attention-heads", |
| 96 | + "32", |
| 97 | + "--ffn-hidden-size", |
| 98 | + "8192", |
| 99 | + "--kv-channels", |
| 100 | + "64", |
| 101 | + "--group-query-attention", |
| 102 | + "--position-embedding-type", |
| 103 | + "rope", |
| 104 | + "--attention-backend", |
| 105 | + "fused", |
| 106 | + "--num-query-groups", |
| 107 | + "8", |
| 108 | + "--normalization", |
| 109 | + "RMSNorm", |
| 110 | + "--attention-dropout", |
| 111 | + "0.0", |
| 112 | + "--hidden-dropout", |
| 113 | + "0.0", |
| 114 | + "--tensor-model-parallel-size", |
| 115 | + "1", |
| 116 | + "--pipeline-model-parallel-size", |
| 117 | + "1", |
| 118 | + "--seq-length", |
| 119 | + "8192", |
| 120 | + "--max-position-embeddings", |
| 121 | + "8192", |
| 122 | + "--micro-batch-size", |
| 123 | + "1", |
| 124 | + "--global-batch-size", |
| 125 | + "8", |
| 126 | + "--mock-data", |
| 127 | + "--tokenizer-type", |
| 128 | + "NullTokenizer", |
| 129 | + "--vocab-size", |
| 130 | + "131072", |
| 131 | + "--train-iters", |
| 132 | + f"{train_iters}", |
| 133 | + "--save-interval", |
| 134 | + "5", |
| 135 | + "--eval-interval", |
| 136 | + "5", |
| 137 | + "--eval-iters", |
| 138 | + "5", |
| 139 | + "--load", |
| 140 | + load_dir, |
| 141 | + "--save", |
| 142 | + MCORE_CKPT, |
| 143 | + "--ckpt-format", |
| 144 | + "torch_dist", |
| 145 | + "--log-progress", |
| 146 | + "--bf16", |
| 147 | + "--lr", |
| 148 | + "4.5e-4", |
| 149 | + "--min-lr", |
| 150 | + "4.5e-5", |
| 151 | + "--num-workers", |
| 152 | + "2", |
| 153 | + "--tensorboard-dir", |
| 154 | + TB_DIR, |
| 155 | + "--log-interval", |
| 156 | + "1", |
| 157 | + "--log-throughput", |
| 158 | + "--no-load-optim", |
| 159 | + ], |
| 160 | + ) |
| 161 | + |
| 162 | + # Run MLM script |
| 163 | + torchrun_main() |
| 164 | + |
| 165 | + def test_remove_artifacts(self): |
| 166 | + """Removes model artifacts""" |
| 167 | + shutil.rmtree(BASE_DIR) |
| 168 | + |
| 169 | + assert not os.path.exists(BASE_DIR) |
0 commit comments