Model Description

このモデルは東京大学松尾・岩澤研究室のLLM講座2024の課題のために作られたものです。

  • Base Model type: google/gemma-2-9b
  • Language(s) (NLP): Main languages are English, Japanese
  • License: This model is licensed under Gemma

Uses

以下はGoogle ColabでL4を用いた場合の実行コードです。 事前に現在のディレクトリ配下にelyza-tasks-100-TV_0.jsonlを配置する必要があります。

# ライブラリのインストール
!pip install transformers==4.46.3 accelerate==1.1.1
# モデル・アダプター・トークナイザーの読み込み
import torch
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
)

HF_TOKEN = "{your_hf_token}"
model_id = "google/gemma-2-9b"
adapter_id = "miya-99999/matsuo_llm_exp07_ckp6000"
device = "cuda" if torch.cuda.is_available() else "cpu"

tokenizer = AutoTokenizer.from_pretrained(adapter_id, token=HF_TOKEN)
model = AutoModelForCausalLM.from_pretrained(
    model_id, 
    device_map=device,
    token=HF_TOKEN,
    torch_dtype=torch.bfloat16,
    use_cache=True
)

model.load_adapter(adapter_id, token=HF_TOKEN)
# テストデータの読み込み
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"]

  chat = [
    {"role": "user", "content": input},
  ]

  tokenized_input = tokenizer.apply_chat_template(chat, add_generation_prompt=True, tokenize=True, return_tensors="pt").to(model.device)
  with torch.no_grad():
      outputs = model.generate(
          tokenized_input,
          max_new_tokens=512,
          do_sample=False,
          repetition_penalty=1.2,
          pad_token_id=tokenizer.pad_token_id,
          eos_token_id=[1,107],
      )[0]
  output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
  print("入力:", input)
  print("出力:", output)
  print("\n", "="*50, "\n")

  results.append({"task_id": data["task_id"], "input": input, "output": output})
# 推論結果の保存
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')

Fine-tuning Dataset

(*1)…シングルターンになるように対話の1回目だけ抽出。
(*2)…50,439件をランダムサンプリング。

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model is not currently available via any of the supported Inference Providers.
The model cannot be deployed to the HF Inference API: The model has no pipeline_tag.

Collection including miya-99999/matsuo_llm_exp07_ckp6000