Update README.md
Browse files
README.md
CHANGED
@@ -20,3 +20,110 @@ 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 |
+
以下の手順に従うことで、Hugging Face上のモデル(llm-jp/llm-jp-3-13b + u-10bei/llm-jp-3-13b-lora-orca-ichikara2_Tengentoppa)を用いて入力データ(elyza-tasks-100-TV_0.jsonl)を推論し、その結果を{adapter_id}-outputs.jsonlというファイルに出力できます。
|
26 |
+
|
27 |
+
# 前提条件
|
28 |
+
Python環境があること(例: Google Colab)
|
29 |
+
Hugging Faceのアクセストークン (HF_TOKEN) が取得済みであること
|
30 |
+
セットアップ
|
31 |
+
必要なライブラリのインストールを行います。
|
32 |
+
以下は、Google Colabでの実行例です。
|
33 |
+
|
34 |
+
```
|
35 |
+
!pip uninstall unsloth -y
|
36 |
+
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
37 |
+
!pip install --upgrade torch
|
38 |
+
!pip install --upgrade xformers
|
39 |
+
|
40 |
+
# Google Colab シークレットを使う場合、左のサイドバーより🔑マークをクリック
|
41 |
+
# 任意の名前で Value に Hugging Face Token を入れてください。
|
42 |
+
# ノートブックからのアクセスのトグルをオンにし、下記コードを実行してください。
|
43 |
+
|
44 |
+
from google.colab import userdata
|
45 |
+
HF_TOKEN = userdata.get('{シークレットキー}') #シークレットキーの名前を入力
|
46 |
+
|
47 |
+
# llm-jp/llm-jp-3-13bを4bit量子化のqLoRA設定でロード。
|
48 |
+
|
49 |
+
from unsloth import FastLanguageModel
|
50 |
+
import torch
|
51 |
+
max_seq_length = 512 # unslothではRoPEをサポートしているのでコンテキスト長は自由に設定可能
|
52 |
+
dtype = None # Noneにしておけば自動で設定
|
53 |
+
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
54 |
+
|
55 |
+
model_id = "u-10bei/llm-jp-3-13b-lora-orca-ichikara2_Tengentoppa"
|
56 |
+
|
57 |
+
# FastLanguageModel インスタンスを作成
|
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 |
+
# SFT用のモデルを用意
|
67 |
+
|
68 |
+
model = FastLanguageModel.get_peft_model(
|
69 |
+
model,
|
70 |
+
r = 32,
|
71 |
+
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
|
72 |
+
"gate_proj", "up_proj", "down_proj",],
|
73 |
+
lora_alpha = 32,
|
74 |
+
lora_dropout = 0.05,
|
75 |
+
bias = "none",
|
76 |
+
use_gradient_checkpointing = "unsloth",
|
77 |
+
random_state = 3407,
|
78 |
+
use_rslora = False,
|
79 |
+
loftq_config = None,
|
80 |
+
max_seq_length = max_seq_length,
|
81 |
+
)
|
82 |
+
|
83 |
+
# 入力データの準備
|
84 |
+
# ./elyza-tasks-100-TV_0.jsonlというファイルからデータセットをロードします。
|
85 |
+
|
86 |
+
import json
|
87 |
+
datasets = []
|
88 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
89 |
+
item = ""
|
90 |
+
for line in f:
|
91 |
+
line = line.strip()
|
92 |
+
item += line
|
93 |
+
if item.endswith("}"):
|
94 |
+
datasets.append(json.loads(item))
|
95 |
+
item = ""
|
96 |
+
|
97 |
+
# 推論実行
|
98 |
+
|
99 |
+
from tqdm import tqdm
|
100 |
+
|
101 |
+
# 推論するためにモデルのモードを変更
|
102 |
+
|
103 |
+
FastLanguageModel.for_inference(model)
|
104 |
+
|
105 |
+
results = []
|
106 |
+
for dt in tqdm(datasets):
|
107 |
+
input = dt["input"]
|
108 |
+
|
109 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
110 |
+
|
111 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
112 |
+
|
113 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
114 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
115 |
+
|
116 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
117 |
+
|
118 |
+
new_model_id = "llm-jp-3-13b-lora-orca-ichikara2_Tengentoppa"
|
119 |
+
|
120 |
+
# 出力の保存
|
121 |
+
# 最後に、adapter_idをベースにしたファイル名でJSONL形式の出力ファイルを保存します。
|
122 |
+
|
123 |
+
with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
124 |
+
for result in results:
|
125 |
+
json.dump(result, f, ensure_ascii=False)
|
126 |
+
f.write('\n')
|
127 |
+
|
128 |
+
```
|
129 |
+
以上の手順で、{adapter_id}-outputs.jsonlというファイルに推論結果が書き出されます。
|