Update README.md
Browse files
README.md
CHANGED
@@ -34,6 +34,108 @@ This is the model card of a 🤗 transformers model that has been pushed on the
|
|
34 |
- **Paper [optional]:** [More Information Needed]
|
35 |
- **Demo [optional]:** [More Information Needed]
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
## Uses
|
38 |
|
39 |
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
|
|
34 |
- **Paper [optional]:** [More Information Needed]
|
35 |
- **Demo [optional]:** [More Information Needed]
|
36 |
|
37 |
+
## Usage
|
38 |
+
|
39 |
+
```python
|
40 |
+
!pip install -U bitsandbytes
|
41 |
+
!pip install -U transformers
|
42 |
+
!pip install -U accelerate
|
43 |
+
!pip install -U datasets
|
44 |
+
```
|
45 |
+
|
46 |
+
```python
|
47 |
+
from transformers import (
|
48 |
+
AutoModelForCausalLM,
|
49 |
+
AutoTokenizer,
|
50 |
+
BitsAndBytesConfig,
|
51 |
+
)
|
52 |
+
import torch
|
53 |
+
from tqdm import tqdm
|
54 |
+
import json
|
55 |
+
```
|
56 |
+
|
57 |
+
```python
|
58 |
+
HF_TOKEN = "YOUR-HF-TOKEN"
|
59 |
+
```
|
60 |
+
|
61 |
+
```python
|
62 |
+
base_model_id = "llm-jp/llm-jp-3-13b"
|
63 |
+
adapter_id = "koichi-sumizaki/llm-jp-3-13b-it2024-12-14_lora"
|
64 |
+
```
|
65 |
+
|
66 |
+
```python
|
67 |
+
# QLoRA config
|
68 |
+
bnb_config = BitsAndBytesConfig(
|
69 |
+
load_in_4bit=True,
|
70 |
+
bnb_4bit_quant_type="nf4",
|
71 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
72 |
+
)
|
73 |
+
```
|
74 |
+
|
75 |
+
```python
|
76 |
+
# Load model
|
77 |
+
model = AutoModelForCausalLM.from_pretrained(
|
78 |
+
base_model_id,
|
79 |
+
quantization_config=bnb_config,
|
80 |
+
device_map="auto",
|
81 |
+
token = HF_TOKEN
|
82 |
+
)
|
83 |
+
|
84 |
+
# Load tokenizer
|
85 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, token = HF_TOKEN)
|
86 |
+
|
87 |
+
# 元のモデルにLoRAのアダプタを統合。
|
88 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
89 |
+
```
|
90 |
+
|
91 |
+
```python
|
92 |
+
datasets = []
|
93 |
+
with open("./YOUR-DATA.jsonl", "r") as f:
|
94 |
+
item = ""
|
95 |
+
for line in f:
|
96 |
+
line = line.strip()
|
97 |
+
item += line
|
98 |
+
if item.endswith("}"):
|
99 |
+
datasets.append(json.loads(item))
|
100 |
+
item = ""
|
101 |
+
```
|
102 |
+
|
103 |
+
```python
|
104 |
+
results = []
|
105 |
+
for data in tqdm(datasets):
|
106 |
+
|
107 |
+
input = data["input"]
|
108 |
+
|
109 |
+
prompt = f"""### 指示
|
110 |
+
{input}
|
111 |
+
### 回答:
|
112 |
+
"""
|
113 |
+
|
114 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
115 |
+
with torch.no_grad():
|
116 |
+
outputs = model.generate(
|
117 |
+
tokenized_input,
|
118 |
+
max_new_tokens=512,
|
119 |
+
do_sample=True,
|
120 |
+
temperature=0.1,
|
121 |
+
top_k=1,
|
122 |
+
top_p=1,
|
123 |
+
repetition_penalty=1.1
|
124 |
+
)[0]
|
125 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
126 |
+
|
127 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
128 |
+
```
|
129 |
+
|
130 |
+
```python
|
131 |
+
import re
|
132 |
+
model_name = re.sub(".*/", "", model_name)
|
133 |
+
with open(f"./{model_name}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
134 |
+
for result in results:
|
135 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
136 |
+
f.write('\n')
|
137 |
+
```
|
138 |
+
|
139 |
## Uses
|
140 |
|
141 |
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|