mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-08-14 10:30:05 +08:00
Fix VAE SageAttention integration tensor shape mismatch
- Add proper tensor reshaping in vae_sage_attention() wrapper - Convert VAE format (B, C, H, W) to SageAttention format (B, seq_len, dim) - Add fallback mechanism to pytorch attention if SageAttention fails - Enable SageAttention selection in vae_attention() function - Fixes ValueError: too many values to unpack (expected 3) error - Maintains compatibility with existing VAE attention functions This resolves the tensor shape incompatibility between VAE spatial attention and SageAttention sequence attention, allowing VAE operations to benefit from SageAttention optimization when --use-sage-attention flag is enabled.
This commit is contained in:
parent
4f1f26ac6c
commit
0b0753e708
@ -293,8 +293,44 @@ def pytorch_attention(q, k, v):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def vae_sage_attention(q, k, v):
|
||||||
|
"""Wrapper for attention_sage to work with VAE single-head attention"""
|
||||||
|
from ..attention import attention_sage
|
||||||
|
|
||||||
|
# Store original shape for output reshaping
|
||||||
|
orig_shape = q.shape
|
||||||
|
B = orig_shape[0]
|
||||||
|
C = orig_shape[1]
|
||||||
|
|
||||||
|
# Reshape from VAE format (B, C, H, W) to SageAttention format (B, seq_len, dim)
|
||||||
|
# Following the same pattern as xformers_attention
|
||||||
|
q, k, v = map(
|
||||||
|
lambda t: t.view(B, C, -1).transpose(1, 2).contiguous(),
|
||||||
|
(q, k, v),
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Call SageAttention with heads=1 (VAE uses single-head attention)
|
||||||
|
out = attention_sage(q, k, v, heads=1, skip_reshape=False)
|
||||||
|
# Reshape back to original VAE format
|
||||||
|
out = out.transpose(1, 2).reshape(orig_shape)
|
||||||
|
except Exception as e:
|
||||||
|
# Fallback to pytorch attention if SageAttention fails
|
||||||
|
import logging
|
||||||
|
logging.warning(f"SageAttention failed in VAE: {e}, falling back to pytorch attention")
|
||||||
|
# Reshape back to original format for fallback
|
||||||
|
q = q.transpose(1, 2).reshape(orig_shape)
|
||||||
|
k = k.transpose(1, 2).reshape(orig_shape)
|
||||||
|
v = v.transpose(1, 2).reshape(orig_shape)
|
||||||
|
out = pytorch_attention(q, k, v)
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
def vae_attention():
|
def vae_attention():
|
||||||
if model_management.xformers_enabled_vae():
|
if model_management.sage_attention_enabled():
|
||||||
|
logging.info("Using sage attention in VAE")
|
||||||
|
return vae_sage_attention
|
||||||
|
elif model_management.xformers_enabled_vae():
|
||||||
logging.info("Using xformers attention in VAE")
|
logging.info("Using xformers attention in VAE")
|
||||||
return xformers_attention
|
return xformers_attention
|
||||||
elif model_management.pytorch_attention_enabled_vae():
|
elif model_management.pytorch_attention_enabled_vae():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user