Tagawa commited on
Commit
7d33e8e
·
verified ·
1 Parent(s): a2f57ec

Update README.md

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