Update README.md
Browse files
README.md
CHANGED
@@ -40,25 +40,20 @@ from peft import PeftModel
|
|
40 |
import torch
|
41 |
|
42 |
#ベースモデル ID とアダプタファイルパス
|
43 |
-
|
44 |
base_model_id = "llm-jp/llm-jp-3-13b"
|
45 |
adapter_model_path = ""/path/to/adapter_model.safetensors""
|
46 |
|
47 |
#デバイス設定
|
48 |
-
|
49 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
50 |
|
51 |
#トークナイザーとベースモデルのロード
|
52 |
-
|
53 |
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
|
54 |
base_model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.float16).to(device)
|
55 |
|
56 |
-
|
57 |
-
|
58 |
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
def generate_text(prompt, max_length=256, temperature=0.7):
|
63 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
64 |
outputs = model.generate(
|
@@ -71,29 +66,29 @@ def generate_text(prompt, max_length=256, temperature=0.7):
|
|
71 |
)
|
72 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
prompt = "日本の経済について説明してください。"
|
77 |
print("Generating text...")
|
78 |
generated_text = generate_text(prompt)
|
79 |
print("\nGenerated Text:")
|
80 |
print(generated_text)
|
|
|
81 |
|
82 |
# 量子化
|
83 |
-
|
84 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
85 |
from peft import PeftModel
|
86 |
import torch
|
87 |
from transformers import BitsAndBytesConfig
|
88 |
|
89 |
-
|
90 |
base_model_id = "llm-jp/llm-jp-3-13b"
|
91 |
adapter_model_path = "path/to/"
|
92 |
|
93 |
-
|
94 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
95 |
|
96 |
-
|
97 |
bnb_config = BitsAndBytesConfig(
|
98 |
load_in_4bit=True, # 4-bit 量子化を有効化
|
99 |
bnb_4bit_use_double_quant=True,
|
@@ -101,7 +96,7 @@ bnb_config = BitsAndBytesConfig(
|
|
101 |
bnb_4bit_compute_dtype=torch.float16, # 推論時の計算精度
|
102 |
)
|
103 |
|
104 |
-
|
105 |
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
|
106 |
|
107 |
ベースモデルのロード(量子化設定を使用)
|
@@ -110,14 +105,13 @@ base_model = AutoModelForCausalLM.from_pretrained(
|
|
110 |
quantization_config=bnb_config,
|
111 |
device_map="auto", # 自動的に GPU に割り当て
|
112 |
)
|
113 |
-
|
114 |
-
アダプタの読み込み
|
115 |
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device)
|
116 |
|
117 |
`pad_token_id` の設定(トークナイザーから取得)
|
118 |
model.config.pad_token_id = tokenizer.pad_token_id
|
119 |
|
120 |
-
|
121 |
def generate_text(prompt, max_length=256, temperature=0.7):
|
122 |
# トークナイズして `attention_mask` を設定し、max_length を適用
|
123 |
inputs = tokenizer(
|
@@ -140,7 +134,7 @@ def generate_text(prompt, max_length=256, temperature=0.7):
|
|
140 |
)
|
141 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
142 |
|
143 |
-
|
144 |
prompt = "日本の経済について説明してください。"
|
145 |
print("Generating text...")
|
146 |
generated_text = generate_text(prompt, max_length=256) # 最大長さを明示的に指定
|
|
|
40 |
import torch
|
41 |
|
42 |
#ベースモデル ID とアダプタファイルパス
|
|
|
43 |
base_model_id = "llm-jp/llm-jp-3-13b"
|
44 |
adapter_model_path = ""/path/to/adapter_model.safetensors""
|
45 |
|
46 |
#デバイス設定
|
|
|
47 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
48 |
|
49 |
#トークナイザーとベースモデルのロード
|
|
|
50 |
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
|
51 |
base_model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.float16).to(device)
|
52 |
|
53 |
+
#アダプタの読み込み
|
|
|
54 |
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device)
|
55 |
|
56 |
+
#推論関数
|
|
|
57 |
def generate_text(prompt, max_length=256, temperature=0.7):
|
58 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
59 |
outputs = model.generate(
|
|
|
66 |
)
|
67 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
68 |
|
69 |
+
#テストプロンプト
|
|
|
70 |
prompt = "日本の経済について説明してください。"
|
71 |
print("Generating text...")
|
72 |
generated_text = generate_text(prompt)
|
73 |
print("\nGenerated Text:")
|
74 |
print(generated_text)
|
75 |
+
```
|
76 |
|
77 |
# 量子化
|
78 |
+
```python
|
79 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
80 |
from peft import PeftModel
|
81 |
import torch
|
82 |
from transformers import BitsAndBytesConfig
|
83 |
|
84 |
+
#ベースモデル ID とアダプタファイルパス
|
85 |
base_model_id = "llm-jp/llm-jp-3-13b"
|
86 |
adapter_model_path = "path/to/"
|
87 |
|
88 |
+
#デバイス設定
|
89 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
90 |
|
91 |
+
#量子化の設定
|
92 |
bnb_config = BitsAndBytesConfig(
|
93 |
load_in_4bit=True, # 4-bit 量子化を有効化
|
94 |
bnb_4bit_use_double_quant=True,
|
|
|
96 |
bnb_4bit_compute_dtype=torch.float16, # 推論時の計算精度
|
97 |
)
|
98 |
|
99 |
+
#トークナイザーのロード
|
100 |
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
|
101 |
|
102 |
ベースモデルのロード(量子化設定を使用)
|
|
|
105 |
quantization_config=bnb_config,
|
106 |
device_map="auto", # 自動的に GPU に割り当て
|
107 |
)
|
108 |
+
#アダプタの読み込み
|
|
|
109 |
model = PeftModel.from_pretrained(base_model, adapter_model_path).to(device)
|
110 |
|
111 |
`pad_token_id` の設定(トークナイザーから取得)
|
112 |
model.config.pad_token_id = tokenizer.pad_token_id
|
113 |
|
114 |
+
#推論関数
|
115 |
def generate_text(prompt, max_length=256, temperature=0.7):
|
116 |
# トークナイズして `attention_mask` を設定し、max_length を適用
|
117 |
inputs = tokenizer(
|
|
|
134 |
)
|
135 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
136 |
|
137 |
+
#テストプロンプト
|
138 |
prompt = "日本の経済について説明してください。"
|
139 |
print("Generating text...")
|
140 |
generated_text = generate_text(prompt, max_length=256) # 最大長さを明示的に指定
|