lyn-rgb / FreeU_Diffusers

"FreeU: Free Lunch in Diffusion U-Net" for Huggingface Diffusers
Apache License 2.0
94 stars 9 forks source link

How to use it in SimpleCrossAttnUpBlock2D? #8

Closed americanexplorer13 closed 11 months ago

americanexplorer13 commented 11 months ago

I've tried to change your code in order to maintain SimpleCrossAttnUpBlock2D however it seems that shapes doesn't fit up. How can I do it?


  File "/usr/local/lib/python3.9/dist-packages/gradio/routes.py", line 523, in run_predict
    output = await app.get_blocks().process_api(
  File "/usr/local/lib/python3.9/dist-packages/gradio/blocks.py", line 1437, in process_api
    result = await self.call_function(
  File "/usr/local/lib/python3.9/dist-packages/gradio/blocks.py", line 1109, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/usr/local/lib/python3.9/dist-packages/anyio/to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/usr/local/lib/python3.9/dist-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
  File "/usr/local/lib/python3.9/dist-packages/anyio/_backends/_asyncio.py", line 807, in run
    result = context.run(func, *args)
  File "/usr/local/lib/python3.9/dist-packages/gradio/utils.py", line 865, in wrapper
    response = f(*args, **kwargs)
  File "/home/ubuntu/mimesis-ml-gan-backend/app.py", line 128, in generate
    image = pipe(image=input_image,
  File "/usr/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/ubuntu/mimesis-ml-gan-backend/src/diffusions/kandinsky/pipeline_kandinsky_img2img_scheduler.py", line 125, in __call__
    noise_pred = self.unet(
  File "/usr/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/diffusers/models/unet_2d_condition.py", line 1020, in forward
    sample = upsample_block(
  File "/usr/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/ubuntu/mimesis-ml-gan-backend/free_lunch_utils.py", line 166, in forward
    hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1)
RuntimeError: Tensors must have same number of dimensions: got 3 and 4 ```

Thanks! 
lyn-rgb commented 11 months ago

This does not seem to be a problem with freeu, but rather a mismatch in the number of channels between your backbone features and skip features. The latest diffusers has built-in freeu, you can use it more conveniently. Please refer https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/unet_2d_condition.py line 737 and 761

americanexplorer13 commented 11 months ago

@lyn-rgb hey, thank you for your answer! Yes, I already find that the code for SimpleCrossAttnUpBlock is not really similar to CrossAttnUpBlock, so yep, I've already fixed it!