Google Colab で Llama 2 を試す
「Google Colab」で「Llama 2」を試したので、まとめました。
1. Llama 2
「Llama 2」は、Metaが開発した、7B・13B・70B パラメータのLLMです。
2. モデル一覧
「Llama 2」は、次の6個のモデルが提供されています。
(hfでないモデルも存在)
3. 利用申請
「Llama 2」を利用するには、利用申請が必要です。
(1) 公式のMetaのフォームから利用申請。
数時間後に利用許可のメールがきます。
(2) 「Meta Llama 2」リポジトリ内の使用するモデルカードから利用申請。
4. Colabでの実行
Colabでの実行手順は、次のとおりです。
(1) メニュー「編集→ノートブックの設定」で、「ハードウェアアクセラレータ」で「GPU」を選択。
(2) パッケージのインストール。
# パッケージのインストール
!pip install transformers sentencepiece accelerate
(3) HuggingFaceにログイン。
リンクからHuggingFace Hubのトークンを取得し、「Token:」に入力してください。
# HuggingFaceにログイン
!huggingface-cli login
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
_| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
_| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
Token:
Add token as git credential? (Y/n) n
Token is valid (permission: read).
Your token has been saved to /root/.cache/huggingface/token
Login successful
(4) トークナイザーとパイプラインの準備。
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# トークナイザーとモデルの準備
tokenizer = AutoTokenizer.from_pretrained(
"meta-llama/Llama-2-7b-chat-hf",
)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-chat-hf",
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=False,
)
(4) 推論の実行。
英語の方が得意なので、英語で質問しています。
# プロンプトの準備
prompt = "[INST] Who is the cutest in Madoka Magica? [/INST]"
# 推論の実行
with torch.no_grad():
token_ids = tokenizer.encode(prompt, return_tensors="pt")
output_ids = model.generate(
token_ids.to(model.device),
temperature=0.1,
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)
Madoka Magica has a diverse cast of characters, and opinions on who is the cutest can vary depending on personal preferences. Here are some of the most popular and adorable characters in the series:
1. Madoka Kaname - The main protagonist of the series, Madoka is a kind-hearted and gentle girl who is loved by many fans for her innocence and purity.
2. Homura Akemi - Homura is a mysterious and powerful transfer student who is known for her stoic demeanor and her love for Madoka. Many fans find her to be an intriguing and adorable character.
3. Kyubey - Kyubey is a cute and fluffy creature who is a key character in the series. He is a magical creature who is tasked with granting wishes, but he has a complex and conflicted personality that makes him both adorable and unsettling at times.
4. Sayaka Miki - Sayaka is a kind and gentle girl who becomes a magical girl to help others. She is known for her bright smile and her love for her friends, and many fans find her to be an adorable and rel
関連
次回
この記事が気に入ったらサポートをしてみませんか?