flux的kontext安装和使用

1. 资源

1.1 模型

https://github.com/black-forest-labs/flux
https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev

  • Pro 和 Max 版本一如既往,是 Black forest Lab 相关模型的最顶尖的能力仅可以通过 API 来调用
  • Dev 版本是开源版本供社区学习和研究使用,目前已经开源。相关的代码和模型权重地址都可以在 官网 找到。

1.2 Prompt

promot 教程: https://docs.bfl.ai/guides/prompting_guide_kontext_i2i

prompt 优化: https://linux.do/t/topic/753477

google app: https://aistudio.google.com/apps?source=user

2. 安装

2.1 autodl

安装教程:https://docs.comfy.org/zh-CN/tutorials/flux/flux-1-kontext-dev

  1. PyTorch 2.7.0
  2. Python 3.12
  3. Cuda 12.8
1
2
3
4
5
6
git clone https://github.com/comfyanonymous/ComfyUI
cd ComfyUI
pip install -r requirements.txt

# 监听 6006 端口
python main.py --listen --port 6006
  • torchaudio missing, ACE model will be broken
1
2
pip uninstall torch torchvision torchaudio -y
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu128

2.2 runpod

注意 runpod 每次重启后本地文件会全部消失。

1
2
3
4
5
6
7
8
9
10
runpod/pytorch:2.4.0-py3.11-cuda12.4.1-devel-ubuntu22.04



git clone https://github.com/comfyanonymous/ComfyUI
cd ComfyUI
pip install -r requirements.txt

# 监听 6006 端口
python main.py --listen --port 6006

执行后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
To see the GUI go to: http://0.0.0.0:6006
To see the GUI go to: http://[::]:6006
got prompt
Using pytorch attention in VAE
Using pytorch attention in VAE
VAE load device: cuda:0, offload device: cpu, dtype: torch.bfloat16
Requested to load AutoencodingEngine
loaded completely 19884.8125 159.87335777282715 True
Using scaled fp8: fp8 matrix mult: False, scale input: False
CLIP/text encoder model load device: cuda:0, offload device: cpu, current: cpu, dtype: torch.float16
clip missing: ['text_projection.weight']
Requested to load FluxClipModel_
loaded completely 22347.739142227172 4903.231597900391 True
Using scaled fp8: fp8 matrix mult: True, scale input: True
model weight dtype torch.bfloat16, manual cast: None
model_type FLUX
Requested to load Flux
  • load device: cuda:0: VAE模型主要在 GPU 0号 上运行。
  • offload device: cpu: 这是一个很聪明的内存管理策略。当GPU显存(VRAM)紧张时,会把暂时不用的模型部分临时存放到内存(由CPU管理)里,需要时再加载回GPU。这可以让你在显存有限的情况下运行更大的模型。
  • dtype: torch.bfloat16: 这是模型使用的数据类型(半精度浮点数)。相比传统的*float32*,它占用的显存减半,并且在现代GPU(如NVIDIA RTX 30/40系列)上计算速度更快,同时保持了足够的精度。
  • Using scaled fp8 这是一个非常关键的性能优化!fp8 是一种比 bfloat16 还要节省显存、计算速度更快的8位数据格式。启用它意味着你的程序正在利用最新的GPU硬件特性(通常是NVIDIA RTX 40系列或更新的显卡)来达到极致的运行速度。

3. 参考资料