diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py index 2bd602e099..cea08bbc3b 100644 --- a/unsloth/models/rl.py +++ b/unsloth/models/rl.py @@ -24,9 +24,12 @@ def PatchRL(FastLanguageModel): @contextmanager def unsloth_unwrap_model_for_generation(model, *args, **kwargs): + # Must use for_inference to allow inference in Unsloth FastLanguageModel.for_inference(model) - with unwrap_model_for_generation(model, *args, **kwargs) as unwrapped_model: - yield unwrapped_model + with torch.inference_mode(): + with unwrap_model_for_generation(model, *args, **kwargs) as unwrapped_model: + yield unwrapped_model + # Return back to training mode FastLanguageModel.for_training (model) pass