Instructions to use nagayaoh/LLM_2024_final_round_model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use nagayaoh/LLM_2024_final_round_model with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("nagayaoh/LLM_2024_final_round_model", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Unsloth Desktop
Update README.md
Browse files
README.md
CHANGED
|
@@ -40,13 +40,13 @@ You can use Google colab in T4 runtime:
|
|
| 40 |
* WandB to check parameter tuning
|
| 41 |
|
| 42 |
## Code
|
| 43 |
-
|
| 44 |
```
|
| 45 |
# -*- coding: utf-8 -*-
|
| 46 |
-
# !!!
|
| 47 |
|
| 48 |
# ===============================================================================================
|
| 49 |
-
#
|
| 50 |
!pip uninstall unsloth -y
|
| 51 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git" -qU
|
| 52 |
!pip install --upgrade torch torchvision torchaudio -qU
|
|
@@ -58,21 +58,22 @@ if torch.cuda.get_device_capability()[0] >= 8:
|
|
| 58 |
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3" -qU
|
| 59 |
|
| 60 |
# ===============================================================================================
|
| 61 |
-
#
|
| 62 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 63 |
-
|
|
|
|
| 64 |
proj_path = "/content"
|
| 65 |
input_path = proj_path + "/input/ichikara-instruction-003-001-1.json"
|
| 66 |
eval_path = proj_path + "/eval/elyza-tasks-100-TV_0.jsonl"
|
| 67 |
result_path = proj_path + "/results"
|
| 68 |
|
| 69 |
# ===============================================================================================
|
| 70 |
-
#
|
| 71 |
model_id = "llm-jp/llm-jp-3-13b"
|
| 72 |
-
new_model_id = "llm-jp-3-13b-it" #
|
| 73 |
-
dtype = None # None
|
| 74 |
-
load_in_4bit = True #
|
| 75 |
-
max_seq_length = 512 #
|
| 76 |
|
| 77 |
# パラメータをPack
|
| 78 |
config={
|
|
@@ -97,46 +98,46 @@ config={
|
|
| 97 |
}
|
| 98 |
|
| 99 |
# ===============================================================================================
|
| 100 |
-
# llm-jp/llm-jp-3-13b
|
| 101 |
from unsloth import FastLanguageModel
|
| 102 |
-
|
| 103 |
model_name = config.model_id,
|
| 104 |
dtype = config.model_dtype,
|
| 105 |
load_in_4bit = config.model_load_in_4bit,
|
| 106 |
trust_remote_code= True,
|
| 107 |
)
|
| 108 |
|
| 109 |
-
model = FastLanguageModel.get_peft_model(
|
| 110 |
-
|
| 111 |
-
r = config.lora_r,
|
| 112 |
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj",],
|
|
|
|
| 113 |
lora_alpha = config.lora_alpha,
|
| 114 |
lora_dropout = config.lora_dropout,
|
| 115 |
bias = config.lora_bias,
|
| 116 |
-
random_state = config.seed,
|
| 117 |
use_rslora = config.lora_use_rslora,
|
| 118 |
loftq_config = config.lora_loftq_config,
|
| 119 |
max_seq_length = config.max_seq_length,
|
| 120 |
use_gradient_checkpointing = "unsloth",
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
# ===============================================================================================
|
| 124 |
-
#
|
| 125 |
from datasets import load_dataset
|
| 126 |
train_dataset = load_dataset("json", data_files= input_path, split="train[:80%]" )
|
| 127 |
test_dataset = load_dataset("json", data_files= input_path, split="train[80%:]")
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
EOS_TOKEN = tokenizer.eos_token
|
| 131 |
-
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
| 132 |
|
| 133 |
def formatting_prompts_func(examples):
|
| 134 |
-
input = examples["text"]
|
| 135 |
-
output = examples["output"]
|
| 136 |
-
text = prompt.format(input, output) + EOS_TOKEN
|
| 137 |
-
return { "formatted_text" : text, } #
|
| 138 |
|
| 139 |
-
#
|
| 140 |
train_dataset = train_dataset.map( formatting_prompts_func, num_proc= 4 )
|
| 141 |
test_dataset = test_dataset.map( formatting_prompts_func, num_proc= 4 )
|
| 142 |
|
|
@@ -145,10 +146,10 @@ from trl import SFTTrainer
|
|
| 145 |
from transformers import TrainingArguments, EarlyStoppingCallback
|
| 146 |
from unsloth import is_bfloat16_supported
|
| 147 |
|
| 148 |
-
# EarlyStoppingCallback
|
| 149 |
early_stopping_callback = EarlyStoppingCallback(
|
| 150 |
-
early_stopping_patience = 3,
|
| 151 |
-
early_stopping_threshold = 0.0
|
| 152 |
)
|
| 153 |
|
| 154 |
trainer = SFTTrainer(
|
|
@@ -159,7 +160,7 @@ trainer = SFTTrainer(
|
|
| 159 |
max_seq_length = config.max_seq_length,
|
| 160 |
dataset_text_field = "formatted_text",
|
| 161 |
packing = False,
|
| 162 |
-
callbacks=[early_stopping_callback],
|
| 163 |
args = TrainingArguments(
|
| 164 |
per_device_train_batch_size = config.per_device_train_batch_size,
|
| 165 |
gradient_accumulation_steps = config.gradient_accumulation_steps,
|
|
@@ -168,14 +169,14 @@ trainer = SFTTrainer(
|
|
| 168 |
max_steps = config.max_steps,
|
| 169 |
learning_rate = config.learning_rate,
|
| 170 |
seed = config.seed,
|
| 171 |
-
evaluation_strategy = "steps",
|
| 172 |
-
eval_steps = 20,
|
| 173 |
-
save_strategy = "steps",
|
| 174 |
-
save_steps = 60,
|
| 175 |
save_total_limit = 3,
|
| 176 |
-
load_best_model_at_end = True,
|
| 177 |
-
metric_for_best_model = "eval_loss",
|
| 178 |
-
greater_is_better = False,
|
| 179 |
output_dir = "outputs",
|
| 180 |
report_to = "wandb",
|
| 181 |
fp16 = not is_bfloat16_supported(),
|
|
@@ -186,11 +187,11 @@ trainer = SFTTrainer(
|
|
| 186 |
)
|
| 187 |
|
| 188 |
# ===============================================================================================
|
| 189 |
-
#
|
| 190 |
trainer_stats = trainer.train()
|
| 191 |
|
| 192 |
# ===============================================================================================
|
| 193 |
-
# Elyza-100
|
| 194 |
import json
|
| 195 |
eval_datasets = []
|
| 196 |
elyza_tasks_path = eval_path
|
|
@@ -203,7 +204,7 @@ with open(elyza_tasks_path, "r") as f:
|
|
| 203 |
eval_datasets.append(json.loads(item))
|
| 204 |
item = ""
|
| 205 |
|
| 206 |
-
#
|
| 207 |
from tqdm import tqdm
|
| 208 |
FastLanguageModel.for_inference(model)
|
| 209 |
model.eval()
|
|
@@ -217,14 +218,14 @@ for dt in tqdm(eval_datasets):
|
|
| 217 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
| 218 |
|
| 219 |
# ===============================================================================================
|
| 220 |
-
# jsonl
|
| 221 |
jsonl_result_path = result_path + f"/{new_model_id}_output.jsonl",
|
| 222 |
with open( jsonl_result_path, 'w', encoding='utf-8') as f:
|
| 223 |
for result in results:
|
| 224 |
json.dump(result, f, ensure_ascii=False)
|
| 225 |
f.write('\n')
|
| 226 |
|
| 227 |
-
# LoRA
|
| 228 |
HF_TOKEN = "hugging face へのアクセストークン"
|
| 229 |
model.push_to_hub_merged(
|
| 230 |
new_model_id+"_lora",
|
|
|
|
| 40 |
* WandB to check parameter tuning
|
| 41 |
|
| 42 |
## Code
|
| 43 |
+
## This is ipynb code
|
| 44 |
```
|
| 45 |
# -*- coding: utf-8 -*-
|
| 46 |
+
# !!! paste below code as ipynb in Google Colab
|
| 47 |
|
| 48 |
# ===============================================================================================
|
| 49 |
+
# --- Install Python Packages
|
| 50 |
!pip uninstall unsloth -y
|
| 51 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git" -qU
|
| 52 |
!pip install --upgrade torch torchvision torchaudio -qU
|
|
|
|
| 58 |
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3" -qU
|
| 59 |
|
| 60 |
# ===============================================================================================
|
| 61 |
+
# --- Parameter definition
|
| 62 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 63 |
+
|
| 64 |
+
# --- Put data files according to path definition
|
| 65 |
proj_path = "/content"
|
| 66 |
input_path = proj_path + "/input/ichikara-instruction-003-001-1.json"
|
| 67 |
eval_path = proj_path + "/eval/elyza-tasks-100-TV_0.jsonl"
|
| 68 |
result_path = proj_path + "/results"
|
| 69 |
|
| 70 |
# ===============================================================================================
|
| 71 |
+
# Setting Parameters
|
| 72 |
model_id = "llm-jp/llm-jp-3-13b"
|
| 73 |
+
new_model_id = "llm-jp-3-13b-it" # Adaptor name
|
| 74 |
+
dtype = None # None is OK +
|
| 75 |
+
load_in_4bit = True # True for 13B model
|
| 76 |
+
max_seq_length = 512 # Any length can be used because of RoPE
|
| 77 |
|
| 78 |
# パラメータをPack
|
| 79 |
config={
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
# ===============================================================================================
|
| 101 |
+
# Load llm-jp/llm-jp-3-13b as 4bit-quantized qLoRA
|
| 102 |
from unsloth import FastLanguageModel
|
| 103 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 104 |
model_name = config.model_id,
|
| 105 |
dtype = config.model_dtype,
|
| 106 |
load_in_4bit = config.model_load_in_4bit,
|
| 107 |
trust_remote_code= True,
|
| 108 |
)
|
| 109 |
|
| 110 |
+
model = FastLanguageModel.get_peft_model(
|
| 111 |
+
model,
|
|
|
|
| 112 |
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj",],
|
| 113 |
+
r = config.lora_r,
|
| 114 |
lora_alpha = config.lora_alpha,
|
| 115 |
lora_dropout = config.lora_dropout,
|
| 116 |
bias = config.lora_bias,
|
|
|
|
| 117 |
use_rslora = config.lora_use_rslora,
|
| 118 |
loftq_config = config.lora_loftq_config,
|
| 119 |
max_seq_length = config.max_seq_length,
|
| 120 |
use_gradient_checkpointing = "unsloth",
|
| 121 |
+
random_state = config.seed,
|
| 122 |
)
|
| 123 |
|
| 124 |
# ===============================================================================================
|
| 125 |
+
# Load dataset and spilit into train and test
|
| 126 |
from datasets import load_dataset
|
| 127 |
train_dataset = load_dataset("json", data_files= input_path, split="train[:80%]" )
|
| 128 |
test_dataset = load_dataset("json", data_files= input_path, split="train[80%:]")
|
| 129 |
|
| 130 |
+
# Formatting in prompt style
|
| 131 |
+
EOS_TOKEN = tokenizer.eos_token
|
| 132 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
| 133 |
|
| 134 |
def formatting_prompts_func(examples):
|
| 135 |
+
input = examples["text"]
|
| 136 |
+
output = examples["output"]
|
| 137 |
+
text = prompt.format(input, output) + EOS_TOKEN
|
| 138 |
+
return { "formatted_text" : text, } # Retrun new field "formatted_text"
|
| 139 |
|
| 140 |
+
# Assign Prompt style
|
| 141 |
train_dataset = train_dataset.map( formatting_prompts_func, num_proc= 4 )
|
| 142 |
test_dataset = test_dataset.map( formatting_prompts_func, num_proc= 4 )
|
| 143 |
|
|
|
|
| 146 |
from transformers import TrainingArguments, EarlyStoppingCallback
|
| 147 |
from unsloth import is_bfloat16_supported
|
| 148 |
|
| 149 |
+
# EarlyStoppingCallback
|
| 150 |
early_stopping_callback = EarlyStoppingCallback(
|
| 151 |
+
early_stopping_patience = 3,
|
| 152 |
+
early_stopping_threshold = 0.0
|
| 153 |
)
|
| 154 |
|
| 155 |
trainer = SFTTrainer(
|
|
|
|
| 160 |
max_seq_length = config.max_seq_length,
|
| 161 |
dataset_text_field = "formatted_text",
|
| 162 |
packing = False,
|
| 163 |
+
callbacks=[early_stopping_callback],
|
| 164 |
args = TrainingArguments(
|
| 165 |
per_device_train_batch_size = config.per_device_train_batch_size,
|
| 166 |
gradient_accumulation_steps = config.gradient_accumulation_steps,
|
|
|
|
| 169 |
max_steps = config.max_steps,
|
| 170 |
learning_rate = config.learning_rate,
|
| 171 |
seed = config.seed,
|
| 172 |
+
evaluation_strategy = "steps",
|
| 173 |
+
eval_steps = 20,
|
| 174 |
+
save_strategy = "steps",
|
| 175 |
+
save_steps = 60,
|
| 176 |
save_total_limit = 3,
|
| 177 |
+
load_best_model_at_end = True,
|
| 178 |
+
metric_for_best_model = "eval_loss",
|
| 179 |
+
greater_is_better = False,
|
| 180 |
output_dir = "outputs",
|
| 181 |
report_to = "wandb",
|
| 182 |
fp16 = not is_bfloat16_supported(),
|
|
|
|
| 187 |
)
|
| 188 |
|
| 189 |
# ===============================================================================================
|
| 190 |
+
# Trainning
|
| 191 |
trainer_stats = trainer.train()
|
| 192 |
|
| 193 |
# ===============================================================================================
|
| 194 |
+
# Load Elyza-100 tasks
|
| 195 |
import json
|
| 196 |
eval_datasets = []
|
| 197 |
elyza_tasks_path = eval_path
|
|
|
|
| 204 |
eval_datasets.append(json.loads(item))
|
| 205 |
item = ""
|
| 206 |
|
| 207 |
+
# Do tasks
|
| 208 |
from tqdm import tqdm
|
| 209 |
FastLanguageModel.for_inference(model)
|
| 210 |
model.eval()
|
|
|
|
| 218 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
| 219 |
|
| 220 |
# ===============================================================================================
|
| 221 |
+
# Save result as jsonl
|
| 222 |
jsonl_result_path = result_path + f"/{new_model_id}_output.jsonl",
|
| 223 |
with open( jsonl_result_path, 'w', encoding='utf-8') as f:
|
| 224 |
for result in results:
|
| 225 |
json.dump(result, f, ensure_ascii=False)
|
| 226 |
f.write('\n')
|
| 227 |
|
| 228 |
+
# Send LoRA adaptors into HugginFace modelcard page
|
| 229 |
HF_TOKEN = "hugging face へのアクセストークン"
|
| 230 |
model.push_to_hub_merged(
|
| 231 |
new_model_id+"_lora",
|