Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions tests/quantization/gptq/test_gptq.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require_torch_gpu,
require_torch_multi_gpu,
slow,
torch_device,
)
from transformers.utils import is_gptqmodel_available

Expand Down Expand Up @@ -105,6 +106,7 @@ class GPTQTest(unittest.TestCase):
EXPECTED_OUTPUTS.add("Hello my name is John and I am a very friendly and caring")
EXPECTED_OUTPUTS.add("Hello my name is Nils, I am a student in the field")
EXPECTED_OUTPUTS.add("Hello my name is Michael, I am a professional photographer and I")
EXPECTED_OUTPUTS.add("Hello my name is Nils and I am a professional photographer.")

# this seems a little small considering that we are doing 4bit quant but we have a small model and ww don't quantize the embeddings
EXPECTED_RELATIVE_DIFFERENCE = 1.664253062
Expand Down Expand Up @@ -168,7 +170,7 @@ def test_device_and_dtype_assignment(self):
Checks also if other models are casted correctly.
"""
# This should work
if self.device_map in (None, "cpu"):
if self.device_map is None:
_ = self.quantized_model.to(0)

with self.assertRaises(ValueError):
Expand Down Expand Up @@ -237,12 +239,14 @@ def test_serialization(self):
with tempfile.TemporaryDirectory() as tmpdirname:
self.tokenizer.save_pretrained(tmpdirname)
self.quantized_model.save_pretrained(tmpdirname)
if self.device_map == "cpu":
quantized_model_from_saved = AutoModelForCausalLM.from_pretrained(tmpdirname, device_map=self.device_map)

if self.device_map == "cpu" or torch_device == "cpu":
quant_type = "torch_aten_kernel"
elif torch_device == "xpu":
quant_type = "torch_fused"
else:
quant_type = "exllamav2"
quantized_model_from_saved = AutoModelForCausalLM.from_pretrained(tmpdirname, device_map=self.device_map)

self.check_quantized_layers_type(quantized_model_from_saved, quant_type)
self.check_inference_correctness(quantized_model_from_saved)

Expand All @@ -258,9 +262,8 @@ def test_serialization_big_model_inference(self):
self.check_inference_correctness(quantized_model_from_saved)


@require_torch_gpu
class GPTQTestCUDA(GPTQTest):
device_map = {"": 0}
class GPTQTestAccelerator(GPTQTest):
device_map = {"": 0} if torch_device != "cpu" else "cpu"

def test_change_loading_attributes(self):
"""
Expand All @@ -274,14 +277,19 @@ def test_change_loading_attributes(self):
device_map=self.device_map,
)
self.assertEqual(quantized_model_from_saved.config.quantization_config.bits, self.bits)
quant_type = "exllamav2" if self.device_map != "cpu" else "torch"
if self.device_map == "cpu":
quant_type = "torch_aten_kernel"
elif torch_device == "xpu":
quant_type = "torch_fused"
else:
quant_type = "exllamav2"
self.check_quantized_layers_type(quantized_model_from_saved, quant_type)
self.check_inference_correctness(quantized_model_from_saved)


@require_accelerate
@require_torch_multi_gpu
class GPTQTestDeviceMap(GPTQTestCUDA):
class GPTQTestDeviceMap(GPTQTestAccelerator):
device_map = "auto"


Expand Down
17 changes: 8 additions & 9 deletions tests/quantization/mxfp4/test_mxfp4.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from transformers.testing_utils import (
require_kernels,
require_torch,
require_torch_gpu,
require_torch_large_accelerator,
require_triton,
slow,
Expand Down Expand Up @@ -143,9 +142,9 @@ def test_quantizer_validation_no_accelerator(self):
# CPU already supported MXFP4
quantizer.validate_environment()

@require_torch_gpu
@unittest.skipUnless(torch_device in {"cuda", "xpu"}, "test requires CUDA or XPU")
def test_quantizer_validation_low_compute_capability(self):
"""Test quantizer validation with CUDA low compute capability"""
"""Test quantizer validation with CUDA low compute capability or supported XPU"""
with patch("torch.cuda.get_device_capability", return_value=(7, 0)):
from transformers.quantizers.quantizer_mxfp4 import Mxfp4HfQuantizer

Expand All @@ -156,9 +155,9 @@ def test_quantizer_validation_low_compute_capability(self):
with self.assertRaises(ValueError):
quantizer.validate_environment()

@require_torch_gpu
@unittest.skipUnless(torch_device in {"cuda", "xpu"}, "test requires CUDA or XPU")
def test_quantizer_validation_low_compute_capability_with_prequantized(self):
"""Test quantizer validation with CUDA low compute capability"""
"""Test pre-quantized validation with CUDA low compute capability or supported XPU"""
with patch("torch.cuda.get_device_capability", return_value=(7, 0)):
from transformers.quantizers.quantizer_mxfp4 import Mxfp4HfQuantizer

Expand All @@ -169,9 +168,9 @@ def test_quantizer_validation_low_compute_capability_with_prequantized(self):
quantizer.validate_environment()
self.assertTrue(quantizer.quantization_config.dequantize)

@require_torch_gpu
@unittest.skipUnless(torch_device in {"cuda", "xpu"}, "test requires CUDA or XPU")
def test_quantizer_validation_low_compute_capability_with_dequantize(self):
"""Test quantizer validation with CUDA low compute capability but dequantize enabled"""
"""Test quantizer validation with dequantize enabled"""
with patch("torch.cuda.get_device_capability", return_value=(7, 0)):
from transformers.quantizers.quantizer_mxfp4 import Mxfp4HfQuantizer

Expand Down Expand Up @@ -251,7 +250,7 @@ def test_is_trainable(self):
# MXFP4 is not trainable
self.assertFalse(quantizer.is_trainable)

@require_torch_gpu
@unittest.skipUnless(torch_device in {"cuda", "xpu"}, "test requires CUDA or XPU")
def test_warning_distinguishes_triton_from_kernels(self):
"""When only one dependency is missing, warning should mention it specifically."""
from transformers.quantizers.quantizer_mxfp4 import Mxfp4HfQuantizer
Expand Down Expand Up @@ -288,7 +287,7 @@ def test_warning_distinguishes_triton_from_kernels(self):
self.assertIn("triton", warning_text.lower())
self.assertTrue(quantizer.quantization_config.dequantize)

@require_torch_gpu
@unittest.skipUnless(torch_device in {"cuda", "xpu"}, "test requires CUDA or XPU")
def test_error_distinguishes_triton_from_kernels(self):
"""When quantizing without a dependency, ValueError should mention it specifically."""
from transformers.quantizers.quantizer_mxfp4 import Mxfp4HfQuantizer
Expand Down
Loading