update readme
Browse files
README.md
CHANGED
@@ -21,7 +21,97 @@ This llama model was trained 2x faster with [Unsloth](https://github.com/unsloth
|
|
21 |
|
22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
23 |
|
24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
```
|
26 |
!pip uninstall unsloth -y
|
27 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
|
|
21 |
|
22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
23 |
|
24 |
+
# 使用方法
|
25 |
+
```
|
26 |
+
!pip install -U bitsandbytes
|
27 |
+
!pip install -U transformers
|
28 |
+
!pip install -U accelerate
|
29 |
+
!pip install -U datasets
|
30 |
+
!pip install -U peft
|
31 |
+
!pip install ipywidgets --upgrade
|
32 |
+
from transformers import (
|
33 |
+
AutoModelForCausalLM,
|
34 |
+
AutoTokenizer,
|
35 |
+
BitsAndBytesConfig,
|
36 |
+
)
|
37 |
+
from peft import PeftModel
|
38 |
+
import torch
|
39 |
+
from tqdm import tqdm
|
40 |
+
import json
|
41 |
+
|
42 |
+
# Hugging Faceで取得したTokenをこちらに貼る。
|
43 |
+
HF_TOKEN = "xxx"
|
44 |
+
# ベースとなるモデルと学習したLoRAのアダプタ。
|
45 |
+
#model_id = "models/models--llm-jp--llm-jp-3-13b/snapshots/cd3823f4c1fcbb0ad2e2af46036ab1b0ca13192a"
|
46 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
47 |
+
adapter_id = "fajoie/llmjp3_lora"
|
48 |
+
# QLoRA config
|
49 |
+
bnb_config = BitsAndBytesConfig(
|
50 |
+
load_in_4bit=True,
|
51 |
+
bnb_4bit_quant_type="nf4",
|
52 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
53 |
+
)
|
54 |
+
# Load model
|
55 |
+
model = AutoModelForCausalLM.from_pretrained(
|
56 |
+
model_id,
|
57 |
+
quantization_config=bnb_config,
|
58 |
+
device_map="auto",
|
59 |
+
token = HF_TOKEN
|
60 |
+
)
|
61 |
+
|
62 |
+
# Load tokenizer
|
63 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, token = HF_TOKEN)
|
64 |
+
|
65 |
+
# 元のモデルにLoRAのアダプタを統合。
|
66 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
67 |
+
|
68 |
+
# データセットの読み込み。
|
69 |
+
datasets = []
|
70 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
71 |
+
item = ""
|
72 |
+
for line in f:
|
73 |
+
line = line.strip()
|
74 |
+
item += line
|
75 |
+
if item.endswith("}"):
|
76 |
+
datasets.append(json.loads(item))
|
77 |
+
item = ""
|
78 |
+
|
79 |
+
# llmjp
|
80 |
+
results = []
|
81 |
+
for data in tqdm(datasets):
|
82 |
+
|
83 |
+
input = data["input"]
|
84 |
+
|
85 |
+
prompt = f"""### 指示
|
86 |
+
{input}
|
87 |
+
### 回答
|
88 |
+
"""
|
89 |
+
|
90 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
91 |
+
attention_mask = torch.ones_like(tokenized_input)
|
92 |
+
with torch.no_grad():
|
93 |
+
outputs = model.generate(
|
94 |
+
tokenized_input,
|
95 |
+
attention_mask=attention_mask,
|
96 |
+
max_new_tokens=100,
|
97 |
+
do_sample=False,
|
98 |
+
repetition_penalty=1.2,
|
99 |
+
pad_token_id=tokenizer.eos_token_id
|
100 |
+
)[0]
|
101 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
102 |
+
|
103 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
104 |
+
|
105 |
+
# ファイル保存
|
106 |
+
import re
|
107 |
+
jsonl_id = re.sub(".*/", "", adapter_id)
|
108 |
+
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
109 |
+
for result in results:
|
110 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
111 |
+
f.write('\n')
|
112 |
+
```
|
113 |
+
|
114 |
+
# 学習手法
|
115 |
```
|
116 |
!pip uninstall unsloth -y
|
117 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|