aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMrCheeze <fishycheeze@yahoo.ca>2022-10-16 18:44:39 -0400
committerAUTOMATIC1111 <16777216c@gmail.com>2022-10-17 07:54:36 +0300
commit0fd130767102ebcf90e97c6c191ecf199a2d4091 (patch)
treeaba93f97e4412f0066beb123a45c96482f6aba47
parenta1d3cbf92cfde6b3e02a9c795412d01cdc268934 (diff)
improve performance of 3-way merge on machines with not enough ram, by only accessing two of the models at a time
-rw-r--r--modules/extras.py27
1 files changed, 17 insertions, 10 deletions
diff --git a/modules/extras.py b/modules/extras.py
index 0819ed37..340a45fd 100644
--- a/modules/extras.py
+++ b/modules/extras.py
@@ -175,11 +175,14 @@ def run_pnginfo(image):
def run_modelmerger(primary_model_name, secondary_model_name, teritary_model_name, interp_method, multiplier, save_as_half, custom_name):
- def weighted_sum(theta0, theta1, theta2, alpha):
+ def weighted_sum(theta0, theta1, alpha):
return ((1 - alpha) * theta0) + (alpha * theta1)
- def add_difference(theta0, theta1, theta2, alpha):
- return theta0 + (theta1 - theta2) * alpha
+ def get_difference(theta1, theta2):
+ return theta1 - theta2
+
+ def add_difference(theta0, theta1_2_diff, alpha):
+ return theta0 + (alpha * theta1_2_diff)
primary_model_info = sd_models.checkpoints_list[primary_model_name]
secondary_model_info = sd_models.checkpoints_list[secondary_model_name]
@@ -201,20 +204,24 @@ def run_modelmerger(primary_model_name, secondary_model_name, teritary_model_nam
theta_2 = None
theta_funcs = {
- "Weighted sum": weighted_sum,
- "Add difference": add_difference,
+ "Weighted sum": (None, weighted_sum),
+ "Add difference": (get_difference, add_difference),
}
- theta_func = theta_funcs[interp_method]
+ theta_func1, theta_func2 = theta_funcs[interp_method]
print(f"Merging...")
+ if theta_func1:
+ for key in tqdm.tqdm(theta_1.keys()):
+ if 'model' in key:
+ t2 = theta_2.get(key, torch.zeros_like(theta_1[key]))
+ theta_1[key] = theta_func1(theta_1[key], t2)
+ del theta_2, teritary_model
+
for key in tqdm.tqdm(theta_0.keys()):
if 'model' in key and key in theta_1:
- t2 = (theta_2 or {}).get(key)
- if t2 is None:
- t2 = torch.zeros_like(theta_0[key])
- theta_0[key] = theta_func(theta_0[key], theta_1[key], t2, multiplier)
+ theta_0[key] = theta_func2(theta_0[key], theta_1[key], multiplier)
if save_as_half:
theta_0[key] = theta_0[key].half()