ishikawakazuhiko commited on
Commit
45bc110
·
verified ·
1 Parent(s): 40877b9

Update README.md

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