使用 Kubeflow Trainer v2 进行 Fine-Tuning

使用 Kubeflow Trainer v2 在 Kubernetes 上通过 LlamaFactory 运行 supervised fine-tuning。

Trainer v2 将作业拆分为可复用的 TrainingRuntime(镜像 + pipeline 步骤 + LlamaFactory 配置)和按实验运行的 TrainJob,后者只覆盖发生变化的部分(模型、数据集、超参数、GPU 资源)。

前提条件

要求详情
Kubeflow Trainer v2可用的 trainer.kubeflow.org API group
Kueue可选;用于作业排队和配额
Shared PVC在所有 training pod 上均可用的 RWX,或已正确配置的 RWO
Git 凭据Secret aml-image-builder-secret,包含 MODEL_REPO_GIT_USERMODEL_REPO_GIT_TOKEN
GPU 节点NVIDIA GPU;根据你的节点调整 nodeSelector
kubectl 访问在你的 namespace 中管理 trainingruntimestrainjobs 的权限

如果遇到 RBAC 错误,请让集群管理员为你的 workbench ServiceAccount 授予在目标 namespace 中对 trainjobstrainingruntimes 的读写权限(示例 role:apiGroups: ["trainer.kubeflow.org"], resources: ["trainjobs","trainingruntimes"])。

构建或使用预构建镜像

可以使用 alaudadockerhub/fine_tune_with_llamafactory:v0.1.11,也可以根据 assets/build-train-image/ 下的 Containerfile 自行构建。

运行示例 notebook

fine-tune-with-trainer-v2.ipynb 下载到你的 workbench 中,并按单元格执行。该 notebook 会创建一个 TrainingRuntime,然后提交一个挂载共享 PVC 并使用 aml-image-builder-secretTrainJob

对于华为 Ascend NPU,请改用 fine-tune-with-trainer-v2-mindspeed-npu.ipynb —— 它会在 huawei.com/Ascend910B4 资源上使用 runtimeClassName: ascend 运行 MindSpeed-LLM SFT pipeline(HF → MCore checkpoint、preprocess、train)。

使用 Kueue 进行调度

当安装了 Kueue 时,TrainJob 会保持 suspended,直到 Kueue 基于已配置的 ClusterQueue quota 接纳它们。可直接应用的 YAML 位于 assets/kueue/

base=https://raw.githubusercontent.com/alauda/aml-docs/master/docs/en/train/guides/assets/kueue
NS=my-namespace  # edit to the namespace where you submit jobs
# 1. Cluster admin — one ResourceFlavor + one ClusterQueue (edit nominalQuota to taste)
kubectl apply -f $base/cluster-queue.yaml
# 2. Namespace admin — LocalQueue pointing at the ClusterQueue
curl -fsSL $base/local-queue.yaml | sed "s/<your-namespace>/$NS/" | kubectl apply -f -
# 3. Submit a TrainJob labelled with the queue name; Kueue admits it
curl -fsSL $base/trainjob-kueue-example.yaml | sed "s/<your-namespace>/$NS/" | kubectl create -f -

这三个文件分别是:

  • cluster-queue.yaml — 一个 ResourceFlavor 加一个覆盖 cpu / memory / nvidia.com/gpuClusterQueue。集群管理员需要在每个 quota pool 上应用一次。
  • local-queue.yaml — 一个命名空间级别的 LocalQueue,引用 cluster-queue。namespace 管理员需要在每个 namespace 上应用一次。
  • trainjob-kueue-example.yaml — 一个带有标签 kueue.x-k8s.io/queue-name: local-queueTrainJob。该 TrainJob 会保持 Suspended,直到 Kueue 接纳它;一旦被接纳,JobSet 就会启动 trainer pod。

完整配置请参见 Kueue docs

NOTE

当 Kueue 的 PodsReady timeout 较短且训练镜像较大时,首次尝试可能会因 image-pull timeout 而被驱逐。重新提交通常会成功,因为镜像已经缓存在节点上。