Spaces:
Running
on
Zero
Running
on
Zero
File size: 73,408 Bytes
934bde2 |
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 |
# Copyright 2023 The HuggingFace Team. 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 math
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union
import datetime
import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import functional as F
from torch.nn.modules.normalization import GroupNorm
import os
from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.models.attention_processor import AttentionProcessor
from diffusers.utils import USE_PEFT_BACKEND
from diffusers.models.autoencoders import AutoencoderKL
from diffusers.models.lora import LoRACompatibleConv
from diffusers.models.modeling_utils import ModelMixin
from diffusers.models.unets.unet_2d_blocks import (
CrossAttnDownBlock2D,
CrossAttnUpBlock2D,
DownBlock2D,
Downsample2D,
ResnetBlock2D,
Transformer2DModel,
UpBlock2D,
Upsample2D,
)
from diffusers.models.unets.unet_2d_condition import UNet2DConditionModel
from diffusers.utils import BaseOutput, logging
import numpy as np
from PIL import Image
from safetensors import safe_open
from .attention_autoencoder import AttentionAutoencoder, PositionalEncoding
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
@dataclass
class ControlNetXSOutput(BaseOutput):
"""
The output of [`ControlNetXSModel`].
Args:
sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
The output of the `ControlNetXSModel`. Unlike `ControlNetOutput` this is NOT to be added to the base model
output, but is already the final output.
"""
sample: torch.FloatTensor = None
# copied from diffusers.models.controlnet.ControlNetConditioningEmbedding
class ControlNetConditioningEmbedding(nn.Module):
"""
Quoting from https://arxiv.org/abs/2302.05543: "Stable Diffusion uses a pre-processing method similar to VQ-GAN
[11] to convert the entire dataset of 512 × 512 images into smaller 64 × 64 “latent images” for stabilized
training. This requires ControlNets to convert image-based conditions to 64 × 64 feature space to match the
convolution size. We use a tiny network E(·) of four convolution layers with 4 × 4 kernels and 2 × 2 strides
(activated by ReLU, channels are 16, 32, 64, 128, initialized with Gaussian weights, trained jointly with the full
model) to encode image-space conditions ... into feature maps ..."
"""
def __init__(
self,
conditioning_embedding_channels: int,
conditioning_channels: int = 3,
block_out_channels: Tuple[int, ...] = (16, 32, 96, 256),
):
super().__init__()
self.conv_in = nn.Conv2d(conditioning_channels, block_out_channels[0], kernel_size=3, padding=1)
self.blocks = nn.ModuleList([])
for i in range(len(block_out_channels) - 1):
channel_in = block_out_channels[i]
channel_out = block_out_channels[i + 1]
self.blocks.append(nn.Conv2d(channel_in, channel_in, kernel_size=3, padding=1))
self.blocks.append(nn.Conv2d(channel_in, channel_out, kernel_size=3, padding=1, stride=2))
self.conv_out = zero_module(
nn.Conv2d(block_out_channels[-1], conditioning_embedding_channels, kernel_size=3, padding=1)
)
def forward(self, conditioning):
embedding = self.conv_in(conditioning)
embedding = F.silu(embedding)
for block in self.blocks:
embedding = block(embedding)
embedding = F.silu(embedding)
embedding = self.conv_out(embedding)
return embedding
class ControlNetConditioningEmbeddingBig(nn.Module):
def __init__(
self,
conditioning_embedding_channels: int,
conditioning_channels: int = 4,
block_out_channels: Tuple[int, ...] = (16, 32, 96, 256),
text_embed_dim: int = 768,
):
super().__init__()
self.conv_in = nn.Conv2d(conditioning_channels, block_out_channels[0], kernel_size=3, padding=1)
self.cross_attention = CrossAttention(block_out_channels[0], text_embed_dim)
# Encoder with increasing feature maps and more downsampling
self.encoder = nn.ModuleList([
nn.Conv2d(block_out_channels[0], 64, kernel_size=3, stride=2, padding=1),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1),
nn.Conv2d(256, 320, kernel_size=3, stride=2, padding=1),
nn.Conv2d(320, 512, kernel_size=3, stride=2, padding=1),
nn.Conv2d(512, 640, kernel_size=3, stride=2, padding=1),
])
# Global embedding processing
self.global_fc = nn.Linear(640, 640)
# Bottleneck
self.bottleneck_down = nn.Conv2d(640, 6, kernel_size=3, stride=1, padding=1)
self.bottleneck_up = nn.Conv2d(6, 320, kernel_size=3, stride=1, padding=1)
# Smaller decoder to get back to 320x64x64
self.decoder = nn.ModuleList([
nn.ConvTranspose2d(320, 320, kernel_size=4, stride=2, padding=1), # 4x4 -> 8x8
nn.ConvTranspose2d(320, 320, kernel_size=4, stride=2, padding=1), # 8x8 -> 16x16
nn.ConvTranspose2d(320, 320, kernel_size=4, stride=2, padding=1), # 16x16 -> 32x32
])
def forward(self, x, text_embeds):
x = self.conv_in(x)
x = self.cross_attention(x, text_embeds)
# Encoder
for encoder_layer in self.encoder:
x = encoder_layer(x)
x = F.relu(x)
# Global embedding processing
b, c, h, w = x.shape
x_flat = x.view(b, c, -1).mean(dim=2) # Global average pooling
x_global = self.global_fc(x_flat).view(b, c, 1, 1)
x = x + x_global.expand_as(x) # Add global features to local features
# Bottleneck
x = self.bottleneck_down(x)
x = self.bottleneck_up(x)
# Decoder
for decoder_layer in self.decoder:
x = decoder_layer(x)
x = F.relu(x)
#print(x.shape)
return x
class CrossAttention(nn.Module):
def __init__(self, dim, context_dim):
super().__init__()
self.to_q = nn.Conv2d(dim, dim, 1)
self.to_k = nn.Linear(context_dim, dim)
self.to_v = nn.Linear(context_dim, dim)
self.scale = dim ** -0.5
def forward(self, x, context):
b, c, h, w = x.shape
q = self.to_q(x).view(b, c, -1).permute(0, 2, 1) # (B, H*W, C)
k = self.to_k(context) # (B, T, C)
v = self.to_v(context) # (B, T, C)
attn = torch.matmul(q, k.transpose(-2, -1)) * self.scale # (B, H*W, T)
attn = attn.softmax(dim=-1)
out = torch.matmul(attn, v) # (B, H*W, C)
out = out.permute(0, 2, 1).view(b, c, h, w) # (B, C, H, W)
return out + x
def zero_module(module):
for p in module.parameters():
nn.init.zeros_(p)
return module
class StyleCodesModel(ModelMixin, ConfigMixin):
r"""
Based off ControlNet-XS
"""
@classmethod
def init_original(cls, base_model: UNet2DConditionModel, is_sdxl=True):
"""
Create a ControlNetXS model with the same parameters as in the original paper (https://github.com/vislearn/ControlNet-XS).
Parameters:
base_model (`UNet2DConditionModel`):
Base UNet model. Needs to be either StableDiffusion or StableDiffusion-XL.
is_sdxl (`bool`, defaults to `True`):
Whether passed `base_model` is a StableDiffusion-XL model.
"""
def get_dim_attn_heads(base_model: UNet2DConditionModel, size_ratio: float, num_attn_heads: int):
"""
Currently, diffusers can only set the dimension of attention heads (see https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 for why).
The original ControlNet-XS model, however, define the number of attention heads.
That's why compute the dimensions needed to get the correct number of attention heads.
"""
block_out_channels = [int(size_ratio * c) for c in base_model.config.block_out_channels]
dim_attn_heads = [math.ceil(c / num_attn_heads) for c in block_out_channels]
return dim_attn_heads
if is_sdxl:
return StyleCodesModel.from_unet(
base_model,
time_embedding_mix=0.95,
learn_embedding=True,
size_ratio=0.1,
conditioning_embedding_out_channels=(16, 32, 96, 256),
num_attention_heads=get_dim_attn_heads(base_model, 0.1, 64),
)
else:
return StyleCodesModel.from_unet(
base_model,
time_embedding_mix=1.0,
learn_embedding=True,
size_ratio=0.0125,
conditioning_embedding_out_channels=(16, 32, 96, 256),
num_attention_heads=get_dim_attn_heads(base_model, 0.0125, 8),
)
@classmethod
def _gather_subblock_sizes(cls, unet: UNet2DConditionModel, base_or_control: str):
"""To create correctly sized connections between base and control model, we need to know
the input and output channels of each subblock.
Parameters:
unet (`UNet2DConditionModel`):
Unet of which the subblock channels sizes are to be gathered.
base_or_control (`str`):
Needs to be either "base" or "control". If "base", decoder is also considered.
"""
if base_or_control not in ["base", "control"]:
raise ValueError("`base_or_control` needs to be either `base` or `control`")
channel_sizes = {"down": [], "mid": [], "up": []}
# input convolution
channel_sizes["down"].append((unet.conv_in.in_channels, unet.conv_in.out_channels))
# encoder blocks
for module in unet.down_blocks:
if isinstance(module, (CrossAttnDownBlock2D, DownBlock2D)):
for r in module.resnets:
channel_sizes["down"].append((r.in_channels, r.out_channels))
if module.downsamplers:
channel_sizes["down"].append(
(module.downsamplers[0].channels, module.downsamplers[0].out_channels)
)
else:
raise ValueError(f"Encountered unknown module of type {type(module)} while creating ControlNet-XS.")
# middle block
channel_sizes["mid"].append((unet.mid_block.resnets[0].in_channels, unet.mid_block.resnets[0].out_channels))
# decoder blocks
#if base_or_control == "base":
for module in unet.up_blocks:
if isinstance(module, (CrossAttnUpBlock2D, UpBlock2D)):
for r in module.resnets:
channel_sizes["up"].append((r.in_channels, r.out_channels))
else:
raise ValueError(
f"Encountered unknown module of type {type(module)} while creating ControlNet-XS."
)
return channel_sizes
def _make_colab_linear_layer(self, in_channels, out_channels):
# Create a Linear layer where in_features = in_channels + out_channels
#in_features = in_channels + out_channels
linear_layer = nn.Linear(in_channels, out_channels)
# Initialize weights as identity
with torch.no_grad():
linear_layer.weight.copy_(torch.eye(in_channels))
return linear_layer
@register_to_config
def __init__(
self,
conditioning_channels: int = 3,
conditioning_embedding_out_channels: Tuple[int] = (16, 32, 96, 256),
controlnet_conditioning_channel_order: str = "rgb",
time_embedding_input_dim: int = 320,
time_embedding_dim: int = 1280,
time_embedding_mix: float = 1.0,
learn_embedding: bool = False,
base_model_channel_sizes: Dict[str, List[Tuple[int]]] = {
"down": [
(4, 320),
(320, 320),
(320, 320),
(320, 320),
(320, 640),
(640, 640),
(640, 640),
(640, 1280),
(1280, 1280),
],
"mid": [(1280, 1280)],
"up": [
(2560, 1280),
(2560, 1280),
(1920, 1280),
(1920, 640),
(1280, 640),
(960, 640),
(960, 320),
(640, 320),
(640, 320),
],
},
sample_size: Optional[int] = None,
down_block_types: Tuple[str] = (
"CrossAttnDownBlock2D",
"CrossAttnDownBlock2D",
"CrossAttnDownBlock2D",
"DownBlock2D",
),
up_block_types: Tuple[str] = ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D"),
block_out_channels: Tuple[int] = (320, 640, 1280, 1280),
norm_num_groups: Optional[int] = 32,
cross_attention_dim: Union[int, Tuple[int]] = 1280,
transformer_layers_per_block: Union[int, Tuple[int], Tuple[Tuple]] = 1,
num_attention_heads: Optional[Union[int, Tuple[int]]] = 8,
upcast_attention: bool = False,
):
super().__init__()
# 1 - Create control unet
self.control_model = UNet2DConditionModel(
sample_size=sample_size,
down_block_types=down_block_types,
up_block_types=up_block_types,
block_out_channels=block_out_channels,
norm_num_groups=norm_num_groups,
cross_attention_dim=cross_attention_dim,
transformer_layers_per_block=transformer_layers_per_block,
attention_head_dim=num_attention_heads,
use_linear_projection=True,
upcast_attention=upcast_attention,
time_embedding_dim=time_embedding_dim,
)
# 2 - Do model surgery on control model
# 2.1 - Allow to use the same time information as the base model
adjust_time_dims(self.control_model, time_embedding_input_dim, time_embedding_dim)
# 2.2 - Allow for information infusion from base model
# We concat the output of each base encoder subblocks to the input of the next control encoder subblock
# (We ignore the 1st element, as it represents the `conv_in`.)
extra_input_channels = [input_channels for input_channels, _ in base_model_channel_sizes["down"][1:]]
it_extra_input_channels = iter(extra_input_channels)
# print(extra_input_channels)
# for b, block in enumerate(self.control_model.down_blocks):
# for r in range(len(block.resnets)):
# increase_block_input_in_encoder_resnet(
# self.control_model, block_no=b, resnet_idx=r, by=next(it_extra_input_channels)
# )
# if block.downsamplers:
# increase_block_input_in_encoder_downsampler(
# self.control_model, block_no=b, by=next(it_extra_input_channels)
# )
# increase_block_input_in_mid_resnet(self.control_model, by=extra_input_channels[-1])
def get_flat_subblock_channel_sizes_down(model):
subblock_channel_sizes = []
for block in model.down_blocks:
# Iterate through ResnetBlock2D subblocks
for resnet in block.resnets:
# Only handle the first convolution for ResnetBlock2D
if hasattr(resnet, 'conv1'):
input_channels = resnet.conv1.in_channels
output_channels = resnet.conv1.out_channels
subblock_channel_sizes.append((input_channels, output_channels))
# Check and iterate through Upsample2D subblocks only if they exist
if hasattr(block, 'upsamplers') and block.upsamplers:
for upsampler in block.upsamplers:
if hasattr(upsampler, 'conv'):
input_channels = upsampler.conv.in_channels
output_channels = upsampler.conv.out_channels
subblock_channel_sizes.append((input_channels, output_channels))
print("down" ,subblock_channel_sizes)
return subblock_channel_sizes
def get_flat_subblock_channel_sizes(model):
subblock_channel_sizes = []
for block in model.up_blocks:
# Iterate through ResnetBlock2D subblocks
for resnet in block.resnets:
# Only handle the first convolution for ResnetBlock2D
if hasattr(resnet, 'conv1'):
input_channels = resnet.conv1.in_channels
output_channels = resnet.conv1.out_channels
subblock_channel_sizes.append((input_channels, output_channels))
# Check and iterate through Upsample2D subblocks only if they exist
if hasattr(block, 'upsamplers') and block.upsamplers:
for upsampler in block.upsamplers:
if hasattr(upsampler, 'conv'):
input_channels = upsampler.conv.in_channels
output_channels = upsampler.conv.out_channels
# subblock_channel_sizes.append((input_channels, output_channels))
print("up", subblock_channel_sizes)
return subblock_channel_sizes
get_flat_subblock_channel_sizes_down(self.control_model)
# Now use this function to dynamically get the extra input channels
#extra_input_channels_up = [t[1] for t in get_flat_subblock_channel_sizes(self.control_model)]
#all_channels_up = get_flat_subblock_channel_sizes(self.control_model)
#print(extra_input_channels_up)
# it_extra_input_channels = iter(extra_input_channels_up)
# #print(self.control_model.up_blocks)
# for b, block in enumerate(self.control_model.up_blocks):
# for r in range(len(block.resnets)):
# increase_block_input_in_decoder_resnet(
# self.control_model, block_no=b, resnet_idx=r, by=next(it_extra_input_channels)
# )
# print(len(block.resnets))
# # if block.upsamplers:
# #increase_block_input_in_decoder_downsampler(
# # self.control_model, block_no=b, by=next(it_extra_input_channels)
# #)
# 2.3 - Make group norms work with modified channel sizes
adjust_group_norms(self.control_model)
# 3 - Gather Channel Sizes
self.ch_inout_ctrl = StyleCodesModel._gather_subblock_sizes(self.control_model, base_or_control="control")
self.ch_inout_base = base_model_channel_sizes
# 4 - Build connections between base and control model
self.control_model.down_zero_convs_in = nn.ModuleList([])
self.control_model.middle_block_out = nn.ModuleList([])
#self.control_model.middle_block_in = nn.ModuleList([])
self.control_model.up_zero_convs_out = nn.ModuleList([])
#self.control_model.up_zero_convs_in = nn.ModuleList([])
#for ch_io_base in self.ch_inout_base["down"]:
# for i in range(len(self.ch_inout_base["down"])):
# if i < len(self.ch_inout_ctrl["down"]) - 1:
# ch_io_base = self.ch_inout_base["down"][i]
# self.control_model.down_zero_convs_in.append(self._make_zero_conv(in_channels=ch_io_base[1], out_channels=ch_io_base[1]))
#self.control_model.down_zero_convs_in.append(self._make_zero_conv(in_channels=ch_io_base[1], out_channels=ch_io_base[1]))
linear_shape = self.ch_inout_ctrl["mid"][-1][1] + self.ch_inout_ctrl["mid"][-1][1]
self.middle_block_out = self._make_colab_linear_layer(in_channels=linear_shape, out_channels=linear_shape)
#self.up_zero_convs_out.append(
# self._make_zero_conv(self.ch_inout_ctrl["down"][-1][1], self.ch_inout_base["mid"][-1][1])
#)
#skip connections i dont care about these
#for i in range(1, len(self.ch_inout_ctrl["down"])):
# self.up_zero_convs_out.append(
# self._make_zero_conv(self.ch_inout_ctrl["down"][-(i + 1)][1], self.ch_inout_base["up"][i - 1][1])
# )
#up blocks for output
#need to check the input sizes
#need to implement the increased input size for the up blocks as done already with the down blocks
base_last_out_channels = [1280,1280, 1280, 1280, 1280, 1280, 1280, 640, 640, 640, 320, 320,320]
base_current_in_channels = [1280, 1280, 1280, 1280, 1280, 1280, 640, 640, 640, 320, 320,320]
#JANK WARNING REMEMBER TO FIX LATER BEFORE ACTUALLY PUTTING THIS CODE ANYWHERE
print(f"subblock up sizes {self.ch_inout_ctrl}")
# for i in range(len(base_current_in_channels)):
# self.control_model.up_zero_convs_in.append(
# self._make_zero_conv(base_last_out_channels[i], base_current_in_channels[i])
# )
for i in range(len(self.ch_inout_base["up"])):
#for ch_io_base in self.ch_inout_base["up"]:
ch_io_base = self.ch_inout_base["up"][i]
if i < len(self.ch_inout_ctrl["up"]):
linear_shape = ch_io_base[1] + ch_io_base[1]
self.control_model.up_zero_convs_out.append(
self._make_colab_linear_layer(in_channels=linear_shape, out_channels=linear_shape)
)
# for i in range(len(self.ch_inout_ctrl["up"])):
# self.control_model.up_zero_convs_out.append(
# self._make_zero_conv(self.ch_inout_ctrl["up"][i][1], self.ch_inout_base["up"][i][1])
# )
# 5 - Create conditioning hint embedding
# self.controlnet_cond_embedding = ControlNetConditioningEmbedding(
# conditioning_embedding_channels=block_out_channels[0],
# block_out_channels=conditioning_embedding_out_channels,
# conditioning_channels=conditioning_channels,
# )
self.sref_autoencoder = AttentionAutoencoder().to(device='cuda')
# In the mininal implementation setting, we only need the control model up to the mid block
#del self.control_model.up_blocks
del self.control_model.down_blocks
del self.control_model.conv_norm_out
del self.control_model.conv_out
del self.control_model.time_embedding
del self.control_model.conv_in
def load_model(self, path: str):
"""Load the model from the given path.
Parameters:
path (`str`):
Path to the model checkpoint.
"""
if os.path.splitext(path)[-1] == ".safetensors":
state_dict = {"image_proj": {}, "ip_adapter": {}, "controlnet": {}}
with safe_open(path, framework="pt", device="cpu") as f:
for key in f.keys():
if key.startswith("image_proj."):
state_dict["image_proj"][key.replace("image_proj.", "")] = f.get_tensor(key)
elif key.startswith("ip_adapter."):
state_dict["ip_adapter"][key.replace("ip_adapter.", "")] = f.get_tensor(key)
elif key.startswith("controlnet."):
state_dict["controlnet"][key.replace("controlnet.", "")] = f.get_tensor(key)
else:
state_dict = torch.load(path, map_location="cpu")
print("load controlnet", self.load_state_dict(state_dict["controlnet"],strict=False))
@classmethod
def from_unet(
cls,
unet: UNet2DConditionModel,
conditioning_channels: int = 3,
conditioning_embedding_out_channels: Tuple[int] = (16, 32, 96, 256),
controlnet_conditioning_channel_order: str = "rgb",
learn_embedding: bool = False,
time_embedding_mix: float = 1.0,
block_out_channels: Optional[Tuple[int]] = None,
size_ratio: Optional[float] = None,
num_attention_heads: Optional[Union[int, Tuple[int]]] = 8,
norm_num_groups: Optional[int] = None,
):
r"""
Instantiate a [`ControlNetXSModel`] from [`UNet2DConditionModel`].
Parameters:
unet (`UNet2DConditionModel`):
The UNet model we want to control. The dimensions of the ControlNetXSModel will be adapted to it.
conditioning_channels (`int`, defaults to 3):
Number of channels of conditioning input (e.g. an image)
conditioning_embedding_out_channels (`tuple[int]`, defaults to `(16, 32, 96, 256)`):
The tuple of output channel for each block in the `controlnet_cond_embedding` layer.
controlnet_conditioning_channel_order (`str`, defaults to `"rgb"`):
The channel order of conditional image. Will convert to `rgb` if it's `bgr`.
learn_embedding (`bool`, defaults to `False`):
Wether to use time embedding of the control model. If yes, the time embedding is a linear interpolation
of the time embeddings of the control and base model with interpolation parameter
`time_embedding_mix**3`.
time_embedding_mix (`float`, defaults to 1.0):
Linear interpolation parameter used if `learn_embedding` is `True`.
block_out_channels (`Tuple[int]`, *optional*):
Down blocks output channels in control model. Either this or `size_ratio` must be given.
size_ratio (float, *optional*):
When given, block_out_channels is set to a relative fraction of the base model's block_out_channels.
Either this or `block_out_channels` must be given.
num_attention_heads (`Union[int, Tuple[int]]`, *optional*):
The dimension of the attention heads. The naming seems a bit confusing and it is, see https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 for why.
norm_num_groups (int, *optional*, defaults to `None`):
The number of groups to use for the normalization of the control unet. If `None`,
`int(unet.config.norm_num_groups * size_ratio)` is taken.
"""
# Check input
fixed_size = block_out_channels is not None
relative_size = size_ratio is not None
if not (fixed_size ^ relative_size):
raise ValueError(
"Pass exactly one of `block_out_channels` (for absolute sizing) or `control_model_ratio` (for relative sizing)."
)
# Create model
if block_out_channels is None:
block_out_channels = [int(size_ratio * c) for c in unet.config.block_out_channels]
# Check that attention heads and group norms match channel sizes
# - attention heads
def attn_heads_match_channel_sizes(attn_heads, channel_sizes):
if isinstance(attn_heads, (tuple, list)):
return all(c % a == 0 for a, c in zip(attn_heads, channel_sizes))
else:
return all(c % attn_heads == 0 for c in channel_sizes)
num_attention_heads = num_attention_heads or unet.config.attention_head_dim
if not attn_heads_match_channel_sizes(num_attention_heads, block_out_channels):
raise ValueError(
f"The dimension of attention heads ({num_attention_heads}) must divide `block_out_channels` ({block_out_channels}). If you didn't set `num_attention_heads` the default settings don't match your model. Set `num_attention_heads` manually."
)
# - group norms
def group_norms_match_channel_sizes(num_groups, channel_sizes):
return all(c % num_groups == 0 for c in channel_sizes)
if norm_num_groups is None:
if group_norms_match_channel_sizes(unet.config.norm_num_groups, block_out_channels):
norm_num_groups = unet.config.norm_num_groups
else:
norm_num_groups = min(block_out_channels)
if group_norms_match_channel_sizes(norm_num_groups, block_out_channels):
print(
f"`norm_num_groups` was set to `min(block_out_channels)` (={norm_num_groups}) so it divides all block_out_channels` ({block_out_channels}). Set it explicitly to remove this information."
)
else:
raise ValueError(
f"`block_out_channels` ({block_out_channels}) don't match the base models `norm_num_groups` ({unet.config.norm_num_groups}). Setting `norm_num_groups` to `min(block_out_channels)` ({norm_num_groups}) didn't fix this. Pass `norm_num_groups` explicitly so it divides all block_out_channels."
)
def get_time_emb_input_dim(unet: UNet2DConditionModel):
return unet.time_embedding.linear_1.in_features
def get_time_emb_dim(unet: UNet2DConditionModel):
return unet.time_embedding.linear_2.out_features
# Clone params from base unet if
# (i) it's required to build SD or SDXL, and
# (ii) it's not used for the time embedding (as time embedding of control model is never used), and
# (iii) it's not set further below anyway
to_keep = [
"cross_attention_dim",
"down_block_types",
"sample_size",
"transformer_layers_per_block",
"up_block_types",
"upcast_attention",
]
kwargs = {k: v for k, v in dict(unet.config).items() if k in to_keep}
kwargs.update(block_out_channels=block_out_channels)
kwargs.update(num_attention_heads=num_attention_heads)
kwargs.update(norm_num_groups=norm_num_groups)
# Add controlnetxs-specific params
kwargs.update(
conditioning_channels=conditioning_channels,
controlnet_conditioning_channel_order=controlnet_conditioning_channel_order,
time_embedding_input_dim=get_time_emb_input_dim(unet),
time_embedding_dim=get_time_emb_dim(unet),
time_embedding_mix=time_embedding_mix,
learn_embedding=learn_embedding,
base_model_channel_sizes=StyleCodesModel._gather_subblock_sizes(unet, base_or_control="base"),
conditioning_embedding_out_channels=conditioning_embedding_out_channels,
)
return cls(**kwargs)
@property
def attn_processors(self) -> Dict[str, AttentionProcessor]:
r"""
Returns:
`dict` of attention processors: A dictionary containing all attention processors used in the model with
indexed by its weight name.
"""
return self.control_model.attn_processors
def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]):
r"""
Sets the attention processor to use to compute attention.
Parameters:
processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
The instantiated processor class or a dictionary of processor classes that will be set as the processor
for **all** `Attention` layers.
If `processor` is a dict, the key needs to define the path to the corresponding cross attention
processor. This is strongly recommended when setting trainable attention processors.
"""
self.control_model.set_attn_processor(processor)
def set_default_attn_processor(self):
"""
Disables custom attention processors and sets the default attention implementation.
"""
self.control_model.set_default_attn_processor()
def set_attention_slice(self, slice_size):
r"""
Enable sliced attention computation.
When this option is enabled, the attention module splits the input tensor in slices to compute attention in
several steps. This is useful for saving some memory in exchange for a small decrease in speed.
Args:
slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`):
When `"auto"`, input to the attention heads is halved, so attention is computed in two steps. If
`"max"`, maximum amount of memory is saved by running only one slice at a time. If a number is
provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim`
must be a multiple of `slice_size`.
"""
self.control_model.set_attention_slice(slice_size)
def _set_gradient_checkpointing(self, module, value=False):
if isinstance(module, (UNet2DConditionModel)):
if value:
module.enable_gradient_checkpointing()
else:
module.disable_gradient_checkpointing()
def forward(
self,
base_model: UNet2DConditionModel,
sample: torch.FloatTensor,
timestep: Union[torch.Tensor, float, int],
encoder_hidden_states: torch.Tensor,
encoder_hidden_states_controlnet: torch.Tensor,
controlnet_cond: torch.Tensor,
conditioning_scale: float = 1.0,
class_labels: Optional[torch.Tensor] = None,
timestep_cond: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
cross_attention_kwargs: Optional[Dict[str, Any]] = None,
added_cond_kwargs: Optional[Dict[str, torch.Tensor]] = None,
return_dict: bool = True,
stylecode=None,
) -> Union[ControlNetXSOutput, Tuple]:
"""
The [`ControlNetModel`] forward method.
Args:
base_model (`UNet2DConditionModel`):
The base unet model we want to control.
sample (`torch.FloatTensor`):
The noisy input tensor.
timestep (`Union[torch.Tensor, float, int]`):
The number of timesteps to denoise an input.
encoder_hidden_states (`torch.Tensor`):
The encoder hidden states.
controlnet_cond (`torch.FloatTensor`):
The conditional input tensor of shape `(batch_size, sequence_length, hidden_size)`.
conditioning_scale (`float`, defaults to `1.0`):
How much the control model affects the base model outputs.
class_labels (`torch.Tensor`, *optional*, defaults to `None`):
Optional class labels for conditioning. Their embeddings will be summed with the timestep embeddings.
timestep_cond (`torch.Tensor`, *optional*, defaults to `None`):
Additional conditional embeddings for timestep. If provided, the embeddings will be summed with the
timestep_embedding passed through the `self.time_embedding` layer to obtain the final timestep
embeddings.
attention_mask (`torch.Tensor`, *optional*, defaults to `None`):
An attention mask of shape `(batch, key_tokens)` is applied to `encoder_hidden_states`. If `1` the mask
is kept, otherwise if `0` it is discarded. Mask will be converted into a bias, which adds large
negative values to the attention scores corresponding to "discard" tokens.
added_cond_kwargs (`dict`):
Additional conditions for the Stable Diffusion XL UNet.
cross_attention_kwargs (`dict[str]`, *optional*, defaults to `None`):
A kwargs dictionary that if specified is passed along to the `AttnProcessor`.
return_dict (`bool`, defaults to `True`):
Whether or not to return a [`~models.controlnet.ControlNetOutput`] instead of a plain tuple.
Returns:
[`~models.controlnetxs.ControlNetXSOutput`] **or** `tuple`:
If `return_dict` is `True`, a [`~models.controlnetxs.ControlNetXSOutput`] is returned, otherwise a
tuple is returned where the first element is the sample tensor.
"""
# check channel order
channel_order = self.config.controlnet_conditioning_channel_order
if channel_order == "rgb":
# in rgb order by default
...
elif channel_order == "bgr":
controlnet_cond = torch.flip(controlnet_cond, dims=[1])
else:
raise ValueError(f"unknown `controlnet_conditioning_channel_order`: {channel_order}")
# scale control strength
n_connections = 0 + 1 + len(self.control_model.up_zero_convs_out)
scale_list = torch.full((n_connections,), conditioning_scale)
# prepare attention_mask
if attention_mask is not None:
attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0
attention_mask = attention_mask.unsqueeze(1)
# 1. time
timesteps = timestep
if not torch.is_tensor(timesteps):
# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
# This would be a good case for the `match` statement (Python 3.10+)
is_mps = sample.device.type == "mps"
if isinstance(timestep, float):
dtype = torch.float32 if is_mps else torch.float64
else:
dtype = torch.int32 if is_mps else torch.int64
timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device)
elif len(timesteps.shape) == 0:
timesteps = timesteps[None].to(sample.device)
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
timesteps = timesteps.expand(sample.shape[0])
t_emb = base_model.time_proj(timesteps)
# timesteps does not contain any weights and will always return f32 tensors
# but time_embedding might actually be running in fp16. so we need to cast here.
# there might be better ways to encapsulate this.
t_emb = t_emb.to(dtype=sample.dtype)
if self.config.learn_embedding:
ctrl_temb = self.control_model.time_embedding(t_emb, timestep_cond)
base_temb = base_model.time_embedding(t_emb, timestep_cond)
interpolation_param = self.config.time_embedding_mix**0.3
temb = ctrl_temb * interpolation_param + base_temb * (1 - interpolation_param)
else:
temb = base_model.time_embedding(t_emb)
# added time & text embeddings
aug_emb = None
aug_emb_ctrl = None
if base_model.class_embedding is not None:
if class_labels is None:
raise ValueError("class_labels should be provided when num_class_embeds > 0")
if base_model.config.class_embed_type == "timestep":
class_labels = base_model.time_proj(class_labels)
class_emb = base_model.class_embedding(class_labels).to(dtype=self.dtype)
temb = temb + class_emb
if base_model.config.addition_embed_type is not None:
if base_model.config.addition_embed_type == "text":
aug_emb = base_model.add_embedding(encoder_hidden_states)
aug_emb_ctrl = base_model.add_embedding(encoder_hidden_states_controlnet)
elif base_model.config.addition_embed_type == "text_image":
raise NotImplementedError()
elif base_model.config.addition_embed_type == "text_time":
# SDXL - style
if "text_embeds" not in added_cond_kwargs:
raise ValueError(
f"{self.__class__} has the config param `addition_embed_type` set to 'text_time' which requires the keyword argument `text_embeds` to be passed in `added_cond_kwargs`"
)
text_embeds = added_cond_kwargs.get("text_embeds")
if "time_ids" not in added_cond_kwargs:
raise ValueError(
f"{self.__class__} has the config param `addition_embed_type` set to 'text_time' which requires the keyword argument `time_ids` to be passed in `added_cond_kwargs`"
)
time_ids = added_cond_kwargs.get("time_ids")
time_embeds = base_model.add_time_proj(time_ids.flatten())
time_embeds = time_embeds.reshape((text_embeds.shape[0], -1))
add_embeds = torch.concat([text_embeds, time_embeds], dim=-1)
add_embeds = add_embeds.to(temb.dtype)
aug_emb = base_model.add_embedding(add_embeds)
elif base_model.config.addition_embed_type == "image":
raise NotImplementedError()
elif base_model.config.addition_embed_type == "image_hint":
raise NotImplementedError()
temb = temb + aug_emb if aug_emb is not None else temb
#temb_ctrl = torch.zeros_like(temb)
temb_ctrl = temb + aug_emb_ctrl if aug_emb_ctrl is not None else temb
# text embeddings
#note when i have more time actually skip the cross attention layers
cemb = encoder_hidden_states
#cemb_ctrl = torch.zeros_like(encoder_hidden_states)
cemb_ctrl = encoder_hidden_states
# Preparation
#print("1:cond, 2: embeddings",controlnet_cond.shape,encoder_hidden_states_controlnet.shape)
#save_debug_image(controlnet_cond[0])
#guided_hint = self.controlnet_cond_embedding(controlnet_cond)
#guided_hint=None
h_ctrl = h_base = sample
hs_base, hs_ctrl = [], []
it_up_convs_out = iter (self.control_model.up_zero_convs_out)
scales = iter(scale_list)
base_down_subblocks = self.to_sub_blocks(base_model.down_blocks)
#ctrl_down_subblocks = self.to_sub_blocks(self.control_model.down_blocks)
base_mid_subblocks = self.to_sub_blocks([base_model.mid_block])
ctrl_mid_subblocks = self.to_sub_blocks([self.control_model.mid_block])
base_up_subblocks = self.to_sub_blocks(base_model.up_blocks)
ctrl_up_subblocks = self.to_sub_blocks(self.control_model.up_blocks)
# Cross Control
# 0 - conv in
h_base = base_model.conv_in(h_base)
#h_ctrl = self.control_model.conv_in(h_ctrl)
#if guided_hint is not None:
h_ctrl = controlnet_cond
# h_base = h_base + next(it_down_convs_out)(h_ctrl) * next(scales) # D - add ctrl -> base
hs_base.append(h_base)
#hs_ctrl.append(h_ctrl)
# 1 - down
for m_base in base_down_subblocks:
#h_ctrl = torch.cat([h_ctrl, next(it_down_convs_in)(h_base)], dim=1) # A - concat base -> ctrl
h_base = m_base(h_base, temb, cemb, attention_mask, cross_attention_kwargs) # B - apply base subblock
#h_ctrl = m_ctrl(h_ctrl, temb_ctrl, cemb_ctrl, attention_mask, cross_attention_kwargs) # C - apply ctrl subblock
#h_base = h_base + next(it_down_convs_out)(h_ctrl) * next(scales) # D - add ctrl -> base
hs_base.append(h_base)
#hs_ctrl.append(h_ctrl)
print("using stylecode",stylecode)
if stylecode is None:
h_ctrl,encoded_strings = self.sref_autoencoder.forward_encoding(h_ctrl,h_base.shape[2],h_base.shape[3])
else:
h_ctrl = self.sref_autoencoder.forward_from_stylecode(stylecode,h_base.shape[2],h_base.shape[3],h_base.dtype, h_base.device)
# 2 - mid
#h_ctrl = torch.cat([h_ctrl, next(it_down_convs_in)(h_base)], dim=1) # A - concat base -> ctrl
for m_base, m_ctrl in zip(base_mid_subblocks, ctrl_mid_subblocks):
h_base = m_base(h_base, temb, cemb, attention_mask, cross_attention_kwargs) # B - apply base subblock
h_ctrl = m_ctrl(h_ctrl, temb_ctrl, cemb_ctrl, attention_mask, cross_attention_kwargs) # C - apply ctrl subblock
#taken from https://github.com/dvlab-research/ControlNeXt/blob/main/ControlNeXt-SD1.5/models/unet.py
#mid_block_additional_residual = self.middle_block_out(h_ctrl)
# mid_block_additional_residual = mid_block_out
# mid_block_additional_residual=nn.functional.adaptive_avg_pool2d(mid_block_additional_residual, h_base.shape[-2:])
# mid_block_additional_residual = mid_block_additional_residual.to(h_base)
# mean_latents, std_latents = torch.mean(h_base, dim=(1, 2, 3), keepdim=True), torch.std(h_base, dim=(1, 2, 3), keepdim=True)
# mean_control, std_control = torch.mean(mid_block_additional_residual, dim=(1, 2, 3), keepdim=True), torch.std(mid_block_additional_residual, dim=(1, 2, 3), keepdim=True)
# mid_block_additional_residual = (mid_block_additional_residual - mean_control) * (std_latents / (std_control + 1e-12)) + mean_latents
# h_base = h_base + mid_block_additional_residual * next(scales)
batch_size, channels, height, width = h_ctrl.shape
colab_input = torch.cat([h_ctrl, h_base], dim=1).view(batch_size, channels * 2, height * width).permute(0, 2, 1)
colab_output = self.middle_block_out(colab_input)
sequence_len = height * width
colab_output = colab_output.permute(0, 2, 1).view(batch_size, channels * 2, height, width) # Reshape back
h_ctrl, h_base_output = torch.chunk(colab_output, 2, dim=1)
#mix using cond scale
h_base = h_base * (1 - conditioning_scale) + h_base_output * conditioning_scale
#h_base = h_base + mid_block_additional_residual * next(scales) # D - add ctrl -> base
# 3 - up
for m_base,m_ctrl in zip(base_up_subblocks,ctrl_up_subblocks):
hs_base_new = hs_base.pop()
h_base_with_skip = torch.cat([h_base, hs_base_new], dim=1) # concat info from base encoder+ctrl encoder
empty = torch.zeros_like(hs_base_new)
h_ctrl = torch.cat([h_ctrl, empty], dim=1) # concat info from ctrl encoder + skip connections
h_ctrl = m_ctrl(h_ctrl, temb_ctrl, cemb_ctrl, attention_mask, cross_attention_kwargs) # C - apply ctrl subblock
h_base = m_base(h_base_with_skip, temb, cemb, attention_mask, cross_attention_kwargs)
batch_size, channels, height, width = h_ctrl.shape
colab_input = torch.cat([h_ctrl, h_base], dim=1).view(batch_size, channels * 2, height * width).permute(0, 2, 1)
colab_output = next(it_up_convs_out)(colab_input)
colab_output = colab_output.permute(0, 2, 1).view(batch_size, channels * 2, height, width)
h_ctrl, h_base_output = torch.chunk(colab_output, 2, dim=1)
h_base = h_base * (1 - conditioning_scale) + h_base_output * conditioning_scale
#hn_ctrl = next(it_up_convs_out)(h_ctrl)
#print(hn_ctrl)
#h_base = h_base + hn_ctrl * next(scales) # D - add ctrl -> base
h_base = base_model.conv_norm_out(h_base)
h_base = base_model.conv_act(h_base)
h_base = base_model.conv_out(h_base)
if not return_dict:
return h_base
return ControlNetXSOutput(sample=h_base)
#needs new stuff to work correctly
# def pre_process(
# self,
# base_model: UNet2DConditionModel,
# sample: torch.FloatTensor,
# timestep: Union[torch.Tensor, float, int],
# encoder_hidden_states: torch.Tensor,
# controlnet_cond: torch.Tensor,
# conditioning_scale: float = 1.0,
# class_labels: Optional[torch.Tensor] = None,
# timestep_cond: Optional[torch.Tensor] = None,
# attention_mask: Optional[torch.Tensor] = None,
# cross_attention_kwargs: Optional[Dict[str, Any]] = None,
# added_cond_kwargs: Optional[Dict[str, torch.Tensor]] = None,
# return_dict: bool = True
# ):
# """
# The [`ControlNetModel`] forward method.
# Args:
# base_model (`UNet2DConditionModel`):
# The base unet model we want to control.
# sample (`torch.FloatTensor`):
# The noisy input tensor.
# timestep (`Union[torch.Tensor, float, int]`):
# The number of timesteps to denoise an input.
# encoder_hidden_states (`torch.Tensor`):
# The encoder hidden states.
# controlnet_cond (`torch.FloatTensor`):
# The conditional input tensor of shape `(batch_size, sequence_length, hidden_size)`.
# conditioning_scale (`float`, defaults to `1.0`):
# How much the control model affects the base model outputs.
# class_labels (`torch.Tensor`, *optional*, defaults to `None`):
# Optional class labels for conditioning. Their embeddings will be summed with the timestep embeddings.
# timestep_cond (`torch.Tensor`, *optional*, defaults to `None`):
# Additional conditional embeddings for timestep. If provided, the embeddings will be summed with the
# timestep_embedding passed through the `self.time_embedding` layer to obtain the final timestep
# embeddings.
# attention_mask (`torch.Tensor`, *optional*, defaults to `None`):
# An attention mask of shape `(batch, key_tokens)` is applied to `encoder_hidden_states`. If `1` the mask
# is kept, otherwise if `0` it is discarded. Mask will be converted into a bias, which adds large
# negative values to the attention scores corresponding to "discard" tokens.
# added_cond_kwargs (`dict`):
# Additional conditions for the Stable Diffusion XL UNet.
# cross_attention_kwargs (`dict[str]`, *optional*, defaults to `None`):
# A kwargs dictionary that if specified is passed along to the `AttnProcessor`.
# return_dict (`bool`, defaults to `True`):
# Whether or not to return a [`~models.controlnet.ControlNetOutput`] instead of a plain tuple.
# Returns:
# [`~models.controlnetxs.ControlNetXSOutput`] **or** `tuple`:
# If `return_dict` is `True`, a [`~models.controlnetxs.ControlNetXSOutput`] is returned, otherwise a
# tuple is returned where the first element is the sample tensor.
# """
# # check channel order
# channel_order = self.config.controlnet_conditioning_channel_order
# if channel_order == "rgb":
# # in rgb order by default
# ...
# elif channel_order == "bgr":
# controlnet_cond = torch.flip(controlnet_cond, dims=[1])
# else:
# raise ValueError(f"unknown `controlnet_conditioning_channel_order`: {channel_order}")
# # scale control strength
# n_connections = len(self.control_model.down_zero_convs_out) + 1 + len(self.control_model.up_zero_convs_out)
# scale_list = torch.full((n_connections,), conditioning_scale)
# # prepare attention_mask
# if attention_mask is not None:
# attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0
# attention_mask = attention_mask.unsqueeze(1)
# # 1. time
# timesteps = timestep
# if not torch.is_tensor(timesteps):
# # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
# # This would be a good case for the `match` statement (Python 3.10+)
# is_mps = sample.device.type == "mps"
# if isinstance(timestep, float):
# dtype = torch.float32 if is_mps else torch.float64
# else:
# dtype = torch.int32 if is_mps else torch.int64
# timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device)
# elif len(timesteps.shape) == 0:
# timesteps = timesteps[None].to(sample.device)
# # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
# timesteps = timesteps.expand(sample.shape[0])
# t_emb = base_model.time_proj(timesteps)
# # timesteps does not contain any weights and will always return f32 tensors
# # but time_embedding might actually be running in fp16. so we need to cast here.
# # there might be better ways to encapsulate this.
# t_emb = t_emb.to(dtype=sample.dtype)
# if self.config.learn_embedding:
# ctrl_temb = self.control_model.time_embedding(t_emb, timestep_cond)
# base_temb = base_model.time_embedding(t_emb, timestep_cond)
# interpolation_param = self.config.time_embedding_mix**0.3
# temb = ctrl_temb * interpolation_param + base_temb * (1 - interpolation_param)
# else:
# temb = base_model.time_embedding(t_emb)
# # added time & text embeddings
# aug_emb = None
# if base_model.class_embedding is not None:
# if class_labels is None:
# raise ValueError("class_labels should be provided when num_class_embeds > 0")
# if base_model.config.class_embed_type == "timestep":
# class_labels = base_model.time_proj(class_labels)
# class_emb = base_model.class_embedding(class_labels).to(dtype=self.dtype)
# temb = temb + class_emb
# if base_model.config.addition_embed_type is not None:
# if base_model.config.addition_embed_type == "text":
# aug_emb = base_model.add_embedding(encoder_hidden_states)
# elif base_model.config.addition_embed_type == "text_image":
# raise NotImplementedError()
# elif base_model.config.addition_embed_type == "text_time":
# # SDXL - style
# if "text_embeds" not in added_cond_kwargs:
# raise ValueError(
# f"{self.__class__} has the config param `addition_embed_type` set to 'text_time' which requires the keyword argument `text_embeds` to be passed in `added_cond_kwargs`"
# )
# text_embeds = added_cond_kwargs.get("text_embeds")
# if "time_ids" not in added_cond_kwargs:
# raise ValueError(
# f"{self.__class__} has the config param `addition_embed_type` set to 'text_time' which requires the keyword argument `time_ids` to be passed in `added_cond_kwargs`"
# )
# time_ids = added_cond_kwargs.get("time_ids")
# time_embeds = base_model.add_time_proj(time_ids.flatten())
# time_embeds = time_embeds.reshape((text_embeds.shape[0], -1))
# add_embeds = torch.concat([text_embeds, time_embeds], dim=-1)
# add_embeds = add_embeds.to(temb.dtype)
# aug_emb = base_model.add_embedding(add_embeds)
# elif base_model.config.addition_embed_type == "image":
# raise NotImplementedError()
# elif base_model.config.addition_embed_type == "image_hint":
# raise NotImplementedError()
# temb = temb + aug_emb if aug_emb is not None else temb
# # text embeddings
# cemb = encoder_hidden_states
# # Preparation
# guided_hint = self.controlnet_cond_embedding(controlnet_cond)
# #guided_hint=None
# # h_ctrl = h_base = sample
# # hs_base, hs_ctrl = [], []
# # it_down_convs_in, it_down_convs_out, it_up_convs_in, it_up_convs_out = map(
# # iter, (self.control_model.down_zero_convs_in, self.control_model.down_zero_convs_out, self.control_model.up_zero_convs_in, self.control_model.up_zero_convs_out)
# # )
# scales = iter(scale_list)
# return temb,cemb,scales,guided_hint
def _make_zero_conv(self, in_channels, out_channels=None):
# keep running track of channels sizes
#self.in_channels = in_channels
#self.out_channels = out_channels or in_channels
#
return zero_module(nn.Conv2d(in_channels, out_channels, 1, padding=0))
def _make_identity_conv(self, in_channels, out_channels=None):
#out_channels = out_channels or in_channels
return nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0, bias=False)
@torch.no_grad()
def _check_if_vae_compatible(self, vae: AutoencoderKL):
condition_downscale_factor = 2 ** (len(self.config.conditioning_embedding_out_channels) - 1)
vae_downscale_factor = 2 ** (len(vae.config.block_out_channels) - 1)
compatible = condition_downscale_factor == vae_downscale_factor
return compatible, condition_downscale_factor, vae_downscale_factor
def to_sub_blocks(self,blocks):
if not is_iterable(blocks):
blocks = [blocks]
sub_blocks = []
for b in blocks:
if hasattr(b, "resnets"):
if hasattr(b, "attentions") and b.attentions is not None:
for r, a in zip(b.resnets, b.attentions):
sub_blocks.append([r, a])
num_resnets = len(b.resnets)
num_attns = len(b.attentions)
if num_resnets > num_attns:
# we can have more resnets than attentions, so add each resnet as separate subblock
for i in range(num_attns, num_resnets):
sub_blocks.append([b.resnets[i]])
else:
for r in b.resnets:
sub_blocks.append([r])
# upsamplers are part of the same subblock
if hasattr(b, "upsamplers") and b.upsamplers is not None:
for u in b.upsamplers:
sub_blocks[-1].extend([u])
# downsamplers are own subblock
if hasattr(b, "downsamplers") and b.downsamplers is not None:
for d in b.downsamplers:
sub_blocks.append([d])
return list(map(SubBlock, sub_blocks))
class SubBlock(nn.ModuleList):
"""A SubBlock is the largest piece of either base or control model, that is executed independently of the other model respectively.
Before each subblock, information is concatted from base to control. And after each subblock, information is added from control to base.
"""
def __init__(self, ms, *args, **kwargs):
if not is_iterable(ms):
ms = [ms]
super().__init__(ms, *args, **kwargs)
def forward(
self,
x: torch.Tensor,
temb: torch.Tensor,
cemb: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
cross_attention_kwargs: Optional[Dict[str, Any]] = None,
):
"""Iterate through children and pass correct information to each."""
for m in self:
if isinstance(m, ResnetBlock2D):
x = m(x, temb)
elif isinstance(m, Transformer2DModel):
x = m(x, cemb, attention_mask=attention_mask, cross_attention_kwargs=cross_attention_kwargs).sample
elif isinstance(m, Downsample2D):
x = m(x)
elif isinstance(m, Upsample2D):
x = m(x)
else:
raise ValueError(
f"Type of m is {type(m)} but should be `ResnetBlock2D`, `Transformer2DModel`, `Downsample2D` or `Upsample2D`"
)
return x
def adjust_time_dims(unet: UNet2DConditionModel, in_dim: int, out_dim: int):
unet.time_embedding.linear_1 = nn.Linear(in_dim, out_dim)
def increase_block_input_in_encoder_resnet(unet: UNet2DConditionModel, block_no, resnet_idx, by):
"""Increase channels sizes to allow for additional concatted information from base model"""
r = unet.down_blocks[block_no].resnets[resnet_idx]
old_norm1, old_conv1 = r.norm1, r.conv1
# norm
norm_args = "num_groups num_channels eps affine".split(" ")
for a in norm_args:
assert hasattr(old_norm1, a)
norm_kwargs = {a: getattr(old_norm1, a) for a in norm_args}
norm_kwargs["num_channels"] += by # surgery done here
# conv1
conv1_args = [
"in_channels",
"out_channels",
"kernel_size",
"stride",
"padding",
"dilation",
"groups",
"bias",
"padding_mode",
]
#if not USE_PEFT_BACKEND:
# conv1_args.append("lora_layer")
for a in conv1_args:
assert hasattr(old_conv1, a)
conv1_kwargs = {a: getattr(old_conv1, a) for a in conv1_args}
conv1_kwargs["bias"] = "bias" in conv1_kwargs # as param, bias is a boolean, but as attr, it's a tensor.
conv1_kwargs["in_channels"] += by # surgery done here
# conv_shortcut
# as we changed the input size of the block, the input and output sizes are likely different,
# therefore we need a conv_shortcut (simply adding won't work)
conv_shortcut_args_kwargs = {
"in_channels": conv1_kwargs["in_channels"],
"out_channels": conv1_kwargs["out_channels"],
# default arguments from resnet.__init__
"kernel_size": 1,
"stride": 1,
"padding": 0,
"bias": True,
}
# swap old with new modules
unet.down_blocks[block_no].resnets[resnet_idx].norm1 = GroupNorm(**norm_kwargs)
unet.down_blocks[block_no].resnets[resnet_idx].conv1 = (
nn.Conv2d(**conv1_kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**conv1_kwargs)
)
unet.down_blocks[block_no].resnets[resnet_idx].conv_shortcut = (
nn.Conv2d(**conv_shortcut_args_kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**conv_shortcut_args_kwargs)
)
print(f"increasing down {unet.down_blocks[block_no].resnets[resnet_idx].in_channels} by {by}")
unet.down_blocks[block_no].resnets[resnet_idx].in_channels += by # surgery done here
def increase_block_input_in_decoder_resnet(unet: UNet2DConditionModel, block_no, resnet_idx, by):
"""Increase channels sizes to allow for additional concatted information from base model"""
r = unet.up_blocks[block_no].resnets[resnet_idx]
old_norm1, old_conv1 = r.norm1, r.conv1
# norm
norm_args = "num_groups num_channels eps affine".split(" ")
for a in norm_args:
assert hasattr(old_norm1, a)
norm_kwargs = {a: getattr(old_norm1, a) for a in norm_args}
norm_kwargs["num_channels"] += by # surgery done here
# conv1
conv1_args = [
"in_channels",
"out_channels",
"kernel_size",
"stride",
"padding",
"dilation",
"groups",
"bias",
"padding_mode",
]
#if not USE_PEFT_BACKEND:
# conv1_args.append("lora_layer")
for a in conv1_args:
assert hasattr(old_conv1, a)
conv1_kwargs = {a: getattr(old_conv1, a) for a in conv1_args}
conv1_kwargs["bias"] = "bias" in conv1_kwargs # as param, bias is a boolean, but as attr, it's a tensor.
conv1_kwargs["in_channels"] += by # surgery done here
# conv_shortcut
# as we changed the input size of the block, the input and output sizes are likely different,
# therefore we need a conv_shortcut (simply adding won't work)
conv_shortcut_args_kwargs = {
"in_channels": conv1_kwargs["in_channels"],
"out_channels": conv1_kwargs["out_channels"],
# default arguments from resnet.__init__
"kernel_size": 1,
"stride": 1,
"padding": 0,
"bias": True,
}
# swap old with new modules
unet.up_blocks[block_no].resnets[resnet_idx].norm1 = GroupNorm(**norm_kwargs)
unet.up_blocks[block_no].resnets[resnet_idx].conv1 = (
nn.Conv2d(**conv1_kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**conv1_kwargs)
)
unet.up_blocks[block_no].resnets[resnet_idx].conv_shortcut = (
nn.Conv2d(**conv_shortcut_args_kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**conv_shortcut_args_kwargs)
)
#by =unet.up_blocks[block_no].resnets[resnet_idx].out_channels
print(f"increasing up {unet.up_blocks[block_no].resnets[resnet_idx].in_channels} by {by}")
unet.up_blocks[block_no].resnets[resnet_idx].in_channels += by # surgery done here
def increase_block_input_in_encoder_downsampler(unet: UNet2DConditionModel, block_no, by):
"""Increase channels sizes to allow for additional concatted information from base model"""
old_down = unet.down_blocks[block_no].downsamplers[0].conv
args = [
"in_channels",
"out_channels",
"kernel_size",
"stride",
"padding",
"dilation",
"groups",
"bias",
"padding_mode",
]
#if not USE_PEFT_BACKEND:
# args.append("lora_layer")
for a in args:
assert hasattr(old_down, a)
kwargs = {a: getattr(old_down, a) for a in args}
kwargs["bias"] = "bias" in kwargs # as param, bias is a boolean, but as attr, it's a tensor.
kwargs["in_channels"] += by # surgery done here
# swap old with new modules
unet.down_blocks[block_no].downsamplers[0].conv = (
nn.Conv2d(**kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**kwargs)
)
unet.down_blocks[block_no].downsamplers[0].channels += by # surgery done here
def increase_block_input_in_decoder_downsampler(unet: UNet2DConditionModel, block_no, by):
"""Increase channels sizes to allow for additional concatted information from base model"""
old_down = unet.up_blocks[block_no].upsamplers[0].conv
args = [
"in_channels",
"out_channels",
"kernel_size",
"stride",
"padding",
"dilation",
"groups",
"bias",
"padding_mode",
]
if not USE_PEFT_BACKEND:
args.append("lora_layer")
for a in args:
assert hasattr(old_down, a)
kwargs = {a: getattr(old_down, a) for a in args}
kwargs["bias"] = "bias" in kwargs # as param, bias is a boolean, but as attr, it's a tensor.
kwargs["in_channels"] += by # surgery done here
# swap old with new modules
unet.up_blocks[block_no].upsamplers[0].conv = (
nn.Conv2d(**kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**kwargs)
)
unet.up_blocks[block_no].upsamplers[0].channels += by # surgery done here
def increase_block_input_in_mid_resnet(unet: UNet2DConditionModel, by):
"""Increase channels sizes to allow for additional concatted information from base model"""
m = unet.mid_block.resnets[0]
old_norm1, old_conv1 = m.norm1, m.conv1
# norm
norm_args = "num_groups num_channels eps affine".split(" ")
for a in norm_args:
assert hasattr(old_norm1, a)
norm_kwargs = {a: getattr(old_norm1, a) for a in norm_args}
norm_kwargs["num_channels"] += by # surgery done here
conv1_args = [
"in_channels",
"out_channels",
"kernel_size",
"stride",
"padding",
"dilation",
"groups",
"bias",
"padding_mode",
]
#if not USE_PEFT_BACKEND:
# conv1_args.append("lora_layer")
conv1_kwargs = {a: getattr(old_conv1, a) for a in conv1_args}
conv1_kwargs["bias"] = "bias" in conv1_kwargs # as param, bias is a boolean, but as attr, it's a tensor.
conv1_kwargs["in_channels"] += by # surgery done here
# conv_shortcut
# as we changed the input size of the block, the input and output sizes are likely different,
# therefore we need a conv_shortcut (simply adding won't work)
conv_shortcut_args_kwargs = {
"in_channels": conv1_kwargs["in_channels"],
"out_channels": conv1_kwargs["out_channels"],
# default arguments from resnet.__init__
"kernel_size": 1,
"stride": 1,
"padding": 0,
"bias": True,
}
# swap old with new modules
unet.mid_block.resnets[0].norm1 = GroupNorm(**norm_kwargs)
unet.mid_block.resnets[0].conv1 = (
nn.Conv2d(**conv1_kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**conv1_kwargs)
)
unet.mid_block.resnets[0].conv_shortcut = (
nn.Conv2d(**conv_shortcut_args_kwargs) if USE_PEFT_BACKEND else LoRACompatibleConv(**conv_shortcut_args_kwargs)
)
unet.mid_block.resnets[0].in_channels += by # surgery done here
def adjust_group_norms(unet: UNet2DConditionModel, max_num_group: int = 32):
def find_denominator(number, start):
if start >= number:
return number
while start != 0:
residual = number % start
if residual == 0:
return start
start -= 1
for block in [*unet.down_blocks, unet.mid_block]:
# resnets
for r in block.resnets:
if r.norm1.num_groups < max_num_group:
r.norm1.num_groups = find_denominator(r.norm1.num_channels, start=max_num_group)
if r.norm2.num_groups < max_num_group:
r.norm2.num_groups = find_denominator(r.norm2.num_channels, start=max_num_group)
# transformers
if hasattr(block, "attentions"):
for a in block.attentions:
if a.norm.num_groups < max_num_group:
a.norm.num_groups = find_denominator(a.norm.num_channels, start=max_num_group)
def is_iterable(o):
if isinstance(o, str):
return False
try:
iter(o)
return True
except TypeError:
return False
def save_debug_image(image, folder='debug_images', noise_threshold=0.1):
os.makedirs(folder, exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"debug_image_{timestamp}.png"
filepath = os.path.join(folder, filename)
print("Debugging image information:")
print(f"Type of image: {type(image)}")
if isinstance(image, torch.Tensor):
print(f"Image tensor shape: {image.shape}")
print(f"Image tensor dtype: {image.dtype}")
print(f"Image tensor device: {image.device}")
print(f"Image tensor min: {image.min()}, max: {image.max()}")
image_np = image.cpu().detach().numpy()
elif isinstance(image, np.ndarray):
image_np = image
else:
print(f"Unexpected image type: {type(image)}")
return
print(f"Numpy array shape: {image_np.shape}")
print(f"Numpy array dtype: {image_np.dtype}")
print(f"Numpy array min: {image_np.min()}, max: {image_np.max()}")
# Handle different array shapes
if image_np.ndim == 4:
image_np = np.squeeze(image_np, axis=0)
image_np = np.transpose(image_np, (1, 2, 0))
elif image_np.ndim == 3:
if image_np.shape[0] in [1, 3, 4]:
image_np = np.transpose(image_np, (1, 2, 0))
elif image_np.ndim == 2:
image_np = np.expand_dims(image_np, axis=-1)
print(f"Processed numpy array shape: {image_np.shape}")
# Normalize the image, accounting for noise
if image_np.dtype != np.uint8:
if image_np.max() <= 1 + noise_threshold:
# Assume the image is in [0, 1] range with some noise
image_np = np.clip(image_np, 0, 1)
image_np = (image_np * 255).astype(np.uint8)
else:
# Assume the image is in a wider range, possibly due to noise
lower_percentile = np.percentile(image_np, 1)
upper_percentile = np.percentile(image_np, 99)
image_np = np.clip(image_np, lower_percentile, upper_percentile)
image_np = ((image_np - lower_percentile) / (upper_percentile - lower_percentile) * 255).astype(np.uint8)
print(f"Normalized array min: {image_np.min()}, max: {image_np.max()}")
try:
image_pil = Image.fromarray(image_np.squeeze() if image_np.shape[-1] == 1 else image_np)
image_pil.save(filepath)
print(f"Debug image saved as '{filepath}'")
except Exception as e:
print(f"Error saving image: {str(e)}")
print("Attempting to save as numpy array...")
np_filepath = filepath.replace('.png', '.npy')
np.save(np_filepath, image_np)
print(f"Numpy array saved as '{np_filepath}'")
def zero_module(module):
for p in module.parameters():
nn.init.zeros_(p)
return module
|