Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

901 B
Raw Blame
import torch

from invokeai.backend.quantization.bnb_import import record_bnb_import_failure

try:
    from bitsandbytes.nn.modules import Params4bit

    bnb_available: bool = True
except (ImportError, OSError, RuntimeError) as e:  # bnb may import and then fail loading its native library
    record_bnb_import_failure(e)
    bnb_available: bool = False


def get_param_shape(param: torch.Tensor) -> torch.Size:
    """A helper function to get the shape of a parameter that handles `bitsandbytes.nn.Params4Bit` correctly."""
    # Accessing the `.shape` attribute of `bitsandbytes.nn.Params4Bit` will return an incorrect result. Instead, we must
    # access the `.quant_state.shape` attribute.
    if bnb_available and type(param) is Params4bit:  # type: ignore
        quant_state = param.quant_state
        if quant_state is not None:
            return quant_state.shape
    return param.shape