add handling of mismatched layers

This commit is contained in:
silveroxides
2025-12-18 21:43:32 +01:00
parent 6c08202ef5
commit 19bc0472dc
5 changed files with 214 additions and 42 deletions
+20 -1
View File
@@ -39,4 +39,23 @@
**Parameters:**
- **Alpha:** The Rank (dimension) for the LoRA's standard layers.
- **Beta:** The Rank (dimension) specifically for 3x3 convolution layers.
- **Gamma:** The clamp quantile for weight values. `0.99` is a typical value to prevent outlier weights from dominating.
- **Gamma:** The clamp quantile for weight values. `0.99` is a typical value to prevent outlier weights from dominating.
---
## Layer Mismatch Handling
When merging models with different layer structures (e.g., fine-tuned models with added/removed layers, or LoRAs with partial layer coverage), the `mismatch_mode` parameter controls behavior:
| Mode | Behavior |
|------|----------|
| `skip` | Missing layers in B use A's values **(default)** |
| `zeros` | Missing layers in B are treated as zeros |
| `error` | Fail if any layer is missing |
**Common scenarios:**
- Merging a fine-tuned model with added/removed layers
- Combining LoRA-extracted differences with base models
- Cross-architecture experiments
**Note:** Extra layers in Model B that don't exist in Model A are currently ignored. The output will always have the same layer structure as Model A.
+24 -1
View File
@@ -40,4 +40,27 @@
**Parameters:**
- **Alpha:** Weights the comparison between Model B (`0.0`) and Model C (`1.0`).
- **Beta:** A multiplier for the final extracted dissimilar features.
- **Gamma:** A bias exponent for the similarity calculation.
- **Gamma:** A bias exponent for the similarity calculation.
---
## Layer Mismatch Handling
When merging models with different layer structures, the `mismatch_mode` parameter controls behavior:
| Mode | Behavior |
|------|----------|
| `skip` | Missing layers in B/C use A's values **(default)** |
| `zeros` | Missing layers in B/C are treated as zeros |
| `error` | Fail if any layer is missing |
**Special case for B - C subtraction (Add-Difference, Train-Difference):**
- If **both** B and C have the layer: Normal subtraction is performed
- If **either** B or C is missing the layer: The difference is treated as `None` (no contribution from this layer), and Model A's value is used
**Common scenarios:**
- Merging a fine-tuned model with added/removed layers
- Extracting style differences between models with slightly different architectures
- Combining partial LoRAs or adapters
**Note:** Extra layers in Models B/C that don't exist in Model A are currently ignored. The output will always have the same layer structure as Model A.
+48 -6
View File
@@ -3,7 +3,7 @@ import torch
import folder_paths
import comfy.utils
from safetensors.torch import save_file
from .merger_ops import TWO_MODEL_MODES, THREE_MODEL_MODES
from .merger_ops import TWO_MODEL_MODES, THREE_MODEL_MODES, MissingTensorBehavior
from .merger_utils import MemoryEfficientSafeOpen
def load_documentation_from_file(filename):
@@ -23,6 +23,7 @@ def _create_two_model_inputs(model_folder_name):
"model_a": (["None"] + folder_paths.get_filename_list(model_folder_name),),
"model_b": (["None"] + folder_paths.get_filename_list(model_folder_name),),
"calc_mode": ([m.name for m in TWO_MODEL_MODES],),
"mismatch_mode": (["skip", "zeros", "error"], {"default": "skip"}),
"alpha": ("FLOAT", {"default": 0.5, "min": -2.0, "max": 3.0, "step": 0.01}),
"beta": ("FLOAT", {"default": 0.5, "min": -2.0, "max": 3.0, "step": 0.01}),
"gamma": ("FLOAT", {"default": 0.99, "min": 0.0, "max": 1.0, "step": 0.001}),
@@ -39,6 +40,7 @@ def _create_three_model_inputs(model_folder_name):
"model_b": (["None"] + folder_paths.get_filename_list(model_folder_name),),
"model_c": (["None"] + folder_paths.get_filename_list(model_folder_name),),
"calc_mode": ([m.name for m in THREE_MODEL_MODES],),
"mismatch_mode": (["skip", "zeros", "error"], {"default": "skip"}),
"alpha": ("FLOAT", {"default": 0.5, "min": -2.0, "max": 3.0, "step": 0.01}),
"beta": ("FLOAT", {"default": 0.5, "min": -2.0, "max": 3.0, "step": 0.01}),
"gamma": ("FLOAT", {"default": 0.5, "min": -2.0, "max": 3.0, "step": 0.01}),
@@ -56,26 +58,64 @@ class MergerLogic:
if not calc_mode_class: raise ValueError(f"Calc mode '{calc_mode}' not found.")
primary_model_name = model_names.get('model_a')
if not primary_model_name or primary_model_name == "None": raise ValueError("Model A is required to run the merge.")
handlers = {}
for name in model_names.values():
if name and name != "None":
path = folder_paths.get_full_path(model_type, name)
if not path: raise FileNotFoundError(f"Model '{name}' not found.")
handlers[name] = MemoryEfficientSafeOpen(path)
all_keys = handlers[primary_model_name].keys()
metadata = handlers[primary_model_name].header.get("__metadata__", {})
# Convert mismatch_mode string to enum
mismatch_mode_str = recipe_params.get('mismatch_mode', 'skip')
mismatch_mode = MissingTensorBehavior(mismatch_mode_str)
recipe_params['mismatch_mode'] = mismatch_mode
# Log mismatched keys for debugging
primary_keys = set(all_keys)
for name, handler in handlers.items():
if name != primary_model_name:
secondary_keys = set(handler.keys())
missing_in_secondary = primary_keys - secondary_keys
extra_in_secondary = secondary_keys - primary_keys
if missing_in_secondary:
print(f"[Merger] {name} is missing {len(missing_in_secondary)} keys present in Model A")
if extra_in_secondary:
print(f"[Merger] {name} has {len(extra_in_secondary)} extra keys not in Model A (ignored)")
merged_state_dict = {}
pbar = comfy.utils.ProgressBar(len(all_keys))
save_dtype = recipe_params.pop('save_dtype')
save_torch_dtype = {"fp32": torch.float32, "fp16": torch.float16, "bf16": torch.bfloat16}.get(save_dtype)
recipe_params.update({"handlers": handlers}) # Add handlers, device and dtype are already in
recipe_params.update({"handlers": handlers})
process_device = recipe_params.get('device', 'cpu')
skipped_keys = 0
for key in all_keys:
recipe = calc_mode_class.create_recipe(key=key, **recipe_params)
result = recipe.merge()
# Handle None result (mismatch occurred with skip mode)
if result is None:
# Fall back to Model A's value
result = handlers[primary_model_name].get_tensor(key).to(
device=process_device, dtype=torch.float32
)
skipped_keys += 1
if isinstance(result, dict):
for r_key, r_tensor in result.items(): merged_state_dict[r_key] = r_tensor.to(save_torch_dtype).cpu().clone()
else: merged_state_dict[key] = result.to(save_torch_dtype).cpu().clone()
for r_key, r_tensor in result.items():
merged_state_dict[r_key] = r_tensor.to(save_torch_dtype).cpu().clone()
else:
merged_state_dict[key] = result.to(save_torch_dtype).cpu().clone()
pbar.update(1)
if skipped_keys > 0:
print(f"[Merger] Used Model A's values for {skipped_keys} keys due to mismatches")
for handler in handlers.values(): handler.__exit__(None, None, None)
output_folder = "loras" if calc_mode == "SVD LoRA Extraction" else model_type
output_dir = folder_paths.get_folder_paths(output_folder)[0]
@@ -89,13 +129,14 @@ class BaseTwoMerger(MergerLogic):
FUNCTION = "merge"; CATEGORY = "ModelUtils/Merging"
RETURN_TYPES = ("STRING", "STRING",); RETURN_NAMES = ("output_filename", "documentation",)
def merge(self, execution_mode, model_a, model_b, calc_mode, alpha, beta, gamma, seed, output_filename, save_dtype, process_device):
def merge(self, execution_mode, model_a, model_b, calc_mode, mismatch_mode, alpha, beta, gamma, seed, output_filename, save_dtype, process_device):
doc = load_documentation_from_file('merger_2_model_modes.md')
if execution_mode == "DOCUMENTATION ONLY":
return ("Documentation mode active. No merge performed.", doc)
recipe_params = {
"model_a": model_a, "model_b": model_b, "calc_mode": calc_mode,
"mismatch_mode": mismatch_mode,
"alpha": alpha, "beta": beta, "gamma": gamma, "seed": seed,
"output_filename": output_filename, "save_dtype": save_dtype,
"device": process_device, "dtype": torch.float32
@@ -108,13 +149,14 @@ class BaseThreeMerger(MergerLogic):
FUNCTION = "merge"; CATEGORY = "ModelUtils/Merging"
RETURN_TYPES = ("STRING", "STRING",); RETURN_NAMES = ("output_filename", "documentation",)
def merge(self, execution_mode, model_a, model_b, model_c, calc_mode, alpha, beta, gamma, delta, seed, output_filename, save_dtype, process_device):
def merge(self, execution_mode, model_a, model_b, model_c, calc_mode, mismatch_mode, alpha, beta, gamma, delta, seed, output_filename, save_dtype, process_device):
doc = load_documentation_from_file('merger_3_model_modes.md')
if execution_mode == "DOCUMENTATION ONLY":
return ("Documentation mode active. No merge performed.", doc)
recipe_params = {
"model_a": model_a, "model_b": model_b, "model_c": model_c, "calc_mode": calc_mode,
"mismatch_mode": mismatch_mode,
"alpha": alpha, "beta": beta, "gamma": gamma, "delta": delta, "seed": seed,
"output_filename": output_filename, "save_dtype": save_dtype,
"device": process_device, "dtype": torch.float32
+95 -33
View File
@@ -1,6 +1,19 @@
import torch
import scipy.ndimage
import torch.nn.functional as F
from enum import Enum
class MissingTensorBehavior(Enum):
"""Controls behavior when a tensor key is missing from a model."""
ERROR = "error" # Raise exception (strict mode)
SKIP = "skip" # Return None, let caller handle
ZEROS = "zeros" # Return zeros tensor matching shape from primary model
class MissingTensorError(Exception):
"""Raised when a tensor is missing and behavior is ERROR."""
pass
# ################# #
# MERGE OPERATORS
@@ -34,29 +47,62 @@ class Operation:
return self.merge_func(self)
class LoadTensor(Operation):
def __init__(self, key, model_name, handlers, device, dtype):
def __init__(self, key, model_name, handlers, device, dtype,
on_missing=MissingTensorBehavior.ERROR, fallback_shape=None, fallback_dtype=None):
super().__init__(key)
self.model_name = model_name
self.handlers = handlers
self.device = device
self.dtype = dtype
self.on_missing = on_missing
self.fallback_shape = fallback_shape
self.fallback_dtype = fallback_dtype
def merge(self) -> torch.Tensor:
if self.model_name not in self.handlers:
raise ValueError(f"Model '{self.model_name}' is required for this mode but was not provided.")
return self.handlers[self.model_name].get_tensor(self.key).to(device=self.device, dtype=self.dtype)
handler = self.handlers[self.model_name]
if self.key not in handler.keys():
if self.on_missing == MissingTensorBehavior.ERROR:
raise MissingTensorError(
f"Key '{self.key}' not found in model '{self.model_name}'"
)
elif self.on_missing == MissingTensorBehavior.SKIP:
return None
elif self.on_missing == MissingTensorBehavior.ZEROS:
if self.fallback_shape is None:
raise ValueError(
f"Cannot create zeros for '{self.key}' without fallback_shape"
)
dtype = self.fallback_dtype if self.fallback_dtype else self.dtype
return torch.zeros(self.fallback_shape, device=self.device, dtype=dtype)
return handler.get_tensor(self.key).to(device=self.device, dtype=self.dtype)
class Multiply(Operation):
def __init__(self, key, alpha, *sources):
super().__init__(key, *sources)
self.alpha = alpha
def oper(self, a) -> torch.Tensor:
if a is None:
return None
return a * self.alpha
class Add(Operation):
def oper(self, a, b) -> torch.Tensor:
if a is None:
return b
if b is None:
return a
return a + b
class Sub(Operation):
def oper(self, a, b) -> torch.Tensor:
if a is None or b is None:
return None # Cannot compute difference with missing operand
return a - b
class SVD(Operation):
@@ -142,9 +188,11 @@ class WeightSum(CalcMode):
models_used = ['A', 'B']
param_docs = {'alpha': 'Interpolation weight. 0.0 is 100% Model A, 1.0 is 100% Model B.'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_b = {**loader_args_a, "on_missing": mismatch_mode}
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_b)
if kwargs['alpha'] >= 1: return b
if kwargs['alpha'] <= 0: return a
c = Multiply(key, 1 - kwargs['alpha'], a)
@@ -156,10 +204,12 @@ class AddDifference(CalcMode):
models_used = ['A', 'B', 'C']
param_docs = {'alpha': 'Multiplier for the (B - C) difference.', 'beta': 'Smoothing (0=Off, 1=On).'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
c = LoadTensor(key, kwargs['model_c'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_bc = {**loader_args_a, "on_missing": mismatch_mode}
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_bc)
c = LoadTensor(key, kwargs['model_c'], **loader_args_bc)
diff = Sub(key, b, c)
if kwargs['beta'] == 1: diff = Smooth(key, diff)
diffm = Multiply(key, kwargs['alpha'], diff)
@@ -170,10 +220,12 @@ class TrainDifference(CalcMode):
models_used = ['A', 'B', 'C']
param_docs = {'alpha': 'Multiplier for the calculated difference.'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
c = LoadTensor(key, kwargs['model_c'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_bc = {**loader_args_a, "on_missing": mismatch_mode}
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_bc)
c = LoadTensor(key, kwargs['model_c'], **loader_args_bc)
diff = TrainDiff(key, a, b, c)
diffm = Multiply(key, kwargs['alpha'], diff)
return Add(key, a, diffm)
@@ -183,10 +235,12 @@ class InterpDifference(CalcMode):
models_used = ['A', 'B']
param_docs = {'alpha': 'Curve strength.', 'beta': 'Style (0=Similarity, 1=Difference).', 'gamma': 'Mix (0=Random, 1=Linear).'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
if key.startswith('cond_stage_model.transformer.text_model.embeddings'): return LoadTensor(key, kwargs['model_a'], **loader_args)
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_b = {**loader_args_a, "on_missing": mismatch_mode}
if key.startswith('cond_stage_model.transformer.text_model.embeddings'): return LoadTensor(key, kwargs['model_a'], **loader_args_a)
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_b)
return InterpolateDifference(key, kwargs['alpha'], kwargs['beta'], kwargs['gamma'], kwargs['seed'], a, b)
class Extract(CalcMode):
name = 'Extract-Features'
@@ -194,10 +248,12 @@ class Extract(CalcMode):
models_used = ['A', 'B', 'C']
param_docs = {'alpha': 'Weighting B vs C.', 'beta': 'Similarity vs dissimilarity.', 'gamma': 'Similarity bias.', 'delta': 'Multiplier for final features.'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
c = LoadTensor(key, kwargs['model_c'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_bc = {**loader_args_a, "on_missing": mismatch_mode}
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_bc)
c = LoadTensor(key, kwargs['model_c'], **loader_args_bc)
extracted = ExtractOp(key, kwargs['alpha'], kwargs['beta'], kwargs['gamma'] * 15, a, b, c)
multiplied = Multiply(key, kwargs['delta'], extracted)
return Add(key, a, multiplied)
@@ -207,10 +263,12 @@ class AddDisimilarity(CalcMode):
models_used = ['A', 'B', 'C']
param_docs = {'alpha': 'Weighting B vs C.', 'beta': 'Multiplier for final features.', 'gamma': 'Similarity bias.'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
c = LoadTensor(key, kwargs['model_c'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_bc = {**loader_args_a, "on_missing": mismatch_mode}
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_bc)
c = LoadTensor(key, kwargs['model_c'], **loader_args_bc)
extracted = Similarities(key, kwargs['alpha'], 1, kwargs['gamma'] * 15, b, c)
multiplied = Multiply(key, kwargs['beta'], extracted)
return Add(key, a, multiplied)
@@ -220,22 +278,26 @@ class PowerUp(CalcMode):
models_used = ['A', 'B']
param_docs = {'alpha': 'Dropout rate (proportion of weights from B to *drop*).', 'beta': 'Multiplier for the final difference.'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_b = {**loader_args_a, "on_missing": mismatch_mode}
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_b)
deltahat = PowerUpOp(key, kwargs['alpha'], kwargs['seed'], a, b)
res = Multiply(key, kwargs['beta'], deltahat)
return Add(key, a, res)
class SVD(CalcMode):
class SVDMode(CalcMode):
name = 'SVD LoRA Extraction'
description = 'Extracts the difference between B and A into a LoRA file.'
models_used = ['A', 'B']
param_docs = {'alpha': 'Rank for standard layers.', 'beta': 'Rank for 3x3 conv layers.', 'gamma': 'Clamp quantile for weights.'}
def create_recipe(self, key, **kwargs):
loader_args = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
a = LoadTensor(key, kwargs['model_a'], **loader_args)
b = LoadTensor(key, kwargs['model_b'], **loader_args)
mismatch_mode = kwargs.get('mismatch_mode', MissingTensorBehavior.SKIP)
loader_args_a = {"handlers": kwargs['handlers'], "device": kwargs['device'], "dtype": kwargs['dtype']}
loader_args_b = {**loader_args_a, "on_missing": mismatch_mode}
a = LoadTensor(key, kwargs['model_a'], **loader_args_a)
b = LoadTensor(key, kwargs['model_b'], **loader_args_b)
return SVD(key, kwargs['alpha'], kwargs['beta'], kwargs['gamma'], a, b)
TWO_MODEL_MODES = [WeightSum(), InterpDifference(), PowerUp(), SVD()]
TWO_MODEL_MODES = [WeightSum(), InterpDifference(), PowerUp(), SVDMode()]
THREE_MODEL_MODES = [AddDifference(), TrainDifference(), Extract(), AddDisimilarity()]
+27 -1
View File
@@ -77,4 +77,30 @@ class MemoryEfficientSafeOpen:
elif dtype_str == "F8_E4M3" and hasattr(torch, "float8_e4m3fn"):
return byte_tensor.view(torch.float8_e4m3fn).reshape(shape)
else:
raise ValueError(f"Unsupported float8 type: {dtype_str}. Your PyTorch version may be too old.")
raise ValueError(f"Unsupported float8 type: {dtype_str}. Your PyTorch version may be too old.")
def get_union_keys(*handlers):
"""Returns the union of all tensor keys across multiple model handlers.
Useful when you want to iterate over all unique keys present in any model.
"""
all_keys = set()
for handler in handlers:
if handler is not None:
all_keys.update(handler.keys())
return sorted(all_keys)
def get_intersection_keys(*handlers):
"""Returns only keys present in ALL model handlers.
Useful when you want to iterate only over keys that exist in every model.
"""
handlers = [h for h in handlers if h is not None]
if not handlers:
return []
all_keys = set(handlers[0].keys())
for handler in handlers[1:]:
all_keys &= set(handler.keys())
return sorted(all_keys)