SEが最近起こったことを書くブログ

ITエンジニアが試したこと、気になったことを書いていきます。

line-corporation/japanese-large-lm-3.6b-instruction-sftを動かしてみた

ほぼ、npakaさんの記事そのままですが、誰かの役に立つかもしれないので、 line-corporation/japanese-large-lm-3.6b-instruction-sftをGoogle ColabのT4GPUで動かしたときのメモを残します

# パッケージのインストール
!pip install transformers accelerate bitsandbytes
!pip install sentencepiece
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

# トークナイザーとモデルの準備
tokenizer = AutoTokenizer.from_pretrained(
    "line-corporation/japanese-large-lm-3.6b-instruction-sft",
    use_fast=False
)
model = AutoModelForCausalLM.from_pretrained(
    "line-corporation/japanese-large-lm-3.6b-instruction-sft",
    load_in_8bit=True,
    torch_dtype=torch.float16,
    device_map="auto",

    )

input_text = """四国の県名を全て列挙してください。"""
# プロンプトの準備
prompt = f"ユーザー: {input_text}\nシステム: "

# 推論の実行
input_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
tokens = model.generate(
    input_ids.to(device=model.device),
    max_length=256,
    temperature=0.7,
    do_sample=True,
    pad_token_id=tokenizer.pad_token_id,
)
output = tokenizer.decode(tokens[0])
print(output)

参考ページ

note.com

engineering.linecorp.com