-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrain-lora.py
More file actions
248 lines (216 loc) · 7.75 KB
/
train-lora.py
File metadata and controls
248 lines (216 loc) · 7.75 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
242
243
244
245
246
247
import os
import sys
import argparse
from dataclasses import dataclass
# Add the src directory to the Python path if it's not already there
src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "src"))
if src_path not in sys.path:
sys.path.insert(0, src_path)
import torch
from diffusers import AutoencoderTiny, StableDiffusionPipeline, StableDiffusionXLPipeline
from diffusers.utils import load_image
from streamdiffusion import StreamDiffusion
from streamdiffusion.image_utils import postprocess_image
from streamdiffusion.acceleration.tensorrt import accelerate_with_tensorrt
@dataclass
class LoraSpec:
"""Represents a LoRA with its path and optional weight."""
path: str
weight: float = 1.0
@classmethod
def parse(cls, spec: str) -> "LoraSpec":
"""
Parse a LoRA specification string.
Supports formats:
- "path/to/lora.safetensors" (weight defaults to 1.0)
- "path/to/lora.safetensors:0.85" (explicit weight)
"""
if ":" in spec:
# Check if the last colon separates a weight value
last_colon = spec.rfind(":")
potential_weight = spec[last_colon + 1:]
try:
weight = float(potential_weight)
path = spec[:last_colon]
return cls(path=path, weight=weight)
except ValueError:
# Not a valid weight, treat entire string as path
pass
return cls(path=spec, weight=1.0)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Train/accelerate LoRA models with TensorRT for StreamDiffusion",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s --model SimianLuo/LCM_Dreamshaper_v7 --output ./engines
%(prog)s --type sdxl --model stabilityai/stable-diffusion-xl-base-1.0 --output ./engines
%(prog)s --model ./my_model --lora style.safetensors --lora detail.safetensors:0.5
%(prog)s --model ./model --min-batch 1 --max-batch 4 --min-resolution 512 --max-resolution 1024
"""
)
# Model configuration
parser.add_argument(
"-t", "--type",
choices=["sd15", "sdxl"],
default="sd15",
help="Model type: sd15 (Stable Diffusion 1.5) or sdxl (default: sd15)"
)
parser.add_argument(
"-m", "--model",
default="SimianLuo/LCM_Dreamshaper_v7",
help="Model source path or HuggingFace model ID (default: SimianLuo/LCM_Dreamshaper_v7)"
)
parser.add_argument(
"-o", "--output",
default="./engines",
help="Output path for TensorRT engines (default: ./engines)"
)
# LoRA configuration
parser.add_argument(
"-l", "--lora",
action="append",
dest="loras",
default=[],
metavar="PATH[:WEIGHT]",
help="LoRA file to load. Can be specified multiple times. "
"Optional weight can be appended after colon (e.g., lora.safetensors:0.85)"
)
parser.add_argument(
"--lora-scale",
type=float,
default=2.5,
help="Global LoRA fusion scale (default: 2.5)"
)
# Batch size configuration
parser.add_argument(
"--min-batch",
type=int,
default=1,
help="Minimum batch size for TensorRT optimization (default: 1)"
)
parser.add_argument(
"--max-batch",
type=int,
default=2,
help="Maximum batch size for TensorRT optimization (default: 2)"
)
parser.add_argument(
"--opt-batch",
type=int,
default=2,
help="Optimal batch size for TensorRT optimization (default: 2)"
)
# Resolution configuration
parser.add_argument(
"--min-resolution",
type=int,
default=1024,
help="Minimum image resolution for TensorRT optimization (default: 1024)"
)
parser.add_argument(
"--max-resolution",
type=int,
default=1024,
help="Maximum image resolution for TensorRT optimization (default: 1024)"
)
parser.add_argument(
"--opt-height",
type=int,
default=None,
help="Optimal image height (default: same as max-resolution)"
)
parser.add_argument(
"--opt-width",
type=int,
default=None,
help="Optimal image width (default: same as max-resolution)"
)
args = parser.parse_args()
# Set defaults for optional resolution parameters
if args.opt_height is None:
args.opt_height = args.max_resolution
if args.opt_width is None:
args.opt_width = args.max_resolution
# Validate batch sizes
if args.min_batch > args.max_batch:
parser.error("--min-batch cannot be greater than --max-batch")
if args.opt_batch < args.min_batch or args.opt_batch > args.max_batch:
parser.error("--opt-batch must be between --min-batch and --max-batch")
# Validate resolutions
if args.min_resolution > args.max_resolution:
parser.error("--min-resolution cannot be greater than --max-resolution")
# Parse LoRA specifications
args.lora_specs = [LoraSpec.parse(lora) for lora in args.loras]
return args
args = parse_args()
if args.type == "sdxl":
pipe = StableDiffusionXLPipeline.from_pretrained(args.model).to(
device=torch.device("cuda"),
dtype=torch.float16,
)
else:
pipe = StableDiffusionPipeline.from_pretrained(args.model).to(
device=torch.device("cuda"),
dtype=torch.float16,
)
stream = StreamDiffusion(
pipe,
t_index_list=[30, 45],
torch_dtype=torch.float16,
cfg_type="none",
)
# Load LoRAs with their individual weights
for lora_spec in args.lora_specs:
stream.load_lora(lora_spec.path)
print(f"Loaded LoRA: {lora_spec.path} (weight: {lora_spec.weight})")
if len(args.lora_specs) > 0:
# Calculate per-LoRA weights by multiplying individual weights with global scale
lora_weights = [spec.weight for spec in args.lora_specs]
effective_scale = args.lora_scale
# If multiple LoRAs have different weights, we apply the average weighted by lora_scale
if len(set(lora_weights)) > 1:
print(f"Note: Multiple LoRAs with different weights detected.")
print(f"Individual weights: {lora_weights}, global scale: {args.lora_scale}")
stream.fuse_lora(
fuse_unet=True,
fuse_text_encoder=True,
lora_scale=effective_scale,
safe_fusing=False
)
if args.type == "sd15":
stream.vae = AutoencoderTiny.from_pretrained("madebyollin/taesd").to(
device=pipe.device, dtype=pipe.dtype
)
else:
stream.vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl").to(
device=pipe.device, dtype=pipe.dtype
)
engine_build_options = {
"opt_batch_size": args.opt_batch,
"min_image_resolution": args.min_resolution,
"max_image_resolution": args.max_resolution,
"opt_image_height": args.opt_height,
"opt_image_width": args.opt_width,
}
# Enable static shapes when all min/max values are equal
use_static_shapes = (
args.min_batch == args.max_batch and
args.min_resolution == args.max_resolution and
args.opt_height == args.opt_width == args.max_resolution
)
print(f"Building TensorRT engine with options:")
print(f" Batch size: min={args.min_batch}, opt={args.opt_batch}, max={args.max_batch}")
print(f" Resolution: min={args.min_resolution}, max={args.max_resolution}")
print(f" Optimal dimensions: {args.opt_width}x{args.opt_height}")
print(f" Static shapes: {use_static_shapes}")
print(f" Output: {args.output}")
stream = accelerate_with_tensorrt(
stream,
args.output,
min_batch_size=args.min_batch,
max_batch_size=args.max_batch,
engine_build_options=engine_build_options,
static_shapes=use_static_shapes,
# use_v2v=True, # Enable V2V for C++ testing
)