File size: 5,683 Bytes
2fad6bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70688b6
2fad6bb
 
9cdba96
 
8e13a1d
 
9cdba96
9499a83
941c3a6
d6f87ec
 
 
 
 
f781a2d
81a8335
 
f781a2d
9cdba96
15bf711
24b9b8f
15bf711
 
 
 
7d72697
15bf711
 
 
7d72697
15bf711
 
7d72697
15bf711
 
 
4884d9f
15bf711
 
4884d9f
15bf711
 
 
 
 
 
 
 
 
 
 
 
4884d9f
15bf711
 
 
 
 
4884d9f
15bf711
dc2a6e4
4884d9f
dc2a6e4
 
 
 
 
4884d9f
dc2a6e4
 
 
4884d9f
dc2a6e4
 
4884d9f
dc2a6e4
 
 
 
 
 
 
4884d9f
dc2a6e4
 
 
 
 
 
 
 
4884d9f
dc2a6e4
 
 
 
 
4884d9f
dc2a6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4884d9f
dc2a6e4
 
 
 
 
24b9b8f
9cdba96
9499a83
2e50cc2
d6f87ec
 
 
 
 
98d49af
d6f87ec
 
 
 
 
 
 
 
98d49af
d6f87ec
2e50cc2
2fad6bb
 
1455323
1
2
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
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
164
165
166
---
base_model: llm-jp/llm-jp-3-13b
tags:
- text-generation-inference
- transformers
- unsloth
- llama
- trl
language:
- en
---

# Uploaded  model

- **Developed by:** ShotaMatsumoto
- **License:** CC BY-NC-SA 
- **Finetuned from model :** llm-jp/llm-jp-3-13b

# 概要
「llm-jp/llm-jp-3-13bモデルにichikara-instructionデータを用いてファインチューニングを行ったモデルです。
松尾研究室の講義のコンペ用としてモデル作成しました。

https://weblab.t.u-tokyo.ac.jp/lecture/course-list/large-language-model/
# データセットは以下を使いました。
-    ichikara-instruction-003-001-1.json
-    ichikara-instruction-003-001-2.1.json
-    ichikara-instruction-003-001-2.2.json
-    ichikara-instruction-003-001-5.1.json
-    ichikara-instruction-003-001-5.2.json
-    ichikara-instruction-003-003-1.json
# 注意
ichikara-instructionデータは、CC BY-NC-SAライセンス(表示-非営利-継承)で公開されています。このライセンスの下では、非営利目的での利用が許可されていますが、商用利用は認められていません。
詳しくはこちらのホームページで確認してください。https://llm-jp.nii.ac.jp/blog/2024/04/30/v2.0-release.html

# 推論方法
事前にadapter_model.safetensorsをダウンロードしてください。
```python
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import torch

#ベースモデル ID とアダプタファイルパス
base_model_id = "llm-jp/llm-jp-3-13b"
adapter_model_path = ""/path/to/adapter_model.safetensors""

#デバイス設定
device = "cuda" if torch.cuda.is_available() else "cpu"

#トークナイザーとベースモデルのロード
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
base_model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.float16).to(device)

#アダプタの読み込み
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device)

#推論関数
def generate_text(prompt, max_length=256, temperature=0.7):
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    outputs = model.generate(
        inputs["input_ids"],
        max_length=max_length,
        temperature=temperature,
        do_sample=True,
        top_k=50,
        top_p=0.9
    )
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

#テストプロンプト
prompt = "日本の経済について説明してください。"
print("Generating text...")
generated_text = generate_text(prompt)
print("\nGenerated Text:")
print(generated_text)
```

# 量子化
```python
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import torch
from transformers import BitsAndBytesConfig

#ベースモデル ID とアダプタファイルパス
base_model_id = "llm-jp/llm-jp-3-13b"
adapter_model_path = "path/to/"

#デバイス設定
device = "cuda" if torch.cuda.is_available() else "cpu"

#量子化の設定
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,  # 4-bit 量子化を有効化
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",  # 量子化スキーム
    bnb_4bit_compute_dtype=torch.float16,  # 推論時の計算精度
)

#トークナイザーのロード
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)

ベースモデルのロード(量子化設定を使用)
base_model = AutoModelForCausalLM.from_pretrained(
    base_model_id,
    quantization_config=bnb_config,
    device_map="auto",  # 自動的に GPU に割り当て
)
#アダプタの読み込み
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device)

`pad_token_id` の設定(トークナイザーから取得)
model.config.pad_token_id = tokenizer.pad_token_id

#推論関数
def generate_text(prompt, max_length=256, temperature=0.7):
    # トークナイズして `attention_mask` を設定し、max_length を適用
    inputs = tokenizer(
        prompt,
        return_tensors="pt",
        padding=True,
        truncation=True,
        max_length=max_length  # 最大トークン数を制限
    ).to(device)
    
    outputs = model.generate(
        inputs["input_ids"],
        attention_mask=inputs["attention_mask"],
        max_length=max_length,
        temperature=temperature,
        do_sample=True,
        top_k=50,
        top_p=0.9,
        pad_token_id=tokenizer.pad_token_id  # 安全な動作のため明示的に指定
    )
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

#テストプロンプト
prompt = "日本の経済について説明してください。"
print("Generating text...")
generated_text = generate_text(prompt, max_length=256)  # 最大長さを明示的に指定
print("\nGenerated Text:")
print(generated_text)
```

# jsonlファイルの出力方法は以下の通りです。
```python
import json
from google.colab import files

file_name = "elyza-tasks-results.jsonl"

# JSON形式で保存
with open(file_name, "w", encoding="utf-8") as f:
    json.dump(results, f, ensure_ascii=False)

# 内容確認
with open(file_name, 'w', encoding='utf-8') as f:
    for result in results:
        json.dump(result, f, ensure_ascii=False)
        f.write('\n')
# ファイルのダウンロード
files.download(file_name)
```
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.

[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)