490 lines
29 KiB
Markdown
490 lines
29 KiB
Markdown
# <span style="color: #b19cd9;">LoraExtractKJ Comprehensive Documentation</span>
|
||
|
||
## <span style="color: #98d8c8;">📖 Overview</span>
|
||
|
||
The `LoraExtractKJ` class is a ComfyUI node designed to extract <span style="color: #ffdab9;">LoRA (Low-Rank Adaptation)</span> 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.
|
||
|
||
## <span style="color: #98d8c8;">📍 Location</span>
|
||
- **File**: `ComfyUI-KJNodes/nodes/lora_nodes.py`
|
||
- **Line**: 153-205
|
||
- **Category**: KJNodes/lora
|
||
|
||
## <span style="color: #98d8c8;">⚙️ Input Parameters</span>
|
||
|
||
| 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 |
|
||
|
||
## <span style="color: #98d8c8;">🎨 LoRA Types and Methods</span>
|
||
|
||
### 1. <span style="color: #f0e68c;">Standard LoRA</span> (`"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. <span style="color: #dda0dd;">Full LoRA</span> (`"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. <span style="color: #87ceeb;">Adaptive Ratio</span> (`"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. <span style="color: #98fb98;">Adaptive Quantile</span> (`"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. <span style="color: #f4a460;">Adaptive Energy</span> (`"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. <span style="color: #ffa07a;">Adaptive Frobenius</span> (`"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)
|
||
|
||
## <span style="color: #98d8c8;">🔬 SVD Algorithms</span>
|
||
|
||
### 1. <span style="color: #add8e6;">Full SVD</span> (`"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. <span style="color: #ffb6c1;">Low-rank SVD</span> (`"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
|
||
|
||
## <span style="color: #98d8c8;">💾 Data Types</span>
|
||
|
||
| Type | Description | Use Case |
|
||
|------|-------------|----------|
|
||
| <span style="color: #e6e6fa;">`fp16`</span> | 16-bit floating point | Balance of quality and file size |
|
||
| <span style="color: #ffd700;">`bf16`</span> | 16-bit bfloat floating point | Better range than fp16, similar size |
|
||
| <span style="color: #40e0d0;">`fp32`</span> | 32-bit floating point | Maximum precision, larger files |
|
||
|
||
## <span style="color: #98d8c8;">🎛️ Advanced Parameters</span>
|
||
|
||
### <span style="color: #d8bfd8;">`adaptive_param` Usage by LoRA Type</span>
|
||
|
||
| LoRA Type | `adaptive_param` Meaning | Recommended Range |
|
||
|-----------|-------------------------|-------------------|
|
||
| <span style="color: #87ceeb;">`adaptive_ratio`</span> | Ratio to maximum singular value | 0.01-0.3 |
|
||
| <span style="color: #98fb98;">`adaptive_quantile`</span> | Quantile of singular values | 0.1-0.9 |
|
||
| <span style="color: #f4a460;">`adaptive_energy`</span> | Energy preservation percentage | 0.8-0.99 |
|
||
| <span style="color: #ffa07a;">`adaptive_fro`</span> | Frobenius norm retention | 0.8-0.99 |
|
||
|
||
### <span style="color: #d8bfd8;">`clamp_quantile`</span>
|
||
- **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
|
||
|
||
### <span style="color: #d8bfd8;">`lowrank_iters`</span>
|
||
- **Purpose**: Number of iterations for low-rank SVD approximation
|
||
- **Effect**: Higher values = better accuracy, slower processing
|
||
- **Range**: 1-100
|
||
- **Default**: 7 (good balance)
|
||
|
||
## <span style="color: #98d8c8;">📁 Output File Naming</span>
|
||
|
||
The node generates files with this naming convention:
|
||
```
|
||
{filename}_rank_{rank/type}_{dtype}_{counter:05}_.safetensors
|
||
```
|
||
|
||
<span style="color: #dda0dd;">**Examples:**</span>
|
||
- `my_lora_rank_8_fp16_00001_.safetensors`
|
||
- `my_lora_rank_adaptive_ratio_0.15_fp16_00001_.safetensors`
|
||
|
||
## <span style="color: #98d8c8;">⭐ Recommended Configurations</span>
|
||
|
||
### <span style="color: #ffa07a;">🏆 High Quality Extraction</span>
|
||
```yaml
|
||
lora_type: "adaptive_fro"
|
||
adaptive_param: 0.95
|
||
algorithm: "svd_linalg"
|
||
output_dtype: "fp32"
|
||
clamp_quantile: true
|
||
```
|
||
|
||
### <span style="color: #87ceeb;">⚖️ Balanced Quality/Size</span>
|
||
```yaml
|
||
lora_type: "adaptive_ratio"
|
||
adaptive_param: 0.1
|
||
algorithm: "svd_linalg"
|
||
output_dtype: "fp16"
|
||
clamp_quantile: true
|
||
```
|
||
|
||
### <span style="color: #98fb98;">⚡ Fast Extraction</span>
|
||
```yaml
|
||
lora_type: "standard"
|
||
rank: 16
|
||
algorithm: "svd_lowrank"
|
||
lowrank_iters: 4
|
||
output_dtype: "fp16"
|
||
```
|
||
|
||
### <span style="color: #dda0dd;">💎 Maximum Quality</span>
|
||
```yaml
|
||
lora_type: "full"
|
||
output_dtype: "fp32"
|
||
```
|
||
|
||
### <span style="color: #f0e68c;">📦 Small File Size</span>
|
||
```yaml
|
||
lora_type: "adaptive_ratio"
|
||
adaptive_param: 0.02
|
||
algorithm: "svd_linalg"
|
||
output_dtype: "fp16"
|
||
clamp_quantile: true
|
||
```
|
||
|
||
## <span style="color: #98d8c8;">📋 Usage Guidelines</span>
|
||
|
||
### <span style="color: #b19cd9;">🎯 When to Use Each LoRA Type</span>
|
||
|
||
1. <span style="color: #f4a460;">**Character/Stylistic LoRAs**</span>: `adaptive_ratio` with 0.05-0.1
|
||
2. <span style="color: #87ceeb;">**Concept LoRAs**</span>: `adaptive_fro` with 0.85-0.95
|
||
3. <span style="color: #98fb98;">**Technical LoRAs**</span>: `adaptive_energy` with 0.9-0.95
|
||
4. <span style="color: #f0e68c;">**Testing/Prototyping**</span>: `standard` with rank 4-8
|
||
5. <span style="color: #dda0dd;">**Archive/Backup**</span>: `full` with fp32
|
||
|
||
### <span style="color: #b19cd9;">⚡ Performance Considerations</span>
|
||
|
||
- **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
|
||
|
||
### <span style="color: #b19cd9;">🔧 Common Issues and Solutions</span>
|
||
|
||
| Issue | Cause | Solution |
|
||
|-------|-------|----------|
|
||
| <span style="color: #ff6b6b;">Large file sizes</span> | `adaptive_param` too high | Reduce to 0.05-0.15 |
|
||
| <span style="color: #ff6b6b;">Quality loss</span> | `adaptive_param` too low | Increase to 0.15-0.3 |
|
||
| <span style="color: #ff6b6b;">Slow processing</span> | Using full SVD on large models | Switch to `svd_lowrank` |
|
||
| <span style="color: #ff6b6b;">Training instability</span> | Extreme weight values | Enable `clamp_quantile` |
|
||
| <span style="color: #ff6b6b;">Memory errors</span> | Large model with full SVD | Use `svd_lowrank` or reduce model size |
|
||
|
||
## <span style="color: #98d8c8;">📊 Comparison of LoRA Types</span>
|
||
|
||
### <span style="color: #b19cd9;">📈 Quality vs File Size Trade-offs</span>
|
||
```
|
||
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
|
||
```
|
||
|
||
### <span style="color: #b19cd9;">⏱️ Processing Speed Comparison</span>
|
||
```
|
||
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 │
|
||
└─────────────────────────────────────────────────────────┘
|
||
```
|
||
|
||
## <span style="color: #98d8c8;">🔬 Algorithm Details</span>
|
||
|
||
### <span style="color: #add8e6;">📊 SVD Decomposition Process</span>
|
||
|
||
```
|
||
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
|
||
|
||
### <span style="color: #ffb6c1;">🎯 Adaptive Rank Selection</span>
|
||
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
|
||
```
|
||
|
||
#### <span style="color: #87ceeb;">📏 Adaptive Ratio Method</span>
|
||
```
|
||
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: ↑↑↑↑↑↑↑↑↑↑↑↑↑
|
||
```
|
||
|
||
#### <span style="color: #98fb98;">📊 Adaptive Quantile Method</span>
|
||
```
|
||
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)
|
||
```
|
||
|
||
#### <span style="color: #f4a460;">⚡ Adaptive Energy Method</span>
|
||
```
|
||
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)
|
||
```
|
||
|
||
#### <span style="color: #ffa07a;">🎯 Adaptive Frobenius Method</span>
|
||
```
|
||
Frobenius Norm: √(S₁² + S₂² + ... + Sᵣ²) = 0.95 × √(S₁² + ... + Sₙ²)
|
||
|
||
Progressive: ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
|
||
└──────────────────────────────────────┘
|
||
95% of total Frobenius norm
|
||
|
||
Selected: ↑↑↑↑↑↑↑↑↑ (rank = 9, preserves 95% of matrix information)
|
||
```
|
||
|
||
- <span style="color: #87ceeb;">**Ratio**</span>: Keeps components above relative importance threshold
|
||
- <span style="color: #98fb98;">**Quantile**</span>: Statistical distribution-based selection
|
||
- <span style="color: #f4a460;">**Energy**</span>: Preserves mathematical energy content
|
||
- <span style="color: #ffa07a;">**Frobenius**</span>: Maintains matrix information content
|
||
|
||
Each method provides feedback showing the actual rank selected and preservation metrics.
|
||
|
||
## <span style="color: #98d8c8;">⚙️ Technical Implementation</span>
|
||
|
||
### <span style="color: #e6e6fa;">🔑 Key Functions</span>
|
||
- `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)
|
||
|
||
### <span style="color: #e6e6fa;">💾 Memory Management</span>
|
||
- Models are moved to CPU after processing
|
||
- GPU memory is cleared between operations
|
||
- Progress bars show extraction status
|
||
- Batch processing for large models
|
||
|
||
### <span style="color: #e6e6fa;">🛡️ Error Handling</span>
|
||
- Skips 5D tensors (patch embeddings)
|
||
- Handles failed SVD decompositions gracefully
|
||
- Validates algorithm compatibility
|
||
- Provides detailed logging
|
||
|
||
## <span style="color: #98d8c8;">🔗 Integration Notes</span>
|
||
|
||
### <span style="color: #ffd700;">🎨 ComfyUI Integration</span>
|
||
- Located in "KJNodes/lora" category
|
||
- Output node (saves to disk)
|
||
- Supports standard ComfyUI model handling
|
||
- Compatible with all model formats
|
||
|
||
### <span style="color: #ffd700;">📁 File Format</span>
|
||
- Outputs in SafeTensors format
|
||
- Includes metadata about extraction parameters
|
||
- Compatible with standard LoRA loaders
|
||
- Supports automatic dtype conversion
|
||
|
||
## <span style="color: #98d8c8;">🎯 Decision Tree for Choosing LoRA Type</span>
|
||
|
||
```
|
||
┌─────────────────────────────────────────────────────────────────┐
|
||
│ 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) │
|
||
└───────────────┘ └─────────────┘ └─────────────┘
|
||
```
|
||
|
||
## <span style="color: #98d8c8;">🎯 Memory Flow Diagram</span>
|
||
|
||
```
|
||
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 │
|
||
└─────────────────────────────────────────────────────────────────┘
|
||
```
|
||
|
||
## <span style="color: #98d8c8;">🎯 Conclusion</span>
|
||
|
||
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 |