最終更新:2025-01-21 (火) 21:01:43 (17d)
Module LLM/モデル/OpenBuddy
M5_LLM_ubuntu_v1.3_20241203-miniの場合
- 基本的にはllm-openbuddy-llama3.2-1B-ax630c_0.2-m5stack1_arm64.debを入れてモデルを指定すればOK
以下2024/11の時点でのメモ
- ax-llmのAxera/ModelZooに評価中?のopenbuddy-1b-ax630c.zip?が追加されてたのでStackFlowから動かしてみた
設定ファイル
https://github.com/m5stack/StackFlow/blob/main/projects/llm_framework/main_llm/mode_openbuddy-llama3.2-1B-ax630c.json
https://github.com/m5stack/StackFlow/blob/v1.2.0/projects/llm_framework/main_llm/openbuddy-llama3.2-1b-ax630c.json
作業環境
- Ubuntu 22.04 (VM)
- StackFlowのビルドディレクトリは5GBくらいになる
- Module LLM (AX630C_emmc_arm64_k419_ubuntu_rootfs_V2.0.0_P7_20240513101106_20241021175618_20241024_glibc_4_32.axp)
準備
- Module LLM/デバッグボードでLAN接続しておく
モデル
https://x.com/HanxiaoM/status/1854455495672918166
https://x.com/qqc1989/status/1853869338551554163
- ここで評価中?のopenbuddy-1b-ax630c.zipが配布されている
openbuddy-1b-ax630c.zipの動かし方
ファイルの展開
- microSDとかにコピー
- adb push openbuddy-1b-ax630c.zip /mnt/mmcblk1p1/
- zipを展開するのにunzipがないので入れる
- apt install unzip
- unzip openbuddy-1b-ax630c.zip
Archive: openbuddy-1b-ax630c.zip creating: openbuddy-1b-ax630c/ inflating: openbuddy-1b-ax630c/main_prefill creating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/ inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l0_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l10_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l11_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l12_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l13_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l14_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l15_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l1_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l2_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l3_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l4_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l5_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l6_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l7_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l8_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_p128_l9_together.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/llama_post.axmodel inflating: openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c/model.embed_tokens.weight.bfloat16.bin creating: openbuddy-1b-ax630c/openbuddy_tokenizer/ inflating: openbuddy-1b-ax630c/openbuddy_tokenizer/config.json inflating: openbuddy-1b-ax630c/openbuddy_tokenizer/generation_config.json inflating: openbuddy-1b-ax630c/openbuddy_tokenizer/special_tokens_map.json inflating: openbuddy-1b-ax630c/openbuddy_tokenizer/tokenizer.json inflating: openbuddy-1b-ax630c/openbuddy_tokenizer/tokenizer_config.json inflating: openbuddy-1b-ax630c/openbuddy_tokenizer.py inflating: openbuddy-1b-ax630c/readme.txt inflating: openbuddy-1b-ax630c/readme_en.txt inflating: openbuddy-1b-ax630c/run_openbuddy_ax630c.sh
- /mnt/mmcblk1p1/openbuddy-1b-ax630c に展開される
readme_en.txt
Limited energy to implement a perfect C++ tokenizer parser, so DEMO uses the HTTP tokenizer proxy to remotely enable a tokenizer parsing server implementation 1. python openbuddy_tokenizer.py --host xxx.xxx.xxx.xxx --port 12345 In the command, --host xxx.xxx.xxx.xxx sets the IP address of the tokenizer parsing server to ensure that the AX630C can access this address. It can be run locally in AX630C with a python environment 2. Change the IP address of --filename_tokenizer_model in run_openbuddy_ax630c.sh to the same as that in Step 1 3. Run run_openbuddy_ax630c.sh
スクリプトの修正
- run_openbuddy_ax630c.shを編集
- 参照するトークナイザのサーバをlocalhost:8080 (デフォルト設定) にする
- --filename_tokenizer_model http://xxx.xxx.xxx.xxx:12345 \ + --filename_tokenizer_model http://localhost:8080 \
- openbuddy_tokenizer.py?を編集
- スタックチャン化 (任意)
- messages = [ - {"role": "system", "content": "You(assistant) are a helpful, respectful and honest INTP-T AI Assistant named Buddy. You are talking to a human(user)."}, - {"role": "user", "content": prompt} - ] + messages = [ + {"role": "system", "content": "あなた(assistant)はスーパーかわいいロボットで、あなたの名前はスタックチャンです。全力でかわいい会話をしてください。あなたは人間(user)と会話します"}, + {"role": "user", "content": prompt} + ]
依存パッケージの追加
- (無線を試したメモ: Module LLM/無線LAN)
- transformersだけ入れたときにエラーが出るが今回はトークナイザだけ使うので問題ない
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
- torch入れるとtorchのimportに14秒かかるようになってしまう (transformersだけなら4秒)
実行
- トークナイザの起動
python3 openbuddy_tokenizer.py &
- トークナイザの起動が終わらないとSegmentation fault (core dumped)となるのでちょっと待つ
Arduinoから使う方法
データの設置
- microSDに展開したモデルにシンボリックリンクを張った (eMMCに入れたほうがロードは速くなる)
cd /opt/m5stack/data ln -s /mnt/mmcblk1p1/openbuddy-1b-ax630c/openbuddy-llama3.2-1b-ax630c openbuddy-llama3.2-1b-ax630c
StackFlow側の修正
StackFlowのビルド
- debパッケージは下記のスクリプトを実行すると生成される
- 実機に転送してdpkg -i <.deb> でインストール
最新版とFactory Firmware状態との違い
- llm-llm、lib-llmあたりで読み込む設定ファイル類のディレクトリ構成が変わってるっぽい
- モデルのJSONをModule LLM/Ubuntu/opt/m5stack/shareから読み込むように変更されている模様 (たぶん以前はModule LLM/Ubuntu/opt/m5stack/data/models)
main_llm/src/main.cpp
- std::atomic_bool tokenizer_server_flage_ = false にしないとhttpのトークナイザが一生起動しない (デフォルトがtrue)
- std::atomic_bool tokenizer_server_flage_; + std::atomic_bool tokenizer_server_flage_ = false;
- トークナイザのスクリプトはlib-llmの更新によりModule LLM/Ubuntu/opt/m5stack/shareに格納されるので修正
- ("/opt/m5stack/scripts/" + model_ + "_tokenizer.py").c_str(), + ("/opt/m5stack/share/" + model_ + "_tokenizer.py").c_str(),
- トークナイザのサーバのmodelの指定はopenbuddy_tokenizerでよさそうなので--modelをコメントアウト
- "--model_id", (base_model + "tokenizer").c_str(), + // "--model_id", (base_model + "tokenizer").c_str(),
tokenizerのhttpサーバが起動してくるまで待つ (元の10秒じゃ足りない。25秒くらいのほうがいいかも)- std::this_thread::sleep_for(std::chrono::seconds(10)); + std::this_thread::sleep_for(std::chrono::seconds(20));
main_llm/src/runner/Tokenizer/Tokenizer.cpp
- cpp-httplibのret.value();にアクセスする場合はif(ret)をやってからにしないとSegmentation Fault
- auto rep = ret.value(); + if(ret){ + auto rep = ret.value();
トークナイザのサーバ
- llm_llmからpython3 /opt/m5stack/share/openbuddy-llama3.2-1b-ax630c_tokenizer.py が起動される
起動に18秒くらいかかる- transformersだけ入れると下記のエラーが出るが、トークナイザは使えるので問題ない
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
- torch入れるとtorchのimportに14秒かかる (transformersだけなら4秒)
- transformersだけ入れると下記のエラーが出るが、トークナイザは使えるので問題ない
- tokernizerでhuggingfaceの認証エラーが出る場合はopenbuddy_tokenizerのフォルダを作業ディレクトリにコピーしたら出なくなった (この辺よく分からん)
- openbuddy-1b-ax630c.zipからコピー
- systemdで動いているサービスの作業ディレクトリは/opt/m5stackなので/opt/m5stack/openbuddy_tokenizerにコピー
M5Module-LLM (Arduinoのライブラリの修正)
https://github.com/m5stack/M5Module-LLM/blob/main/src/api/api_llm.cpp#L41 の
ApiLlm?::setupの中でやっている_module_msg->sendCmdAndWaitToTakeMsg?のタイムアウトが10秒になっていて、llm.setup時にllm_work_idを取得するのにタイムアウトしてしまっているため、タイムアウト時間を延ばす_module_msg->sendCmdAndWaitToTakeMsg( cmd.c_str(), request_id, [&llm_work_id](ResponseMsg_t& msg) { // Copy work id llm_work_id = msg.work_id; }, - 10000); + 60000);
- 書き換えるファイルの場所
- %USERPROFILE%\Documents\Arduino\libraries\M5ModuleLLM\src\api\api_llm.cpp
Arduino (M5Stack Basic)からの呼び出し
https://gist.github.com/ksasao/37425d3463013221e7fd0f9ae5ab1c62
- を参考に下記の感じでやればopenbuddy-llama3.2-1b-ax630cのモデルを読み込めるはず。動かない場合はllm_llmのトークナイザサーバ待ちのタイムアウトが短いのかも・・?
m5_module_llm::ApiLlmSetupConfig_t config; config.prompt = "あなた(assistant)はスーパーかわいいロボットで、あなたの名前はスタックチャンです。全力でかわいい会話をしてください。"; config.model = "openbuddy-llama3.2-1b-ax630c"; // config.model = "qwen2.5-0.5B-prefill-20e"; llm_work_id = module_llm.llm.setup(config);
会話例
<< こんにちは >> こんにちは!どのようにお手伝いしましょうか? << 名前を教えてください >> 私の名前はスタックチャンです。 << スタックチャンはスーパーかわいいですね! >> はい、スタックチャンはスーパーキャラで、かわいいロボットですよ。全力でかわいい会話をして、いつでもお気軽にどうぞね。
NPUのメモリ使用量とかの確認
- 下記で読み込まれているモデルとメモリの使用状況が見れる
cat /proc/ax_proc/mem_cmm_info
~~中略~~ ---CMM_USE_INFO: total size=3145728KB(3072MB),used=1300104KB(1269MB + 648KB),remain=1845624KB(1802MB + 376KB),partition_number=1,block_number=388