|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from json import JSONEncoder |
| 4 | +import torch |
| 5 | + |
| 6 | + |
| 7 | +class SDVersion(Enum): |
| 8 | + SD1 = 1 |
| 9 | + SD2 = 2 |
| 10 | + SDXL = 3 |
| 11 | + Unknown = -1 |
| 12 | + |
| 13 | + def __str__(self): |
| 14 | + return self.name |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def from_str(cls, str): |
| 18 | + try: |
| 19 | + return cls[str] |
| 20 | + except KeyError: |
| 21 | + return cls.Unknown |
| 22 | + |
| 23 | + def match(self, sd_model): |
| 24 | + if sd_model.is_sd1 and self == SDVersion.SD1: |
| 25 | + return True |
| 26 | + elif sd_model.is_sd2 and self == SDVersion.SD2: |
| 27 | + return True |
| 28 | + elif sd_model.is_sdxl and self == SDVersion.SDXL: |
| 29 | + return True |
| 30 | + elif self == SDVersion.Unknown: |
| 31 | + return True |
| 32 | + else: |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +class ModelType(Enum): |
| 37 | + UNET = 0 |
| 38 | + CONTROLNET = 1 |
| 39 | + LORA = 2 |
| 40 | + UNDEFINED = -1 |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def from_string(cls, s): |
| 44 | + return getattr(cls, s.upper(), None) |
| 45 | + |
| 46 | + def __str__(self): |
| 47 | + return self.name.lower() |
| 48 | + |
| 49 | + |
| 50 | +@dataclass |
| 51 | +class ModelConfig: |
| 52 | + profile: dict |
| 53 | + static_shapes: bool |
| 54 | + fp32: bool |
| 55 | + inpaint: bool |
| 56 | + refit: bool |
| 57 | + lora: bool |
| 58 | + vram: int |
| 59 | + unet_hidden_dim: int = 4 |
| 60 | + |
| 61 | + def is_compatible_from_dict(self, feed_dict: dict): |
| 62 | + distance = 0 |
| 63 | + for k, v in feed_dict.items(): |
| 64 | + _min, _opt, _max = self.profile[k] |
| 65 | + v_tensor = torch.Tensor(list(v.shape)) |
| 66 | + r_min = torch.Tensor(_max) - v_tensor |
| 67 | + r_opt = (torch.Tensor(_opt) - v_tensor).abs() |
| 68 | + r_max = v_tensor - torch.Tensor(_min) |
| 69 | + if torch.any(r_min < 0) or torch.any(r_max < 0): |
| 70 | + return (False, distance) |
| 71 | + distance += r_opt.sum() + 0.5 * (r_max.sum() + 0.5 * r_min.sum()) |
| 72 | + return (True, distance) |
| 73 | + |
| 74 | + def is_compatible( |
| 75 | + self, width: int, height: int, batch_size: int, max_embedding: int |
| 76 | + ): |
| 77 | + distance = 0 |
| 78 | + sample = self.profile["sample"] |
| 79 | + embedding = self.profile["encoder_hidden_states"] |
| 80 | + |
| 81 | + batch_size *= 2 |
| 82 | + width = width // 8 |
| 83 | + height = height // 8 |
| 84 | + |
| 85 | + _min, _opt, _max = sample |
| 86 | + if _min[0] > batch_size or _max[0] < batch_size: |
| 87 | + return (False, distance) |
| 88 | + if _min[2] > height or _max[2] < height: |
| 89 | + return (False, distance) |
| 90 | + if _min[3] > width or _max[3] < width: |
| 91 | + return (False, distance) |
| 92 | + |
| 93 | + _min_em, _opt_em, _max_em = embedding |
| 94 | + if _min_em[1] > max_embedding or _max_em[1] < max_embedding: |
| 95 | + return (False, distance) |
| 96 | + |
| 97 | + distance = ( |
| 98 | + abs(_opt[0] - batch_size) |
| 99 | + + abs(_opt[2] - height) |
| 100 | + + abs(_opt[3] - width) |
| 101 | + + 0.5 * (abs(_max[2] - height) + abs(_max[3] - width)) |
| 102 | + ) |
| 103 | + |
| 104 | + return (True, distance) |
| 105 | + |
| 106 | + |
| 107 | +class ModelConfigEncoder(JSONEncoder): |
| 108 | + def default(self, o: ModelConfig): |
| 109 | + return o.__dict__ |
| 110 | + |
| 111 | + |
| 112 | +@dataclass |
| 113 | +class ProfileSettings: |
| 114 | + bs_min: int |
| 115 | + bs_opt: int |
| 116 | + bs_max: int |
| 117 | + h_min: int |
| 118 | + h_opt: int |
| 119 | + h_max: int |
| 120 | + w_min: int |
| 121 | + w_opt: int |
| 122 | + w_max: int |
| 123 | + t_min: int |
| 124 | + t_opt: int |
| 125 | + t_max: int |
| 126 | + static_shape: bool = False |
| 127 | + |
| 128 | + def __str__(self) -> str: |
| 129 | + return "Batch Size: {}-{}-{}\nHeight: {}-{}-{}\nWidth: {}-{}-{}\nToken Count: {}-{}-{}".format( |
| 130 | + self.bs_min, |
| 131 | + self.bs_opt, |
| 132 | + self.bs_max, |
| 133 | + self.h_min, |
| 134 | + self.h_opt, |
| 135 | + self.h_max, |
| 136 | + self.w_min, |
| 137 | + self.w_opt, |
| 138 | + self.w_max, |
| 139 | + self.t_min, |
| 140 | + self.t_opt, |
| 141 | + self.t_max, |
| 142 | + ) |
| 143 | + |
| 144 | + def out(self): |
| 145 | + return ( |
| 146 | + self.bs_min, |
| 147 | + self.bs_opt, |
| 148 | + self.bs_max, |
| 149 | + self.h_min, |
| 150 | + self.h_opt, |
| 151 | + self.h_max, |
| 152 | + self.w_min, |
| 153 | + self.w_opt, |
| 154 | + self.w_max, |
| 155 | + self.t_min, |
| 156 | + self.t_opt, |
| 157 | + self.t_max, |
| 158 | + ) |
| 159 | + |
| 160 | + def token_to_dim(self, static_shapes: bool): |
| 161 | + self.t_min = (self.t_min // 75) * 77 |
| 162 | + self.t_opt = (self.t_opt // 75) * 77 |
| 163 | + self.t_max = (self.t_max // 75) * 77 |
| 164 | + |
| 165 | + if static_shapes: |
| 166 | + self.t_min = self.t_max = self.t_opt |
| 167 | + self.bs_min = self.bs_max = self.bs_opt |
| 168 | + self.h_min = self.h_max = self.h_opt |
| 169 | + self.w_min = self.w_max = self.w_opt |
| 170 | + self.static_shape = True |
| 171 | + |
| 172 | + def get_latent_dim(self): |
| 173 | + return ( |
| 174 | + self.h_min // 8, |
| 175 | + self.h_opt // 8, |
| 176 | + self.h_max // 8, |
| 177 | + self.w_min // 8, |
| 178 | + self.w_opt // 8, |
| 179 | + self.w_max // 8, |
| 180 | + ) |
| 181 | + |
| 182 | + def get_a1111_batch_dim(self): |
| 183 | + static_batch = self.bs_min == self.bs_max == self.bs_opt |
| 184 | + if self.t_max <= 77: |
| 185 | + return (self.bs_min * 2, self.bs_opt * 2, self.bs_max * 2) |
| 186 | + elif self.t_max > 77 and static_batch: |
| 187 | + return (self.bs_opt, self.bs_opt, self.bs_opt) |
| 188 | + elif self.t_max > 77 and not static_batch: |
| 189 | + if self.t_opt > 77: |
| 190 | + return (self.bs_min, self.bs_opt, self.bs_max * 2) |
| 191 | + return (self.bs_min, self.bs_opt * 2, self.bs_max * 2) |
| 192 | + else: |
| 193 | + raise Exception("Uncovered case in get_batch_dim") |
| 194 | + |
| 195 | + |
| 196 | +class ProfilePrests: |
| 197 | + def __init__(self): |
| 198 | + self.profile_presets = { |
| 199 | + "512x512 | Batch Size 1 (Static)": ProfileSettings( |
| 200 | + 1, 1, 1, 512, 512, 512, 512, 512, 512, 75, 75, 75 |
| 201 | + ), |
| 202 | + "768x768 | Batch Size 1 (Static)": ProfileSettings( |
| 203 | + 1, 1, 1, 768, 768, 768, 768, 768, 768, 75, 75, 75 |
| 204 | + ), |
| 205 | + "1024x1024 | Batch Size 1 (Static)": ProfileSettings( |
| 206 | + 1, 1, 1, 1024, 1024, 1024, 1024, 1024, 1024, 75, 75, 75 |
| 207 | + ), |
| 208 | + "256x256 - 512x512 | Batch Size 1-4": ProfileSettings( |
| 209 | + 1, 1, 4, 256, 512, 512, 256, 512, 512, 75, 75, 150 |
| 210 | + ), |
| 211 | + "512x512 - 768x768 | Batch Size 1-4": ProfileSettings( |
| 212 | + 1, 1, 4, 512, 512, 768, 512, 512, 768, 75, 75, 150 |
| 213 | + ), |
| 214 | + "768x768 - 1024x1024 | Batch Size 1-4": ProfileSettings( |
| 215 | + 1, 1, 4, 768, 1024, 1024, 768, 1024, 1024, 75, 75, 150 |
| 216 | + ), |
| 217 | + } |
| 218 | + self.default = ProfileSettings( |
| 219 | + 1, 1, 4, 512, 512, 768, 512, 512, 768, 75, 75, 150 |
| 220 | + ) |
| 221 | + self.default_xl = ProfileSettings( |
| 222 | + 1, 1, 1, 1024, 1024, 1024, 1024, 1024, 1024, 75, 75, 75 |
| 223 | + ) |
| 224 | + |
| 225 | + def get_settings_from_version(self, version: str): |
| 226 | + static = False |
| 227 | + if version == "Default": |
| 228 | + return *self.default.out(), static |
| 229 | + if "Static" in version: |
| 230 | + static = True |
| 231 | + return *self.profile_presets[version].out(), static |
| 232 | + |
| 233 | + def get_choices(self): |
| 234 | + return list(self.profile_presets.keys()) + ["Default"] |
| 235 | + |
| 236 | + def get_default(self, is_xl: bool): |
| 237 | + if is_xl: |
| 238 | + return self.default_xl |
| 239 | + return self.default |
0 commit comments