ノート:
- このチュートリアルでは、Oracle Cloudへのアクセスが必要です。無料アカウントにサインアップするには、Oracle Cloud Infrastructure Free Tierの開始を参照してください。
- Oracle Cloud Infrastructureの資格証明、テナンシおよびコンパートメントの値の例を使用します。演習を完了するときに、これらの値をクラウド環境に固有の値に置き換えます。
ワンクリック・デプロイメントを使用したOracle Resource Managerを使用したOCI Compute A10インスタンスでのミストラルLLMモデルの実行
イントロダクション
Oracle Cloud Infrastructure (OCI) Computeでは、様々なタイプのシェイプを作成して、ローカルにデプロイされた人工知能(AI)モデルのグラフィック処理ユニット(GPU)をテストできます。このチュートリアルでは、Oracle Resource Managerから選択できる既存のVCNおよびサブネット・リソースとともにA10シェイプを使用します。
Terraformコードには、自然言語処理タスク用にローカルのVirtual Large Language Model (vLLM) Mistralモデルを実行するためのインスタンスの構成も含まれます。
目的
- OCI ComputeでA10シェイプを作成し、Mistral AI LLMモデルをダウンロードして、ローカルvLLMモデルを問い合せます。
前提条件
-
OCI Virtual Cloud Network (VCN)と、仮想マシン(VM)がデプロイされるサブネットがあることを確認します。
-
ネットワーク・コンポーネントとその関係の理解。詳細は、ネットワーキングの概要を参照してください。
-
クラウドでのネットワーキングの理解。詳細は、クラウドにおけるネットワーキングのビデオEP.01: Virtual Cloud Networksのビデオをご覧ください。
-
要件:
- インスタンス・タイプ: 1つのNvidia GPUを持つA10シェイプ。
- オペレーティング・システム: Oracle Linux。
- イメージの選択:デプロイメント・スクリプトは、GPUサポートを含む最新のOracle Linuxイメージを選択します。
- タグ:フリーフォーム・タグGPU_TAG = "A10-1"を追加します。
- ブート・ボリューム・サイズ: 250GB
- 初期化: cloud-initを使用して、vLLM Mistralモデルをダウンロードおよび構成します。
タスク1: ワンクリック・デプロイメント用のTerraformコードのダウンロード
orm_stack_a10_gpu-main.zipからORM Terraformコードをダウンロードして、Mistral vLLMモデルをローカルに実装します。これにより、既存のVCNおよびサブネットを選択して、A10インスタンス・シェイプでのMistral vLLMモデルのローカル・デプロイメントをテストできます。
ORM Terraformコードをローカルにダウンロードしたら、フォルダからのスタックの作成のステップに従ってスタックをアップロードし、Terraformコードの適用を実行します。
ノート: VMがデプロイされるOCI Virtual Cloud Network (VCN)およびサブネットが作成されていることを確認します。
タスク2: OCIでのVCNの作成(オプション(まだ作成されていない場合)
Oracle Cloud InfrastructureでVCNを作成するには、OCIでVirtual Cloudネットワークを作成する方法の確認ビデオを参照してください。
または
VCNを作成するには、次のステップに従います。
-
OCIコンソールにログインし、「クラウド・テナント名」、「ユーザー名」および「パスワード」を入力します。
-
左上隅のハンバーガーメニュー(≡)をクリックします。
-
「ネットワーキング」、「Virtual Cloud Networks」に移動し、「リスト範囲」セクションから適切なコンパートメントを選択します。
-
「インターネット接続性を持つVCN」,を選択し、「VCNウィザードの起動」をクリックします
-
「インターネット接続性を持つVCNの作成」ページで、次の情報を入力して「次」をクリックします。
- VCN名:
OCI_HOL_VCN
と入力します。 - コンパートメント:適切なコンパートメントを選択します。
- VCN CIDRブロック:
10.0.0.0/16
と入力します。 - PUBLIC SUBNET CIDR BLOCK:
10.0.2.0/24
と入力します。 - PRIVATE SUBNET CIDR BLOCK:
10.0.1.0/24
と入力します。 - DNS解決: 「このVCNでDNSホスト名を使用」を選択します。
- VCN名:
-
「確認」ページで設定を確認し、「作成」をクリックします。
VCNの作成にはしばらく時間がかかり、進行状況画面ではワークフローが表示されなくなります。
-
VCNが作成されたら、「Virtual Cloud Networkの表示」をクリックします。
実際の状況では、アクセスの必要性(どのポートを開くか)およびアクセスできるユーザーに基づいて、複数のVCNを作成します。
タスク3: cloud-init構成の詳細を参照
cloud-init
スクリプトは、必要なすべての依存関係をインストールし、Dockerを起動し、vLLM Mistralモデルをダウンロードして起動します。タスク1でダウンロードしたcloudinit.sh
ファイルに、次のコードがあります。
dnf install -y dnf-utils zip unzip
dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
dnf remove -y runc
dnf install -y docker-ce --nobest
systemctl enable docker.service
dnf install -y nvidia-container-toolkit
systemctl start docker.service
...
Cloud-initは、Hugging Faceで事前定義されたAPIトークンに基づいてMistralモデルの実行に必要なすべてのファイルをダウンロードします。
APIトークン作成では、ORM GUIからの入力に基づいてMistralモデルが選択され、モデル・ファイルをローカルにダウンロードするために必要な認証が可能になります。詳細は、ユーザー・アクセス・トークンを参照してください。
タスク4: システムのモニター
次のコマンドを使用して、cloud-init
スクリプトの完了およびGPUリソースの使用状況を追跡します(必要な場合)。
-
cloud-initの完了をモニターします:
tail -f /var/log/cloud-init-output.log
。 -
GPU使用率の監視:
nvidia-smi dmon -s mu -c 100
。 -
Pythonを使用してvLLM Mistralモデルをデプロイおよび操作します: (必要な場合にのみパラメータを変更します(コマンドは
cloud-init
スクリプトにすでに含まれています)。python -O -u -m vllm.entrypoints.openai.api_server \ --host 0.0.0.0 \ --model "/home/opc/models/${MODEL}" \ --tokenizer hf-internal-testing/llama-tokenizer \ --max-model-len 16384 \ --enforce-eager \ --gpu-memory-utilization 0.8 \ --max-num-seqs 2 \ >> "${MODEL}.log" 2>&1 &
タスク5: モデル統合のテスト
コマンドまたはJupyter Notebookの詳細を使用して、次の方法でモデルと対話します。
-
cloud-init
スクリプトが完了したら、コマンドライン・インタフェース(CLI)からモデルをテストします。curl -X 'POST' 'http://0.0.0.0:8000/v1/chat/completions' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "model": "/home/opc/models/'"$MODEL"'", "messages": [{"role":"user", "content":"Write a small poem."}], "max_tokens": 64 }'
-
Jupyter Notebookからモデルをテストします(ポート
8888
を開いていることを確認します)。import requests import json import os # Retrieve the MODEL environment variable model = os.environ.get('MODEL') url = 'http://0.0.0.0:8000/v1/chat/completions' headers = { 'accept': 'application/json', 'Content-Type': 'application/json', } data = { "model": f"/home/opc/models/{model}", "messages": [{"role": "user", "content": "Write a short conclusion."}], "max_tokens": 64 } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() # Pretty print the response for better readability formatted_response = json.dumps(result, indent=4) print("Response:", formatted_response) else: print("Request failed with status code:", response.status_code) print("Response:", response.text)
-
Gradioとチャットボットを統合してモデルを問い合せる。
import requests import gradio as gr import os def interact_with_model(prompt): model = os.getenv("MODEL") # Retrieve the MODEL environment variable within the function url = 'http://0.0.0.0:8000/v1/chat/completions' headers = { 'accept': 'application/json', 'Content-Type': 'application/json', } data = { "model": f"/home/opc/models/{model}", "messages": [{"role": "user", "content": prompt}], "max_tokens": 64 } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() completion_text = result["choices"][0]["message"]["content"].strip() # Extract the generated text return completion_text else: return {"error": f"Request failed with status code {response.status_code}"} # Example Gradio interface iface = gr.Interface( fn=interact_with_model, inputs=gr.Textbox(lines=2, placeholder="Write a prompt..."), outputs=gr.Textbox(type="text", placeholder="Response..."), title="Mistral 7B Chat Interface", description="Interact with the Mistral 7B model deployed locally via Gradio.", live=True ) # Launch the Gradio interface iface.launch(share=True)
タスク6: Dockerを使用したモデルのデプロイ(必要な場合)
または、Dockerおよび外部ソースを使用してモデルをデプロイします。
docker run --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HUGGING_FACE_HUB_TOKEN=$ACCESS_TOKEN" \
-p 8000:8000 \
--ipc=host \
--restart always \
vllm/vllm-openai:latest \
--model mistralai/$MODEL \
--max-model-len 16384
次の方法でモデルを問い合せることができます。
-
CLIを使用して、Dockerおよび外部ソースで開始したモデルを問い合せます。
curl -X 'POST' 'http://0.0.0.0:8000/v1/chat/completions' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "model": "mistralai/'"$MODEL"'", "messages": [{"role": "user", "content": "Write a small poem."}], "max_tokens": 64 }'
-
Jupyter Notebookを使用して、外部ソースからDockerでモデルを問い合せます。
import requests import json import os # Retrieve the MODEL environment variable model = os.environ.get('MODEL') url = 'http://0.0.0.0:8000/v1/chat/completions' headers = { 'accept': 'application/json', 'Content-Type': 'application/json', } data = { "model": f"mistralai/{model}", "messages": [{"role": "user", "content": "Write a short conclusion."}], "max_tokens": 64 } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() # Pretty print the response for better readability formatted_response = json.dumps(result, indent=4) print("Response:", formatted_response) else: print("Request failed with status code:", response.status_code) print("Response:", response.text)
-
Jupyter NotebookおよびGradio Chatbotを使用して、外部ソースからDockerでモデルを問い合せます。
import requests import gradio as gr import os # Function to interact with the model via API def interact_with_model(prompt): url = 'http://0.0.0.0:8000/v1/chat/completions' headers = { "accept": "application/json", "Content-Type": "application/json", } # Retrieve the MODEL environment variable model = os.environ.get('MODEL') data = { "model": f"mistralai/{model}", "messages": [{"role": "user", "content": prompt}], "max_tokens": 64 } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() completion_text = result["choices"][0]["message"]["content"].strip() # Extract the generated text return completion_text else: return {"error": f"Request failed with status code {response.status_code}"} # Example Gradio interface iface = gr.Interface( fn=interact_with_model, inputs=gr.Textbox(lines=2, placeholder="Write a prompt..."), outputs=gr.Textbox(type="text", placeholder="Response..."), title="Model Interface", # Set a title for your Gradio interface description="Interact with the model deployed via Gradio.", # Set a description live=True ) # Launch the Gradio interface iface.launch(share=True)
-
すでにダウンロードされているローカル・ファイルを使用してdockerで実行中のモデル(より速く起動します)。
docker run --gpus all \ -v /home/opc/models/$MODEL/:/mnt/model/ \ --env "HUGGING_FACE_HUB_TOKEN=$TOKEN_ACCESS" \ -p 8000:8000 \ --env "TRANSFORMERS_OFFLINE=1" \ --env "HF_DATASET_OFFLINE=1" \ --ipc=host vllm/vllm-openai:latest \ --model="/mnt/model/" \ --max-model-len 16384 \ --tensor-parallel-size 2
-
ローカル・ファイルおよびCLIを使用して、Dockerでモデルを問い合せます。
curl -X 'POST' 'http://0.0.0.0:8000/v1/chat/completions' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{ > "model": "/mnt/model/", > "messages": [{"role": "user", "content": "Write a humorous limerick about the wonders of GPU computing."}], > "max_tokens": 64, > "temperature": 0.7, > "top_p": 0.9 > }'
-
ローカル・ファイルおよびJupyter Notebookを使用して、Dockerでモデルを問い合せます。
import requests import json import os url = "http://0.0.0.0:8000/v1/chat/completions" headers = { "accept": "application/json", "Content-Type": "application/json", } # Assuming `MODEL` is an environment variable set appropriately model = f"/mnt/model/" # Adjust this based on your specific model path or name data = { "model": model, "messages": [{"role": "user", "content": "Write a humorous limerick about the wonders of GPU computing."}], "max_tokens": 64, "temperature": 0.7, "top_p": 0.9 } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() # Extract the generated text from the response completion_text = result["choices"][0]["message"]["content"].strip() print("Generated Text:", completion_text) else: print("Request failed with status code:", response.status_code) print("Response:", response.text)
-
Jupyter NotebookおよびGradio Chatbotを使用して、外部ソースからDockerでモデルを問い合せます。
import requests import gradio as gr import os # Function to interact with the model via API def interact_with_model(prompt): url = 'http://0.0.0.0:8000/v1/chat/completions' # Update the URL to match the correct endpoint headers = { "accept": "application/json", "Content-Type": "application/json", } # Assuming `MODEL` is an environment variable set appropriately model = "/mnt/model/" # Adjust this based on your specific model path or name data = { "model": model, "messages": [{"role": "user", "content": prompt}], "max_tokens": 64, "temperature": 0.7, "top_p": 0.9 } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() completion_text = result["choices"][0]["message"]["content"].strip() return completion_text else: return {"error": f"Request failed with status code {response.status_code}"} # Example Gradio interface iface = gr.Interface( fn=interact_with_model, inputs=gr.Textbox(lines=2, placeholder="Write a humorous limerick about the wonders of GPU computing."), outputs=gr.Textbox(type="text", placeholder="Response..."), title="Model Interface", # Set your desired title here description="Interact with the model deployed locally via Gradio.", live=True ) # Launch the Gradio interface iface.launch(share=True)
ノート: Jupyter Notebookの
8888
ポートを開くファイアウォール・コマンド。sudo firewall-cmd --zone=public --permanent --add-port 8888/tcp sudo firewall-cmd --reload sudo firewall-cmd --list-all
承認
-
著者 - Bogdan Bazarca (シニア・クラウド・エンジニア)
-
コントリビュータ - Oracle NACI-AI-CN-DEVチーム
その他の学習リソース
docs.oracle.com/learnの他のラボを確認するか、Oracle Learning YouTubeチャネルで無料のラーニング・コンテンツにアクセスしてください。また、education.oracle.com/learning-explorerにアクセスしてOracle Learning Explorerになります。
製品ドキュメントは、Oracle Help Centerを参照してください。
Run Mistral LLM Model on OCI Compute A10 Instance with Oracle Resource Manager using One Click Deployment
G11816-01
July 2024