Commit 16ce04c5 authored by Your Name's avatar Your Name

Fix cleanup handling for different model types

- Make cleanup() method handle StableDiffusion and other non-ModelManager objects
- Add try-except in on-demand swap to handle cleanup failures gracefully
- Check if cleanup method exists before calling
parent 362b8452
...@@ -2514,7 +2514,15 @@ class MultiModelManager: ...@@ -2514,7 +2514,15 @@ class MultiModelManager:
# Unload the current model from VRAM # Unload the current model from VRAM
current_model = self.models[self.current_model_key] current_model = self.models[self.current_model_key]
print(f"ON-DEMAND SWAP: Unloading model '{self.current_model_key}' from VRAM to load '{requested_model}'") print(f"ON-DEMAND SWAP: Unloading model '{self.current_model_key}' from VRAM to load '{requested_model}'")
current_model.cleanup() try:
if hasattr(current_model, 'cleanup') and callable(getattr(current_model, 'cleanup')):
current_model.cleanup()
elif hasattr(current_model, 'model') and current_model.model is not None:
# Handle ModelManager objects
if hasattr(current_model.model, 'cleanup'):
current_model.model.cleanup()
except Exception as e:
print(f"ON-DEMAND SWAP: Warning during cleanup of '{self.current_model_key}': {e}")
del self.models[self.current_model_key] del self.models[self.current_model_key]
# Load the new model on-demand # Load the new model on-demand
...@@ -2562,7 +2570,15 @@ class MultiModelManager: ...@@ -2562,7 +2570,15 @@ class MultiModelManager:
# Unload the current model from VRAM # Unload the current model from VRAM
current_model = self.models[self.current_model_key] current_model = self.models[self.current_model_key]
print(f"ON-DEMAND SWAP: Unloading model '{self.current_model_key}' from VRAM to load '{model_name}'") print(f"ON-DEMAND SWAP: Unloading model '{self.current_model_key}' from VRAM to load '{model_name}'")
current_model.cleanup() try:
if hasattr(current_model, 'cleanup') and callable(getattr(current_model, 'cleanup')):
current_model.cleanup()
elif hasattr(current_model, 'model') and current_model.model is not None:
# Handle ModelManager objects
if hasattr(current_model.model, 'cleanup'):
current_model.model.cleanup()
except Exception as e:
print(f"ON-DEMAND SWAP: Warning during cleanup of '{self.current_model_key}': {e}")
del self.models[self.current_model_key] del self.models[self.current_model_key]
# Load the new model on-demand # Load the new model on-demand
...@@ -2713,8 +2729,22 @@ class MultiModelManager: ...@@ -2713,8 +2729,22 @@ class MultiModelManager:
def cleanup(self): def cleanup(self):
"""Cleanup all models.""" """Cleanup all models."""
for model in self.models.values(): for key, model in self.models.items():
model.cleanup() try:
if hasattr(model, 'cleanup') and callable(getattr(model, 'cleanup')):
model.cleanup()
elif hasattr(model, 'model') and model.model is not None:
# Handle ModelManager objects
if hasattr(model.model, 'cleanup'):
model.model.cleanup()
elif hasattr(model.model, 'model'):
# Nested model (e.g., StableDiffusion wrapper)
if model.model.model is not None:
del model.model.model
# Remove from dict
del self.models[key]
except Exception as e:
print(f"Warning: Failed to cleanup model '{key}': {e}")
self.models.clear() self.models.clear()
# Global multi-model manager # Global multi-model manager
multi_model_manager = MultiModelManager() multi_model_manager = MultiModelManager()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment