Hide101111001111000 commited on
Commit
30138d8
·
verified ·
1 Parent(s): 0b936a8

Update read.md

Browse files
Files changed (1) hide show
  1. README.md +150 -0
README.md CHANGED
@@ -20,3 +20,153 @@ 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
+
27
+ '''python'''
28
+
29
+ import torch
30
+ if torch.cuda.get_device_capability()[0] >= 8:
31
+ !pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
32
+
33
+ from unsloth import FastLanguageModel
34
+ import torch
35
+ max_seq_length = 512 # unslothではRoPEをサポートしているのでコンテキスト長は自由に設定可能
36
+ dtype = None # Noneにしておけば自動で設定
37
+ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
38
+
39
+ model_id = "llm-jp/llm-jp-3-13b"
40
+ new_model_id = "llm-jp-3-13b-it" #Fine-Tuningしたモデルにつけたい名前、it: Instruction Tuning
41
+ # FastLanguageModel インスタンスを作成
42
+ model, tokenizer = FastLanguageModel.from_pretrained(
43
+ model_name=model_id,
44
+ dtype=dtype,
45
+ load_in_4bit=load_in_4bit,
46
+ trust_remote_code=True,
47
+ )
48
+
49
+ # SFT用のモデルを用意
50
+ model = FastLanguageModel.get_peft_model(
51
+ model,
52
+ r = 32,
53
+ target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
54
+ "gate_proj", "up_proj", "down_proj",],
55
+ lora_alpha = 32,
56
+ lora_dropout = 0.05,
57
+ bias = "none",
58
+ use_gradient_checkpointing = "unsloth",
59
+ random_state = 3407,
60
+ use_rslora = False,
61
+ loftq_config = None,
62
+ max_seq_length = max_seq_length,
63
+ )
64
+
65
+
66
+ from datasets import load_dataset
67
+
68
+ dataset = load_dataset("json", data_files="ichikara-instruction-003-001-1.json")
69
+
70
+ # 学習時のプロンプトフォーマットの定義
71
+ prompt = """### 指示
72
+ {}
73
+ ### 回答
74
+ {}"""
75
+
76
+
77
+ """
78
+ formatting_prompts_func: 各データをプロンプトに合わせた形式に合わせる
79
+ """
80
+ EOS_TOKEN = tokenizer.eos_token # トークナイザーのEOSトークン(文末トークン)
81
+ def formatting_prompts_func(examples):
82
+ input = examples["text"] # 入力データ
83
+ output = examples["output"] # 出力データ
84
+ text = prompt.format(input, output) + EOS_TOKEN # プロンプトの作成
85
+ return { "formatted_text" : text, } # 新しいフィールド "formatted_text" を返す
86
+ pass
87
+
88
+ # # 各データにフォーマットを適用
89
+ dataset = dataset.map(
90
+ formatting_prompts_func,
91
+ num_proc= 4, # 並列処理数を指定
92
+ )
93
+
94
+ dataset
95
+
96
+ from trl import SFTTrainer
97
+ from transformers import TrainingArguments
98
+ from unsloth import is_bfloat16_supported
99
+
100
+ trainer = SFTTrainer(
101
+ model = model,
102
+ tokenizer = tokenizer,
103
+ train_dataset=dataset["train"],
104
+ max_seq_length = max_seq_length,
105
+ dataset_text_field="formatted_text",
106
+ packing = False,
107
+ args = TrainingArguments(
108
+ per_device_train_batch_size = 2,
109
+ gradient_accumulation_steps = 4,
110
+ num_train_epochs = 1,
111
+ logging_steps = 10,
112
+ warmup_steps = 10,
113
+ save_steps=100,
114
+ save_total_limit=2,
115
+ max_steps=-1,
116
+ learning_rate = 2e-4,
117
+ fp16 = not is_bfloat16_supported(),
118
+ bf16 = is_bfloat16_supported(),
119
+ group_by_length=True,
120
+ seed = 3407,
121
+ output_dir = "outputs",
122
+ report_to = "none",
123
+ ),
124
+ )
125
+
126
+ #@title 学習実行
127
+ trainer_stats = trainer.train()
128
+
129
+ import json
130
+ datasets = []
131
+ with open("elyza-tasks-100-TV_0.jsonl", "r") as f:
132
+ item = ""
133
+ for line in f:
134
+ line = line.strip()
135
+ item += line
136
+ if item.endswith("}"):
137
+ datasets.append(json.loads(item))
138
+ item = ""
139
+
140
+ # 学習したモデルを用いてタスクを実行
141
+ from tqdm import tqdm
142
+
143
+ # 推論するためにモデルのモードを変更
144
+ FastLanguageModel.for_inference(model)
145
+
146
+ results = []
147
+ for dt in tqdm(datasets):
148
+ input = dt["input"]
149
+
150
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
151
+
152
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
153
+
154
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
155
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
156
+
157
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
158
+
159
+ # jsonlで保存
160
+ with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
161
+ for result in results:
162
+ json.dump(result, f, ensure_ascii=False)
163
+ f.write('\n')
164
+
165
+ # LoRAアダプタだけ保存
166
+ model.push_to_hub_merged(
167
+ new_model_id+"_lora",
168
+ tokenizer=tokenizer,
169
+ save_method="lora",
170
+ token="token",
171
+ private=True
172
+ )