Skip to content

Commit

Permalink
Merge branch 'master' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
jn-jairo committed Nov 11, 2023
2 parents 67f11ca + 4a8a839 commit 5a26cc3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 21 deletions.
21 changes: 16 additions & 5 deletions comfy/model_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import comfy.model_management

class ModelPatcher:
def __init__(self, model, load_device, offload_device, size=0, current_device=None):
def __init__(self, model, load_device, offload_device, size=0, current_device=None, weight_inplace_update=False):
self.size = size
self.model = model
self.patches = {}
Expand All @@ -22,6 +22,8 @@ def __init__(self, model, load_device, offload_device, size=0, current_device=No
else:
self.current_device = current_device

self.weight_inplace_update = weight_inplace_update

def model_size(self):
if self.size > 0:
return self.size
Expand Down Expand Up @@ -171,15 +173,20 @@ def patch_model(self, device_to=None):

weight = model_sd[key]

inplace_update = self.weight_inplace_update

if key not in self.backup:
self.backup[key] = weight.to(self.offload_device)
self.backup[key] = weight.to(device=device_to, copy=inplace_update)

if device_to is not None:
temp_weight = comfy.model_management.cast_to_device(weight, device_to, torch.float32, copy=True)
else:
temp_weight = weight.to(torch.float32, copy=True)
out_weight = self.calculate_weight(self.patches[key], temp_weight, key).to(weight.dtype)
comfy.utils.set_attr(self.model, key, out_weight)
if inplace_update:
comfy.utils.copy_to_param(self.model, key, out_weight)
else:
comfy.utils.set_attr(self.model, key, out_weight)
del temp_weight

if device_to is not None:
Expand Down Expand Up @@ -295,8 +302,12 @@ def calculate_weight(self, patches, weight, key):
def unpatch_model(self, device_to=None):
keys = list(self.backup.keys())

for k in keys:
comfy.utils.set_attr(self.model, k, self.backup[k])
if self.weight_inplace_update:
for k in keys:
comfy.utils.copy_to_param(self.model, k, self.backup[k])
else:
for k in keys:
comfy.utils.set_attr(self.model, k, self.backup[k])

self.backup = {}

Expand Down
24 changes: 9 additions & 15 deletions comfy/ops.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import torch
from contextlib import contextmanager

class Linear(torch.nn.Module):
def __init__(self, in_features: int, out_features: int, bias: bool = True,
device=None, dtype=None) -> None:
factory_kwargs = {'device': device, 'dtype': dtype}
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = torch.nn.Parameter(torch.empty((out_features, in_features), **factory_kwargs))
if bias:
self.bias = torch.nn.Parameter(torch.empty(out_features, **factory_kwargs))
else:
self.register_parameter('bias', None)

def forward(self, input):
return torch.nn.functional.linear(input, self.weight, self.bias)
class Linear(torch.nn.Linear):
def reset_parameters(self):
return None

class Conv2d(torch.nn.Conv2d):
def reset_parameters(self):
return None

class Conv3d(torch.nn.Conv3d):
def reset_parameters(self):
return None

def conv_nd(dims, *args, **kwargs):
if dims == 2:
return Conv2d(*args, **kwargs)
elif dims == 3:
return Conv3d(*args, **kwargs)
else:
raise ValueError(f"unsupported dimensions: {dims}")

Expand Down
2 changes: 1 addition & 1 deletion comfy/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def calc_cond_uncond_batch(model_function, cond, uncond, x_in, timestep, max_tot

cond, uncond = calc_cond_uncond_batch(model_function, cond, uncond, x, timestep, max_total_area, model_options)
if "sampler_cfg_function" in model_options:
args = {"cond": x - cond, "uncond": x - uncond, "cond_scale": cond_scale, "timestep": timestep, "input": x}
args = {"cond": x - cond, "uncond": x - uncond, "cond_scale": cond_scale, "timestep": timestep, "input": x, "sigma": timestep}
return x - model_options["sampler_cfg_function"](args)
else:
return uncond + (cond - uncond) * cond_scale
Expand Down
8 changes: 8 additions & 0 deletions comfy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ def set_attr(obj, attr, value):
setattr(obj, attrs[-1], torch.nn.Parameter(value))
del prev

def copy_to_param(obj, attr, value):
# inplace update tensor instead of replacing it
attrs = attr.split(".")
for name in attrs[:-1]:
obj = getattr(obj, name)
prev = getattr(obj, attrs[-1])
prev.data.copy_(value)

def get_attr(obj, attr):
attrs = attr.split(".")
for name in attrs:
Expand Down
40 changes: 40 additions & 0 deletions comfy_extras/nodes_model_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,46 @@ class ModelSamplingAdvanced(sampling_base, sampling_type):
m.add_object_patch("model_sampling", model_sampling)
return (m, )

class RescaleCFG:
@classmethod
def INPUT_TYPES(s):
return {"required": { "model": ("MODEL",),
"multiplier": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}),
}}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"

CATEGORY = "advanced/model"

def patch(self, model, multiplier):
def rescale_cfg(args):
cond = args["cond"]
uncond = args["uncond"]
cond_scale = args["cond_scale"]
sigma = args["sigma"]
sigma = sigma.view(sigma.shape[:1] + (1,) * (cond.ndim - 1))
x_orig = args["input"]

#rescale cfg has to be done on v-pred model output
x = x_orig / (sigma * sigma + 1.0)
cond = ((x - (x_orig - cond)) * (sigma ** 2 + 1.0) ** 0.5) / (sigma)
uncond = ((x - (x_orig - uncond)) * (sigma ** 2 + 1.0) ** 0.5) / (sigma)

#rescalecfg
x_cfg = uncond + cond_scale * (cond - uncond)
ro_pos = torch.std(cond, dim=(1,2,3), keepdim=True)
ro_cfg = torch.std(x_cfg, dim=(1,2,3), keepdim=True)

x_rescaled = x_cfg * (ro_pos / ro_cfg)
x_final = multiplier * x_rescaled + (1.0 - multiplier) * x_cfg

return x_orig - (x - x_final * sigma / (sigma * sigma + 1.0) ** 0.5)

m = model.clone()
m.set_model_sampler_cfg_function(rescale_cfg)
return (m, )

NODE_CLASS_MAPPINGS = {
"ModelSamplingDiscrete": ModelSamplingDiscrete,
"RescaleCFG": RescaleCFG,
}

0 comments on commit 5a26cc3

Please sign in to comment.