Skip to content

Commit c926b99

Browse files
committed
Fix shape mismatch on tail batch in GRPO training and host CPU OOM
- Fixes shape mismatch issues on tail batches in GRPO training by enabling drop_remainder=True on training and test datasets. - Updates unit tests (train_rl_test.py) to mock configurations compatible with drop_remainder=True and specific batch sizes. - Prevents host CPU OOM by keeping the pool size of math_verify_pool constant. - Includes a template gpt_oss_rl.json for RL chat.
1 parent 7a80e6c commit c926b99

4 files changed

Lines changed: 11 additions & 5 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"SYSTEM_PROMPT": "You are given a problem. Think about the problem and provide your reasoning. Place it between {reasoning_start_token} and {reasoning_end_token}. Then, provide the final answer (i.e., just one numerical value) between {solution_start_token} and {solution_end_token}.",
3+
"TEMPLATE": "<|start|>system<|message|>You are ChatGPT, a large language model trained by OpenAI.\nKnowledge cutoff: 2024-06\nCurrent date: 2026-06-19\n\nReasoning: medium\n\n# Valid channels: analysis, commentary, final. Channel must be included for every message.\nCalls to these tools must go to the commentary channel: 'functions'.<|end|><|start|>user<|message|><start_of_turn>user\n{system_prompt}\n\n{question}<end_of_turn>\n<start_of_turn>model<|end|><|start|>assistant"
4+
}

src/maxtext/trainers/post_train/rl/math_verify_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ def math_verify_pool(
226226

227227
cpu_count = multiprocessing.cpu_count()
228228
if num_procs is None:
229-
num_procs = min(_DEFAULT_MAX_PROCS, len(items), cpu_count)
229+
num_procs = min(_DEFAULT_MAX_PROCS, cpu_count)
230230
else:
231-
num_procs = max(1, min(num_procs, len(items), cpu_count))
231+
num_procs = max(1, min(num_procs, cpu_count))
232232

233233
cnt = 0
234234
pool = _get_pool(num_procs)

src/maxtext/trainers/post_train/rl/train_rl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def _use_raw_prompt(x):
339339
dataset_size = int(trainer_config.num_batches * trainer_config.batch_size * trainer_config.train_fraction)
340340
train_dataset = train_dataset[:dataset_size]
341341
train_dataset = train_dataset.repeat(trainer_config.num_epoch)
342-
train_dataset = train_dataset.to_iter_dataset().batch(trainer_config.batch_size)
342+
train_dataset = train_dataset.to_iter_dataset().batch(trainer_config.batch_size, drop_remainder=True)
343343

344344
if trainer_config.num_test_batches > 0:
345345
# eval_batch_size = -1 (default) → use trainer_config.batch_size (legacy
@@ -358,7 +358,7 @@ def _use_raw_prompt(x):
358358
test_dataset = test_dataset[
359359
trainer_config.test_batch_start_index : trainer_config.num_test_batches * eval_batch_size_for_eval
360360
]
361-
test_dataset = test_dataset.to_iter_dataset().batch(eval_batch_size_for_eval)
361+
test_dataset = test_dataset.to_iter_dataset().batch(eval_batch_size_for_eval, drop_remainder=True)
362362

363363
return train_dataset, test_dataset
364364

tests/post_training/unit/train_rl_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ def get_filtered_data_side_effect(dataset_name, model_tokenizer, template_config
359359
data_shuffle_seed=42,
360360
max_prefill_predict_length=10,
361361
batch_size=2,
362+
eval_batch_size=1,
362363
num_batches=2,
363364
train_fraction=1.0,
364365
num_epoch=1,
@@ -422,7 +423,8 @@ def test_prepare_datasets_with_split(self, mock_load):
422423
data_template_path="maxtext/examples/chat_templates/gsm8k_rl.json",
423424
data_shuffle_seed=42,
424425
num_batches=1,
425-
batch_size=5,
426+
batch_size=2,
427+
eval_batch_size=1,
426428
train_fraction=1.0,
427429
num_epoch=1,
428430
num_test_batches=1,

0 commit comments

Comments
 (0)