|
1 | 1 | import torch |
2 | 2 | import math |
3 | 3 | import torch.nn.functional as F |
4 | | -from comfy.ldm.modules.attention import optimized_attention |
| 4 | +from comfy.ldm.modules.attention import attention_sage, optimized_attention |
5 | 5 | from .utils import tensor_to_size |
6 | 6 |
|
7 | 7 | class Attn2Replace: |
@@ -64,7 +64,10 @@ def to(self, device, *args, **kwargs): |
64 | 64 |
|
65 | 65 | def ipadapter_attention(out, q, k, v, extra_options, module_key='', ipadapter=None, weight=1.0, cond=None, cond_alt=None, uncond=None, weight_type="linear", mask=None, sigma_start=0.0, sigma_end=1.0, unfold_batch=False, embeds_scaling='V only', **kwargs): |
66 | 66 | ipadapter = ipadapter.get_multigpu_clone(q.device) |
67 | | - |
| 67 | + |
| 68 | + epsilon = 0.0 |
| 69 | + if optimized_attention == attention_sage: |
| 70 | + epsilon = 1e-5 |
68 | 71 | dtype = q.dtype |
69 | 72 | cond_or_uncond = extra_options["cond_or_uncond"] |
70 | 73 | block_type = extra_options["block"][0] |
@@ -105,17 +108,17 @@ def ipadapter_attention(out, q, k, v, extra_options, module_key='', ipadapter=No |
105 | 108 | if weight_type == "style transfer precise": |
106 | 109 | if layers == 11 and t_idx == 3: |
107 | 110 | uncond = cond |
108 | | - cond = cond * 0 |
| 111 | + cond = cond * epsilon |
109 | 112 | elif layers == 16 and (t_idx == 4 or t_idx == 5): |
110 | 113 | uncond = cond |
111 | | - cond = cond * 0 |
| 114 | + cond = cond * epsilon |
112 | 115 | elif weight_type == "composition precise": |
113 | 116 | if layers == 11 and t_idx != 3: |
114 | 117 | uncond = cond |
115 | | - cond = cond * 0 |
| 118 | + cond = cond * epsilon |
116 | 119 | elif layers == 16 and (t_idx != 4 and t_idx != 5): |
117 | 120 | uncond = cond |
118 | | - cond = cond * 0 |
| 121 | + cond = cond * epsilon |
119 | 122 |
|
120 | 123 | weight = weight[t_idx] |
121 | 124 |
|
@@ -170,7 +173,7 @@ def ipadapter_attention(out, q, k, v, extra_options, module_key='', ipadapter=No |
170 | 173 | weight = weight.repeat(len(cond_or_uncond), 1, 1) # repeat for cond and uncond |
171 | 174 | elif weight == 0: |
172 | 175 | return 0 |
173 | | - |
| 176 | + |
174 | 177 | k_cond = ipadapter.ip_layers.to_kvs[k_key](cond).repeat(batch_prompt, 1, 1) |
175 | 178 | k_uncond = ipadapter.ip_layers.to_kvs[k_key](uncond).repeat(batch_prompt, 1, 1) |
176 | 179 | v_cond = ipadapter.ip_layers.to_kvs[v_key](cond).repeat(batch_prompt, 1, 1) |
|
0 commit comments