Spaces:
Paused
Paused
File size: 12,654 Bytes
3f9c56c |
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 |
import unittest.mock
import importlib
from typing import Any
utils = importlib.import_module('extensions.sd-webui-controlnet.tests.utils', 'utils')
utils.setup_test_env()
from modules import processing, scripts, shared
from scripts import controlnet, external_code, batch_hijack
batch_hijack.instance.undo_hijack()
original_process_images_inner = processing.process_images_inner
class TestBatchHijack(unittest.TestCase):
@unittest.mock.patch('modules.script_callbacks.on_script_unloaded')
def setUp(self, on_script_unloaded_mock):
self.on_script_unloaded_mock = on_script_unloaded_mock
self.batch_hijack_object = batch_hijack.BatchHijack()
self.batch_hijack_object.do_hijack()
def tearDown(self):
self.batch_hijack_object.undo_hijack()
def test_do_hijack__registers_on_script_unloaded(self):
self.on_script_unloaded_mock.assert_called_once_with(self.batch_hijack_object.undo_hijack)
def test_do_hijack__call_once__hijacks_once(self):
self.assertEqual(getattr(processing, '__controlnet_original_process_images_inner'), original_process_images_inner)
self.assertEqual(processing.process_images_inner, self.batch_hijack_object.processing_process_images_hijack)
@unittest.mock.patch('modules.processing.__controlnet_original_process_images_inner')
def test_do_hijack__multiple_times__hijacks_once(self, process_images_inner_mock):
self.batch_hijack_object.do_hijack()
self.batch_hijack_object.do_hijack()
self.batch_hijack_object.do_hijack()
self.assertEqual(process_images_inner_mock, getattr(processing, '__controlnet_original_process_images_inner'))
class TestGetControlNetBatchesWorks(unittest.TestCase):
def setUp(self):
self.p = unittest.mock.MagicMock()
self.p.scripts = scripts.scripts_txt2img
self.cn_script = controlnet.Script()
self.p.scripts.alwayson_scripts = [self.cn_script]
self.p.script_args = []
def tearDown(self):
batch_hijack.instance.dispatch_callbacks(batch_hijack.instance.postprocess_batch_callbacks, self.p)
def assert_get_cn_batches_works(self, batch_images_list):
self.cn_script.args_from = 0
self.cn_script.args_to = self.cn_script.args_from + len(self.p.script_args)
is_cn_batch, batches, output_dir, _ = batch_hijack.get_cn_batches(self.p)
batch_hijack.instance.dispatch_callbacks(batch_hijack.instance.process_batch_callbacks, self.p, batches, output_dir)
batch_units = [unit for unit in self.p.script_args if getattr(unit, 'input_mode', batch_hijack.InputMode.SIMPLE) == batch_hijack.InputMode.BATCH]
if batch_units:
self.assertEqual(min(len(list(unit.batch_images)) for unit in batch_units), len(batches))
else:
self.assertEqual(1, len(batches))
for i, unit in enumerate(self.cn_script.enabled_units):
self.assertListEqual(batch_images_list[i], list(unit.batch_images))
def test_get_cn_batches__empty(self):
is_batch, batches, _, _ = batch_hijack.get_cn_batches(self.p)
self.assertEqual(1, len(batches))
self.assertEqual(is_batch, False)
def test_get_cn_batches__1_simple(self):
self.p.script_args.append(external_code.ControlNetUnit(image=get_dummy_image()))
self.assert_get_cn_batches_works([
[self.p.script_args[0].image],
])
def test_get_cn_batches__2_simples(self):
self.p.script_args.extend([
external_code.ControlNetUnit(image=get_dummy_image(0)),
external_code.ControlNetUnit(image=get_dummy_image(1)),
])
self.assert_get_cn_batches_works([
[get_dummy_image(0)],
[get_dummy_image(1)],
])
def test_get_cn_batches__1_batch(self):
self.p.script_args.extend([
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(0),
get_dummy_image(1),
],
),
])
self.assert_get_cn_batches_works([
[
get_dummy_image(0),
get_dummy_image(1),
],
])
def test_get_cn_batches__2_batches(self):
self.p.script_args.extend([
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(0),
get_dummy_image(1),
],
),
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(2),
get_dummy_image(3),
],
),
])
self.assert_get_cn_batches_works([
[
get_dummy_image(0),
get_dummy_image(1),
],
[
get_dummy_image(2),
get_dummy_image(3),
],
])
def test_get_cn_batches__2_mixed(self):
self.p.script_args.extend([
external_code.ControlNetUnit(image=get_dummy_image(0)),
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(1),
get_dummy_image(2),
],
),
])
self.assert_get_cn_batches_works([
[
get_dummy_image(0),
get_dummy_image(0),
],
[
get_dummy_image(1),
get_dummy_image(2),
],
])
def test_get_cn_batches__3_mixed(self):
self.p.script_args.extend([
external_code.ControlNetUnit(image=get_dummy_image(0)),
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(1),
get_dummy_image(2),
get_dummy_image(3),
],
),
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(4),
get_dummy_image(5),
],
),
])
self.assert_get_cn_batches_works([
[
get_dummy_image(0),
get_dummy_image(0),
],
[
get_dummy_image(1),
get_dummy_image(2),
],
[
get_dummy_image(4),
get_dummy_image(5),
],
])
class TestProcessImagesPatchWorks(unittest.TestCase):
@unittest.mock.patch('modules.script_callbacks.on_script_unloaded')
def setUp(self, on_script_unloaded_mock):
self.on_script_unloaded_mock = on_script_unloaded_mock
self.p = unittest.mock.MagicMock()
self.p.scripts = scripts.scripts_txt2img
self.cn_script = controlnet.Script()
self.p.scripts.alwayson_scripts = [self.cn_script]
self.p.script_args = []
self.p.all_seeds = [0]
self.p.all_subseeds = [0]
self.old_model, shared.sd_model = shared.sd_model, unittest.mock.MagicMock()
self.batch_hijack_object = batch_hijack.BatchHijack()
self.callbacks_mock = unittest.mock.MagicMock()
self.batch_hijack_object.process_batch_callbacks.append(self.callbacks_mock.process)
self.batch_hijack_object.process_batch_each_callbacks.append(self.callbacks_mock.process_each)
self.batch_hijack_object.postprocess_batch_each_callbacks.insert(0, self.callbacks_mock.postprocess_each)
self.batch_hijack_object.postprocess_batch_callbacks.insert(0, self.callbacks_mock.postprocess)
self.batch_hijack_object.do_hijack()
shared.state.begin()
def tearDown(self):
shared.state.end()
self.batch_hijack_object.undo_hijack()
shared.sd_model = self.old_model
@unittest.mock.patch('modules.processing.__controlnet_original_process_images_inner')
def assert_process_images_hijack_called(self, process_images_mock, batch_count):
process_images_mock.return_value = processing.Processed(self.p, [get_dummy_image('output')])
with unittest.mock.patch.dict(shared.opts.data, {
'controlnet_show_batch_images_in_ui': True,
}):
res = processing.process_images_inner(self.p)
self.assertEqual(res, process_images_mock.return_value)
if batch_count > 0:
self.callbacks_mock.process.assert_called()
self.callbacks_mock.postprocess.assert_called()
else:
self.callbacks_mock.process.assert_not_called()
self.callbacks_mock.postprocess.assert_not_called()
self.assertEqual(self.callbacks_mock.process_each.call_count, batch_count)
self.assertEqual(self.callbacks_mock.postprocess_each.call_count, batch_count)
def test_process_images_no_units_forwards(self):
self.assert_process_images_hijack_called(batch_count=0)
def test_process_images__only_simple_units__forwards(self):
self.p.script_args = [
external_code.ControlNetUnit(image=get_dummy_image()),
external_code.ControlNetUnit(image=get_dummy_image()),
]
self.assert_process_images_hijack_called(batch_count=0)
def test_process_images__1_batch_1_unit__runs_1_batch(self):
self.p.script_args = [
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(),
],
),
]
self.assert_process_images_hijack_called(batch_count=1)
def test_process_images__2_batches_1_unit__runs_2_batches(self):
self.p.script_args = [
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(0),
get_dummy_image(1),
],
),
]
self.assert_process_images_hijack_called(batch_count=2)
def test_process_images__8_batches_1_unit__runs_8_batches(self):
batch_count = 8
self.p.script_args = [
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[get_dummy_image(i) for i in range(batch_count)]
),
]
self.assert_process_images_hijack_called(batch_count=batch_count)
def test_process_images__1_batch_2_units__runs_1_batch(self):
self.p.script_args = [
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[get_dummy_image(0)]
),
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[get_dummy_image(1)]
),
]
self.assert_process_images_hijack_called(batch_count=1)
def test_process_images__2_batches_2_units__runs_2_batches(self):
self.p.script_args = [
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(0),
get_dummy_image(1),
],
),
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(2),
get_dummy_image(3),
],
),
]
self.assert_process_images_hijack_called(batch_count=2)
def test_process_images__3_batches_2_mixed_units__runs_3_batches(self):
self.p.script_args = [
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.BATCH,
batch_images=[
get_dummy_image(0),
get_dummy_image(1),
get_dummy_image(2),
],
),
controlnet.UiControlNetUnit(
input_mode=batch_hijack.InputMode.SIMPLE,
image=get_dummy_image(3),
),
]
self.assert_process_images_hijack_called(batch_count=3)
def get_dummy_image(name: Any = 0):
return f'base64#{name}...'
if __name__ == '__main__':
unittest.main() |