diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py index 6be2caf95d..05432fc190 100644 --- a/unsloth/chat_templates.py +++ b/unsloth/chat_templates.py @@ -20,6 +20,7 @@ __all__ = [ "to_sharegpt", "standardize_sharegpt", + "standardize_data_formats", "apply_chat_template", "train_on_responses_only", @@ -37,7 +38,9 @@ from .models._utils import patch_tokenizer import re from unsloth_zoo.dataset_utils import ( train_on_responses_only, + standardize_data_formats, ) +standardize_sharegpt = standardize_data_formats CHAT_TEMPLATES = {} DEFAULT_SYSTEM_MESSAGE = {} @@ -1474,90 +1477,6 @@ def to_sharegpt( pass -def standardize_sharegpt( - dataset, - aliases_for_system = ["system",], - aliases_for_user = ["user", "human", "input",], - aliases_for_assistant = ["gpt", "assistant", "output",], -): - """ - Standardizes ShareGPT and other formats to user/assistant Hugging Face format. - - Get aliases for the system, user and assistant roles. - These shall map to "system", "user" and "assistant" respectively. - - aliases_for_system = ["system",], - aliases_for_user = ["user", "human", "input",], - aliases_for_assistant = ["gpt", "assistant", "output",], - """ - import collections - import itertools - - convos = dataset[:10]["conversations"] - uniques = collections.defaultdict(list) - for convo in convos: - for message in convo: - for key, value in message.items(): - uniques[key].append(value) - pass - - # Must be only 2 entries - assert(len(uniques.keys()) == 2) - - keys = list(uniques.keys()) - length_first = len(set(uniques[keys[0]])) - length_second = len(set(uniques[keys[1]])) - - if length_first < length_second: - # Role is assigned to the first element - role_key = keys[0] - content_key = keys[1] - else: - role_key = keys[1] - content_key = keys[0] - pass - - # Check roles are in aliases - all_aliases = set(aliases_for_system + aliases_for_user + aliases_for_assistant) - roles = set(uniques[role_key]) - leftover_aliases = (all_aliases | roles) - all_aliases - if len(leftover_aliases) != 0: - raise TypeError( - f"Unsloth: {list(leftover_aliases)} are not in aliases. Please update aliases." - ) - pass - - # Mapping for aliases - aliases_mapping = {} - for x in aliases_for_system: aliases_mapping[x] = "system" - for x in aliases_for_user: aliases_mapping[x] = "user" - for x in aliases_for_assistant: aliases_mapping[x] = "assistant" - - def _standardize_dataset(examples): - convos = examples["conversations"] - all_convos = [] - for convo in convos: - new_convo = [ - { "role" : aliases_mapping[message[role_key]], "content" : message[content_key], } - for message in convo - ] - all_convos.append(new_convo) - pass - return { "conversations" : all_convos, } - pass - - from multiprocessing import cpu_count - num_proc = cpu_count() - - return dataset.map( - _standardize_dataset, - batched = True, - desc = "Unsloth: Standardizing formats", - num_proc = num_proc, - ) -pass - - def get_ollama_eos_tokens(tokenizer, extra_eos_tokens = []): added_tokens_decoder = tokenizer.added_tokens_decoder.values() added_tokens_decoder = [str(x) for x in added_tokens_decoder] diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index 7ae6e92d11..bb2c7569d8 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -38,6 +38,7 @@ from ..kernels import * from ..tokenizer_utils import * if HAS_FLASH_ATTENTION: from flash_attn import flash_attn_func +from .vision import FastBaseModel # Final patching code from transformers.models.llama.modeling_llama import ( @@ -1648,6 +1649,7 @@ class FastLlamaModel: disable_log_stats = False, **kwargs, ): + os.environ["UNSLOTH_USE_NEW_MODEL"] = "0" if trust_remote_code: if fast_inference: raise NotImplementedError("Unsloth: Fast inference does not support `trust_remote_code` yet.") @@ -2016,6 +2018,31 @@ class FastLlamaModel: temporary_location = "_unsloth_temporary_saved_buffers", **kwargs, ): + if os.environ.get("UNSLOTH_USE_NEW_MODEL", "0") == "1": + return FastBaseModel.get_model( + model = model, + r = r, + target_modules = target_modules, + lora_alpha = lora_alpha, + lora_dropout = lora_dropout, + bias = bias, + finetune_vision_layers = False, + finetune_language_layers = True, + finetune_attention_modules = True, + finetune_mlp_modules = True, + layers_to_transform = layers_to_transform, + layers_pattern = layers_pattern, + use_gradient_checkpointing = use_gradient_checkpointing, + random_state = random_state, + max_seq_length = max_seq_length, + use_rslora = use_rslora, + modules_to_save = modules_to_save, + init_lora_weights = init_lora_weights, + loftq_config = loftq_config, + temporary_location = temporary_location, + **kwargs, + ) + pass if os.environ.get("UNSLOTH_ENABLE_FULL_FINETUNING", "0") == "1": print("Unsloth: Full finetuning is enabled, so .get_peft_model has no effect") return model diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index c595bcd80c..020bd4e56b 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -70,7 +70,7 @@ class FastLanguageModel(FastLlamaModel): @staticmethod def from_pretrained( model_name = "unsloth/Llama-3.2-1B-Instruct", - max_seq_length = None, + max_seq_length = 2048, dtype = None, load_in_4bit = True, load_in_8bit = False, @@ -96,7 +96,7 @@ class FastLanguageModel(FastLlamaModel): if load_in_8bit or full_finetuning: return FastModel.from_pretrained( model_name = model_name, - max_seq_length = max_seq_length, # [TODO] No effect + max_seq_length = max_seq_length, dtype = dtype, load_in_4bit = load_in_4bit, load_in_8bit = load_in_8bit, @@ -295,7 +295,7 @@ class FastLanguageModel(FastLlamaModel): else: return FastModel.from_pretrained( model_name = model_name, - max_seq_length = max_seq_length, # [TODO] No effect + max_seq_length = max_seq_length, dtype = dtype, load_in_4bit = load_in_4bit, load_in_8bit = load_in_8bit, @@ -442,7 +442,7 @@ class FastModel(FastBaseModel): @staticmethod def from_pretrained( model_name = "unsloth/Llama-3.2-11B-Vision-Instruct-bnb-4bit", - max_seq_length = None, # [TODO] No effect + max_seq_length = 2048, dtype = None, load_in_4bit = True, load_in_8bit = False, @@ -668,7 +668,7 @@ class FastModel(FastBaseModel): use_gradient_checkpointing = use_gradient_checkpointing, *args, **kwargs, ) - + if resize_model_vocab is not None: model.resize_token_embeddings(resize_model_vocab) pass diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py index 0f6eda6555..a65b538745 100644 --- a/unsloth/models/vision.py +++ b/unsloth/models/vision.py @@ -25,7 +25,6 @@ try: except: from transformers import AutoModelForVision2Seq pass -from .llama import * from ..kernels import ( post_patch_loss_function, ) @@ -100,7 +99,7 @@ class FastBaseModel: @staticmethod def from_pretrained( model_name = "unsloth/Llama-3.2-1B-Instruct", - max_seq_length = None, + max_seq_length = 2048, dtype = None, load_in_4bit = True, load_in_8bit = False, @@ -114,6 +113,7 @@ class FastBaseModel: use_gradient_checkpointing = "unsloth", **kwargs, ): + os.environ["UNSLOTH_USE_NEW_MODEL"] = "1" if trust_remote_code: print( "Unsloth: WARNING `trust_remote_code` is True.\n"\