Uh oh!
There was an error while loading. Please reload this page.
Redo custom attention processor to support other attention types - #6550
Redo custom attention processor to support other attention types#6550StAlKeR7779 wants to merge 27 commits into
Conversation
RyanJDick
commented
Jun 27, 2024
I haven't looked at the code yet, but do you know if there are still use cases for using attention processors other than Torch 2.0 SDP? Based on the benchmarking that diffusers has done, it seems like the all around best choice. But maybe there are still reasons to use other implementation e.g. very-low-vram system? |
StAlKeR7779
commented
Jun 27, 2024
I thought roughly same: |
psychedelicious
commented
Jun 28, 2024
On CUDA, torch's SDP was faster than xformers for me when I last checked a month or so back. IIRC it was just a couple % faster. |
RyanJDick
commented
Jul 4, 2024
I thought about this some more, and I'm hesitant to proceed with trying to merge this until we have more clarity around which attention implementations we actually want to support. Right now, we have _adjust_memory_efficient_attention, which tries to configure attention based on the config and the system properties. The logic in this function is outdated, and I think there has been hesitation to change it out of fear of causing a regression on some systems. Let's get to the bottom of this, before deciding how to proceed with this PR. My current guess is that just supporting torch SDP and sliced attention would cover all use cases. But, we need to do some testing to determine if this is accurate. A few data points to consider:
@StAlKeR7779 do you want to look into this? |
@RyanJDick ok, I removed |
Co-Authored-By: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
psychedelicious
commented
Aug 4, 2024
pls run |
Co-Authored-By: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
There was a problem hiding this comment.
Looks good to me. I just did a basic smoke test - looks like others have done more rigorous testing.
A few minor things:
- Delete
_ignore_xformers_triton_message_on_windows - Delete
logging.getLogger("xformers").addFilter(lambda record: "A matching Triton is not available" not in record.getMessage()) - Remove
xformersinstructions from020_INSTALL_MANUAL.md
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-Authored-By: Ryan Dick <14897797+RyanJDick@users.noreply.github.com>
ebr
left a comment
There was a problem hiding this comment.
Only requesting changes here to ensure we don't merge this before testing on a couple of older GPUs. will test it asap
How critical is it to remove
Other than that, |
hipsterusername
commented
Aug 7, 2024
That seems reasonable, with a configurable override if user wants to force one. Does look like we should add it back. |
ebr
commented
Aug 7, 2024
The PyTorch blog says Flash Attention is supported from sm80 compute capability onwards: https://pytorch.org/blog/accelerated-pytorch-2/, so perhaps we should default to |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
RyanJDick
commented
Aug 8, 2024
Not for this PR, but I did some performance testing and we'll probably want to address this at some point: SDXL: >>> Time taken to prepare attention processors: 0.10069823265075684s
>>> Time taken to prepare attention processors: 0.07877492904663086s
>>> Time taken to set attention processors: 0.1278061866760254s
>>> Time taken to reset attention processors: 0.13225793838500977sCode used to measure: defapply_custom_attention(self, unet: UNet2DConditionModel):
"""A context manager that patches `unet` with CustomAttnProcessor2_0 attention layers."""start=time.time()
attn_procs=self._prepare_attention_processors(unet)
time_1=time.time()
print(f">>> Time taken to prepare attention processors: {time_1-start}s")
orig_attn_processors=unet.attn_processorstime_2=time.time()
print(f">>> Time taken to prepare attention processors: {time_2-time_1}s")
try:
# Note to future devs: set_attn_processor(...) does something slightly unexpected - it pops elements from# the passed dict. So, if you wanted to keep the dict for future use, you'd have to make a# moderately-shallow copy of it. E.g. `attn_procs_copy = {k: v for k, v in attn_procs.items()}`.unet.set_attn_processor(attn_procs)
time_3=time.time()
print(f">>> Time taken to set attention processors: {time_3-time_2}s")
yieldNonefinally:
time_4=time.time()
unet.set_attn_processor(orig_attn_processors)
time_5=time.time()
print(f">>> Time taken to reset attention processors: {time_5-time_4}s") |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ebr
left a comment
There was a problem hiding this comment.
Tested after changes, seeing expected performance increases on Ampere and no performance degradation on Pascal. LGTM!!
RyanJDick
left a comment
There was a problem hiding this comment.
I think this is just about good-to-go. There are a couple minor requests for docs, but otherwise the code looks good to me. (I haven't tested all cases myself, but it sounds like others have.)
Uh oh!
There was an error while loading. Please reload this page.
…d cuda, multihead xformers for high heads count)
Co-Authored-By: Ryan Dick <14897797+RyanJDick@users.noreply.github.com>
It looks like there was a significant re-write of the attention logic after the latest round of review and testing on this PR. @StAlKeR7779 can you shed some light on the benefits / motivation for that latest re-write? Given the amount of testing and discussion that went into this branch before the re-write, I'm wondering if we should revert those latest changes, merge this PR, and then open a new PR with the changes. Let me know what you think. |

Summary
Current attention processor implements only
torch-sdpattention type, so when any ip-adapter or regional prompt used, we override model to runtorch-sdpattention.New attention processor combines 4 attention processors(
normal,sliced,xformers,torch-sdp) by moving parts of attention that differs(mask preparation and attention itself), to separate function call, where required implementation executed.Related Issues / Discussions
None
QA Instructions
Change
attention_typeininvokeai.yamland then run generation with ip-adapter or regional prompt.Merge Plan
None?
Checklist
@dunkeroni@RyanJDick