fajoie commited on
Commit
1e8cc17
·
verified ·
1 Parent(s): c82e361

update readme

Browse files
Files changed (1) hide show
  1. README.md +132 -0
README.md CHANGED
@@ -20,3 +20,135 @@ language:
20
  This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
21
 
22
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
21
 
22
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
23
+
24
+ # 手順
25
+ ```
26
+ !pip uninstall unsloth -y
27
+ !pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
28
+ !pip install --upgrade torch
29
+ !pip install --upgrade xformers
30
+
31
+ # Install Flash Attention 2 for softcapping support
32
+ import torch
33
+ if torch.cuda.get_device_capability()[0] >= 8:
34
+ !pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
35
+
36
+ # Hugging Face Token を指定
37
+ HF_TOKEN = "xxx"
38
+
39
+ # llm-jp/llm-jp-3-13bを4bit量子化のqLoRA設定でロード。
40
+ from unsloth import FastLanguageModel
41
+ import torch
42
+ max_seq_length = 512
43
+ dtype = None
44
+ load_in_4bit = True
45
+
46
+ model_id = "llm-jp/llm-jp-3-13b"
47
+ new_model_id = "llm-jp-3-13b-it"
48
+ # FastLanguageModel
49
+ model, tokenizer = FastLanguageModel.from_pretrained(
50
+ model_name=model_id,
51
+ dtype=dtype,
52
+ load_in_4bit=load_in_4bit,
53
+ trust_remote_code=True,
54
+ )
55
+
56
+ # SFT用のモデルを用意
57
+ model = FastLanguageModel.get_peft_model(
58
+ model,
59
+ r = 32,
60
+ target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
61
+ "gate_proj", "up_proj", "down_proj",],
62
+ lora_alpha = 32,
63
+ lora_dropout = 0.05,
64
+ bias = "none",
65
+ use_gradient_checkpointing = "unsloth",
66
+ random_state = 3407,
67
+ use_rslora = False,
68
+ loftq_config = None,
69
+ max_seq_length = max_seq_length,
70
+ )
71
+
72
+ from datasets import Dataset, load_dataset, concatenate_datasets
73
+
74
+ # 使用したいデータセットのパス すべてのichikaraのデータセットを利用
75
+ data_dir = "/content/"
76
+ data_files = [
77
+ "ichikara-instruction-003-001-1.json",
78
+ "ichikara-instruction-003-001-2.1.json",
79
+ "ichikara-instruction-003-001-2.2.json",
80
+ "ichikara-instruction-003-001-5.1.json",
81
+ "ichikara-instruction-003-001-5.2.json",
82
+ "ichikara-instruction-003-003-1.json"
83
+ ]
84
+
85
+ dataset = Dataset.from_dict({"ID": [], "text": [], "output":[]})
86
+ for data_file in data_files:
87
+ tmp = load_dataset("json", data_files=f"{data_dir}{data_file}", split="train", streaming=False)
88
+ if len(dataset) == 0:
89
+ dataset = tmp
90
+ else:
91
+ dataset = concatenate_datasets([dataset,tmp])
92
+
93
+ # 学習時のプロンプトフォーマットの定義
94
+ prompt = """### 指示
95
+ {}
96
+ ### 回答
97
+ {}"""
98
+
99
+ EOS_TOKEN = tokenizer.eos_token
100
+ def formatting_prompts_func(examples):
101
+ input = examples["text"]
102
+ output = examples["output"]
103
+ text = prompt.format(input, output) + EOS_TOKEN
104
+ return { "formatted_text" : text, }
105
+ pass
106
+
107
+ # # 各データにフォーマットを適用
108
+ dataset = dataset.map(
109
+ formatting_prompts_func,
110
+ num_proc= 4,
111
+ )
112
+
113
+ from trl import SFTTrainer
114
+ from transformers import TrainingArguments
115
+ from unsloth import is_bfloat16_supported
116
+
117
+ # 学習の設定
118
+ trainer = SFTTrainer(
119
+ model = model,
120
+ tokenizer = tokenizer,
121
+ train_dataset=dataset,
122
+ max_seq_length = max_seq_length,
123
+ dataset_text_field="formatted_text",
124
+ packing = False,
125
+ args = TrainingArguments(
126
+ per_device_train_batch_size = 16, #Google Colab Pro+を使ったのでバッチサイズを上げた
127
+ gradient_accumulation_steps = 1, #蓄積は逆になしに
128
+ num_train_epochs = 1, #上げたら過学習してさがったので、最終的に1回にした
129
+ logging_steps = 10,
130
+ warmup_steps = 100,
131
+ save_steps=100,
132
+ save_total_limit=2,
133
+ max_steps=-1,
134
+ learning_rate = 2e-4,
135
+ fp16 = not is_bfloat16_supported(),
136
+ bf16 = is_bfloat16_supported(),
137
+ group_by_length=True,
138
+ seed = 3407,
139
+ output_dir = "outputs",
140
+ report_to = "none",
141
+ ),
142
+ )
143
+
144
+ # 学習実行
145
+ trainer_stats = trainer.train()
146
+
147
+ # LoRAアダプタだけ保存
148
+ model.push_to_hub_merged(
149
+ new_model_id+"_lora_4",
150
+ tokenizer=tokenizer,
151
+ save_method="lora",
152
+ token=HF_TOKEN,
153
+ private=True
154
+ )