Downloads · 30 days
0
Ryota2865/gemma-2-9b-finetune5
gemma-2-9b-finetune5 is a machine learning model from Ryota2865. 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. The card lists the license as gemma.
Downloads · 30 days
0
Access
Public
Updated Jan 15, 2025
Repo size
255 MB
Likes
0
Public
Click a slice to open those files.
.safetensors216 MB · 85%
From the Hugging Face model README
gemma-2-9bを日本語データセットでファインチューニングしたモデルです。
Hirokazu Kiyomaru and Takashi Kodama.
以下は、質問応答タスクとして使用した、elyza-tasks-100-TV_0.jsonl出力用のコードです。
!pip install -U bitsandbytes
!pip install -U transformers
!pip install -U accelerate
!pip install -U datasets
!pip install -U peft
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
from peft import PeftModel
import torch
from tqdm import tqdm
import json
HF_TOKEN = "YourHaggingFaceToken"
base_model_id = "google/gemma-2-9b"
adapter_id = "Ryota2865/gemma-2-9b-finetune5"
# QLoRA config
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
# Load model
model = AutoModelForCausalLM.from_pretrained(
base_model_id,
quantization_config=bnb_config,
device_map="auto",
token = HF_TOKEN
)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True, token = HF_TOKEN)
# 元のモデルにLoRAのアダプタを統合。
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
# データセットの読み込み。
datasets = []
with open("/content/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 = ""
# 学習したモデルを用いてタスクを実行
results = []
for data in tqdm(datasets):
input = data["input"]
prompt = f"""### 指示
{input}
### 回答
"""
input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**input_ids, max_new_tokens=512, do_sample=False, repetition_penalty=1.2,)
output = tokenizer.decode(outputs[0][input_ids.input_ids.size(1):], skip_special_tokens=True)
results.append({"task_id": data["task_id"], "input": input, "output": output})
# jsonlで保存
import re
jsonl_id = re.sub(".*/", "", adapter_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')