# Google Colab

<figure><img src="/files/f9d31188e8906c978f32d8a7663ae58fd9d3950b" alt=""><figcaption></figcaption></figure>

如果你从未使用过 Colab 笔记本，这里是关于笔记本本身的快速入门：

1. **每个“单元(Cell)”的播放按钮。** 点击它以运行该单元的代码。你不应跳过任何单元，并且必须按时间顺序运行每个单元。如果遇到错误，只需重新运行未运行的单元。另一种选择是按 CTRL + ENTER，如果你不想点击播放按钮。
2. **顶部工具栏的运行时按钮。** 你也可以使用此按钮并选择“全部运行”以一次运行整个笔记本。这将跳过所有自定义步骤，但作为第一次尝试是不错的选择。
3. **连接 / 重新连接 T4 按钮。** T4 是 Google 提供的免费 GPU。它相当强大！

第一个安装单元如下所示：记得点击方括号 \[ ] 中的播放按钮。我们会获取我们开源的 Github 包，并安装一些其他包。

<figure><img src="/files/9677ac001ec3b5566bf3ba642e159ee60eb61d8c" alt=""><figcaption></figcaption></figure>

### Colab 示例代码

Unsloth 用于微调 gpt-oss-20b 的示例代码：

```python
from unsloth import FastLanguageModel, FastModel
import torch
from trl import SFTTrainer, SFTConfig
from datasets import load_dataset
max_seq_length = 2048 # 内部支持 RoPE 缩放，因此可任意选择！
# 获取 LAION 数据集
url = "https://huggingface.co/datasets/laion/OIG/resolve/main/unified_chip2.jsonl"
dataset = load_dataset("json", data_files = {"train" : url}, split = "train")

# 我们支持的 4bit 预量化模型，可实现 4 倍更快的下载且不会 OOM（显存溢出）。
fourbit_models = [
    "unsloth/gpt-oss-20b-unsloth-bnb-4bit", # 或选择任何模型

] # 更多模型见 https://huggingface.co/unsloth

model, tokenizer = FastModel.from_pretrained(
    model_name = "unsloth/gpt-oss-20b",
    max_seq_length = 2048, # 为长上下文选择任意值！
    load_in_4bit = True,  # 4 位量化。False = 16 位 LoRA。
    load_in_8bit = False, # 8 位量化
    load_in_16bit = False, # [新！] 16 位 LoRA
    full_finetuning = False, # 用于完整微调。
    # token = "hf_...", # 如果使用限制访问的模型则使用该 token
)

# 对模型进行补丁并添加快速 LoRA 权重
model = FastLanguageModel.get_peft_model(
    model,
    r = 16,
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                      "gate_proj", "up_proj", "down_proj",],
    lora_alpha = 16,
    lora_dropout = 0, # 支持任意值，但 = 0 是优化设置
    bias = "none",    # 支持任意值，但 = "none" 是优化设置
    # [新] “unsloth” 使用 30% 更少的显存，可容纳 2 倍更大的批次！
    use_gradient_checkpointing = "unsloth", # 对于非常长的上下文使用 True 或 "unsloth"
    random_state = 3407,
    max_seq_length = max_seq_length,
    use_rslora = False,  # 我们支持秩稳定化的 LoRA
    loftq_config = None, # 以及 LoftQ
)

trainer = SFTTrainer(
    model = model,
    train_dataset = dataset,
    tokenizer = tokenizer,
    args = SFTConfig(
        max_seq_length = max_seq_length,
        per_device_train_batch_size = 2,
        gradient_accumulation_steps = 4,
        warmup_steps = 10,
        max_steps = 60,
        logging_steps = 1,
        output_dir = "outputs",
        optim = "adamw_8bit",
        seed = 3407,
    ),
)
trainer.train()

# 访问 https://docs.unsloth.ai 获取高级提示，例如
# (1) 保存为 GGUF / 合并为 16 位以用于 vLLM
# (2) 从已保存的 LoRA 适配器继续训练
# (3) 添加评估循环 / 处理 OOM（显存溢出）
# (4) 自定义聊天模板
```


---

# 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/google-colab.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.
