Masaki5555 commited on
Commit
b3a2678
·
verified ·
1 Parent(s): 6ee5511

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +92 -0
README.md CHANGED
@@ -20,3 +20,95 @@ 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
+ # README
27
+ ## ベンチマークの出力方法
28
+
29
+ - 以下のコードを実行する(適宜修正が必要)
30
+ ```python
31
+ # 必要なライブラリをインストール
32
+ %%capture
33
+ !pip install unsloth
34
+ !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
35
+ !pip install -U torch
36
+ !pip install -U peft
37
+ ```
38
+
39
+ ```python
40
+ # 必要なライブラリを読み込み
41
+ from unsloth import FastLanguageModel
42
+ from peft import PeftModel
43
+ import torch
44
+ import json
45
+ from tqdm import tqdm
46
+ import re
47
+ ```
48
+
49
+ ```python
50
+ # ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
51
+ model_id = "llm-jp/llm-jp-3-13b"
52
+ adapter_id = "Masaki5555/llm-jp-3-13b-finetune-ver4"
53
+ ```
54
+
55
+ ```python
56
+ # Hugging Face Token を指定。
57
+ HF_TOKEN = "your_Hugging_Face_Token"
58
+ ```
59
+
60
+
61
+ ```python
62
+ # unslothのFastLanguageModelで元のモデルをロード。
63
+ dtype = None
64
+ load_in_4bit = True
65
+
66
+ model, tokenizer = FastLanguageModel.from_pretrained(
67
+ model_name=model_id,
68
+ dtype=dtype,
69
+ load_in_4bit=load_in_4bit,
70
+ trust_remote_code=True,
71
+ )
72
+ ```
73
+
74
+ ```python
75
+ # 元のモデルにLoRAのアダプタを統合。
76
+ model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
77
+ ```
78
+
79
+ ```python
80
+ # タスクとなるデータの読み込み。
81
+ datasets = []
82
+ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
83
+ item = ""
84
+ for line in f:
85
+ line = line.strip()
86
+ item += line
87
+ if item.endswith("}"):
88
+ datasets.append(json.loads(item))
89
+ item = ""
90
+ ```
91
+
92
+ ```python
93
+ # モデルを用いてタスクの推論。
94
+ FastLanguageModel.for_inference(model)
95
+
96
+ results = []
97
+ for dt in tqdm(datasets):
98
+ input = dt["input"]
99
+ prompt = f"""### 指示\n{input}\n### 回答\n"""
100
+ inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
101
+ outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
102
+ prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
103
+ results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
104
+ ```
105
+
106
+ ```python
107
+ # 結果をjsonlで保存。
108
+ json_file_id = re.sub(".*/", "", adapter_id)
109
+
110
+ with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
111
+ for result in results:
112
+ json.dump(result, f, ensure_ascii=False)
113
+ f.write('\n')
114
+ ```