|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +from diffusers import NunchakuLiteQuantizationConfig |
| 7 | +from diffusers.quantizers.nunchaku import NunchakuLiteQuantizer |
| 8 | + |
| 9 | + |
| 10 | +def _compact_config(precision="nvfp4"): |
| 11 | + return { |
| 12 | + "svdq_w4a4": { |
| 13 | + "precision": precision, |
| 14 | + "group_size": 16 if precision == "nvfp4" else 64, |
| 15 | + "rank": 4, |
| 16 | + "targets": ["linear1"], |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | +class NunchakuLiteEnvironmentTests(unittest.TestCase): |
| 22 | + def test_environment_rejects_hopper_cuda(self): |
| 23 | + quantization_config = NunchakuLiteQuantizationConfig(compute_dtype=torch.bfloat16, **_compact_config("int4")) |
| 24 | + quantizer = NunchakuLiteQuantizer(quantization_config) |
| 25 | + |
| 26 | + with ( |
| 27 | + patch("diffusers.quantizers.nunchaku.nunchaku_quantizer.is_kernels_available", return_value=True), |
| 28 | + patch("torch.cuda.is_available", return_value=True), |
| 29 | + patch("torch.cuda.get_device_capability", return_value=(9, 0)), |
| 30 | + ): |
| 31 | + with self.assertRaisesRegex(ValueError, "not supported on Hopper NVIDIA GPUs"): |
| 32 | + quantizer.validate_environment() |
| 33 | + |
| 34 | + def test_nvfp4_environment_requires_blackwell_cuda(self): |
| 35 | + quantization_config = NunchakuLiteQuantizationConfig(compute_dtype=torch.bfloat16, **_compact_config("nvfp4")) |
| 36 | + quantizer = NunchakuLiteQuantizer(quantization_config) |
| 37 | + |
| 38 | + with ( |
| 39 | + patch("diffusers.quantizers.nunchaku.nunchaku_quantizer.is_kernels_available", return_value=True), |
| 40 | + patch("torch.cuda.is_available", return_value=True), |
| 41 | + patch("torch.cuda.get_device_capability", return_value=(8, 9)), |
| 42 | + ): |
| 43 | + with self.assertRaisesRegex(ValueError, "Blackwell or newer NVIDIA GPU"): |
| 44 | + quantizer.validate_environment() |
| 45 | + |
| 46 | + def test_nvfp4_environment_allows_blackwell_cuda(self): |
| 47 | + quantization_config = NunchakuLiteQuantizationConfig(compute_dtype=torch.bfloat16, **_compact_config("nvfp4")) |
| 48 | + quantizer = NunchakuLiteQuantizer(quantization_config) |
| 49 | + |
| 50 | + with ( |
| 51 | + patch("diffusers.quantizers.nunchaku.nunchaku_quantizer.is_kernels_available", return_value=True), |
| 52 | + patch("torch.cuda.is_available", return_value=True), |
| 53 | + patch("torch.cuda.get_device_capability", return_value=(10, 0)), |
| 54 | + ): |
| 55 | + quantizer.validate_environment() |
| 56 | + |
| 57 | + def test_int4_environment_requires_turing_cuda(self): |
| 58 | + quantization_config = NunchakuLiteQuantizationConfig(compute_dtype=torch.bfloat16, **_compact_config("int4")) |
| 59 | + quantizer = NunchakuLiteQuantizer(quantization_config) |
| 60 | + |
| 61 | + with ( |
| 62 | + patch("diffusers.quantizers.nunchaku.nunchaku_quantizer.is_kernels_available", return_value=True), |
| 63 | + patch("torch.cuda.is_available", return_value=True), |
| 64 | + patch("torch.cuda.get_device_capability", return_value=(7, 0)), |
| 65 | + ): |
| 66 | + with self.assertRaisesRegex(ValueError, "Turing or newer NVIDIA GPU"): |
| 67 | + quantizer.validate_environment() |
0 commit comments