Tidy SilenceWarnings context manager:

- Fix type errors
- Enable SilenceWarnings to be used as both a context manager and a decorator
- Remove duplicate implementation
- Check the initial verbosity on __enter__() rather than __init__()
This commit is contained in:
Ryan Dick 2024-06-06 10:28:00 -04:00
parent d13aafb514
commit 8e47e005a7
3 changed files with 21 additions and 35 deletions

View File

@ -10,7 +10,7 @@ from picklescan.scanner import scan_file_path
import invokeai.backend.util.logging as logger import invokeai.backend.util.logging as logger
from invokeai.app.util.misc import uuid_string from invokeai.app.util.misc import uuid_string
from invokeai.backend.model_hash.model_hash import HASHING_ALGORITHMS, ModelHash from invokeai.backend.model_hash.model_hash import HASHING_ALGORITHMS, ModelHash
from invokeai.backend.util.util import SilenceWarnings from invokeai.backend.util.silence_warnings import SilenceWarnings
from .config import ( from .config import (
AnyModelConfig, AnyModelConfig,

View File

@ -1,29 +1,36 @@
"""Context class to silence transformers and diffusers warnings."""
import warnings import warnings
from typing import Any from contextlib import ContextDecorator
from diffusers import logging as diffusers_logging from diffusers.utils import logging as diffusers_logging
from transformers import logging as transformers_logging from transformers import logging as transformers_logging
class SilenceWarnings(object): # Inherit from ContextDecorator to allow using SilenceWarnings as both a context manager and a decorator.
"""Use in context to temporarily turn off warnings from transformers & diffusers modules. class SilenceWarnings(ContextDecorator):
"""A context manager that disables warnings from transformers & diffusers modules while active.
As context manager:
```
with SilenceWarnings(): with SilenceWarnings():
# do something # do something
```
As decorator:
```
@SilenceWarnings()
def some_function():
# do something
```
""" """
def __init__(self) -> None:
self.transformers_verbosity = transformers_logging.get_verbosity()
self.diffusers_verbosity = diffusers_logging.get_verbosity()
def __enter__(self) -> None: def __enter__(self) -> None:
self._transformers_verbosity = transformers_logging.get_verbosity()
self._diffusers_verbosity = diffusers_logging.get_verbosity()
transformers_logging.set_verbosity_error() transformers_logging.set_verbosity_error()
diffusers_logging.set_verbosity_error() diffusers_logging.set_verbosity_error()
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
def __exit__(self, *args: Any) -> None: def __exit__(self, *args) -> None:
transformers_logging.set_verbosity(self.transformers_verbosity) transformers_logging.set_verbosity(self._transformers_verbosity)
diffusers_logging.set_verbosity(self.diffusers_verbosity) diffusers_logging.set_verbosity(self._diffusers_verbosity)
warnings.simplefilter("default") warnings.simplefilter("default")

View File

@ -3,12 +3,9 @@ import io
import os import os
import re import re
import unicodedata import unicodedata
import warnings
from pathlib import Path from pathlib import Path
from diffusers import logging as diffusers_logging
from PIL import Image from PIL import Image
from transformers import logging as transformers_logging
# actual size of a gig # actual size of a gig
GIG = 1073741824 GIG = 1073741824
@ -80,21 +77,3 @@ class Chdir(object):
def __exit__(self, *args): def __exit__(self, *args):
os.chdir(self.original) os.chdir(self.original)
class SilenceWarnings(object):
"""Context manager to temporarily lower verbosity of diffusers & transformers warning messages."""
def __enter__(self):
"""Set verbosity to error."""
self.transformers_verbosity = transformers_logging.get_verbosity()
self.diffusers_verbosity = diffusers_logging.get_verbosity()
transformers_logging.set_verbosity_error()
diffusers_logging.set_verbosity_error()
warnings.simplefilter("ignore")
def __exit__(self, type, value, traceback):
"""Restore logger verbosity to state before context was entered."""
transformers_logging.set_verbosity(self.transformers_verbosity)
diffusers_logging.set_verbosity(self.diffusers_verbosity)
warnings.simplefilter("default")