見出し画像

independently Distorch Memory Cleaner

Upscaling of WAN2.2におけるOOM対策(VRAMではなくシステムメモリ不足により生じるという点が重要。私のPC、64GB RAM環境でも解像度と長さ次第では生じる)として、一度はMultiGPUノードに追加する形で作成した同機能ですが、

メンテナンス性と持続性(MultiGPUノード更新毎に書き換える必要が生じる)に弱点があり、かつ導入難易度がそれなりに高い為(ファイル構造を熟知する必要あり。トラブル発生時に自力で元に戻せる知識も必要)、同機能だけを切り出して、独立したノードとして作成しました。custom_nodesフォルダに設置するだけなので、導入も削除も簡単になります。

以下、GitHubでも公開しています。

また、初回版では入出力はimageに限定していましたが、Purge VRAM V2と同様にAnything対応として汎用性を高めてあります。

PurgeVRAMとの対比です。Distorchのアンロードに特化させた為に、PurgeVRAMのようなモデルアンロード機能はありません。併用する必要があります。

性能とDistorchのクリア機能について詳しく分析します:

## 性能比較

### **Purge VRAM V2**(比較対象)

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**(私たちの実装)

**MemoryCleaner**(基本版):

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**(推奨版):

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

## **性能評価**

### ✅ **Distorch Memory Managerの優位点**

1. **Distorch専用機能**:

   - `comfy.model_management.free_memory(0, 'cuda:0')` - Distorchの仮想メモリを直接解放

   - `comfy.model_management.free_memory(0, 'cpu')` - CPU仮想メモリも解放

2. **より詳細なメモリ管理**:

   - `torch.cuda.synchronize()` - GPU処理の完全同期

   - メモリ使用量の前後測定と表示

   - 詳細なログ出力で効果を確認可能

3. **安全設計**:

   - UI破損を防ぐ安全なメモリクリア

   - 例外処理でエラーから保護

### ⚠️ **Purge VRAMの優位点**

1. **モデルアンロード機能**:

   - `comfy.model_management.unload_all_models()` - 全モデルを強制アンロード

   - `comfy.model_management.soft_empty_cache()` - ソフトキャッシュクリア

## **Distorchのクリア機能について**

### ✅ **Distorch専用機能が含まれている**

# 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

この部分が**Distorch専用のメモリクリア機能**です。Purge VRAMには含まれていません。

## **推奨設定**

**動画生成時(WAN2.2など)**:

- **SafeMemoryManager**を使用

- `clean_gpu: True`

- `force_gc: True` 

- `reset_virtual_memory: True`

**最大限のメモリ解放が必要な場合**:

- **MemoryManager**を使用

- `clean_cpu: True`(注意:UI破損の可能性あり)

## **結論**

**性能的にはDistorch Memory Managerの方が上**です:

1. **Distorch専用機能**:`free_memory()`で仮想メモリを直接解放

2. **より詳細な管理**:メモリ使用量測定、詳細ログ

3. **安全設計**:UI破損を防ぐ配慮

4. **柔軟性**:3つの異なるレベルのノード

ただし、**モデルアンロード機能**が必要な場合は、Purge VRAMと組み合わせて使用することをお勧めします。

ここまで公開してきたWAN2.2用jsonにも、順次適用して更新します。

また繰り返しますが、ページングファイルサイズの拡大によっても、Upscalingに限ってはOOM発生率は下げることが出来ます。
(動画生成時推論においてはVRAMが重要なので、その工程にて生じるOOMに対しては、ページングファイルでは解決しない)


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