使用 StarCoder 創建編程助手 – 摘要

概覽

  • Goal: 將 StarCoder(16 B 參數的程式碼生成模型)轉換為聊天式編程助手 – StarChat‑α
  • Key steps:
    1. Add special chat tokens (<|system|>, <|assistant|>, <|user|>, \n).
    2. Prepare a dialogue dataset by filtering the Open‑Assistant oasst1 dataset to English‑only conversations and adding the new tokens.
    3. Mask user labels so the loss is computed only on assistant outputs.
    4. Fine‑tune the model using DeepSpeed ZeRO‑3 (or alternatives like LoRA/FSDP) because the model is too large for a single GPU.

Tokenizer 與遮蔽

special_tokens = {
    "additional_special_tokens": ["<|system|>", "<|assistant|>", "<|user|>", "\n"]
}
tokenizer.add_special_tokens(special_tokens)
  • 驗證 <|assistant|> 映射到單一 token ID (49153)。
  • mask_user_labels 會將使用者回合的 token ID 以及直到下一個助理 token 的所有內容替換為 -100,使其在損失計算中被忽略。

使用 DeepSpeed ZeRO‑3 進行訓練

git clone https://github.com/bigcode-project/starcoder.git
cd starcoder/chat
conda create -n starchat python=3.10 && conda activate starchat
# install pytorch (see pytorch.org) then:
pip install -r requirements.txt
huggingface-cli login
sudo apt-get install git-lfs

torchrun --nproc_per_node=8 train.py config.yaml \
    --deepspeed deepspeed_z3_config_bf16.json
  • 使用 8 GPU A100(80 GB)設定;訓練約在 45 分鐘內完成。
  • config.yaml 定義資料集路徑、模型名稱、訓練超參數等。

範例輸出(編程任務)

提示 模型輸出
Bar plot – 從字典繪製身高與年齡的條形圖 產生完整的 pandas/seaborn 程式碼,包含兩個子圖並顯示圖形。
World map – 將德國與西班牙著色為紅色 產生 geopandas 程式碼,載入 GeoJSON,挑選這兩個國家,並以指定顏色繪製。
Basketball stats – 繪製得分與籃板的散佈圖 返回 seaborn 散佈圖,為每位球員加上註解並提供適當的標題/標籤。

評估

  • 標準基準(ARC、HellaSwag、MMLU、TruthfulQA)在指令微調後顯示出適度提升:
    模型 ARC HellaSwag MMLU TruthfulQA
    StarCoderBase 0.30 0.46 0.33 0.40
    StarChat‑α 0.33 0.49 0.34 0.44
  • Human‑in‑the‑loop / AI‑in‑the‑loop:使用 115 個精選的 Python 提示(來源於 ChatGPT 產生的種子),讓 GPT‑4 與 ChatGPT 以 1‑8 分制評分模型回應。StarChat‑α 約 96 % 時間勝過基礎模型。

限制與風險

  • 仍可能出現幻覺與不安全內容(缺乏 RLHF 或安全過濾)。
  • 人口統計偏見反映了大多數訓練資料來源的 GitHub 社群。
  • 基準測試未能捕捉對話品質;需要人工或 LLM 為基礎的評估。

未來方向

  • 探索 LoRA 或其他 PEFT 方法以降低微調成本。
  • 結合以程式碼為中心的資料與更自然語言的對話,以提升對話能力。
  • 部署為開源助手,供更廣泛社群使用。

資源


引用

@article{Tunstall2023starchat-alpha,
  author = {Tunstall, Lewis and Lambert, Nathan and Rajani, Nazneen and Beeching, Edward and Le Scao, Teven and von Werra, Leandro and Han, Sheon and Schmid, Philipp and Rush, Alexander},
  title = {Creating a Coding Assistant with StarCoder},
  journal = {Hugging Face Blog},
  year = {2023},
  note = {https://huggingface.co/blog/starchat-alpha},
}

Sources