> ## Documentation Index
> Fetch the complete documentation index at: https://docs.devinenterprise.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Devin 和 Modal 对自定义模型进行后训练

> 使用 Devin 在 Modal GPU outpost 上对自定义模型进行后训练：微调 FLUX LoRA，将其部署为 Modal 端点，并通过 PR 交付

<video controls width="100%" playsInline>
  <source src="https://mintcdn.com/cognitionai-enterprise/lI6J3knrFHRXaW6-/images/tutorial-library/post-training-walkthrough.mp4?fit=max&auto=format&n=lI6J3knrFHRXaW6-&q=85&s=86cf58b823331c747f994ada34b79612" type="video/mp4" />
</video>

Devin 在 Modal GPU 上对 FLUX LoRA 进行后训练，部署效果最佳的检查点，并将其添加到聊天应用中

作业需要在哪里运行，Devin 就能在哪里运行。借助 [Devin Outposts](/zh/cloud/outposts/overview)，会话可以在你自己的基础设施或合作伙伴平台上运行；本例中，会话在 [Modal](https://modal.com/docs/devin) GPU 沙盒中运行，因此为你编写代码的那个 Devin，同样能端到端地完成模型的训练、评估和部署。在本教程中，Devin 用 18 张水獭图片微调 FLUX LoRA，让 "Devin the software engineer" 生成的形象是一只水獭，随后将其部署为 Modal 端点，并添加到聊天应用的模型选择器中。

## 开始之前

* **创建 Modal outpost**：运行 `uvx modal-devin init`，并为生成的 Modal 应用选择 GPU。参见[在 Modal 上运行 Devin](https://modal.com/docs/devin)和 [Outposts 快速开始](/zh/cloud/outposts/quickstart.md)。
* **连接您的代码仓库**：即您希望接入该模型的应用。视频中使用的是一个私有的演示聊天应用，带有图像模型选择器，并已检出到持久化的 Modal Volume 上。还没有应用？可以先让 Devin 构建一个 (例如，`build a simple chat app with a model picker that calls an image generation endpoint`) ，或者跳过应用，只让 Devin 训练并部署模型。
* **准备训练数据**：将带说明文字的图片添加到代码仓库或 outpost 挂载的 Modal Volume 中。示例使用了 18 张卡通水獭图片，说明文字形如“Devin the software engineer in a data center”。
* **添加 secrets**：将您的 Weights & Biases 密钥保存到[设置 > 密钥](https://app.devin.ai/settings/secrets)中。Modal 沙盒已自带 Modal 凭据，因此 Devin 无需额外配置即可部署端点。
* **为长时间运行的任务设置规则**：添加[组织规则](/zh/product-guides/plugins.md#rules) (或直接写入提示中) ，例如：每 15 分钟发布一次进度更新、在每个检查点渲染验证图像，以及在每个 PR 中附上预览链接。
* **连接 Slack** (可选) ：训练运行实例通常需要数小时，因此建议[将会话同步到 Slack 会话线程](/zh/integrations/slack.md#sync-sessions-with-slack-threads)，以便随时跟进进度。

## 步骤 1：在 outpost 上启动会话

打开 [Devin](https://app.devin.ai/)，在提示框下方的 **Virtual environment** 菜单中选择你的 Modal outpost，然后在消息编辑器中打开 Slack 开关。检查硬件配置：

```text theme={null}
详细介绍一下你所在的环境
```

Devin 反馈称，环境中有一块 NVIDIA B200，已安装 PyTorch 和 diffusers，项目则位于持久化的 Modal Volume 上。

## 步骤 2：检查训练数据

```text theme={null}
嘿 devin，给我看看我在监督学习里用的图片
```

Devin 会在项目中找到该数据集，并将这 18 张图片连同说明文字一起发送到会话和 Slack 会话线程中。

## 步骤 3：开始运行训练

```text theme={null}
对 flux 进行后训练，创建 otterimage 1.0 模型。完成后，将 otterimage 添加到 otterchat 的模型选择器中
```

`otterchat` 是视频中的演示应用；请替换为您的代码仓库名称。

Devin 会按照上述规则自主运行该作业：

1. **训练 LoRA**：约 1,000 步，日志记录到 Weights & Biases，每 100 步渲染一次验证图像。
2. **向 Slack 汇报**：每 15 分钟发送一次进度和最新渲染图。早期渲染图中是人物；随着训练推进，水獭逐渐显现。
3. **挑选最佳检查点**：选定第 700 步，因为第 900 步和第 1,000 步已出现过拟合迹象。
4. **部署并交付**：将第 700 步的检查点部署为 serverless Modal 端点，将 `otterimage 1.0` 添加到聊天应用的模型选择器中，[录制](/zh/work-with-devin/testing-and-recordings.md)一次端到端测试，并创建一个附带预览链接的 PR。

![各检查点验证图像网格](https://mintcdn.com/cognitionai-enterprise/lI6J3knrFHRXaW6-/images/tutorial-library/post-training-validation.png?fit=max\&auto=format\&n=lI6J3knrFHRXaW6-\&q=85\&s=508dd2b263df31cecb698f97fac2f4a0)

第 500 步至第 1,000 步的验证渲染图

## 步骤 4：试用模型

打开 PR 中的预览链接，让模型生成一张图片：

```text theme={null}
生成一张软件工程师 Devin 吃意大利面的图片
```

![聊天应用返回了一只正在吃意大利面的卡通水獭](https://mintcdn.com/cognitionai-enterprise/lI6J3knrFHRXaW6-/images/tutorial-library/post-training-otterchat.png?fit=max\&auto=format\&n=lI6J3knrFHRXaW6-\&q=85\&s=01d56500b347f5fdddde6dbb5e075fba)

而在通用图像模型上使用同样的提示，生成的则是一个人。

## 步骤 5：借助子会话迭代

后训练很少能一次运行就完成。把一篇论文交给 Devin，让它并行尝试各种替代方案：

```text theme={null}
https://arxiv.org/pdf/2309.14859v2 启动子会话，用这篇论文中列出的除 LoRA 以外的其他技术训练 otterimage
```

Devin 在一个 PR 中将这些新方法添加到训练脚本中，并在该 outpost 上启动三个[子会话](/zh/work-with-devin/advanced-capabilities.md#managed-devins)，分别负责 LoHa、LoKr 和原生微调。

![Devin 的会话界面，显示分别负责 LoHa、LoKr 和原生微调的三个子会话](https://mintcdn.com/cognitionai-enterprise/lI6J3knrFHRXaW6-/images/tutorial-library/post-training-child-sessions.png?fit=max\&auto=format\&n=lI6J3knrFHRXaW6-\&q=85\&s=e000e44ded334420c7438f429503771b)

## 后续步骤

> **[在 Modal 上运行 Devin](https://modal.com/docs/devin)**
>
> 创建基于 Modal 的 outpost，并配备 GPU、Volumes 和 Secrets

> **[Devin Outposts](/zh/cloud/outposts/overview)**
>
> 在您自己的基础设施或合作伙伴平台上运行会话

> **[Slack 集成](/zh/integrations/slack.md)**
>
> 在 Slack 会话线程中跟进长时间运行的会话

> **[托管 Devins](/zh/work-with-devin/advanced-capabilities.md#managed-devins)**
>
> 启动并监控子会话，并行开展实验


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.