cddb07a176
docs / deploy (push) Has been cancelled
docs / changes (push) Has been cancelled
docs / check-and-build (push) Has been cancelled
build container image / cpu (push) Has been cancelled
build container image / cuda (push) Has been cancelled
build container image / rocm (push) Has been cancelled
frontend checks / frontend-checks (push) Has been cancelled
frontend tests / frontend-tests (push) Has been cancelled
lfs checks / lfs-check (push) Has been cancelled
python checks / python-checks (push) Has been cancelled
python tests / py3.12: macos-default (push) Has been cancelled
python tests / py3.11: windows-cpu (push) Has been cancelled
python tests / py3.12: windows-cpu (push) Has been cancelled
python tests / py3.11: linux-cpu (push) Has been cancelled
typegen checks / typegen-checks (push) Has been cancelled
uv lock checks / uv-lock-checks (push) Has been cancelled
openapi checks / openapi-checks (push) Has been cancelled
python tests / py3.11: macos-default (push) Has been cancelled
python tests / py3.12: linux-cpu (push) Has been cancelled
36 lines
952 B
Python
36 lines
952 B
Python
from __future__ import annotations
|
|
|
|
from contextlib import contextmanager
|
|
from typing import TYPE_CHECKING
|
|
|
|
from diffusers import UNet2DConditionModel
|
|
|
|
from invokeai.backend.stable_diffusion.extensions.base import ExtensionBase
|
|
|
|
if TYPE_CHECKING:
|
|
from invokeai.app.shared.models import FreeUConfig
|
|
from invokeai.backend.util.original_weights_storage import OriginalWeightsStorage
|
|
|
|
|
|
class FreeUExt(ExtensionBase):
|
|
def __init__(
|
|
self,
|
|
freeu_config: FreeUConfig,
|
|
):
|
|
super().__init__()
|
|
self._freeu_config = freeu_config
|
|
|
|
@contextmanager
|
|
def patch_unet(self, unet: UNet2DConditionModel, original_weights: OriginalWeightsStorage):
|
|
unet.enable_freeu(
|
|
b1=self._freeu_config.b1,
|
|
b2=self._freeu_config.b2,
|
|
s1=self._freeu_config.s1,
|
|
s2=self._freeu_config.s2,
|
|
)
|
|
|
|
try:
|
|
yield
|
|
finally:
|
|
unet.disable_freeu()
|