調整開放式模型

本指南說明如何對 Llama 3.1 等開放模型執行監督式微調。本指南涵蓋下列主題:

支援的模型和微調方法

支援的模型

調整方法

調整方法 說明 優點 缺點
完整微調 在訓練期間調整模型的所有參數。 可達到最高品質和效能。 需要更多運算資源、時間和更大的資料集。
低秩調整 (LoRA) 具參數運用效率的調整方法,只會調整模型的一小部分參數。 成本效益更高、所需訓練資料較少,且比完整微調更快。 對於複雜度高的任務,可能無法達到全面微調的品質水準。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  8. 安裝並初始化 Vertex AI SDK for Python
  9. 匯入下列程式庫:
    import os import time import uuid import vertexai  vertexai.init(project=PROJECT_ID, location=REGION)  from google.cloud import aiplatform from vertexai.preview.tuning import sft, SourceModel 
  10. 準備用於調整的資料集

    微調需要訓練資料集。為獲得最佳結果,您也應準備選用的驗證資料集,評估微調模型的成效。

    資料集必須採用 JSON Lines (JSONL) 格式,其中每一行都包含一個微調範例。

  • 回合制對話格式

    {"messages": [   {"content": "You are a chatbot that helps with scientific literature and generates state-of-the-art abstracts from articles.",     "role": "system"},   {"content": "Summarize the paper in one paragraph.",     "role": "user"},   {"content": " Here is a one paragraph summary of the paper:\n\nThe paper describes PaLM, ...",     "role": "assistant"} ]} 

將 JSONL 檔案上傳至 Cloud Storage。

建立微調工作

您可以從下列來源微調模型:

  • 支援的基礎模型,例如 Llama 3.1。如需支援的型號清單,請參閱「支援的型號」。
  • 與支援的基礎模型架構相同的模型。這可以是來自 Hugging Face 等存放區的自訂模型檢查點,也可以是先前透過 Vertex AI 調整工作調整的模型。這樣就能繼續調整先前調整過的模型。

Cloud Console

  1. 您可以透過下列任一方式啟動微調作業:

  2. 設定微調作業,然後按一下「開始微調」

這會啟動微調工作,您可以在「Managed tuning」(管理微調) 分頁下的「Tuning」(微調) 頁面監控這項工作。

調整工作完成後,您可以在「詳細資料」分頁中查看調整後模型的相關資訊。

Python 適用的 Vertex AI SDK

如要建立微調工作,請替換下列程式碼中的預留位置值,然後執行程式碼:

sft_tuning_job = sft.preview_train(     source_model=SourceModel(       base_model="meta/[email protected]",       # Optional, folder that either a custom model checkpoint or previously tuned model       custom_base_model="gs://{STORAGE-URI}",     ),     tuning_mode="FULL", # FULL or PEFT_ADAPTER     epochs=3,     train_dataset="gs://{STORAGE-URI}", # JSONL file     validation_dataset="gs://{STORAGE-URI}", # JSONL file     output_uri="gs://{STORAGE-URI}", ) 

工作完成後,調整後模型的模型構件會儲存在 <output_uri>/postprocess/node-0/checkpoints/final 資料夾中。

部署微調後的模型

您可以將調整過的模型部署至 Vertex AI 端點。您也可以從 Cloud Storage 匯出微調模型,並部署至其他位置。

如要將調整過的模型部署至 Vertex AI 端點,請按照下列步驟操作:

Cloud Console

  1. 前往 Model Garden 頁面,然後按一下「Deploy model with custom weights」(使用自訂權重部署模型)

    前往 Model Garden

  2. 設定部署設定,然後按一下「部署」

Python 適用的 Vertex AI SDK

如要部署模型,請使用 G2 machine預先建構的容器

from vertexai.preview import model_garden  MODEL_ARTIFACTS_STORAGE_URI = "gs://{STORAGE-URI}/postprocess/node-0/checkpoints/final"  model = model_garden.CustomModel(     gcs_uri=MODEL_ARTIFACTS_STORAGE_URI, )  # deploy the model to an endpoint using GPUs. Cost will incur for the deployment endpoint = model.deploy(   machine_type="g2-standard-12",   accelerator_type="NVIDIA_L4",   accelerator_count=1, ) 

取得推論結果

將模型部署至端點後,即可傳送預測要求。前幾項要求的延遲時間可能會較長。

# Loads the deployed endpoint endpoint = aiplatform.Endpoint("projects/{PROJECT_ID}/locations/{REGION}/endpoints/{endpoint_name}")  prompt = "Summarize the following article. Article: Preparing a perfect risotto requires patience and attention to detail. Begin by heating butter in a large, heavy-bottomed pot over medium heat. Add finely chopped onions and minced garlic to the pot, and cook until they're soft and translucent, about 5 minutes. Next, add Arborio rice to the pot and cook, stirring constantly, until the grains are coated with the butter and begin to toast slightly. Pour in a splash of white wine and cook until it's absorbed. From there, gradually add hot chicken or vegetable broth to the rice, stirring frequently, until the risotto is creamy and the rice is tender with a slight bite.. Summary:"  # Define input to the prediction call instances = [     {         "prompt": "What is a car?",         "max_tokens": 200,         "temperature": 1.0,         "top_p": 1.0,         "top_k": 1,         "raw_response": True,     }, ]  # Request the prediction response = endpoint.predict(     instances=instances )  for prediction in response.predictions:     print(prediction) 

如要進一步瞭解如何從已部署的模型取得推論結果,請參閱「取得線上推論結果」。

受管理開放模型會使用 chat.completions 方法,而非已部署模型使用的 predict 方法。如要進一步瞭解如何從代管模型取得推論結果,請參閱「呼叫 Llama 模型」。

限制與配額

Vertex AI 會對並行調整工作的數量強制執行配額。每個專案都有預設配額,至少可執行一項微調作業。這是全域配額,適用於所有可用區域和支援的模型。如要同時執行更多工作,可以申請更多配額Global concurrent managed OSS model fine-tuning jobs per project

定價

系統會根據模型微調的定價,向您收取微調費用。

您也需要支付相關服務的費用,例如 Cloud Storage 和 Vertex AI Prediction。

瞭解 Vertex AI 定價Cloud Storage 定價,並使用 Pricing Calculator 根據您預測的使用情形產生預估費用。

後續步驟