附註:

透過 dstack 在 Oracle Cloud Infrastructure 上微調及提供大型語言模型

簡介

dstack 是一種開放原始碼工具,可簡化人工智慧 (AI) 容器協調流程,讓大型語言模型 (LLM) 的分散式訓練與部署更容易存取。結合 dstack 和 Oracle Cloud Infrastructure (OCI) 可解鎖簡化流程,設定用於分散式訓練和可擴展模型部署的雲端基礎架構。

dstack 的運作方式為何?

dstack 提供統一的介面,可跨任何雲端或資料中心開發、訓練及部署 AI 模型。例如,您可以指定訓練工作或要部署之模型的組態,而 dstack 則會負責設定必要的基礎架構及協調容器。dstack 提供的優點之一就是可以運用任何硬體、架構和命令檔。

目標

必要條件

工作 1:使用 OCI 設定 dstack

  1. 使用下列指令安裝 dstack Python 套裝軟體。由於 dstack 支援多個雲端提供者,因此我們可以將範圍縮小為 OCI。

    pip install dstack[oci]
    
  2. ~/.dstack/server/config.yml 檔案內設定 OCI 特定證明資料。下列程式碼假設您已設定 OCI 命令行介面 (CLI) 的證明資料。如需其他配置選項的資訊,請參閱 dstack documentation

    projects:
    - name: main
      backends:
      - type: oci
        creds:
          type: default
    
  3. 使用下列指令執行 dstack 伺服器。

    dstack server
    INFO     Applying ~/.dstack/server/config.yml...
    INFO     Configured the main project in ~/.dstack/config.yml
    INFO     The admin token is ab6e8759-9cd9-4e84-8d47-5b94ac877ebf
    INFO     The dstack server 0.18.4 is running at http://127.0.0.1:3000
    
  4. 使用您的專案命令檔切換至資料夾,並使用下列命令起始 dstack

    dstack init
    

工作 2:使用 dstack 對 OCI 進行微調工作

為了微調 Gemma 7B 模型,我們將使用 Hugging Face Alignment 手冊確保註冊成立最佳微調做法。本教學課程的原始程式碼可從以下網址取得:GitHub 。讓我們深入實用的步驟來微調您的 LLM。

一旦您切換至專案資料夾,請使用下列命令起始 OCI 上的微調工作 (使用 dstack)。

ACCEL_CONFIG_PATH=fsdp_qlora_full_shard.yaml \
  FT_MODEL_CONFIG_PATH=qlora_finetune_config.yaml \
  HUGGING_FACE_HUB_TOKEN=xxxx \
  WANDB_API_KEY=xxxx \
  dstack run . -f ft.task.dstack.yml

FT_MODEL_CONFIG_PATHACCEL_CONFIG_PATHHUGGING_FACE_HUB_TOKEN 以及 WANDB_API_KEY 環境變數定義在 ft.task.dstack.yml 工作組態中。dstack run 會送出 OCI 上 ft.task.dstack.yml 中定義的工作。

注意:執行工作時,dstack 會自動複製目前的目錄內容。

讓我們探索每個 YAML 檔案的主要部分 (如需完整內容,請查看儲存庫)。

qlora_finetune_config.yaml 檔案是「對齊手冊」用來瞭解如何微調大型語言模型 (LLM) 的處方組態。

# Model arguments
model_name_or_path: google/gemma-7b
tokenizer_name_or_path: philschmid/gemma-tokenizer-chatml
torch_dtype: bfloat16
bnb_4bit_quant_storage: bfloat16

# LoRA arguments
load_in_4bit: true
use_peft: true
lora_r: 8
lora_alpha: 16
lora_dropout: 0.05
lora_target_modules:
  - q_proj
  - k_proj
# ...

# Data training arguments
dataset_mixer:
  chansung/mental_health_counseling_conversations: 1.0
dataset_splits:
  - train
  - test
# ...

fsdp_qlora_full_shard.yaml 檔案設定可加速如何使用基礎基礎架構來微調 LLM。

compute_environment: LOCAL_MACHINE
distributed_type: FSDP  # Use Fully Sharded Data Parallelism
fsdp_config:
  fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP
  fsdp_backward_prefetch: BACKWARD_PRE
  fsdp_cpu_ram_efficient_loading: true
  fsdp_use_orig_params: false
  fsdp_offload_params: true
  fsdp_sharding_strategy: FULL_SHARD
  # ... (other FSDP configurations)
# ... (other configurations)

混合分區

使用 distributed_typeFSDPfsdp_configfsdp_sharding_strategyFULL_SHARD,模型將在單一機器中跨多個 GPU 分區。處理多個運算節點時,每個節點都會代管相同的模型複本,而此複本本身會分割到該節點內的多個 GPU。這表示每個節點上的每個分割模型實例都會處理您資料集的不同區段或批次。若要將單一模型分散至跨多個節點的多個 GPU,請將 fsdp_sharding_strategy 參數設定為 HYBRID_SHARD

其他參數 (例如 machine_ranknum_machinesnum_processes) 對協調非常重要。不過,建議您在程式實際執行時動態設定這些值,因為這可在不同基礎架構設定之間切換時提供彈性。

作業 3:使用簡化的組態來啟動設定基礎架構,指示 dstack

讓我們探索將所有項目放在一起的 fsdp_qlora_full_shard.yaml 組態,並指示 dstack 如何佈建基礎架構及執行作業。

type: task
nodes: 3

python: "3.11"
env:
  - ACCEL_CONFIG_PATH
  - FT_MODEL_CONFIG_PATH
  - HUGGING_FACE_HUB_TOKEN
  - WANDB_API_KEY
commands:
  # ... (setup steps, cloning repo, installing requirements)
  - ACCELERATE_LOG_LEVEL=info accelerate launch \
      --config_file recipes/custom/accel_config.yaml \
      --main_process_ip=$DSTACK_MASTER_NODE_IP \
      --main_process_port=8008 \
      --machine_rank=$DSTACK_NODE_RANK \
      --num_processes=$DSTACK_GPUS_NUM \
      --num_machines=$DSTACK_NODES_NUM \
      scripts/run_sft.py recipes/custom/config.yaml
ports:
  - 6006
resources:
  gpu: 1..2
  shm_size: 24GB

要強調的關鍵點::

工作 4:使用 dstack 提供微調模型服務

細微調整模型之後,dstack 可讓您使用 Hugging Face Text Generation Inference (TGI) 架構在 OCI 上部署模型。

以下是如何在 dstack 中定義服務的範例:

type: service
image: ghcr.io/huggingface/text-generation-inference:latest
env:
  - HUGGING_FACE_HUB_TOKEN
  - MODEL_ID=chansung/mental_health_counseling_merged_v0.1
commands:
  - text-generation-launcher \
    --max-input-tokens 512 --max-total-tokens 1024 \
    --max-batch-prefill-tokens 512 --port 8000
port: 8000

resources:
  gpu:
    memory: 48GB

# (Optional) Enable the OpenAI-compatible endpoint
model:
  format: tgi
  type: chat
  name: chansung/mental_health_counseling_merged_v0.1

此方法的主要優點:

此時,您可以使用標準 curl 指令和 Python 要求、OpenAI SDK 和 Hugging Face 的 Inference Client 程式庫與服務互動。例如,下列程式碼片段會顯示 Curl 的範例。

curl -X POST https://black-octopus-1.mycustomdomain.com/generate \
  -H "Authorization: Bearer <dstack-token>" \
  -H 'Content-Type: application/json' \
  -d '{"inputs": "I feel bad...", "parameters": {"max_new_tokens": 128}}'

此外,對於已部署的模型,dstack 會自動提供使用者介面,以直接與模型互動。

使用者介面

接下來的步驟

遵循本教學課程中概述的步驟,您已解鎖強大的方法,使用 dstack、OCI 和 Hugging Face 生態系統的結合功能來微調和部署 LLM。您現在可以運用 dstack 的使用者易記介面有效管理 OCI 資源,簡化為 LLM 專案設定分散式訓練環境的程序。

此外,與 Hugging Face 的對齊手冊和 TGI 架構整合,可讓您順暢地微調和服務模型,確保模型針對效能進行最佳化,並準備好進行實際的應用系統。我們鼓勵您進一步探索可能性,並對不同的模型和組態進行實驗,以實現自然語言處理世界的期望結果。

認可

其他學習資源

瀏覽 docs.oracle.com/learn 的其他實驗室,或前往 Oracle Learning YouTube 頻道存取更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。

如需產品文件,請造訪 Oracle Help Center