-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vae encode example with test #294
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
044844f
[WIP] tracing_mode="symbolic" fails for VAE encode
aviator19941 13b682c
[WIP] Update VAE encode accuracy after diffusers change
aviator19941 0694390
[WIP] Add temp decomp for randn.generator to bypass VAE encode error
aviator19941 7ae597c
Update sd_test to include vae encode
aviator19941 669b145
Fix black formatting and keep decomps in separate file
aviator19941 1ba10b7
Add get_decompositions import for passes.py
aviator19941 f97b1ea
Add expected torch stable version
aviator19941 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import torch | ||
from torch._prims_common.wrappers import out_wrapper | ||
from torch._prims_common import ( | ||
DeviceLikeType, | ||
TensorLikeType, | ||
) | ||
import torch._refs as _refs | ||
from torch._decomp import get_decompositions, register_decomposition | ||
from torch import Tensor | ||
from typing import Dict, List, Tuple, Optional | ||
|
||
|
||
@register_decomposition(torch.ops.aten._scaled_dot_product_flash_attention.default) | ||
def scaled_dot_product_flash_attention( | ||
query, | ||
key, | ||
value, | ||
dropout_p: float = 0.0, | ||
is_causal: bool = False, | ||
return_debug_mask: bool = False, | ||
*, | ||
scale: float = None, | ||
) -> Tuple[Tensor, Tensor, Tensor, Tensor, int, int, Tensor, Tensor, Tensor]: | ||
dtype = query.dtype | ||
batchSize, num_head, qSize, headSize = ( | ||
query.shape[0], | ||
query.shape[1], | ||
query.shape[2], | ||
query.shape[3], | ||
) | ||
|
||
logsumexp = torch.empty([batchSize, qSize, num_head, headSize], dtype=torch.float) | ||
cum_seq_q, cum_seq_k = torch.empty([], dtype=torch.long), torch.empty( | ||
[], dtype=torch.long | ||
) | ||
max_q, max_k = 0, 0 | ||
philox_seed, philox_offset = torch.empty([], dtype=torch.long), torch.empty( | ||
[], dtype=torch.long | ||
) | ||
debug_attn_mask = torch.empty( | ||
[], | ||
dtype=query.dtype, | ||
device="cpu", | ||
requires_grad=query.requires_grad, | ||
) | ||
output, _ = torch.ops.aten._scaled_dot_product_attention_math.default( | ||
query, key, value, None, dropout_p, is_causal, None, scale=scale | ||
) | ||
output = output.transpose(1, 2).contiguous(memory_format=torch.contiguous_format) | ||
return ( | ||
output.transpose(1, 2), | ||
logsumexp, | ||
cum_seq_q, | ||
cum_seq_k, | ||
max_q, | ||
max_k, | ||
philox_seed, | ||
philox_offset, | ||
debug_attn_mask, | ||
) | ||
|
||
|
||
# manually add decomposition to bypass the error that comes | ||
# from VAE encode(inp).latent_dist.sample() failing to symbolically | ||
# trace from torch fx. | ||
aviator19941 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# diffusers side issue: https://github.com/huggingface/diffusers/issues/6239 | ||
# temporary torch fix: https://github.com/pytorch/pytorch/issues/107170 | ||
@register_decomposition(torch.ops.aten.randn.generator) | ||
@out_wrapper() | ||
def randn_generator( | ||
*shape, | ||
generator: Optional[torch.Generator] = None, | ||
dtype: Optional[torch.dtype] = None, | ||
device: Optional[DeviceLikeType] = None, | ||
layout: Optional[torch.layout] = None, | ||
requires_grad: bool = False, | ||
pin_memory: bool = False, | ||
) -> TensorLikeType: | ||
# We should eventually support the generator overload. | ||
# However, if someone passes in a None generator explicitly, | ||
# we can jut fall back to randn.default | ||
if generator is None: | ||
return _refs.randn( | ||
*shape, | ||
dtype=dtype, | ||
device=device, | ||
layout=layout, | ||
requires_grad=requires_grad, | ||
pin_memory=pin_memory, | ||
) | ||
return NotImplemented |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this being here, but I can't think of a better place for now.