本地部署推理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

相关文章

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI