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

本文主要是介绍本地部署推理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

这篇关于本地部署推理TextDiffuser-2:释放语言模型用于文本渲染的力量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/786568

相关文章

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O