# 使用 Hugging Face Jobs 部署 LLM

本指南介绍如何使用 [Unsloth](https://github.com/unslothai/unsloth) 和 [Liquid LFM2.5](/docs/zh/mo-xing/tutorials/lfm2.5.md) 通过 Open Code、Claude Code、Codex 等编码代理进行快速 LLM 微调的 Unsloth [Claude Code](/docs/zh/ji-chu/claude-code.md)。与标准方法相比，Unsloth 提供约 2 倍更快的训练速度和约 60% 更少的 VRAM 占用。

### 你将需要

* 一个 [Hugging Face](https://huggingface.co) 账号（HF Jobs 需要）
* 一个具有写入权限的 Hugging Face 令牌
* 一个编码代理（Open Code、Claude Code、Codex）
* 阅读我们的 [Claude Code](/docs/zh/ji-chu/claude-code.md) 配置指南。

### 安装技能

#### Claude Code

Claude Code 通过其 [插件系统](https://code.claude.com/docs/en/discover-plugins).

1. 添加市场：

```bash
/plugin marketplace add huggingface/skills
```

2. 在 **Discover** 标签页中浏览可用技能：

```bash
/plugin
```

3. 安装模型训练器技能：

```bash
/plugin install hugging-face-model-trainer@huggingface-skills
```

更多详情请参阅 [Claude Code 插件文档](https://code.claude.com/docs/en/discover-plugins) 以及 [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`**

{% code overflow="wrap" %}

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

{% endcode %}

更多详情请参阅 [Codex Skills 文档](https://developers.openai.com/codex/skills) 以及 [AGENTS.md 指南](https://developers.openai.com/codex/guides/agents-md).

### 快速开始

安装好技能后，让你的编码代理训练一个模型。我们使用的是 [Liquid LFM2.5](/docs/zh/mo-xing/tutorials/lfm2.5.md)

{% code overflow="wrap" %}

```
使用 Unsloth 在 HF Jobs 上训练 LiquidAI/LFM2.5-1.2B-Instruct 于 trl-lib/Capybara
```

{% endcode %}

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

### 使用 Hugging Face Jobs

训练任务将在 [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs) 上运行——完全托管的云端 GPU。如果你熟悉 Google Colab 点数，Hugging Face Jobs 也提供类似的点数系统。它采用按量付费结构，或者你也可以提前获取点数。该代理：

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

#### 示例训练脚本

该技能会生成类似这样的脚本：

{% code expandable="true" %}

```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(
    "Qwen/Qwen2.5-0.5B",
    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", "o_proj",
                    "gate_proj", "up_proj", "down_proj"],
)

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()
```

{% endcode %}

使用 Hugging Face Jobs 训练的费用如下：

| 模型大小     | 推荐 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 space 定价的完整概览，请查看指南 [这里](https://huggingface.co/docs/hub/en/spaces-overview#hardware-resources).

### 与编码代理协作的技巧

* 明确要使用的模型和数据集，并包含 Hub ID（例如， `Qwen/Qwen2.5-0.5B`, `trl-lib/Capybara`）。代理会搜索并验证这些组合。
* 如果你希望使用 Unsloth，请明确提及。否则，代理会根据模型和预算决定框架。
* 在启动大型任务前先请求成本估算
* 要求 Trackio 监控以获取实时损失曲线
* 在提交后，让代理检查日志以查看任务状态

### 资源

* [Hugging Face Skills 仓库](https://github.com/huggingface/skills)

{% embed url="<https://youtu.be/Gh5P4niIFNA>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/zh/ji-chu/inference-and-deployment/deploying-llms-with-hugging-face-jobs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
