Skip to content

Commit

Permalink
Add option to use in place weight updating in ModelPatcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
comfyanonymous committed Nov 11, 2023
1 parent 412d3ff commit 4a8a839
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 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
8 changes: 8 additions & 0 deletions comfy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,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

0 comments on commit 4a8a839

Please sign in to comment.