Skip to content

Commit

Permalink
R1-Style distributed GRPO (#2326)
Browse files Browse the repository at this point in the history
Co-authored-by: Felipe Mello <fmellomascarenhas@gmail.com>
Co-authored-by: ebsmothers <ebs@meta.com>
Co-authored-by: salman <salman.mohammadi@outlook.com>
  • Loading branch information
4 people authored Feb 21, 2025
1 parent 952078e commit cf0142b
Show file tree
Hide file tree
Showing 21 changed files with 2,425 additions and 2 deletions.
140 changes: 140 additions & 0 deletions recipes/configs/dev/3B_full_grpo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Config for multi-node GRPO in dev/grpo_full_finetune_distributed.py
# using a Llama3.2 3B Base model
#
# This config assumes that you've run the following command before launching
# this run:
# tune download meta-llama/Llama-3.2-3B --output-dir /tmp/Llama-3.2-3B --ignore-patterns "original/consolidated.00.pth"
#
# It can be beneficial to first train the base model with SFT using the 3B_sft recipe.
#
# To launch on 4 devices, run the following command from root:
# tune run --nproc_per_node 4 dev/grpo_full_finetune_distributed --config dev/3B_full_grpo
#
# You can add specific overrides through the command line. For example
# to override the checkpointer directory while launching training
# you can run:
# tune run --nproc_per_node 4 dev/grpo_full_finetune_distributed --config dev/grpo/3B_full_rl checkpointer.checkpoint_dir=<YOUR_CHECKPOINT_DIR>
#
# This config works best when the model is being fine-tuned on 2+ GPUs.
#
# Furthermore, you can launch it on multiple nodes by going to recipes/dev/ and using
# sbatch multinode_grpo.sbatch

name: grpo_llama3b

output_dir: /tmp/checkpoints/${name}
base_model_path: /tmp/llama3B_gsm8k_sft_part0/epoch_0 # Use this to train from the slightly trained SFT model

# Tokenizer
tokenizer:
_component_: torchtune.models.llama3.llama3_tokenizer
path: /tmp/Llama-3.2-3B/original/tokenizer.model
max_seq_len: null

# Dataset
dataset:
_component_: torchtune.dev.grpo.gsm8k.gsm8k_dataset
partition: 1-9/10
seed: null
shuffle: False

# Model Arguments
model:
_component_: torchtune.models.llama3_2.llama3_2_3b

checkpointer:
_component_: torchtune.training.FullModelHFCheckpointer
checkpoint_dir: ${base_model_path}
checkpoint_files: [
model-00001-of-00002.safetensors,
model-00002-of-00002.safetensors,
]
recipe_checkpoint: null
output_dir: ${output_dir}
model_type: LLAMA3


ref_checkpointer:
_component_: torchtune.training.FullModelHFCheckpointer
checkpoint_dir: ${base_model_path}
checkpoint_files: [
model-00001-of-00002.safetensors,
model-00002-of-00002.safetensors,
]
recipe_checkpoint: null
output_dir: ${output_dir}/ref # shouldn't be used?
model_type: LLAMA3


resume_from_checkpoint: False
save_every_n_epochs: 1

# Fine-tuning arguments
batch_size: 1
grpo_samples: 16
forward_batch_size: 1
max_generated_tokens: 512
top_k: null
temperature: 1.0

ppo_epochs: 1

num_steps: 200

clip_grad_norm: 1.0

epochs: 10
optimizer:
_component_: torch.optim.AdamW
lr: 1e-5
fused: True
lr_scheduler:
_component_: torchtune.training.lr_schedulers.get_cosine_schedule_with_warmup
num_warmup_steps: 50
loss:
_component_: torchtune.dev.grpo.loss.GRPOSimpleLoss
kl_coeff: 0.01
epsilon: 0.2

# Training env
device: cuda

# Memory management
enable_activation_checkpointing: True # True reduces memory
compile: False # pytorch compile, set to true for better perf/memory

# Reduced precision
dtype: bf16


# Logging
metric_logger:
_component_: torchtune.training.metric_logging.DiskLogger
log_dir: ${output_dir}/logs
log_every_n_steps: 1
log_peak_memory_stats: True

# Profiler (disabled)
profiler:
_component_: torchtune.training.setup_torch_profiler
enabled: True

#Output directory of trace artifacts
output_dir: ${output_dir}/profiling_outputs

#`torch.profiler.ProfilerActivity` types to trace
cpu: True
cuda: True

#trace options passed to `torch.profiler.profile`
profile_memory: True
with_stack: True
record_shapes: True
with_flops: False

# `torch.profiler.schedule` options:
# wait_steps -> wait, warmup_steps -> warmup, active_steps -> active, num_cycles -> repeat
wait_steps: 5
warmup_steps: 3
active_steps: 2
num_cycles: 1
109 changes: 109 additions & 0 deletions recipes/configs/dev/3B_sft_for_grpo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Config for multi-device SFT for reasoning in full_finetune_distributed.py
# using a Llama3.2 3B Base model
#
# This config assumes that you've run the following command before launching
# this run:
# tune download meta-llama/Meta-Llama-3.2-3B --output-dir /tmp/Meta-Llama-3.2-3B-Instruct --ignore-patterns "original/consolidated.00.pth"
#
# To launch on 4 devices, run the following command from root:
# tune run --nproc_per_node 4 full_finetune_distributed --config dev/3B_grpo_sft
#
# You can add specific overrides through the command line. For example
# to override the checkpointer directory while launching training
# you can run:
# tune run --nproc_per_node 4 full_finetune_distributed --config dev/grpo/3B_sft checkpointer.checkpoint_dir=<YOUR_CHECKPOINT_DIR>
#
# This config works best when the model is being fine-tuned on 2+ GPUs.


name: llama3B_gsm8k_sft_part0

output_dir: /tmp/${name}

# Tokenizer
tokenizer:
_component_: torchtune.models.llama3.llama3_tokenizer
path: /tmp/Llama-3.2-3B/original/tokenizer.model
max_seq_len: null

# Dataset
dataset:
_component_: torchtune.dev.grpo.gsm8k.gsm8k_sft
partition: 0-0/10
seed: null
shuffle: True

# Model Arguments
model:
_component_: torchtune.models.llama3_2.llama3_2_3b

checkpointer:
_component_: torchtune.training.FullModelHFCheckpointer
checkpoint_dir: /tmp/Llama-3.2-3B/
checkpoint_files: [
model-00001-of-00002.safetensors,
model-00002-of-00002.safetensors,
]
recipe_checkpoint: null
output_dir: ${output_dir}
model_type: LLAMA3
resume_from_checkpoint: False

# Fine-tuning arguments
batch_size: 2
epochs: 1

optimizer:
_component_: torch.optim.AdamW
lr: 1e-5
fused: True
loss:
_component_: torchtune.modules.loss.CEWithChunkedOutputLoss
max_steps_per_epoch: null
clip_grad_norm: null
compile: False # torch.compile the model + loss, True increases speed + decreases memory
optimizer_in_bwd: False # True saves memory. Requires gradient_accumulation_steps=1
gradient_accumulation_steps: 1 # Use to increase effective batch size

# Training env
device: cuda

# Memory management
enable_activation_checkpointing: True # True reduces memory
enable_activation_offloading: False # True reduces memory

# Reduced precision
dtype: bf16

# Logging
metric_logger:
_component_: torchtune.training.metric_logging.DiskLogger
log_dir: ${output_dir}/logs
log_every_n_steps: 1
log_peak_memory_stats: True


# Profiler (disabled)
profiler:
_component_: torchtune.training.setup_torch_profiler
enabled: False

#Output directory of trace artifacts
output_dir: ${output_dir}/profiling_outputs

#`torch.profiler.ProfilerActivity` types to trace
cpu: True
cuda: True

#trace options passed to `torch.profiler.profile`
profile_memory: False
with_stack: False
record_shapes: True
with_flops: False

# `torch.profiler.schedule` options:
# wait_steps -> wait, warmup_steps -> warmup, active_steps -> active, num_cycles -> repeat
wait_steps: 5
warmup_steps: 3
active_steps: 2
num_cycles: 1
Loading

0 comments on commit cf0142b

Please sign in to comment.