add readme
Browse files
README.md
CHANGED
@@ -20,3 +20,140 @@ language:
|
|
20 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
21 |
|
22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
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 |
+
# How to Use
|
25 |
+
|
26 |
+
```python
|
27 |
+
!pip uninstall unsloth -y
|
28 |
+
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
29 |
+
!pip install --upgrade torch
|
30 |
+
!pip install --upgrade xformers
|
31 |
+
!pip install ipywidgets --upgrade
|
32 |
+
|
33 |
+
import torch
|
34 |
+
if torch.cuda.get_device_capability()[0] >= 8:
|
35 |
+
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
|
36 |
+
|
37 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
38 |
+
from unsloth import FastLanguageModel
|
39 |
+
import torch
|
40 |
+
max_seq_length = 512
|
41 |
+
dtype = None
|
42 |
+
load_in_4bit = True
|
43 |
+
|
44 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
45 |
+
new_model_id = "llm-jp-3-13b-finetune-2"
|
46 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
47 |
+
model_name=model_id,
|
48 |
+
dtype=dtype,
|
49 |
+
load_in_4bit=load_in_4bit,
|
50 |
+
trust_remote_code=True,
|
51 |
+
)
|
52 |
+
|
53 |
+
model = FastLanguageModel.get_peft_model(
|
54 |
+
model,
|
55 |
+
r = 32,
|
56 |
+
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
|
57 |
+
"gate_proj", "up_proj", "down_proj",],
|
58 |
+
lora_alpha = 32,
|
59 |
+
lora_dropout = 0.05,
|
60 |
+
bias = "none",
|
61 |
+
use_gradient_checkpointing = "unsloth",
|
62 |
+
random_state = 3407,
|
63 |
+
use_rslora = False,
|
64 |
+
loftq_config = None,
|
65 |
+
max_seq_length = max_seq_length,
|
66 |
+
)
|
67 |
+
|
68 |
+
HF_TOKEN = "" #@param {type:"string"}
|
69 |
+
|
70 |
+
from datasets import load_dataset
|
71 |
+
dataset = load_dataset("json", data_files="/content/ichikara-instruction-003-001-2.1.json")
|
72 |
+
|
73 |
+
prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
74 |
+
{}
|
75 |
+
### εη
|
76 |
+
{}"""
|
77 |
+
|
78 |
+
|
79 |
+
"""
|
80 |
+
formatting_prompts_func: εγγΌγΏγγγγ³γγγ«εγγγε½’εΌγ«εγγγ
|
81 |
+
"""
|
82 |
+
EOS_TOKEN = tokenizer.eos_token
|
83 |
+
def formatting_prompts_func(examples):
|
84 |
+
input = examples["text"]
|
85 |
+
output = examples["output"]
|
86 |
+
text = prompt.format(input, output) + EOS_TOKEN
|
87 |
+
return { "formatted_text" : text, }
|
88 |
+
pass
|
89 |
+
|
90 |
+
dataset = dataset.map(
|
91 |
+
formatting_prompts_func,
|
92 |
+
num_proc= 4,
|
93 |
+
)
|
94 |
+
|
95 |
+
from trl import SFTTrainer
|
96 |
+
from transformers import TrainingArguments
|
97 |
+
from unsloth import is_bfloat16_supported
|
98 |
+
|
99 |
+
trainer = SFTTrainer(
|
100 |
+
model = model,
|
101 |
+
tokenizer = tokenizer,
|
102 |
+
train_dataset=dataset["train"],
|
103 |
+
max_seq_length = max_seq_length,
|
104 |
+
dataset_text_field="formatted_text",
|
105 |
+
packing = False,
|
106 |
+
args = TrainingArguments(
|
107 |
+
per_device_train_batch_size = 2,
|
108 |
+
gradient_accumulation_steps = 4,
|
109 |
+
num_train_epochs = 1,
|
110 |
+
logging_steps = 10,
|
111 |
+
warmup_steps = 10,
|
112 |
+
save_steps=100,
|
113 |
+
save_total_limit=2,
|
114 |
+
max_steps=-1,
|
115 |
+
learning_rate = 2e-4,
|
116 |
+
fp16 = not is_bfloat16_supported(),
|
117 |
+
bf16 = is_bfloat16_supported(),
|
118 |
+
group_by_length=True,
|
119 |
+
seed = 3407,
|
120 |
+
output_dir = "outputs",
|
121 |
+
report_to = "none",
|
122 |
+
),
|
123 |
+
)
|
124 |
+
|
125 |
+
trainer_stats = trainer.train()
|
126 |
+
|
127 |
+
import json
|
128 |
+
datasets = []
|
129 |
+
with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
|
130 |
+
item = ""
|
131 |
+
for line in f:
|
132 |
+
line = line.strip()
|
133 |
+
item += line
|
134 |
+
if item.endswith("}"):
|
135 |
+
datasets.append(json.loads(item))
|
136 |
+
item = ""
|
137 |
+
|
138 |
+
from tqdm import tqdm
|
139 |
+
|
140 |
+
FastLanguageModel.for_inference(model)
|
141 |
+
|
142 |
+
results = []
|
143 |
+
for dt in tqdm(datasets):
|
144 |
+
input = dt["input"]
|
145 |
+
|
146 |
+
prompt = f"""### ζη€Ί\n{input}\n### εη\n"""
|
147 |
+
|
148 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
149 |
+
|
150 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
151 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### εη')[-1]
|
152 |
+
|
153 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
154 |
+
|
155 |
+
with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
156 |
+
for result in results:
|
157 |
+
json.dump(result, f, ensure_ascii=False)
|
158 |
+
f.write('\n')
|
159 |
+
```
|