-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
nemotron3_22b_64k.py
171 lines (153 loc) · 6.51 KB
/
nemotron3_22b_64k.py
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
# Copyright (c) 2024, 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.
from typing import Optional
import nemo_run as run
import pytorch_lightning as pl
import torch
from nemo.collections.llm.api import pretrain
from nemo.collections.llm.gpt.data.mock import MockDataModule
from nemo.collections.llm.recipes.log.default import default_log, default_resume, tensorboard_logger
from nemo.collections.llm.recipes.nemotron import nemotron_model, nemotron_trainer
from nemo.collections.llm.recipes.optim.adam import distributed_fused_adam_with_cosine_annealing
from nemo.utils.exp_manager import TimingCallback
NAME = "nemotron3_22b_64k"
@run.cli.factory(name=NAME)
def model() -> run.Config[pl.LightningModule]:
"""
Factory function to create a Nemotron3 22B model with 64k sequence length.
Returns:
run.Config[pl.LightningModule]: Configuration for the Nemotron3 22b and 64k sequence length model.
Examples:
CLI usage:
$ nemo llm pretrain model=nemotron3_22b_64k ...
Python API usage:
>>> model_config = model()
>>> print(model_config)
"""
return nemotron_model(version=NAME)
@run.cli.factory(target=pretrain, name=NAME)
def pretrain_recipe(
# General
dir: Optional[str] = None,
name: str = "default",
# Trainer
tensor_parallelism: int = 4,
pipeline_parallelism: int = 2,
pipeline_parallelism_type: Optional[torch.dtype] = torch.bfloat16,
virtual_pipeline_parallelism: Optional[int] = None,
context_parallelism: int = 4,
sequence_parallelism: bool = True,
num_nodes: int = 4,
num_gpus_per_node: int = 8,
max_steps: int = 300000,
precision: str = "bf16-mixed",
accumulate_grad_batches: int = 1,
gradient_clip_val: float = 1.0,
limit_test_batches: int = 32,
limit_val_batches: int = 32,
log_every_n_steps: int = 10,
val_check_interval: int = 2000,
# Data
global_batch_size=32,
micro_batch_size=1,
seq_length=65536,
# Optimizer
warmup_steps=500,
constant_steps=0,
min_lr=1e-5,
max_lr=1e-4,
# Training function
fn=pretrain,
) -> run.Partial:
"""
Create a pre-training recipe for Nemotron3 22B model with 16k sequence length.
This function sets up a complete configuration for pre-training, including
model, trainer, data, logging, optimization, and resumption settings.
Args:
dir (Optional[str]): Directory for saving logs and checkpoints.
name (str): Name of the pre-training run.
tensor_parallelism (int): Degree of tensor model parallelism.
pipeline_parallelism (int): Degree of pipeline model parallelism.
pipeline_parallelism_type (Optional[torch.dtype]): Data type for pipeline parallelism.
virtual_pipeline_parallelism (Optional[int]): Size of virtual pipeline parallelism.
context_parallelism (int): Degree of context parallelism.
sequence_parallelism (bool): Whether to use sequence parallelism.
num_nodes (int): Number of compute nodes to use.
num_gpus_per_node (int): Number of GPUs per node.
max_steps (int): Maximum number of training steps.
precision (str): Precision configuration, one of fp32, 16-mixed or bf16-mixed.
accumulate_grad_batches (int): Number of steps per gradient accumulation.
gradient_clip_val (float): Value for gradient clipping.
limit_test_batches (int): Limit the number of test batches.
limit_val_batches (int): Limit the number of validation batches.
log_every_n_steps (int): Log every n steps.
val_check_interval (int): Run validation every N steps.
global_batch_size (int): Global batch size.
micro_batch_size (int): Micro batch size.
seq_length (int): Sequence length.
warmup_steps (int): Number of warmup steps.
constant_steps (int): Number of constant steps.
min_lr (float): Minimum learning rate.
max_lr (float): Maximum learning rate.
fn (Callable): The pre-training function to use.
Returns:
run.Partial: Partial configuration for pre-training.
Examples:
CLI usage:
$ nemo llm pretrain --factory nemotron3_22b_64k
$ nemo llm pretrain --factory "nemotron3_22b_64k(num_nodes=2, name='my_nemotron_pretrain')"
Python API usage:
>>> recipe = pretrain_recipe(name="nemotron_pretrain", num_nodes=2)
>>> print(recipe)
Note:
This recipe uses a mock dataset, look for the finetune examples to see how to change the dataset.
"""
return run.Partial(
fn,
model=model(),
trainer=nemotron_trainer(
tensor_parallelism=tensor_parallelism,
pipeline_parallelism=pipeline_parallelism,
pipeline_parallelism_type=pipeline_parallelism_type,
virtual_pipeline_parallelism=virtual_pipeline_parallelism,
context_parallelism=context_parallelism,
sequence_parallelism=sequence_parallelism,
num_nodes=num_nodes,
num_gpus_per_node=num_gpus_per_node,
max_steps=max_steps,
precision=precision,
accumulate_grad_batches=accumulate_grad_batches,
limit_test_batches=limit_test_batches,
limit_val_batches=limit_val_batches,
log_every_n_steps=log_every_n_steps,
val_check_interval=val_check_interval,
callbacks=[run.Config(TimingCallback)],
),
data=run.Config(
MockDataModule,
seq_length=seq_length,
global_batch_size=global_batch_size,
micro_batch_size=micro_batch_size,
),
log=default_log(dir=dir, name=name, tensorboard_logger=tensorboard_logger(name=name)),
optim=distributed_fused_adam_with_cosine_annealing(
precision=precision,
warmup_steps=warmup_steps,
constant_steps=constant_steps,
min_lr=min_lr,
max_lr=max_lr,
clip_grad=gradient_clip_val,
),
resume=default_resume(),
)