File size: 9,387 Bytes
1ae8021 |
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 |
import sys
from typing import Any, Callable, Union
from torch import nn
from torch.utils.hooks import RemovableHandle
from ldm.modules.diffusionmodules.openaimodel import (
TimestepEmbedSequential,
)
from ldm.modules.attention import (
SpatialTransformer,
BasicTransformerBlock,
CrossAttention,
MemoryEfficientCrossAttention,
)
from ldm.modules.diffusionmodules.openaimodel import (
ResBlock,
)
from modules.processing import StableDiffusionProcessing
from modules import shared
class ForwardHook:
def __init__(self, module: nn.Module, fn: Callable[[nn.Module, Callable[..., Any], Any], Any]):
self.o = module.forward
self.fn = fn
self.module = module
self.module.forward = self.forward
def remove(self):
if self.module is not None and self.o is not None:
self.module.forward = self.o
self.module = None
self.o = None
self.fn = None
def forward(self, *args, **kwargs):
if self.module is not None and self.o is not None:
if self.fn is not None:
return self.fn(self.module, self.o, *args, **kwargs)
return None
class SDHook:
def __init__(self, enabled: bool):
self._enabled = enabled
self._handles: list[Union[RemovableHandle,ForwardHook]] = []
@property
def enabled(self):
return self._enabled
@property
def batch_num(self):
return shared.state.job_no
@property
def step_num(self):
return shared.state.current_image_sampling_step
def __enter__(self):
if self.enabled:
pass
def __exit__(self, exc_type, exc_value, traceback):
if self.enabled:
for handle in self._handles:
handle.remove()
self._handles.clear()
self.dispose()
def dispose(self):
pass
def setup(
self,
p: StableDiffusionProcessing
):
if not self.enabled:
return
wrapper = getattr(p.sd_model, "model", None)
unet: Union[nn.Module,None] = getattr(wrapper, "diffusion_model", None) if wrapper is not None else None
vae: Union[nn.Module,None] = getattr(p.sd_model, "first_stage_model", None)
clip: Union[nn.Module,None] = getattr(p.sd_model, "cond_stage_model", None)
assert unet is not None, "p.sd_model.diffusion_model is not found. broken model???"
self._do_hook(p, p.sd_model, unet=unet, vae=vae, clip=clip) # type: ignore
self.on_setup()
def on_setup(self):
pass
def _do_hook(
self,
p: StableDiffusionProcessing,
model: Any,
unet: Union[nn.Module,None],
vae: Union[nn.Module,None],
clip: Union[nn.Module,None]
):
assert model is not None, "empty model???"
if clip is not None:
self.hook_clip(p, clip)
if unet is not None:
self.hook_unet(p, unet)
if vae is not None:
self.hook_vae(p, vae)
def hook_vae(
self,
p: StableDiffusionProcessing,
vae: nn.Module
):
pass
def hook_unet(
self,
p: StableDiffusionProcessing,
unet: nn.Module
):
pass
def hook_clip(
self,
p: StableDiffusionProcessing,
clip: nn.Module
):
pass
def hook_layer(
self,
module: Union[nn.Module,Any],
fn: Callable[[nn.Module, tuple, Any], Any]
):
if not self.enabled:
return
assert module is not None
assert isinstance(module, nn.Module)
self._handles.append(module.register_forward_hook(fn))
def hook_layer_pre(
self,
module: Union[nn.Module,Any],
fn: Callable[[nn.Module, tuple], Any]
):
if not self.enabled:
return
assert module is not None
assert isinstance(module, nn.Module)
self._handles.append(module.register_forward_pre_hook(fn))
def hook_forward(
self,
module: Union[nn.Module,Any],
fn: Callable[[nn.Module, Callable[..., Any], Any], Any]
):
assert module is not None
assert isinstance(module, nn.Module)
self._handles.append(ForwardHook(module, fn))
def log(self, msg: str):
print(msg, file=sys.stderr)
# enumerate SpatialTransformer in TimestepEmbedSequential
def each_transformer(unet_block: TimestepEmbedSequential):
for block in unet_block.children():
if isinstance(block, SpatialTransformer):
yield block
# enumerate BasicTransformerBlock in SpatialTransformer
def each_basic_block(trans: SpatialTransformer):
for block in trans.transformer_blocks.children():
if isinstance(block, BasicTransformerBlock):
yield block
# enumerate Attention Layers in TimestepEmbedSequential
# each_transformer + each_basic_block
def each_attns(unet_block: TimestepEmbedSequential):
for n, trans in enumerate(each_transformer(unet_block)):
for depth, basic_block in enumerate(each_basic_block(trans)):
# attn1: Union[CrossAttention,MemoryEfficientCrossAttention]
# attn2: Union[CrossAttention,MemoryEfficientCrossAttention]
attn1, attn2 = basic_block.attn1, basic_block.attn2
assert isinstance(attn1, CrossAttention) or isinstance(attn1, MemoryEfficientCrossAttention)
assert isinstance(attn2, CrossAttention) or isinstance(attn2, MemoryEfficientCrossAttention)
yield n, depth, attn1, attn2
def each_unet_attn_layers(unet: nn.Module):
def get_attns(layer_index: int, block: TimestepEmbedSequential, format: str):
for n, d, attn1, attn2 in each_attns(block):
kwargs = {
'layer_index': layer_index,
'trans_index': n,
'block_index': d
}
yield format.format(attn_name='sattn', **kwargs), attn1
yield format.format(attn_name='xattn', **kwargs), attn2
def enumerate_all(blocks: nn.ModuleList, format: str):
for idx, block in enumerate(blocks.children()):
if isinstance(block, TimestepEmbedSequential):
yield from get_attns(idx, block, format)
inputs: nn.ModuleList = unet.input_blocks # type: ignore
middle: TimestepEmbedSequential = unet.middle_block # type: ignore
outputs: nn.ModuleList = unet.output_blocks # type: ignore
yield from enumerate_all(inputs, 'IN{layer_index:02}_{trans_index:02}_{block_index:02}_{attn_name}')
yield from get_attns(0, middle, 'M{layer_index:02}_{trans_index:02}_{block_index:02}_{attn_name}')
yield from enumerate_all(outputs, 'OUT{layer_index:02}_{trans_index:02}_{block_index:02}_{attn_name}')
def each_unet_transformers(unet: nn.Module):
def get_trans(layer_index: int, block: TimestepEmbedSequential, format: str):
for n, trans in enumerate(each_transformer(block)):
kwargs = {
'layer_index': layer_index,
'block_index': n,
'block_name': 'trans',
}
yield format.format(**kwargs), trans
def enumerate_all(blocks: nn.ModuleList, format: str):
for idx, block in enumerate(blocks.children()):
if isinstance(block, TimestepEmbedSequential):
yield from get_trans(idx, block, format)
inputs: nn.ModuleList = unet.input_blocks # type: ignore
middle: TimestepEmbedSequential = unet.middle_block # type: ignore
outputs: nn.ModuleList = unet.output_blocks # type: ignore
yield from enumerate_all(inputs, 'IN{layer_index:02}_{block_index:02}_{block_name}')
yield from get_trans(0, middle, 'M{layer_index:02}_{block_index:02}_{block_name}')
yield from enumerate_all(outputs, 'OUT{layer_index:02}_{block_index:02}_{block_name}')
def each_resblock(unet_block: TimestepEmbedSequential):
for block in unet_block.children():
if isinstance(block, ResBlock):
yield block
def each_unet_resblock(unet: nn.Module):
def get_resblock(layer_index: int, block: TimestepEmbedSequential, format: str):
for n, res in enumerate(each_resblock(block)):
kwargs = {
'layer_index': layer_index,
'block_index': n,
'block_name': 'resblock',
}
yield format.format(**kwargs), res
def enumerate_all(blocks: nn.ModuleList, format: str):
for idx, block in enumerate(blocks.children()):
if isinstance(block, TimestepEmbedSequential):
yield from get_resblock(idx, block, format)
inputs: nn.ModuleList = unet.input_blocks # type: ignore
middle: TimestepEmbedSequential = unet.middle_block # type: ignore
outputs: nn.ModuleList = unet.output_blocks # type: ignore
yield from enumerate_all(inputs, 'IN{layer_index:02}_{block_index:02}_{block_name}')
yield from get_resblock(0, middle, 'M{layer_index:02}_{block_index:02}_{block_name}')
yield from enumerate_all(outputs, 'OUT{layer_index:02}_{block_index:02}_{block_name}')
|