当前位置: 首页 > news >正文

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

系列文章目录

文章目录

  • 系列文章目录
  • 一、模型下载和环境配置
  • 二、模型训练
    • (一)训练布局规划器
    • (二)训练扩散模型
  • 三、模型推理
    • (一)准备训练好的模型checkpoint
    • (二)全参数推理
    • (三)LoRA微调推理
  • 四、遇到的错误
    • (一)importerror,缺少某些库
    • (二)报错:libGL.so.1: cannot open shared object file: No such file or directory
    • (三)各种奇奇怪怪的错误(本质上是diffusers版本不对)
    • (四)各种库的版本不兼容
    • (五)RuntimeError: expected scalar type float Float bu found Half


一、模型下载和环境配置

  1. 将textdiffuser-2模型仓库克隆到本地
git clone https://github.com/microsoft/unilm/
cd unilm/textdiffuser-2
  1. 创建并激活虚拟环境,在textdiffuser-2目录下安装需要的软件包
conda create -n textdiffuser2 python=3.8
conda activate textdiffuser2
pip install -r requirements.txt
  1. 安装与系统版本和cuda版本相匹配的torch、torchvision、xformers (我的环境下cuda是12.2的,其他版本需要自己去官网查询)
    在这里插入图片描述
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模型仓库克隆下来

git clone https://github.com/Dao-AILab/flash-attention.git

然后安装对应的软件包

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

二、模型训练

(一)训练布局规划器

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

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

  1. 进行训练
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. 对于全参数训练:
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 训练:
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 planner、diffusion model (full parameter fine-tuning) 和diffusion model (lora fine-tuning)

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

(二)全参数推理

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微调推理

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,于是就是缺少哪个库就下载那个库:

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

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

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.
pip install diffusers==0.24.0 -i https://pypi.mirrors.ustc.edu.cn/simple/

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

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

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 为什么不用 index 做 key?
  • 使用 Docker 部署 Next Terminal 轻量级堡垒机
  • 项目解决方案:视频监控接入和录像系统设计方案(下)
  • 【Python从入门到进阶】50、当当网Scrapy项目实战(三)
  • Midjourney绘图欣赏系列(七)
  • [2023年]-hadoop面试真题(一)
  • 【py】加载sdk文件夹中的dll
  • 蓝桥杯2023年-平方差(数学)
  • 谷歌开源的LLM大模型 Gemma 简介
  • Elasticsearch 通过索引阻塞实现数据保护深入解析
  • 网络安全: Kali Linux 进行 SSH 渗透与防御
  • pyqt线程正确使用
  • MyBatisPlus理解
  • 代码随想录 贪心算法-简单题目
  • Unity 整体界面淡入淡出效果
  • 分享的文章《人生如棋》
  • “大数据应用场景”之隔壁老王(连载四)
  • 【个人向】《HTTP图解》阅后小结
  • 2017 前端面试准备 - 收藏集 - 掘金
  • canvas 绘制双线技巧
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • ES6之路之模块详解
  • golang中接口赋值与方法集
  • HTTP请求重发
  • IDEA常用插件整理
  • iOS 系统授权开发
  • JDK 6和JDK 7中的substring()方法
  • Mac转Windows的拯救指南
  • PHP的类修饰符与访问修饰符
  • PHP那些事儿
  • Vue--数据传输
  • windows下使用nginx调试简介
  • 不上全站https的网站你们就等着被恶心死吧
  • 从重复到重用
  • 电商搜索引擎的架构设计和性能优化
  • 利用jquery编写加法运算验证码
  • 免费小说阅读小程序
  • 如何解决微信端直接跳WAP端
  • 微服务核心架构梳理
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • ​ubuntu下安装kvm虚拟机
  • # Maven错误Error executing Maven
  • (2)leetcode 234.回文链表 141.环形链表
  • (31)对象的克隆
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (几何:六边形面积)编写程序,提示用户输入六边形的边长,然后显示它的面积。
  • (三)SvelteKit教程:layout 文件
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .cfg\.dat\.mak(持续补充)
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法