This commit is contained in:
thot-experiment 2025-05-12 16:50:15 -07:00
parent 26c4d5a57d
commit 20cf3d36e0

View File

@ -27,7 +27,7 @@ class APG:
def pre_cfg_function(args): def pre_cfg_function(args):
nonlocal running_avg, prev_sigma nonlocal running_avg, prev_sigma
cond = args["conds_out"][0] cond = args["conds_out"][0]
uncond = args["conds_out"][1] uncond = args["conds_out"][1]
sigma = args["sigma"][0] sigma = args["sigma"][0]
@ -35,29 +35,29 @@ class APG:
if prev_sigma is not None and sigma > prev_sigma: if prev_sigma is not None and sigma > prev_sigma:
running_avg = 0 running_avg = 0
prev_sigma = sigma prev_sigma = sigma
guidance = cond - uncond guidance = cond - uncond
if momentum > 0: if momentum > 0:
if not torch.is_tensor(running_avg): if not torch.is_tensor(running_avg):
running_avg = guidance running_avg = guidance
else: else:
running_avg = momentum * running_avg + guidance running_avg = momentum * running_avg + guidance
guidance = running_avg guidance = running_avg
if norm_threshold > 0: if norm_threshold > 0:
guidance_norm = guidance.norm(p=2, dim=[-1, -2, -3], keepdim=True) guidance_norm = guidance.norm(p=2, dim=[-1, -2, -3], keepdim=True)
scale = torch.minimum( scale = torch.minimum(
torch.ones_like(guidance_norm), torch.ones_like(guidance_norm),
norm_threshold / guidance_norm norm_threshold / guidance_norm
) )
guidance = guidance * scale guidance = guidance * scale
guidance_parallel, guidance_orthogonal = project(guidance, cond) guidance_parallel, guidance_orthogonal = project(guidance, cond)
modified_guidance = guidance_orthogonal + eta * guidance_parallel modified_guidance = guidance_orthogonal + eta * guidance_parallel
modified_cond = uncond + modified_guidance modified_cond = uncond + modified_guidance
return [modified_cond, uncond] return [modified_cond, uncond]
m = model.clone() m = model.clone()
@ -70,4 +70,4 @@ NODE_CLASS_MAPPINGS = {
NODE_DISPLAY_NAME_MAPPINGS = { NODE_DISPLAY_NAME_MAPPINGS = {
"APG": "Adaptive Projected Guidance", "APG": "Adaptive Projected Guidance",
} }