Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,84 @@
|
|
1 |
---
|
2 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
+
datasets:
|
4 |
+
- Anthropic/hh-rlhf
|
5 |
+
- kunishou/hh-rlhf-49k-ja
|
6 |
+
language:
|
7 |
+
- ja
|
8 |
+
library_name: transformers
|
9 |
+
pipeline_tag: text-generation
|
10 |
---
|
11 |
+
[cyberagent/open-calm-7b](https://huggingface.co/cyberagent/open-calm-7b)に対して[kunishou/
|
12 |
+
hh-rlhf-49k-ja](https://huggingface.co/datasets/kunishou/hh-rlhf-49k-ja)をpeftを用いて(というより[tloen/alpaca-lora](https://github.com/tloen/alpaca-lora)を改変して)チューニングしたものの差分です。
|
13 |
+
lora-alpacaから学習時のパラメータは特に変えていません。
|
14 |
+
```
|
15 |
+
import torch
|
16 |
+
from peft import PeftModel
|
17 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
18 |
+
|
19 |
+
LOAD_8BIT = False
|
20 |
+
BASE_MODEL = "cyberagent/open-calm-7b"
|
21 |
+
LORA_WEIGHTS = "nakayama/lora-hh-rlhf-49k-ja-for-open-calm-7b"
|
22 |
+
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
24 |
+
|
25 |
+
model = AutoModelForCausalLM.from_pretrained(
|
26 |
+
BASE_MODEL,
|
27 |
+
load_in_8bit=LOAD_8BIT,
|
28 |
+
torch_dtype=torch.float16,
|
29 |
+
device_map="auto",
|
30 |
+
)
|
31 |
+
model = PeftModel.from_pretrained(
|
32 |
+
model,
|
33 |
+
LORA_WEIGHTS,
|
34 |
+
torch_dtype=torch.float16,
|
35 |
+
adapter_name=LORA_WEIGHTS
|
36 |
+
)
|
37 |
+
|
38 |
+
def generate_prompt(instruction, input=None):
|
39 |
+
if input:
|
40 |
+
return f"""以下は、タスクを説明する命令と、さらなるコンテキストを提供する入力の組み合わせです。要求を適切に満たすような応答を書きなさい。
|
41 |
+
|
42 |
+
### Instruction:
|
43 |
+
{instruction}
|
44 |
+
|
45 |
+
### Input:
|
46 |
+
{input}
|
47 |
+
|
48 |
+
### Response:"""
|
49 |
+
else:
|
50 |
+
return f"""以下は、ある作業を記述した指示です。依頼を適切に完了させる回答を書きなさい。
|
51 |
+
|
52 |
+
### Instruction:
|
53 |
+
{instruction}
|
54 |
+
|
55 |
+
### Response:"""
|
56 |
+
|
57 |
+
if not LOAD_8BIT:
|
58 |
+
model.half()
|
59 |
+
|
60 |
+
instruction="次の日本の観光地について説明してください。"
|
61 |
+
|
62 |
+
input="富士山"
|
63 |
+
|
64 |
+
prompt = generate_prompt(instruction, input)
|
65 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
66 |
+
|
67 |
+
with torch.no_grad():
|
68 |
+
generation_output = model.generate(
|
69 |
+
**inputs,
|
70 |
+
do_sample=True,
|
71 |
+
temperature=0.1,
|
72 |
+
top_p=0.75,
|
73 |
+
top_k=20,
|
74 |
+
return_dict_in_generate=True,
|
75 |
+
output_scores=True,
|
76 |
+
max_new_tokens=128,
|
77 |
+
repetition_penalty=1.5,
|
78 |
+
no_repeat_ngram_size=5,
|
79 |
+
pad_token_id=tokenizer.pad_token_id,
|
80 |
+
)
|
81 |
+
s = generation_output.sequences[0]
|
82 |
+
output = tokenizer.decode(s)
|
83 |
+
print(output.split("### Response:")[1].strip())
|
84 |
+
```
|