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.

8.5 KB
Raw Blame
"""Unit tests for SDNQTensor class."""

import torch

from invokeai.backend.quantization.sdnq.sdnq_tensor import SDNQTensor
from invokeai.backend.quantization.sdnq.utils import SDNQQuantizationType


class TestSDNQTensor:
    """Tests for SDNQTensor dequantization and operations."""

    def test_symmetric_dequantization(self):
        """Test symmetric dequantization: result = weight * scale."""
        weight = torch.tensor([[1, 2], [3, 4]], dtype=torch.int8)
        scale = torch.tensor([0.1], dtype=torch.float32)

        tensor = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([2, 2]),
            compute_dtype=torch.float32,
            scale=scale,
        )

        dequantized = tensor.get_dequantized_tensor()
        expected = torch.tensor([[0.1, 0.2], [0.3, 0.4]], dtype=torch.float32)
        assert torch.allclose(dequantized, expected, atol=1e-6)

    def test_asymmetric_dequantization(self):
        """Test asymmetric dequantization: result = zero_point + weight * scale.

        Matches the SDNQ upstream convention (Disty0/sdnq: torch.addcmul(zero_point, weight, scale)),
        where zero_point is a post-scale bias rather than a pre-scale integer offset.
        """
        weight = torch.tensor([[10, 20], [30, 40]], dtype=torch.int8)
        scale = torch.tensor([0.1], dtype=torch.float32)
        zero_point = torch.tensor([5], dtype=torch.float32)

        tensor = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_ASYM,
            tensor_shape=torch.Size([2, 2]),
            compute_dtype=torch.float32,
            scale=scale,
            zero_point=zero_point,
        )

        dequantized = tensor.get_dequantized_tensor()
        # zero_point + weight * scale = 5 + [[10, 20], [30, 40]] * 0.1
        expected = torch.tensor([[6.0, 7.0], [8.0, 9.0]], dtype=torch.float32)
        assert torch.allclose(dequantized, expected, atol=1e-6)

    def test_svd_correction(self):
        """Test SVD correction: result = dequant + svd_up @ svd_down."""
        weight = torch.tensor([[0, 0], [0, 0]], dtype=torch.int8)
        scale = torch.tensor([1.0], dtype=torch.float32)
        svd_up = torch.tensor([[1.0], [2.0]], dtype=torch.float32)
        svd_down = torch.tensor([[1.0, 1.0]], dtype=torch.float32)

        tensor = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([2, 2]),
            compute_dtype=torch.float32,
            scale=scale,
            svd_up=svd_up,
            svd_down=svd_down,
        )

        assert tensor.has_svd
        dequantized = tensor.get_dequantized_tensor()
        # SVD correction: [[1], [2]] @ [[1, 1]] = [[1, 1], [2, 2]]
        expected = torch.tensor([[1.0, 1.0], [2.0, 2.0]], dtype=torch.float32)
        assert torch.allclose(dequantized, expected, atol=1e-6)

    def test_shape_properties(self):
        """Test tensor shape properties."""
        weight = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int8)
        scale = torch.tensor([0.1], dtype=torch.float32)

        tensor = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([2, 3]),
            compute_dtype=torch.float32,
            scale=scale,
        )

        assert tensor.shape == torch.Size([2, 3])
        assert tensor.size() == torch.Size([2, 3])
        assert tensor.size(0) == 2
        assert tensor.size(1) == 3
        assert tensor.quantized_shape == torch.Size([2, 3])

    def test_is_asymmetric_property(self):
        """Test is_asymmetric property."""
        weight = torch.tensor([[1]], dtype=torch.int8)
        scale = torch.tensor([0.1], dtype=torch.float32)

        # Symmetric (no zero_point)
        tensor_sym = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([1, 1]),
            compute_dtype=torch.float32,
            scale=scale,
        )
        assert not tensor_sym.is_asymmetric

        # Asymmetric (has zero_point)
        tensor_asym = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_ASYM,
            tensor_shape=torch.Size([1, 1]),
            compute_dtype=torch.float32,
            scale=scale,
            zero_point=torch.tensor([0.0]),
        )
        assert tensor_asym.is_asymmetric

    def test_has_svd_property(self):
        """Test has_svd property."""
        weight = torch.tensor([[1]], dtype=torch.int8)
        scale = torch.tensor([0.1], dtype=torch.float32)

        # No SVD
        tensor_no_svd = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([1, 1]),
            compute_dtype=torch.float32,
            scale=scale,
        )
        assert not tensor_no_svd.has_svd

        # With SVD
        tensor_with_svd = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([1, 1]),
            compute_dtype=torch.float32,
            scale=scale,
            svd_up=torch.tensor([[1.0]]),
            svd_down=torch.tensor([[1.0]]),
        )
        assert tensor_with_svd.has_svd

    def test_repr(self):
        """Test tensor string representation."""
        weight = torch.tensor([[1, 2], [3, 4]], dtype=torch.int8)
        scale = torch.tensor([0.1], dtype=torch.float32)

        tensor = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([2, 2]),
            compute_dtype=torch.float32,
            scale=scale,
        )

        repr_str = repr(tensor)
        assert "SDNQTensor" in repr_str
        assert "int8_sym" in repr_str
        assert "has_svd=False" in repr_str

    def test_compute_dtype_bfloat16(self):
        """Test dequantization with bfloat16 compute dtype."""
        weight = torch.tensor([[1, 2], [3, 4]], dtype=torch.int8)
        scale = torch.tensor([0.5], dtype=torch.float32)

        tensor = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([2, 2]),
            compute_dtype=torch.bfloat16,
            scale=scale,
        )

        dequantized = tensor.get_dequantized_tensor()
        assert dequantized.dtype == torch.bfloat16

    def test_requires_grad_noop(self):
        """Test that requires_grad_ is a no-op for inference-only tensor."""
        weight = torch.tensor([[1]], dtype=torch.int8)
        scale = torch.tensor([0.1], dtype=torch.float32)

        tensor = SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size([1, 1]),
            compute_dtype=torch.float32,
            scale=scale,
        )

        # Should return self without error
        result = tensor.requires_grad_(True)
        assert result is tensor

    @staticmethod
    def _make_tensor(values: list[list[int]], scale_value: float) -> SDNQTensor:
        weight = torch.tensor(values, dtype=torch.int8)
        return SDNQTensor(
            data=weight,
            quantization_type=SDNQQuantizationType.INT8_SYM,
            tensor_shape=torch.Size(weight.shape),
            compute_dtype=torch.float32,
            scale=torch.tensor([scale_value], dtype=torch.float32),
        )

    def test_torch_cat_dequantizes_sequence_args(self):
        """torch.cat passes its tensors inside a list; the op must dequantize them instead of
        redispatching onto the SDNQTensors (which would recurse until RecursionError)."""
        a = self._make_tensor([[1, 2], [3, 4]], 0.1)
        b = self._make_tensor([[5, 6], [7, 8]], 0.2)

        result = torch.cat([a, b], dim=0)
        expected = torch.cat([a.get_dequantized_tensor(), b.get_dequantized_tensor()], dim=0)

        assert not isinstance(result, SDNQTensor)
        assert torch.allclose(result, expected, atol=1e-6)

    def test_torch_stack_dequantizes_sequence_args(self):
        """Same as cat, for torch.stack (also receives a list of tensors)."""
        a = self._make_tensor([[1, 2], [3, 4]], 0.1)
        b = self._make_tensor([[5, 6], [7, 8]], 0.2)

        result = torch.stack([a, b], dim=0)
        expected = torch.stack([a.get_dequantized_tensor(), b.get_dequantized_tensor()], dim=0)

        assert not isinstance(result, SDNQTensor)
        assert result.shape == torch.Size([2, 2, 2])
        assert torch.allclose(result, expected, atol=1e-6)