Skip to content

Commit dd5b57e

Browse files
authored
fix for SAG with Kohya HRFix/ Deep Shrink (#5546)
now works with arbitrary downscale factors
1 parent 75a818c commit dd5b57e

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

comfy_extras/nodes_sag.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ def create_blur_map(x0, attn, sigma=3.0, threshold=1.0):
5757
attn = attn.reshape(b, -1, hw1, hw2)
5858
# Global Average Pool
5959
mask = attn.mean(1, keepdim=False).sum(1, keepdim=False) > threshold
60-
ratio = 2**(math.ceil(math.sqrt(lh * lw / hw1)) - 1).bit_length()
61-
mid_shape = [math.ceil(lh / ratio), math.ceil(lw / ratio)]
60+
61+
f = float(lh) / float(lw)
62+
fh = f ** 0.5
63+
fw = (1/f) ** 0.5
64+
S = mask.size(1) ** 0.5
65+
w = int(0.5 + S * fw)
66+
h = int(0.5 + S * fh)
6267

6368
# Reshape
6469
mask = (
65-
mask.reshape(b, *mid_shape)
70+
mask.reshape(b, h, w)
6671
.unsqueeze(1)
6772
.type(attn.dtype)
6873
)

0 commit comments

Comments
 (0)