moyanwang
commited on
Commit
·
75c208c
1
Parent(s):
ddb8777
remove unuse code
Browse files- .gitignore +3 -0
- demo.py +3 -2
- lyraSD/muse_trt/models.py +1 -506
- output/sd-img2img-0.jpg +0 -0
- output/sd-text2img-0.jpg +0 -0
- output/text2img_demo.jpg +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.un~
|
| 2 |
+
*.pyc
|
| 3 |
+
__pycache__
|
demo.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
from lyraSD import LyraSD
|
| 2 |
|
| 3 |
t2imodel = LyraSD("text2img", "./sd1.5-engine")
|
| 4 |
-
t2imodel.inference(prompt="
|
| 5 |
|
| 6 |
|
| 7 |
from PIL import Image
|
| 8 |
i2imodel = LyraSD("img2img", "./sd1.5-engine")
|
| 9 |
demo_img = Image.open("output/text2img_demo.jpg")
|
| 10 |
-
i2imodel.inference(prompt="
|
|
|
|
| 11 |
|
|
|
|
| 1 |
from lyraSD import LyraSD
|
| 2 |
|
| 3 |
t2imodel = LyraSD("text2img", "./sd1.5-engine")
|
| 4 |
+
t2imodel.inference(prompt="A fantasy landscape, trending on artstation", use_super=True)
|
| 5 |
|
| 6 |
|
| 7 |
from PIL import Image
|
| 8 |
i2imodel = LyraSD("img2img", "./sd1.5-engine")
|
| 9 |
demo_img = Image.open("output/text2img_demo.jpg")
|
| 10 |
+
i2imodel.inference(prompt="A fantasy landscape, trending on artstation",
|
| 11 |
+
image=demo_img)
|
| 12 |
|
lyraSD/muse_trt/models.py
CHANGED
|
@@ -259,44 +259,6 @@ class VAEEncoder(BaseModel):
|
|
| 259 |
batch_size, image_height, image_width)
|
| 260 |
return torch.randn(batch_size, 3, image_height, image_width, dtype=torch.float32, device=self.device)
|
| 261 |
|
| 262 |
-
def optimize(self, onnx_graph, minimal_optimization=False):
|
| 263 |
-
enable_optimization = not minimal_optimization
|
| 264 |
-
|
| 265 |
-
# Decompose InstanceNormalization into primitive Ops
|
| 266 |
-
bRemoveInstanceNorm = enable_optimization
|
| 267 |
-
# Remove Cast Node to optimize Attention block
|
| 268 |
-
bRemoveCastNode = enable_optimization
|
| 269 |
-
# Insert GroupNormalization Plugin
|
| 270 |
-
bGroupNormPlugin = enable_optimization
|
| 271 |
-
|
| 272 |
-
opt = Optimizer(onnx_graph, verbose=self.verbose)
|
| 273 |
-
opt.info('VAE Encoder: original')
|
| 274 |
-
|
| 275 |
-
if bRemoveInstanceNorm:
|
| 276 |
-
num_instancenorm_replaced = opt.decompose_instancenorms()
|
| 277 |
-
opt.info('VAE Encoder: replaced ' +
|
| 278 |
-
str(num_instancenorm_replaced)+' InstanceNorms')
|
| 279 |
-
|
| 280 |
-
if bRemoveCastNode:
|
| 281 |
-
num_casts_removed = opt.remove_casts()
|
| 282 |
-
opt.info('VAE Encoder: removed '+str(num_casts_removed)+' casts')
|
| 283 |
-
|
| 284 |
-
opt.cleanup()
|
| 285 |
-
opt.info('VAE Encoder: cleanup')
|
| 286 |
-
opt.fold_constants()
|
| 287 |
-
opt.info('VAE Encoder: fold constants')
|
| 288 |
-
opt.infer_shapes()
|
| 289 |
-
opt.info('VAE Encoder: shape inference')
|
| 290 |
-
|
| 291 |
-
if bGroupNormPlugin:
|
| 292 |
-
num_groupnorm_inserted = opt.insert_groupnorm_plugin()
|
| 293 |
-
opt.info('VAE Encoder: inserted '+str(num_groupnorm_inserted) +
|
| 294 |
-
' GroupNorm plugins')
|
| 295 |
-
|
| 296 |
-
onnx_opt_graph = opt.cleanup(return_onnx=True)
|
| 297 |
-
opt.info('VAE Encoder: final')
|
| 298 |
-
return onnx_opt_graph
|
| 299 |
-
|
| 300 |
|
| 301 |
class VAEDecoder(BaseModel):
|
| 302 |
def get_model(self):
|
|
@@ -345,471 +307,4 @@ class VAEDecoder(BaseModel):
|
|
| 345 |
def get_sample_input(self, batch_size, image_height, image_width):
|
| 346 |
latent_height, latent_width = self.check_dims(
|
| 347 |
batch_size, image_height, image_width)
|
| 348 |
-
return torch.randn(batch_size, 4, latent_height, latent_width, dtype=torch.float32, device=self.device)
|
| 349 |
-
|
| 350 |
-
def optimize(self, onnx_graph, minimal_optimization=False):
|
| 351 |
-
enable_optimization = not minimal_optimization
|
| 352 |
-
|
| 353 |
-
# Decompose InstanceNormalization into primitive Ops
|
| 354 |
-
bRemoveInstanceNorm = enable_optimization
|
| 355 |
-
# Remove Cast Node to optimize Attention block
|
| 356 |
-
bRemoveCastNode = enable_optimization
|
| 357 |
-
# Insert GroupNormalization Plugin
|
| 358 |
-
bGroupNormPlugin = enable_optimization
|
| 359 |
-
|
| 360 |
-
opt = Optimizer(onnx_graph, verbose=self.verbose)
|
| 361 |
-
opt.info('VAE Decoder: original')
|
| 362 |
-
|
| 363 |
-
if bRemoveInstanceNorm:
|
| 364 |
-
num_instancenorm_replaced = opt.decompose_instancenorms()
|
| 365 |
-
opt.info('VAE Decoder: replaced ' +
|
| 366 |
-
str(num_instancenorm_replaced)+' InstanceNorms')
|
| 367 |
-
|
| 368 |
-
if bRemoveCastNode:
|
| 369 |
-
num_casts_removed = opt.remove_casts()
|
| 370 |
-
opt.info('VAE Decoder: removed '+str(num_casts_removed)+' casts')
|
| 371 |
-
|
| 372 |
-
opt.cleanup()
|
| 373 |
-
opt.info('VAE Decoder: cleanup')
|
| 374 |
-
opt.fold_constants()
|
| 375 |
-
opt.info('VAE Decoder: fold constants')
|
| 376 |
-
opt.infer_shapes()
|
| 377 |
-
opt.info('VAE Decoder: shape inference')
|
| 378 |
-
|
| 379 |
-
if bGroupNormPlugin:
|
| 380 |
-
num_groupnorm_inserted = opt.insert_groupnorm_plugin()
|
| 381 |
-
opt.info('VAE Decoder: inserted '+str(num_groupnorm_inserted) +
|
| 382 |
-
' GroupNorm plugins')
|
| 383 |
-
|
| 384 |
-
onnx_opt_graph = opt.cleanup(return_onnx=True)
|
| 385 |
-
opt.info('VAE Decoder: final')
|
| 386 |
-
return onnx_opt_graph
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
class SuperModelX4(nn.Module):
|
| 390 |
-
def __init__(self, model_dir, scale=4, pre_pad=0):
|
| 391 |
-
super().__init__()
|
| 392 |
-
self.scale = scale
|
| 393 |
-
self.pre_pad = pre_pad
|
| 394 |
-
|
| 395 |
-
rrdb = RealESRGAN(model_dir=model_dir,
|
| 396 |
-
model_name="RealESRGAN_x4plus_anime_6B").upsampler.model
|
| 397 |
-
self.rrdb = rrdb.eval()
|
| 398 |
-
|
| 399 |
-
def forward(self, x):
|
| 400 |
-
x = x / 255.
|
| 401 |
-
x = F.pad(x, (0, self.pre_pad, 0, self.pre_pad), 'reflect')
|
| 402 |
-
x = self.rrdb(x)
|
| 403 |
-
_, _, h, w = x.size()
|
| 404 |
-
x = x[:, :, 0:h-self.pre_pad * self.scale, 0:w-self.pre_pad*self.scale]
|
| 405 |
-
x = x.clamp(0, 1)
|
| 406 |
-
x = (x * 255).round()
|
| 407 |
-
return x
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
class SuperResX4():
|
| 411 |
-
def __init__(
|
| 412 |
-
self,
|
| 413 |
-
local_model_path=None,
|
| 414 |
-
fp16=True,
|
| 415 |
-
device='cuda',
|
| 416 |
-
verbose=True,
|
| 417 |
-
max_batch_size=8
|
| 418 |
-
):
|
| 419 |
-
self.fp16 = fp16
|
| 420 |
-
self.device = device
|
| 421 |
-
self.verbose = verbose
|
| 422 |
-
self.local_model_path = local_model_path
|
| 423 |
-
|
| 424 |
-
# Defaults
|
| 425 |
-
self.min_batch = 1
|
| 426 |
-
self.max_batch = max_batch_size
|
| 427 |
-
self.min_height = 64
|
| 428 |
-
self.max_height = 640
|
| 429 |
-
self.min_width = 64
|
| 430 |
-
self.max_width = 640
|
| 431 |
-
|
| 432 |
-
def get_model(self):
|
| 433 |
-
model = SuperModelX4(self.local_model_path, scale=4, pre_pad=0).to(device=self.device)
|
| 434 |
-
if self.fp16:
|
| 435 |
-
model = model.half()
|
| 436 |
-
return model
|
| 437 |
-
|
| 438 |
-
def get_input_names(self):
|
| 439 |
-
return ['input_image']
|
| 440 |
-
|
| 441 |
-
def get_output_names(self):
|
| 442 |
-
return ['output_image']
|
| 443 |
-
|
| 444 |
-
def get_dynamic_axes(self):
|
| 445 |
-
return {
|
| 446 |
-
'input_image': {0: 'B', },
|
| 447 |
-
'output_image': {0: 'B', }
|
| 448 |
-
}
|
| 449 |
-
|
| 450 |
-
def check_dims(self, batch_size, image_height, image_width):
|
| 451 |
-
assert batch_size >= self.min_batch and batch_size <= self.max_batch
|
| 452 |
-
return (image_height, image_width)
|
| 453 |
-
|
| 454 |
-
def get_minmax_dims(self, batch_size, image_height, image_width, static_batch, static_shape):
|
| 455 |
-
min_batch = batch_size if static_batch else self.min_batch
|
| 456 |
-
max_batch = batch_size if static_batch else self.max_batch
|
| 457 |
-
min_image_height = image_height if static_shape else self.min_height
|
| 458 |
-
max_image_height = image_height if static_shape else self.max_height
|
| 459 |
-
min_image_width = image_width if static_shape else self.min_width
|
| 460 |
-
max_image_width = image_width if static_shape else self.max_width
|
| 461 |
-
return (min_batch, max_batch, min_image_height, max_image_height, min_image_width, max_image_width)
|
| 462 |
-
|
| 463 |
-
def get_input_profile(self, batch_size, image_height, image_width, static_batch, static_shape):
|
| 464 |
-
image_height, image_width = self.check_dims(
|
| 465 |
-
batch_size, image_height, image_width)
|
| 466 |
-
min_batch, max_batch, min_image_height, max_image_height, min_image_width, max_image_width = \
|
| 467 |
-
self.get_minmax_dims(batch_size, image_height,
|
| 468 |
-
image_width, static_batch, static_shape)
|
| 469 |
-
return {
|
| 470 |
-
'input_image': [(min_batch, 3, min_image_height, min_image_width), (batch_size, 3, image_height, image_width), (max_batch, 3, max_image_height, max_image_width)]
|
| 471 |
-
}
|
| 472 |
-
|
| 473 |
-
def get_shape_dict(self, batch_size, image_height, image_width):
|
| 474 |
-
image_height, image_width = self.check_dims(
|
| 475 |
-
batch_size, image_height, image_width)
|
| 476 |
-
return {
|
| 477 |
-
'input_image': (batch_size, 3, image_height, image_width),
|
| 478 |
-
'output_image': (batch_size, 3, image_height*4, image_width*4),
|
| 479 |
-
}
|
| 480 |
-
|
| 481 |
-
def get_sample_input(self, batch_size, image_height, image_width):
|
| 482 |
-
dtype = torch.float16 if self.fp16 else torch.float32
|
| 483 |
-
image_height, image_width = self.check_dims(
|
| 484 |
-
batch_size, image_height, image_width)
|
| 485 |
-
return torch.randn(batch_size, 3, image_height, image_width, dtype=dtype, device=self.device)
|
| 486 |
-
|
| 487 |
-
def optimize(self, onnx_graph, minimal_optimization=False):
|
| 488 |
-
enable_optimization = not minimal_optimization
|
| 489 |
-
|
| 490 |
-
# Decompose InstanceNormalization into primitive Ops
|
| 491 |
-
bRemoveInstanceNorm = enable_optimization
|
| 492 |
-
# Remove Cast Node to optimize Attention block
|
| 493 |
-
bRemoveCastNode = enable_optimization
|
| 494 |
-
# Insert GroupNormalization Plugin
|
| 495 |
-
bGroupNormPlugin = enable_optimization
|
| 496 |
-
|
| 497 |
-
opt = Optimizer(onnx_graph, verbose=self.verbose)
|
| 498 |
-
opt.info('SuperX4: original')
|
| 499 |
-
|
| 500 |
-
if bRemoveInstanceNorm:
|
| 501 |
-
num_instancenorm_replaced = opt.decompose_instancenorms()
|
| 502 |
-
opt.info('SuperX4: replaced ' +
|
| 503 |
-
str(num_instancenorm_replaced)+' InstanceNorms')
|
| 504 |
-
|
| 505 |
-
if bRemoveCastNode:
|
| 506 |
-
num_casts_removed = opt.remove_casts()
|
| 507 |
-
opt.info('SuperX4: removed '+str(num_casts_removed)+' casts')
|
| 508 |
-
|
| 509 |
-
opt.cleanup()
|
| 510 |
-
opt.info('SuperX4: cleanup')
|
| 511 |
-
opt.fold_constants()
|
| 512 |
-
opt.info('SuperX4: fold constants')
|
| 513 |
-
opt.infer_shapes()
|
| 514 |
-
opt.info('SuperX4: shape inference')
|
| 515 |
-
|
| 516 |
-
if bGroupNormPlugin:
|
| 517 |
-
num_groupnorm_inserted = opt.insert_groupnorm_plugin()
|
| 518 |
-
opt.info('SuperX4: inserted '+str(num_groupnorm_inserted) +
|
| 519 |
-
' GroupNorm plugins')
|
| 520 |
-
|
| 521 |
-
onnx_opt_graph = opt.cleanup(return_onnx=True)
|
| 522 |
-
opt.info('SuperX4: final')
|
| 523 |
-
return onnx_opt_graph
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
class FusedControlNetModule(nn.Module):
|
| 527 |
-
def __init__(self, base_model_dir, control_model_dir, fp16=True) -> None:
|
| 528 |
-
super().__init__()
|
| 529 |
-
self.device = 'cuda:0'
|
| 530 |
-
self.fp16 = fp16
|
| 531 |
-
model_opts = {'revision': 'fp16',
|
| 532 |
-
'torch_dtype': torch.float16} if self.fp16 else {}
|
| 533 |
-
self.base = UNet2DConditionModel.from_pretrained(
|
| 534 |
-
base_model_dir, subfolder="unet",
|
| 535 |
-
**model_opts
|
| 536 |
-
).eval().to(self.device)
|
| 537 |
-
self.control = ControlNetModel.from_pretrained(
|
| 538 |
-
control_model_dir,
|
| 539 |
-
**model_opts
|
| 540 |
-
).eval().to(self.device)
|
| 541 |
-
|
| 542 |
-
def forward(self, sample, timestep, encoder_hidden_states, controlnet_cond):
|
| 543 |
-
controlnet_conditioning_scale: float = 1.0
|
| 544 |
-
down_block_res_samples, mid_block_res_sample = self.control(
|
| 545 |
-
sample,
|
| 546 |
-
timestep,
|
| 547 |
-
encoder_hidden_states=encoder_hidden_states,
|
| 548 |
-
controlnet_cond=controlnet_cond,
|
| 549 |
-
return_dict=False,
|
| 550 |
-
)
|
| 551 |
-
|
| 552 |
-
down_block_res_samples = [
|
| 553 |
-
down_block_res_sample * controlnet_conditioning_scale
|
| 554 |
-
for down_block_res_sample in down_block_res_samples
|
| 555 |
-
]
|
| 556 |
-
mid_block_res_sample *= controlnet_conditioning_scale
|
| 557 |
-
|
| 558 |
-
# predict the noise residual
|
| 559 |
-
noise_pred = self.base(
|
| 560 |
-
sample,
|
| 561 |
-
timestep,
|
| 562 |
-
encoder_hidden_states=encoder_hidden_states,
|
| 563 |
-
down_block_additional_residuals=down_block_res_samples,
|
| 564 |
-
mid_block_additional_residual=mid_block_res_sample,
|
| 565 |
-
).sample
|
| 566 |
-
|
| 567 |
-
return noise_pred
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
class FusedControlNet(BaseModel):
|
| 571 |
-
def __init__(self, local_model_path=None, controlnet_model_path=None, hf_token=None, text_maxlen=77,
|
| 572 |
-
embedding_dim=768, fp16=False, device='cuda', verbose=True, max_batch_size=16):
|
| 573 |
-
super().__init__(local_model_path, hf_token, text_maxlen, embedding_dim, fp16, device, verbose, max_batch_size)
|
| 574 |
-
# if controlnet_model_path is None:
|
| 575 |
-
# raise ValueError("Must give controlnet_model_path for FusedControlNet to load control net")
|
| 576 |
-
self.controlnet_model_path = controlnet_model_path
|
| 577 |
-
self.min_height = 256
|
| 578 |
-
self.max_height = 1024
|
| 579 |
-
self.min_width = 256
|
| 580 |
-
self.max_width = 1024
|
| 581 |
-
|
| 582 |
-
def get_minmax_dims(self, batch_size, image_height, image_width, static_batch, static_shape):
|
| 583 |
-
r = list(super().get_minmax_dims(batch_size, image_height, image_width, static_batch, static_shape))
|
| 584 |
-
min_height = image_height if static_shape else self.min_height
|
| 585 |
-
max_height = image_height if static_shape else self.max_height
|
| 586 |
-
min_width = image_width if static_shape else self.min_width
|
| 587 |
-
max_width = image_width if static_shape else self.max_width
|
| 588 |
-
r.extend([min_height, max_height, min_width, max_width])
|
| 589 |
-
return r
|
| 590 |
-
|
| 591 |
-
def get_model(self):
|
| 592 |
-
model = FusedControlNetModule(
|
| 593 |
-
base_model_dir=self.local_model_path,
|
| 594 |
-
control_model_dir=self.controlnet_model_path,
|
| 595 |
-
fp16=self.fp16
|
| 596 |
-
)
|
| 597 |
-
return model
|
| 598 |
-
|
| 599 |
-
def get_input_names(self):
|
| 600 |
-
return ['sample', 'timestep', 'encoder_hidden_states', 'controlnet_cond']
|
| 601 |
-
|
| 602 |
-
def get_output_names(self):
|
| 603 |
-
return ['latent']
|
| 604 |
-
|
| 605 |
-
def get_dynamic_axes(self):
|
| 606 |
-
return {
|
| 607 |
-
'sample': {0: '2B', 2: 'H', 3: 'W'},
|
| 608 |
-
'encoder_hidden_states': {0: '2B'},
|
| 609 |
-
'controlnet_cond': {0: '2B', 2: '8H', 3: '8W'}, # controlnet_cond is 8X sample and lantent
|
| 610 |
-
'latent': {0: '2B', 2: 'H', 3: 'W'}
|
| 611 |
-
}
|
| 612 |
-
|
| 613 |
-
def get_input_profile(self, batch_size, image_height, image_width, static_batch, static_shape):
|
| 614 |
-
latent_height, latent_width = self.check_dims(
|
| 615 |
-
batch_size, image_height, image_width)
|
| 616 |
-
min_batch, max_batch, min_latent_height, max_latent_height, min_latent_width, max_latent_width, min_height, max_height, min_width, max_width = \
|
| 617 |
-
self.get_minmax_dims(batch_size, image_height,
|
| 618 |
-
image_width, static_batch, static_shape)
|
| 619 |
-
return {
|
| 620 |
-
'sample': [(2*min_batch, 4, min_latent_height, min_latent_width), (2*batch_size, 4, latent_height, latent_width), (2*max_batch, 4, max_latent_height, max_latent_width)],
|
| 621 |
-
'encoder_hidden_states': [(2*min_batch, self.text_maxlen, self.embedding_dim), (2*batch_size, self.text_maxlen, self.embedding_dim), (2*max_batch, self.text_maxlen, self.embedding_dim)],
|
| 622 |
-
'controlnet_cond': [(2*min_batch, 3, min_height, min_width), (2*batch_size, 3, image_height, image_width), (2*max_batch, 3, max_height, max_width)]
|
| 623 |
-
}
|
| 624 |
-
|
| 625 |
-
def get_shape_dict(self, batch_size, image_height, image_width):
|
| 626 |
-
latent_height, latent_width = self.check_dims(
|
| 627 |
-
batch_size, image_height, image_width)
|
| 628 |
-
return {
|
| 629 |
-
'sample': (2*batch_size, 4, latent_height, latent_width),
|
| 630 |
-
'encoder_hidden_states': (2*batch_size, self.text_maxlen, self.embedding_dim),
|
| 631 |
-
'controlnet_cond': (2*batch_size, 3, image_height, image_width),
|
| 632 |
-
'latent': (2*batch_size, 4, latent_height, latent_width)
|
| 633 |
-
}
|
| 634 |
-
|
| 635 |
-
def get_sample_input(self, batch_size, image_height, image_width):
|
| 636 |
-
latent_height, latent_width = self.check_dims(
|
| 637 |
-
batch_size, image_height, image_width)
|
| 638 |
-
dtype = torch.float16 if self.fp16 else torch.float32
|
| 639 |
-
return (
|
| 640 |
-
torch.randn(2*batch_size, 4, latent_height, latent_width,
|
| 641 |
-
dtype=torch.float32, device=self.device), # sample
|
| 642 |
-
torch.tensor([1.], dtype=torch.float32, device=self.device), # timestep
|
| 643 |
-
torch.randn(2*batch_size, self.text_maxlen, # encoder_hidden_states
|
| 644 |
-
self.embedding_dim, dtype=dtype, device=self.device),
|
| 645 |
-
torch.randn(2*batch_size, 3, image_height, image_width,
|
| 646 |
-
dtype=torch.float32, device=self.device) # controlnet_cond
|
| 647 |
-
)
|
| 648 |
-
|
| 649 |
-
def optimize(self, onnx_graph, minimal_optimization=False):
|
| 650 |
-
class_name = self.__class__.__name__
|
| 651 |
-
|
| 652 |
-
enable_optimization = not minimal_optimization
|
| 653 |
-
|
| 654 |
-
# Decompose InstanceNormalization into primitive Ops
|
| 655 |
-
bRemoveInstanceNorm = enable_optimization
|
| 656 |
-
# Remove Cast Node to optimize Attention block
|
| 657 |
-
bRemoveCastNode = enable_optimization
|
| 658 |
-
# Remove parallel Swish ops
|
| 659 |
-
bRemoveParallelSwish = enable_optimization
|
| 660 |
-
# Adjust the bias to be the second input to the Add ops
|
| 661 |
-
bAdjustAddNode = enable_optimization
|
| 662 |
-
# Change Resize node to take size instead of scale
|
| 663 |
-
bResizeFix = enable_optimization
|
| 664 |
-
|
| 665 |
-
# Common override for disabling all plugins below
|
| 666 |
-
bDisablePlugins = minimal_optimization
|
| 667 |
-
# Use multi-head attention Plugin
|
| 668 |
-
bMHAPlugin = True
|
| 669 |
-
# Use multi-head cross attention Plugin
|
| 670 |
-
bMHCAPlugin = True
|
| 671 |
-
# Insert GroupNormalization Plugin
|
| 672 |
-
bGroupNormPlugin = True
|
| 673 |
-
# Insert LayerNormalization Plugin
|
| 674 |
-
bLayerNormPlugin = True
|
| 675 |
-
# Insert Split+GeLU Plugin
|
| 676 |
-
bSplitGeLUPlugin = True
|
| 677 |
-
# Replace BiasAdd+ResidualAdd+SeqLen2Spatial with plugin
|
| 678 |
-
bSeqLen2SpatialPlugin = True
|
| 679 |
-
|
| 680 |
-
opt = Optimizer(onnx_graph, verbose=self.verbose)
|
| 681 |
-
opt.info(f'{class_name}: original')
|
| 682 |
-
|
| 683 |
-
if bRemoveInstanceNorm:
|
| 684 |
-
num_instancenorm_replaced = opt.decompose_instancenorms()
|
| 685 |
-
opt.info(f'{class_name}: replaced ' +
|
| 686 |
-
str(num_instancenorm_replaced)+' InstanceNorms')
|
| 687 |
-
|
| 688 |
-
if bRemoveCastNode:
|
| 689 |
-
num_casts_removed = opt.remove_casts()
|
| 690 |
-
opt.info(f'{class_name}: removed '+str(num_casts_removed)+' casts')
|
| 691 |
-
|
| 692 |
-
if bRemoveParallelSwish:
|
| 693 |
-
num_parallel_swish_removed = opt.remove_parallel_swish()
|
| 694 |
-
opt.info(f'{class_name}: removed ' +
|
| 695 |
-
str(num_parallel_swish_removed)+' parallel swish ops')
|
| 696 |
-
|
| 697 |
-
if bAdjustAddNode:
|
| 698 |
-
num_adjust_add = opt.adjustAddNode()
|
| 699 |
-
opt.info(f'{class_name}: adjusted '+str(num_adjust_add)+' adds')
|
| 700 |
-
|
| 701 |
-
if bResizeFix:
|
| 702 |
-
num_resize_fix = opt.resize_fix()
|
| 703 |
-
opt.info(f'{class_name}: fixed '+str(num_resize_fix)+' resizes')
|
| 704 |
-
|
| 705 |
-
opt.cleanup()
|
| 706 |
-
opt.info(f'{class_name}: cleanup')
|
| 707 |
-
opt.fold_constants()
|
| 708 |
-
opt.info(f'{class_name}: fold constants')
|
| 709 |
-
opt.infer_shapes()
|
| 710 |
-
opt.info(f'{class_name}: shape inference')
|
| 711 |
-
|
| 712 |
-
num_heads = 8
|
| 713 |
-
if bMHAPlugin and not bDisablePlugins:
|
| 714 |
-
num_fmha_inserted = opt.insert_fmha_plugin(num_heads)
|
| 715 |
-
opt.info(f'{class_name}: inserted '+str(num_fmha_inserted)+' fMHA plugins')
|
| 716 |
-
|
| 717 |
-
if bMHCAPlugin and not bDisablePlugins:
|
| 718 |
-
props = cudart.cudaGetDeviceProperties(0)[1]
|
| 719 |
-
sm = props.major * 10 + props.minor
|
| 720 |
-
num_fmhca_inserted = opt.insert_fmhca_plugin(num_heads, sm)
|
| 721 |
-
opt.info(f'{class_name}: inserted '+str(num_fmhca_inserted)+' fMHCA plugins')
|
| 722 |
-
|
| 723 |
-
if bGroupNormPlugin and not bDisablePlugins:
|
| 724 |
-
num_groupnorm_inserted = opt.insert_groupnorm_plugin()
|
| 725 |
-
opt.info(f'{class_name}: inserted '+str(num_groupnorm_inserted) +
|
| 726 |
-
' GroupNorm plugins')
|
| 727 |
-
|
| 728 |
-
if bLayerNormPlugin and not bDisablePlugins:
|
| 729 |
-
num_layernorm_inserted = opt.insert_layernorm_plugin()
|
| 730 |
-
opt.info(f'{class_name}: inserted '+str(num_layernorm_inserted) +
|
| 731 |
-
' LayerNorm plugins')
|
| 732 |
-
|
| 733 |
-
if bSplitGeLUPlugin and not bDisablePlugins:
|
| 734 |
-
num_splitgelu_inserted = opt.insert_splitgelu_plugin()
|
| 735 |
-
opt.info(f'{class_name}: inserted '+str(num_splitgelu_inserted) +
|
| 736 |
-
' SplitGeLU plugins')
|
| 737 |
-
|
| 738 |
-
if bSeqLen2SpatialPlugin and not bDisablePlugins:
|
| 739 |
-
num_seq2spatial_inserted = opt.insert_seq2spatial_plugin()
|
| 740 |
-
opt.info(f'{class_name}: inserted '+str(num_seq2spatial_inserted) +
|
| 741 |
-
' SeqLen2Spatial plugins')
|
| 742 |
-
|
| 743 |
-
onnx_opt_graph = opt.cleanup(return_onnx=True)
|
| 744 |
-
opt.info(f'{class_name}: final')
|
| 745 |
-
return onnx_opt_graph
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
class ControlNetModule(nn.Module):
|
| 749 |
-
def __init__(self, control_model_dir, fp16=True) -> None:
|
| 750 |
-
super().__init__()
|
| 751 |
-
self.device = 'cuda:0'
|
| 752 |
-
self.fp16 = fp16
|
| 753 |
-
model_opts = {'revision': 'fp16',
|
| 754 |
-
'torch_dtype': torch.float16} if self.fp16 else {}
|
| 755 |
-
self.control = ControlNetModel.from_pretrained(
|
| 756 |
-
control_model_dir,
|
| 757 |
-
**model_opts
|
| 758 |
-
).eval().to(self.device)
|
| 759 |
-
|
| 760 |
-
def forward(self, sample, timestep, encoder_hidden_states, controlnet_cond):
|
| 761 |
-
controlnet_conditioning_scale: float = 1.0
|
| 762 |
-
down_block_res_samples, mid_block_res_sample = self.control(
|
| 763 |
-
sample,
|
| 764 |
-
timestep,
|
| 765 |
-
encoder_hidden_states=encoder_hidden_states,
|
| 766 |
-
controlnet_cond=controlnet_cond,
|
| 767 |
-
return_dict=False,
|
| 768 |
-
)
|
| 769 |
-
down_block_res_samples = [
|
| 770 |
-
down_block_res_sample * controlnet_conditioning_scale
|
| 771 |
-
for down_block_res_sample in down_block_res_samples
|
| 772 |
-
]
|
| 773 |
-
mid_block_res_sample *= controlnet_conditioning_scale
|
| 774 |
-
# @vane: currently, only retun mid_blocks_res_sample: (B, 1280, height//8//8, width//8//8)
|
| 775 |
-
# down_block_res_samples is a tensor tuple that length is 12.
|
| 776 |
-
# it will be flatten to 12 nodes if we return the down_block_res_samples
|
| 777 |
-
return mid_block_res_sample
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
class ControlNet(FusedControlNet):
|
| 781 |
-
def __init__(self, local_model_path=None, controlnet_model_path=None, hf_token=None, text_maxlen=77,
|
| 782 |
-
embedding_dim=768, fp16=False, device='cuda', verbose=True, max_batch_size=16):
|
| 783 |
-
super().__init__(local_model_path, controlnet_model_path, hf_token,
|
| 784 |
-
text_maxlen, embedding_dim, fp16, device, verbose, max_batch_size)
|
| 785 |
-
|
| 786 |
-
def get_model(self):
|
| 787 |
-
model = ControlNetModule(
|
| 788 |
-
control_model_dir=self.controlnet_model_path,
|
| 789 |
-
fp16=self.fp16
|
| 790 |
-
)
|
| 791 |
-
return model
|
| 792 |
-
|
| 793 |
-
def get_input_names(self):
|
| 794 |
-
return ['sample', 'timestep', 'encoder_hidden_states', 'controlnet_cond']
|
| 795 |
-
|
| 796 |
-
def get_output_names(self):
|
| 797 |
-
return ['mids']
|
| 798 |
-
|
| 799 |
-
def get_dynamic_axes(self):
|
| 800 |
-
return {
|
| 801 |
-
'sample': {0: '2B', 2: '8H', 3: '8W'},
|
| 802 |
-
'encoder_hidden_states': {0: '2B'},
|
| 803 |
-
'controlnet_cond': {0: '2B', 2: '16H', 3: '16W'},
|
| 804 |
-
'mids': {0: '2B', 2: 'H', 3: 'W'}
|
| 805 |
-
}
|
| 806 |
-
|
| 807 |
-
def get_shape_dict(self, batch_size, image_height, image_width):
|
| 808 |
-
latent_height, latent_width = self.check_dims(
|
| 809 |
-
batch_size, image_height, image_width)
|
| 810 |
-
return {
|
| 811 |
-
'sample': (2*batch_size, 4, latent_height, latent_width),
|
| 812 |
-
'encoder_hidden_states': (2*batch_size, self.text_maxlen, self.embedding_dim),
|
| 813 |
-
'controlnet_cond': (2*batch_size, 3, image_height, image_width),
|
| 814 |
-
'mids': (2*batch_size, 1280, latent_height//8, latent_width//8)
|
| 815 |
-
}
|
|
|
|
| 259 |
batch_size, image_height, image_width)
|
| 260 |
return torch.randn(batch_size, 3, image_height, image_width, dtype=torch.float32, device=self.device)
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
class VAEDecoder(BaseModel):
|
| 264 |
def get_model(self):
|
|
|
|
| 307 |
def get_sample_input(self, batch_size, image_height, image_width):
|
| 308 |
latent_height, latent_width = self.check_dims(
|
| 309 |
batch_size, image_height, image_width)
|
| 310 |
+
return torch.randn(batch_size, 4, latent_height, latent_width, dtype=torch.float32, device=self.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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