Update README.md
Browse files
README.md
CHANGED
|
@@ -3,200 +3,226 @@ base_model: google/gemma-2-9b-it
|
|
| 3 |
library_name: peft
|
| 4 |
---
|
| 5 |
|
| 6 |
-
#
|
| 7 |
|
| 8 |
-
|
| 9 |
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
##
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
###
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
-
|
| 148 |
-
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
#### Hardware
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Software
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
## Citation [optional]
|
| 172 |
-
|
| 173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
-
|
| 175 |
-
**BibTeX:**
|
| 176 |
-
|
| 177 |
-
[More Information Needed]
|
| 178 |
-
|
| 179 |
-
**APA:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
-
|
| 185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
-
|
| 187 |
-
[More Information Needed]
|
| 188 |
-
|
| 189 |
-
## More Information [optional]
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## Model Card Authors [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Contact
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
| 200 |
-
### Framework versions
|
| 201 |
-
|
| 202 |
-
- PEFT 0.14.0
|
|
|
|
| 3 |
library_name: peft
|
| 4 |
---
|
| 5 |
|
| 6 |
+
# Gemma-9B Instruction Tuned Model Inference Guide
|
| 7 |
|
| 8 |
+
このガイドでは、Hugging Faceからhiraki/gemma9b-it-sftモデルをダウンロードし、推論を実行する手順を説明します。
|
| 9 |
|
| 10 |
+
## パッケージのインストール
|
| 11 |
|
| 12 |
+
はじめに、必要なパッケージをインストールします:
|
| 13 |
|
| 14 |
+
```bash
|
| 15 |
+
# 依存関係のアップグレード
|
| 16 |
+
pip install --upgrade datasets bitsandbytes trl peft
|
| 17 |
+
pip install --upgrade torch
|
| 18 |
|
| 19 |
+
# その他の必要なパッケージ
|
| 20 |
+
pip install transformers tqdm
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
## 環境設定
|
| 24 |
+
|
| 25 |
+
- CUDA対応のGPU(最低16GB以上のVRAMを推奨)
|
| 26 |
+
- Python 3.8以上
|
| 27 |
+
- 十分なストレージ空間(モデルのダウンロード用)
|
| 28 |
|
| 29 |
+
## インストールの確認
|
| 30 |
+
|
| 31 |
+
インストール後、以下のコードで環境を確認できます:
|
| 32 |
+
|
| 33 |
+
```python
|
| 34 |
+
import torch
|
| 35 |
+
print(f"PyTorch version: {torch.__version__}")
|
| 36 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 37 |
+
print(f"CUDA device count: {torch.cuda.device_count()}")
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
## 手順
|
| 41 |
+
|
| 42 |
+
### 1. Hugging Faceの設定
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
import os
|
| 46 |
+
|
| 47 |
+
# HuggingFaceのトークンを設定
|
| 48 |
+
os.environ["HF_TOKEN"] = "your_token_here" # あなたのトークンに置き換えてください
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
### 2. 推論スクリプトの作成
|
| 52 |
+
|
| 53 |
+
以下の内容で`inference.py`を作成してください:
|
| 54 |
+
|
| 55 |
+
```python
|
| 56 |
+
import os
|
| 57 |
+
import json
|
| 58 |
+
import torch
|
| 59 |
+
from tqdm import tqdm
|
| 60 |
+
from peft import AutoPeftModelForCausalLM
|
| 61 |
+
from transformers import AutoTokenizer
|
| 62 |
+
|
| 63 |
+
def run_inference(model_path, input_file, output_file):
|
| 64 |
+
# モデルとトークナイザーの読み込み
|
| 65 |
+
print("モデルを読み込んでいます...")
|
| 66 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 67 |
+
model_path,
|
| 68 |
+
device_map={"": "cuda"},
|
| 69 |
+
torch_dtype=torch.float16,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 73 |
+
|
| 74 |
+
if tokenizer.pad_token is None:
|
| 75 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 76 |
+
tokenizer.padding_side = "right"
|
| 77 |
+
|
| 78 |
+
# 入力データの読み込み
|
| 79 |
+
print("入力データを読み込んでいます...")
|
| 80 |
+
data = []
|
| 81 |
+
with open(input_file, 'r', encoding='utf-8') as f:
|
| 82 |
+
for line in f:
|
| 83 |
+
data.append(json.loads(line))
|
| 84 |
|
| 85 |
+
# 推論実行
|
| 86 |
+
results = []
|
| 87 |
+
print("推論を実行中...")
|
| 88 |
+
for dt in tqdm(data, desc="Processing"):
|
| 89 |
+
task_id = dt["task_id"]
|
| 90 |
+
input_text = dt["input"]
|
| 91 |
+
|
| 92 |
+
# プロンプトの生成
|
| 93 |
+
prompt = f"### 指示\n{input_text}\n### 回答\n"
|
| 94 |
+
|
| 95 |
+
# 入力のトークン化
|
| 96 |
+
inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
|
| 97 |
+
|
| 98 |
+
# 出力の生成
|
| 99 |
+
outputs = model.generate(
|
| 100 |
+
inputs.input_ids,
|
| 101 |
+
attention_mask=inputs.attention_mask,
|
| 102 |
+
max_new_tokens=512,
|
| 103 |
+
temperature=0.7,
|
| 104 |
+
top_p=0.9,
|
| 105 |
+
repetition_penalty=1.2,
|
| 106 |
+
do_sample=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# 出力のデコード
|
| 110 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('### 回答\n')[-1].strip()
|
| 111 |
+
|
| 112 |
+
# 結果を保存
|
| 113 |
+
results.append({
|
| 114 |
+
"task_id": task_id,
|
| 115 |
+
"input": input_text,
|
| 116 |
+
"output": prediction
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
# 結果の保存
|
| 120 |
+
print(f"結果を保存中: {output_file}")
|
| 121 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
| 122 |
+
for result in results:
|
| 123 |
+
json.dump(result, f, ensure_ascii=False)
|
| 124 |
+
f.write('\n')
|
| 125 |
+
|
| 126 |
+
print("処理が完了しました")
|
| 127 |
+
return results
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
model_path = "hiraki/gemma9b-it-sft"
|
| 131 |
+
input_file = "elyza-tasks-100-TV_0.jsonl" # 入力ファイル名
|
| 132 |
+
output_file = "output.jsonl" # 出力ファイル名
|
| 133 |
+
|
| 134 |
+
results = run_inference(model_path, input_file, output_file)
|
| 135 |
+
|
| 136 |
+
# 最初の結果を表示して確認
|
| 137 |
+
print("\n最初の結果の例:")
|
| 138 |
+
print(json.dumps(results[0], ensure_ascii=False, indent=2))
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
### 3. 入力データの準備
|
| 142 |
+
|
| 143 |
+
入力JSONLファイルは以下の形式である必要があります:
|
| 144 |
+
|
| 145 |
+
```json
|
| 146 |
+
{
|
| 147 |
+
"task_id": "タスクの一意な識別子",
|
| 148 |
+
"input": "入力テキスト"
|
| 149 |
+
}
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
### 4. 実行
|
| 153 |
+
|
| 154 |
+
```bash
|
| 155 |
+
python inference.py
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
## 出力形式
|
| 159 |
+
|
| 160 |
+
出力されるJSONLファイルは以下の形式です:
|
| 161 |
+
|
| 162 |
+
```json
|
| 163 |
+
{
|
| 164 |
+
"task_id": "タスクID",
|
| 165 |
+
"input": "入力テキスト",
|
| 166 |
+
"output": "生成されたテキスト"
|
| 167 |
+
}
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
## 生成パラメータのカスタマイズ
|
| 171 |
+
|
| 172 |
+
スクリプト内の以下のパラメータを調整することで、生成結果をカスタマイズできます:
|
| 173 |
+
|
| 174 |
+
- `max_new_tokens`: 生成する最大トークン数 (デフォルト: 512)
|
| 175 |
+
- `temperature`: 生成の多様性 (デフォルト: 0.7)
|
| 176 |
+
- `top_p`: サンプリングの閾値 (デフォルト: 0.9)
|
| 177 |
+
- `repetition_penalty`: 繰り返しの抑制 (デフォルト: 1.2)
|
| 178 |
+
- `do_sample`: ランダムサンプリングの有効/無効 (デフォルト: False)
|
| 179 |
+
|
| 180 |
+
## トラブルシューティング
|
| 181 |
+
|
| 182 |
+
### よくあるエラー
|
| 183 |
+
|
| 184 |
+
1. CUDA Out of Memory
|
| 185 |
+
```
|
| 186 |
+
RuntimeError: CUDA out of memory
|
| 187 |
+
```
|
| 188 |
+
- 解決策:
|
| 189 |
+
- GPUのメモリを解放する
|
| 190 |
+
- `max_new_tokens`の値を小さくする
|
| 191 |
+
- より小さいバッチサイズを使用する
|
| 192 |
+
|
| 193 |
+
2. パッケージのバージョンの不一致
|
| 194 |
+
```
|
| 195 |
+
ImportError: Cannot import name 'X' from 'Y'
|
| 196 |
+
```
|
| 197 |
+
- 解決策:
|
| 198 |
+
- すべてのパッケージを最新バージョンにアップグレードする
|
| 199 |
+
- `pip install --upgrade`コマンドを再実行する
|
| 200 |
+
|
| 201 |
+
3. モデルのダウンロードエラー
|
| 202 |
+
```
|
| 203 |
+
OSError: Incorrect Hugging Face token
|
| 204 |
+
```
|
| 205 |
+
- 解決策:
|
| 206 |
+
- HF_TOKENが正しく設定されているか確認
|
| 207 |
+
- インターネット接続を確認
|
| 208 |
+
|
| 209 |
+
4. 入力ファイルのフォーマットエラー
|
| 210 |
+
```
|
| 211 |
+
JSONDecodeError: Expecting value
|
| 212 |
+
```
|
| 213 |
+
- 解決策:
|
| 214 |
+
- 入力JSONLファイルの形式を確認
|
| 215 |
+
- ファイルのエンコーディングがUTF-8であることを確認
|
| 216 |
+
|
| 217 |
+
## 注意事項
|
| 218 |
+
|
| 219 |
+
- 推論には十分なGPUメモリが必要です
|
| 220 |
+
- 大量のデータを処理する場合は、進捗バーで進行状況を確認できます
|
| 221 |
+
- 出力ファイルは自動的に上書きされるため、必要に応じてバックアップを作成してください
|
| 222 |
+
|
| 223 |
+
## サポート
|
| 224 |
+
|
| 225 |
+
問題が発生した場合は、以下を確認してください:
|
| 226 |
+
1. Hugging Faceのモデルカード: [hiraki/gemma9b-it-sft](https://huggingface.co/hiraki/gemma9b-it-sft)
|
| 227 |
+
2. PEFT GitHub: [microsoft/PEFT](https://github.com/microsoft/PEFT)
|
| 228 |
+
3. Transformers GitHub: [huggingface/transformers](https://github.com/huggingface/transformers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|