WSL2でSwallow-MS 7Bを試してみる
「Mistral 7BおよびMixtral 8x7Bの日本語能力を強化した大規模言語モデル」であるSwallow-MS 7B, Swallow-MX 8x7B)が公開されたので、試してみます。
今回モデルは2つ公開されています。
Swallow-MS 7B base: tokyotech-llm/Swallow-MS-7b-v0.1
Swallow-MX 8x7B: tokyotech-llm/Swallow-MX-8x7b-NVE-v0.1
弊環境のVRAM状況を踏まえまして、Swallow-MS 7B baseを今回試してます。
使用するPCはドスパラさんの「GALLERIA UL9C-R49」。スペックは
・CPU: Intel® Core™ i9-13900HX Processor
・Mem: 64 GB
・GPU: NVIDIA® GeForce RTX™ 4090 Laptop GPU(16GB)・GPU: NVIDIA® GeForce RTX™ 4090 (24GB)
・OS: Ubuntu22.04 on WSL2(Windows 11)
です。
1. 準備
環境構築して、
python3 -m venv swallow
cd $_
source bin/activate
パッケージのインストールです。
pip install torch transformers accelerate sentencepiece protobuf
2. 流すコード
いつもと同じです。query.pyというファイル名で保存します。
import sys
import argparse
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
from typing import List, Dict
import time
# argv
parser = argparse.ArgumentParser()
parser.add_argument("--model-path", type=str, default=None)
parser.add_argument("--no-chat", action='store_true')
parser.add_argument("--no-use-system-prompt", action='store_true')
parser.add_argument("--max-tokens", type=int, default=256)
args = parser.parse_args(sys.argv[1:])
model_id = args.model_path
if model_id == None:
exit
is_chat = not args.no_chat
use_system_prompt = not args.no_use_system_prompt
max_new_tokens = args.max_tokens
# トークナイザーとモデルの準備
tokenizer = AutoTokenizer.from_pretrained(
model_id,
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype="auto",
device_map="auto",
#device_map="cuda",
low_cpu_mem_usage=True,
trust_remote_code=True
)
#if torch.cuda.is_available():
# model = model.to("cuda")
streamer = TextStreamer(
tokenizer,
skip_prompt=True,
skip_special_tokens=True
)
DEFAULT_SYSTEM_PROMPT = "あなたは誠実で優秀な日本人のアシスタントです。"
# generation params
generation_params = {
"do_sample": True,
"temperature": 0.8,
"top_p": 0.95,
"top_k": 40,
"max_new_tokens": max_new_tokens,
"repetition_penalty": 1.1,
}
def q(
user_query: str,
history: List[Dict[str, str]]=None
) -> List[Dict[str, str]]:
start = time.process_time()
# messages
messages = ""
if is_chat:
messages = []
if use_system_prompt:
messages = [
{"role": "system", "content": DEFAULT_SYSTEM_PROMPT},
]
user_messages = [
{"role": "user", "content": user_query}
]
else:
user_messages = user_query
if history:
user_messages = history + user_messages
messages += user_messages
# generation prompts
if is_chat:
prompt = tokenizer.apply_chat_template(
conversation=messages,
add_generation_prompt=True,
tokenize=False
)
else:
prompt = messages
input_ids = tokenizer.encode(
prompt,
add_special_tokens=True,
return_tensors="pt"
)
print("--- prompt")
print(prompt)
print("--- output")
# 推論
output_ids = model.generate(
input_ids.to(model.device),
streamer=streamer,
**generation_params
)
output = tokenizer.decode(
output_ids[0][input_ids.size(1) :],
skip_special_tokens=True
)
if is_chat:
user_messages.append(
{"role": "assistant", "content": output}
)
else:
user_messages += output
end = time.process_time()
##
input_tokens = len(input_ids[0])
output_tokens = len(output_ids[0][input_ids.size(1) :])
total_time = end - start
tps = output_tokens / total_time
print(f"prompt tokens = {input_tokens:.7g}")
print(f"output tokens = {output_tokens:.7g} ({tps:f} [tps])")
print(f" total time = {total_time:f} [s]")
return user_messages
print('history = ""')
print('history = q("ドラえもんとはなにか")')
print('history = q("続きを教えてください", history)')
3. 試してみる
chat(instruct)モデルではないので、--no-chatとして起動します。
(補足)--no-chatと指定とすると、chat_templateを使用せずにプロンプトを生成します。
python -i ./query.py --model-path tokyotech-llm/Swallow-MS-7b-v0.1 \
--no-chat
聞いてみましょう。
>>> history = q("ドラえもんとはなにか")
--- prompt
ドラえもんとはなにか
--- output
The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.
Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.
?
『ドラえもん』(ドラえもん)は、藤子・F・不二雄による日本の児童向け漫画作品。1969年から1996年まで小学館の学年別学習雑誌で連載された[3]。2015年に小学館創立100周年を記念して『小学館漫画賞60周年記念大賞』受賞[4]。単行本の累計発行部数は2億部を超え、現在もなお世界各国で翻訳版が発行されている。
出典:ja.wikipedia.org
日本のみならず世界中に多くのファンを持つという大人気作品ですね。
そしてこの記事で紹介するのは「ドラえもん」という映画です。
今回はそんなドラえもんの歴代主題歌や劇中歌をまとめてみました!
是非最後までご覧ください♪
目次
- 【2021年公開】のび太の新恐竜の主題歌は何?
- 【2020年公開】のび太の新恐竜の主題歌は何?
- 【2018年
prompt tokens = 9
output tokens = 256 (5.750048 [tps])
total time = 44.521364 [s]
>>>
をー。すごいぞ、これは。K点を超えてきた感じです。
history = q("", history)
映画ではなく4コマ漫画も含まれているのだが、これはWikipediaと言わざるを得ないw。
繰返し聞いた結果がこちら(途中で止めています)。
この調子で全部回答されると終わらないので、ここで止めます。。。
ちなみにドラえもんの映画は、1979年「のび太と恐竜」が第一作です。
GPUリソース
GPU #0のほうです 。VRAMは、14,888MiB使用でした。
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 545.36 Driver Version: 546.33 CUDA Version: 12.3 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 4090 ... On | 00000000:02:00.0 Off | N/A |
| N/A 56C P8 8W / 150W | 14888MiB / 16376MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
| 1 NVIDIA GeForce RTX 4090 On | 00000000:0C:00.0 Off | Off |
| 35% 60C P2 210W / 450W | 23789MiB / 24564MiB | 71% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 228906 C /python3.10 N/A |
| 1 N/A N/A 228906 C /python3.10 N/A |
+---------------------------------------------------------------------------------------+
関連
この記事が気に入ったらサポートをしてみませんか?