Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Tolerate missing `@torch.library.custom_op` (#7234)

FeepingCreature9c98c6358be2
diff --git a/comfy/ldm/modules/attention.py b/comfy/ldm/modules/attention.py
index 3e5089a6f..7908d1313 100644
--- a/comfy/ldm/modules/attention.py
+++ b/comfy/ldm/modules/attention.py
@@ -503,16 +503,23 @@ def attention_sage(q, k, v, heads, mask=None, attn_precision=None, skip_reshape=
return out
[email protected]_op("flash_attention::flash_attn", mutates_args=())
-def flash_attn_wrapper(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor,
- dropout_p: float = 0.0, causal: bool = False) -> torch.Tensor:
- return flash_attn_func(q, k, v, dropout_p=dropout_p, causal=causal)
-
-
-@flash_attn_wrapper.register_fake
-def flash_attn_fake(q, k, v, dropout_p=0.0, causal=False):
- # Output shape is the same as q
- return q.new_empty(q.shape)
+try:
+ @torch.library.custom_op("flash_attention::flash_attn", mutates_args=())
+ def flash_attn_wrapper(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor,
+ dropout_p: float = 0.0, causal: bool = False) -> torch.Tensor:
+ return flash_attn_func(q, k, v, dropout_p=dropout_p, causal=causal)
+
+
+ @flash_attn_wrapper.register_fake
+ def flash_attn_fake(q, k, v, dropout_p=0.0, causal=False):
+ # Output shape is the same as q
+ return q.new_empty(q.shape)
+except AttributeError as error:
+ FLASH_ATTN_ERROR = error
+
+ def flash_attn_wrapper(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor,
+ dropout_p: float = 0.0, causal: bool = False) -> torch.Tensor:
+ assert False, f"Could not define flash_attn_wrapper: {FLASH_ATTN_ERROR}"
def attention_flash(q, k, v, heads, mask=None, attn_precision=None, skip_reshape=False, skip_output_reshape=False):