# 如何在 VS Code 中使用 Unsloth 和 Colab GPU 微调 LLM

您现在可以直接从 Visual Studio Code (VS Code) 对大型语言模型（LLM）进行微调，本地或通过使用 Google Colab 的扩展。在本指南中，您将学习如何使用开源训练 [存储库：Unsloth](https://github.com/unslothai/unsloth)，将任何 [微调笔记本](https://unsloth.ai/docs/zh/kai-shi-shi-yong/unsloth-notebooks) 在 VS Code 中连接到 Colab 运行时，以便您可以在本地或免费的 Colab GPU 上进行训练。您也可以观看我们的视频教程 [此处](#video-tutorial).

{% stepper %}
{% step %}

### VS Code 与 Colab 教程：

开始前我们需要：

* 已安装 [VS Code](https://code.visualstudio.com/)。Git（用于克隆笔记本存储库）通常默认已安装。
* 一个 **Google 帐号** （用于与 Colab 进行身份验证）
* 推荐： **Jupyter** 扩展（大多数 VS Code 配置通常已有）
  {% endstep %}

{% step %}

#### 在 VS Code 中安装 Colab 扩展

1. 打开 **扩展** 在 VS Code 中（`Ctrl+Shift+X` / `Cmd+Shift+X`)
2. 搜索 **“Colab”** 并安装 **Google Colab** 扩展

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FJ1f5BS4EK0ok5QLy6vz5%2Fcolab_img_1.png?alt=media&#x26;token=1faa7aac-c016-4c31-90ba-ccad655244e1" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 打开 Unsloth 笔记本

1. 克隆 Unsloth [笔记本存储库](https://github.com/unslothai/notebooks):

```bash
git clone https://github.com/unslothai/notebooks
cd notebooks/nb
```

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FwRL8pvgtLRpz5rcmS6vy%2FScreenshot%202026-02-18%20at%206.29.16%E2%80%AFPM.png?alt=media&#x26;token=7a72c2ec-6f94-4015-8355-27e76ba9b974" alt="" width="375"><figcaption></figcaption></figure>

2. 打开您想要的笔记本。Unsloth 支持大多数模型，包括 [嵌入](https://unsloth.ai/docs/zh/ji-chu/embedding-finetuning), [语音合成 (TTS)](https://unsloth.ai/docs/zh/ji-chu/text-to-speech-tts-fine-tuning)。例如，我们将使用 Qwen3-4B RL： `nb/Qwen3_(4B)-GRPO.ipynb`

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FleZtIzyflSgi1o1QcZDU%2Fcolab_img_2.png?alt=media&#x26;token=805ebe8b-cb50-49a4-83d4-0daf96ad21c1" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 选择内核并选择 Colab

在笔记本工具栏中，点击 **选择内核**，然后选择 **Colab**

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FleGtAKSVehrlspPGvDy2%2Fcolab_img_3.png?alt=media&#x26;token=ba3d8f1d-9cbb-44ed-8e52-8d0e52280607" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 添加新的 Colab 服务器

选择 **Colab**后，您会看到带有服务器选项的下拉菜单。

1. 点击 **+ 添加新的 Colab 服务器**
2. 第一次可能会打开浏览器窗口进行 Google 身份验证
   * 登录，授予访问权限，然后返回 VS Code

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FZcDsB2i0aUStIKEeFTvF%2Fcolab_img_4.png?alt=media&#x26;token=a7c3642f-db35-4215-933f-99c17ebc92a2" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 选择 GPU 并为服务器命名

1. 将 **硬件加速器** 设置为 **GPU**
2. 选择 GPU 类型（例如 **T4**，如果可用）
3. 为服务器命名（随您喜欢）

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FLuK8oheihdCNn9MAZSlk%2Fcolab_img_5.png?alt=media&#x26;token=e5ed396f-afe2-4c09-b1b8-120c7301793c" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="info" %}
注意：GPU 的可用性取决于您的 Colab 计划和当前容量。如果您看不到 GPU 选项，请参见下面的故障排除。
{% endhint %}
{% endstep %}

{% step %}

#### 选择 Python 内核

连接到 Colab 服务器后，选择该运行时显示的 **Python** 内核（通常是 Python 3 内核）。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FBexUiXJ7LRFBXs95tX2S%2Fcolab_img_6.png?alt=media&#x26;token=391e343d-d2fa-4a77-8e3f-ef244adfcb4c" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 运行笔记本

* 点击 **全部运行** 在笔记本工具栏中（或自上而下运行单元格）
* 观察设置单元格安装依赖项，然后启动 Unsloth 工作流
* 您可以查看我们专门的 [微调](https://unsloth.ai/docs/zh/kai-shi-shi-yong/fine-tuning-llms-guide) 或 [强化学习](https://unsloth.ai/docs/zh/kai-shi-shi-yong/reinforcement-learning-rl-guide) 指南以获取有关如何开始使用 Unsloth 的更多信息。
  {% endstep %}
  {% endstepper %}

### 视频教程

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

### 故障排除

#### 在 Colab 服务器断开后，笔记本无法在新服务器上运行

**发生了什么：**\
如果在 Colab 服务器断开时笔记本仍保持打开，VS Code 在重新连接后可能会陷入不良的内核/运行时状态。相关 [GitHub 问题](https://github.com/googlecolab/colab-vscode/issues/200).

**修复：** 完全关闭笔记本选项卡并重新打开笔记本。

#### 无法选择 GPU（仅显示 CPU）

可能的原因和解决方法：

* **Colab 免费层容量：** GPU 可能暂时不可用 → 稍后再试。
* **实际上未连接到 Colab 运行时：** 重新检查 **选择内核 → Colab** 并确保 Colab 服务器处于活动状态。
* **帐户/区域限制或达到配额：** 您可能需要等待或使用不同的 Google 帐号 / 计划。

#### 一切正常，但在重新连接后软件包“消失”了

Colab 运行时是 **短暂的**。当服务器重启时，通常需要重新运行设置/安装单元格（通常是笔记本的前几个单元格）。


---

# 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/kai-shi-shi-yong/install/vs-code.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.
