Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,128 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
---
|
3 |
+
base_model: llm-jp/llm-jp-3-13b
|
4 |
+
library_name: peft
|
5 |
+
---
|
6 |
+
|
7 |
+
# モデルカード: llm-jp/llm-jp-3-13b
|
8 |
+
|
9 |
+
## モデル詳細
|
10 |
+
|
11 |
+
### モデル概要
|
12 |
+
|
13 |
+
このモデルは、松尾研LLM講座の終了課題の提出用のモデルです。
|
14 |
+
|
15 |
+
- **開発者:** [masakiai]
|
16 |
+
- **ファインチューニング元モデル:** [llm-jp/llm-jp-3-13b]
|
17 |
+
- **対応言語:** 日本語
|
18 |
+
- **ライセンス:** [apache-2.0]
|
19 |
+
|
20 |
+
### モデルソース
|
21 |
+
|
22 |
+
- **リポジトリ:** [https://huggingface.co/masakiai/llm-jp-3-13b-finetune]
|
23 |
+
|
24 |
+
---
|
25 |
+
|
26 |
+
## 使用方法
|
27 |
+
|
28 |
+
### 以下は、elyza-tasks-100-TV-0.jsonlの回答のためのコードです
|
29 |
+
|
30 |
+
```python
|
31 |
+
import torch
|
32 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
33 |
+
import json
|
34 |
+
from tqdm import tqdm
|
35 |
+
import os
|
36 |
+
import re
|
37 |
+
|
38 |
+
# 環境変数の設定
|
39 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
40 |
+
model_name = "masakiai/llm-jp-3-13b-finetune"
|
41 |
+
ELYZA_TASKS_100_TV_0_JSONL_PATH = "./elyza-tasks-100-TV_0.jsonl"
|
42 |
+
|
43 |
+
# 8ビット量子化の設定
|
44 |
+
bnb_config = BitsAndBytesConfig(
|
45 |
+
load_in_4bit=True,
|
46 |
+
bnb_4bit_quant_type="nf4",
|
47 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
48 |
+
)
|
49 |
+
|
50 |
+
# モデルの読み込み
|
51 |
+
model = AutoModelForCausalLM.from_pretrained(
|
52 |
+
model_name,
|
53 |
+
quantization_config=bnb_config,
|
54 |
+
device_map="auto"
|
55 |
+
)
|
56 |
+
|
57 |
+
# トークナイザーの読み込み
|
58 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
59 |
+
|
60 |
+
# データセットの読み込み
|
61 |
+
datasets = []
|
62 |
+
with open(ELYZA_TASKS_100_TV_0_JSONL_PATH , "r") as f:
|
63 |
+
item = ""
|
64 |
+
for line in f:
|
65 |
+
line = line.strip()
|
66 |
+
item += line
|
67 |
+
if item.endswith("}"):
|
68 |
+
datasets.append(json.loads(item))
|
69 |
+
item = ""
|
70 |
+
|
71 |
+
# 推論の実行
|
72 |
+
results = []
|
73 |
+
for data in tqdm(datasets):
|
74 |
+
|
75 |
+
input = data["input"]
|
76 |
+
|
77 |
+
prompt = f"""### 指示
|
78 |
+
{input}
|
79 |
+
### 回答
|
80 |
+
"""
|
81 |
+
|
82 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
83 |
+
attention_mask = torch.ones_like(tokenized_input)
|
84 |
+
|
85 |
+
with torch.no_grad():
|
86 |
+
outputs = model.generate(
|
87 |
+
tokenized_input,
|
88 |
+
attention_mask=attention_mask,
|
89 |
+
max_new_tokens=100,
|
90 |
+
do_sample=False,
|
91 |
+
repetition_penalty=1.2,
|
92 |
+
pad_token_id=tokenizer.eos_token_id
|
93 |
+
)[0]
|
94 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
95 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
96 |
+
|
97 |
+
|
98 |
+
# ファイルの保存
|
99 |
+
jsonl_id = re.sub(".*/", "", model_name)
|
100 |
+
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
101 |
+
for result in results:
|
102 |
+
json.dump(result, f, ensure_ascii=False)
|
103 |
+
f.write('\n')
|
104 |
+
```
|
105 |
+
|
106 |
+
### 直接的な使用
|
107 |
+
|
108 |
+
このモデルは以下のような日本語タスクに使用できます:
|
109 |
+
- テキスト生成
|
110 |
+
- 質問応答
|
111 |
+
- 翻訳
|
112 |
+
- 要約
|
113 |
+
|
114 |
+
```python
|
115 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
116 |
+
import os
|
117 |
+
|
118 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
119 |
+
model_name = "masakiai/llm-jp-3-13b-finetune"
|
120 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
121 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
122 |
+
|
123 |
+
text = "日本の文化について教えてください。"
|
124 |
+
input_ids = tokenizer(text, return_tensors="pt").input_ids
|
125 |
+
output = model.generate(input_ids, max_length=50)
|
126 |
+
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
127 |
+
```
|
128 |
+
---
|