シンギュラリティ実験ノート

購入した1000ドルPCで何がどこまでできるのか実験した記録です。

UM790 ProのNPUでLLM(opt-1.3b)を動かしてみた

Ryzen AI Software version1.1 がリリースされた。リリース情報を良く読んでみると、version0.8の時点で既に「opt-1.3b」というLLMのデモプロが公開されていたことが分かった。「Llama2 7b」は難易度が高そうだったので、手始めにこのLLMのデモプロから試すことにした。

Copilotに頼んだらDall E3が描いてくれました

opt-1.3bデモプロのダウンロード

上記のリリース情報にあるリンクからダウンロードしようとするとアカウント登録を求められる。メールアドレス等を入力し、アカウント登録するとデモプロ(zipファイル)をダウンロードできる。

デモプロのセットアップ

1)ZIPファイルを展開する

手順はtransformersフォルダの下にあるREADME.mdにある通り。ホームディレクトリにTransformersというフォルダを作りその下にZIPファイルを展開するとtransformersフォルダ以下にデモ環境がコピーされる。

2)Pythonの仮想環境を作る

以下のコマンドで仮想環境 ryzenai-transformers を作る。

cd C:\Users\Transformers\transformers
conda env create --file=env.yaml

仮想環境名を変更したい場合は env.yaml の1行目に定義されている名前を変更する。

仮想環境が構築できたら Anaconda-navigator から作成した仮想環境を立ち上げる。手順書では  conda コマンドでアクティベートするようになっているが、私の場合はうまくいかなかった。Anaconda からアクティベートしてから次の手順に移ったほうがいい。

3)セットアップ

AnadondaからCMDプロンプトを起動して以下のコマンドを実行する。

setup.bat
pip install ops\cpp

4)ユニットテスト

以下のコマンドで動作確認する。

cd tests\python
pytest --num_dlls 2 --num_workers 2 test_qlinear.py

以下のような結果が表示された。ワーニングは出たが問題はなさそうだ。

===== 8 passed, 1 warning in 12.52s =====

デモプロの実行

セットアップが完了したら、models\opt フォルダの下の README.md に従ってデモプロを実行する。 

1)量子化

モデルを量子化して保存するためのコマンドを実行する。しばらくすると量子化したモデルが保存される。

cd models/opt
python save_weights.py --action save --model_name opt-1.3b
Namespace(model_name='opt-1.3b', quant_mode='smooth', action='save')
config.json: 100%|█████████████████████████████████████████████████████████████████████| 653/653 [00:00<00:00, 212kB/s]
(中略)
model.decoder.layers.23.fc2 DynamicQuantizedLinear (2048, 8192) (2048,)
lm_head DynamicQuantizedLinear (50272, 2048) None
Num of params: 1250.609375MB

2)デモプロ実行(その1)

あらかじめ設定された幾つかのプロンプトに30トークンで答えるデモプロである。実行デバイスとしてCPUかNPUを選べるようになっている。NPUの場合は aie と指定するようだ。aie を指定して実行してみた。

python run.py --model_name opt-1.3b --load --smoothquant --quant_mode ptdq --target aie
Namespace(model_name='opt-1.3b', target='aie', quant_mode='ptdq', smoothquant=True, perplexity=False, benchmark=False, dataset='non-raw', load=True)
tokenizer_config.json: 100%|███████████████████████████████████████████████████████████| 685/685 [00:00<00:00, 228kB/s]
(中略)
**********
[PROFILE][CPU] tokenizer: 0.0012726783752441406
[PROFILE][AIE] generate: 6.778407335281372 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
What is the meaning of life?

The meaning of life is the question that is asked by many people. The meaning of life is the
**********
[PROFILE][CPU] tokenizer: 0.0
[PROFILE][AIE] generate: 5.774519681930542 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
Tell me something you don't know.
I don't know how to make a sandwich.
I don't know how to make a sandwich
**********
[PROFILE][CPU] tokenizer: 0.00150299072265625
[PROFILE][AIE] generate: 5.7631471157073975 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
What does Xilinx do?

Xilinx is a global leader in the design and development of integrated circuits, software, and services
**********
[PROFILE][CPU] tokenizer: 0.0010001659393310547
[PROFILE][AIE] generate: 5.767273187637329 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
What is the mass of earth?

The mass of the Earth is about 4.5 billion tons.

What is the mass of
**********
[PROFILE][CPU] tokenizer: 0.0010118484497070312
[PROFILE][AIE] generate: 6.13343620300293 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
What is a poem?

A poem is a written work of art that is written in a particular style. Poems are usually written in
**********
[PROFILE][CPU] tokenizer: 0.0
[PROFILE][AIE] generate: 6.025868892669678 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
What is recursion?

Recursion is a programming technique that allows you to create a recursive function that returns the same result as the original
**********
[PROFILE][CPU] tokenizer: 0.0012636184692382812
[PROFILE][AIE] generate: 5.432344675064087 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
Tell me a one line joke.
I'm not sure I can do that.
I'm not sure I can do that either.

**********
[PROFILE][CPU] tokenizer: 0.0010001659393310547
[PROFILE][AIE] generate: 5.722018718719482 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0
Who is Gilgamesh?
He's the guy who's name is on the door to the right of the entrance to the dungeon.

**********
[PROFILE][CPU] tokenizer: 0.0
[PROFILE][AIE] generate: 5.764328241348267 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0009996891021728516
Tell me something about cryptocurrency.
**********
[PROFILE][CPU] tokenizer: 0.0
[PROFILE][AIE] generate: 5.823832750320435 .. for 30 tokens.
[PROFILE][CPU] tokenizer decode: 0.0010080337524414062
How did it all begin?

I was born in the United States, but I was raised in the United Kingdom. I was born in

3)デモプロ実行(その2)

プロンプトを自由に入力できるデモプロがあったので、「The singulality is(シンギュラリティとは)」と入力して実行してみた。単語と回答のトークン数を入力すると、その後の文章を生成してくれた。

python opt_demo.py  --quant_mode ptdq --target aie --load
Namespace(model_name='opt-1.3b', target='aie', quant_mode='ptdq', load=True)
(中略)
********************
Enter prompt or 'exit': The singularity is
Enter response length (1-1000): 100
Enter 0(greedy search) 1(stochastic search) or 2(contrastive search): 2
Setting search to:  Contrastive search
The singularity is a concept that has been around since at least the 1960’s. The idea was originally developed by Ray Kurzweil, the founder of Google and author of “The Singularity Is Near”. Kurzweil was one of the first to predict the singularity.

Kurzweil has been working on this idea for a long time and has been able to make it come to fruition with his book. In the book he says
********************
Enter prompt or 'exit': exit

Google翻訳による回答の翻訳(Googleの創設者ではないけどGoogleの中でAIの研究活動をしているはず)

シンギュラリティは、少なくとも 1960 年代から存在する概念です。このアイデアはもともと、Google の創設者であり、「The Singularity Is Near」の著者であるレイ カーツワイルによって開発されました。カーツワイルは特異点を最初に予測した人の一人でした。

カーツワイルはこのアイデアに長い間取り組み、著書でそれを実現することができました。本の中で彼はこう言っています

まとめ

意外とすんなりとNPUでLLMが動作してくれた。ただしNPUの動作状況が見えないので、「本当にNPUで動いているのか」と少し不安になる。引数でデバイスとしてCPUを指定して実行しても体感的には変わらない。この辺りはもう少し試してから評価したい。CPUより数倍でも早い結果となってくれるとうれしいのだが。。。

AMDは 7940HS のNPU性能を10TOPS、プロセッサ全体(CPU+GPU+NPU)で30TOPSと言っているので、7940HSのNPUの性能はCPUとさほど変わらないのかもしれない。今年から販売されるRyzen 8000シリーズではNPUの性能が上がるようだ。NPUが面白くなるのはこれからなのだろう。