Update README.md
Browse files
README.md
CHANGED
|
@@ -1,119 +1,125 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
| 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 |
-
prompt
|
| 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 |
**注意**: このモデルを使用する際は、Gemma-2-9bのライセンスに従ってください。
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
datasets:
|
| 4 |
+
- elyza/ELYZA-tasks-100
|
| 5 |
+
language:
|
| 6 |
+
- ja
|
| 7 |
+
base_model:
|
| 8 |
+
- google/gemma-2-9b
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# lora_gemma_9b
|
| 12 |
+
|
| 13 |
+
このモデルは、[Gemma-2-9b](https://huggingface.co/google/gemma-2-9b) をベースに微調整されたモデルです。
|
| 14 |
+
LLM講座の最終課題のために作成されたものになります。
|
| 15 |
+
|
| 16 |
+
## 出力方法
|
| 17 |
+
|
| 18 |
+
以下のコードを使用して、モデルをロードし、結果を生成できます。
|
| 19 |
+
|
| 20 |
+
```python
|
| 21 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 22 |
+
from peft import PeftModel
|
| 23 |
+
from datasets import load_dataset
|
| 24 |
+
from tqdm import tqdm
|
| 25 |
+
import torch
|
| 26 |
+
import json
|
| 27 |
+
|
| 28 |
+
# トークナイザーのロード
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained("ultimatemagic79/lora_gemma_9b", use_fast=False)
|
| 30 |
+
|
| 31 |
+
# ベースモデルのロード
|
| 32 |
+
bnb_config = BitsAndBytesConfig(
|
| 33 |
+
load_in_8bit=True,
|
| 34 |
+
llm_int8_threshold=6.0,
|
| 35 |
+
)
|
| 36 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
+
"google/gemma-2-9b",
|
| 38 |
+
device_map="auto",
|
| 39 |
+
quantization_config=bnb_config,
|
| 40 |
+
torch_dtype=torch.float16,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# 微調整モデルのロード
|
| 44 |
+
model = PeftModel.from_pretrained(base_model, "ultimatemagic79/lora_gemma_9b")
|
| 45 |
+
|
| 46 |
+
# Few-Shot Promptの設定
|
| 47 |
+
dataset = load_dataset("elyza/ELYZA-tasks-100")
|
| 48 |
+
num_samples = 3
|
| 49 |
+
few_shot_samples = dataset["test"].select(range(num_samples))
|
| 50 |
+
|
| 51 |
+
# 推論の実行
|
| 52 |
+
# ELYZA-tasks-100-TVデータセットのロード
|
| 53 |
+
def load_elyza_tasks(file_path):
|
| 54 |
+
data = []
|
| 55 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 56 |
+
for line in f:
|
| 57 |
+
data.append(json.loads(line))
|
| 58 |
+
return data
|
| 59 |
+
|
| 60 |
+
test_dataset = load_elyza_tasks('elyza-tasks-100-TV_0.jsonl')
|
| 61 |
+
|
| 62 |
+
def generate_prompt(input_text, examples):
|
| 63 |
+
prompt = ""
|
| 64 |
+
for idx, example in enumerate(examples, 1):
|
| 65 |
+
prompt += f"[例{idx}]\n"
|
| 66 |
+
prompt += f"入力: {example['input']}\n"
|
| 67 |
+
prompt += f"出力: {example['output']}\n\n"
|
| 68 |
+
prompt += "[あなたの質問]\n"
|
| 69 |
+
prompt += f"入力: {input_text}\n"
|
| 70 |
+
prompt += "出力:"
|
| 71 |
+
return prompt
|
| 72 |
+
|
| 73 |
+
def generate_response(model, tokenizer, prompt):
|
| 74 |
+
inputs = tokenizer(
|
| 75 |
+
prompt,
|
| 76 |
+
return_tensors="pt",
|
| 77 |
+
truncation=True,
|
| 78 |
+
max_length=1024,
|
| 79 |
+
).to(model.device)
|
| 80 |
+
|
| 81 |
+
with torch.no_grad():
|
| 82 |
+
outputs = model.generate(
|
| 83 |
+
input_ids=inputs["input_ids"],
|
| 84 |
+
attention_mask=inputs["attention_mask"],
|
| 85 |
+
max_new_tokens=256,
|
| 86 |
+
temperature=0.7,
|
| 87 |
+
repetition_penalty=1.1,
|
| 88 |
+
top_p=0.9,
|
| 89 |
+
do_sample=True,
|
| 90 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
output_text = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
|
| 94 |
+
return output_text
|
| 95 |
+
|
| 96 |
+
# 推論と結果の収集
|
| 97 |
+
results = []
|
| 98 |
+
for test_data in tqdm(test_dataset):
|
| 99 |
+
task_id = test_data["task_id"]
|
| 100 |
+
input_text = test_data["input"]
|
| 101 |
+
prompt = generate_prompt(input_text, few_shot_samples)
|
| 102 |
+
response = generate_response(model, tokenizer, prompt)
|
| 103 |
+
results.append({
|
| 104 |
+
"task_id": task_id,
|
| 105 |
+
"output": response,
|
| 106 |
+
})
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
## ELYZA-tasks-100データセットの利用について
|
| 110 |
+
|
| 111 |
+
このモデルは、ELYZA社が公開する [ELYZA-tasks-100](https://huggingface.co/datasets/elyza/ELYZA-tasks-100)を使用してファインチューニング,プロンプトエンジニアリングを行っています。
|
| 112 |
+
|
| 113 |
+
ELYZA-tasks-100は [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/deed.ja)でライセンスされています。
|
| 114 |
+
|
| 115 |
+
詳細なライセンス情報は、[ELYZA-tasks-100のモデルカード](https://huggingface.co/datasets/elyza/ELYZA-tasks-100)をご参照ください。
|
| 116 |
+
|
| 117 |
+
## Gemma-2-9bの使用権利について
|
| 118 |
+
|
| 119 |
+
このモデルは、Google社が提供する [gemma-2-9b](https://huggingface.co/google/gemma-2-9b) をベースに微調整されています。
|
| 120 |
+
|
| 121 |
+
Gemma-2-9bは、商用利用が許可されたライセンスの下で公開されています。
|
| 122 |
+
|
| 123 |
+
詳細なライセンス情報は、[gemma-2-9bのモデルカード](https://huggingface.co/google/gemma-2-9b)をご参照ください。
|
| 124 |
+
|
| 125 |
**注意**: このモデルを使用する際は、Gemma-2-9bのライセンスに従ってください。
|