見出し画像

Google Colab で Mixtral-8x7B-v0.1 を試す

「Google Colab」で「Mistral AI」の「Mixtral-8x7B-v0.1」を試したので、まとめました。

【注意】Google Colab Pro/Pro+のA100で動作確認しています。


1. Mixtral-8x7B-v0.1

Mixtral 8x7B」は「Mistral AI」が開発した高品質な「SMoE」(sparse mixture-of-experts)モデルです。推論は6倍速く、ほとんどのベンチマークで「Llama2 70B」を上回っています。

2. Mixtral-8x7B-v0.1 のモデル

「Mixtral-8x7B-v0.1」は、現在2つのモデルのみが提供されています。

mistralai/Mixtral-8x7B-v0.1 : ベースモデル
mistralai/Mixtral-8x7B-Instruct-v0.1 : 指示モデル

3. Colabでの実行

Colabでの実行手順は、次のとおりです。

(1) Colabのノートブックを開き、メニュー「編集 → ノートブックの設定」で「GPU」で「A100」を選択。

(2) パッケージのインストール。

# パッケージのインストール
!pip install -U transformers sentencepiece accelerate bitsandbytes

(2) トークナイザーとモデルの準備。
今回は、「mistralai/Mixtral-8x7B-Instruct-v0.1」を4bit量子化で読み込みました。20分ほどかかりました。

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# トークナイザーとモデルの準備
tokenizer = AutoTokenizer.from_pretrained(
    "mistralai/Mixtral-8x7B-Instruct-v0.1",
)
model = AutoModelForCausalLM.from_pretrained(
    "mistralai/Mixtral-8x7B-Instruct-v0.1",
    torch_dtype=torch.bfloat16,
    load_in_4bit=True,
    device_map="auto",
    trust_remote_code=False,
)

(3) 推論の実行。

# メッセージリストの準備
messages = [
    {"role": "user", "content": "Who is the cutest in Madoka Magica?"},
]

# 推論の実行
with torch.no_grad():
    token_ids = tokenizer.apply_chat_template(messages, return_tensors="pt")
    output_ids = model.generate(
        token_ids.to(model.device),
        temperature=0.5,
        do_sample=True,
        top_p=0.95,
        top_k=40,
        max_new_tokens=256,
    )
output = tokenizer.decode(output_ids[0][token_ids.size(1) :])
print(output)
The concept of "cuteness" is subjective and can vary from person to person. In the anime series Madoka Magica, different characters may appeal to different people for various reasons. Some fans may find Madoka Kaname to be the cutest due to her kind and innocent personality, while others may find Homura Akemi's determination and resilience endearing. Still, others may find the quirky and energetic personality of Kyubey to be the most appealing. Ultimately, the cutest character in Madoka Magica is a matter of personal preference.</s>

【翻訳】
「かわいい」の概念は主観的なものであり、人によって異なります。 アニメシリーズ「まどか☆マギカ」では、さまざまなキャラクターがさまざまな理由でさまざまな人々にアピールする可能性があります。 優しくて無邪気な性格の鹿目まどかが最もかわいいと感じるファンもいるかもしれませんが、暁美ほむらの決意と回復力が愛らしいと感じる人もいるかもしれません。 それでも、キュゥべえの風変わりでエネルギッシュな性格が最も魅力的だと感じる人もいるかもしれません。 結局のところ、まどか☆マギカで一番かわいいキャラクターは個人の好みの問題です。</s>

関連



この記事が気に入ったらサポートをしてみませんか?