ctrl-x icon indicating copy to clipboard operation
ctrl-x copied to clipboard

KeyError: 'appearance_cond'

Open MythicalChu opened this issue 2 years ago • 3 comments

Hi! Thank you for your work and answers on reddit! This issue occurs when the inference is supposed to drop the appearance/structure cond at the default 0.6 step% mark.

Ctrl-X inference: 60%|███████████████████████████████████████ | 6/10 [02:59<01:59, 30.00s/it] Traceback (most recent call last): File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\gradio\queueing.py", line 536, in process_events response = await route_utils.call_process_api( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\gradio\route_utils.py", line 322, in call_process_api output = await app.get_blocks().process_api( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\gradio\blocks.py", line 1935, in process_api result = await self.call_function( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\gradio\blocks.py", line 1520, in call_function prediction = await anyio.to_thread.run_sync( # type: ignore File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\anyio\to_thread.py", line 56, in run_sync return await get_async_backend().run_sync_in_worker_thread( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\anyio_backends_asyncio.py", line 2405, in run_sync_in_worker_thread return await future File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\anyio_backends_asyncio.py", line 914, in run result = context.run(func, *args) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\gradio\utils.py", line 826, in wrapper response = f(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\utils_contextlib.py", line 116, in decorate_context return func(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\app_ctrlx.py", line 187, in inference result, structure, appearance = pipe( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\utils_contextlib.py", line 116, in decorate_context return func(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\ctrl_x\pipelines\pipeline_sdxl.py", line 512, in call concat_noise_pred = self.unet( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1553, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1562, in _call_impl return forward_call(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\accelerate\hooks.py", line 170, in new_forward output = module._old_forward(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\diffusers\models\unets\unet_2d_condition.py", line 1250, in forward sample = self.mid_block( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1553, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1562, in _call_impl return forward_call(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\diffusers\models\unets\unet_2d_blocks.py", line 889, in forward hidden_states = attn( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1553, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1562, in _call_impl return forward_call(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\diffusers\models\transformers\transformer_2d.py", line 448, in forward hidden_states = block( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1553, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1562, in _call_impl return forward_call(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\diffusers\models\attention.py", line 329, in forward attn_output = self.attn1( File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1553, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\torch\nn\modules\module.py", line 1562, in _call_impl return forward_call(*args, **kwargs) File "C:\AI\CtrlX\ctrl-x\venv\lib\site-packages\diffusers\models\attention_processor.py", line 539, in forward return self.processor( File "C:\AI\CtrlX\ctrl-x\ctrl_x\utils\sdxl.py", line 178, in call hidden_states = appearance_transfer(hidden_states, query_normed, key_normed, batch_order=attn.batch_order) File "C:\AI\CtrlX\ctrl-x\ctrl_x\utils\feature.py", line 63, in appearance_transfer q_normed_dict["cond"], k_normed_dict["appearance_cond"], v_dict["appearance_cond"], KeyError: 'appearance_cond'

I'm running almost everything on default parameters, but only 10 steps and 768x768, but from my tests earlier before this recent update, it was happening regardless of step count or inference resolution. I'm also using sequential_cpu_offloading instead of model_offloading otherwise I crash OOM, and I'm on DirectML. But I'm guessing it's not these factors causing the error...

On the previous version I added some extra checks to bypass the injections in case appearance/structure wasn't found in the batch_order and that allowed it to work as intended (except that my outputs were full/extremely noisy, but I can't check the exact source of the problems with all these modifications I had to do, and inference being so slow on my card (rx580 (8Gb)))

This is what added to 'class AttnProcessor2_0' to circunvent the error: if "structure_cond" not in attn.batch_order: do_structure_control=False if "appearance_cond" not in attn.batch_order: do_appearance_control=False

Still, haven't been able to get a decent result, even with schedules to 1.0 instead of 0.6 to avoid the dropping of cond...

MythicalChu avatar Sep 29 '24 03:09 MythicalChu

I just pushed a new version of the code that supports sequential CPU offloading (now with ~3.8GB of peak VRAM usage). I tested it with your exact settings (10 steps, 768x768) and it works well! (I would recommend cloning a fresh version of this repo because it seems like you have made some changes to yours.)

From my own testing, however, 10 steps is too few for good appearance transfer (at least with appearance schedule of 0.6). I start to get passable results around 20 steps, so I recommend increasing your inference steps.

kuanhenglin avatar Sep 29 '24 19:09 kuanhenglin

Additionally, our implementation of Ctrl-X right now is not the most memory-efficient because we are batching all three branches (structure, appearance, and output) during generation, so the memory usage is higher than vanilla SDXL.

In the future, we plan on implementing a version of the pipeline where the three branches are computed separately and intermediate features cached for structure + appearance transfer, which should lower memory usage at the cost of code readability/cleanness and longer inference time. However, since this will take a good amount of code rewriting and reorganization don't expect this to come too soon :P

kuanhenglin avatar Sep 29 '24 19:09 kuanhenglin

I had that error both before your update and after. I was already using sequential offloading, since model offloading wasn't enough to avoid OOM on my 8Gb. I can't use a completely unmodified version of your code since it doesn't support DirectML/AMD, but anyway... I'll close this issue since this is too heavy for my machine regardless. I'll try again after the batch rewrite in the future! Best of luck!

MythicalChu avatar Sep 29 '24 22:09 MythicalChu