Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7929d39
memory optimizations for Nemotron12B 12k seqlen DPO training
ybgao-nvidia Aug 14, 2025
02bee2a
implement suggested changes
ybgao-nvidia Aug 18, 2025
fb8c1bb
add copyright
ybgao-nvidia Aug 18, 2025
87f858b
make lint pass
ybgao-nvidia Aug 18, 2025
b637b03
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 18, 2025
00d5d9a
update configuration key and README
ybgao-nvidia Aug 19, 2025
9be4a2c
fix allocator setting
ybgao-nvidia Aug 19, 2025
63a82de
update readme and lint
ybgao-nvidia Aug 19, 2025
a2cdc5a
disable expandable segments by default
ybgao-nvidia Aug 20, 2025
1a38935
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 20, 2025
34124c8
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 20, 2025
d3c9ad7
Update README.md
ybgao-nvidia Aug 20, 2025
ae128cc
remove configure_expandable_segments
ybgao-nvidia Aug 20, 2025
caaa87f
Update README.md
ybgao-nvidia Aug 20, 2025
60e2909
fix config schema
ybgao-nvidia Aug 20, 2025
0b2164a
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 20, 2025
35918c7
add test script
ybgao-nvidia Aug 21, 2025
a94b4fb
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 21, 2025
e1d0447
will tests pass now?
ybgao-nvidia Aug 21, 2025
51a2607
make tests pass
ybgao-nvidia Aug 23, 2025
e7f2b26
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 24, 2025
28cae68
include field in logger config
ybgao-nvidia Aug 25, 2025
5562dac
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 25, 2025
38aca9c
remove expandable segments from v2
ybgao-nvidia Aug 25, 2025
189868b
please pass :(
ybgao-nvidia Aug 25, 2025
7573f6d
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 26, 2025
3271a08
empty cache
ybgao-nvidia Aug 26, 2025
b97abd2
Merge branch 'main' into ybgao/aug13-dpo-12k-memory
ybgao-nvidia Aug 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions examples/configs/dpo_nemotron12b.yaml
Comment thread
ybgao-nvidia marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# DPO Algorithm Configuration
dpo:
max_num_epochs: 1
max_num_steps: 100
val_period: 10
val_batches: 1
val_global_batch_size: 16
val_micro_batch_size: 1
val_at_start: true
seed: 42

reference_policy_kl_penalty: 0.1
preference_average_log_probs: False # whether normalizing log probs according to the sequence length in preference_loss
sft_average_log_probs: ${.preference_average_log_probs} # whether normalizing log probs according to the sequence length in sft_loss

preference_loss_weight: 1 # the coefficient of the preference loss
sft_loss_weight: 0 # the coefficient of the SFT loss

checkpointing:
enabled: true
checkpoint_dir: "results/dpo"
Comment thread
ybgao-nvidia marked this conversation as resolved.
Outdated
metric_name: "val_loss"
higher_is_better: false
keep_top_k: null
save_period: 50

policy:
model_name: "mistralai/Mistral-Nemo-Instruct-2407"
tokenizer:
name: ${policy.model_name}

# number of preference samples per batch
# each preference sample corresponds to a pair of chosen and rejected responses
# so the actual batch size processed by the model is train_global_batch_size * 2
train_global_batch_size: 8
train_micro_batch_size: 1


#logprob_batch_size: ${policy.train_micro_batch_size}
max_total_sequence_length: 12288
precision: "bfloat16"
fsdp_offload_enabled: false

dtensor_cfg:
enabled: true
cpu_offload: false
sequence_parallel: false
activation_checkpointing: true
tensor_parallel_size: 8
context_parallel_size: 1
custom_parallel_plan: null

dynamic_batching:
enabled: false

sequence_packing:
enabled: false

# makes the training sequence length divisible by the tensor parallel size
# this is useful for sequence parallel training
make_sequence_length_divisible_by: ${policy.dtensor_cfg.tensor_parallel_size}
max_grad_norm: 1.0

optimizer:
name: "torch.optim.AdamW"
kwargs:
lr: 1.0e-6
weight_decay: 0.01
betas: [0.9, 0.999]
eps: 1e-8
# when using Dtensor, we need to set foreach
# and fused to False
foreach: False
fused: False

scheduler:
- name: "torch.optim.lr_scheduler.ConstantLR"
kwargs:
factor: 1.0
total_iters: 10000000000
- milestones: []

data:
dataset_name: "HelpSteer3"
max_input_seq_length: ${policy.max_total_sequence_length}
logger:
log_dir: "logs" # Base directory for all logs
Comment thread
ybgao-nvidia marked this conversation as resolved.
Outdated
wandb_enabled: true # Make sure you do a ``wandb login [Your API key]'' before running
tensorboard_enabled: false
mlflow_enabled: false
monitor_gpus: true # If true, will monitor GPU usage and log to wandb and/or tensorboard
wandb:
project: "ybgao-aug7-dpo-oom-test"
name: "baseline"
Comment thread
ybgao-nvidia marked this conversation as resolved.
Outdated
gpu_monitoring:
collection_interval: 10 # How often to collect GPU usage metrics (in seconds)
flush_interval: 10 # How often to flush GPU usage metrics to the loggers (in seconds)

cluster:
gpus_per_node: 8
num_nodes: 1
20 changes: 20 additions & 0 deletions nemo_rl/models/dtensor/parallelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,26 @@ def _parallelize_model(
for i in range(len(layers)):
layers[i].mlp = checkpoint_wrapper(layers[i].mlp) # type: ignore

"""
the extra memory overhead for layer norm seems to be only present
in mistral models, where some intermediate state is converted to float32

need to find a better solution for checkpointing
"""
if "self_attn" in layers[i].__dict__:
layers[i].self_attn = checkpoint_wrapper(layers[i].self_attn) # type: ignore

if (
"input_layernorm" in layers[i].__dict__
and "post_attention_layernorm" in layers[i].__dict__
):
Comment thread
ybgao-nvidia marked this conversation as resolved.
Outdated
layers[i].input_layernorm = checkpoint_wrapper(
layers[i].input_layernorm # type: ignore
)
layers[i].post_attention_layernorm = checkpoint_wrapper(
layers[i].post_attention_layernorm # type: ignore
)

mp_policy = MixedPrecisionPolicy(
param_dtype=param_dtype,
reduce_dtype=torch.float32,
Expand Down
2 changes: 2 additions & 0 deletions nemo_rl/models/policy/dtensor_policy_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ def train(
for mb_idx, mb in enumerate(
itertools.chain(mb_iterator, dummy_iterator)
):
torch.cuda.empty_cache()

with torch.autocast(device_type="cuda", dtype=self.dtype):
if self.enable_seq_packing:
input_ids = mb.get("input_ids").cuda()
Expand Down
3 changes: 3 additions & 0 deletions nemo_rl/models/policy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def configure_expandable_segments() -> None:
# Add expandable_segments to existing configuration
if existing_conf:
# Append to existing configuration

# max_split_size_mb:64 reduces fragmentation, enables 12B models
Comment thread
wangshangsam marked this conversation as resolved.
Outdated
# with 12K context to fit on a single node
Comment thread
ybgao-nvidia marked this conversation as resolved.
Outdated
new_conf = f"{existing_conf},expandable_segments:True"
else:
# Set new configuration
Expand Down