Sample Use
# python 3.10.12
!pip install -U pip
!pip install -U transformers
!pip install -U bitsandbytes
!pip install -U accelerate
!pip install -U datasets
!pip install -U peft
!pip install -U trl
!pip install -U wandb
!pip install ipywidgets --upgrade
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
TrainingArguments,
logging,
)
from peft import (
LoraConfig,
PeftModel,
get_peft_model,
)
import os, torch, gc
from datasets import load_dataset
import bitsandbytes as bnb
from trl import SFTTrainer
# Hugging Face Token
HF_TOKEN = "your-token"
# ใขใใซใ่ชญใฟ่พผใฟใ
# llm-jp-3 1.8B, 3.7B, 13Bใฎsnapshotใใใฆใณใญใผใๆธใฟใงmodelsใใฃใฌใฏใใชใซๆ ผ็ดใใฆใใใพใใ
# base_model_idใฎๅคใฏomnicampusใฎ็ฐๅขใซใใใใขใใซใฎใในใ่กจใใฆใใใใใไปฅๅคใฎ็ฐๅขใงๅฎ่กใใๅ ดๅใฏๅคๆดใฎๅฟ
่ฆใใใใพใใ
# ใใฎไปใฎใขใใซใฏๅๅพใซๆฟ่ซพใๅฟ
่ฆใชใใใๅ่ชใงใใฆใณใญใผใใ้กใใใพใใ
base_model_id = "models/models--llm-jp--llm-jp-3-13b/snapshots/cd3823f4c1fcbb0ad2e2af46036ab1b0ca13192a" #Fine-Tuningใใใใผในใขใใซ
# omnicampusไปฅๅคใฎ็ฐๅขใใๅฉ็จใฎๆนใฏไปฅไธใใๅฉ็จใใ ใใใ
# base_model_id = "llm-jp/llm-jp-3-13b"
new_model_id = "llm-jp-3-13b-finetune" #Fine-Tuningใใใขใใซใซใคใใใๅๅ
"""
bnb_config: ้ๅญๅใฎ่จญๅฎ
- load_in_4bit:
- 4bit้ๅญๅๅฝขๅผใงใขใใซใใญใผใ
- bnb_4bit_quant_type:
- ้ๅญๅใฎๅฝขๅผใๆๅฎ
- bnb_4bit_compute_dtype:
- ้ๅญๅใใใ้ใฟใ็จใใฆ่จ็ฎใใ้ใฎใใผใฟๅ
"""
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4", # nf4ใฏ้ๅธธใฎINT4ใใ็ฒพๅบฆใ้ซใใใใฅใผใฉใซใใใใฏใผใฏใฎๅๅธใซๆ้ฉใงใ
bnb_4bit_compute_dtype=torch.bfloat16,
)
"""
model: ใขใใซ
- base_model:
- ่ชญใฟ่พผใใใผในใขใใซ (ไบๅใซๅฎ็พฉใใใใฎ)
- quantization_config:
- bnb_configใง่จญๅฎใใ้ๅญๅ่จญๅฎ
- device_map:
- ใขใใซใๅฒใๅฝใฆใใใใคใน (CPU/GPU) "auto"ใง่ชๅใซๅฒใๅฝใฆใใใพใใ
tokenizer: ใใผใฏใใคใถใผ
- base_model:
- ่ชญใฟ่พผใใใผในใขใใซ (ไบๅใซๅฎ็พฉใใใใฎ)
- trust_remote_code:
- ใชใขใผใใณใผใใฎๅฎ่กใ่จฑๅฏ (ใซในใฟใ ใขใใซใชใฉ)
"""
model = AutoModelForCausalLM.from_pretrained(
base_model_id,
quantization_config=bnb_config,
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
"""
find_all_linear_names: ใขใใซๅ
ใฎ4bit้ๅญๅ็ทๅฝขๅฑคใๆขใใพใใ
"""
def find_all_linear_names(model):
cls = bnb.nn.Linear4bit # 4bit้ๅญๅ็ทๅฝขๅฑคใฏใฉในใๆๅฎ
lora_module_names = set() # ใใใซๅๅพใใ็ทๅฝขๅฑคใไฟๆใใพใใ
# ใขใใซๅ
ใฎๅ
จใฆใฎใขใธใฅใผใซใๆข็ดขใใพใ
for name, module in model.named_modules():
if isinstance(module, cls): # ใขใธใฅใผใซใ4bit้ๅญๅ็ทๅฝขๅฑคใฎๅ ดๅ
names = name.split('.') # ใขใธใฅใผใซใฎๅๅใๅๅฒ (ใในใใใใฆใ้ใชใฉใซๅฏพๅฆ)
lora_module_names.add(names[0] if len(names) == 1 else names[-1]) # ๆไธๅฑคใฎๅๅใlora_module_namesใซ่ฟฝๅ
# 'lm_head' ใฏ16ใใใๆผ็ฎใฎ้ใซ้คๅคใใๅฟ
่ฆใใใใใใlora_module_namesใใๅ้ค
if 'lm_head' in lora_module_names:
lora_module_names.remove('lm_head')
return list(lora_module_names) # lora_module_namesใใชในใใซๅคๆใใฆ่ฟใใพใใ
modules = find_all_linear_names(model)
"""
peft_config: PEFTใฎๆงๆ่จญๅฎ
- r
- LoRA ใฎใฉใณใฏ (4, 8, 16 ,32...)
- ๅขใใใปใฉๅญฆ็ฟใๆใใ, ้ๅญฆ็ฟใฎใชในใฏใ้ซใพใใฎใงๆณจๆ
- lora_alpha
- LoRAใฎในใฑใผใชใณใฐไฟๆฐ
- lora_dropout
- ใใญใใใขใฆใ็๏ผ้ๅญฆ็ฟใ้ฒใใใใฎๅฒๅ๏ผ
- bias
- ใใคใขใน้
ใฎๆฑใ ("none"ใฎๅ ดๅใLoRAใฏใใคใขในใๅญฆ็ฟใใชใ)
- task_type
- ใฟในใฏใฟใคใ
- target_modules
- LoRAใ้ฉ็จใใใฟใผใฒใใใขใธใฅใผใซ (ๅใฎใณใผใใง็นๅฎใใๅฑค)
"""
peft_config = LoraConfig(
r=16,
lora_alpha=32,
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
target_modules=modules,
)
model = get_peft_model(model, peft_config)
"""
ๅญฆ็ฟใซ็จใใใใผใฟใปใใใฎๆๅฎ
ไปๅใฏLLM-jp ใฎๅ
ฌ้ใใฆใใ Ichikara Instruction ใไฝฟใใพใใใใผใฟใซใขใฏใปในใใใใใซใฏ็ณ่ซใๅฟ
่ฆใงใใฎใงใไฝฟใใใๆนใฎใฟ็ณ่ซใใใฆใใ ใใใ
Ichikara Instruciton ใ Hugging Face Hub ใซใฆๅ
ฌ้ใใใใจใฏใๆงใใใ ใใใ
ใพใใCC-BY-NC-SAใงใใฎใงใขใใซใฏใฉใคใปใณในใ็ถๆฟใใๅๆใงใไฝฟใใใ ใใใ
ไธ่จใฎใชใณใฏใใ็ณ่ซใ็ตใใๅ
ใซ Google Drive ใใใใDistribution20241221_all ใจใใใใฉใซใใใจใใฆใณใญใผใใใฆใใ ใใใ
ไปๅใฏใichikara-instruction-003-001-1.jsonใใไฝฟใใพใใๅฟ
่ฆใงใใใฐๅฑ้๏ผ!unzip ใชใฉ๏ผใใใใผใฟใปใใใฎใในใ้ฉๅใซๆๅฎใใฆใใ ใใใ
omnicampusใฎ้็บ็ฐๅขใงใฏๅๅพใใใใผใฟใๅทฆๅดใซใใฉใใฐใขใณใใใญใใใใฆใไฝฟใใใ ใใใ
https://liat-aip.sakura.ne.jp/wp/llmใฎใใใฎๆฅๆฌ่ชใคใณในใใฉใฏใทใงใณใใผใฟไฝๆ/llmใฎใใใฎๆฅๆฌ่ชใคใณในใใฉใฏใทใงใณใใผใฟ-ๅ
ฌ้/
้ขๆ น่ก, ๅฎ่คใพใ, ๅพ่ค็พ็ฅๅญ, ้ดๆจไน
็พ, ๆฒณๅๅคง่ผ, ไบไนไธ็ดไน, ไนพๅฅๅคช้. ichikara-instruction: LLMใฎใใใฎๆฅๆฌ่ชใคใณในใใฉใฏใทใงใณใใผใฟใฎๆง็ฏ. ่จ่ชๅฆ็ๅญฆไผ็ฌฌ30ๅๅนดๆฌกๅคงไผ(2024)
"""
dataset = load_dataset("json", data_files="./ichikara-instruction-003-001-1.json")
dataset
# ๅญฆ็ฟๆใฎใใญใณใใใใฉใผใใใใฎๅฎ็พฉ
prompt = """### ๆ็คบ
{}
### ๅ็ญ
{}"""
"""
formatting_prompts_func: ๅใใผใฟใใใญใณใใใซๅใใใๅฝขๅผใซๅใใใ
"""
EOS_TOKEN = tokenizer.eos_token # ใใผใฏใใคใถใผใฎEOSใใผใฏใณ๏ผๆๆซใใผใฏใณ๏ผ
def formatting_prompts_func(examples):
input = examples["text"] # ๅ
ฅๅใใผใฟ
output = examples["output"] # ๅบๅใใผใฟ
text = prompt.format(input, output) + EOS_TOKEN # ใใญใณใใใฎไฝๆ
return { "formatted_text" : text, } # ๆฐใใใใฃใผใซใ "formatted_text" ใ่ฟใ
pass
# # ๅใใผใฟใซใใฉใผใใใใ้ฉ็จ
dataset = dataset.map(
formatting_prompts_func,
num_proc= 4, # ไธฆๅๅฆ็ๆฐใๆๅฎ
)
dataset
# ใใผใฟใ็ขบ่ช
print(dataset["train"]["formatted_text"][3])
"""
training_arguments: ๅญฆ็ฟใฎ่จญๅฎ
- output_dir:
-ใใฌใผใใณใฐๅพใฎใขใใซใไฟๅญใใใใฃใฌใฏใใช
- per_device_train_batch_size:
- ใใใคในใใจใฎใใฌใผใใณใฐใใใใตใคใบ
- per_device_
_batch_size:
- ใใใคในใใจใฎ่ฉไพกใใใใตใคใบ
- gradient_accumulation_steps:
- ๅพ้
ใๆดๆฐใใๅใซในใใใใ็ฉใฟ้ใญใๅๆฐ
- optim:
- ใชใใใฃใใคใถใฎ่จญๅฎ
- num_train_epochs:
- ใจใใใฏๆฐ
- eval_strategy:
- ่ฉไพกใฎๆฆ็ฅ ("no"/"steps"/"epoch")
- eval_steps:
- eval_strategyใ"steps"ใฎใจใใ่ฉไพกใ่กใstep้้
- logging_strategy:
- ใญใฐ่จ้ฒใฎๆฆ็ฅ
- logging_steps:
- ใญใฐใๅบๅใใในใใใ้้
- warmup_steps:
- ๅญฆ็ฟ็ใฎใฆใฉใผใ ใขใใในใใใๆฐ
- save_steps:
- ใขใใซใไฟๅญใใในใใใ้้
- save_total_limit:
- ไฟๅญใใฆใใcheckpointใฎๆฐ
- max_steps:
- ใใฌใผใใณใฐใฎๆๅคงในใใใๆฐ
- learning_rate:
- ๅญฆ็ฟ็
- fp16:
- 16bitๆตฎๅๅฐๆฐ็นใฎไฝฟ็จ่จญๅฎ๏ผ็ฌฌ8ๅๆผ็ฟใๅ่ใซใใใจ่ฏใใงใ๏ผ
- bf16:
- BFloat16ใฎไฝฟ็จ่จญๅฎ
- group_by_length:
- ๅ
ฅๅใทใผใฑใณในใฎ้ทใใซใใใใใใใฐใซใผใๅ (ใใฌใผใใณใฐใฎๅน็ๅ)
- report_to:
- ใญใฐใฎ้ไฟกๅ
("wandb"/"tensorboard"ใชใฉ)
"""
training_arguments = TrainingArguments(
output_dir=new_model_id,
per_device_train_batch_size=1,
gradient_accumulation_steps=2,
optim="paged_adamw_32bit",
num_train_epochs=1,
logging_strategy="steps",
logging_steps=10,
warmup_steps=10,
save_steps=100,
save_total_limit = 2,
max_steps = -1,
learning_rate=5e-5,
fp16=False,
bf16=False,
seed = 3407,
group_by_length=True,
report_to="none"
)
"""
SFTTrainer: Supervised Fine-Tuningใซ้ขใใ่จญๅฎ
- model:
- ่ชญใฟ่พผใใ ใใผในใฎใขใใซ
- train_dataset:
- ใใฌใผใใณใฐใซไฝฟ็จใใใใผใฟใปใใ
- eval_dataset:
- ่ฉไพกใซไฝฟ็จใใใใผใฟใปใใ
- peft_config:
- PEFT๏ผParameter-Efficient Fine-Tuning๏ผใฎ่จญๅฎ๏ผLoRAใๅฉ็จใใๅ ดๅใซๆๅฎ๏ผ
- max_seq_length:
- ใขใใซใซๅ
ฅๅใใใใทใผใฑใณในใฎๆๅคงใใผใฏใณ้ท
- dataset_text_field:
- ใใผใฟใปใใๅ
ใฎๅญฆ็ฟใซไฝฟใใใญในใใๅซใใใฃใผใซใๅ
- tokenizer:
- ใขใใซใซๅฏพๅฟใใใใผใฏใใคใถใผ
- args:
- ใใฌใผใใณใฐใซไฝฟ็จใใใใคใใผใใฉใกใผใฟ๏ผTrainingArgumentsใฎ่จญๅฎใๆๅฎ๏ผ
- packing:
- ๅ
ฅๅใทใผใฑใณในใฎใใใญใณใฐใ่กใใใฉใใใฎ่จญๅฎ (False ใซ่จญๅฎใใใใจใงใๅๅ
ฅๅใ็ฌ็ซใใฆๆฑใ)
"""
trainer = SFTTrainer(
model=model,
train_dataset=dataset["train"],
peft_config=peft_config,
max_seq_length= 512,
dataset_text_field="formatted_text",
tokenizer=tokenizer,
args=training_arguments,
packing= False,
)
model.config.use_cache = False # ใญใฃใใทใฅๆฉ่ฝใ็กๅนๅ
trainer.train() # ใใฌใผใใณใฐใๅฎ่ก
# ใฟในใฏใจใชใใใผใฟใฎ่ชญใฟ่พผใฟใ
# omnicampusใฎ้็บ็ฐๅขใงใฏใๅทฆใซใฟในใฏใฎjsonlใใใฉใใฐใขใณใใใญใใใใฆใใๅฎ่กใ
import json
datasets = []
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
item = ""
for line in f:
line = line.strip()
item += line
if item.endswith("}"):
datasets.append(json.loads(item))
item = ""
# ใขใใซใซใใใฟในใฏใฎๆจ่ซใ
from tqdm import tqdm
results = []
for data in tqdm(datasets):
input = data["input"]
prompt = f"""### ๆ็คบ
{input}
### ๅ็ญ
"""
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
attention_mask = torch.ones_like(tokenized_input)
with torch.no_grad():
outputs = model.generate(
tokenized_input,
attention_mask=attention_mask,
max_new_tokens=100,
do_sample=False,
repetition_penalty=1.2,
pad_token_id=tokenizer.eos_token_id
)[0]
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
results.append({"task_id": data["task_id"], "input": input, "output": output})
# ใใกใใงjsolใ็ๆใใใ
# ๆฌใณใผใใงใฏinputใจeval_aspectใๅซใใงใใพใใใใชใใฆใๅ้กใใใพใใใ
# ๅฟ
้ ใชใฎใฏtask_idใจoutputใจใชใใพใใ
import re
jsonl_id = re.sub(".*/", "", new_model_id)
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
for result in results:
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
f.write('\n')
Model Card for Model ID
Model Details
Model Description
This is the model card of a ๐ค transformers model that has been pushed on the Hub. This model card has been automatically generated.
- Developed by: [More Information Needed]
- Funded by [optional]: [More Information Needed]
- Shared by [optional]: [More Information Needed]
- Model type: [More Information Needed]
- Language(s) (NLP): [More Information Needed]
- License: [More Information Needed]
- Finetuned from model [optional]: [More Information Needed]
Model Sources [optional]
- Repository: [More Information Needed]
- Paper [optional]: [More Information Needed]
- Demo [optional]: [More Information Needed]
Uses
Direct Use
[More Information Needed]
Downstream Use [optional]
[More Information Needed]
Out-of-Scope Use
[More Information Needed]
Bias, Risks, and Limitations
[More Information Needed]
Recommendations
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
How to Get Started with the Model
Use the code below to get started with the model.
[More Information Needed]
Training Details
Training Data
[More Information Needed]
Training Procedure
Preprocessing [optional]
[More Information Needed]
Training Hyperparameters
- Training regime: [More Information Needed]
Speeds, Sizes, Times [optional]
[More Information Needed]
Evaluation
Testing Data, Factors & Metrics
Testing Data
[More Information Needed]
Factors
[More Information Needed]
Metrics
[More Information Needed]
Results
[More Information Needed]
Summary
Model Examination [optional]
[More Information Needed]
Environmental Impact
Carbon emissions can be estimated using the Machine Learning Impact calculator presented in Lacoste et al. (2019).
- Hardware Type: [More Information Needed]
- Hours used: [More Information Needed]
- Cloud Provider: [More Information Needed]
- Compute Region: [More Information Needed]
- Carbon Emitted: [More Information Needed]
Technical Specifications [optional]
Model Architecture and Objective
[More Information Needed]
Compute Infrastructure
[More Information Needed]
Hardware
[More Information Needed]
Software
[More Information Needed]
Citation [optional]
BibTeX:
[More Information Needed]
APA:
[More Information Needed]
Glossary [optional]
[More Information Needed]
More Information [optional]
[More Information Needed]
Model Card Authors [optional]
[More Information Needed]
Model Card Contact
[More Information Needed]
Model tree for s02414js/llm-jp-3-13b-finetune
Base model
llm-jp/llm-jp-3-13b