SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

independently Distorch Memory Cleaner

As an OOM countermeasure for WAN2.2 upscaling(It is important to note that this occurs due to system memory shortages, not VRAM. Even on my PC with 64GB of RAM, it can happen depending on the resolution and length), I initially created this feature by adding it to the MultiGPU node, but...

It had weaknesses in terms of maintainability and sustainability(it requires rewriting every time the MultiGPU node is updated), and the installation difficulty was relatively high(requires deep knowledge of the file structure. You also need the knowledge to revert it yourself if a problem occurs), so I extracted just this feature and created it as an independent node. Since it only needs to be placed in the custom_nodes folder, installation and removal are now simple.

It is also available on GitHub below.

Also, while the initial version limited input/output to images, I have increased its versatility to support Anything, similar to Purge VRAM V2.

This is a comparison with PurgeVRAM. Because it is specialized for unloading Distorch, it does not have a model unloading function like PurgeVRAM. You need to use them together.

I will analyze the performance and Distorch's clearing function in detail:

## Performance Comparison

### **Purge VRAM V2** (Comparison target)

def purge_vram_v2(self, anything, purge_cache, purge_models):
    clear_memory()  # gc.collect() + torch.cuda.empty_cache() + torch.cuda.ipc_collect()
    if purge_models:
        comfy.model_management.unload_all_models()  # 全モデルアンロード
        comfy.model_management.soft_empty_cache()   # ソフトキャッシュクリア
    return (anything,)

### **Distorch Memory Manager** (Our implementation)

**MemoryCleaner** (Basic version):

def clean_memory(self, anything):
    torch.cuda.empty_cache()      # GPUキャッシュクリア
    torch.cuda.synchronize()      # GPU同期(追加)
    gc.collect()                  # ガベージコレクション
    comfy.model_management.free_memory(0, 'cuda:0')  # Distorch専用
    comfy.model_management.free_memory(0, 'cpu')     # Distorch専用

**SafeMemoryManager** (Recommended version):

def safe_manage_memory(self, anything, clean_gpu, force_gc, reset_virtual_memory):
    # メモリ使用量測定(前後)
    # GPUメモリクリア + 同期
    # ガベージコレクション
    # Distorch仮想メモリリセット
    # 詳細なログ出力

## **Performance Evaluation**

### ✅ **Advantages of Distorch Memory Manager**

1. **Distorch-specific features**:

- `comfy.model_management.free_memory(0, 'cuda:0')` - Directly frees Distorch virtual memory

- `comfy.model_management.free_memory(0, 'cpu')` - Also frees CPU virtual memory

2. **More detailed memory management**:

- `torch.cuda.synchronize()` - Complete synchronization of GPU processing

- Measurement and display of memory usage before and after

- Effectiveness can be verified through detailed log output

3. **Safety Design**:

- Safe memory clearing to prevent UI corruption

- Protection against errors via exception handling

### ⚠️ **Advantages of Purge VRAM**

1. **Model Unload Function**:

- `comfy.model_management.unload_all_models()` - Force unload all models

- `comfy.model_management.soft_empty_cache()` - Soft cache clear

## **About Distorch's Clearing Function**

### ✅ **Includes Distorch-exclusive features**

# DisTorchの仮想メモリを解放
try:
    import comfy.model_management
    # 仮想メモリの割り当てをリセット
    if hasattr(comfy.model_management, 'free_memory'):
        comfy.model_management.free_memory(0, 'cuda:0')  # Distorch GPU仮想メモリ
        comfy.model_management.free_memory(0, 'cpu')     # Distorch CPU仮想メモリ
except:
    pass

This part is the **Distorch-exclusive memory clearing function**. It is not included in Purge VRAM.

## **Recommended Settings**

**When generating videos (e.g., WAN2.2)**:

- Use **SafeMemoryManager**

- `clean_gpu: True`

- `force_gc: True`

- `reset_virtual_memory: True`

**When maximum memory release is required**:

- Use **MemoryManager**

- `clean_cpu: True` (Note: Potential for UI corruption)

## **Conclusion**

**In terms of performance, Distorch Memory Manager is superior**:

1. **Distorch-exclusive feature**: Directly release virtual memory with `free_memory()`

2. **More detailed management**: Memory usage measurement, detailed logs

3. **Safety design**: Considerations to prevent UI corruption

4. **Flexibility**: Three different levels of nodes

However, if you need a **model unloading function**, we recommend using it in combination with Purge VRAM.

...

I will also sequentially apply and update the JSON for WAN2.2 that I have released so far.

Also, to reiterate, increasing the paging file size can reduce the OOM occurrence rate specifically for Upscaling.
(Since VRAM is important for inference during video generation, paging files will not resolve OOM that occurs during that process.)


いいなと思ったら応援しよう!