TaroNakasendo
commited on
Commit
•
4ee6348
1
Parent(s):
7582e2e
推論方法を追加
Browse files
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 |
+
```
|
27 |
+
# -*- coding: utf-8 -*-
|
28 |
+
"""Model_Inference_Template_unsloth_20241127.ipynb
|
29 |
+
|
30 |
+
Automatically generated by Colab.
|
31 |
+
|
32 |
+
Original file is located at
|
33 |
+
https://colab.research.google.com/drive/1CinXAmtYZCBbGizZ6TbotfGLd9kS3jwL
|
34 |
+
|
35 |
+
# 推論用コード
|
36 |
+
Omnicampusで学習環境を整えておき、以下を新規notebookのセルにコピーペーストして実行します
|
37 |
+
"""
|
38 |
+
|
39 |
+
# Commented out IPython magic to ensure Python compatibility.
|
40 |
+
# # 必要なライブラリをインストール
|
41 |
+
# %%capture
|
42 |
+
# !pip install unsloth
|
43 |
+
# !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
44 |
+
# !pip install -U torch
|
45 |
+
# !pip install -U peft
|
46 |
+
|
47 |
+
# 必要なライブラリを読み込み
|
48 |
+
from unsloth import FastLanguageModel
|
49 |
+
from peft import PeftModel
|
50 |
+
import torch
|
51 |
+
import json
|
52 |
+
from tqdm import tqdm
|
53 |
+
import re
|
54 |
+
|
55 |
+
# ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
|
56 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
57 |
+
adapter_id = "TaroNakasendo/hitsuji-llm-jp-3-13b_lora"
|
58 |
+
|
59 |
+
# Hugging Face Token を指定。
|
60 |
+
# 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。
|
61 |
+
# https://huggingface.co/settings/tokens
|
62 |
+
|
63 |
+
|
64 |
+
# unslothのFastLanguageModelで元のモデルをロード。
|
65 |
+
dtype = None # Noneにしておけば自動で設定
|
66 |
+
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
67 |
+
|
68 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
69 |
+
model_name=model_id,
|
70 |
+
dtype=dtype,
|
71 |
+
load_in_4bit=load_in_4bit,
|
72 |
+
trust_remote_code=True,
|
73 |
+
)
|
74 |
+
|
75 |
+
# 元のモデルにLoRAのアダプタを統合。
|
76 |
+
model = PeftModel.from_pretrained(model, adapter_id)
|
77 |
+
|
78 |
+
# タスクとなるデータの読み込み。
|
79 |
+
# 事前にデータをアップロードしてください。
|
80 |
+
datasets = []
|
81 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
82 |
+
item = ""
|
83 |
+
for line in f:
|
84 |
+
line = line.strip()
|
85 |
+
item += line
|
86 |
+
if item.endswith("}"):
|
87 |
+
datasets.append(json.loads(item))
|
88 |
+
item = ""
|
89 |
+
|
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 |
+
# 結果をjsonlで保存。
|
109 |
+
|
110 |
+
# ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
|
111 |
+
json_file_id = re.sub(".*/", "", adapter_id)
|
112 |
+
with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
113 |
+
for result in results:
|
114 |
+
json.dump(result, f, ensure_ascii=False)
|
115 |
+
f.write('\n')
|
116 |
+
|
117 |
+
```
|