forked from PaddlePaddle/PaddleMIX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_to_image_generation_tgate.py
More file actions
241 lines (218 loc) · 7.32 KB
/
text_to_image_generation_tgate.py
File metadata and controls
241 lines (218 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import paddle
from tgate import TgateFLUXLoader, TgateSDXLLoader
from ppdiffusers import (
DPMSolverMultistepScheduler,
FluxPipeline,
StableDiffusionXLPipeline,
)
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of TGATE V2.")
parser.add_argument(
"--prompt",
type=str,
default=None,
help="the input prompts",
)
parser.add_argument(
"--image",
type=str,
default=None,
help="the dir of input image to generate video",
)
parser.add_argument(
"--saved_path",
type=str,
default=None,
required=True,
help="Path to save the generated results.",
)
parser.add_argument(
"--model",
type=str,
default="pixart",
help="[pixart_alpha,sdxl,lcm_sdxl,lcm_pixart_alpha,svd]",
)
parser.add_argument(
"--gate_step",
type=int,
default=10,
help="When re-using the cross-attention",
)
parser.add_argument(
"--sp_interval",
type=int,
default=5,
help="The time-step interval to cache self attention before gate_step (Semantics-Planning Phase).",
)
parser.add_argument(
"--fi_interval",
type=int,
default=1,
help="The time-step interval to cache self attention after gate_step (Fidelity-Improving Phase).",
)
parser.add_argument(
"--warm_up",
type=int,
default=2,
help="The time step to warm up the model inference",
)
parser.add_argument(
"--inference_step",
type=int,
default=25,
help="total inference steps",
)
parser.add_argument(
"--deepcache",
action="store_true",
default=False,
help="do deep cache",
)
parser.add_argument(
"--seed",
type=int,
default=None,
help="Random seed for generation. Set for reproducible results.",
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
os.makedirs(args.saved_path, exist_ok=True)
if args.prompt:
saved_path = os.path.join(args.saved_path, "test.png")
elif args.image:
saved_path = os.path.join(args.saved_path, "test.mp4")
# Create generator if seed is provided
generator = None
if args.seed is not None:
generator = paddle.Generator().manual_seed(args.seed)
if args.model == "sdxl":
pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
paddle_dtype=paddle.float16,
variant="fp16",
)
pipe = TgateSDXLLoader(pipe)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
image = pipe.tgate(
prompt=args.prompt,
gate_step=args.gate_step,
sp_interval=args.sp_interval if not args.deepcache else 1,
fi_interval=args.fi_interval,
warm_up=args.warm_up if not args.deepcache else 0,
num_inference_steps=args.inference_step,
generator=generator,
).images[0]
image.save(saved_path)
# elif args.model == "lcm_sdxl":
# unet = UNet2DConditionModel.from_pretrained(
# "latent-consistency/lcm-sdxl",
# paddle_dtype=paddle.float16,
# variant="fp16",
# )
# pipe = StableDiffusionXLPipeline.from_pretrained(
# "stabilityai/stable-diffusion-xl-base-1.0",
# unet=unet,
# paddle_dtype=paddle.float16,
# variant="fp16",
# )
# pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
# pipe = TgateSDXLLoader(pipe)
# image = pipe.tgate(
# prompt=args.prompt,
# gate_step=args.gate_step,
# sp_interval=1,
# fi_interval=args.fi_interval,
# warm_up=0,
# num_inference_steps=args.inference_step,
# lcm=True,
# generator=generator,
# ).images[0]
# image.save(saved_path)
elif args.model == "flux":
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", paddle_dtype=paddle.float16)
pipe = TgateFLUXLoader(pipe)
image = pipe.tgate(
prompt=args.prompt,
height=1024,
width=1024,
gate_step=args.gate_step,
sp_interval=args.sp_interval,
fi_interval=args.fi_interval,
warm_up=args.warm_up,
num_inference_steps=args.inference_step,
generator=generator,
).images[0]
image.save(saved_path)
# elif args.model == "pixart_alpha":
# pipe = PixArtAlphaPipeline.from_pretrained(
# "PixArt-alpha/PixArt-XL-2-1024-MS",
# paddle_dtype=paddle.float16,
# )
# pipe = TgatePixArtAlphaLoader(pipe)
# image = pipe.tgate(
# prompt=args.prompt,
# gate_step=args.gate_step,
# sp_interval=args.sp_interval,
# fi_interval=args.fi_interval,
# warm_up=args.warm_up,
# num_inference_steps=args.inference_step,
# generator=generator,
# ).images[0]
# image.save(saved_path)
# elif args.model == "lcm_pixart":
# pipe = PixArtAlphaPipeline.from_pretrained(
# "PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
# paddle_dtype=paddle.float16,
# )
# pipe = TgatePixArtAlphaLoader(pipe)
# image = pipe.tgate(
# args.prompt,
# gate_step=args.gate_step,
# sp_interval=1,
# fi_interval=args.fi_interval,
# warm_up=0,
# num_inference_steps=args.inference_step,
# lcm=True,
# guidance_scale=0.0,
# generator=generator,
# ).images[0]
# image.save(saved_path)
# elif args.model == 'svd':
# pipe = StableVideoDiffusionPipeline.from_pretrained(
# "stabilityai/stable-video-diffusion-img2vid-xt",
# paddle_dtype=paddle.float16,
# variant="fp16",
# )
# pipe = TgateSVDLoader(pipe)
# image = load_image(args.image)
# frames = pipe.tgate(
# image,
# gate_step=args.gate_step,
# num_inference_steps=args.inference_step,
# warm_up=args.warm_up,
# sp_interval=args.sp_interval,
# fi_interval=args.fi_interval,
# num_frames=25,
# decode_chunk_size=8,
# generator=generator,
# ).frames[0]
# export_to_video(frames, saved_path, fps=7)
else:
raise Exception("Please sepcify the model name!")