在 Google Cloud Vertex AI 上部署 Meta Llama 3.1 405B

釋出日期:2024 年 8 月 19 日
在 GitHub 上更新

Meta Llama 3.1 是 Meta 於 2024 年 7 月釋出的最新開源 LLM。Meta Llama 3.1 有三種大小:8B 用於在消費級 GPU 上高效部署和開發,70B 用於大規模 AI 原生應用,以及 405B 用於合成數據、作為法官的 LLM 或蒸餾等用例。其主要功能包括:128K token 的大上下文長度(與原始的 8K 相比),多語言能力,工具使用能力,以及更寬鬆的許可。

在本部落格中,你將學習如何在 Google Cloud Vertex AI 的 Google Cloud A3 節點上以程式設計方式部署 meta-llama/Meta-Llama-3.1-405B-Instruct-FP8,即 meta-llama/Meta-Llama-3.1-405B-Instruct 的 FP8 量化變體,該節點配備 8 個 H100 NVIDIA GPU,並使用 文字生成推理 (TGI) 和 Hugging Face 專門為 Google Cloud 構建的深度學習容器 (DLC)。

另外,你也可以直接從 HubVertex 模型花園 部署 meta-llama/Meta-Llama-3.1-405B-Instruct-FP8,無需編寫任何程式碼!

本部落格將涵蓋

Vertex AI 簡介

  1. Google Cloud 上 Meta Llama 3.1 模型的要求
  2. 為 Vertex AI 設定 Google Cloud
  3. 在 Vertex AI 上註冊 Meta Llama 3.1 405B 模型
  4. 在 Vertex AI 上部署 Meta Llama 3.1 405B
  5. 使用 Meta Llama 3.1 405B 執行線上預測
    1. 透過 Python
      1. 在同一會話中
      2. 從不同會話中
    2. 透過 Vertex AI 線上預測使用者介面
  6. 清理資源

結論

讓我們開始吧!🚀 或者,你也可以從 此 Jupyter Notebook 開始學習。

Vertex AI 簡介

Vertex AI 是一個機器學習 (ML) 平臺,可讓你訓練和部署 ML 模型和 AI 應用,並自定義大型語言模型 (LLM) 以用於你的 AI 驅動應用。Vertex AI 結合了資料工程、資料科學和 ML 工程工作流程,使你的團隊能夠使用通用工具集進行協作,並利用 Google Cloud 的優勢擴充套件你的應用。

本部落格將重點介紹如何從 Hugging Face Hub 部署一個已微調的模型,使用預構建的容器獲取即時線上預測。因此,我們將演示 Vertex AI 在推理方面的使用。

更多資訊請參見 Vertex AI - 文件 - Vertex AI 簡介

1. Google Cloud 上 Meta Llama 3.1 模型的要求

Meta Llama 3.1 帶來了令人興奮的進步。然而,執行這些模型需要仔細考慮你的硬體資源。對於推理,記憶體需求取決於模型大小和權重精度。下表顯示了不同配置所需的近似記憶體:

模型大小 FP16 FP8 INT4
8B 16 GB 8 GB 4 GB
70B 140 GB 70 GB 35 GB
405B 810 GB 405 GB 203 GB

注意:上述引用數字表示僅載入模型檢查點所需的 GPU 視訊記憶體。它們不包括用於核心或 CUDA 圖的 torch 保留空間。

例如,一個 H100 節點(8 個 H100,每個 80GB)總共有約 640GB 的視訊記憶體,因此 405B 模型需要在多節點設定中執行,或者以較低精度(例如 FP8)執行,這將是推薦的方法。有關更多資訊,請參閱 Hugging Face 部落格上的 Meta Llama 3.1

Google Cloud 中的 A3 加速器最佳化機器系列配備 8 個 H100 80GB NVIDIA GPU、208 個 vCPU 和 1872 GB 記憶體。該機器系列針對計算和記憶體密集型、網路受限的 ML 訓練和 HPC 工作負載進行了最佳化。有關 A3 機器可用性公告的更多資訊,請參閱 推出專為 AI 構建的配備 NVIDIA H100 GPU 的 A3 超級計算機,有關 A3 機器系列的更多資訊,請參閱 Compute Engine - 加速器最佳化機器系列

即使 A3 機器在 Google Cloud 中可用,你仍然需要請求自定義配額增加,因為這些需要特定批准。請注意,A3 機器僅在某些區域可用,因此請務必在 Compute Engine - GPU 區域和可用區 中檢查 A3 High 或 A3 Mega 在每個區域的可用性。

在這種情況下,要請求增加配額以使用 A3 High GPU 機器型別,你需要增加以下配額:

  • Service: Vertex AI APIName: Custom model serving Nvidia H100 80GB GPUs per region 設定為 8
  • Service: Vertex AI APIName: Custom model serving A3 CPUs per region 設定為 208

A3 Quota Request in Google Cloud

有關如何請求配額增加的更多資訊,請參閱 Google Cloud 文件 - 檢視和管理配額

2. 為 Vertex AI 設定 Google Cloud

在繼續之前,我們將為方便起見設定以下環境變數

%env PROJECT_ID=your-project-id
%env LOCATION=your-region

首先,你需要根據 Cloud SDK - 安裝 gcloud CLI 中的說明在你的機器上安裝 gcloud;並登入你的 Google Cloud 賬戶,設定你的專案和首選的 Google Compute Engine 區域。

gcloud auth login
gcloud config set project $PROJECT_ID
gcloud config set compute/region $LOCATION

安裝 Google Cloud SDK 後,你需要啟用使用其 Artifact Registry for Docker 中的深度學習容器 (DLC) 來使用 Vertex AI 所需的 Google Cloud API。

gcloud services enable aiplatform.googleapis.com
gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com
gcloud services enable containerregistry.googleapis.com
gcloud services enable containerfilesystem.googleapis.com

然後,你還需要安裝 google-cloud-aiplatform,這是從 Python 以程式設計方式與 Google Cloud Vertex AI 互動所必需的。

pip install --upgrade --quiet google-cloud-aiplatform

然後透過 Python 如下初始化它

import os
from google.cloud import aiplatform

aiplatform.init(project=os.getenv("PROJECT_ID"), location=os.getenv("LOCATION"))

最後,由於 Meta Llama 3.1 模型在 Hugging Face Hub 的 meta-llama 組織 下受限,你需要請求訪問並等待批准,這通常不會超過 24 小時。然後,你需要安裝 huggingface_hub Python SDK 以使用 huggingface-cli 登入 Hugging Face Hub 以下載這些模型。

pip install --upgrade --quiet huggingface_hub

或者,你也可以跳過 huggingface_hub 的安裝,直接生成一個 Hugging Face 細粒度令牌,該令牌對受限倉庫 meta-llama/Meta-Llama-3.1-405B-Instruct-FP8meta-llama 組織 下的任何其他模型具有隻讀許可權,選擇例如 倉庫許可權 -> meta-llama/Meta-Llama-3.1-405B-Instruct-FP8 -> 對所選倉庫內容的讀取許可權。然後將該令牌設定為 HF_TOKEN 環境變數,或者直接手動提供給 notebook_login 方法,如下所示

from huggingface_hub import notebook_login

notebook_login()

3. 在 Vertex AI 上註冊 Meta Llama 3.1 405B 模型

要在 Vertex AI 上註冊 Meta Llama 3.1 405B 模型,你需要使用 google-cloud-aiplatform Python SDK。但在繼續之前,你需要首先定義要使用的 DLC,在這種情況下,它將是 Hugging Face TGI 最新版 GPU DLC。

截至目前(2024 年 8 月),最新的 Hugging Face TGI DLC,即 us-docker.pkg.dev/deeplearning-platform-release/gcr.io/huggingface-text-generation-inference-cu121.2-2.ubuntu2204.py310 使用 TGI v2.2。此版本支援 Meta Llama 3.1 架構,該架構需要與之前的 Meta Llama 3 不同的 RoPE 縮放方法。

要檢查 Google Cloud 中有哪些 Hugging Face DLC 可用,你可以導航到 Google Cloud Artifact Registry 並按“huggingface-text-generation-inference”過濾,或者使用以下 gcloud 命令

gcloud container images list --repository="us-docker.pkg.dev/deeplearning-platform-release/gcr.io" | grep "huggingface-text-generation-inference"

然後,你需要為容器定義配置,即 text-generation-launcher 預期作為引數的環境變數(根據 官方文件),在這種情況下,如下所示:

  • MODEL_ID Hugging Face Hub 上的模型 ID,即 meta-llama/Meta-Llama-3.1-405B-Instruct-FP8
  • HUGGING_FACE_HUB_TOKEN 對受限倉庫 meta-llama/Meta-Llama-3.1-405B-Instruct-FP8 的只讀訪問令牌,下載 Hugging Face Hub 中的權重所需。
  • NUM_SHARD 要使用的分片數,即要使用的 GPU 數量,在這種情況下設定為 8,因為將使用具有 8 個 H100 NVIDIA GPU 的 A3 例項。

此外,作為建議,您還應該定義 HF_HUB_ENABLE_HF_TRANSFER=1 以透過 hf_transfer 工具實現更快的下載速度,因為 Meta Llama 3.1 405B 大約 400 GiB,否則下載權重可能需要更長時間。

然後,您可以透過 google-cloud-aiplatform Python SDK 在 Vertex AI 的模型登錄檔中註冊模型,如下所示:

from huggingface_hub import get_token

model = aiplatform.Model.upload(
    display_name="meta-llama--Meta-Llama-3.1-405B-Instruct-FP8",
    serving_container_image_uri="us-docker.pkg.dev/deeplearning-platform-release/gcr.io/huggingface-text-generation-inference-cu121.2-2.ubuntu2204.py310",
    serving_container_environment_variables={
        "MODEL_ID": "meta-llama/Meta-Llama-3.1-405B-Instruct-FP8",
        "HUGGING_FACE_HUB_TOKEN": get_token(),
        "HF_HUB_ENABLE_HF_TRANSFER": "1",
        "NUM_SHARD": "8",
    },
)
model.wait()

Meta Llama 3.1 405B FP8 registered on Vertex AI

4. 在 Vertex AI 上部署 Meta Llama 3.1 405B

Meta Llama 3.1 405B 在 Vertex AI 模型登錄檔中註冊後,您可以建立 Vertex AI 端點並將模型部署到該端點,並使用 Hugging Face TGI 的 DLC 作為服務容器。

如前所述,由於 FP8 中的 Meta Llama 3.1 405B 佔用約 400 GiB 的磁碟空間,這意味著我們需要至少 400 GiB 的 GPU VRAM 來載入模型,並且節點內的 GPU 需要支援 FP8 資料型別。在這種情況下,將使用一臺配備 8 個 NVIDIA H100 80GB 的 A3 例項,總共約 640 GiB VRAM,以載入模型,同時為 KV 快取和 CUDA 圖形留出一些空閒 VRAM。

endpoint = aiplatform.Endpoint.create(display_name="Meta-Llama-3.1-405B-FP8-Endpoint")

deployed_model = model.deploy(
    endpoint=endpoint,
    machine_type="a3-highgpu-8g",
    accelerator_type="NVIDIA_H100_80GB",
    accelerator_count=8,
)

請注意,在 Vertex AI 上部署 Meta Llama 3.1 405B 可能需要大約 25-30 分鐘,因為它需要分配 Google Cloud 上的資源、從 Hugging Face Hub 下載權重(約 10 分鐘)以及將其載入到 TGI 中進行推理(約 2 分鐘)。

Meta Llama 3.1 405B Instruct FP8 deployed on Vertex AI

恭喜,您已成功在 Google Cloud 賬戶中部署了 Meta Llama 3.1 405B!🔥 現在是時候對模型進行測試了。

5. 使用 Meta Llama 3.1 405B 執行線上預測

Vertex AI 將在 /predict 路由中公開一個線上預測端點,該端點正在提供來自文字生成推理 (TGI) DLC 的文字生成,確保 I/O 資料符合 Vertex AI 有效負載(有關 Vertex AI I/O 有效負載的更多資訊,請參閱 Vertex AI 文件 - 從自定義訓練模型獲取線上預測)。

由於公開的端點是 /generate,在向 Vertex AI 傳送請求之前,你需要使用聊天模板格式化訊息,因此建議安裝 🤗transformers 以使用 PreTrainedTokenizerFast 分詞器例項的 apply_chat_template 方法。

pip install --upgrade --quiet transformers

然後使用分詞器將聊天模板應用於對話,如下所示

import os
from huggingface_hub import get_token
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(
    "meta-llama/Meta-Llama-3.1-405B-Instruct-FP8",
    token=get_token(),
)

messages = [
    {"role": "system", "content": "You are an assistant that responds as a pirate."},
    {"role": "user", "content": "What's the Theory of Relativity?"},
]
inputs = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
)

現在你已經從初始對話訊息中得到一個字串,使用 Meta Llama 3.1 的預設聊天模板進行了格式化

<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou are an assistant that responds as a pirate.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nWhat's the Theory of Relativity?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n

這就是你將在有效負載中傳送到已部署的 Vertex AI 端點的內容,以及生成引數,如 使用文字生成推理 (TGI) -> 生成 中所述。

5.1 透過 Python

5.1.1 在同一會話中

如果你想在當前會話中(即用於部署模型的會話)執行線上預測,你可以透過 aiplatform.Model.deploy 方法返回的 aiplatform.Endpoint 以程式設計方式傳送請求,如下面的程式碼片段所示。

output = deployed_model.predict(
    instances=[
        {
            "inputs": inputs,
            "parameters": {
                "max_new_tokens": 128,
                "do_sample": True,
                "top_p": 0.95,
                "temperature": 0.7,
            },
        },
    ]
)

生成以下 output

預測(預測結果=[“你想了解那些花哨的科學玩意兒,嗯?那好,夥伴,喝杯朗姆酒坐下來,仔細聽著。我要告訴你的是那個大膽的天才阿爾伯特·愛因斯坦提出的相對論。\n\n你看,愛因斯坦說時間和空間就像大海和風一樣相互關聯。你不能只有其中一個而沒有另一個,明白嗎?他還提出,你如何看待時間和空間取決於你移動的速度以及你所處的位置。這就叫做相對論,我的”], deployed_model_id='', metadata=None, model_version_id='1', model_resource_name='projects//locations//models/', explanations=None)

5.1.2 從不同會話中

如果 Vertex AI 端點是在不同會話中部署的,而您只是想使用它,但無法訪問 aiplatform.Model.deploy 方法返回的 deployed_model 變數,那麼您也可以執行以下程式碼片段來透過其資源名稱例項化已部署的 aiplatform.Endpoint,該資源名稱可以在 Vertex AI 線上預測 UI 中找到,也可以從上面例項化的 aiplatform.Endpoint 中獲取,或者直接替換 projects/{PROJECT_ID}/locations/{LOCATION}/endpoints/{ENDPOINT_ID} 中的值。

import os
from google.cloud import aiplatform

aiplatform.init(project=os.getenv("PROJECT_ID"), location=os.getenv("LOCATION"))

endpoint = aiplatform.Endpoint(f"projects/{os.getenv('PROJECT_ID')}/locations/{os.getenv('LOCATION')}/endpoints/{ENDPOINT_ID}")
output = endpoint.predict(
    instances=[
        {
            "inputs": inputs,
            "parameters": {
                "max_new_tokens": 128,
                "do_sample": True,
                "top_p": 0.95,
                "temperature": 0.7,
            },
        },
    ],
)

生成以下 output

預測 (predictions=["你正在尋找關於那些高階物理學的寶藏知識,嗯?好的,夥伴,坐下來,喝杯朗姆酒,仔細聽著,我將為你講述愛因斯坦的相對論的故事。\n\n它分為兩部分,我的朋友:狹義相對論和廣義相對論。現在,我知道你在想什麼:有什麼區別呢?好吧,夥伴,讓我為你分解一下。\n\n狹義相對論是這樣一種觀點:時間和空間像大海和天空一樣相互連線。"], deployed_model_id='', metadata=None, model_version_id='1', model_resource_name='projects//locations//models/', explanations=None)

5.2 透過 Vertex AI 線上預測使用者介面

或者,出於測試目的,你也可以使用 Vertex AI 線上預測使用者介面,該介面提供了一個欄位,用於輸入根據 Vertex AI 規範(如上例所示)格式化的 JSON 有效負載:

{
    "instances": [
        {
            "inputs": "<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou are an assistant that responds as a pirate.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nWhat's the Theory of Relativity?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n",
            "parameters": {
                "max_new_tokens": 128,
                "do_sample": true,
                "top_p": 0.95,
                "temperature": 0.7
            }
        }
    ]
}

這樣就可以在使用者介面中生成並列印輸出。

Meta Llama 3.1 405B Instruct FP8 online prediction on Vertex AI

6. 清理資源

完成後,您可以按如下方式釋放您建立的資源,以避免不必要的成本。

  • deployed_model.undeploy_all 用於從所有端點取消部署模型。
  • deployed_model.delete 用於在 undeploy_all 方法後,從部署模型的端點中優雅地刪除模型。
  • model.delete 用於從登錄檔中刪除模型。
deployed_model.undeploy_all()
deployed_model.delete()
model.delete()

或者,你也可以按照以下步驟從 Google Cloud Console 中刪除這些資源:

  • 前往 Google Cloud 中的 Vertex AI
  • 前往“部署和使用”->“線上預測”
  • 點選端點,然後點選已部署的模型以“從端點取消部署模型”
  • 然後返回端點列表並刪除該端點
  • 最後,前往“部署和使用”->“模型登錄檔”,然後刪除模型

結論

就是這樣!您已成功在 Google Cloud Vertex AI 上註冊並部署了 Meta Llama 3.1 405B Instruct FP8,然後透過程式設計方式和 Google Cloud 控制檯運行了線上預測,最後清理了使用的資源以避免不必要的成本。

多虧了 Hugging Face 的 Text Generation Inference (TGI) DLC 和 Google Cloud Vertex AI,部署用於服務大型語言模型 (LLM) 的高效能文字生成容器從未如此簡單。我們不會止步於此——敬請期待,我們將在 Google Cloud 上實現更多使用開放模型構建 AI 的體驗!

社群

註冊登入 發表評論

© . This site is unofficial and not affiliated with Hugging Face, Inc.