deprecated - as it has been fixed in latest nunchaku version (see nunchaku-tech/ComfyUI-nunchaku#406 (comment) )
based on: https://huggingface.co/lym00/comfyui_nunchaku_lora_patch/blob/main/patch_comfyui_nunchaku_lora.py
This script patches .safetensors LoRA models that are missing adaLN (Adaptive Layer Normalization) weights in their final layer. This is a common issue with some LoRA models that can cause compatibility problems with certain loaders, such as the Nunchaku LoRA loader in ComfyUI, which expect these weights to be present.
When a LoRA model is trained without adaLN weights for the final layer, loaders that strictly require them will fail to load the model, often with a key mismatch error. This script provides a simple solution by adding dummy (zero-filled) adaLN weights, allowing the model to be loaded without errors.
The script automatically scans the lora/ directory for any .safetensors files. For each file found, it inspects it for final_layer.linear weights. If these weights exist but the corresponding final_layer.adaLN_modulation_1 weights are missing, the script performs the following actions:
- Reads the existing
final_layer.lineartensors to determine their shape. - Generates new zero-filled "dummy" tensors for
adaLN_modulation_1that match the shape of the linear layer tensors. - Adds these new dummy tensors to the model's state dictionary under the correct keys.
- Saves the modified state dictionary to a new
.safetensorsfile in the same directory, with a_patchedsuffix (e.g.,my_lora_patched.safetensors).
The script is designed to be robust and checks for common key prefixes (e.g., lora_unet_final_layer, final_layer) to maximize compatibility. It will skip any files that already have the _patched suffix.
Make sure you have Python installed, along with the torch and safetensors libraries. If you don't have them, you can install them using pip:
pip install torch safetensors- Place all the LoRA models you want to patch into the
lora/directory. - Run the script from your terminal in the project directory:
python patch_comfyui_nunchaku_lora.py
- The script will automatically find, patch, and save the new files in the
lora/directory.
$ python patch_comfyui_nunchaku_lora.py
π Universal final_layer.adaLN LoRA patcher (.safetensors)
Looking for .safetensors files in the 'lora' directory...
Found 1 file(s) to process.
-----------------------------------------------------
Processing: lora\fal-Realism-Detailer-Kontext.safetensors
β
Loaded 130 tensors.
β
Patch applied using prefix 'lora_unet_final_layer'.
β
Patched file saved to: lora\fal-Realism-Detailer-Kontext_patched.safetensors
β
Verification successful: `adaLN` keys are present.
-----------------------------------------------------
π Done. Patched 1 file(s).
Note: This script does not create functional adaLN layers; it only adds zero-filled placeholder weights to ensure compatibility. The patched model should produce the same visual output as the original.