# LoraExtractKJ Comprehensive Documentation
## π Overview
The `LoraExtractKJ` class is a ComfyUI node designed to extract LoRA (Low-Rank Adaptation) weights from the differences between a fine-tuned model and an original base model. This node provides multiple extraction algorithms and adaptive methods to create efficient LoRA files that can be used to apply model modifications with minimal storage overhead.
## π Location
- **File**: `ComfyUI-KJNodes/nodes/lora_nodes.py`
- **Line**: 153-205
- **Category**: KJNodes/lora
## βοΈ Input Parameters
| Parameter | Type | Default | Range/Options | Description |
|-----------|------|---------|---------------|-------------|
| `finetuned_model` | MODEL | - | - | The fine-tuned model from which to extract LoRA weights |
| `original_model` | MODEL | - | - | The original base model to compare against |
| `filename_prefix` | STRING | "loras/ComfyUI_extracted_lora" | - | Output file path and name prefix |
| `rank` | INT | 8 | 1-4096 | Target rank for standard LoRA or maximum rank limit for adaptive methods |
| `lora_type` | ENUM | "standard" | 6 options | Extraction method type (see details below) |
| `algorithm` | ENUM | "svd_linalg" | 2 options | SVD algorithm to use |
| `lowrank_iters` | INT | 7 | 1-100 | Number of subspace iterations for lowrank SVD |
| `output_dtype` | ENUM | "fp16" | 3 options | Output data type for the LoRA file |
| `bias_diff` | BOOLEAN | True | - | Whether to extract bias differences |
| `adaptive_param` | FLOAT | 0.15 | 0.0-1.0 | Parameter for adaptive extraction methods |
| `clamp_quantile` | BOOLEAN | True | - | Whether to clamp values using quantile method |
## π¨ LoRA Types and Methods
### 1. Standard LoRA (`"standard"`)
- **Description**: Traditional fixed-rank LoRA extraction
- **Use Case**: When you want consistent, predictable file sizes and performance
- **Algorithm**: Uses SVD to decompose weight differences into low-rank matrices
- **Parameters**: `rank` directly controls the LoRA rank
### 2. Full LoRA (`"full"`)
- **Description**: Stores complete weight differences without rank reduction
- **Use Case**: Maximum quality preservation, larger file sizes
- **Algorithm**: No decomposition, stores raw weight differences
- **Parameters**: `rank` parameter is ignored
### 3. Adaptive Ratio (`"adaptive_ratio"`)
- **Description**: Dynamically selects rank based on singular value ratios
- **Algorithm**: Keeps singular values > `adaptive_param * max(singular_value)`
- **Use Case**: Balanced quality and size based on importance thresholds
- **Default**: 0.15 (15% of maximum singular value)
### 4. Adaptive Quantile (`"adaptive_quantile"`)
- **Description**: Selects rank based on cumulative distribution of singular values
- **Algorithm**: Keeps singular values until cumulative sum reaches `adaptive_param * total_sum`
- **Use Case**: Statistical approach to rank selection
- **Default**: 0.15 (15% of total singular value sum)
### 5. Adaptive Energy (`"adaptive_energy"`)
- **Description**: Rank selection based on energy preservation
- **Algorithm**: Keeps components until cumulative energy reaches `adaptive_param * total_energy`
- **Use Case**: Preserving specific percentage of mathematical energy
- **Default**: 0.15 (15% of total energy)
### 6. Adaptive Frobenius (`"adaptive_fro"`)
- **Description**: Rank selection based on Frobenius norm preservation
- **Algorithm**: Keeps components until Frobenius norm reaches `adaptive_paramΒ² * total_norm`
- **Use Case**: Maintaining specific percentage of matrix information
- **Default**: 0.15 (15% Frobenius norm retention)
## π¬ SVD Algorithms
### 1. Full SVD (`"svd_linalg"`)
- **Method**: `torch.linalg.svd()`
- **Pros**: Most accurate, complete decomposition
- **Cons**: Slower, higher memory usage
- **Recommended**: For high-quality extractions and adaptive methods
### 2. Low-rank SVD (`"svd_lowrank"`)
- **Method**: `torch.svd_lowrank()`
- **Pros**: Faster, lower memory usage
- **Cons**: Less accurate, approximation
- **Limitation**: Only works with standard LoRA type
- **Recommended**: For quick extractions when speed is prioritized
## πΎ Data Types
| Type | Description | Use Case |
|------|-------------|----------|
| `fp16` | 16-bit floating point | Balance of quality and file size |
| `bf16` | 16-bit bfloat floating point | Better range than fp16, similar size |
| `fp32` | 32-bit floating point | Maximum precision, larger files |
## ποΈ Advanced Parameters
### `adaptive_param` Usage by LoRA Type
| LoRA Type | `adaptive_param` Meaning | Recommended Range |
|-----------|-------------------------|-------------------|
| `adaptive_ratio` | Ratio to maximum singular value | 0.01-0.3 |
| `adaptive_quantile` | Quantile of singular values | 0.1-0.9 |
| `adaptive_energy` | Energy preservation percentage | 0.8-0.99 |
| `adaptive_fro` | Frobenius norm retention | 0.8-0.99 |
### `clamp_quantile`
- **Purpose**: Prevents extreme values in LoRA weights
- **Method**: Clips values to [-99th percentile, +99th percentile]
- **Effect**: More stable training and inference
- **Recommendation**: Keep enabled unless you need full dynamic range
### `lowrank_iters`
- **Purpose**: Number of iterations for low-rank SVD approximation
- **Effect**: Higher values = better accuracy, slower processing
- **Range**: 1-100
- **Default**: 7 (good balance)
## π Output File Naming
The node generates files with this naming convention:
```
{filename}_rank_{rank/type}_{dtype}_{counter:05}_.safetensors
```
**Examples:**
- `my_lora_rank_8_fp16_00001_.safetensors`
- `my_lora_rank_adaptive_ratio_0.15_fp16_00001_.safetensors`
## β Recommended Configurations
### π High Quality Extraction
```yaml
lora_type: "adaptive_fro"
adaptive_param: 0.95
algorithm: "svd_linalg"
output_dtype: "fp32"
clamp_quantile: true
```
### βοΈ Balanced Quality/Size
```yaml
lora_type: "adaptive_ratio"
adaptive_param: 0.1
algorithm: "svd_linalg"
output_dtype: "fp16"
clamp_quantile: true
```
### β‘ Fast Extraction
```yaml
lora_type: "standard"
rank: 16
algorithm: "svd_lowrank"
lowrank_iters: 4
output_dtype: "fp16"
```
### π Maximum Quality
```yaml
lora_type: "full"
output_dtype: "fp32"
```
### π¦ Small File Size
```yaml
lora_type: "adaptive_ratio"
adaptive_param: 0.02
algorithm: "svd_linalg"
output_dtype: "fp16"
clamp_quantile: true
```
## π Usage Guidelines
### π― When to Use Each LoRA Type
1. **Character/Stylistic LoRAs**: `adaptive_ratio` with 0.05-0.1
2. **Concept LoRAs**: `adaptive_fro` with 0.85-0.95
3. **Technical LoRAs**: `adaptive_energy` with 0.9-0.95
4. **Testing/Prototyping**: `standard` with rank 4-8
5. **Archive/Backup**: `full` with fp32
### β‘ Performance Considerations
- **Memory Usage**: Low-rank SVD uses ~50% less memory
- **Processing Time**: Low-rank SVD is 2-3x faster
- **File Size**: Adaptive methods can reduce size by 40-80%
- **Quality Loss**: Usually <5% for well-chosen parameters
### π§ Common Issues and Solutions
| Issue | Cause | Solution |
|-------|-------|----------|
| Large file sizes | `adaptive_param` too high | Reduce to 0.05-0.15 |
| Quality loss | `adaptive_param` too low | Increase to 0.15-0.3 |
| Slow processing | Using full SVD on large models | Switch to `svd_lowrank` |
| Training instability | Extreme weight values | Enable `clamp_quantile` |
| Memory errors | Large model with full SVD | Use `svd_lowrank` or reduce model size |
## π Comparison of LoRA Types
### π Quality vs File Size Trade-offs
```
Quality
β²
β Full LoRA βββββββββββββββββββββββ
β β
β Adaptive Fro (0.95) βββββββββββββ
β β
β Adaptive Energy (0.9) βββββββββββ
β β
β Adaptive Quantile (0.5) βββββββββ
β β
β Adaptive Ratio (0.15) βββββββββ
β β
β Standard (Rank 16) βββββββββββββ
β β
β Standard (Rank 8) ββββββββββββββ
β β
β Standard (Rank 4) βββββββββββββ
βββββββββββββββββββββββββββββββββββββΊ File Size
Small Large
Memory Usage (MB) for 1B Model:
Full LoRA: ββββββββββββββββββββββββββββββββββββ ~4000MB
Standard R8: ββββββββββββ ~500MB
Adaptive Ratio: ββββββββ ~300MB
Adaptive Energy: βββββββββ ~400MB
```
### β±οΈ Processing Speed Comparison
```
Full LoRA
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Direct weight difference extraction β
β Time: ββββββββββββββββββββββββββββββββββββββββ 10x β
β Memory: ββββββββββββββββββββββββββββββββββββββββ High β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Standard LoRA (SVD Low-rank)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Fast SVD approximation β
β Time: ββββββββββββ 1x (Fastest) β
β Memory: ββββββββ Low β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Adaptive Methods (Full SVD)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Complete SVD with rank analysis β
β Time: ββββββββββββββββββββββββ 3-5x β
β Memory: ββββββββββββββββ Medium β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## π¬ Algorithm Details
### π SVD Decomposition Process
```
Step 1: Compute Weight Difference
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Fine-tuned β β Original β β Weight Diff β
β Model Weights β ββ β Model Weights β = β (W_diff) β
β (W_finetuned) β β (W_original) β β β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
Step 2: Apply SVD Decomposition
βββββββββββββββββββββββββββββββββββ
β W_diff Matrix β
β [out_dim Γ in_dim] β
βββββββββββββββββββββββββββββββββββ
β
βΌ SVD
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β U Matrix β β S Matrix β β Vh Matrix β
β [out_dim Γ rank]β β [rank Γ rank] β β [rank Γ in_dim] β
β (Left) β β (Singular Values)β β (Right) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
Step 3: Create LoRA Matrices
βββββββββββββββββββ βββββββββββββββββββ
β U Γ βS β β βS Γ Vh β
β β β β
β lora_up β β lora_down β
β [out_dim Γ rank]β β [rank Γ in_dim] β
βββββββββββββββββββ βββββββββββββββββββ
β β
ββββββββββββββββ¬βββββββββββββββββ
βΌ
βββββββββββββββββββ
β LoRA Pair β
β (Efficient Low β
β Rank Approx.) β
βββββββββββββββββββ
```
1. Compute weight difference: `W_diff = W_finetuned - W_original`
2. Apply SVD: `W_diff = U @ S @ Vh`
3. For LoRA: `lora_up = U @ sqrt(S)`, `lora_down = sqrt(S) @ Vh`
4. Clamp values if enabled
5. Save with proper data type conversion
### π― Adaptive Rank Selection
The adaptive methods automatically determine the optimal rank based on different criteria:
```
Singular Values Spectrum
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Sβ β Sβ β Sβ β Sβ β Sβ
β Sβ β Sβ β Sβ β Sβ β Sββ β ... β Sβ β
β β β β β β β β β β β β β
β βββββ βββββ βββββ βββββ βββββ ββββββββββββββββββ β
β β β β β β negligible β
β important important important important important values β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β² β² β² β²
β β β β
Standard Ratio Quantile Energy/Fro
Rank R Threshold Distribution Preservation
```
#### π Adaptive Ratio Method
```
Threshold = adaptive_param Γ max(singular_value)
Singular Values: [100, 95, 88, 72, 45, 23, 8, 3, 1, 0.5, ...]
βββββββββββββββββββββββββββββββββββββββββ
Threshold: 15 (0.15 Γ 100) βββββββββββββββββββββββββ
Selected: βββββββ (rank = 7)
Discarded: βββββββββββββ
```
#### π Adaptive Quantile Method
```
Cumulative Sum Distribution: ββββββββββββββββββββββββββββββββ
ββββββββββββββ
0.15 Γ total_sum
Singular Values: [100, 95, 88, 72, 45, 23, 8, 3, 1, 0.5, ...]
Cumulative: [100, 195, 283, 355, 400, 423, 431, 434, 435, ...]
Target: 0.15 Γ 450 = 67.5
Selected: βββ (rank = 3, since 283 > 67.5)
```
#### β‘ Adaptive Energy Method
```
Energy (SΒ²): [10000, 9025, 7744, 5184, 2025, 529, 64, 9, 1, 0.25, ...]
Cumulative: ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββ
0.95 Γ total_energy (95% energy preservation)
Selected: βββββββ (rank = 7, preserves 95% of total energy)
```
#### π― Adaptive Frobenius Method
```
Frobenius Norm: β(SβΒ² + SβΒ² + ... + Sα΅£Β²) = 0.95 Γ β(SβΒ² + ... + SβΒ²)
Progressive: ββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββ
95% of total Frobenius norm
Selected: βββββββββ (rank = 9, preserves 95% of matrix information)
```
- **Ratio**: Keeps components above relative importance threshold
- **Quantile**: Statistical distribution-based selection
- **Energy**: Preserves mathematical energy content
- **Frobenius**: Maintains matrix information content
Each method provides feedback showing the actual rank selected and preservation metrics.
## βοΈ Technical Implementation
### π Key Functions
- `extract_lora()`: Core SVD extraction logic (lines 14-99)
- `calc_lora_model()`: Model-wide processing (lines 102-151)
- `LoraExtractKJ.save()`: Main execution method (lines 181-205)
### πΎ Memory Management
- Models are moved to CPU after processing
- GPU memory is cleared between operations
- Progress bars show extraction status
- Batch processing for large models
### π‘οΈ Error Handling
- Skips 5D tensors (patch embeddings)
- Handles failed SVD decompositions gracefully
- Validates algorithm compatibility
- Provides detailed logging
## π Integration Notes
### π¨ ComfyUI Integration
- Located in "KJNodes/lora" category
- Output node (saves to disk)
- Supports standard ComfyUI model handling
- Compatible with all model formats
### π File Format
- Outputs in SafeTensors format
- Includes metadata about extraction parameters
- Compatible with standard LoRA loaders
- Supports automatic dtype conversion
## π― Decision Tree for Choosing LoRA Type
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β What's your priority? β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββ
β β β
βββββββββΌβββββββ ββββββββΌβββββββ βββββββΌββββββββ
β Quality β β Speed β β File Size β
βββββββββ¬βββββββ ββββββββ¬βββββββ βββββββ¬ββββββββ
β β β
βββββββββΌβββββββ ββββββββΌβββββββ βββββββΌββββββββ
β Full LoRA β β Standard + β β Adaptive β
β (fp32) β β Low-rank β β Ratio β
β β β SVD β β (0.02-0.05) β
βββββββββββββββββ βββββββββββββββ βββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β What type of content? β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββ
β β β
βββββββββΌβββββββ ββββββββΌβββββββ βββββββΌββββββββ
β Characters β β Concepts β β Style β
βββββββββ¬βββββββ ββββββββ¬βββββββ βββββββ¬ββββββββ
β β β
βββββββββΌβββββββ ββββββββΌβββββββ βββββββΌββββββββ
β Adaptive β β Adaptive β β Adaptive β
β Ratio β β Fro (0.95) β β Energy β
β (0.05-0.1) β β β β (0.9-0.95) β
βββββββββββββββββ βββββββββββββββ βββββββββββββββ
```
## π― Memory Flow Diagram
```
GPU Memory Management During Extraction:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Initial State β
β βββββββββββββββ βββββββββββββββ β
β β Fine-tuned β β Original β β
β β Model β β Model β β
β βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Weight Difference β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββ β
β β Fine-tuned β ββ β Original β = β W_diff β β
β β Model β β Model β β (GPU) β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ SVD Processing
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β LoRA Extraction (GPU) β
β βββββββββββββββ βββββββββββββββ β
β β lora_up β β lora_down β β
β β (GPU) β β (GPU) β β
β βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ Memory Optimization
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Transfer to CPU & Save β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββ β
β β lora_up β β lora_down β β SafeTensors β β
β β (CPU) β β (CPU) β β File β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β GPU Memory Cleared β
β Ready for Next Layer β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## π― Conclusion
The `LoraExtractKJ` node provides a comprehensive solution for LoRA extraction with multiple algorithms and adaptive methods. The choice of parameters depends on your specific use case, balancing quality, file size, and processing speed. The adaptive methods are particularly useful for automatically optimizing the rank selection based on the actual content of the weight differences.
**Key Takeaways:**
- π― **Quality matters**: Use adaptive methods for better preservation
- β‘ **Speed matters**: Use low-rank SVD for faster processing
- πΎ **Size matters**: Adaptive ratio can significantly reduce file sizes
- π§ **Flexibility**: Different methods suit different content types