Skip to content

About

Bit-exact PixArt-Sigma inference lab on an 8 GB GPU: manual sampler, Triton FFN kernels, staged/resident runtimes, and reproducible experiments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

diffusion-inference-lab

cpu-tests

在 8 GB 笔记本 GPU 上把 PixArt-Sigma 512 的推理拆开、测清楚,并做到逐位可复现。 Python 包名是 diffusion_lab,命令行入口是 diffusion-lab。

English: A bit-exact PixArt-Sigma inference lab on an 8 GB laptop GPU: an explicit sampler, Triton FFN kernels, staged/resident runtimes, and eight experiments with raw data and honest negative results.

三个提示词的本地生成结果

上面三张图由本仓库的 runtime 本地生成:原提示词重编码、红色陶瓷茶壶、风暴中的灯塔。

这不是一个推理服务,也不是一个模型训练项目。它是一份可复核的推理工程记录: 每个结论都有脚本、原始 JSON/日志和报告;正结果和负结果都保留。

结果速览

实验 结论
manual-sampler 手写采样循环 vs diffusers pipeline:3 个 seed × 20 步,20/20 步逐位相同,首个分歧:无
ffn-linear-v2 12 个 Triton 配置全部没有超过 v1/cuBLAS;有效的负结果
precision-audit 发现 PyTorch 默认 reduced-precision reduction 在 contract 尺寸有 330/2,359,296 个元素超差;Triton v1 与严格 cuBLAS 逐位相同
ffn-gelu-fusion Linear+bias+GELU 融合:28/28 模块 + 完整 DiT forward 逐位相同;模块级快 6.4–8.9%,完整 DiT 无可测收益(1.003,CI [0.969, 1.029])
sampling-regression eager vs fused,3 个 seed、20 步,PNG 最大像素差 0;20 步去噪约 2.35–2.47 s
local-runtime-v0 DiT/VAE 各只加载 1 次;连续请求 42→1234→42 全部通过;中位请求耗时 eager 3.655 s / fused 3.493 s
residency-v1 resident 常驻比 staged 快 13.6–13.9%,12/12 配对全胜;代价 +1.46 GiB 常驻、+1.14 GiB 峰值
text-to-image-v0 接入真实提示词;原 prompt 重编码张量与历史文件逐位相同;新 prompt 的条件和 latent 确实不同
review 独立复核纠正了报告自身的错误:16% → 13.6%,并指出报告与 results.json 矛盾、切换成本漏记

本次整理额外验证:eager/fused × staged/resident 四种组合都复现了仓库中提交的 参考 PNG(torch.equal / np.array_equal),说明抽包没有改变数值行为。

环境

项目 值
GPU NVIDIA GeForce RTX 5060 Laptop GPU,8 GB,sm_120
驱动 617.14
系统 WSL2(Linux 6.6)
Python 3.12.3
torch 2.14.1+cu130
Triton 3.8.0
diffusers 0.40.0
transformers 5.18.0
模型 PixArt-Sigma-XL-2-512-MS + SDXL VAE + T5(约 21 GB)
系统 RAM 31 GiB(T5 编码门槛 22 GiB)

快速开始

git clone https://github.com/Echomosome/diffusion-inference-lab.git
cd diffusion-inference-lab

python3.12 -m venv .venv
source .venv/bin/activate

# 锁定版本;CUDA 版本不同时请改成对应的 torch wheel
pip install -r requirements.txt
pip install -e . --no-deps

# 下载 21 GB 权重到 models/(已锁定 revision)
bash scripts/download_models.sh

# 默认提示词:山间小屋
python -m diffusion_lab --seed 42 --outdir outputs/cabin

第一次运行会先用子进程加载 FP32 T5 编码提示词(约 32 s),子进程退出后才加载 DiT/VAE 并出图。输出目录中会有 image_req00_seed42.png 和 run.json。

常用命令

# 新提示词
python -m diffusion_lab \
  --prompt "a red ceramic teapot on a wooden table, studio light" \
  --seed 42 --outdir outputs/teapot

# 融合内核 + staged(显存更省)
python -m diffusion_lab --prompt "a lighthouse in a storm" \
  --backend fused --residency staged --outdir outputs/lighthouse

# 一次进程处理多个 seed
python -m diffusion_lab --prompt "a lighthouse in a storm" \
  --seeds 42,1234,20241005 --outdir outputs/seeds

# 复用已有条件文件,跳过 T5
python -m diffusion_lab \
  --embeddings-file outputs/first-image-pjop3fga/text_embeddings.pt \
  --seed 42 --outdir outputs/reuse

--backend 可选 eager / fused,--residency 可选 staged / resident。 默认是 eager + resident:数值已验证,稳态更快,显存足够。

Python API

from diffusion_lab import encode_prompt, load_conditioning_file, LocalDiffusionRuntime

# 1) 先编码:T5 在短命子进程中运行,退出后才加载图像模型
cond = load_conditioning_file(
    encode_prompt("a red ceramic teapot on a wooden table", outdir="outputs/text")
)

# 2) 一次加载,连续请求
with LocalDiffusionRuntime(backend="eager", residency="resident") as rt:
    result = rt.generate(seed=42, cond=cond)
    result["image"].save("teapot.png")
    print(result["record"]["total_wall_s"])

generate() 默认拒绝在 runtime 进程内直接编码提示词(allow_inline_encode=False), 因为 FP32 T5 约 19 GB,必须避免和 DiT/VAE 同时驻留在一个进程里。

仓库结构

src/diffusion_lab/
├── runtime.py          # 一次加载、连续请求;staged / resident
├── sampler.py          # 显式 20 步采样循环、条件拼接、噪声
├── kernels/
│   ├── ffn_linear.py   # Triton FP16 Linear(v1)
│   └── fused_ffn.py    # Linear+bias+GELU(tanh) 融合与模块替换
├── text.py             # 子进程文本编码的父进程侧
├── text_worker.py      # FP32 T5 短命编码进程
├── precision.py        # 严格 FP16 GEMM 设置
├── config.py           # 512×512 / 20 步 / CFG 4.5
└── cli.py              # python -m diffusion_lab
experiments/            # 8 组实验 + 1 份复核,保持原样
archive/initial/        # 最初的探索脚本,保留作为来源
scripts/
├── download_models.sh
└── make_reference_embeddings.py
tests/
triton_ffn_linear_v1.py # 实验脚本仍导入它,且是 provenance SHA 的对象

experiments/ 下的脚本没有被改写:它们包含当时的路径、日志和原始 JSON。 新的公共包在 src/,两者并存,历史记录优先。

实验索引

目录 日期 主题
manual-sampler 2026-10-05 显式采样循环 vs pipeline,逐步一致性
ffn-linear-v2 2026-10-05 Triton FFN 配置筛选,负结果
precision-audit 2026-10-05 精度基线复核,撤回未经证明的说法
ffn-gelu-fusion 2026-10-05 Linear+bias+GELU 融合,模块级收益
sampling-regression 2026-10-05 eager/fused 20 步采样回归
local-runtime-v0 2026-10-05 一次加载、连续请求的 runtime
residency-v1 2026-10-05 staged vs resident 驻留策略
text-to-image-v0 2026-10-05 真实提示词接入
review 2026-10-05 对 residency-v1 的只读复核

复现与测试

# 测试依赖
pip install pytest

# CPU-only
pytest tests/test_paths.py tests/test_sampler.py -q

# GPU:Triton kernel 与严格 cuBLAS 的逐位比较
pytest tests/test_ffn_linear.py tests/test_fused_ffn.py -q

# GPU + 权重:完整 20 步复现提交的参考 PNG(eager / fused)
DIFFUSION_LAB_RUN_SLOW=1 pytest tests/test_reference_image.py -q

慢测试需要 outputs/first-image-pjop3fga/text_embeddings.pt。如果克隆后没有这个文件, 先运行一次:

python scripts/make_reference_embeddings.py

该脚本重编码默认提示词;张量内容与历史文件逐位相同(.pt 容器字节可能不同, 不影响实验)。

可复现性约定

  • “逐位相同”指 torch.equal,不是误差摘要。
  • 性能结论使用配对中位比、双进程重复和置信区间;不把不同统计口径混在一起。
  • 原始 JSON、日志和报告保留;.pt / .npy 张量不提交,因为可由脚本重新生成。
  • 负结果和复核纠错一并保留;复核不修改历史文件。
  • 模型权重不提交,下载脚本锁定 revision。

设计要点

手动采样循环:不调用 PixArtSigmaPipeline.__call__,逐步驱动 DiT 和 DPMSolverMultistepScheduler。CFG 两臂合批为一次 forward;条件按 [negative, positive] 拼接;out_channels // 2 == in_channels 时走 learned-sigma 分支;每个请求新建 scheduler 和噪声。语义逐条从安装的 diffusers 源码核对。

严格精度:allow_tf32=False、allow_fp16_reduced_precision_reduction=False、 allow_fp16_accumulation=False、float32_matmul_precision="highest"。这是所有逐位 结论成立的前提。

驻留策略:staged 在请求间把 DiT/VAE 放回 CPU;resident 让两者常驻 GPU。 参考机器上 resident 稳态快 13.6–13.9%,代价是 +1.46 GiB 常驻显存。

融合内核:把 FFN 升维的 Linear(1152→4608)+bias+GELU(tanh) 合成一个 Triton kernel,保留中间结果的 FP16 舍入,数值与原实现逐位相同。模块级更快,但只占完整 DiT 时间约 1.3–1.7%,端到端收益低于测量分辨率——报告如实写“未测到”。

文本编码:T5 在独立短命进程中以 FP32 跑在 CPU,编码完退出,父进程才加载 DiT/VAE。这样 19 GB 的文本编码器不会和图像 runtime 抢同一份 RAM。

边界与不做的事

  • 单模型、单卡、512×512、20 步、CFG 4.5;没有 batching、并发、量化、HTTP 服务。
  • 性能数字来自 RTX 5060 Laptop 8GB;不要外推到其他 GPU。
  • 融合 kernel 的端到端收益未测到;v2 配置筛选是负结果。
  • resident 更费显存;staged 是显存不足时的退路。
  • T5 编码需要约 19 GB RAM,默认 22 GiB 门槛;内存不足时 worker 会拒绝启动。
  • 这是实验记录,不是生产推理服务。

许可

本仓库的代码使用 MIT License。模型权重不随仓库分发:

  • Transformer:PixArt-alpha/PixArt-Sigma-XL-2-512-MS
  • VAE / T5 / scheduler:PixArt-alpha/pixart_sigma_sdxlvae_T5_diffusers

下载和使用模型权重时,请遵守各自模型卡上的许可(PixArt-α、SDXL VAE、T5)。 diffusers 使用 Apache-2.0, Triton 使用 MIT。

生成的图片是实验产物,不构成对任何第三方作品的权利主张。

About

Bit-exact PixArt-Sigma inference lab on an 8 GB GPU: manual sampler, Triton FFN kernels, staged/resident runtimes, and reproducible experiments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages