本地部署推理TextDiffuser-2:释放语言模型用于文本渲染的力量

系列文章目录

文章目录


一、模型下载和环境配置

  1. 将textdiffuser-2模型仓库克隆到本地
python 复制代码
git clone https://github.com/microsoft/unilm/
cd unilm/textdiffuser-2
  1. 创建并激活虚拟环境,在textdiffuser-2目录下安装需要的软件包
python 复制代码
conda create -n textdiffuser2 python=3.8
conda activate textdiffuser2
pip install -r requirements.txt
  1. 安装与系统版本和cuda版本相匹配的torch、torchvision、xformers (我的环境下cuda是12.2的,其他版本需要自己去官网查询)
python 复制代码
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xformers
  1. 如果想用FastChat训练布局规划器,还需要安装flash-attention:

先将flash-attention模型仓库克隆下来

python 复制代码
git clone https://github.com/Dao-AILab/flash-attention.git

然后安装对应的软件包

python 复制代码
pip install packaging
pip uninstall -y ninja && pip install ninja
conda install -c nvidia cuda
pip install flash-attn --no-build-isolation
  1. 为了训练文本修复任务,还需要安装 differs 包
python 复制代码
pip install https://github.com/JingyeChen/diffusers_td2.git

二、模型训练

(一)训练布局规划器

  1. 需要先下载lmsys/vicuna-7b-v1.5模型FastChat模型

模型下载方式: 采用git远程clone下来,具体方式可以参考之前的内容:huggingface学习 | 云服务器使用git-lfs下载huggingface上的模型文件

  1. 进行训练
python 复制代码
CUDA_VISIBLE_DEVICES=4,5 torchrun --nproc_per_node=2 --master_port=50008 FastChat-main/fastchat/train/train_mem.py \
    --model_name_or_path vicuna-7b-v1.5  \
    --data_path data/layout_planner_data_5k.json \
    --bf16 True \
    --output_dir experiment_result \
    --num_train_epochs 6 \
    --per_device_train_batch_size 2 \
    --per_device_eval_batch_size 2 \
    --gradient_accumulation_steps 16 \
    --evaluation_strategy "no" \
    --save_strategy "steps" \
    --save_steps 500 \
    --save_total_limit 5 \
    --learning_rate 2e-5 \
    --weight_decay 0. \
    --warmup_ratio 0.03 \
    --lr_scheduler_type "cosine" \
    --logging_steps 1 \
    --fsdp "full_shard auto_wrap" \
    --fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \
    --tf32 True \
    --model_max_length 2048 \
    --gradient_checkpointing True \
    --lazy_preprocess True

(二)训练扩散模型

  1. 需要先准备需要训练的扩散模型:stable-diffusion-v1-5模型
  2. 对于全参数训练:
python 复制代码
accelerate launch train_textdiffuser2_t2i_full.py \
    --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \
    --train_batch_size=18 \
    --gradient_accumulation_steps=4 \
    --gradient_checkpointing \
    --mixed_precision="fp16" \
    --num_train_epochs=6 \
    --learning_rate=1e-5 \
    --max_grad_norm=1 \
    --lr_scheduler="constant" \
    --lr_warmup_steps=0 \
    --output_dir="diffusion_experiment_result" \
    --enable_xformers_memory_efficient_attention \
    --dataloader_num_workers=8 \
    --index_file_path='/path/to/train_dataset_index.txt' \
    --dataset_path='/path/to/laion-ocr-select/' \
    --granularity=128 \
    --coord_mode="ltrb" \
    --max_length=77 \
    --resume_from_checkpoint="latest"
  1. 对于 LoRA 训练:
python 复制代码
accelerate launch train_textdiffuser2_t2i_lora.py \
    --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \
    --train_batch_size=18 \
    --gradient_accumulation_steps=4 \
    --gradient_checkpointing \
    --mixed_precision="fp16" \
    --num_train_epochs=6 \
    --learning_rate=1e-4 \
    --text_encoder_learning_rate=1e-5 \
    --lr_scheduler="constant" \
    --output_dir="diffusion_experiment_result" \
    --enable_xformers_memory_efficient_attention \
    --dataloader_num_workers=8 \
    --index_file_path='/path/to/train_dataset_index.txt' \
    --dataset_path='/path/to/laion-ocr-select/' \
    --granularity=128 \
    --coord_mode="ltrb" \
    --max_length=77 \
    --resume_from_checkpoint="latest"

三、模型推理

(一)准备训练好的模型checkpoint

  1. 下载官网提供的模型checkpoint:layout plannerdiffusion model (full parameter fine-tuning)diffusion model (lora fine-tuning)

  2. 准备stable-diffusion-v1-5模型

(二)全参数推理

python 复制代码
CUDA_VISIBLE_DEVICES=4 accelerate launch inference_textdiffuser2_t2i_full.py \
  --pretrained_model_name_or_path="./stable-diffusion-v1-5" \
  --mixed_precision="fp16" \
  --output_dir="inference_results_1" \
  --enable_xformers_memory_efficient_attention \
  --resume_from_checkpoint="./textdiffuser2-full-ft" \
  --granularity=128 \
  --max_length=77 \
  --coord_mode="ltrb" \
  --cfg=7.5 \
  --sample_steps=20 \
  --seed=43555 \
  --m1_model_path="./textdiffuser2_layout_planner" \
  --input_format='prompt' \
  --input_prompt='a hotdog with mustard and other toppings on it'

推理结果:

(三)LoRA微调推理

python 复制代码
CUDA_VISIBLE_DEVICES=4 accelerate launch inference_textdiffuser2_t2i_lora.py \
  --pretrained_model_name_or_path="./stable-diffusion-v1-5" \
  --gradient_accumulation_steps=4 \
  --gradient_checkpointing \
  --mixed_precision="fp16" \
  --output_dir="inference_results_2" \
  --enable_xformers_memory_efficient_attention \
  --resume_from_checkpoint="./textdiffuser2-lora-ft" \
  --granularity=128 \
  --coord_mode="ltrb" \
  --cfg=7.5 \
  --sample_steps=50 \
  --seed=43555 \
  --m1_model_path="./textdiffuser2_layout_planner" \
  --input_format='prompt' \
  --input_prompt='a stamp of u.s.a'

运行结果:

四、遇到的错误

(一)importerror,缺少某些库

在运行过程中出现了各种各样的importerror,于是就是缺少哪个库就下载那个库:

python 复制代码
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
python 复制代码
pip install protobuf

(二)报错:libGL.so.1: cannot open shared object file: No such file or directory

python 复制代码
pip uninstall opencv-python
pip install opencv-python-headless

(三)各种奇奇怪怪的错误(本质上是diffusers版本不对)

  • RuntimeError: expected mat1 and mat2 to have the same dtype, but got: float != c10::Half
  • The deprecation tuple ('LoRAXFormersAttnProcessor', '0.26.0', 'Make sure use XFormersAttnProcessor instead by settingLoRA layers to `self.
python 复制代码
pip install diffusers==0.24.0 -i https://pypi.mirrors.ustc.edu.cn/simple/

(四)各种库的版本不兼容

由于作者在官网上提供了实验中使用的软件包列表可供参考,所以我直接将textdiffuser-2的assets文件夹下的refere_requirements.txt文件中的库一次性安装下来:

python 复制代码
cd assets
pip install -r reference_requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/

(五)RuntimeError: expected scalar type float Float bu found Half

这个错误是因为安装的diffusers包里有个文件需要用官网提供的新文件进行替换

可以先根据错误提示找到diffusers库包中attention_processor.py所在的位置,然后用assets文件夹下attention_processor.py进行替换即可解决问题。

参考:libGL.so.1: cannot open shared object file: No such file or directory

相关推荐
AI营销干货站6 小时前
工业B2B获客难?原圈科技解析2026五大AI营销增长引擎
人工智能
程序员老刘·6 小时前
重拾Eval能力:D4rt为Flutter注入AI进化基因
人工智能·flutter·跨平台开发·客户端开发
kebijuelun6 小时前
FlashInfer-Bench:把 AI 生成的 GPU Kernel 放进真实 LLM 系统的“闭环引擎”
人工智能·gpt·深度学习·机器学习·语言模型
Deepoch6 小时前
Deepoc具身模型开发板:让炒菜机器人成为您的智能厨师
人工智能·机器人·开发板·具身模型·deepoc·炒菜机器人·厨房机器人
执携6 小时前
算法 -- 冒泡排序
数据结构·算法
Elastic 中国社区官方博客6 小时前
Elastic:DevRel 通讯 — 2026 年 1 月
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
寻星探路6 小时前
【算法专题】滑动窗口:从“无重复字符”到“字母异位词”的深度剖析
java·开发语言·c++·人工智能·python·算法·ai
盈创力和20076 小时前
智慧城市中智能井盖的未来演进:从边缘感知节点到城市智能体
人工智能·智慧城市·智慧市政·智慧水务·智能井盖传感器·综合管廊
njsgcs6 小时前
ppo 找出口模型 训练笔记
人工智能·笔记
萤丰信息6 小时前
从 “钢筋水泥” 到 “数字神经元”:北京 AI 原点社区重构城市进化新逻辑
java·大数据·人工智能·安全·重构·智慧城市·智慧园区