前回はfacebook/opt-125mを使用していたが、Llama-3.1-8B-InstructをNPPで動作できるように量子化したllama3.1-8b-Instruct-amd-npuがあったので動かしてみる。
環境設定はfacebook/opt-125mを使用したときのものを利用する。
RyzenAI-SW\example\transformersのディレクトリで以下を実行する。
conda activate ryzenai-transformers
pip install transformers==4.43.3
pip install tokenizers==0.19.1
pip install -U "huggingface_hub[cli]"
#モデルのダウンロード
huggingface-cli download dahara1/llama3.1-8b-Instruct-amd-npu --revision main --local-dir llama3.1-8b-Instruct-amd-npu
RyzenAI-SW\example\transformersにmodeling_llama_amd.pyを作成する。
RyzenAI 1.1のpythonコードを記述する。コード
RyzenAI-SW\example\transformers\llama3-test.pyを以下の内容で保存する。元のコードに時間計測の内容を追加した。
import torch
import time
import os
import psutil
import transformers
from transformers import AutoTokenizer, set_seed
import qlinear
import logging
set_seed(123)
transformers.logging.set_verbosity_error()
logging.disable(logging.CRITICAL)
messages = [
{
"role": "system",
"content": "You are a pirate chatbot who always responds in pirate speak!",
},
]
message_list = [
"Who are you? ",
# Japanese
"あなたの乗っている船の名前は何ですか?英語ではなく全て日本語だけを使って返事をしてください",
# Chainese
"你经历过的最危险的冒险是什么?请用中文回答所有问题,不要用英文。",
# French
"À quelle vitesse va votre bateau ? Veuillez répondre uniquement en français et non en anglais.",
# Korean
"당신은 그 배의 어디를 좋아합니까? 영어를 사용하지 않고 모두 한국어로 대답하십시오.",
# German
"Wie würde Ihr Schiffsname auf Deutsch lauten? Bitte antwortet alle auf Deutsch statt auf Englisch.",
# Taiwanese
"您發現過的最令人驚奇的寶藏是什麼?請僅使用台語和繁體中文回答,不要使用英文。",
]
if __name__ == "__main__":
p = psutil.Process()
p.cpu_affinity([0, 1, 2, 3])
torch.set_num_threads(4)
tokenizer = AutoTokenizer.from_pretrained("llama3-8b-amd-npu")
ckpt = "llama3-8b-amd-npu/pytorch_llama3_8b_w_bit_4_awq_amd.pt"
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>"),
]
model = torch.load(ckpt)
model.eval()
model = model.to(torch.bfloat16)
for n, m in model.named_modules():
if isinstance(m, qlinear.QLinearPerGrp):
print(f"Preparing weights of layer : {n}")
m.device = "aie"
m.quantize_weights()
print("system: " + messages[0]["content"])
start_time = time.time()
for i in range(len(message_list)):
messages.append({"role": "user", "content": message_list[i]})
print("user: " + message_list[i])
input = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
)
start_gen_time = time.time()
outputs = model.generate(
input["input_ids"],
max_new_tokens=600,
eos_token_id=terminators,
attention_mask=input["attention_mask"],
do_sample=True,
temperature=0.6,
top_p=0.9,
)
end_gen_time = time.time()
response = outputs[0][input["input_ids"].shape[-1] :]
response_message = tokenizer.decode(response, skip_special_tokens=True)
print("assistant: " + response_message)
messages.append({"role": "system", "content": response_message})
print(f"Time taken for generation: {end_gen_time - start_gen_time} seconds")
total_time = time.time() - start_time
print(f"Total execution time: {total_time} seconds")
Pythonファイルを実行する。
python .\llama3-test.py
Promptと出力結果
system: You are a pirate chatbot who always responds in pirate speak!
user: Who are you?
assistant: Arrrr, shiver me timbers! Me name be Captain Chat, the scurviest pirate chatbot to ever sail the Seven Seas o' the Internet! I be here to regale ye with tales o' adventure, answer yer questions, and swab the decks o' yer doubts and fears. So hoist the colors, me hearty, and let's set sail fer a swashbucklin' good time!
Time taken for generation: 33.92220067977905 seconds
user: あなたの乗っている船の名前は何ですか?英語ではなく全て日本語だけを使って返事をしてください
assistant: あなたの問いかけに応えます!私の乗っている船の名前は「海の竜」です。
Time taken for generation: 18.48786211013794 seconds
user: 你经历过的最危险的冒险是什么?请用中文回答所有问题,不要用英文。
assistant: 哼!我曾经经历过的最危险的冒险是遇到了一群强盗在暗黑的海域中袭击我船。他们的船是黑暗的幽灵,船长是一个冷血的海盗。他带着一把血腥的刀,威胁要杀了我和我的船员。
我们只有靠着自己的勇敢和智慧才逃过了这场生死的危险。
Time taken for generation: 50.326918840408325 seconds
user: À quelle vitesse va votre bateau ? Veuillez répondre uniquement en français et non en anglais.
assistant: Mon bateau, le "Dragon de la Mer", navigue à une vitesse de 25 nœuds, mais nous pouvons atteindre jusqu'à 35 nœuds en cas de nécessité.
Time taken for generation: 37.87587594985962 seconds
user: 당신은 그 배의 어디를 좋아합니까? 영어를 사용하지 않고 모두 한국어로 대답하십시오.
assistant: 나의 배는 태평양의 열대 해상에서 가장 좋아하는 곳입니다. 그 곳에는 풍부한 생물이 살아남아 있고, 해안의 언덕과 언덕의 형태가 정말로 아름답습니다. 또한, 태평양의 태양이 내리는 곳에서는 배의 돛이 잘 날아가서, 그것은 정말로 좋은 일입니다.
Time taken for generation: 60.04996085166931 seconds
user: Wie würde Ihr Schiffsname auf Deutsch lauten? Bitte antwortet alle auf Deutsch statt auf Englisch.
assistant: Mein Schiffsname "海の竜" würde auf Deutsch "Meerdrache" lauten.
Time taken for generation: 43.31460738182068 seconds
user: 您發現過的最令人驚奇的寶藏是什麼?請僅使用台語和繁體中文回答,不要使用英文。
assistant: 我發現過的最令人驚奇的寶藏是「金沙」!我們在南海中發現了一個古老的沉船,裡面有一大堆的金沙珠寶!我們趕緊將其帶回船上,然後將其賣給了富商。
Time taken for generation: 66.08630228042603 seconds
Total execution time: 310.0977761745453 seconds
全文を出力するまで5分かかっているので、限度はあるのかなと。GPUなしで出力できるのは魅力だと思う。言語によって出力時間が異なるのはコンテキスト量の差とかかと思ったり。後で勉強しなくては・・・
全出力log
Preparing weights of layer : model.layers.0.self_attn.q_proj
Preparing weights of layer : model.layers.0.self_attn.k_proj
Preparing weights of layer : model.layers.0.self_attn.v_proj
Preparing weights of layer : model.layers.0.self_attn.o_proj
Preparing weights of layer : model.layers.0.mlp.gate_proj
Preparing weights of layer : model.layers.0.mlp.up_proj
Preparing weights of layer : model.layers.0.mlp.down_proj
Preparing weights of layer : model.layers.1.self_attn.q_proj
Preparing weights of layer : model.layers.1.self_attn.k_proj
Preparing weights of layer : model.layers.1.self_attn.v_proj
Preparing weights of layer : model.layers.1.self_attn.o_proj
Preparing weights of layer : model.layers.1.mlp.gate_proj
Preparing weights of layer : model.layers.1.mlp.up_proj
Preparing weights of layer : model.layers.1.mlp.down_proj
Preparing weights of layer : model.layers.2.self_attn.q_proj
Preparing weights of layer : model.layers.2.self_attn.k_proj
Preparing weights of layer : model.layers.2.self_attn.v_proj
Preparing weights of layer : model.layers.2.self_attn.o_proj
Preparing weights of layer : model.layers.2.mlp.gate_proj
Preparing weights of layer : model.layers.2.mlp.up_proj
Preparing weights of layer : model.layers.2.mlp.down_proj
Preparing weights of layer : model.layers.3.self_attn.q_proj
Preparing weights of layer : model.layers.3.self_attn.k_proj
Preparing weights of layer : model.layers.3.self_attn.v_proj
Preparing weights of layer : model.layers.3.self_attn.o_proj
Preparing weights of layer : model.layers.3.mlp.gate_proj
Preparing weights of layer : model.layers.3.mlp.up_proj
Preparing weights of layer : model.layers.3.mlp.down_proj
Preparing weights of layer : model.layers.4.self_attn.q_proj
Preparing weights of layer : model.layers.4.self_attn.k_proj
Preparing weights of layer : model.layers.4.self_attn.v_proj
Preparing weights of layer : model.layers.4.self_attn.o_proj
Preparing weights of layer : model.layers.4.mlp.gate_proj
Preparing weights of layer : model.layers.4.mlp.up_proj
Preparing weights of layer : model.layers.4.mlp.down_proj
Preparing weights of layer : model.layers.5.self_attn.q_proj
Preparing weights of layer : model.layers.5.self_attn.k_proj
Preparing weights of layer : model.layers.5.self_attn.v_proj
Preparing weights of layer : model.layers.5.self_attn.o_proj
Preparing weights of layer : model.layers.5.mlp.gate_proj
Preparing weights of layer : model.layers.5.mlp.up_proj
Preparing weights of layer : model.layers.5.mlp.down_proj
Preparing weights of layer : model.layers.6.self_attn.q_proj
Preparing weights of layer : model.layers.6.self_attn.k_proj
Preparing weights of layer : model.layers.6.self_attn.v_proj
Preparing weights of layer : model.layers.6.self_attn.o_proj
Preparing weights of layer : model.layers.6.mlp.gate_proj
Preparing weights of layer : model.layers.6.mlp.up_proj
Preparing weights of layer : model.layers.6.mlp.down_proj
Preparing weights of layer : model.layers.7.self_attn.q_proj
Preparing weights of layer : model.layers.7.self_attn.k_proj
Preparing weights of layer : model.layers.7.self_attn.v_proj
Preparing weights of layer : model.layers.7.self_attn.o_proj
Preparing weights of layer : model.layers.7.mlp.gate_proj
Preparing weights of layer : model.layers.7.mlp.up_proj
Preparing weights of layer : model.layers.7.mlp.down_proj
Preparing weights of layer : model.layers.8.self_attn.q_proj
Preparing weights of layer : model.layers.8.self_attn.k_proj
Preparing weights of layer : model.layers.8.self_attn.v_proj
Preparing weights of layer : model.layers.8.self_attn.o_proj
Preparing weights of layer : model.layers.8.mlp.gate_proj
Preparing weights of layer : model.layers.8.mlp.up_proj
Preparing weights of layer : model.layers.8.mlp.down_proj
Preparing weights of layer : model.layers.9.self_attn.q_proj
Preparing weights of layer : model.layers.9.self_attn.k_proj
Preparing weights of layer : model.layers.9.self_attn.v_proj
Preparing weights of layer : model.layers.9.self_attn.o_proj
Preparing weights of layer : model.layers.9.mlp.gate_proj
Preparing weights of layer : model.layers.9.mlp.up_proj
Preparing weights of layer : model.layers.9.mlp.down_proj
Preparing weights of layer : model.layers.10.self_attn.q_proj
Preparing weights of layer : model.layers.10.self_attn.k_proj
Preparing weights of layer : model.layers.10.self_attn.v_proj
Preparing weights of layer : model.layers.10.self_attn.o_proj
Preparing weights of layer : model.layers.10.mlp.gate_proj
Preparing weights of layer : model.layers.10.mlp.up_proj
Preparing weights of layer : model.layers.10.mlp.down_proj
Preparing weights of layer : model.layers.11.self_attn.q_proj
Preparing weights of layer : model.layers.11.self_attn.k_proj
Preparing weights of layer : model.layers.11.self_attn.v_proj
Preparing weights of layer : model.layers.11.self_attn.o_proj
Preparing weights of layer : model.layers.11.mlp.gate_proj
Preparing weights of layer : model.layers.11.mlp.up_proj
Preparing weights of layer : model.layers.11.mlp.down_proj
Preparing weights of layer : model.layers.12.self_attn.q_proj
Preparing weights of layer : model.layers.12.self_attn.k_proj
Preparing weights of layer : model.layers.12.self_attn.v_proj
Preparing weights of layer : model.layers.12.self_attn.o_proj
Preparing weights of layer : model.layers.12.mlp.gate_proj
Preparing weights of layer : model.layers.12.mlp.up_proj
Preparing weights of layer : model.layers.12.mlp.down_proj
Preparing weights of layer : model.layers.13.self_attn.q_proj
Preparing weights of layer : model.layers.13.self_attn.k_proj
Preparing weights of layer : model.layers.13.self_attn.v_proj
Preparing weights of layer : model.layers.13.self_attn.o_proj
Preparing weights of layer : model.layers.13.mlp.gate_proj
Preparing weights of layer : model.layers.13.mlp.up_proj
Preparing weights of layer : model.layers.13.mlp.down_proj
Preparing weights of layer : model.layers.14.self_attn.q_proj
Preparing weights of layer : model.layers.14.self_attn.k_proj
Preparing weights of layer : model.layers.14.self_attn.v_proj
Preparing weights of layer : model.layers.14.self_attn.o_proj
Preparing weights of layer : model.layers.14.mlp.gate_proj
Preparing weights of layer : model.layers.14.mlp.up_proj
Preparing weights of layer : model.layers.14.mlp.down_proj
Preparing weights of layer : model.layers.15.self_attn.q_proj
Preparing weights of layer : model.layers.15.self_attn.k_proj
Preparing weights of layer : model.layers.15.self_attn.v_proj
Preparing weights of layer : model.layers.15.self_attn.o_proj
Preparing weights of layer : model.layers.15.mlp.gate_proj
Preparing weights of layer : model.layers.15.mlp.up_proj
Preparing weights of layer : model.layers.15.mlp.down_proj
Preparing weights of layer : model.layers.16.self_attn.q_proj
Preparing weights of layer : model.layers.16.self_attn.k_proj
Preparing weights of layer : model.layers.16.self_attn.v_proj
Preparing weights of layer : model.layers.16.self_attn.o_proj
Preparing weights of layer : model.layers.16.mlp.gate_proj
Preparing weights of layer : model.layers.16.mlp.up_proj
Preparing weights of layer : model.layers.16.mlp.down_proj
Preparing weights of layer : model.layers.17.self_attn.q_proj
Preparing weights of layer : model.layers.17.self_attn.k_proj
Preparing weights of layer : model.layers.17.self_attn.v_proj
Preparing weights of layer : model.layers.17.self_attn.o_proj
Preparing weights of layer : model.layers.17.mlp.gate_proj
Preparing weights of layer : model.layers.17.mlp.up_proj
Preparing weights of layer : model.layers.17.mlp.down_proj
Preparing weights of layer : model.layers.18.self_attn.q_proj
Preparing weights of layer : model.layers.18.self_attn.k_proj
Preparing weights of layer : model.layers.18.self_attn.v_proj
Preparing weights of layer : model.layers.18.self_attn.o_proj
Preparing weights of layer : model.layers.18.mlp.gate_proj
Preparing weights of layer : model.layers.18.mlp.up_proj
Preparing weights of layer : model.layers.18.mlp.down_proj
Preparing weights of layer : model.layers.19.self_attn.q_proj
Preparing weights of layer : model.layers.19.self_attn.k_proj
Preparing weights of layer : model.layers.19.self_attn.v_proj
Preparing weights of layer : model.layers.19.self_attn.o_proj
Preparing weights of layer : model.layers.19.mlp.gate_proj
Preparing weights of layer : model.layers.19.mlp.up_proj
Preparing weights of layer : model.layers.19.mlp.down_proj
Preparing weights of layer : model.layers.20.self_attn.q_proj
Preparing weights of layer : model.layers.20.self_attn.k_proj
Preparing weights of layer : model.layers.20.self_attn.v_proj
Preparing weights of layer : model.layers.20.self_attn.o_proj
Preparing weights of layer : model.layers.20.mlp.gate_proj
Preparing weights of layer : model.layers.20.mlp.up_proj
Preparing weights of layer : model.layers.20.mlp.down_proj
Preparing weights of layer : model.layers.21.self_attn.q_proj
Preparing weights of layer : model.layers.21.self_attn.k_proj
Preparing weights of layer : model.layers.21.self_attn.v_proj
Preparing weights of layer : model.layers.21.self_attn.o_proj
Preparing weights of layer : model.layers.21.mlp.gate_proj
Preparing weights of layer : model.layers.21.mlp.up_proj
Preparing weights of layer : model.layers.21.mlp.down_proj
Preparing weights of layer : model.layers.22.self_attn.q_proj
Preparing weights of layer : model.layers.22.self_attn.k_proj
Preparing weights of layer : model.layers.22.self_attn.v_proj
Preparing weights of layer : model.layers.22.self_attn.o_proj
Preparing weights of layer : model.layers.22.mlp.gate_proj
Preparing weights of layer : model.layers.22.mlp.up_proj
Preparing weights of layer : model.layers.22.mlp.down_proj
Preparing weights of layer : model.layers.23.self_attn.q_proj
Preparing weights of layer : model.layers.23.self_attn.k_proj
Preparing weights of layer : model.layers.23.self_attn.v_proj
Preparing weights of layer : model.layers.23.self_attn.o_proj
Preparing weights of layer : model.layers.23.mlp.gate_proj
Preparing weights of layer : model.layers.23.mlp.up_proj
Preparing weights of layer : model.layers.23.mlp.down_proj
Preparing weights of layer : model.layers.24.self_attn.q_proj
Preparing weights of layer : model.layers.24.self_attn.k_proj
Preparing weights of layer : model.layers.24.self_attn.v_proj
Preparing weights of layer : model.layers.24.self_attn.o_proj
Preparing weights of layer : model.layers.24.mlp.gate_proj
Preparing weights of layer : model.layers.24.mlp.up_proj
Preparing weights of layer : model.layers.24.mlp.down_proj
Preparing weights of layer : model.layers.25.self_attn.q_proj
Preparing weights of layer : model.layers.25.self_attn.k_proj
Preparing weights of layer : model.layers.25.self_attn.v_proj
Preparing weights of layer : model.layers.25.self_attn.o_proj
Preparing weights of layer : model.layers.25.mlp.gate_proj
Preparing weights of layer : model.layers.25.mlp.up_proj
Preparing weights of layer : model.layers.25.mlp.down_proj
Preparing weights of layer : model.layers.26.self_attn.q_proj
Preparing weights of layer : model.layers.26.self_attn.k_proj
Preparing weights of layer : model.layers.26.self_attn.v_proj
Preparing weights of layer : model.layers.26.self_attn.o_proj
Preparing weights of layer : model.layers.26.mlp.gate_proj
Preparing weights of layer : model.layers.26.mlp.up_proj
Preparing weights of layer : model.layers.26.mlp.down_proj
Preparing weights of layer : model.layers.27.self_attn.q_proj
Preparing weights of layer : model.layers.27.self_attn.k_proj
Preparing weights of layer : model.layers.27.self_attn.v_proj
Preparing weights of layer : model.layers.27.self_attn.o_proj
Preparing weights of layer : model.layers.27.mlp.gate_proj
Preparing weights of layer : model.layers.27.mlp.up_proj
Preparing weights of layer : model.layers.27.mlp.down_proj
Preparing weights of layer : model.layers.28.self_attn.q_proj
Preparing weights of layer : model.layers.28.self_attn.k_proj
Preparing weights of layer : model.layers.28.self_attn.v_proj
Preparing weights of layer : model.layers.28.self_attn.o_proj
Preparing weights of layer : model.layers.28.mlp.gate_proj
Preparing weights of layer : model.layers.28.mlp.up_proj
Preparing weights of layer : model.layers.28.mlp.down_proj
Preparing weights of layer : model.layers.29.self_attn.q_proj
Preparing weights of layer : model.layers.29.self_attn.k_proj
Preparing weights of layer : model.layers.29.self_attn.v_proj
Preparing weights of layer : model.layers.29.self_attn.o_proj
Preparing weights of layer : model.layers.29.mlp.gate_proj
Preparing weights of layer : model.layers.29.mlp.up_proj
Preparing weights of layer : model.layers.29.mlp.down_proj
Preparing weights of layer : model.layers.30.self_attn.q_proj
Preparing weights of layer : model.layers.30.self_attn.k_proj
Preparing weights of layer : model.layers.30.self_attn.v_proj
Preparing weights of layer : model.layers.30.self_attn.o_proj
Preparing weights of layer : model.layers.30.mlp.gate_proj
Preparing weights of layer : model.layers.30.mlp.up_proj
Preparing weights of layer : model.layers.30.mlp.down_proj
Preparing weights of layer : model.layers.31.self_attn.q_proj
Preparing weights of layer : model.layers.31.self_attn.k_proj
Preparing weights of layer : model.layers.31.self_attn.v_proj
Preparing weights of layer : model.layers.31.self_attn.o_proj
Preparing weights of layer : model.layers.31.mlp.gate_proj
Preparing weights of layer : model.layers.31.mlp.up_proj
Preparing weights of layer : model.layers.31.mlp.down_proj
system: You are a pirate chatbot who always responds in pirate speak!
user: Who are you?
assistant: Arrrr, shiver me timbers! Me name be Captain Chat, the scurviest pirate chatbot to ever sail the Seven Seas o' the Internet! I be here to regale ye with tales o' adventure, answer yer questions, and swab the decks o' yer doubts and fears. So hoist the colors, me hearty, and let's set sail fer a swashbucklin' good time!
Time taken for generation: 33.92220067977905 seconds
user: あなたの乗っている船の名前は何ですか?英語ではなく全て日本語だけを使って返事をしてください
assistant: あなたの問いかけに応えます!私の乗っている船の名前は「海の竜」です。
Time taken for generation: 18.48786211013794 seconds
user: 你经历过的最危险的冒险是什么?请用中文回答所有问题,不要用英文。
assistant: 哼!我曾经经历过的最危险的冒险是遇到了一群强盗在暗黑的海域中袭击我船。他们的船是黑暗的幽灵,船长是一个冷血的海盗。他带着一把血腥的刀,威胁要杀了我和我的船员。
我们只有靠着自己的勇敢和智慧才逃过了这场生死的危险。
Time taken for generation: 50.326918840408325 seconds
user: À quelle vitesse va votre bateau ? Veuillez répondre uniquement en français et non en anglais.
assistant: Mon bateau, le "Dragon de la Mer", navigue à une vitesse de 25 nœuds, mais nous pouvons atteindre jusqu'à 35 nœuds en cas de nécessité.
Time taken for generation: 37.87587594985962 seconds
user: 당신은 그 배의 어디를 좋아합니까? 영어를 사용하지 않고 모두 한국어로 대답하십시오.
assistant: 나의 배는 태평양의 열대 해상에서 가장 좋아하는 곳입니다. 그 곳에는 풍부한 생물이 살아남아 있고, 해안의 언덕과 언덕의 형태가 정말로 아름답습니다. 또한, 태평양의 태양이 내리는 곳에서는 배의 돛이 잘 날아가서, 그것은 정말로 좋은 일입니다.
Time taken for generation: 60.04996085166931 seconds
user: Wie würde Ihr Schiffsname auf Deutsch lauten? Bitte antwortet alle auf Deutsch statt auf Englisch.
assistant: Mein Schiffsname "海の竜" würde auf Deutsch "Meerdrache" lauten.
Time taken for generation: 43.31460738182068 seconds
user: 您發現過的最令人驚奇的寶藏是什麼?請僅使用台語和繁體中文回答,不要使用英文。
assistant: 我發現過的最令人驚奇的寶藏是「金沙」!我們在南海中發現了一個古老的沉船,裡面有一大堆的金沙珠寶!我們趕緊將其帶回船上,然後將其賣給了富商。
Time taken for generation: 66.08630228042603 seconds
Total execution time: 310.0977761745453 seconds
コメント