Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

fix: remove auto wandb.finish() after train() to allow post-training evaluate() (#4564)

Krishna Chaitanya11606c502587
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index 52fd7f7ecc5..581244e4d33 100755
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -357,7 +357,6 @@ from transformers.training_args import ParallelMode
from unsloth_zoo.device_type import DEVICE_TYPE, device_synchronize
# Wrap trainer with padding to right and enable training mode
-# Also patches W&B since multiple runs must use wandb.finish()
import functools
from types import MethodType
try:
@@ -367,6 +366,23 @@ except:
def prepare_for_training_mode(f):
@functools.wraps(f)
def wrapper(self, *args, **kwargs):
+ # Finish the previous W&B run if this is a subsequent train() call.
+ # We do this at the START of train() (not the end) so that
+ # evaluate() / log() still work after train() completes.
+ # HF's WandbCallback.setup() will call wandb.init() for the new run.
+ # See: https://github.com/unslothai/unsloth/issues/3954
+ if getattr(self, '_unsloth_training_completed', False):
+ try:
+ import wandb
+ if wandb.run is not None:
+ wandb.finish()
+ # Reset HF's WandbCallback so it calls wandb.init() for the new run
+ for cb in self.callback_handler.callbacks:
+ if type(cb).__name__ == 'WandbCallback':
+ cb._initialized = False
+ break
+ except:
+ pass
# Enable training mode
_was_training = None
# Get gradient checkpointing setting from training arguments
@@ -387,12 +403,9 @@ def prepare_for_training_mode(f):
reset_unsloth_gradient_checkpointing_buffers()
except:
pass
- # Patch W&B to enable logging on future runs, otherwise it'll overwrite the first run
- try:
- import wandb
- wandb.finish()
- except:
- pass
+ # Mark that training completed so the next train() call can
+ # finish this W&B run before starting a new one
+ self._unsloth_training_completed = True
return output
return wrapper
pass