From e554eb6f28c6faa788d135cc97da78362880e9f1 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 17 Feb 2025 22:17:11 -0800 Subject: [PATCH] unsloth_num_chunks --- unsloth/models/rl.py | 4 ++++ unsloth/models/rl_replacements.py | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py index f36598b0ac..fa617d5d46 100644 --- a/unsloth/models/rl.py +++ b/unsloth/models/rl.py @@ -117,6 +117,10 @@ class Unsloth{RLConfig_name}({RLConfig_name}): default = None, metadata = {{'help': 'vLLM SamplingParams'}}, ) + unsloth_num_chunks : Optional[int] = field( + default = 1, + metadata = {{'help': 'Chunk size to reduce memory usage'}}, + ) def __init__({RLConfig_arguments}, vllm_sampling_params = None, **kwargs, diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py index 405f790942..decaf32096 100644 --- a/unsloth/models/rl_replacements.py +++ b/unsloth/models/rl_replacements.py @@ -177,7 +177,7 @@ def grpo_trainer__get_per_token_logps(function_name, function): if function_name != "_get_per_token_logps": return function def _get_per_token_logps(self, model, input_ids, attention_mask, logits_to_keep): - return None + if self.args.unsloth_num_chunks != 1: return None if not hasattr(self, '_autocast_dtype'): self._autocast_dtype = torch.float16 if os.environ.get('ACCELERATE_MIXED_PRECISION', 'fp16') == 'fp16' else torch.bfloat16 with torch.amp.autocast(device_type = 'cuda', dtype = self._autocast_dtype): @@ -240,7 +240,8 @@ def grpo_trainer_compute_loss(function_name, function): ) else: loss, completion_length, mean_kl = grpo_accumulated_loss( - self, _input_ids, logits_to_keep, completion_mask, advantages, n_chunks = 2, + self, _input_ids, logits_to_keep, completion_mask, advantages, + n_chunks = self.args.unsloth_num_chunks, ) # Log the metrics