Skip to content

Commit a527d0c

Browse files
Code refactor.
1 parent 2a23ba0 commit a527d0c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

comfy/ldm/modules/diffusionmodules/openaimodel.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ def __init__(self, dim):
251251
def forward(self, t):
252252
return timestep_embedding(t, self.dim)
253253

254+
def apply_control(h, control, name):
255+
if control is not None and name in control and len(control[name]) > 0:
256+
ctrl = control[name].pop()
257+
if ctrl is not None:
258+
h += ctrl
259+
return h
254260

255261
class UNetModel(nn.Module):
256262
"""
@@ -617,25 +623,17 @@ def forward(self, x, timesteps=None, context=None, y=None, control=None, transfo
617623
for id, module in enumerate(self.input_blocks):
618624
transformer_options["block"] = ("input", id)
619625
h = forward_timestep_embed(module, h, emb, context, transformer_options)
620-
if control is not None and 'input' in control and len(control['input']) > 0:
621-
ctrl = control['input'].pop()
622-
if ctrl is not None:
623-
h += ctrl
626+
h = apply_control(h, control, 'input')
624627
hs.append(h)
628+
625629
transformer_options["block"] = ("middle", 0)
626630
h = forward_timestep_embed(self.middle_block, h, emb, context, transformer_options)
627-
if control is not None and 'middle' in control and len(control['middle']) > 0:
628-
ctrl = control['middle'].pop()
629-
if ctrl is not None:
630-
h += ctrl
631+
h = apply_control(h, control, 'middle')
631632

632633
for id, module in enumerate(self.output_blocks):
633634
transformer_options["block"] = ("output", id)
634635
hsp = hs.pop()
635-
if control is not None and 'output' in control and len(control['output']) > 0:
636-
ctrl = control['output'].pop()
637-
if ctrl is not None:
638-
hsp += ctrl
636+
h = apply_control(h, control, 'output')
639637

640638
if "output_block_patch" in transformer_patches:
641639
patch = transformer_patches["output_block_patch"]

0 commit comments

Comments
 (0)