2023-04-28 04:41:52 +00:00
|
|
|
"""
|
2023-05-03 16:38:18 +00:00
|
|
|
Manage a RAM cache of diffusion/transformer models for fast switching.
|
|
|
|
They are moved between GPU VRAM and CPU RAM as necessary. If the cache
|
2023-04-28 04:41:52 +00:00
|
|
|
grows larger than a preset maximum, then the least recently used
|
|
|
|
model will be cleared and (re)loaded from disk when next needed.
|
2023-05-03 16:38:18 +00:00
|
|
|
|
|
|
|
The cache returns context manager generators designed to load the
|
|
|
|
model into the GPU within the context, and unload outside the
|
|
|
|
context. Use like this:
|
|
|
|
|
|
|
|
cache = ModelCache(max_models_cached=6)
|
|
|
|
with cache.get_model('runwayml/stable-diffusion-1-5') as SD1,
|
|
|
|
cache.get_model('stabilityai/stable-diffusion-2') as SD2:
|
|
|
|
do_something_in_GPU(SD1,SD2)
|
|
|
|
|
|
|
|
|
2023-04-28 04:41:52 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import contextlib
|
|
|
|
import gc
|
2023-05-03 16:38:18 +00:00
|
|
|
import hashlib
|
2023-05-07 22:07:28 +00:00
|
|
|
import logging
|
2023-04-28 04:41:52 +00:00
|
|
|
import warnings
|
2023-05-05 03:15:32 +00:00
|
|
|
from collections import Counter
|
2023-05-02 02:57:30 +00:00
|
|
|
from enum import Enum
|
2023-04-28 04:41:52 +00:00
|
|
|
from pathlib import Path
|
2023-05-07 22:07:28 +00:00
|
|
|
from psutil import Process
|
|
|
|
from typing import Dict, Sequence, Union, Tuple, types
|
2023-04-28 04:41:52 +00:00
|
|
|
|
2023-05-03 16:38:18 +00:00
|
|
|
import torch
|
2023-05-06 19:58:44 +00:00
|
|
|
import safetensors.torch
|
2023-05-05 23:32:28 +00:00
|
|
|
from diffusers import StableDiffusionPipeline, AutoencoderKL, SchedulerMixin, UNet2DConditionModel
|
2023-05-03 16:38:18 +00:00
|
|
|
from diffusers import logging as diffusers_logging
|
|
|
|
from diffusers.pipelines.stable_diffusion.safety_checker import \
|
|
|
|
StableDiffusionSafetyChecker
|
|
|
|
from huggingface_hub import HfApi
|
|
|
|
from picklescan.scanner import scan_file_path
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
|
|
|
|
from transformers import logging as transformers_logging
|
|
|
|
|
2023-05-05 03:15:32 +00:00
|
|
|
import invokeai.backend.util.logging as logger
|
2023-05-03 16:38:18 +00:00
|
|
|
from ..globals import global_cache_dir
|
|
|
|
from ..stable_diffusion import StableDiffusionGeneratorPipeline
|
2023-04-28 04:41:52 +00:00
|
|
|
|
2023-05-07 22:07:28 +00:00
|
|
|
# Maximum size of the cache, in gigs
|
|
|
|
# Default is roughly enough to hold three fp16 diffusers models in RAM simultaneously
|
|
|
|
DEFAULT_MAX_CACHE_SIZE = 6.0
|
|
|
|
|
|
|
|
# actual size of a gig
|
|
|
|
GIG = 1073741824
|
2023-05-02 02:57:30 +00:00
|
|
|
|
|
|
|
# This is the mapping from the stable diffusion submodel dict key to the class
|
|
|
|
class SDModelType(Enum):
|
|
|
|
diffusion_pipeline=StableDiffusionGeneratorPipeline # whole thing
|
2023-05-06 19:58:44 +00:00
|
|
|
diffusers=StableDiffusionGeneratorPipeline # same thing
|
|
|
|
vae=AutoencoderKL # diffusers parts
|
2023-05-02 02:57:30 +00:00
|
|
|
text_encoder=CLIPTextModel
|
|
|
|
tokenizer=CLIPTokenizer
|
|
|
|
unet=UNet2DConditionModel
|
|
|
|
scheduler=SchedulerMixin
|
|
|
|
safety_checker=StableDiffusionSafetyChecker
|
|
|
|
feature_extractor=CLIPFeatureExtractor
|
2023-05-06 19:58:44 +00:00
|
|
|
# These are all loaded as dicts of tensors
|
|
|
|
lora=dict
|
|
|
|
textual_inversion=dict
|
|
|
|
ckpt=dict
|
2023-05-05 23:32:28 +00:00
|
|
|
|
|
|
|
class ModelStatus(Enum):
|
|
|
|
unknown='unknown'
|
|
|
|
not_loaded='not loaded'
|
|
|
|
in_ram='cached'
|
|
|
|
in_vram='in gpu'
|
|
|
|
active='locked in gpu'
|
2023-05-07 22:07:28 +00:00
|
|
|
|
|
|
|
# This is used to guesstimate the size of a model before we load it.
|
|
|
|
# After loading, we will know it exactly.
|
|
|
|
# Sizes are in Gigs, estimated for float16; double for float32
|
|
|
|
SIZE_GUESSTIMATE = {
|
|
|
|
SDModelType.diffusion_pipeline: 2.5,
|
|
|
|
SDModelType.diffusers: 2.5,
|
|
|
|
SDModelType.vae: 0.35,
|
|
|
|
SDModelType.text_encoder: 0.5,
|
|
|
|
SDModelType.tokenizer: 0.0001,
|
|
|
|
SDModelType.unet: 3.4,
|
|
|
|
SDModelType.scheduler: 0.0001,
|
|
|
|
SDModelType.safety_checker: 1.2,
|
|
|
|
SDModelType.feature_extractor: 0.0001,
|
|
|
|
SDModelType.lora: 0.1,
|
|
|
|
SDModelType.textual_inversion: 0.0001,
|
|
|
|
SDModelType.ckpt: 4.2,
|
|
|
|
}
|
2023-04-28 04:41:52 +00:00
|
|
|
|
2023-05-02 20:52:27 +00:00
|
|
|
# The list of model classes we know how to fetch, for typechecking
|
2023-05-02 02:57:30 +00:00
|
|
|
ModelClass = Union[tuple([x.value for x in SDModelType])]
|
2023-05-05 23:32:28 +00:00
|
|
|
DiffusionClasses = (StableDiffusionGeneratorPipeline, AutoencoderKL, SchedulerMixin, UNet2DConditionModel)
|
2023-05-02 02:57:30 +00:00
|
|
|
|
2023-05-02 20:52:27 +00:00
|
|
|
# Legacy information needed to load a legacy checkpoint file
|
|
|
|
class LegacyInfo(BaseModel):
|
|
|
|
config_file: Path
|
2023-05-03 16:38:18 +00:00
|
|
|
vae_file: Path = None
|
|
|
|
|
|
|
|
class UnsafeModelException(Exception):
|
|
|
|
"Raised when a legacy model file fails the picklescan test"
|
|
|
|
pass
|
|
|
|
|
|
|
|
class UnscannableModelException(Exception):
|
|
|
|
"Raised when picklescan is unable to scan a legacy model file"
|
|
|
|
pass
|
2023-05-02 20:52:27 +00:00
|
|
|
|
2023-05-06 04:44:12 +00:00
|
|
|
class ModelLocker(object):
|
|
|
|
"Forward declaration"
|
|
|
|
pass
|
|
|
|
|
2023-04-28 04:41:52 +00:00
|
|
|
class ModelCache(object):
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-07 22:07:28 +00:00
|
|
|
max_cache_size: float=DEFAULT_MAX_CACHE_SIZE,
|
2023-04-28 04:41:52 +00:00
|
|
|
execution_device: torch.device=torch.device('cuda'),
|
2023-05-03 16:38:18 +00:00
|
|
|
storage_device: torch.device=torch.device('cpu'),
|
2023-04-28 04:41:52 +00:00
|
|
|
precision: torch.dtype=torch.float16,
|
|
|
|
sequential_offload: bool=False,
|
2023-05-05 03:15:32 +00:00
|
|
|
lazy_offloading: bool=True,
|
2023-05-02 20:52:27 +00:00
|
|
|
sha_chunksize: int = 16777216,
|
2023-05-05 23:32:28 +00:00
|
|
|
logger: types.ModuleType = logger
|
2023-04-28 04:41:52 +00:00
|
|
|
):
|
2023-05-02 20:52:27 +00:00
|
|
|
'''
|
2023-05-03 16:38:18 +00:00
|
|
|
:param max_models: Maximum number of models to cache in CPU RAM [4]
|
2023-05-02 20:52:27 +00:00
|
|
|
:param execution_device: Torch device to load active model into [torch.device('cuda')]
|
2023-05-03 16:38:18 +00:00
|
|
|
:param storage_device: Torch device to save inactive model in [torch.device('cpu')]
|
2023-05-02 20:52:27 +00:00
|
|
|
:param precision: Precision for loaded models [torch.float16]
|
2023-05-05 03:15:32 +00:00
|
|
|
:param lazy_offloading: Keep model in VRAM until another model needs to be loaded
|
2023-05-02 20:52:27 +00:00
|
|
|
:param sequential_offload: Conserve VRAM by loading and unloading each stage of the pipeline sequentially
|
|
|
|
:param sha_chunksize: Chunksize to use when calculating sha256 model hash
|
|
|
|
'''
|
2023-04-28 04:41:52 +00:00
|
|
|
self.models: dict = dict()
|
|
|
|
self.stack: Sequence = list()
|
2023-05-05 03:15:32 +00:00
|
|
|
self.lazy_offloading = lazy_offloading
|
2023-04-28 04:41:52 +00:00
|
|
|
self.sequential_offload: bool=sequential_offload
|
|
|
|
self.precision: torch.dtype=precision
|
2023-05-07 22:07:28 +00:00
|
|
|
self.current_cache_size: int=0
|
|
|
|
self.max_cache_size: int=max_cache_size
|
2023-05-03 16:38:18 +00:00
|
|
|
self.execution_device: torch.device=execution_device
|
|
|
|
self.storage_device: torch.device=storage_device
|
2023-05-02 20:52:27 +00:00
|
|
|
self.sha_chunksize=sha_chunksize
|
2023-05-05 23:32:28 +00:00
|
|
|
self.logger = logger
|
2023-05-05 03:15:32 +00:00
|
|
|
self.loaded_models: set = set() # set of model keys loaded in GPU
|
|
|
|
self.locked_models: Counter = Counter() # set of model keys locked in GPU
|
2023-05-07 22:07:28 +00:00
|
|
|
self.model_sizes: Dict[str,int] = dict()
|
2023-04-28 04:41:52 +00:00
|
|
|
|
|
|
|
def get_model(
|
|
|
|
self,
|
|
|
|
repo_id_or_path: Union[str,Path],
|
2023-05-02 02:57:30 +00:00
|
|
|
model_type: SDModelType=SDModelType.diffusion_pipeline,
|
2023-04-28 04:41:52 +00:00
|
|
|
subfolder: Path=None,
|
2023-05-03 16:38:18 +00:00
|
|
|
submodel: SDModelType=None,
|
2023-04-28 04:41:52 +00:00
|
|
|
revision: str=None,
|
2023-05-02 20:52:27 +00:00
|
|
|
legacy_info: LegacyInfo=None,
|
2023-05-05 23:32:28 +00:00
|
|
|
attach_model_part: Tuple[SDModelType, str] = (None,None),
|
2023-05-03 16:38:18 +00:00
|
|
|
gpu_load: bool=True,
|
2023-05-06 04:44:12 +00:00
|
|
|
)->ModelLocker: # ?? what does it return
|
2023-04-28 04:41:52 +00:00
|
|
|
'''
|
2023-05-03 16:38:18 +00:00
|
|
|
Load and return a HuggingFace model wrapped in a context manager generator, with RAM caching.
|
|
|
|
Use like this:
|
|
|
|
|
|
|
|
cache = ModelCache()
|
|
|
|
with cache.get_model('stabilityai/stable-diffusion-2') as SD2:
|
|
|
|
do_something_with_the_model(SD2)
|
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
You can fetch an individual part of a diffusers model by passing the submodel
|
|
|
|
argument:
|
|
|
|
|
|
|
|
vae_context = cache.get_model(
|
|
|
|
'stabilityai/sd-stable-diffusion-2',
|
|
|
|
submodel=SDModelType.vae
|
|
|
|
)
|
|
|
|
|
|
|
|
Vice versa, you can load and attach an external submodel to a diffusers model
|
|
|
|
before returning it by passing the attach_submodel argument. This only works with
|
|
|
|
diffusers models:
|
|
|
|
|
|
|
|
pipeline_context = cache.get_model(
|
|
|
|
'runwayml/stable-diffusion-v1-5',
|
|
|
|
attach_model_part=(SDModelType.vae,'stabilityai/sd-vae-ft-mse')
|
|
|
|
)
|
|
|
|
|
2023-05-03 16:38:18 +00:00
|
|
|
The model will be locked into GPU VRAM for the duration of the context.
|
2023-04-28 04:41:52 +00:00
|
|
|
:param repo_id_or_path: either the HuggingFace repo_id or a Path to a local model
|
|
|
|
:param subfolder: name of a subfolder in which the model can be found, e.g. "vae"
|
2023-05-03 16:38:18 +00:00
|
|
|
:param submodel: an SDModelType enum indicating the model part to return, e.g. SDModelType.vae
|
2023-05-05 23:32:28 +00:00
|
|
|
:param attach_model_part: load and attach a diffusers model component. Pass a tuple of format (SDModelType,repo_id)
|
2023-04-28 04:41:52 +00:00
|
|
|
:param revision: model revision
|
|
|
|
:param model_class: class of model to return
|
2023-05-03 16:38:18 +00:00
|
|
|
:param gpu_load: load the model into GPU [default True]
|
2023-05-02 20:52:27 +00:00
|
|
|
:param legacy_info: a LegacyInfo object containing additional info needed to load a legacy ckpt
|
2023-04-28 04:41:52 +00:00
|
|
|
'''
|
2023-05-02 20:52:27 +00:00
|
|
|
key = self._model_key( # internal unique identifier for the model
|
2023-05-02 02:57:30 +00:00
|
|
|
repo_id_or_path,
|
|
|
|
model_type.value,
|
|
|
|
revision,
|
|
|
|
subfolder
|
2023-05-07 22:07:28 +00:00
|
|
|
)
|
|
|
|
|
2023-04-28 04:41:52 +00:00
|
|
|
if key in self.models: # cached - move to bottom of stack
|
|
|
|
with contextlib.suppress(ValueError):
|
|
|
|
self.stack.remove(key)
|
|
|
|
self.stack.append(key)
|
2023-05-03 16:38:18 +00:00
|
|
|
model = self.models[key]
|
2023-05-07 22:07:28 +00:00
|
|
|
|
2023-04-28 04:41:52 +00:00
|
|
|
else: # not cached -load
|
2023-05-07 22:07:28 +00:00
|
|
|
self.logger.info(f'Loading model {repo_id_or_path}, type {model_type}')
|
|
|
|
|
|
|
|
# this will remove older cached models until
|
|
|
|
# there is sufficient room to load the requested model
|
|
|
|
self._make_cache_room(key, model_type)
|
|
|
|
|
|
|
|
with MemoryUsage() as usage:
|
|
|
|
model = self._load_model_from_storage(
|
|
|
|
repo_id_or_path=repo_id_or_path,
|
|
|
|
model_class=model_type.value,
|
|
|
|
subfolder=subfolder,
|
|
|
|
revision=revision,
|
|
|
|
legacy_info=legacy_info,
|
|
|
|
)
|
|
|
|
logger.debug(f'Actual memory used to load model: {(usage.mem_used/GIG):.2f} GB')
|
|
|
|
self.model_sizes[key] = usage.mem_used
|
|
|
|
self.current_cache_size += usage.mem_used
|
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
if model_type==SDModelType.diffusion_pipeline and attach_model_part[0]:
|
|
|
|
self.attach_part(model,*attach_model_part)
|
2023-04-28 04:41:52 +00:00
|
|
|
self.stack.append(key) # add to LRU cache
|
|
|
|
self.models[key]=model # keep copy of model in dict
|
2023-05-03 16:38:18 +00:00
|
|
|
|
|
|
|
if submodel:
|
|
|
|
model = getattr(model, submodel.name)
|
2023-05-05 03:15:32 +00:00
|
|
|
|
2023-05-06 04:44:12 +00:00
|
|
|
return self.ModelLocker(self, key, model, gpu_load)
|
|
|
|
|
2023-05-06 19:58:44 +00:00
|
|
|
def uncache_model(self, key: str):
|
|
|
|
'''Remove corresponding model from the cache'''
|
|
|
|
if key is not None and key in self.models:
|
2023-05-07 22:07:28 +00:00
|
|
|
with contextlib.suppress(ValueError), contextlib.suppress(KeyError):
|
2023-05-06 19:58:44 +00:00
|
|
|
del self.models[key]
|
|
|
|
del self.locked_models[key]
|
|
|
|
self.loaded_models.remove(key)
|
2023-05-07 22:07:28 +00:00
|
|
|
self.stack.remove(key)
|
2023-05-06 19:58:44 +00:00
|
|
|
|
2023-05-06 04:44:12 +00:00
|
|
|
class ModelLocker(object):
|
|
|
|
def __init__(self, cache, key, model, gpu_load):
|
|
|
|
self.gpu_load = gpu_load
|
|
|
|
self.cache = cache
|
|
|
|
self.key = key
|
|
|
|
# This will keep a copy of the model in RAM until the locker
|
|
|
|
# is garbage collected. Needs testing!
|
|
|
|
self.model = model
|
|
|
|
|
|
|
|
def __enter__(self)->ModelClass:
|
|
|
|
cache = self.cache
|
|
|
|
key = self.key
|
|
|
|
model = self.model
|
|
|
|
if self.gpu_load and hasattr(model,'to'):
|
|
|
|
cache.loaded_models.add(key)
|
|
|
|
cache.locked_models[key] += 1
|
|
|
|
if cache.lazy_offloading:
|
|
|
|
cache._offload_unlocked_models()
|
|
|
|
cache.logger.debug(f'Loading {key} into {cache.execution_device}')
|
|
|
|
model.to(cache.execution_device) # move into GPU
|
|
|
|
cache._print_cuda_stats()
|
|
|
|
else:
|
|
|
|
# in the event that the caller wants the model in RAM, we
|
|
|
|
# move it into CPU if it is in GPU and not locked
|
|
|
|
if hasattr(model,'to') and (key in cache.loaded_models
|
|
|
|
and cache.locked_models[key] == 0):
|
|
|
|
model.to(cache.storage_device)
|
|
|
|
cache.loaded_models.remove(key)
|
|
|
|
return model
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
key = self.key
|
|
|
|
cache = self.cache
|
|
|
|
cache.locked_models[key] -= 1
|
|
|
|
if not cache.lazy_offloading:
|
|
|
|
cache._offload_unlocked_models()
|
|
|
|
cache._print_cuda_stats()
|
2023-05-05 03:15:32 +00:00
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
def attach_part(self,
|
|
|
|
diffusers_model: StableDiffusionPipeline,
|
|
|
|
part_type: SDModelType,
|
|
|
|
part_id: str
|
|
|
|
):
|
|
|
|
'''
|
|
|
|
Attach a diffusers model part to a diffusers model. This can be
|
|
|
|
used to replace the VAE, tokenizer, textencoder, unet, etc.
|
|
|
|
:param diffuser_model: The diffusers model to attach the part to.
|
|
|
|
:param part_type: An SD ModelType indicating the part
|
|
|
|
:param part_id: A HF repo_id for the part
|
|
|
|
'''
|
|
|
|
part_key = part_type.name
|
|
|
|
part_class = part_type.value
|
|
|
|
part = self._load_diffusers_from_storage(
|
|
|
|
part_id,
|
|
|
|
model_class=part_class,
|
|
|
|
)
|
|
|
|
part.to(diffusers_model.device)
|
|
|
|
setattr(diffusers_model,part_key,part)
|
|
|
|
self.logger.debug(f'Attached {part_key} {part_id}')
|
|
|
|
|
|
|
|
def status(self,
|
|
|
|
repo_id_or_path: Union[str,Path],
|
|
|
|
model_type: SDModelType=SDModelType.diffusion_pipeline,
|
|
|
|
revision: str=None,
|
|
|
|
subfolder: Path=None,
|
|
|
|
)->ModelStatus:
|
|
|
|
key = self._model_key(
|
|
|
|
repo_id_or_path,
|
|
|
|
model_type.value,
|
|
|
|
revision,
|
|
|
|
subfolder)
|
|
|
|
if key not in self.models:
|
|
|
|
return ModelStatus.not_loaded
|
|
|
|
if key in self.loaded_models:
|
|
|
|
if self.locked_models[key] > 0:
|
|
|
|
return ModelStatus.active
|
|
|
|
else:
|
|
|
|
return ModelStatus.in_vram
|
|
|
|
else:
|
|
|
|
return ModelStatus.in_ram
|
2023-04-28 04:41:52 +00:00
|
|
|
|
2023-05-03 16:38:18 +00:00
|
|
|
def model_hash(self,
|
|
|
|
repo_id_or_path: Union[str,Path],
|
2023-05-05 23:32:28 +00:00
|
|
|
revision: str="main")->str:
|
2023-05-02 20:52:27 +00:00
|
|
|
'''
|
|
|
|
Given the HF repo id or path to a model on disk, returns a unique
|
|
|
|
hash. Works for legacy checkpoint files, HF models on disk, and HF repo IDs
|
|
|
|
:param repo_id_or_path: repo_id string or Path to model file/directory on disk.
|
|
|
|
:param revision: optional revision string (if fetching a HF repo_id)
|
|
|
|
'''
|
2023-05-05 23:32:28 +00:00
|
|
|
revision = revision or "main"
|
2023-05-02 20:52:27 +00:00
|
|
|
if self.is_legacy_ckpt(repo_id_or_path):
|
|
|
|
return self._legacy_model_hash(repo_id_or_path)
|
|
|
|
elif Path(repo_id_or_path).is_dir():
|
|
|
|
return self._local_model_hash(repo_id_or_path)
|
|
|
|
else:
|
|
|
|
return self._hf_commit_hash(repo_id_or_path,revision)
|
|
|
|
|
2023-05-07 22:07:28 +00:00
|
|
|
def cache_size(self)->float:
|
|
|
|
"Return the current size of the cache, in GB"
|
|
|
|
return self.current_cache_size / GIG
|
2023-05-03 16:38:18 +00:00
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
@classmethod
|
|
|
|
def is_legacy_ckpt(cls, repo_id_or_path: Union[str,Path])->bool:
|
|
|
|
'''
|
|
|
|
Return true if the indicated path is a legacy checkpoint
|
|
|
|
:param repo_id_or_path: either the HuggingFace repo_id or a Path to a local model
|
|
|
|
'''
|
|
|
|
path = Path(repo_id_or_path)
|
2023-05-06 19:58:44 +00:00
|
|
|
return path.suffix in [".ckpt",".safetensors",".pt"]
|
2023-05-05 23:32:28 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def scan_model(cls, model_name, checkpoint):
|
|
|
|
"""
|
|
|
|
Apply picklescanner to the indicated checkpoint and issue a warning
|
|
|
|
and option to exit if an infected file is identified.
|
|
|
|
"""
|
|
|
|
# scan model
|
|
|
|
logger.debug(f"Scanning Model: {model_name}")
|
|
|
|
scan_result = scan_file_path(checkpoint)
|
|
|
|
if scan_result.infected_files != 0:
|
|
|
|
if scan_result.infected_files == 1:
|
|
|
|
raise UnsafeModelException("The legacy model you are trying to load may contain malware. Aborting.")
|
|
|
|
else:
|
|
|
|
raise UnscannableModelException("InvokeAI was unable to scan the legacy model you requested. Aborting")
|
|
|
|
else:
|
|
|
|
logger.debug("Model scanned ok")
|
|
|
|
|
2023-04-28 04:41:52 +00:00
|
|
|
@staticmethod
|
|
|
|
def _model_key(path,model_class,revision,subfolder)->str:
|
2023-05-02 02:57:30 +00:00
|
|
|
return ':'.join([str(path),model_class.__name__,str(revision or ''),str(subfolder or '')])
|
2023-04-28 04:41:52 +00:00
|
|
|
|
2023-05-03 16:38:18 +00:00
|
|
|
def _has_cuda(self)->bool:
|
|
|
|
return self.execution_device.type == 'cuda'
|
|
|
|
|
|
|
|
def _print_cuda_stats(self):
|
2023-05-07 22:07:28 +00:00
|
|
|
vram = "%4.2fG" % (torch.cuda.memory_allocated() / GIG)
|
|
|
|
ram = "%4.2fG" % (self.current_cache_size / GIG)
|
2023-05-05 03:15:32 +00:00
|
|
|
loaded_models = len(self.loaded_models)
|
|
|
|
locked_models = len([x for x in self.locked_models if self.locked_models[x]>0])
|
2023-05-07 22:07:28 +00:00
|
|
|
logger.debug(f"Current VRAM/RAM usage: {vram}/{ram}; locked_models/loaded_models = {locked_models}/{loaded_models}")
|
|
|
|
|
|
|
|
def _make_cache_room(self, key, model_type):
|
|
|
|
# calculate how much memory this model will require
|
|
|
|
multiplier = 2 if self.precision==torch.float32 else 1
|
|
|
|
bytes_needed = int(self.model_sizes.get(key,0) or SIZE_GUESSTIMATE[model_type]*GIG*multiplier)
|
|
|
|
maximum_size = self.max_cache_size * GIG # stored in GB, convert to bytes
|
|
|
|
current_size = self.current_cache_size
|
|
|
|
|
|
|
|
adjective = 'guesstimated' if key not in self.model_sizes else 'known from previous load'
|
|
|
|
logger.debug(f'{(bytes_needed/GIG):.2f} GB needed to load this model ({adjective})')
|
|
|
|
while current_size+bytes_needed > maximum_size:
|
2023-04-28 04:41:52 +00:00
|
|
|
if least_recently_used_key := self.stack.pop(0):
|
2023-05-07 22:07:28 +00:00
|
|
|
model_size = self.model_sizes.get(least_recently_used_key,0)
|
|
|
|
logger.debug(f'Max cache size exceeded: cache_size={(current_size/GIG):.2f} GB, need an additional {(bytes_needed/GIG):.2f} GB')
|
|
|
|
logger.debug(f'Unloading model {least_recently_used_key} to free {(model_size/GIG):.2f} GB')
|
|
|
|
self.uncache_model(least_recently_used_key)
|
|
|
|
current_size -= model_size
|
|
|
|
self.current_cache_size = current_size
|
2023-04-28 04:41:52 +00:00
|
|
|
gc.collect()
|
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
def _offload_unlocked_models(self):
|
|
|
|
to_offload = set()
|
|
|
|
for key in self.loaded_models:
|
|
|
|
if key not in self.locked_models or self.locked_models[key] == 0:
|
|
|
|
self.logger.debug(f'Offloading {key} from {self.execution_device} into {self.storage_device}')
|
|
|
|
to_offload.add(key)
|
|
|
|
for key in to_offload:
|
|
|
|
self.models[key].to(self.storage_device)
|
|
|
|
self.loaded_models.remove(key)
|
2023-04-28 04:41:52 +00:00
|
|
|
|
|
|
|
def _load_model_from_storage(
|
|
|
|
self,
|
|
|
|
repo_id_or_path: Union[str,Path],
|
|
|
|
subfolder: Path=None,
|
|
|
|
revision: str=None,
|
2023-05-02 02:57:30 +00:00
|
|
|
model_class: ModelClass=StableDiffusionGeneratorPipeline,
|
2023-05-02 20:52:27 +00:00
|
|
|
legacy_info: LegacyInfo=None,
|
2023-05-02 02:57:30 +00:00
|
|
|
)->ModelClass:
|
2023-04-28 04:41:52 +00:00
|
|
|
'''
|
|
|
|
Load and return a HuggingFace model.
|
|
|
|
:param repo_id_or_path: either the HuggingFace repo_id or a Path to a local model
|
|
|
|
:param subfolder: name of a subfolder in which the model can be found, e.g. "vae"
|
|
|
|
:param revision: model revision
|
2023-05-02 20:52:27 +00:00
|
|
|
:param model_class: class of model to return, defaults to StableDiffusionGeneratorPIpeline
|
|
|
|
:param legacy_info: a LegacyInfo object containing additional info needed to load a legacy ckpt
|
2023-04-28 04:41:52 +00:00
|
|
|
'''
|
2023-05-06 19:58:44 +00:00
|
|
|
# silence transformer and diffuser warnings
|
|
|
|
with SilenceWarnings():
|
|
|
|
if self.is_legacy_ckpt(repo_id_or_path):
|
|
|
|
model = self._load_ckpt_from_storage(repo_id_or_path, legacy_info)
|
|
|
|
else:
|
|
|
|
model = self._load_diffusers_from_storage(
|
|
|
|
repo_id_or_path,
|
|
|
|
subfolder,
|
|
|
|
revision,
|
|
|
|
model_class,
|
|
|
|
)
|
2023-05-07 22:07:28 +00:00
|
|
|
if self.sequential_offload and isinstance(model,StableDiffusionGeneratorPipeline):
|
|
|
|
model.enable_offload_submodels(self.execution_device)
|
2023-04-28 04:41:52 +00:00
|
|
|
return model
|
|
|
|
|
2023-05-02 20:52:27 +00:00
|
|
|
def _load_diffusers_from_storage(
|
|
|
|
self,
|
|
|
|
repo_id_or_path: Union[str,Path],
|
|
|
|
subfolder: Path=None,
|
|
|
|
revision: str=None,
|
|
|
|
model_class: ModelClass=StableDiffusionGeneratorPipeline,
|
|
|
|
)->ModelClass:
|
|
|
|
'''
|
|
|
|
Load and return a HuggingFace model using from_pretrained().
|
|
|
|
:param repo_id_or_path: either the HuggingFace repo_id or a Path to a local model
|
|
|
|
:param subfolder: name of a subfolder in which the model can be found, e.g. "vae"
|
|
|
|
:param revision: model revision
|
|
|
|
:param model_class: class of model to return, defaults to StableDiffusionGeneratorPIpeline
|
|
|
|
'''
|
2023-05-05 23:32:28 +00:00
|
|
|
revisions = [revision] if revision \
|
|
|
|
else ['fp16','main'] if self.precision==torch.float16 \
|
|
|
|
else ['main']
|
2023-05-06 04:44:12 +00:00
|
|
|
extra_args = {'torch_dtype': self.precision,
|
|
|
|
'safety_checker': None}\
|
|
|
|
if model_class in DiffusionClasses\
|
|
|
|
else {}
|
|
|
|
|
2023-05-06 19:58:44 +00:00
|
|
|
for rev in revisions:
|
|
|
|
try:
|
|
|
|
model = model_class.from_pretrained(
|
|
|
|
repo_id_or_path,
|
|
|
|
revision=rev,
|
|
|
|
subfolder=subfolder or '.',
|
|
|
|
cache_dir=global_cache_dir('hub'),
|
|
|
|
**extra_args,
|
|
|
|
)
|
|
|
|
self.logger.debug(f'Found revision {rev}')
|
|
|
|
break
|
|
|
|
except OSError:
|
|
|
|
pass
|
2023-05-05 23:32:28 +00:00
|
|
|
return model
|
2023-05-02 20:52:27 +00:00
|
|
|
|
|
|
|
def _load_ckpt_from_storage(self,
|
|
|
|
ckpt_path: Union[str,Path],
|
|
|
|
legacy_info:LegacyInfo)->StableDiffusionGeneratorPipeline:
|
|
|
|
'''
|
|
|
|
Load a legacy checkpoint, convert it, and return a StableDiffusionGeneratorPipeline.
|
|
|
|
:param ckpt_path: string or Path pointing to the weights file (.ckpt or .safetensors)
|
|
|
|
:param legacy_info: LegacyInfo object containing paths to legacy config file and alternate vae if required
|
|
|
|
'''
|
2023-05-06 19:58:44 +00:00
|
|
|
if legacy_info is None or legacy_info.config_file is None:
|
|
|
|
if Path(ckpt_path).suffix == '.safetensors':
|
|
|
|
return safetensors.torch.load_file(ckpt_path)
|
|
|
|
else:
|
|
|
|
return torch.load(ckpt_path)
|
|
|
|
else:
|
|
|
|
# deferred loading to avoid circular import errors
|
|
|
|
from .convert_ckpt_to_diffusers import load_pipeline_from_original_stable_diffusion_ckpt
|
2023-05-05 23:32:28 +00:00
|
|
|
pipeline = load_pipeline_from_original_stable_diffusion_ckpt(
|
|
|
|
checkpoint_path=ckpt_path,
|
|
|
|
original_config_file=legacy_info.config_file,
|
2023-05-06 19:58:44 +00:00
|
|
|
vae_path=legacy_info.vae_file,
|
2023-05-05 23:32:28 +00:00
|
|
|
return_generator_pipeline=True,
|
|
|
|
precision=self.precision,
|
|
|
|
)
|
2023-05-06 19:58:44 +00:00
|
|
|
return pipeline
|
2023-05-02 20:52:27 +00:00
|
|
|
|
|
|
|
def _legacy_model_hash(self, checkpoint_path: Union[str,Path])->str:
|
|
|
|
sha = hashlib.sha256()
|
|
|
|
path = Path(checkpoint_path)
|
2023-05-06 19:58:44 +00:00
|
|
|
assert path.is_file(),f"File {checkpoint_path} not found"
|
2023-05-02 20:52:27 +00:00
|
|
|
|
|
|
|
hashpath = path.parent / f"{path.name}.sha256"
|
|
|
|
if hashpath.exists() and path.stat().st_mtime <= hashpath.stat().st_mtime:
|
|
|
|
with open(hashpath) as f:
|
|
|
|
hash = f.read()
|
|
|
|
return hash
|
|
|
|
|
2023-05-05 03:15:32 +00:00
|
|
|
logger.debug(f'computing hash of model {path.name}')
|
2023-05-02 20:52:27 +00:00
|
|
|
with open(path, "rb") as f:
|
|
|
|
while chunk := f.read(self.sha_chunksize):
|
|
|
|
sha.update(chunk)
|
|
|
|
hash = sha.hexdigest()
|
|
|
|
|
|
|
|
with open(hashpath, "w") as f:
|
|
|
|
f.write(hash)
|
|
|
|
return hash
|
|
|
|
|
|
|
|
def _local_model_hash(self, model_path: Union[str,Path])->str:
|
|
|
|
sha = hashlib.sha256()
|
|
|
|
path = Path(model_path)
|
|
|
|
|
|
|
|
hashpath = path / "checksum.sha256"
|
|
|
|
if hashpath.exists() and path.stat().st_mtime <= hashpath.stat().st_mtime:
|
|
|
|
with open(hashpath) as f:
|
|
|
|
hash = f.read()
|
|
|
|
return hash
|
|
|
|
|
2023-05-05 03:15:32 +00:00
|
|
|
logger.debug(f'computing hash of model {path.name}')
|
2023-05-02 20:52:27 +00:00
|
|
|
for file in list(path.rglob("*.ckpt")) \
|
|
|
|
+ list(path.rglob("*.safetensors")) \
|
|
|
|
+ list(path.rglob("*.pth")):
|
|
|
|
with open(file, "rb") as f:
|
|
|
|
while chunk := f.read(self.sha_chunksize):
|
|
|
|
sha.update(chunk)
|
|
|
|
hash = sha.hexdigest()
|
|
|
|
with open(hashpath, "w") as f:
|
|
|
|
f.write(hash)
|
|
|
|
return hash
|
|
|
|
|
|
|
|
def _hf_commit_hash(self, repo_id: str, revision: str='main')->str:
|
|
|
|
api = HfApi()
|
|
|
|
info = api.list_repo_refs(
|
|
|
|
repo_id=repo_id,
|
|
|
|
repo_type='model',
|
|
|
|
)
|
|
|
|
desired_revisions = [branch for branch in info.branches if branch.name==revision]
|
|
|
|
if not desired_revisions:
|
|
|
|
raise KeyError(f"Revision '{revision}' not found in {repo_id}")
|
|
|
|
return desired_revisions[0].target_commit
|
|
|
|
|
2023-05-05 23:32:28 +00:00
|
|
|
|
2023-04-28 04:41:52 +00:00
|
|
|
class SilenceWarnings(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.transformers_verbosity = transformers_logging.get_verbosity()
|
|
|
|
self.diffusers_verbosity = diffusers_logging.get_verbosity()
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
transformers_logging.set_verbosity_error()
|
|
|
|
diffusers_logging.set_verbosity_error()
|
|
|
|
warnings.simplefilter('ignore')
|
|
|
|
|
|
|
|
def __exit__(self,type,value,traceback):
|
|
|
|
transformers_logging.set_verbosity(self.transformers_verbosity)
|
|
|
|
diffusers_logging.set_verbosity(self.diffusers_verbosity)
|
|
|
|
warnings.simplefilter('default')
|
2023-05-07 22:07:28 +00:00
|
|
|
|
|
|
|
class MemoryUsage(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.vms = None
|
|
|
|
self.mem_used = 0
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.vms = Process().memory_info().vms
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
self.mem_used = Process().memory_info().vms - self.vms
|