microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
python/fineTuning/unsloth/trainEntities.py
167lines · modecode
| 1 | # Copyright (c) Microsoft Corporation and Henry Lucco. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from unsloth import FastLanguageModel |
| 5 | import torch |
| 6 | from knowledgePrompt import get_knowledge_prompt |
| 7 | max_seq_length = 8192 # Choose any! We auto support RoPE Scaling internally! |
| 8 | dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+ |
| 9 | load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False. |
| 10 | |
| 11 | # 4bit pre quantized models we support for 4x faster downloading + no OOMs. |
| 12 | fourbit_models = [ |
| 13 | "unsloth/Meta-Llama-3.1-8B-bnb-4bit", # Llama-3.1 15 trillion tokens model 2x faster! |
| 14 | "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit", |
| 15 | "unsloth/Meta-Llama-3.1-70B-bnb-4bit", |
| 16 | "unsloth/Meta-Llama-3.1-405B-bnb-4bit", # We also uploaded 4bit for 405b! |
| 17 | "unsloth/Mistral-Nemo-Base-2407-bnb-4bit", # New Mistral 12b 2x faster! |
| 18 | "unsloth/Mistral-Nemo-Instruct-2407-bnb-4bit", |
| 19 | "unsloth/mistral-7b-v0.3-bnb-4bit", # Mistral v3 2x faster! |
| 20 | "unsloth/mistral-7b-instruct-v0.3-bnb-4bit", |
| 21 | "unsloth/Phi-3.5-mini-instruct", # Phi-3.5 2x faster! |
| 22 | "unsloth/Phi-3-medium-4k-instruct", |
| 23 | "unsloth/gemma-2-9b-bnb-4bit", |
| 24 | "unsloth/gemma-2-27b-bnb-4bit", # Gemma 2x faster! |
| 25 | ] # More models at https://huggingface.co/unsloth |
| 26 | |
| 27 | model, tokenizer = FastLanguageModel.from_pretrained( |
| 28 | model_name = "unsloth/Phi-4", # Choose any 4bit model from above |
| 29 | max_seq_length = max_seq_length, |
| 30 | dtype = dtype, |
| 31 | load_in_4bit = load_in_4bit, |
| 32 | # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf |
| 33 | ) |
| 34 | |
| 35 | model = FastLanguageModel.get_peft_model( |
| 36 | model, |
| 37 | r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128 |
| 38 | target_modules = ["q_proj", "k_proj", "v_proj", "o_proj", |
| 39 | "gate_proj", "up_proj", "down_proj",], |
| 40 | lora_alpha = 16, |
| 41 | lora_dropout = 0, # Supports any, but = 0 is optimized |
| 42 | bias = "none", # Supports any, but = "none" is optimized |
| 43 | # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes! |
| 44 | use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context |
| 45 | random_state = 3407, |
| 46 | use_rslora = False, # We support rank stabilized LoRA |
| 47 | loftq_config = None, # And LoftQ |
| 48 | ) |
| 49 | |
| 50 | from datasets import Dataset |
| 51 | import pandas as pd |
| 52 | from unsloth import to_sharegpt |
| 53 | # load an array of JSON objects with properties knowledge and message |
| 54 | import json |
| 55 | |
| 56 | # reduce token count by simplifying the knowledge structure |
| 57 | def simplify_knowledge(knowledge): |
| 58 | # change fields ending in EntityName like subjectEntityName to single words like subject |
| 59 | for action in knowledge['actions']: |
| 60 | if 'subjectEntityName' in action: |
| 61 | action['subject'] = action.pop('subjectEntityName') |
| 62 | if 'objectEntityName' in action: |
| 63 | action['object'] = action.pop('objectEntityName') |
| 64 | if 'indirectObjectEntityName' in action: |
| 65 | action['indirectObject'] = action.pop('indirectObjectEntityName') |
| 66 | # also subjectEntityFacet to subjectFacet |
| 67 | if 'subjectEntityFacet' in action: |
| 68 | action['subjectFacet'] = action.pop('subjectEntityFacet') |
| 69 | # remove the inverseActions field if it exists |
| 70 | if 'inverseActions' in knowledge: |
| 71 | knowledge.pop('inverseActions') |
| 72 | # if any fields have value "none", remove those fields |
| 73 | for action in knowledge['actions']: |
| 74 | keys_to_remove = [key for key, value in action.items() if value == "none"] |
| 75 | for key in keys_to_remove: |
| 76 | action.pop(key) |
| 77 | return knowledge |
| 78 | |
| 79 | with open('/data/gpt4o_train_3200.json') as f: |
| 80 | rawData = json.load(f) |
| 81 | # loop through the JSON objects and print the properties |
| 82 | data = [] |
| 83 | for i in range(len(rawData)): |
| 84 | simplified = simplify_knowledge(rawData[i]['knowledge']) |
| 85 | data.append({'output': json.dumps(simplified,separators=(',', ':')), 'instruction': get_knowledge_prompt(rawData[i]['message']), 'input': '', 'text': ''}) |
| 86 | # create a hugging face dataset from the JSON objects with the knowledge property becoming the output property and the message property becoming the instruction property |
| 87 | dataset = Dataset.from_pandas(pd.DataFrame(data=data)) |
| 88 | print(dataset.column_names) |
| 89 | print(dataset[0]) |
| 90 | |
| 91 | from unsloth import to_sharegpt |
| 92 | dataset = to_sharegpt( |
| 93 | dataset, |
| 94 | merged_prompt = "{instruction}[[\nYour input is:\n{input}]]", |
| 95 | output_column_name = "output", |
| 96 | conversation_extension = 1, # Select more to handle longer conversations |
| 97 | ) |
| 98 | |
| 99 | from unsloth import standardize_sharegpt |
| 100 | dataset = standardize_sharegpt(dataset) |
| 101 | |
| 102 | chat_template = """Below are some instructions that describe some tasks. Write responses that appropriately complete each request. |
| 103 | |
| 104 | ### Instruction: |
| 105 | {INPUT} |
| 106 | |
| 107 | ### Response: |
| 108 | {OUTPUT}""" |
| 109 | |
| 110 | from unsloth import apply_chat_template |
| 111 | dataset = apply_chat_template( |
| 112 | dataset, |
| 113 | tokenizer = tokenizer, |
| 114 | chat_template = chat_template, |
| 115 | # default_system_message = "You are a helpful assistant", << [OPTIONAL] |
| 116 | ) |
| 117 | |
| 118 | from trl import SFTTrainer |
| 119 | from transformers import TrainingArguments |
| 120 | from unsloth import is_bfloat16_supported |
| 121 | trainer = SFTTrainer( |
| 122 | model = model, |
| 123 | tokenizer = tokenizer, |
| 124 | train_dataset = dataset, |
| 125 | dataset_text_field = "text", |
| 126 | max_seq_length = max_seq_length, |
| 127 | dataset_num_proc = 2, |
| 128 | packing = False, # Can make training 5x faster for short sequences. |
| 129 | args = TrainingArguments( |
| 130 | per_device_train_batch_size = 2, |
| 131 | gradient_accumulation_steps = 4, |
| 132 | warmup_steps = 5, |
| 133 | max_steps = -1, |
| 134 | num_train_epochs = 1, # For longer training runs! |
| 135 | learning_rate = 2e-4, |
| 136 | fp16 = not is_bfloat16_supported(), |
| 137 | bf16 = is_bfloat16_supported(), |
| 138 | logging_steps = 1, |
| 139 | optim = "adamw_8bit", |
| 140 | weight_decay = 0.01, |
| 141 | lr_scheduler_type = "linear", |
| 142 | seed = 3407, |
| 143 | output_dir = "outputs", |
| 144 | report_to = "none", # Use this for WandB etc |
| 145 | ), |
| 146 | ) |
| 147 | |
| 148 | trainer_stats = trainer.train() |
| 149 | #@title Show final memory and time stats |
| 150 | used_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3) |
| 151 | used_memory_for_lora = round(used_memory, 3) |
| 152 | used_percentage = round(used_memory /48*100, 3) |
| 153 | lora_percentage = round(used_memory_for_lora/48*100, 3) |
| 154 | print(f"{trainer_stats.metrics['train_runtime']} seconds used for training.") |
| 155 | print(f"{round(trainer_stats.metrics['train_runtime']/60, 2)} minutes used for training.") |
| 156 | print(f"Peak reserved memory = {used_memory} GB.") |
| 157 | print(f"Peak reserved memory for training = {used_memory_for_lora} GB.") |
| 158 | print(f"Peak reserved memory % of max memory = {used_percentage} %.") |
| 159 | print(f"Peak reserved memory for training % of max memory = {lora_percentage} %.") |
| 160 | |
| 161 | saveDir = "/data/phi-4-lora-3200" # Change this to your desired save directory |
| 162 | model.save_pretrained(saveDir) # Local saving |
| 163 | tokenizer.save_pretrained(saveDir) |
| 164 | |
| 165 | #print(tokenizer._ollama_modelfile) |
| 166 | # Save to 8bit Q8_0 |
| 167 | #model.save_pretrained_gguf("llama_Q8_0_model", tokenizer,quantization_method = "q8_0") |