microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c765421e8f1db0fb5df3e6aa28254a56e6e33e0a

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

python/fineTuning/unsloth/trainEntities.py

167lines · modecode

1# Copyright (c) Microsoft Corporation and Henry Lucco.
2# Licensed under the MIT License.
3
4from unsloth import FastLanguageModel
5import torch
6from knowledgePrompt import get_knowledge_prompt
7max_seq_length = 8192 # Choose any! We auto support RoPE Scaling internally!
8dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
9load_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.
12fourbit_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
27model, 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
35model = 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
50from datasets import Dataset
51import pandas as pd
52from unsloth import to_sharegpt
53# load an array of JSON objects with properties knowledge and message
54import json
55
56# reduce token count by simplifying the knowledge structure
57def 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
79with open('/data/gpt4o_train_3200.json') as f:
80 rawData = json.load(f)
81# loop through the JSON objects and print the properties
82data = []
83for 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
87dataset = Dataset.from_pandas(pd.DataFrame(data=data))
88print(dataset.column_names)
89print(dataset[0])
90
91from unsloth import to_sharegpt
92dataset = 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
99from unsloth import standardize_sharegpt
100dataset = standardize_sharegpt(dataset)
101
102chat_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
110from unsloth import apply_chat_template
111dataset = 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
118from trl import SFTTrainer
119from transformers import TrainingArguments
120from unsloth import is_bfloat16_supported
121trainer = 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
148trainer_stats = trainer.train()
149#@title Show final memory and time stats
150used_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
151used_memory_for_lora = round(used_memory, 3)
152used_percentage = round(used_memory /48*100, 3)
153lora_percentage = round(used_memory_for_lora/48*100, 3)
154print(f"{trainer_stats.metrics['train_runtime']} seconds used for training.")
155print(f"{round(trainer_stats.metrics['train_runtime']/60, 2)} minutes used for training.")
156print(f"Peak reserved memory = {used_memory} GB.")
157print(f"Peak reserved memory for training = {used_memory_for_lora} GB.")
158print(f"Peak reserved memory % of max memory = {used_percentage} %.")
159print(f"Peak reserved memory for training % of max memory = {lora_percentage} %.")
160
161saveDir = "/data/phi-4-lora-3200" # Change this to your desired save directory
162model.save_pretrained(saveDir) # Local saving
163tokenizer.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")