使用 TorchTune 進行微調

使用 Llama 3 進行微調

Francesca Donadoni

Curriculum Manager, DataCamp

TorchTune 微調的組成要素

  • 模型

    • 定義要微調的架構與預訓練權重
    • 提供不同版本與參數規模
  • 資料集

    • 指定用於訓練的資料
  • 配方

    • 中央設定檔,整合模型、資料集與訓練參數
    • 確保一致性與可重現性

模組化的模型

標示的資料集

設定檔與參數

使用 Llama 3 進行微調

TorchTune 微調的組成要素

  • 模型

    • !tune ls
      llama3/8B_full
      llama3_1/8B_full
      llama3_2/1B_full ...
      
  • 資料集

    • ds.save_to_disk("new_dataset")
  • 配方

    • custom_recipe.yaml

模組化的模型

標示的資料集

設定檔與參數

使用 Llama 3 進行微調

TorchTune 配方的組成

  • 一般設定與輸出目錄
    • 批次大小、裝置、訓練回合數

 

  • 模型
    • 指定架構與模型設定

 

  • 最佳化器

    • 包含學習率
  • 資料集

    • 定義前處理與資料集路徑
batch_size: 4
device: cuda
epochs: 20
output_dir: /tmp/full-llama3.2-finetune

model:
  _component_: 
      torchtune.models.llama3_2.llama3_2_1b

optimizer:
  _component_: bitsandbytes.optim.PagedAdamW8bit
  lr: 2.0e-05

dataset:
  _component_: torchtune.datasets.alpaca_dataset
使用 Llama 3 進行微調

設定 TorchTune 配方

  • 還有更多可用參數
  • 可用 Python 透過 yaml 設定
import yaml

config_dict = {"batch_size": 4, "device": "cuda", "model": { "_component_": "torchtune.models.llama3_2.llama3_2_1b" }, ... }
yaml_file_path = "custom_recipe.yaml" with open(yaml_file_path, "w") as yaml_file: yaml.dump(config_dict, yaml_file)
使用 Llama 3 進行微調

執行自訂微調

tune run --config custom_recipe.yaml
INFO:torchtune.utils.logging:Running 
Writing logs to /tmp/full-llama3.2-finetune/log_1732815689.txt
INFO:torchtune.utils.logging:Model is initialized with precision torch.bfloat16.
INFO:torchtune.utils.logging:Tokenizer is initialized from file.
1|52|Loss: 2.3697006702423096:   0%|▏                     | 52/25880
  • 已儲存日誌
  • 成功完成初始化
  • 顯示 epoch 與步數進度
  • 顯示損失指標
使用 Llama 3 進行微調

一起來練習吧!

使用 Llama 3 進行微調

Preparing Video For Download...