Downloads · 30 days
0
hotake/llm-jp-3-13b-finetune
llm-jp-3-13b-finetune is a machine learning model from hotake. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for transformers.
LLM-JP-3-13B-FineTune は、オープンソースの日本語モデル LLM-JP-3-13B を基にファインチューニングした言語モデルです。このモデルは、LoRA (Low-Rank Adaptation) を用いた効率的なファインチューニング手法を活用して構築されています。
Downloads · 30 days
0
Access
Public
Updated Dec 8, 2024
Repo size
250 MB
Likes
0
Public
Click a slice to open those files.
.safetensors250 MB · 97%
From the Hugging Face model README
LLM-JP-3-13B-FineTune は、オープンソースの日本語モデル LLM-JP-3-13B を基にファインチューニングした言語モデルです。このモデルは、LoRA (Low-Rank Adaptation) を用いた効率的なファインチューニング手法を活用して構築されています。
ファインチューニングには、Ichikara Instruction Dataset を用いています。データセットはCC-BY-NC-SAライセンスに準拠しており、商用利用は制限されています。
以下のコードでベースモデルをロードし、量子化設定を適用します。
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model
from bitsandbytes import BitsAndBytesConfig
import torch
# ベースモデルのID
base_model_id = "llm-jp/llm-jp-3-13b"
# 量子化設定
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
# モデルとトークナイザーの読み込み
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)
LoRAによる効率的なファインチューニングを設定します。
# ターゲットモジュールを特定
modules = ["q_proj", "v_proj"] # モデルに応じて変更
# 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)
学習には Ichikara Instruction Dataset を利用しました。
from datasets import load_dataset
# データセットをロード
dataset = load_dataset("json", data_files="./ichikara-instruction-003-001-1.json")
# プロンプトフォーマット
prompt = """### 指示
{}
### 回答
{}"""
# プロンプトをデータに適用
def formatting_prompts_func(examples):
input = examples["text"]
output = examples["output"]
text = prompt.format(input, output) + tokenizer.eos_token
return {"formatted_text": text}
dataset = dataset.map(formatting_prompts_func, num_proc=4)
transformers ライブラリの TrainingArguments を使用して学習を実行します。
from transformers import TrainingArguments
from trl import SFTTrainer
training_arguments = TrainingArguments(
output_dir="llm-jp-3-13b-finetune",
per_device_train_batch_size=1,
gradient_accumulation_steps=2,
optim="paged_adamw_32bit",
num_train_epochs=1,
logging_strategy="steps",
logging_steps=10,
save_steps=100,
save_total_limit=2,
learning_rate=5e-5,
fp16=False,
bf16=False,
group_by_length=True,
)
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,
)
trainer.train()
学習済みモデルを用いて推論を実行します。
from transformers import pipeline
def generate_response(input_text):
prompt = f"""### 指示
{input_text}
### 回答
"""
inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
inputs,
max_new_tokens=100,
do_sample=False,
pad_token_id=tokenizer.eos_token_id
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
response = generate_response("このモデルについて教えてください。")
print(response)
This model is trained on specific datasets and may reflect biases present in the data. It is intended for research purposes and may not generalize well to all tasks or domains.
このモデルは Hayato Ootake によって開発されました。