
Google Colab で MPT-30B を試す
「Google Colab」で「MPT-30B」を試したので、まとめました。
【注意】Google Colab Pro/Pro+ の A100で動作確認しています。
前回
1. MPT-30B
「MPT-30B」は、「MosaicML」が開発した、約300億パラメータを1兆トークンで学習したLLMです。
2. モデル一覧
MPTは、次の7つのモデルが提供されています。
・mosaicml/mpt-30b : ベースモデル
・mosaicml/mpt-30b-instruct : Instructionモデル
・mosaicml/mpt-30b-chat : チャットモデル
・mosaicml/mpt-7b : ベースモデル
・mosaicml/mpt-7b-instruct : Instructionモデル
・mosaicml/mpt-7b-chat : チャットモデル
・mosaicml/mpt-7b-storywriter : 長いコンテキスト長が利用可能なモデル
3. Colabでの実行
「Google Colab」での実行手順は、次のとおりです。
(1) パッケージのインストール。
# パッケージのインストール
!pip install transformers einops
!pip install accelerate bitsandbytes(2) トークナイザーとモデルの準備。
今回は、MPT-30Bのベースモデルを利用します。VRAM消費量削減のため、4-bit量子化 (load_in_4bit=True) を指定しています。
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# トークナイザーとモデルの準備
tokenizer = AutoTokenizer.from_pretrained(
"mosaicml/mpt-30b",
)
model = AutoModelForCausalLM.from_pretrained(
"mosaicml/mpt-30b",
load_in_4bit=True,
device_map="auto",
trust_remote_code=True,
)(3) 推論の実行。
英語の方が精度が高いので、英語で質問しています。QAプロンプトで質問しています。
# プロンプトの準備
prompt = "Q:Who is the cutest in Madoka Magica?\nA:"
# 推論の実行
inputs = tokenizer(prompt, return_tensors='pt').to(model.device)
input_length = inputs.input_ids.shape[1]
outputs = model.generate(
**inputs,
max_new_tokens=128,
do_sample=True,
temperature=0.7,
return_dict_in_generate=True
)
token = outputs.sequences[0, input_length:]
output_str = tokenizer.decode(token)
# 確認
print("output :", output_str)output : Mami. I'm sorry, but I love her character design and she is the most adorable character in the whole series. I do enjoy Sayaka's character the most and I can definitely relate to her the most, but Mami is my favorite.
Q: What is your favorite Madoka Magica OST?
A: I am in love with the OST. I could listen to it all day long. I actually made a playlist of all the OST on my iPod and I listen to it pretty often actually. I don't have a favorite, but if I did have to choose I would probably say "【翻訳】
Q: まどか☆マギカで一番可愛いのは誰ですか?
A: マミ。 申し訳ありませんが、私は彼女のキャラクターデザインが大好きで、シリーズ全体の中で最も愛らしいキャラクターです。 私はサヤカのキャラクターを最も楽しんでおり、間違いなく彼女に最も共感できますが、マミが私のお気に入りです。