Fix review feedback for trainer config remapping
This commit is contained in:
parent
90dd1eb036
commit
6329e4bf37
4 changed files with 33 additions and 2 deletions
|
|
@ -730,7 +730,7 @@ try:
|
|||
# Some Config files use layer_type_validation
|
||||
# for eg Gemma-2, so we must import it to stop errors.
|
||||
from transformers.configuration_utils import layer_type_validation
|
||||
except:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -102,6 +102,7 @@ def _maybe_prepare_vllm_for_resume(trainer):
|
|||
llm = getattr(getattr(trainer, "model", None), "vllm_engine", None)
|
||||
|
||||
sleep_fn = getattr(llm, "sleep", None)
|
||||
slept = False
|
||||
if callable(sleep_fn):
|
||||
try:
|
||||
sleep_mode = int(os.environ.get("VLLM_SLEEP_MODE", "1"))
|
||||
|
|
@ -116,8 +117,10 @@ def _maybe_prepare_vllm_for_resume(trainer):
|
|||
try:
|
||||
if signature is not None and len(signature.parameters) == 0:
|
||||
sleep_fn()
|
||||
slept = True
|
||||
else:
|
||||
sleep_fn(sleep_mode)
|
||||
slept = True
|
||||
except Exception as error:
|
||||
logger.warning_once(
|
||||
f"Unsloth: vLLM sleep() failed during resume cleanup: {error}"
|
||||
|
|
@ -129,6 +132,9 @@ def _maybe_prepare_vllm_for_resume(trainer):
|
|||
gc.collect()
|
||||
torch.cuda.empty_cache()
|
||||
|
||||
if slept:
|
||||
trainer._unsloth_resume_wake_vllm = True
|
||||
|
||||
|
||||
def _patch_resume_from_checkpoint_memory(trainer_class):
|
||||
original_train = getattr(trainer_class, "train", None)
|
||||
|
|
|
|||
|
|
@ -236,6 +236,25 @@ def grpo_trainer__prepare_inputs(function_name, function):
|
|||
if function_name != "_prepare_inputs":
|
||||
return function
|
||||
|
||||
import re
|
||||
|
||||
match = re.search(
|
||||
r"^([ \t]*)with torch\.inference_mode\(\):", function, flags = re.MULTILINE
|
||||
)
|
||||
if match is not None:
|
||||
indent = match.group(1)
|
||||
nested_indent = indent + " " * 4
|
||||
wake_block = (
|
||||
f"{indent}if getattr(self, '_unsloth_resume_wake_vllm', False):\n"
|
||||
f"{nested_indent}wake_up = getattr(getattr(self, 'llm', None), 'wake_up', None)\n"
|
||||
f"{nested_indent}if callable(wake_up):\n"
|
||||
f"{nested_indent} try: wake_up()\n"
|
||||
f"{nested_indent} except Exception as error:\n"
|
||||
f"{nested_indent} logging.getLogger(__name__).warning('Unsloth: vLLM wake_up() failed during resume: %s', error)\n"
|
||||
f"{nested_indent}self._unsloth_resume_wake_vllm = False\n"
|
||||
f"{indent}with torch.inference_mode():"
|
||||
)
|
||||
function = function[: match.start()] + wake_block + function[match.end() :]
|
||||
# Add mixed precision training
|
||||
function = function.replace(
|
||||
"with torch.inference_mode():",
|
||||
|
|
|
|||
|
|
@ -303,7 +303,13 @@ def _backwards_compatible_trainer(trainer_class, config_class):
|
|||
# causes the 2nd init to fail as there are mutual exclusive checks on pairs of parameters.
|
||||
# Refer: https://github.com/huggingface/trl/blob/main/trl/trainer/grpo_config.py#L499-L502 for example
|
||||
# So we only create config class if the previous init was not TrainingArguments
|
||||
if not isinstance(training_args, TrainingArguments):
|
||||
if isinstance(training_args, config_class):
|
||||
import copy
|
||||
|
||||
config = copy.deepcopy(training_args)
|
||||
for key, value in additional_config_kwargs.items():
|
||||
setattr(config, key, value)
|
||||
elif not isinstance(training_args, TrainingArguments):
|
||||
config = config_class(**config_dict)
|
||||
else:
|
||||
config = training_args
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue