> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/kuai-su-kai-shi/install/vs-code.md).

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

通过 Unsloth 和 Google Colab 在 Visual Studio Code 中直接微调模型的指南。

你现在可以直接从 Visual Studio Code（VSCode）在本地或使用 Google Colab 扩展对 LLM 进行微调。在本指南中，你将学习如何使用开源训练 [仓库：Unsloth](https://github.com/unslothai/unsloth)，以连接任何 [微调笔记本](/docs/zh/kuai-su-kai-shi/unsloth-notebooks.md) VS Code 中的 Colab 运行时，这样你就可以在本地或免费的 Colab GPU 上训练。你还可以观看我们的视频教程 [这里](#video-tutorial).

{% stepper %}
{% step %}

### VS Code 和 Colab 教程：

开始之前，我们需要准备：

* 已安装 [VS Code](https://code.visualstudio.com/)。Git（用于克隆 notebook 仓库）通常默认已安装。
* 一个 **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&amp;token=1faa7aac-c016-4c31-90ba-ccad655244e1" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 打开一个 Unsloth notebook

1. 克隆 Unsloth [notebooks 仓库](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&amp;token=7a72c2ec-6f94-4015-8355-27e76ba9b974" alt="" width="375"><figcaption></figcaption></figure>

2. 打开你想要的 notebook。Unsloth 支持大多数模型，包括 [嵌入](/docs/zh/ji-chu-zhi-shi/embedding-finetuning.md), [TTS](/docs/zh/ji-chu-zhi-shi/text-to-speech-tts-fine-tuning.md)。例如，我们将使用 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&amp;token=805ebe8b-cb50-49a4-83d4-0daf96ad21c1" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 选择一个内核并选择 Colab

在 notebook 工具栏中，点击 **选择内核**，然后在 **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&amp;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&amp;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&amp;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&amp;token=391e343d-d2fa-4a77-8e3f-ef244adfcb4c" alt="" width="375"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 运行 notebook

* 点击 **全部运行** 在 notebook 工具栏中（或从上到下运行各个单元格）
* 观察安装设置单元格先安装依赖项，然后启动 Unsloth 工作流
* 你可以查看我们专门的 [微调](/docs/zh/kuai-su-kai-shi/fine-tuning-llms-guide.md) 或 [强化学习](/docs/zh/kuai-su-kai-shi/reinforcement-learning-rl-guide.md) 指南以获取更多关于如何开始使用 Unsloth 的信息。
  {% endstep %}
  {% endstepper %}

### 视频教程

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

### 故障排除

#### 在 Colab 服务器断开连接后，notebook 不会在新的服务器上运行

**发生了什么：**\
如果 notebook 在 Colab 服务器断开连接时仍然保持打开，那么在重新连接后，VS Code 可能会卡在错误的内核/运行时状态。相关 [GitHub issue](https://github.com/googlecolab/colab-vscode/issues/200).

**修复方法：** 将 notebook 标签页完全关闭，然后重新打开 notebook。

#### 你无法选择 GPU（只显示 CPU）

可能的原因和修复：

* **Colab 免费层容量：** GPU 可能暂时不可用 → 稍后再试。
* **实际上没有连接到 Colab 运行时：** 重新检查 **选择内核 → Colab** 并确保 Colab 服务器处于活动状态。
* **账号/地区限制或已达到上限：** 你可能需要等待，或使用其他 Google 账号/套餐。

#### 一切都运行正常，但重新连接后包“消失”了

Colab 运行时是 **临时的**。当服务器重启时，你通常需要重新运行设置/安装单元格（通常是 notebook 中前几个单元格）。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/zh/kuai-su-kai-shi/install/vs-code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
