使用 Unsloth 和 Hugging Face Jobs 免费训练 AI 模型

Hugging Face Blog 产品

摘要

Hugging Face 和 Unsloth 提供免费额度和训练资源,让开发者可以通过 Hugging Face Jobs 微调 AI 模型。开发者可以训练小型语言模型(如 LFM2.5-1.2B-Instruct),实现 2 倍更快的训练速度和 60% 更低的 VRAM 占用,并可配合 Claude Code 和 Codex 等编程代理使用。

暂无内容
查看原文
查看缓存全文

缓存时间: 2026/04/20 17:27

使用 Unsloth 和 Hugging Face Jobs 免费训练 AI 模型

来源:https://huggingface.co/blog/unsloth-jobs 返回文章 (https://huggingface.co/blog)

  • 你需要什么 (https://huggingface.co/blog/unsloth-jobs#you-will-need)
  • 运行任务 (https://huggingface.co/blog/unsloth-jobs#run-the-job)
  • 安装技能 (https://huggingface.co/blog/unsloth-jobs#installing-the-skill)- Claude Code (https://huggingface.co/blog/unsloth-jobs#claude-code) - Codex (https://huggingface.co/blog/unsloth-jobs#codex) - 其他方式 (https://huggingface.co/blog/unsloth-jobs#anything-else)
  • 快速开始 (https://huggingface.co/blog/unsloth-jobs#quick-start)
  • 工作原理 (https://huggingface.co/blog/unsloth-jobs#how-it-works)- 示例训练脚本 (https://huggingface.co/blog/unsloth-jobs#example-training-script)
  • 与编码代理合作的技巧 (https://huggingface.co/blog/unsloth-jobs#tips-for-working-with-coding-agents)
  • 资源 (https://huggingface.co/blog/unsloth-jobs#resources)

本博文介绍了如何使用 Unsloth (https://github.com/unslothai/unsloth) 和 Hugging Face Jobs 通过 Claude Code 和 Codex 等编码代理进行快速 LLM 微调(特别是 LiquidAI/LFM2.5-1.2B-Instruct (https://huggingface.co/LiquidAI/LFM2.5-1.2B-Instruct))。Unsloth 相比标准方法提供约 2 倍更快的训练速度和约 60% 更少的 VRAM 使用,因此训练小型模型的成本可能仅需几美元。

为什么选择小型模型?LFM2.5-1.2B-Instruct 这样的小型语言模型是微调的理想候选。它们训练成本低廉,迭代速度快,在专门任务上的性能越来越能与大得多的模型相媲美。LFM2.5-1.2B-Instruct 的内存占用不到 1GB,并针对设备端部署进行了优化,因此你微调的模型可以在 CPU、手机和笔记本电脑上运行。

观看视频 (http://www.youtube.com/watch?feature=player_embedded&v=Gh5P4niIFNA)

你需要什么

我们提供免费额度在 Hugging Face Jobs 上微调模型。加入 Unsloth Jobs Explorers (https://huggingface.co/unsloth-jobs) 组织来领取你的免费额度和一个月的 Pro 订阅。

  • 一个 Hugging Face (https://huggingface.co/) 账户(HF Jobs 必需)
  • 计费设置(用于验证,你可以在 billing page (https://huggingface.co/settings/billing) 监控使用情况并管理计费)
  • 具有写入权限的 Hugging Face token
  • (可选)一个编码代理(Open CodeClaude CodeCodex

运行任务

如果想使用 HF Jobs 和 Unsloth 训练模型,只需使用 hf jobs CLI 提交任务。

首先,需要安装 hf CLI。可以运行以下命令:

# mac 或 linux
curl -LsSf https://hf.co/cli/install.sh | bash

接下来可以运行以下命令提交任务:

hf jobs uv run https://huggingface.co/datasets/unsloth/jobs/resolve/main/sft-lfm2.5.py \
    --flavor a10g-small  \
    --secrets HF_TOKEN  \
    --timeout 4h \
    --dataset mlabonne/FineTome-100k \
    --num-epochs 1 \
    --eval-split 0.2 \
    --output-repo your-username/lfm-finetuned

查看 training script (https://huggingface.co/datasets/unsloth/jobs/blob/main/sft-lfm2.5.py) 和 Hugging Face Jobs 文档 (https://huggingface.co/docs/hub/jobs) 了解更多详情。

安装技能

Hugging Face 模型训练技能降低了训练模型的门槛,只需简单提示即可。首先用编码代理安装技能。

Claude Code

Claude Code 通过其 plugin system (https://code.claude.com/docs/en/discover-plugins) 发现技能,所以我们需要先安装 Hugging Face 技能。操作如下:

  1. 添加市场:
/plugin marketplace add huggingface/skills
  1. Discover 标签中浏览可用技能:
/plugin
  1. 安装模型训练师技能:
/plugin install hugging-face-model-trainer@huggingface-skills

更多详情,参阅 hub 技能文档 (https://huggingface.co/docs/hub/en/agents-skills) 或 Claude Code Skills 文档 (https://code.claude.com/docs/en/skills)。

Codex

Codex 通过 AGENTS.md (https://developers.openai.com/codex/guides/agents-md) 文件和 .agents/skills/ (https://developers.openai.com/codex/skills) 目录发现技能。

$skill-installer 安装单个技能:

$skill-installer install https://github.com/huggingface/skills/tree/main/skills/hugging-face-model-trainer

更多详情,参阅 Codex Skills 文档 (https://developers.openai.com/codex/skills) 和 AGENTS.md 指南 (https://developers.openai.com/codex/guides/agents-md)。

其他方式

一个通用的安装方法就是克隆 skills repository (https://github.com/huggingface/skills) 并将 skill (https://github.com/huggingface/skills/tree/main/skills/hugging-face-model-trainer) 复制到代理的技能目录。

git clone https://github.com/huggingface/skills.git
mkdir -p ~/.agents/skills && cp -R skills/skills/hugging-face-model-trainer ~/.agents/skills/

快速开始

安装技能后,请求编码代理训练模型:

Train LiquidAI/LFM2.5-1.2B-Instruct on mlabonne/FineTome-100k using Unsloth on HF Jobs

代理将根据技能中的 example (https://github.com/huggingface/skills/blob/main/skills/hugging-face-model-trainer/scripts/unsloth_sft_example.py) 生成训练脚本,将训练提交到 HF Jobs,并通过 Trackio 提供监控链接。

工作原理

训练任务运行在 Hugging Face Jobs (https://huggingface.co/docs/huggingface_hub/guides/jobs) 上,这是完全托管的云 GPU。代理会:

  1. 生成一个带有内联依赖的 UV 脚本
  2. 通过 hf CLI 将其提交到 HF Jobs
  3. 报告任务 ID 和监控 URL
  4. 将训练后的模型推送到你的 Hugging Face Hub 仓库

示例训练脚本

技能根据 skill (https://github.com/huggingface/skills/blob/main/skills/hugging-face-model-trainer/scripts/unsloth_sft_example.py) 中的示例生成这样的脚本。

# /// script
# dependencies = ["unsloth", "trl>=0.12.0", "datasets", "trackio"]
# ///

from unsloth import FastLanguageModel
from trl import SFTTrainer, SFTConfig
from datasets import load_dataset

model, tokenizer = FastLanguageModel.from_pretrained(
    "LiquidAI/LFM2.5-1.2B-Instruct",
    load_in_4bit=True,
    max_seq_length=2048,
)

model = FastLanguageModel.get_peft_model(
    model,
    r=16,
    lora_alpha=32,
    lora_dropout=0,
    target_modules=[
        "q_proj",
        "k_proj",
        "v_proj",
        "out_proj",
        "in_proj",
        "w1",
        "w2",
        "w3",
    ],
)

dataset = load_dataset("trl-lib/Capybara", split="train")

trainer = SFTTrainer(
    model=model,
    tokenizer=tokenizer,
    train_dataset=dataset,
    args=SFTConfig(
        output_dir="./output",
        push_to_hub=True,
        hub_model_id="username/my-model",
        per_device_train_batch_size=4,
        gradient_accumulation_steps=4,
        num_train_epochs=1,
        learning_rate=2e-4,
        report_to="trackio",
    ),
)

trainer.train()
trainer.push_to_hub()
模型大小推荐 GPU约每小时成本
<1B 参数t4-small~$0.40
1-3B 参数t4-medium~$0.60
3-7B 参数a10g-small~$1.00
7-13B 参数a10g-large~$3.00

有关 Hugging Face Spaces 定价的完整概览,请查看 这里的指南 (https://huggingface.co/docs/hub/en/spaces-overview#hardware-resources)。

与编码代理合作的技巧

  • 明确指定要使用的模型和数据集,并包含 Hub ID(例如,Qwen/Qwen2.5-0.5Btrl-lib/Capybara)。代理将搜索和验证这些组合。
  • 如果想使用 Unsloth,明确提及。否则,代理将根据模型和预算选择框架。
  • 在启动大型任务前请求成本估计。
  • 请求 Trackio 监控以获取实时损失曲线。
  • 通过请求代理检查日志来在提交后查看任务状态。

资源

  • Hugging Face Skills Repository (https://github.com/huggingface/skills)
  • Unsloth Jobs Explorers 免费额度 (https://huggingface.co/unsloth-jobs)
  • Hugging Face Jobs 上的 Unsloth 教程 (https://unsloth.ai/docs/basics/inference-and-deployment/deploying-with-hugging-face-jobs)
  • 示例 Unsloth Jobs 脚本 (https://huggingface.co/datasets/unsloth/jobs)

相似文章

@nini_incrypto_: Hugging Face 把 AI 训练全流程都自动化了啦! 最近有个叫 ml-intern 的项目在 GitHub 爆火,它就像一个 24 小时待命的算法实习生,能独立完成大模型的后训练(Post-training) 1.自主调研:它会…

X AI KOLs Timeline

Hugging Face 推出的 ml-intern 项目在 GitHub 上爆火,能够自主完成论文调研、数据处理、训练脚本编写和模型训练的全流程自动化,无需人工干预即可将小模型(如 Qwen3-1.7B)的性能大幅提升,甚至超越 Claude Code。