diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index b7333f0098..bcd4a7b30a 100644 --- a/unsloth/models/_utils.py +++ b/unsloth/models/_utils.py @@ -410,36 +410,50 @@ pass """ - Remove warnings about missing kwargs + Remove warnings about missing kwargs and patch stuff """ -try: - from transformers.utils.quantization_config import BitsAndBytesConfig, QuantizationMethod - from inspect import getsource - import re - BitsAndBytesConfig__init__ = getsource(BitsAndBytesConfig.__init__) - BitsAndBytesConfig__init__ = re.sub( - r"if[\s]{1,}kwargs\:[\s]{1,}.+?\n", - "", - BitsAndBytesConfig__init__, - flags = re.MULTILINE, - ) - BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.split("\n") - length_spaces = len(re.match(r"[\s]{1,}", BitsAndBytesConfig__init__[0]).group(0)) - BitsAndBytesConfig__init__ = "\n".join(x[length_spaces:] for x in BitsAndBytesConfig__init__) - BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.replace( - "__init__", - "_BitsAndBytesConfig__init__", - ) - exec(BitsAndBytesConfig__init__, globals()) - - import transformers.utils.quantization_config - transformers.utils.quantization_config.BitsAndBytesConfig.__init__ = _BitsAndBytesConfig__init__ -except: - logger.warning_once( - "Unsloth unsuccessfully patched bitsandbytes. Please file a bug report.\n"\ - "Luckily, your training run will still work in the meantime!" - ) +from transformers.utils.quantization_config import BitsAndBytesConfig, QuantizationMethod +from inspect import getsource +from accelerate.utils.dataclasses import DistributedType +import re +BitsAndBytesConfig__init__ = getsource(BitsAndBytesConfig.__init__) +BitsAndBytesConfig__init__ = re.sub( + r"if[\s]{1,}kwargs\:[\s]{1,}.+?\n", + "", + BitsAndBytesConfig__init__, + flags = re.MULTILINE, +) +BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.split("\n") +length_spaces = len(re.match(r"[\s]{1,}", BitsAndBytesConfig__init__[0]).group(0)) +BitsAndBytesConfig__init__ = "\n".join(x[length_spaces:] for x in BitsAndBytesConfig__init__) +BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.replace( + "__init__", + "_BitsAndBytesConfig__init__", +) + +def _prepare_backend( + self, cpu: bool = False, sagemaker_dp = False, backend: str = None, +) -> tuple[str, DistributedType]: + return None, DistributedType.NO pass +import accelerate.state +accelerate.state.PartialState._prepare_backend = _prepare_backend + +import accelerate.accelerator +prepare = inspect.getsource(accelerate.accelerator.Accelerator.prepare) +prepare = prepare.split("\n") +spaces = prepare[0].find("def") +prepare = "\n".join(x[spaces:] for x in prepare) +x = "for obj in args:" +s = " "*spaces +prepare = prepare.replace(x, f'self.state.distributed_type = DistributedType.NO\n{s}{x}', 1) +exec(prepare, globals()) +accelerate.accelerator.Accelerator.prepare = prepare + +exec(BitsAndBytesConfig__init__, globals()) + +import transformers.utils.quantization_config +transformers.utils.quantization_config.BitsAndBytesConfig.__init__ = _BitsAndBytesConfig__init__ # Offloading to disk for modules (lm_head, embed_tokens) diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index 9aeb55e4ea..7dec8624e1 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -1277,6 +1277,7 @@ class FastLlamaModel: "is_sagemaker_mp_enabled()", "False", ) + exec(inner_training_loop, globals()) Trainer._inner_training_loop = _fast_inner_training_loop # Save max_seq_length @@ -1316,6 +1317,7 @@ class FastLlamaModel: # Add save modules patch_saving_functions(model) + Trainer._inner_training_loop = _fast_inner_training_loop # Save tokenizer for inference purposes tokenizer.padding_side = "left" # Force inference @@ -1336,18 +1338,18 @@ class FastLlamaModel: layers = model.model.layers # Torch.compile fails on embedding matrix?? - # Workaround randomnly fixes it for torch versions < 2.2 - model.model.embed_tokens = torch.nn.Embedding.from_pretrained(model.model.embed_tokens.weight) + # Workaround randomnly fixes it for torch versions < 2. + model.set_input_embeddings(torch.nn.Embedding.from_pretrained(model.get_input_embeddings().weight)) model.config.update({"unsloth_version" : __version__}) # We also do this for the lm_head lm_head = torch.nn.Linear(1, 1, bias = None) del lm_head.weight - lm_head.weight = model.lm_head.weight + lm_head.weight = model.get_output_embeddings().weight lm_head.in_features = lm_head.weight.shape[1] lm_head.out_features = lm_head.weight.shape[0] model.lm_head = lm_head - + # Also patch all dtypes - BnB seems to not allocate the correct type? # BnB default dtype seems to be float16! correct_dtype = lm_head.weight.dtype diff --git a/unsloth/save.py b/unsloth/save.py index 7af6280910..5d6f925d45 100644 --- a/unsloth/save.py +++ b/unsloth/save.py @@ -975,6 +975,7 @@ def save_to_gguf( vocab_type = "bpe" pass + use_fast_convert = False if use_fast_convert: command = f"python llama.cpp/convert.py {model_directory} "\ f"--outfile {final_location} --vocab-type {vocab_type} "\ diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py index 6e4d69107e..6afea68057 100644 --- a/unsloth/tokenizer_utils.py +++ b/unsloth/tokenizer_utils.py @@ -579,7 +579,7 @@ def fix_untrained_tokens(model, tokenizer, train_dataset, eps = 1e-16): actual_bad_tokens = tokenizer.convert_ids_to_tokens(where_untrained) # Remove None items in actual_bad_tokens actual_bad_tokens = [x for x in actual_bad_tokens if x is not None] - + # Check if tokenizer and training datasets have bad tokens if_bad_first = False if_bad_second = False