Python ノードを使用した LLM テキスト生成

このデモでは、OpenVINO モデルサーバーを利用して、LLM モデルを使用してコンテンツをリモートで生成する方法を示します。デモでは、Python 計算機を使用して MediaPipe グラフを提供する方法を説明します。Python 計算機では、実行エンジンとして OpenVINO ランタイムを備えた Hugging Face Optimum を使用します。次の 2 つの使用例が考えられます。

  • 単項呼び出しの場合 - クライアントがグラフにプロンプトを送信し、処理の最後に生成された完全な応答を受信する場合

  • gRPC ストリーミングの場合 - クライアントがグラフにプロンプトを送信し、処理中に部分応答のストリームを受信するとき

単項呼び出しはより単純ですが、応答が完全に生成された場合にのみ返されるため、即時にフィードバックされません。

gRPC ストリームは、生成される応答をユーザーが読み取ることができることから、対話型のアプローチが必要な場合に優れた機能です。

このデモでは tiny-llama-1b-chat モデルを使用した使用例を示しますが、提供される Python スクリプトは他のいくつかの LLM モデル用でも使用できます。それには tiny-llama-1b-chat、red-pajama-3b-chat、llama-2-chat-7b、mistral-7b、zephyr-7b-beta、neural-chat-7b-v3-1、notus-7b-v1 および youri-7b-chat があります。このデモでは、モデルは次のように設定できます。

export SELECTED_MODEL=tiny-llama-1b-chat

要件

Docker エンジンがインストールされ、モデルをロードするのに十分な RAM があり、オプションでインテル® GPU カードが装備された Linux* ホスト。このデモは、インテル® Xeon® Gold 6430 とインテル® データセンター GPU Flex 170 を搭載したホストでテストされました。tiny-llama-1b-chat のような小規模なモデルでデモを実行するには、約 4GB の使用可能な RAM が必要です。

イメージをビルド

必要なすべての Python 依存関係を含むイメージをビルドする必要があります。次のコマンドに従います。

git clone https://github.com/openvinotoolkit/model_server.git
cd model_server
make python_image

イメージが作成されます: openvino/model_server:py

モデルのダウンロード

download_model.py スクリプトを使用してモデルをダウンロードします。

cd demos/python_demos/llm_text_generation
pip install -r requirements.txt

python download_model.py --help
INFO:nncf:NNCF initialized successfully. Supported frameworks detected: torch, onnx, openvino
usage: download_model.py [-h] --model {tiny-llama-1b-chat,red-pajama-3b-chat,llama-2-chat-7b,mistral-7b,zephyr-7b-beta,neural-chat-7b-v3-1,notus-7b-v1,youri-7b-chat}

Script to download LLM model based on https://github.com/openvinotoolkit/openvino_notebooks/blob/releases/2024/0/notebooks/254-llm-chatbot

optional arguments:
  -h, --help            show this help message and exit
  --model {tiny-llama-1b-chat,red-pajama-3b-chat,llama-2-chat-7b,mistral-7b,zephyr-7b-beta,neural-chat-7b-v3-1,notus-7b-v1,youri-7b-chat}
                        Select the LLM model out of supported list

python download_model.py --model ${SELECTED_MODEL}

モデルは ./tiny-llama-1b-chat ディレクトリーにあります。

重み圧縮 - オプション

重み圧縮は元のモデルに適用される場合があります。8 ビットまたは 4 ビットの重み圧縮を適用すると、モデルのサイズとメモリー要件が緩和されると同時に、精度の低いレイヤーで計算を実行することで実行速度が向上します。

python compress_model.py --help
INFO:nncf:NNCF initialized successfully. Supported frameworks detected: torch, onnx, openvino
usage: compress_model.py [-h] --model {tiny-llama-1b-chat,red-pajama-3b-chat,llama-2-chat-7b,mistral-7b,zephyr-7b-beta,neural-chat-7b-v3-1,notus-7b-v1,youri-7b-chat}

Script to compress LLM model based on https://github.com/openvinotoolkit/openvino_notebooks/blob/releases/2024/0/notebooks/254-llm-chatbot

optional arguments:
  -h, --help            show this help message and exit
  --model {tiny-llama-1b-chat,red-pajama-3b-chat,llama-2-chat-7b,mistral-7b,zephyr-7b-beta,neural-chat-7b-v3-1,notus-7b-v1,youri-7b-chat}
                        Select the LLM model out of supported list

python compress_model.py --model ${SELECTED_MODEL}

このスクリプトを実行すると、FP16、INT8、INT4 精度のモデルの圧縮バージョンを含む新しいディレクトリーが作成されます。圧縮モデルは互換性のある入力と出力を備えているため、元のモデルの代わりに使用できます。

ls  -1 | grep tiny-llama-1b-chat
tiny-llama-1b-chat
tiny-llama-1b-chat_FP16
tiny-llama-1b-chat_INT4_compressed_weights
tiny-llama-1b-chat_INT8_compressed_weights

注: モデルの重みに量子化を適用すると、モデルの精度に影響を与える可能性があります。テストを行って、結果が許容可能な品質であることを確認してください。

注: FP16 精度をネイティブにサポートするターゲットデバイス (つまり GPU) では、OpenVINO は精度を FP32 から FP16 に自動的に調整します。これによりパフォーマンスが向上しますが、通常は精度に影響しません。元の精度は、ov_config キーを使用して強制できます: {"INFERENCE_PRECISION_HINT": "f32"}

単項呼び出しで LLM を使用

Python 計算機を使用した OpenVINO モデルサーバーのデプロイ

./model ディレクトリーをモデルとともにマウントします。
以下を含む ./servable_unary または ./servable_stream をマウントします。

  • model.py および config.py - 実行に必要な Python スクリプトで、optimum-intel アクセラレーションを備えた Hugging Face ユーティリティーを使用します。

  • config.json - ロードするサーバブルを定義します

  • graph.pbtxt - Python 計算機を含む MediaPipe グラフを定義します

ユースケースに応じて、./servable_unary と ././servable_stream は異なる方法を示します。

  • 単項 - 単一の要求 - 単一の応答。要求にそれほど時間がかからず、中間結果がない場合に便利です

  • ストリーム - 単一の要求 - 新しい中間結果が利用可能になるとすぐに配信される複数の応答

単項の例をテストするには:

docker run -d --rm -p 9000:9000 -v ${PWD}/servable_unary:/workspace -v ${PWD}/${SELECTED_MODEL}:/model \
-e SELECTED_MODEL=${SELECTED_MODEL} openvino/model_server:py --config_path /workspace/config.json --port 9000

コンテナにマウントされているモデルパスを変更するだけで、圧縮モデルをデプロイすることもできます。例えば、8 ビットの重み圧縮モデルをデプロイするには、次のようにします。

docker run -d --rm -p 9000:9000 -v ${PWD}/servable_unary:/workspace -v ${PWD}/${SELECTED_MODEL}_INT8_compressed_weights:/model \
-e SELECTED_MODEL=${SELECTED_MODEL} openvino/model_server:py  --config_path /workspace/config.json --port 9000

注: クライアントから要求を送信する前に、Docker コンテナのログを調べて、モデルがロードされていることを確認します。モデルとハードウェアによって、数秒または数分かかります。

注: CPU ではなくインテル® GPU で推論ロードを実行する場合は、docker の実行に追加のパラメーター --device /dev/dri --group-add=$(stat -c "%g" /dev/dri/render*) を渡すだけです。GPU デバイスをコンテナに渡し、正しいグループ・セキュリティー・コンテキストを設定します。

単項 gRPC 呼び出しでクライアントを実行

Python クライアントの依存関係をインストールします。これはストリーミング・クライアントでも一般的な手順です。

pip install -r client_requirements.txt

ランタイム単項クライアント client_unary.py:

python3 client_unary.py --url localhost:9000 --prompt "What is the theory of relativity?"

出力例:

Question:
What is the theory of relativity?

Completion:
The theory of relativity, also known as special relativity and general relativity, is a branch of physics that explains how objects move through space-time and how light travels at different speeds. It has helped us understand how the cosmos works by changing our understanding of how gravity affects space and time. The theory originated with astronomy but has since become widely applied to the study of everyday phenomena. By explaining that all motion is relative, the theory has led to significant advancements in fields such as physics, mathematics, philosophy, and engineering.

Total time 9491 ms

複数のプロンプトを一度に要求します (複数のバッチを処理すると、通常、全体のスループットが向上します)。

python3 client_unary.py --url localhost:9000 \
  -p "What is the theory of relativity?" \
  -p "Who is Albert Einstein?"

出力例:

==== Prompt: What is the theory of relativity? ====
The Theory of Relativity by Albert Einstein is considered to be one of the most significant discoveries in modern astronomy and physics. It describes the behavior of space-time in certain situations where there is "special" force between two objects with different masses. The theory was introduced by physicists Hermann Ayrton Minkowski and Ferdinand von Lindemann in the early years of the twentieth century. Einstein developed his own version of the theory that significantly changed our understanding of general relativity and our ability to model the behavior of the universe. This theory is one of the pillars of modern cosmology and has led scientists such as Stephen Hawking to find solutions for some of the biggest mysteries of the universe, including dark matter and dark energy.

==== Prompt: Who is Albert Einstein? ====
 Albert Einstein was an English-born German mathematician and physicist who made significant contributions to both fields of physics. He famously coined the term "special relativity" in 1905 and his theory of general relativity in 1916 laid the foundation for modern cosmology. Although he passed away at age 76, his impact on scientific thought has lasted well into the 21st century, influencing everything from quantum mechanics to nanotechnology. His name often appears in media debates, especially regarding global warming, as experts dispute Einstein conclusions. Some even dispute his Nobel Prize win for his discovery of the photoelectric effect, arguing it failed empirical measurements. However, Albert's contributions have helped pave the way for contemporary science and its ability to push frontiers, ultimately advancing society itself.

Total time 18421 ms

gRPC ストリーミングを使用してクライアントを実行

Python 計算機を使用した OpenVINO モデルサーバーのデプロイ

モデルサーバーは、./servable_stream とは別のワークスペースをマウントすることで、ストリーミング・サンプルとともにデプロイできます。これには、execute メソッドの最後で完全な結果を返すのではなく、中間結果を提供する、変更された model.py スクリプトが含まれています。Python Calculator をループで実行するため、graph.pbtxt もサイクルを含めるように変更されています。

docker run -d --rm -p 9000:9000 -v ${PWD}/servable_stream:/workspace -v ${PWD}/${SELECTED_MODEL}:/model \
-e SELECTED_MODEL=${SELECTED_MODEL} openvino/model_server:py --config_path /workspace/config.json --port 9000

単項の例と同様に、コンテナにマウントされたモデルパスを変更するだけで、圧縮モデルをデプロイできます。例えば、8 ビットの重み圧縮モデルをデプロイするには、次のようにします。

docker run -d --rm -p 9000:9000 -v ${PWD}/servable_stream:/workspace -v ${PWD}/${SELECTED_MODEL}_INT8_compressed_weights:/model \
-e SELECTED_MODEL=${SELECTED_MODEL} openvino/model_server:py --config_path /workspace/config.json --port 9000

注: クライアントから要求を送信する前に、Docker コンテナのログを調べて、モデルがロードされていることを確認します。モデルとハードウェアによって、数秒または数分かかります。

注: CPU ではなくインテル® GPU で推論ロードを実行する場合は、docker の実行に追加のパラメーター --device /dev/dri --group-add=$(stat -c "%g" /dev/dri/render*) を渡すだけです。GPU デバイスをコンテナに渡し、正しいグループ・セキュリティー・コンテキストを設定します。

LLM および gRPC ストリーミングを使用してクライアントを実行

ストリーミング・クライアント client_stream.py を実行します。

python3 client_stream.py --url localhost:9000 --prompt "What is the theory of relativity?"

出力例 (生成されたテキストは、サーバー上で利用可能になるとすぐに、まとめてコンソールに表示されます):

Question:
What is the theory of relativity?

The theory of relativity is a scientific concept that explains how objects perceived at different locations on the Earth, regardless of their distance from any other source, move in a "relativistic" fashion, where the length or time
 it takes for an object to travel between two points depends on its velocity. It shows that objects do not move on a
 straight line but curve smoothly around corners, due to a fundamental principle known as the Cauchy-Riemann equations. The general framework was first formulated by Gottfried Leibniz in the late 17th century and independently rediscovered many years later by Albert Einstein in his special theory of relativity. The principles governing these changes of space and time have been fundamental to modern physics and cosmology.
END
Total time 12826 ms
Number of responses 159
First response time 318 ms
Average response time: 80.67 ms

複数のプロンプトを一度に要求します (複数のバッチを処理すると、通常、全体のスループットが向上します)。

python3 client_stream.py --url localhost:9000 \
  -p "What is the theory of relativity?" \
  -p "Who is Albert Einstein?"

出力例 (生成されたテキストはコンソールにチャンク単位で表示されます。チャンクごとにコンソールがクリアされ、再度表示されます):

==== Prompt: What is the theory of relativity? ====
The theory of relativity is an area of scientific inquiry that describes the properties of space and time, which were considered by many scientists as inconceivable before it was revealed through the experiments conducted by Albert Einstein in his seminal work, "General Relativity". It holds the assumption that objects perceived simultaneously from different locations on Earth would appear spacelike separated when measured along the same axis and equally distant in proper time, i.e., the "conduct" of space-time. This theory has revolutionized our understanding of time, motion, gravity, universe, space traversability, and the relationship between matter and energy.

==== Prompt: Who is Albert Einstein? ====
 Albert Einstein was an American-born theoretical physicist who revolutionized the fields of physics, cosmology, and relativity by proposing an explanation for gravity that has since become known as special relativity. His work paved the way for the development of quantum mechanics and led to the field of astrophysics."


END
Total time 13123 ms
Number of responses 133
First response time 473 ms
Average response time: 98.67 ms