# Mochi-1 视频

**Mochi-1** 是 Genmo 的开源 100 亿参数视频生成模型，能够生成 848×480 @ 30fps 的输出并呈现物理上逼真的运动。它采用不对称扩散变换器（AsymmDiT）架构，在运动保真度方面位列开源视频模型的前列。将其部署到 Clore.ai 的 GPU 云上，以远低于商业 API 成本的价格生成专业级视频。

***

## 什么是 Mochi-1？

Mochi-1 是一个 **100 亿参数** 视频扩散模型，训练目标是生成具备以下特性的影片：

* 平滑、物理上合理的运动
* 高时间一致性
* 强烈遵从提示
* 848×480 分辨率，30 fps

它使用了一个 **不对称扩散变换器** （AsymmDiT）架构 — 对视频和文本使用不同的编码器深度 — 可实现规模化的高效推理。权重在 Genmo 开源许可下发布，供研究和商业使用均免费。

**模型亮点：**

* 100 亿参数
* 原生 848×480 @ 30 fps 输出
* 高运动保真度（在社区基准中排名靠前）
* 在 Hugging Face 可用并集成到 diffusers
* 提供 Gradio 演示界面，便于交互

***

## 先决条件

| 要求      | 最低要求     | 推荐配置        |
| ------- | -------- | ----------- |
| GPU 显存  | 24 GB    | 40–80 GB    |
| GPU     | RTX 4090 | A100 / H100 |
| 内存（RAM） | 32 GB    | 64 GB       |
| 存储      | 60 GB    | 100 GB      |
| CUDA    | 11.8+    | 12.1+       |

{% hint style="warning" %}
Mochi-1 是一个大型模型（fp8 约 40 GB / bf16 约 80 GB）。单张 RTX 4090（24 GB）可以通过量化运行。要获得完整质量，请使用 A100 40 GB 或更大。支持多 GPU 配置。
{% endhint %}

***

## 步骤 1 — 在 Clore.ai 上租用 GPU

1. 前往 [clore.ai](https://clore.ai) 并登录。
2. 点击 **市场** 并筛选：
   * 显存（VRAM）： **≥ 24 GB** （最低 RTX 4090，推荐 A100）
   * 对于多 GPU：按 GPU 数量 ≥ 2 进行筛选
3. 选择你的服务器并点击 **配置**.
4. 将 Docker 镜像设置为 `pytorch/pytorch:2.4.1-cuda12.4-cudnn9-devel` （基础镜像 — 我们会在其内安装 Mochi）。
5. 设置开放端口： `22` （SSH）和 `7860` （Gradio 界面）。
6. 点击 **租用**.

{% hint style="info" %}
Clore.ai 提供的 A100 40 GB 实例起价约 \~$0.60–$0.90/小时。对于全质量的 Mochi-1，这是最具成本效益的选择。
{% endhint %}

***

## 第 2 步 — 自定义 Dockerfile

构建你自己的镜像或使用此 `Dockerfile` 来创建一个开箱即用的 Mochi-1 环境：

```dockerfile
FROM pytorch/pytorch:2.4.1-cuda12.4-cudnn9-devel

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    git wget curl ffmpeg \
    libgl1 libglib2.0-0 \
    openssh-server \
    && rm -rf /var/lib/apt/lists/*

# 配置 SSH
RUN mkdir /var/run/sshd && \
    echo 'root:clore123' | chpasswd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    sed -i 's/UsePAM yes/UsePAM no/' /etc/ssh/sshd_config

WORKDIR /workspace

# 克隆 Mochi-1 仓库
RUN git clone https://github.com/genmoai/mochi /workspace/mochi

# 安装 Python 依赖项
RUN cd /workspace/mochi && \
    pip install --upgrade pip && \
    pip install -e . && \
    pip install gradio huggingface_hub

EXPOSE 22 7860

CMD service ssh start && \
    echo "Mochi-1 环境已准备就绪。运行下载脚本然后启动演示。" && \
    tail -f /dev/null
```

### 构建并推送到 Docker Hub

在本地构建镜像并将其推送到你自己的 Docker Hub 帐户（替换 `YOUR_DOCKERHUB_USERNAME` 为你的实际用户名）：

```bash
docker build -t YOUR_DOCKERHUB_USERNAME/mochi-1:latest .
docker push YOUR_DOCKERHUB_USERNAME/mochi-1:latest
```

然后使用 `YOUR_DOCKERHUB_USERNAME/mochi-1:latest` 作为你在 Clore.ai 中的 Docker 镜像。

{% hint style="info" %}
Docker Hub 上没有官方的预构建 Mochi-1 镜像。你需要根据上述 Dockerfile 构建。或者，直接使用 `pytorch/pytorch:2.4.1-cuda12.4-cudnn9-devel` 作为基础镜像，并通过 SSH 手动运行设置命令。
{% endhint %}

***

## 步骤 3 — 通过 SSH 连接

实例启动后：

```bash
ssh root@<clore-host> -p <assigned-ssh-port>
```

***

## 第 4 步 — 下载 Mochi-1 权重

模型权重托管在 Hugging Face。通过 `huggingface_hub` CLI 下载：

```bash
cd /workspace

# 如果未安装 huggingface-cli，则安装
pip install -U huggingface_hub

# 下载 Mochi-1 权重（bf16 约 40 GB）
huggingface-cli download genmo/mochi-1-preview \
    --local-dir /workspace/mochi-weights \
    --include "*.safetensors" "*.json" "*.txt"
```

{% hint style="info" %}
完整的 bf16 模型大约为 80 GB。其 `fp8` 量化版本约为 40 GB，可在带 CPU 卸载的 RTX 4090（24 GB）上运行。指定 `--include "*fp8*"` 以仅下载量化权重。
{% endhint %}

### 替代：仅下载 fp8 量化权重

```bash
huggingface-cli download genmo/mochi-1-preview \
    --local-dir /workspace/mochi-weights \
    --include "*fp8*" "*.json" "*.txt"
```

***

## 第 5 步 — 启动 Gradio 演示

Mochi-1 附带一个 Gradio Web 界面，便于文本到视频的生成：

```bash
cd /workspace/mochi

python demos/gradio_ui.py \
    --model_dir /workspace/mochi-weights \
    --share False \
    --host 0.0.0.0 \
    --port 7860
```

**对于低显存模式（RTX 4090，24 GB）：**

```bash
python demos/gradio_ui.py \
    --model_dir /workspace/mochi-weights \
    --cpu_offload \
    --share False \
    --host 0.0.0.0 \
    --port 7860
```

{% hint style="info" %}
参数 `--cpu_offload` 该标志会在模型层未使用时将其移动到 CPU 内存，从而将峰值显存降低到约 18–20 GB，但生成速度约慢 2 倍。
{% endhint %}

***

## 第 6 步 — 访问 Web 界面

打开浏览器并访问：

```
http://<clore-host>:<public-port-7860>
```

你将看到 Mochi-1 的 Gradio 界面，包含：

* 文本提示输入框
* 生成设置（步数、guidance scale、随机种子）
* 视频输出播放器

***

## 第 7 步 — 生成你的第一个视频

### 示例提示词

**自然场景：**

```
一条雄伟的瀑布从长满苔藓的岩石上倾泻而下，位于郁郁葱葱的热带雨林中， 
黄金时刻的阳光穿过树冠，缓慢的电影式平移
```

**动作场景：**

```
一只猎豹在开阔的大草原上全速奔跑， 
尘土在其身后飞扬，戏剧性的广角镜头，4K 野生动物纪录片风格
```

**抽象/艺术：**

```
彩色颜料在水中以极慢速旋转， 
鲜艳的蓝色和橙色颜料混合，微距镜头，工作室灯光
```

### 推荐设置

| 参数                   | 数值          |
| -------------------- | ----------- |
| 步数                   | 64          |
| guidance scale（引导尺度） | 4.5         |
| 时长                   | 5.1 秒（默认）   |
| 分辨率                  | 848×480（原生） |

{% hint style="info" %}
生成时间因 GPU 而异。在 A100 80 GB 上，生成一个 5 秒视频大约需要 **2–4 分钟**。在带 CPU 卸载的 RTX 4090 上，预计需要 **8–15 分钟**.
{% endhint %}

***

## Python API 使用

要进行编程生成，请使用 diffusers 管道：

```python
import torch
from diffusers import MochiPipeline
from diffusers.utils import export_to_video

# 加载管道
pipe = MochiPipeline.from_pretrained(
    "/workspace/mochi-weights",
    torch_dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()
pipe.enable_vae_slicing()

# 生成视频
with torch.autocast("cuda", torch.bfloat16, cache_enabled=False):
    frames = pipe(
        prompt="一只金毛寻回犬在阳光沙滩上玩取物，电影感",
        num_frames=84,
        guidance_scale=4.5,
        num_inference_steps=64,
        generator=torch.Generator("cuda").manual_seed(42)
    ).frames[0]

# 导出
export_to_video(frames, "output.mp4", fps=30)
print("视频已保存为 output.mp4")
```

### 批量生成脚本

```python
import torch
from diffusers import MochiPipeline
from diffusers.utils import export_to_video
import os

pipe = MochiPipeline.from_pretrained(
    "/workspace/mochi-weights",
    torch_dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()

prompts = [
    "一只蝴蝶慢动作落在花朵上，微距摄影",
    "海浪在日落时拍打着岩石悬崖的镜头，航拍画面",
    "极光在星空下于冰冻湖面上方舞动",
]

os.makedirs("/workspace/outputs", exist_ok=True)

for i, prompt in enumerate(prompts):
    frames = pipe(
        prompt=prompt,
        num_frames=84,
        guidance_scale=4.5,
        num_inference_steps=64,
    ).frames[0]
    
    output_path = f"/workspace/outputs/video_{i:03d}.mp4"
    export_to_video(frames, output_path, fps=30)
    print(f"已保存：{output_path}")
```

***

## 多 GPU 推理

要使用多 GPU 提升生成速度：

```python
import torch
from diffusers import MochiPipeline

# 使用 device_map 实现自动多 GPU 分配
pipe = MochiPipeline.from_pretrained(
    "/workspace/mochi-weights",
    torch_dtype=torch.bfloat16,
    device_map="balanced"
)

# 多 GPU 时无需 cpu_offload
frames = pipe(
    prompt="在此输入你的提示",
    num_frames=84,
    guidance_scale=4.5,
    num_inference_steps=64,
).frames[0]
```

{% hint style="info" %}
Clore.ai 提供多 GPU 服务器（2×、4× RTX 4090 或 A100）。使用 2× A100 80 GB 时，生成 5 秒片段的时间可降至 60 秒以内。
{% endhint %}

***

## 故障排除

### CUDA 内存不足（Out of Memory）

```
torch.cuda.OutOfMemoryError：CUDA 内存不足
```

**将批量大小减小到 1**

1. 将 `--cpu_offload` 添加到 gradio 命令中
2. 启用 VAE 切片： `pipe.enable_vae_slicing()`
3. 减少 `num_frames（帧数）` （尝试用 24 代替 84）
4. 使用 fp8 量化权重代替 bf16

### 模型加载缓慢

**解决方案：** 确保权重存放在快速的 NVMe 驱动器上，而不是 HDD。检查存储速度：

```bash
dd if=/dev/zero of=/workspace/test bs=1M count=1000 conv=fdatasync
```

### 视频伪影 / 时间闪烁

**将批量大小减小到 1**

* 增加推理步数（尝试 80–100）
* 调整 guidance scale（通常 3.5–5.0 范围最佳）
* 使用特定种子以保证可复现性和便于迭代

### 端口 7860 无法访问

检查端口是否在 Clore.ai 中正确打开并且 Gradio 服务器绑定到 `0.0.0.0`:

```bash
ss -tlnp | grep 7860
```

***

## 成本估算

| GPU               | 显存（VRAM） | 预计价格       | 5 秒视频时间    |
| ----------------- | -------- | ---------- | ---------- |
| RTX 4090          | 24 GB    | 约 $0.35/小时 | \~10–15 分钟 |
| A100 40GB         | 40 GB    | \~$0.70/小时 | \~3–5 分钟   |
| 💡 本指南中的所有示例均可部署在 | 80 GB    | \~$1.20/小时 | \~2–3 分钟   |
| 2× A100 80GB      | 160 GB   | \~$2.20/小时 | \~60–90 秒  |

***

## Clore.ai 的 GPU 建议

Mochi-1 对显存要求很高 — 这个 100 亿参数模型需要谨慎选择 GPU。

| GPU               | 显存（VRAM） | Clore.ai 价格 | 模式         | 5 秒视频生成时间  |
| ----------------- | -------- | ----------- | ---------- | ---------- |
| RTX 4090          | 24 GB    | \~$0.70/小时  | 仅 fp8 量化   | \~10–15 分钟 |
| A100 40GB         | 40 GB    | \~$1.20/小时  | 推荐 bf16    | \~3–5 分钟   |
| 💡 本指南中的所有示例均可部署在 | 80 GB    | \~$2.00/小时  | 完整 bf16，快速 | \~2–3 分钟   |
| 2× A100 80GB      | 160 GB   | \~$4.00/小时  | 张量并行，最快    | \~60–90 秒  |

{% hint style="warning" %}
**不推荐使用 RTX 3090（24GB）** — Mochi-1 在 fp8 模式下最低需要 24GB，几乎没有余量。RTX 4090（24GB）在 fp8 下可工作，但在更长序列上经常 OOM。要获得可靠结果，请先使用 A100 40GB。
{% endhint %}

**性价比最高的质量选择：** A100 40GB 约 \~$1.20/小时可在 3–5 分钟内生成一个 5 秒片段。每个视频片段成本约 \~$0.08–0.10 — 显著低于 Runway ML（$0.25–0.50/片）或 Pika Labs 订阅费用。

***

## 有用的资源

* [Mochi-1 GitHub](https://github.com/genmoai/mochi)
* [Mochi-1 在 Hugging Face](https://huggingface.co/genmo/mochi-1-preview)
* [Genmo 博客 — Mochi-1 发布](https://www.genmo.ai/blog/mochi-1)
* [Diffusers Mochi 文档](https://huggingface.co/docs/diffusers/api/pipelines/mochi)
* [Mochi 提示词指南（社区）](https://github.com/genmoai/mochi/blob/main/README.md)
