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.

Allow disabling pe in flux code for some other models.

comfyanonymous3b19fc76e346
diff --git a/comfy/ldm/flux/math.py b/comfy/ldm/flux/math.py
index c0cbd2914..3e0978176 100644
--- a/comfy/ldm/flux/math.py
+++ b/comfy/ldm/flux/math.py
@@ -10,10 +10,11 @@ def attention(q: Tensor, k: Tensor, v: Tensor, pe: Tensor, mask=None) -> Tensor:
q_shape = q.shape
k_shape = k.shape
- q = q.to(dtype=pe.dtype).reshape(*q.shape[:-1], -1, 1, 2)
- k = k.to(dtype=pe.dtype).reshape(*k.shape[:-1], -1, 1, 2)
- q = (pe[..., 0] * q[..., 0] + pe[..., 1] * q[..., 1]).reshape(*q_shape).type_as(v)
- k = (pe[..., 0] * k[..., 0] + pe[..., 1] * k[..., 1]).reshape(*k_shape).type_as(v)
+ if pe is not None:
+ q = q.to(dtype=pe.dtype).reshape(*q.shape[:-1], -1, 1, 2)
+ k = k.to(dtype=pe.dtype).reshape(*k.shape[:-1], -1, 1, 2)
+ q = (pe[..., 0] * q[..., 0] + pe[..., 1] * q[..., 1]).reshape(*q_shape).type_as(v)
+ k = (pe[..., 0] * k[..., 0] + pe[..., 1] * k[..., 1]).reshape(*k_shape).type_as(v)
heads = q.shape[1]
x = optimized_attention(q, k, v, heads, skip_reshape=True, mask=mask)
diff --git a/comfy/ldm/flux/model.py b/comfy/ldm/flux/model.py
index cc34f7585..ef4ba4106 100644
--- a/comfy/ldm/flux/model.py
+++ b/comfy/ldm/flux/model.py
@@ -115,8 +115,11 @@ class Flux(nn.Module):
vec = vec + self.vector_in(y[:,:self.params.vec_in_dim])
txt = self.txt_in(txt)
- ids = torch.cat((txt_ids, img_ids), dim=1)
- pe = self.pe_embedder(ids)
+ if img_ids is not None:
+ ids = torch.cat((txt_ids, img_ids), dim=1)
+ pe = self.pe_embedder(ids)
+ else:
+ pe = None
blocks_replace = patches_replace.get("dit", {})
for i, block in enumerate(self.double_blocks):