Load and apply LoRA weights to model weights using the LoRALoader class. Args: lora_path: Path to LoRA safetensors file weight_dict: Model weights dictionary (will be modified in place) alpha: Global alpha scaling factor key_mapping_rules: Optional list of (
(lora_path, weight_dict, alpha, key_mapping_rules=None, strength=1.0)
| 443 | logger.info(f"Total final model size: {total_final_size_mb:.2f} MB") |
| 444 | logger.info(f"Size reduction in quantized tensors: {size_reduction_mb:.2f} MB ({size_reduction_mb / original_size_mb * 100:.1f}%)") |
| 445 | |
| 446 | if comfyui_mode: |
| 447 | weights["scaled_fp8"] = torch.zeros(2, dtype=torch.float8_e4m3fn) |
| 448 | |
| 449 | return weights |
| 450 | |
| 451 | |
| 452 | def _validate_lora_merge(lora_path, weight_dict, lora_weights, lora_pairs, lora_diffs, alpha, require_alpha): |
| 453 | """Validate a LoRA completely before modifying any base-model tensors.""" |
| 454 | if not lora_pairs and not lora_diffs: |
| 455 | raise ValueError(f"No supported LoRA weights found in: {lora_path}") |
| 456 | |
| 457 | consumed_keys = set() |
| 458 | for pair_info in lora_pairs.values(): |
| 459 | consumed_keys.update((pair_info["up_key"], pair_info["down_key"])) |
| 460 | if pair_info["mid_key"] is not None: |
| 461 | consumed_keys.add(pair_info["mid_key"]) |
| 462 | consumed_keys.update(diff_info["diff_key"] for diff_info in lora_diffs.values()) |
| 463 | |
| 464 | tensor_keys = {key for key in lora_weights if not key.endswith(".alpha")} |
| 465 | unsupported_keys = sorted(tensor_keys - consumed_keys) |
| 466 | missing_model_keys = sorted((set(lora_pairs) | set(lora_diffs)) - set(weight_dict)) |
| 467 | missing_alpha_keys = sorted(model_key for model_key, pair_info in lora_pairs.items() if pair_info["alpha"] is None) |
| 468 | |
| 469 | shape_mismatches = [] |
| 470 | for model_key, pair_info in lora_pairs.items(): |
| 471 | if model_key not in weight_dict: |
| 472 | continue |
| 473 | param = weight_dict[model_key] |
| 474 | lora_up = lora_weights[pair_info["up_key"]] |
no test coverage detected