Add type annotations to ModelPatcher fields

This commit is contained in:
huchenlei 2024-05-25 17:56:37 -04:00
parent e7df27ef13
commit 05de350ab3

View File

@ -1,8 +1,10 @@
from __future__ import annotations
import torch import torch
import copy import copy
import inspect import inspect
import logging import logging
import uuid import uuid
from typing import Generic, Optional, TypeVar
import comfy.utils import comfy.utils
import comfy.model_management import comfy.model_management
@ -41,27 +43,37 @@ def set_model_options_patch_replace(model_options, patch, name, block_name, numb
model_options["transformer_options"] = to model_options["transformer_options"] = to
return model_options return model_options
class ModelPatcher:
def __init__(self, model, load_device, offload_device, size=0, current_device=None, weight_inplace_update=False): ModelType = TypeVar("ModelType", torch.nn.Module)
self.size = size
self.model = model
class ModelPatcher(Generic[ModelType]):
def __init__(
self,
model: ModelType,
load_device: torch.device,
offload_device: torch.device,
size: int = 0,
current_device: Optional[torch.device] = None,
weight_inplace_update: bool = False
):
self.size: int = size
self.model: ModelType = model
self.patches = {} self.patches = {}
self.backup = {} self.backup = {}
self.object_patches = {} self.object_patches = {}
self.object_patches_backup = {} self.object_patches_backup = {}
self.model_options = {"transformer_options":{}} self.model_options = {"transformer_options":{}}
self.model_size() self.model_size()
self.load_device = load_device self.load_device: torch.device = load_device
self.offload_device = offload_device self.offload_device: torch.device = offload_device
if current_device is None: self.current_device: torch.device = (
self.current_device = self.offload_device current_device if current_device is not None else offload_device
else: )
self.current_device = current_device self.weight_inplace_update: bool = weight_inplace_update
self.model_lowvram: bool = False
self.weight_inplace_update = weight_inplace_update self.lowvram_patch_counter: int = 0
self.model_lowvram = False self.patches_uuid: uuid.UUID = uuid.uuid4()
self.lowvram_patch_counter = 0
self.patches_uuid = uuid.uuid4()
def model_size(self): def model_size(self):
if self.size > 0: if self.size > 0: