generated from okotaku/template
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_fast_norm_hook.py
144 lines (132 loc) · 5.77 KB
/
test_fast_norm_hook.py
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
import copy
import unittest
from mmengine.registry import MODELS
from mmengine.testing import RunnerTestCase
from torch import nn
from diffengine.engine.hooks import FastNormHook
from diffengine.models.editors import (
SDDataPreprocessor,
SDXLControlNetDataPreprocessor,
SDXLDataPreprocessor,
StableDiffusion,
StableDiffusionXL,
StableDiffusionXLControlNet,
)
from diffengine.models.losses import L2Loss
from diffengine.models.utils import WhiteNoise
try:
import apex
except ImportError:
apex = None
class TestFastNormHook(RunnerTestCase):
def setUp(self) -> None:
MODELS.register_module(name="StableDiffusion", module=StableDiffusion)
MODELS.register_module(
name="StableDiffusionXL", module=StableDiffusionXL)
MODELS.register_module(
name="SDDataPreprocessor", module=SDDataPreprocessor)
MODELS.register_module(
name="SDXLDataPreprocessor", module=SDXLDataPreprocessor)
MODELS.register_module(
name="StableDiffusionXLControlNet",
module=StableDiffusionXLControlNet)
MODELS.register_module(
name="SDXLControlNetDataPreprocessor",
module=SDXLControlNetDataPreprocessor)
MODELS.register_module(name="L2Loss", module=L2Loss)
MODELS.register_module(name="WhiteNoise", module=WhiteNoise)
return super().setUp()
def tearDown(self) -> None:
MODELS.module_dict.pop("StableDiffusion")
MODELS.module_dict.pop("StableDiffusionXL")
MODELS.module_dict.pop("SDDataPreprocessor")
MODELS.module_dict.pop("SDXLDataPreprocessor")
MODELS.module_dict.pop("StableDiffusionXLControlNet")
MODELS.module_dict.pop("SDXLControlNetDataPreprocessor")
MODELS.module_dict.pop("L2Loss")
MODELS.module_dict.pop("WhiteNoise")
return super().tearDown()
@unittest.skipIf(apex is None, "apex is not installed")
def test_init(self) -> None:
FastNormHook()
@unittest.skipIf(apex is None, "apex is not installed")
def test_before_train(self) -> None:
from apex.normalization import FusedLayerNorm
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.model.type = "StableDiffusion"
cfg.model.model = "diffusers/tiny-stable-diffusion-torch"
runner = self.build_runner(cfg)
hook = FastNormHook(fuse_text_encoder_ln=True)
assert isinstance(
runner.model.unet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
assert isinstance(
runner.model.text_encoder.text_model.encoder.layers[
0].layer_norm1, nn.LayerNorm)
# replace norm
hook.before_train(runner)
assert isinstance(
runner.model.unet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
assert isinstance(
runner.model.text_encoder.text_model.encoder.layers[
0].layer_norm1, FusedLayerNorm)
# Test StableDiffusionXL
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.model.type = "StableDiffusionXL"
cfg.model.model = "hf-internal-testing/tiny-stable-diffusion-xl-pipe"
runner = self.build_runner(cfg)
hook = FastNormHook(fuse_text_encoder_ln=True)
assert isinstance(
runner.model.unet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
assert isinstance(
runner.model.text_encoder_one.text_model.encoder.layers[
0].layer_norm1, nn.LayerNorm)
assert isinstance(
runner.model.text_encoder_two.text_model.encoder.layers[
0].layer_norm1, nn.LayerNorm)
# replace norm
hook.before_train(runner)
assert isinstance(
runner.model.unet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
assert isinstance(
runner.model.text_encoder_one.text_model.encoder.layers[
0].layer_norm1, FusedLayerNorm)
assert isinstance(
runner.model.text_encoder_two.text_model.encoder.layers[
0].layer_norm1, FusedLayerNorm)
# Test StableDiffusionXLControlNet
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.model.type = "StableDiffusionXLControlNet"
cfg.model.model = "hf-internal-testing/tiny-stable-diffusion-xl-pipe"
cfg.model.controlnet_model = "hf-internal-testing/tiny-controlnet-sdxl"
runner = self.build_runner(cfg)
hook = FastNormHook(fuse_text_encoder_ln=True)
assert isinstance(
runner.model.unet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
assert isinstance(
runner.model.controlnet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
assert isinstance(
runner.model.text_encoder_one.text_model.encoder.layers[
0].layer_norm1, nn.LayerNorm)
assert isinstance(
runner.model.text_encoder_two.text_model.encoder.layers[
0].layer_norm1, nn.LayerNorm)
# replace norm
hook.before_train(runner)
assert isinstance(
runner.model.unet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
assert isinstance(
runner.model.controlnet.down_blocks[
1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
assert isinstance(
runner.model.text_encoder_one.text_model.encoder.layers[
0].layer_norm1, FusedLayerNorm)
assert isinstance(
runner.model.text_encoder_two.text_model.encoder.layers[
0].layer_norm1, FusedLayerNorm)