附註:
- 此教學課程需要存取 Oracle Cloud。若要註冊免費帳戶,請參閱開始使用 Oracle Cloud Infrastructure Free Tier 。
- 它使用 Oracle Cloud Infrastructure 憑證、租用戶及區間的範例值。完成實驗室時,請將這些值取代為您雲端環境特有的值。
Run Elyza LLM Model on OCI Compute A10.2 Instance with Oracle Resource Manager using One Click Deployment
簡介
Oracle Cloud Infrastructure (OCI) Compute 可讓您建立不同類型的資源配置,以測試本機部署人工智慧 (AI) 模型的圖形處理單元 (GPU)。在本教學課程中,我們將使用 A10.2 資源配置搭配預先存在的 VCN 和子網路資源,供您從 Oracle Resource Manager 選取。
Terraform 程式碼也包含將執行處理設定為執行本機虛擬大型語言模型 (vLLM) Elyza 模型 (用於自然語言處理工作)。
目標
- 在 OCI Compute 上建立 A10.2 資源配置,下載 Elyza LLM 模型並查詢本機 vLLM 模型。
必要條件
-
請確定您的 OCI 虛擬雲端網路 (VCN) 和將部署虛擬機器 (VM) 的子網路。
-
瞭解網路元件及其關係。如需詳細資訊,請參閱 Networking Overview 。
-
瞭解雲端中的網路。如需詳細資訊,請觀看下列影片:雲端 EP.01 中網路的影片:虛擬雲端網路。
-
需求:
- 執行處理類型:含有兩個 Nvidia GPU 的 A10.2 資源配置。
- 作業系統:Oracle Linux。
- 映像檔選擇:部署命令檔會選取支援 GPU 的最新 Oracle Linux 映像檔。
- 標記:新增自由格式標記 GPU_TAG = "A10-2"。
- 啟動磁碟區大小: 250GB。
- 初始化:使用 cloud-init 來下載與設定 vLLM Elyza 模型。
作業 1:下載單鍵部署的 Terraform 代碼
從此處下載 ORM Terraform 程式碼: orm_stack_a10_2_gpu_elyza_models.zip ,以在本機實行 Elyza vLLM 模型,這可讓您選取現有的 VCN 和子網路,以測試 A10.2 執行處理資源配置中 Elyza vLLM 模型的本機部署。
在本機下載 ORM Terraform 程式碼之後,請依照下列步驟進行:從資料夾建立堆疊以上傳堆疊並執行 Terraform 程式碼的套用。
注意: 請確定您已建立 OCI 虛擬雲端網路 (VCN) 和將部署 VM 的子網路。
作業 2:在 OCI 上建立 VCN (若尚未建立則為選擇性)
若要在 Oracle Cloud Infrastructure 中建立 VCN,請參閱:瞭解如何在 OCI 上建立虛擬雲端網路的影片。
或
若要建立 VCN,請執行下列步驟:
-
登入 OCI 主控台,輸入雲端用戶名稱、使用者名稱和密碼。
-
按一下左上角的漢堡選單 (按)。
-
請前往網路、虛擬雲端網路,然後從清單範圍區段選取適當的區間。
-
選取具備網際網路連線的 VCN ,然後按一下啟動 VCN 精靈。
-
在使用網際網路連線建立 VCN 頁面中,輸入下列資訊,然後按一下下一步。
- VCN 名稱:輸入
OCI_HOL_VCN
。 - COMPARTMENT:選取適當的區間。
- 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 之後,請按一下檢視虛擬雲端網路。
在現實世界中,您將根據對存取 (要開啟哪些連接埠) 的需求和可存取的人員來建立多個 VCN。
工作 3:查看 cloud-init 組態詳細資訊
cloud-init
命令檔會安裝所有必要的相依性、啟動 Docker、下載並啟動 vLLM Elyza 模型。您可以在任務 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 將下載執行 Elyza 模型所需的所有檔案,不需要在 Hugging Face 中預先定義的 API 權杖。在 Task 6 中使用 Docker 啟動 Elyza 模型時,將需要 API 記號。
作業 4:監督系統
使用下列命令 (如有需要) 追蹤 cloud-init Completion 和 GPU 資源使用狀況。
-
監控 Cloud-init 完成度:
tail -f /var/log/cloud-init-output.log
。 -
監控 GPU 使用率:
nvidia-smi dmon -s mu -c 100 --id 0,1
。 -
使用 Python 部署與與 vLLM Elyza 模型互動: (請視需要變更參數 - 指令已包含在
cloud-init
指令碼中):python -O -u -m vllm.entrypoints.api_server \ --host 0.0.0.0 \ --port 8000 \ --model /home/opc/models/${MODEL} \ --tokenizer hf-internal-testing/llama-tokenizer \ --enforce-eager \ --max-num-seqs 1 \ --tensor-parallel-size 2 \ >> /home/opc/${MODEL}.log 2>&1
作業 5:測試模型整合
使用命令或 Jupyter Notebook 詳細資訊,以下列方式與模型互動。
-
在完成 Cloud-init 之後,從命令行介面 (CLI) 測試模型。
curl -X POST "http://0.0.0.0:8000/generate" \ -H "accept: application/json" \ -H "Content-Type: application/json" \ -d '{"prompt": "Write a humorous limerick about the wonders of GPU computing.", "max_tokens": 64, "temperature": 0.7, "top_p": 0.9}'
-
測試 Jupyter Notebook 的模型 (確定開啟連接埠
8888
) 。import requests import json url = "http://0.0.0.0:8000/generate" headers = { "accept": "application/json", "Content-Type": "application/json", } data = { "prompt": "Write a short conclusion.", "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() # 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 # Function to interact with the model via API def interact_with_model(prompt): url = 'http://0.0.0.0:8000/generate' headers = { "accept": "application/json", "Content-Type": "application/json", } data = { "prompt": 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["text"][0].strip() # Extract the generated text return completion_text else: return {"error": f"Request failed with status code {response.status_code}"} # Retrieve the MODEL environment variable model_name = os.getenv("MODEL") # 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=f"{model_name} Interface", # Use model_name to dynamically set the title description=f"Interact with the {model_name} deployed locally via Gradio.", # Use model_name to dynamically set the description live=True ) # Launch the Gradio interface iface.launch(share=True)
工作 6:使用 Docker 部署模型 (如有需要)
或者,使用 Docker 部署封裝環境模型:
-
來自外部來源的模型。
docker run --gpus all \ --env "HUGGING_FACE_HUB_TOKEN=$TOKEN_ACCESS" \ -p 8000:8000 \ --ipc=host \ --restart always \ vllm/vllm-openai:latest \ --tensor-parallel-size 2 \ --model elyza/$MODEL
-
使用 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/" \ --tensor-parallel-size 2
您可以使用下列方式查詢模型:
-
查詢使用 CLI 的 Docker 啟動的模型 (需要進一步注意):
-
從外部來源開始使用 Docker 的模型。
(elyza) [opc@a10-2-gpu ~]$ curl -X 'POST' 'http://0.0.0.0:8000/v1/chat/completions' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "model": "elyza/'${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 }'
-
使用 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 筆記型電腦查詢 Docker 啟動的模型。
-
從 Docker Hub 啟動的模型。
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"elyza/{os.getenv('MODEL')}" 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)
-
容器從本機開始使用 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)
-
-
使用與聊天機器人整合的 Gradio 查詢 Docker 模型。
-
從外部來源開始使用 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 = f"elyza/{os.getenv('MODEL')}" data = { "model": model, "messages": [{"role": "user", "content": prompt}], # Use the user-provided 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() # Extract the generated text return completion_text else: return {"error": f"Request failed with status code {response.status_code}"} # Retrieve the MODEL environment variable model_name = os.getenv("MODEL") # 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=f"{model_name} Interface", # Use model_name to dynamically set the title description=f"Interact with the {model_name} model deployed locally via Gradio.", # Use model_name to dynamically set the description live=True ) # Launch the Gradio interface iface.launch(share=True)
-
使用 Gradio 使用 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 Elyza LLM Model on OCI Compute A10.2 Instance with Oracle Resource Manager using One Click Deployment
G11831-01
July 2024