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.

18.4 KB
Raw Blame
"""SDNQTensor - A torch.Tensor subclass for SDNQ quantized weights with on-the-fly dequantization."""

import logging
from typing import Optional, overload

import torch

from invokeai.backend.quantization.sdnq.utils import (
    SDNQQuantizationType,
    apply_svd_correction,
    dequantize_asymmetric,
    dequantize_int5_per_group,
    dequantize_symmetric,
    dequantize_uint4_per_group,
)

logger = logging.getLogger(__name__)


def dequantize_and_run(func, args, kwargs):
    """Helper function for running math ops on SDNQTensor inputs.

    Dequantizes the inputs and runs the function.
    Also casts other floating point tensors to match the compute_dtype of SDNQTensors.

    Some ops (e.g. ``torch.cat`` / ``torch.stack``) pass their tensors inside a list or tuple rather
    than as top-level args, so both discovery and processing recurse into sequences. Without this a
    sequence arg is left untouched — its SDNQTensors are never dequantized and ``func`` redispatches
    onto them, recursing until ``RecursionError``.
    """
    compute_dtype = None
    target_device = None

    def scan(value) -> None:
        nonlocal compute_dtype, target_device
        if isinstance(value, (list, tuple)):
            for item in value:
                scan(item)
            return
        if compute_dtype is None and hasattr(value, "compute_dtype"):
            compute_dtype = value.compute_dtype
        if target_device is None and isinstance(value, torch.Tensor):
            target_device = value.device

    for a in args:
        scan(a)
        if compute_dtype is not None and target_device is not None:
            break

    if compute_dtype is None or target_device is None:
        for v in kwargs.values():
            scan(v)
            if compute_dtype is not None and target_device is not None:
                break

    def process_tensor(t):
        if isinstance(t, (list, tuple)):
            return type(t)(process_tensor(item) for item in t)
        if hasattr(t, "get_dequantized_tensor"):
            result = t.get_dequantized_tensor()
            if target_device is not None and result.device != target_device:
                result = result.to(target_device)
            return result
        elif isinstance(t, torch.Tensor) and compute_dtype is not None and t.is_floating_point():
            return t.to(compute_dtype)
        return t

    dequantized_args = [process_tensor(a) for a in args]
    dequantized_kwargs = {k: process_tensor(v) for k, v in kwargs.items()}
    return func(*dequantized_args, **dequantized_kwargs)


def apply_to_quantized_tensor(func, args, kwargs):
    """Apply function to quantized tensor and re-wrap result in SDNQTensor.

    Assumes that the first argument is an SDNQTensor.
    """
    sdnq_tensor = args[0]
    assert isinstance(sdnq_tensor, SDNQTensor)
    assert all(not isinstance(a, SDNQTensor) for a in args[1:])
    assert all(not isinstance(v, SDNQTensor) for v in kwargs.values())

    new_data = func(sdnq_tensor.quantized_data, *args[1:], **kwargs)

    if new_data.dtype != sdnq_tensor.quantized_data.dtype:
        raise ValueError("Operation changed the dtype of SDNQTensor unexpectedly.")

    # Realign the auxiliary payloads (scale / zero_point / svd) to the new data's device so the whole
    # wrapper lives on one device. `_to_copy` implements SDNQTensor.to(device); without this a
    # "GPU-resident" SDNQ parameter would keep its scale/zero_point/svd tensors in system RAM,
    # forcing a host->device copy of all of them (both SVD matrices included) on every dequantization
    # of every quantized layer, on every inference step. We only move the device and preserve each
    # tensor's own dtype (a dtype change to the packed data is already rejected above).
    target_device = new_data.device

    def _align(t: Optional[torch.Tensor]) -> Optional[torch.Tensor]:
        if isinstance(t, torch.Tensor) and t.device != target_device:
            return t.to(device=target_device)
        return t

    return SDNQTensor(
        data=new_data,
        quantization_type=sdnq_tensor._quantization_type,
        tensor_shape=sdnq_tensor.tensor_shape,
        compute_dtype=sdnq_tensor.compute_dtype,
        scale=_align(sdnq_tensor._scale),
        zero_point=_align(sdnq_tensor._zero_point),
        svd_up=_align(sdnq_tensor._svd_up),
        svd_down=_align(sdnq_tensor._svd_down),
        group_size=sdnq_tensor._group_size,
    )


SDNQ_TENSOR_OP_TABLE = {
    # Ops to run on the quantized tensor (keep quantized).
    torch.ops.aten.detach.default: apply_to_quantized_tensor,  # pyright: ignore
    torch.ops.aten._to_copy.default: apply_to_quantized_tensor,  # pyright: ignore
    torch.ops.aten.clone.default: apply_to_quantized_tensor,  # pyright: ignore
    # Ops to run on dequantized tensors.
    torch.ops.aten.t.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.linear.default: dequantize_and_run,  # pyright: ignore - needed for F.linear
    torch.ops.aten.addmm.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.mm.default: dequantize_and_run,  # pyright: ignore - matrix multiply
    torch.ops.aten.bmm.default: dequantize_and_run,  # pyright: ignore - batch matrix multiply
    torch.ops.aten.baddbmm.default: dequantize_and_run,  # pyright: ignore - batch add batch mm
    torch.ops.aten.matmul.default: dequantize_and_run,  # pyright: ignore - general matmul
    # Element-wise ops
    torch.ops.aten.mul.Tensor: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.mul.Scalar: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.add.Tensor: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.add.Scalar: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.sub.Tensor: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.sub.Scalar: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.div.Tensor: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.div.Scalar: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.neg.default: dequantize_and_run,  # pyright: ignore
    # Shape manipulation ops
    torch.ops.aten.slice.Tensor: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.view.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.reshape.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten._unsafe_view.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.expand.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.permute.default: dequantize_and_run,  # pyright: ignore - attention reshaping
    torch.ops.aten.transpose.int: dequantize_and_run,  # pyright: ignore - attention
    torch.ops.aten.contiguous.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.squeeze.dim: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.squeeze.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.unsqueeze.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.select.int: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.split.Tensor: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.split_with_sizes.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.chunk.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.cat.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.stack.default: dequantize_and_run,  # pyright: ignore
    # Attention ops
    torch.ops.aten.scaled_dot_product_attention.default: dequantize_and_run,  # pyright: ignore - SDPA
    torch.ops.aten._scaled_dot_product_flash_attention.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten._scaled_dot_product_efficient_attention.default: dequantize_and_run,  # pyright: ignore
    # Normalization and activation ops
    torch.ops.aten.layer_norm.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.group_norm.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.native_layer_norm.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.native_group_norm.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.silu.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.gelu.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.relu.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.softmax.int: dequantize_and_run,  # pyright: ignore
    torch.ops.aten._softmax.default: dequantize_and_run,  # pyright: ignore
    # Reduction ops
    torch.ops.aten.mean.dim: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.sum.dim_IntList: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.var.correction: dequantize_and_run,  # pyright: ignore - for RMSNorm
    torch.ops.aten.std.correction: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.pow.Tensor_Scalar: dequantize_and_run,  # pyright: ignore - for RMSNorm
    torch.ops.aten.rsqrt.default: dequantize_and_run,  # pyright: ignore - for RMSNorm
    torch.ops.aten.sqrt.default: dequantize_and_run,  # pyright: ignore
    # Misc ops
    torch.ops.aten.allclose.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.index_put_.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.embedding.default: dequantize_and_run,  # pyright: ignore
    torch.ops.aten.copy_.default: dequantize_and_run,  # pyright: ignore
    # Conv ops - needed for VAE
    torch.ops.aten.convolution.default: dequantize_and_run,  # pyright: ignore - Conv2d uses this
    torch.ops.aten.conv2d.default: dequantize_and_run,  # pyright: ignore
}


class SDNQTensor(torch.Tensor):
    """A torch.Tensor subclass holding SDNQ quantized weights.

    Provides on-the-fly dequantization when used in operations.
    Supports symmetric/asymmetric quantization and optional SVD correction.
    """

    @staticmethod
    def __new__(
        cls,
        data: torch.Tensor,
        quantization_type: SDNQQuantizationType,
        tensor_shape: torch.Size,
        compute_dtype: torch.dtype,
        scale: torch.Tensor,
        zero_point: Optional[torch.Tensor] = None,
        svd_up: Optional[torch.Tensor] = None,
        svd_down: Optional[torch.Tensor] = None,
        group_size: Optional[int] = None,
    ):
        # Use tensor_shape (the dequantized shape) for the wrapper, not data.shape (packed shape)
        # This ensures PyTorch's load_state_dict sees the correct shape for assignment
        # Compute strides for row-major (C-contiguous) layout
        strides = []
        stride = 1
        for dim in reversed(tensor_shape):
            strides.append(stride)
            stride *= dim
        strides = tuple(reversed(strides))

        return torch.Tensor._make_wrapper_subclass(  # pyright: ignore
            cls,
            tensor_shape,
            dtype=data.dtype,
            layout=data.layout,
            device=data.device,
            strides=strides,
            storage_offset=0,
        )

    def __init__(
        self,
        data: torch.Tensor,
        quantization_type: SDNQQuantizationType,
        tensor_shape: torch.Size,
        compute_dtype: torch.dtype,
        scale: torch.Tensor,
        zero_point: Optional[torch.Tensor] = None,
        svd_up: Optional[torch.Tensor] = None,
        svd_down: Optional[torch.Tensor] = None,
        group_size: Optional[int] = None,
    ):
        self.quantized_data = data
        self._quantization_type = quantization_type
        self.tensor_shape = tensor_shape
        self.compute_dtype = compute_dtype
        self._scale = scale
        self._zero_point = zero_point
        self._svd_up = svd_up
        self._svd_down = svd_down
        self._group_size = group_size

    def __repr__(self, *, tensor_contents=None):
        return (
            f"SDNQTensor(type={self._quantization_type.value}, "
            f"dequantized_shape=({self.tensor_shape}), "
            f"has_svd={self._svd_up is not None})"
        )

    @overload
    def size(self, dim: None = None) -> torch.Size: ...

    @overload
    def size(self, dim: int) -> int: ...

    def size(self, dim: int | None = None):
        """Return the size of the tensor after dequantization."""
        if dim is not None:
            return self.tensor_shape[dim]
        return self.tensor_shape

    @property
    def shape(self) -> torch.Size:  # pyright: ignore[reportIncompatibleVariableOverride]
        """The shape of the tensor after dequantization."""
        return self.size()

    @property
    def quantized_shape(self) -> torch.Size:
        """The shape of the quantized tensor."""
        return self.quantized_data.shape

    @property
    def is_asymmetric(self) -> bool:
        """Whether this tensor uses asymmetric quantization."""
        return self._zero_point is not None

    @property
    def has_svd(self) -> bool:
        """Whether this tensor has SVD correction components."""
        return self._svd_up is not None and self._svd_down is not None

    def storage_tensors(self) -> tuple[torch.Tensor, ...]:
        """Packed data and auxiliary tensors retained by this wrapper."""
        return tuple(
            t
            for t in (self.quantized_data, self._scale, self._zero_point, self._svd_up, self._svd_down)
            if isinstance(t, torch.Tensor)
        )

    def sdnq_storage_nbytes(self) -> int:
        """Actual in-memory storage of this quantized tensor, in bytes.

        Generic size accounting (calc_tensor_size) uses the wrapper's advertised (dequantized) shape
        together with the packed uint8 dtype: for uint4 that double-counts the weight (one byte per
        unpacked element instead of one per two), for int5 it counts one byte per element instead of
        five per eight, and the scale / zero_point / svd payloads are omitted entirely. This returns
        the real storage — the packed data plus every auxiliary tensor — so cache eviction and
        partial-load budgets operate on true sizes.
        """
        return sum(t.nelement() * t.element_size() for t in self.storage_tensors())

    def requires_grad_(self, mode: bool = True) -> torch.Tensor:
        """SDNQTensor is only for inference, not training. This is a no-op."""
        return self

    # Track which tensors we've logged (to avoid spam)
    _logged_tensors: set = set()

    def get_dequantized_tensor(self) -> torch.Tensor:
        """Return the dequantized tensor.

        Returns:
            Dequantized tensor with compute_dtype.
        """
        # Debug logging for the first few tensors. Gated on the log level as well as the count: the
        # `min()`/`max()` calls below are device syncs, and this runs on the hot dequantize path.
        # Nothing here goes to stdout — that would bypass the app's log level, format and handlers.
        tensor_id = id(self)
        should_log = (
            tensor_id not in SDNQTensor._logged_tensors
            and len(SDNQTensor._logged_tensors) < 5
            and logger.isEnabledFor(logging.DEBUG)
        )
        if should_log:
            SDNQTensor._logged_tensors.add(tensor_id)
            logger.debug(
                f"SDNQ dequantize: type={self._quantization_type.value}, "
                f"weight_shape={self.quantized_data.shape}, weight_dtype={self.quantized_data.dtype}, "
                f"scale_shape={self._scale.shape}, scale_range=[{self._scale.min():.6f}, {self._scale.max():.6f}], "
                f"zp={self._zero_point is not None}, svd={self.has_svd}, group_size={self._group_size}"
            )
            if self._zero_point is not None:
                logger.debug(
                    f"  zero_point_shape={self._zero_point.shape}, "
                    f"zp_range=[{self._zero_point.min():.6f}, {self._zero_point.max():.6f}]"
                )

        # Perform dequantization based on quantization type
        if self._quantization_type == SDNQQuantizationType.UINT4_ASYM:
            # uint4 with per-group quantization
            assert self._zero_point is not None
            assert self._group_size is not None
            dequantized = dequantize_uint4_per_group(
                self.quantized_data,
                self._scale,
                self._zero_point,
                self.tensor_shape,
                self._group_size,
                dtype=self.compute_dtype,
            )
        elif self._quantization_type == SDNQQuantizationType.INT5_ASYM:
            # Signed 5-bit with per-group quantization (8 values per 5 bytes). zero_point may
            # be absent for scale-only 5-bit tensors produced by SDNQ's dynamic-mixed pass.
            assert self._group_size is not None
            dequantized = dequantize_int5_per_group(
                self.quantized_data,
                self._scale,
                self._zero_point,
                self.tensor_shape,
                self._group_size,
                dtype=self.compute_dtype,
            )
        elif self.is_asymmetric:
            assert self._zero_point is not None
            dequantized = dequantize_asymmetric(
                self.quantized_data,
                self._scale,
                self._zero_point,
                dtype=self.compute_dtype,
            )
        else:
            dequantized = dequantize_symmetric(
                self.quantized_data,
                self._scale,
                dtype=self.compute_dtype,
            )

        # Apply SVD correction if present
        if self.has_svd:
            dequantized = apply_svd_correction(
                dequantized,
                self._svd_up,
                self._svd_down,
                dtype=self.compute_dtype,
            )

        # Reshape to original tensor shape if needed
        if dequantized.shape != self.tensor_shape:
            dequantized = dequantized.view(self.tensor_shape)

        result = dequantized.to(self.compute_dtype)

        # Log output range for debugging
        if should_log:
            logger.info(f"  -> output_shape={result.shape}, output_range=[{result.min():.6f}, {result.max():.6f}]")

        return result

    # Track unknown ops to avoid spamming warnings
    _warned_ops: set = set()

    @classmethod
    def __torch_dispatch__(cls, func, types, args, kwargs):
        if func in SDNQ_TENSOR_OP_TABLE:
            return SDNQ_TENSOR_OP_TABLE[func](func, args, kwargs)

        # Fallback: dequantize and run for unknown operations
        # This ensures computation works, but may indicate missing ops
        if func not in cls._warned_ops:
            cls._warned_ops.add(func)
            import logging

            logging.getLogger(__name__).warning(
                f"SDNQTensor: unknown op {func}, dequantizing (add to op table for efficiency)"
            )

        return dequantize_and_run(func, args, kwargs)