jic062 commited on
Commit
ec1110c
·
verified ·
1 Parent(s): d49077a

Upload train.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train.py +105 -0
train.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from huggingface_hub import HfApi
3
+ from huggingface_hub import create_repo
4
+ from unsloth import FastLanguageModel
5
+ import torch
6
+ from datasets import load_dataset
7
+ import random
8
+
9
+ max_seq_length = 2048
10
+ dtype = None
11
+ load_in_4bit = True
12
+ repo_name = "instruct-v19"
13
+ # do wandb stuff
14
+ import wandb
15
+ wandb.init(
16
+ project="unsloth_lora",
17
+ name= repo_name,
18
+ )
19
+
20
+ model, tokenizer = FastLanguageModel.from_pretrained(
21
+
22
+ model_name = "meta-llama/Meta-Llama-3.1-8B-Instruct", #mistralai/Mistral-Nemo-Instruct-2407
23
+ max_seq_length = max_seq_length,
24
+ dtype = dtype,
25
+ load_in_4bit = load_in_4bit,
26
+ token = "", # use one if using gated models like meta-llama/Llama-2-7b-hf
27
+ )
28
+
29
+ model = FastLanguageModel.get_peft_model(
30
+
31
+ model,
32
+ r = 64, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
33
+ target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
34
+ "gate_proj", "up_proj", "down_proj",],
35
+ lora_alpha = 16,
36
+ lora_dropout = 0, # Supports any, but = 0 is optimized
37
+ bias = "none", # Supports any, but = "none" is optimized
38
+ # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
39
+ use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
40
+ random_state = 3407,
41
+ use_rslora = False, # We support rank stabilized LoRA
42
+ loftq_config = None, # And LoftQ
43
+ )
44
+
45
+ from datasets import load_dataset
46
+ dataset = load_dataset("Chaser-cz/ChaiTop100-SHAREGPT")
47
+ train_dataset = dataset["train"].shuffle(seed=random.randint(1, 9999))
48
+ from unsloth.chat_templates import get_chat_template
49
+ tokenizer = get_chat_template(
50
+ tokenizer,
51
+ chat_template = "llama-3",
52
+ mapping = {"role" : "from", "content" : "value", "user" : "human", "assistant" : "gpt"}, # ShareGPT style
53
+ )
54
+ def formatting_prompts_func(examples):
55
+ convos = examples["conversations"]
56
+ texts = [tokenizer.apply_chat_template(convo, tokenize = False, add_generation_prompt = False) for convo in convos]
57
+ return { "text" : texts, }
58
+ pass
59
+
60
+ train_dataset = train_dataset.map(formatting_prompts_func, batched = True,)
61
+ from trl import SFTTrainer
62
+ from transformers import TrainingArguments
63
+ from unsloth import is_bfloat16_supported
64
+
65
+ trainer = SFTTrainer(
66
+
67
+ model = model,
68
+ tokenizer = tokenizer,
69
+ train_dataset = train_dataset,
70
+ dataset_text_field = "text",
71
+ max_seq_length = max_seq_length,
72
+ dataset_num_proc = 2,
73
+ packing = False, # Can make training 5x faster for short sequences.
74
+ args = TrainingArguments(
75
+
76
+ per_device_train_batch_size = 2,
77
+ gradient_accumulation_steps = 32,
78
+ warmup_steps = 5,
79
+ max_steps = 1000,
80
+ learning_rate = 2.5e-4,
81
+ fp16 = not is_bfloat16_supported(),
82
+ bf16 = is_bfloat16_supported(),
83
+ logging_steps = 1,
84
+ optim = "adamw_8bit",
85
+ weight_decay = 0.01,
86
+ lr_scheduler_type = "cosine",
87
+ seed = 3407,
88
+ output_dir = "outputs/lora-out-8b",
89
+ save_strategy = "steps",
90
+ save_steps = 500,)
91
+ )
92
+
93
+
94
+ trainer_stats = trainer.train()
95
+
96
+ model.save_pretrained_merged("outputs/lora-out-8b/merged", tokenizer, save_method = "merged_16bit",)
97
+
98
+ api = HfApi()
99
+ create_repo(f"jic062/{repo_name}", repo_type="model",private=True, token="")
100
+ api.upload_folder(
101
+ folder_path="outputs/lora-out-8b/merged",
102
+ repo_id=f"jic062/{repo_name}",
103
+ repo_type="model",
104
+ )
105
+ wandb.finish()