[Performance 2/6] Replace einops.rearrange with torch native ops#15804
Merged
AUTOMATIC1111 merged 1 commit intoAUTOMATIC1111:devfrom Jun 8, 2024
Merged
[Performance 2/6] Replace einops.rearrange with torch native ops#15804AUTOMATIC1111 merged 1 commit intoAUTOMATIC1111:devfrom
AUTOMATIC1111 merged 1 commit intoAUTOMATIC1111:devfrom
Conversation
4 tasks
wfjsw
reviewed
May 17, 2024
| """ | ||
| b, n, _ = t.shape # Get the batch size (b) and sequence length (n) | ||
| d = t.shape[2] // h # Determine the depth per head | ||
| return t.reshape(b, n, h, d) |
Contributor
There was a problem hiding this comment.
t.reshape(b,n,h,-1) should achieve similar result without having to explicitly calculate d
gel-crabs
reviewed
May 27, 2024
|
|
||
| q = _reshape(q_in) | ||
| k = _reshape(k_in) | ||
| v = _reshape(v_in) |
Contributor
There was a problem hiding this comment.
This can be done in 1 line with q, k, v = (_reshape(t) for t in (q_in, k_in, v_in))
AUTOMATIC1111
approved these changes
Jun 8, 2024
AUTOMATIC1111
added a commit
that referenced
this pull request
Jun 8, 2024
lllyasviel
added a commit
to lllyasviel/stable-diffusion-webui-forge
that referenced
this pull request
Aug 8, 2024
wkpark
added a commit
to wkpark/stable-diffusion-webui
that referenced
this pull request
Sep 8, 2024
* replace rearrange to view AUTOMATIC1111#15804 * see also lllyasviel/stable-diffusion-webui-forge@79adfa8 * conditional use torch.rms_norm for torch 2.4 * fix RMSNorm() for clear: use torch.ones()
wkpark
added a commit
to wkpark/stable-diffusion-webui
that referenced
this pull request
Sep 8, 2024
* replace rearrange to view AUTOMATIC1111#15804 * see also lllyasviel/stable-diffusion-webui-forge@79adfa8 * conditional use torch.rms_norm for torch 2.4 * fix RMSNorm() for clear: use torch.ones()
ruchej
pushed a commit
to ruchej/stable-diffusion-webui
that referenced
this pull request
Sep 30, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
According to lllyasviel/stable-diffusion-webui-forge#716 (comment),
einops.rearrangecalls in crossattn is causing extra overhead. Replacing it with torch native ops can save ~55ms/it.Screenshots/videos:
TODO
There are other places where
einops.rearrangecan be replaced by torch native ops, but this one in CrossAttn is the most critical one. Instrument the usage ofeinops.rearrangeelsewhere might also yield some improvements.Checklist: