> 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) 中微调 LLM，既可以在本地进行，也可以使用 Google Colab 扩展。本指南将介绍如何使用开源训练 [仓库：Unsloth](https://github.com/unslothai/unsloth)，将 VS Code 中的任意 [微调笔记本](https://unsloth.ai/docs/zh/kuai-su-kai-shi/unsloth-notebooks.md) 连接到 Colab 运行时，从而使用本地 GPU 或免费的 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%2FnITq5t7dffCUGSW2EVsm%2Fcolab_img_1.optimized.png?alt=media" 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&amp;token=7a72c2ec-6f94-4015-8355-27e76ba9b974" alt="" width="375"><figcaption></figcaption></figure>

2. 打开所需的笔记本。Unsloth 支持大多数模型，包括 [嵌入](https://unsloth.ai/docs/zh/ji-chu-zhi-shi/embedding-finetuning.md), [TTS](https://unsloth.ai/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%2FVBqwvArAyclTQjb7YL5I%2Fcolab_img_2.optimized.png?alt=media" 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&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 %}

#### 运行笔记本

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

### 视频教程

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

### 故障排除

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

**问题原因：**\
如果 Colab 服务器断开连接时笔记本仍处于打开状态，重新连接后 VS Code 可能会陷入异常的内核/运行时状态。相关 [GitHub issue](https://github.com/googlecolab/colab-vscode/issues/200).

**解决方法：** 完全关闭笔记本标签页，然后重新打开笔记本。

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

可能的原因和解决方法：

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

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

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


---

# 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.
