From ede0957f65ee23aa2fa2f3fb96ad2d838adf19e0 Mon Sep 17 00:00:00 2001 From: John Pollock Date: Mon, 29 Sep 2025 04:46:07 -0500 Subject: [PATCH] feat: add caching and logging to IS_CHANGED methods in safetensor overrides - Implement caching of the last computed settings hash using a class attribute `_last_hash` - Compare current hash against the cached one to detect changes - Add logging to indicate first call or when settings have changed, using shortened hash for brevity - Applied consistently across `override_class_with_distorch_safetensor_v2`, `_v2_clip`, and `_v2_clip_no_device` - Improves efficiency by avoiding redundant change detection and aids debugging of settings modifications --- distorch_2.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/distorch_2.py b/distorch_2.py index 7a5c3f8..19143f6 100644 --- a/distorch_2.py +++ b/distorch_2.py @@ -868,9 +868,16 @@ def override_class_with_distorch_safetensor_v2(cls): @classmethod def IS_CHANGED(s, *args, compute_device=None, virtual_vram_gb=4.0, donor_device="cpu", expert_mode_allocations="", keep_loaded=True, **kwargs): - # Create a hash of our specific settings settings_str = f"{compute_device}{virtual_vram_gb}{donor_device}{expert_mode_allocations}{keep_loaded}" - return hashlib.sha256(settings_str.encode()).hexdigest() + current_hash = hashlib.sha256(settings_str.encode()).hexdigest() + + if not hasattr(cls, '_last_hash'): + cls._last_hash = current_hash + logger.mgpu_mm_log(f"IS_CHANGED first call: {current_hash[:8]}") + elif cls._last_hash != current_hash: + cls._last_hash = current_hash + logger.mgpu_mm_log(f"IS_CHANGED CHANGED: {current_hash[:8]} ← settings changed") + return current_hash def override(self, *args, compute_device=None, virtual_vram_gb=4.0, donor_device="cpu", expert_mode_allocations="", keep_loaded=True, **kwargs): @@ -967,7 +974,15 @@ def override_class_with_distorch_safetensor_v2_clip(cls): donor_device="cpu", expert_mode_allocations="", keep_loaded=True, **kwargs): # Create a hash of our specific settings settings_str = f"{device}{virtual_vram_gb}{donor_device}{expert_mode_allocations}{keep_loaded}" # Changed from compute_device - return hashlib.sha256(settings_str.encode()).hexdigest() + current_hash = hashlib.sha256(settings_str.encode()).hexdigest() + + if not hasattr(cls, '_last_hash'): + cls._last_hash = current_hash + logger.mgpu_mm_log(f"IS_CHANGED first call: {current_hash[:8]}") + elif cls._last_hash != current_hash: + cls._last_hash = current_hash + logger.mgpu_mm_log(f"IS_CHANGED CHANGED: {current_hash[:8]} ← settings changed") + return current_hash def override(self, *args, device=None, virtual_vram_gb=4.0, # Changed from compute_device donor_device="cpu", expert_mode_allocations="", keep_loaded=True, **kwargs): @@ -1066,8 +1081,15 @@ def override_class_with_distorch_safetensor_v2_clip_no_device(cls): donor_device="cpu", expert_mode_allocations="", keep_loaded=True, **kwargs): # Create a hash of our specific settings settings_str = f"{device}{virtual_vram_gb}{donor_device}{expert_mode_allocations}{keep_loaded}" # Changed from compute_device - return hashlib.sha256(settings_str.encode()).hexdigest() + current_hash = hashlib.sha256(settings_str.encode()).hexdigest() + if not hasattr(cls, '_last_hash'): + cls._last_hash = current_hash + logger.mgpu_mm_log(f"IS_CHANGED first call: {current_hash[:8]}") + elif cls._last_hash != current_hash: + cls._last_hash = current_hash + logger.mgpu_mm_log(f"IS_CHANGED CHANGED: {current_hash[:8]} ← settings changed") + return current_hash def override(self, *args, device=None, virtual_vram_gb=4.0, # Changed from compute_device donor_device="cpu", expert_mode_allocations="", keep_loaded=True, **kwargs):