From 8dc297d32adf90a079decd0a8649736dc5258089 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Fri, 6 Jun 2025 23:40:46 +0530 Subject: [PATCH 01/13] Update test_functional.py --- tests/test_functional.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/test_functional.py b/tests/test_functional.py index a2964c733..95f75d99f 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -98,6 +98,9 @@ class Test8BitBlockwiseQuantizeFunctional: ) @pytest.mark.parametrize("signed", TRUE_FALSE, ids=id_formatter("signed")) def test_dynamic_blockwise_quantization(self, device, dtype, nested, blocksize, signed): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + iters = 100 if device == "cpu": @@ -150,6 +153,7 @@ def test_dynamic_blockwise_quantization(self, device, dtype, nested, blocksize, assert A2.dtype == dtype @pytest.mark.skipif("cpu" not in get_available_devices(), reason="CPU is required") + @pytest.mark.skipif(HIP_ENVIRONMENT, reason="CPU tests skipped when HIP_ENVIRONMENT is set") @pytest.mark.parametrize("hidden", [128]) @pytest.mark.parametrize("blocksize", [4096, 16384]) def test_blockwise_cpu_large(self, hidden, blocksize): @@ -176,6 +180,9 @@ def test_blockwise_cpu_large(self, hidden, blocksize): @pytest.mark.parametrize("bits", range(2, 9), ids=id_formatter("bits")) @pytest.mark.parametrize("method", ["linear", "fp8", "dynamic", "quantile"]) def test_few_bit_quant(self, device, bits, method): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu" and bits != 8: pytest.skip("CPU implementation only supports 8 bits") @@ -232,6 +239,9 @@ def test_few_bit_quant(self, device, bits, method): @pytest.mark.parametrize("device", get_available_devices()) def test_fp8_quant(self, device): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + # TODO if device == "cpu": pytest.skip("CPU implementation segfaults") @@ -570,6 +580,9 @@ class TestLLMInt8Functional: @pytest.mark.parametrize("dims", (2, 3), ids=id_formatter("dims")) @pytest.mark.parametrize("ldb", (0,), ids=id_formatter("ldb")) def test_int8_linear_matmul(self, device, dim1, dim2, dim3, dim4, dims, ldb): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + for i in range(k): if dims == 2: A = torch.randint(-128, 127, size=(dim1, dim3), dtype=torch.int8, device=device) @@ -588,6 +601,9 @@ def test_int8_linear_matmul(self, device, dim1, dim2, dim3, dim4, dims, ldb): @pytest.mark.parametrize("dim4", [32], ids=id_formatter("dim4")) @pytest.mark.parametrize("dims", (2,), ids=id_formatter("dims")) def test_int8_linear_matmul_half(self, device, dim1, dim2, dim3, dim4, dims): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + for i in range(k): if dims == 2: A = torch.normal(0, 0.5, size=(dim1, dim3), device=device).half() @@ -611,6 +627,9 @@ def test_int8_linear_matmul_half(self, device, dim1, dim2, dim3, dim4, dims): @pytest.mark.parametrize("dims", (2,), ids=id_formatter("dims")) @pytest.mark.parametrize("has_bias", TRUE_FALSE, ids=id_formatter("has_bias")) def test_dequant_mm(self, device, dim1, dim4, dims, has_bias): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + inner = 128 bias = None if has_bias: @@ -734,6 +753,9 @@ def test_int8_double_quant(self, dim1, dim2): ), ) def test_integrated_int8_linear_matmul(self, device, dim1, dim4, inner): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu" and inner > 2048: pytest.skip("Slow on CPU") @@ -767,6 +789,9 @@ def test_integrated_int8_linear_matmul(self, device, dim1, dim4, inner): @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) def test_coo_double_quant(self, device, dim1, dim2): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + threshold = 2.00 for i in range(k): A = torch.randn(dim1, dim2, device=device).half() @@ -787,6 +812,9 @@ def test_coo_double_quant(self, device, dim1, dim2): @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) def test_coo_int8_vectorwise_quant(self, device, dim1, dim2): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + threshold = 3.00 for i in range(k): A = torch.randn(dim1, dim2, device=device).half() @@ -1115,6 +1143,9 @@ class TestQuantize4BitFunctional: [64, 128, 256, 512, 1024, 2048, 4096] if not HIP_ENVIRONMENT else [128, 256, 512, 1024, 2048, 4096], ) def test_4bit_quant(self, device, dtype, quant_type, blocksize): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu" and quant_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") @@ -1150,6 +1181,9 @@ def test_4bit_quant(self, device, dtype, quant_type, blocksize): @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128], ids=id_formatter("blocksize")) def test_4bit_compressed_stats(self, device, quant_type, blocksize): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu" and quant_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") @@ -1228,6 +1262,9 @@ def test_bench_4bit_dequant(self, quant_type): ) @pytest.mark.parametrize("dim", [128, 256, 512, 1024], ids=id_formatter("dim")) def test_gemv_4bit(self, device, dim, dtype, storage_type, quant_storage, double_quant, kind): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": if storage_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") @@ -1384,6 +1421,9 @@ def test_gemv_4bit(self, device, dim, dtype, storage_type, quant_storage, double reason="this test is not supported on ROCm with gfx90a architecture yet", ) def test_gemv_eye_4bit(self, device, storage_type, dtype, double_quant): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu" and storage_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") From f7d8bf340bb9d36c3412cbbedc564c2edecc8308 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Fri, 6 Jun 2025 23:45:28 +0530 Subject: [PATCH 02/13] Update test_linear4bit.py --- tests/test_linear4bit.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_linear4bit.py b/tests/test_linear4bit.py index 60c163477..546ed2681 100644 --- a/tests/test_linear4bit.py +++ b/tests/test_linear4bit.py @@ -25,6 +25,9 @@ @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("save_before_forward", TRUE_FALSE, ids=id_formatter("save_before_forward")) def test_linear_serialization(device, quant_type, compress_statistics, bias, quant_storage, save_before_forward): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": if quant_type == "fp4": pytest.xfail("FP4 is not supported for CPU") @@ -187,6 +190,9 @@ def test_linear_serialization(device, quant_type, compress_statistics, bias, qua @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) def test_copy_param(device, quant_type, blocksize, compress_statistics): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": if compress_statistics: pytest.skip("Currently segfaults on CPU") @@ -212,6 +218,9 @@ def test_copy_param(device, quant_type, blocksize, compress_statistics): @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) def test_deepcopy_param(device, quant_type, blocksize, compress_statistics): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": if compress_statistics: pytest.skip("Currently segfaults on CPU") @@ -244,6 +253,9 @@ def test_deepcopy_param(device, quant_type, blocksize, compress_statistics): @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) def test_params4bit_real_serialization(device, quant_type, blocksize, compress_statistics): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": if compress_statistics: pytest.skip("Currently segfaults on CPU") From fd0a4d0fc4dc610fcf96a0469b41a68299d6daa5 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Fri, 6 Jun 2025 23:52:40 +0530 Subject: [PATCH 03/13] Update test_ops.py --- tests/test_ops.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_ops.py b/tests/test_ops.py index a433a0c4b..3879aa479 100644 --- a/tests/test_ops.py +++ b/tests/test_ops.py @@ -11,6 +11,9 @@ class TestLLMInt8Ops: @pytest.mark.parametrize("device", get_available_devices()) def test_int8_linear_matmul(self, device): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) out = torch.ops.bitsandbytes.int8_linear_matmul.default(A, B) @@ -23,6 +26,9 @@ def test_int8_linear_matmul(self, device): @pytest.mark.parametrize("device", get_available_devices()) def test_int8_linear_matmul_out(self, device): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) @@ -38,6 +44,9 @@ def test_int8_linear_matmul_out(self, device): @pytest.mark.parametrize("threshold", [0.0, 6.0]) @pytest.mark.parametrize("device", get_available_devices()) def test_int8_vectorwise_quant(self, threshold, device): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + A = torch.randn(10, 20, dtype=torch.float16, device=device) A[1][0] = 1000.0 @@ -64,6 +73,9 @@ def test_int8_vectorwise_quant(self, threshold, device): @pytest.mark.parametrize("device", get_available_devices()) def test_int8_mm_dequant(self, device): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + A = torch.randint(-128, 127, (256, 256), dtype=torch.int32, device=device) row_stats = torch.randn(256, dtype=torch.float32, device=device) col_stats = torch.randn(256, dtype=torch.float32, device=device) @@ -79,6 +91,9 @@ def test_int8_mm_dequant(self, device): @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("has_bias", TRUE_FALSE) def test_int8_scaled_mm(self, device, dtype, has_bias): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) row_stats = torch.randn(10, dtype=torch.float32, device=device) @@ -98,6 +113,9 @@ class TestInt8BlockwiseQuantOps: @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_quantize_blockwise(self, device, dtype, blocksize): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": if dtype != torch.float32: pytest.skip("CPU implementation is only available for float32") @@ -122,6 +140,9 @@ def test_quantize_blockwise(self, device, dtype, blocksize): @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_dequantize_blockwise(self, device, dtype, blocksize): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu" and dtype != torch.float32: pytest.skip("CPU implementation is only available for float32") @@ -148,6 +169,9 @@ class Test4bitBlockwiseQuantOps: @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_quantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu" and quant_type != "nf4": pytest.xfail("CPU implementation is only available for nf4") @@ -172,6 +196,9 @@ def test_quantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_dequantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": if quant_type != "nf4": pytest.xfail("CPU implementation is only available for nf4") @@ -209,6 +236,9 @@ def test_dequantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksi @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_gemv_4bit(self, device, dtype, storage_dtype, quant_type, blocksize): + if HIP_ENVIRONMENT and device == "cpu": + pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") + if device == "cpu": pytest.xfail("CPU implementation is not available") From 3551457f987e834999b39f6df01868587e3233e3 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 02:32:43 +0530 Subject: [PATCH 04/13] Update test_functional.py --- tests/test_functional.py | 105 +++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/tests/test_functional.py b/tests/test_functional.py index 95f75d99f..719f21137 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -89,7 +89,10 @@ def reset(self): class Test8BitBlockwiseQuantizeFunctional: - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype) @pytest.mark.parametrize("nested", TRUE_FALSE, ids=id_formatter("nested")) @pytest.mark.parametrize( @@ -98,9 +101,6 @@ class Test8BitBlockwiseQuantizeFunctional: ) @pytest.mark.parametrize("signed", TRUE_FALSE, ids=id_formatter("signed")) def test_dynamic_blockwise_quantization(self, device, dtype, nested, blocksize, signed): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - iters = 100 if device == "cpu": @@ -153,7 +153,6 @@ def test_dynamic_blockwise_quantization(self, device, dtype, nested, blocksize, assert A2.dtype == dtype @pytest.mark.skipif("cpu" not in get_available_devices(), reason="CPU is required") - @pytest.mark.skipif(HIP_ENVIRONMENT, reason="CPU tests skipped when HIP_ENVIRONMENT is set") @pytest.mark.parametrize("hidden", [128]) @pytest.mark.parametrize("blocksize", [4096, 16384]) def test_blockwise_cpu_large(self, hidden, blocksize): @@ -176,13 +175,13 @@ def test_blockwise_cpu_large(self, hidden, blocksize): # print(sum(diffs)/len(diffs)) # print(sum(reldiffs)/len(reldiffs)) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("bits", range(2, 9), ids=id_formatter("bits")) @pytest.mark.parametrize("method", ["linear", "fp8", "dynamic", "quantile"]) def test_few_bit_quant(self, device, bits, method): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu" and bits != 8: pytest.skip("CPU implementation only supports 8 bits") @@ -237,11 +236,11 @@ def test_few_bit_quant(self, device, bits, method): else: torch.testing.assert_close(q1, q2) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_fp8_quant(self, device): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - # TODO if device == "cpu": pytest.skip("CPU implementation segfaults") @@ -572,7 +571,10 @@ def test_ibmm(self, dim1, dim2, dim3, dim4, transpose): class TestLLMInt8Functional: - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dim1", [128], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [256], ids=id_formatter("dim2")) @pytest.mark.parametrize("dim3", [499, 512], ids=id_formatter("dim3")) @@ -580,9 +582,6 @@ class TestLLMInt8Functional: @pytest.mark.parametrize("dims", (2, 3), ids=id_formatter("dims")) @pytest.mark.parametrize("ldb", (0,), ids=id_formatter("ldb")) def test_int8_linear_matmul(self, device, dim1, dim2, dim3, dim4, dims, ldb): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - for i in range(k): if dims == 2: A = torch.randint(-128, 127, size=(dim1, dim3), dtype=torch.int8, device=device) @@ -594,16 +593,16 @@ def test_int8_linear_matmul(self, device, dim1, dim2, dim3, dim4, dims, ldb): C2 = F.int8_linear_matmul(A, B) torch.testing.assert_close(C1, C2.float()) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dim1", [32], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [32], ids=id_formatter("dim2")) @pytest.mark.parametrize("dim3", [32], ids=id_formatter("dim3")) @pytest.mark.parametrize("dim4", [32], ids=id_formatter("dim4")) @pytest.mark.parametrize("dims", (2,), ids=id_formatter("dims")) def test_int8_linear_matmul_half(self, device, dim1, dim2, dim3, dim4, dims): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - for i in range(k): if dims == 2: A = torch.normal(0, 0.5, size=(dim1, dim3), device=device).half() @@ -621,15 +620,15 @@ def test_int8_linear_matmul_half(self, device, dim1, dim2, dim3, dim4, dims): torch.testing.assert_close(C1.view(-1, C1.shape[-1]), output, atol=0.025, rtol=0.05) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dim1", (64, 256), ids=id_formatter("dim1")) @pytest.mark.parametrize("dim4", (64, 1024), ids=id_formatter("dim4")) @pytest.mark.parametrize("dims", (2,), ids=id_formatter("dims")) @pytest.mark.parametrize("has_bias", TRUE_FALSE, ids=id_formatter("has_bias")) def test_dequant_mm(self, device, dim1, dim4, dims, has_bias): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - inner = 128 bias = None if has_bias: @@ -740,7 +739,10 @@ def test_int8_double_quant(self, dim1, dim2): torch.testing.assert_close(Srow.flatten().float(), statsA) torch.testing.assert_close(Scol.flatten().float(), statsAt) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize( ("dim1", "dim4", "inner"), ( @@ -753,9 +755,6 @@ def test_int8_double_quant(self, dim1, dim2): ), ) def test_integrated_int8_linear_matmul(self, device, dim1, dim4, inner): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu" and inner > 2048: pytest.skip("Slow on CPU") @@ -785,13 +784,13 @@ def test_integrated_int8_linear_matmul(self, device, dim1, dim4, inner): err2 = torch.abs(out1 - out3).mean().item() assert err2 <= err1 * 1.025 - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) def test_coo_double_quant(self, device, dim1, dim2): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - threshold = 2.00 for i in range(k): A = torch.randn(dim1, dim2, device=device).half() @@ -808,13 +807,13 @@ def test_coo_double_quant(self, device, dim1, dim2): A2 = (CA.float() * statsA.unsqueeze(1) / 127).half() torch.testing.assert_close(A, A2, rtol=0.05, atol=1.5e-2) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) def test_coo_int8_vectorwise_quant(self, device, dim1, dim2): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - threshold = 3.00 for i in range(k): A = torch.randn(dim1, dim2, device=device).half() @@ -1135,7 +1134,10 @@ def test_coo2csc(self): class TestQuantize4BitFunctional: - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize( @@ -1143,9 +1145,6 @@ class TestQuantize4BitFunctional: [64, 128, 256, 512, 1024, 2048, 4096] if not HIP_ENVIRONMENT else [128, 256, 512, 1024, 2048, 4096], ) def test_4bit_quant(self, device, dtype, quant_type, blocksize): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu" and quant_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") @@ -1177,13 +1176,13 @@ def test_4bit_quant(self, device, dtype, quant_type, blocksize): # 1024 => 0.8, 2048 => 0.88, 4096 => 0.96 assert err.item() < math.log2(blocksize) * 8e-2 - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128], ids=id_formatter("blocksize")) def test_4bit_compressed_stats(self, device, quant_type, blocksize): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu" and quant_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") @@ -1250,7 +1249,10 @@ def test_bench_4bit_dequant(self, quant_type): @pytest.mark.skipif( HIP_ENVIRONMENT, reason="gemv 4bit tests are partially enabled on MI300, others being fixed for warpsize 64" ) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("double_quant", TRUE_FALSE, ids=lambda double_quant: f"DQ_{double_quant}") @pytest.mark.parametrize("storage_type", ["nf4", "fp4"]) @pytest.mark.parametrize("kind", ["fc1", "fc2", "attn", "attn_packed"]) @@ -1262,9 +1264,6 @@ def test_bench_4bit_dequant(self, quant_type): ) @pytest.mark.parametrize("dim", [128, 256, 512, 1024], ids=id_formatter("dim")) def test_gemv_4bit(self, device, dim, dtype, storage_type, quant_storage, double_quant, kind): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": if storage_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") @@ -1412,7 +1411,10 @@ def test_gemv_4bit(self, device, dim, dtype, storage_type, quant_storage, double assert relratio < 1.04 and relratio > 0.96 assert maxratio < 1.02 and maxratio > 0.98 - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("storage_type", ["nf4", "fp4"], ids=["nf4", "fp4"]) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=describe_dtype) @pytest.mark.parametrize("double_quant", [False], ids=["DQ_True"]) @@ -1421,9 +1423,6 @@ def test_gemv_4bit(self, device, dim, dtype, storage_type, quant_storage, double reason="this test is not supported on ROCm with gfx90a architecture yet", ) def test_gemv_eye_4bit(self, device, storage_type, dtype, double_quant): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu" and storage_type != "nf4": pytest.xfail("fp4 quantization is not supported on CPU") From 90437b94837529b7519e59c64a5d5774090fba80 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 02:44:33 +0530 Subject: [PATCH 05/13] Update test_linear4bit.py --- tests/test_linear4bit.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_linear4bit.py b/tests/test_linear4bit.py index 546ed2681..fe3f4b13c 100644 --- a/tests/test_linear4bit.py +++ b/tests/test_linear4bit.py @@ -18,7 +18,10 @@ } -@pytest.mark.parametrize("device", get_available_devices()) +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +) @pytest.mark.parametrize("quant_storage", ["uint8", "float16", "bfloat16", "float32"]) @pytest.mark.parametrize("bias", TRUE_FALSE, ids=id_formatter("bias")) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @@ -185,7 +188,10 @@ def test_linear_serialization(device, quant_type, compress_statistics, bias, qua assert size_ratio < target_compression, ratio_error_msg -@pytest.mark.parametrize("device", get_available_devices()) +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @@ -213,7 +219,10 @@ def test_copy_param(device, quant_type, blocksize, compress_statistics): assert param.data.data_ptr() == shallow_copy_param.data.data_ptr() -@pytest.mark.parametrize("device", get_available_devices()) +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @@ -248,7 +257,10 @@ def test_deepcopy_param(device, quant_type, blocksize, compress_statistics): assert dict_keys_before == dict_keys_copy -@pytest.mark.parametrize("device", get_available_devices()) +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) From a0bdc94db673238c0b3e12ff9ac03117f5f966f2 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 02:51:33 +0530 Subject: [PATCH 06/13] Update test_ops.py --- tests/test_ops.py | 80 +++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/tests/test_ops.py b/tests/test_ops.py index 3879aa479..e3be5fd50 100644 --- a/tests/test_ops.py +++ b/tests/test_ops.py @@ -9,11 +9,11 @@ class TestLLMInt8Ops: - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_linear_matmul(self, device): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) out = torch.ops.bitsandbytes.int8_linear_matmul.default(A, B) @@ -24,11 +24,11 @@ def test_int8_linear_matmul(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_linear_matmul.default, (A, B)) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_linear_matmul_out(self, device): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) @@ -42,11 +42,11 @@ def test_int8_linear_matmul_out(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_linear_matmul.out, (A, B, out)) @pytest.mark.parametrize("threshold", [0.0, 6.0]) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_vectorwise_quant(self, threshold, device): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - A = torch.randn(10, 20, dtype=torch.float16, device=device) A[1][0] = 1000.0 @@ -71,11 +71,11 @@ def test_int8_vectorwise_quant(self, threshold, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_vectorwise_quant, (A, threshold)) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_mm_dequant(self, device): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - A = torch.randint(-128, 127, (256, 256), dtype=torch.int32, device=device) row_stats = torch.randn(256, dtype=torch.float32, device=device) col_stats = torch.randn(256, dtype=torch.float32, device=device) @@ -87,13 +87,13 @@ def test_int8_mm_dequant(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_mm_dequant, (A, row_stats, col_stats)) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("has_bias", TRUE_FALSE) def test_int8_scaled_mm(self, device, dtype, has_bias): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) row_stats = torch.randn(10, dtype=torch.float32, device=device) @@ -109,13 +109,13 @@ def test_int8_scaled_mm(self, device, dtype, has_bias): class TestInt8BlockwiseQuantOps: - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_quantize_blockwise(self, device, dtype, blocksize): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": if dtype != torch.float32: pytest.skip("CPU implementation is only available for float32") @@ -136,13 +136,13 @@ def test_quantize_blockwise(self, device, dtype, blocksize): torch.library.opcheck(torch.ops.bitsandbytes.quantize_blockwise, (A, code, blocksize)) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_dequantize_blockwise(self, device, dtype, blocksize): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu" and dtype != torch.float32: pytest.skip("CPU implementation is only available for float32") @@ -163,15 +163,15 @@ def test_dequantize_blockwise(self, device, dtype, blocksize): class Test4bitBlockwiseQuantOps: - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_quantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu" and quant_type != "nf4": pytest.xfail("CPU implementation is only available for nf4") @@ -190,15 +190,15 @@ def test_quantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize torch.library.opcheck(torch.ops.bitsandbytes.quantize_4bit, (A, blocksize, quant_type, storage_dtype)) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_dequantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": if quant_type != "nf4": pytest.xfail("CPU implementation is only available for nf4") @@ -230,15 +230,15 @@ def test_dequantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksi torch.ops.bitsandbytes.dequantize_4bit.default, (A, absmax, blocksize, quant_type, shape, dtype) ) - @pytest.mark.parametrize("device", get_available_devices()) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_gemv_4bit(self, device, dtype, storage_dtype, quant_type, blocksize): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": pytest.xfail("CPU implementation is not available") From 8a27346f8fd6ecf8eea4127ea13899618d9a921c Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 02:56:08 +0530 Subject: [PATCH 07/13] Update test_linear4bit.py --- tests/test_linear4bit.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/tests/test_linear4bit.py b/tests/test_linear4bit.py index fe3f4b13c..760e4b8c9 100644 --- a/tests/test_linear4bit.py +++ b/tests/test_linear4bit.py @@ -21,16 +21,13 @@ @pytest.mark.parametrize( "device", [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +) @pytest.mark.parametrize("quant_storage", ["uint8", "float16", "bfloat16", "float32"]) @pytest.mark.parametrize("bias", TRUE_FALSE, ids=id_formatter("bias")) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("save_before_forward", TRUE_FALSE, ids=id_formatter("save_before_forward")) def test_linear_serialization(device, quant_type, compress_statistics, bias, quant_storage, save_before_forward): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": if quant_type == "fp4": pytest.xfail("FP4 is not supported for CPU") @@ -191,14 +188,11 @@ def test_linear_serialization(device, quant_type, compress_statistics, bias, qua @pytest.mark.parametrize( "device", [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) def test_copy_param(device, quant_type, blocksize, compress_statistics): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": if compress_statistics: pytest.skip("Currently segfaults on CPU") @@ -222,14 +216,11 @@ def test_copy_param(device, quant_type, blocksize, compress_statistics): @pytest.mark.parametrize( "device", [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) def test_deepcopy_param(device, quant_type, blocksize, compress_statistics): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": if compress_statistics: pytest.skip("Currently segfaults on CPU") @@ -260,14 +251,11 @@ def test_deepcopy_param(device, quant_type, blocksize, compress_statistics): @pytest.mark.parametrize( "device", [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) def test_params4bit_real_serialization(device, quant_type, blocksize, compress_statistics): - if HIP_ENVIRONMENT and device == "cpu": - pytest.skip("CPU tests skipped when HIP_ENVIRONMENT is set") - if device == "cpu": if compress_statistics: pytest.skip("Currently segfaults on CPU") From c945dbb5c8b14bf54631d39f51b6c1d841981043 Mon Sep 17 00:00:00 2001 From: MISHANMAUYRA Date: Wed, 11 Jun 2025 03:05:38 +0530 Subject: [PATCH 08/13] Lint --- tests/test_functional.py | 70 ++++++++++++++++++------------------ tests/test_linear4bit.py | 26 +++++++------- tests/test_ops.py | 78 ++++++++++++++++++++-------------------- 3 files changed, 87 insertions(+), 87 deletions(-) diff --git a/tests/test_functional.py b/tests/test_functional.py index 719f21137..571eea55f 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -236,9 +236,9 @@ def test_few_bit_quant(self, device, bits, method): else: torch.testing.assert_close(q1, q2) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) def test_fp8_quant(self, device): # TODO @@ -571,9 +571,9 @@ def test_ibmm(self, dim1, dim2, dim3, dim4, transpose): class TestLLMInt8Functional: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("dim1", [128], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [256], ids=id_formatter("dim2")) @@ -593,9 +593,9 @@ def test_int8_linear_matmul(self, device, dim1, dim2, dim3, dim4, dims, ldb): C2 = F.int8_linear_matmul(A, B) torch.testing.assert_close(C1, C2.float()) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("dim1", [32], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [32], ids=id_formatter("dim2")) @@ -620,9 +620,9 @@ def test_int8_linear_matmul_half(self, device, dim1, dim2, dim3, dim4, dims): torch.testing.assert_close(C1.view(-1, C1.shape[-1]), output, atol=0.025, rtol=0.05) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("dim1", (64, 256), ids=id_formatter("dim1")) @pytest.mark.parametrize("dim4", (64, 1024), ids=id_formatter("dim4")) @@ -739,10 +739,10 @@ def test_int8_double_quant(self, dim1, dim2): torch.testing.assert_close(Srow.flatten().float(), statsA) torch.testing.assert_close(Scol.flatten().float(), statsAt) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize( ("dim1", "dim4", "inner"), ( @@ -784,10 +784,10 @@ def test_integrated_int8_linear_matmul(self, device, dim1, dim4, inner): err2 = torch.abs(out1 - out3).mean().item() assert err2 <= err1 * 1.025 - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) def test_coo_double_quant(self, device, dim1, dim2): @@ -807,9 +807,9 @@ def test_coo_double_quant(self, device, dim1, dim2): A2 = (CA.float() * statsA.unsqueeze(1) / 127).half() torch.testing.assert_close(A, A2, rtol=0.05, atol=1.5e-2) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) @@ -1134,9 +1134,9 @@ def test_coo2csc(self): class TestQuantize4BitFunctional: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @@ -1176,9 +1176,9 @@ def test_4bit_quant(self, device, dtype, quant_type, blocksize): # 1024 => 0.8, 2048 => 0.88, 4096 => 0.96 assert err.item() < math.log2(blocksize) * 8e-2 - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128], ids=id_formatter("blocksize")) @@ -1249,9 +1249,9 @@ def test_bench_4bit_dequant(self, quant_type): @pytest.mark.skipif( HIP_ENVIRONMENT, reason="gemv 4bit tests are partially enabled on MI300, others being fixed for warpsize 64" ) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("double_quant", TRUE_FALSE, ids=lambda double_quant: f"DQ_{double_quant}") @pytest.mark.parametrize("storage_type", ["nf4", "fp4"]) @@ -1411,9 +1411,9 @@ def test_gemv_4bit(self, device, dim, dtype, storage_type, quant_storage, double assert relratio < 1.04 and relratio > 0.96 assert maxratio < 1.02 and maxratio > 0.98 - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("storage_type", ["nf4", "fp4"], ids=["nf4", "fp4"]) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=describe_dtype) diff --git a/tests/test_linear4bit.py b/tests/test_linear4bit.py index 760e4b8c9..ddc609616 100644 --- a/tests/test_linear4bit.py +++ b/tests/test_linear4bit.py @@ -18,9 +18,9 @@ } -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("quant_storage", ["uint8", "float16", "bfloat16", "float32"]) @pytest.mark.parametrize("bias", TRUE_FALSE, ids=id_formatter("bias")) @@ -185,10 +185,10 @@ def test_linear_serialization(device, quant_type, compress_statistics, bias, qua assert size_ratio < target_compression, ratio_error_msg -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @@ -213,9 +213,9 @@ def test_copy_param(device, quant_type, blocksize, compress_statistics): assert param.data.data_ptr() == shallow_copy_param.data.data_ptr() -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @@ -248,9 +248,9 @@ def test_deepcopy_param(device, quant_type, blocksize, compress_statistics): assert dict_keys_before == dict_keys_copy -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], +@pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) diff --git a/tests/test_ops.py b/tests/test_ops.py index e3be5fd50..9d406b793 100644 --- a/tests/test_ops.py +++ b/tests/test_ops.py @@ -9,10 +9,10 @@ class TestLLMInt8Ops: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_linear_matmul(self, device): A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) @@ -24,10 +24,10 @@ def test_int8_linear_matmul(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_linear_matmul.default, (A, B)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_linear_matmul_out(self, device): A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) @@ -42,10 +42,10 @@ def test_int8_linear_matmul_out(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_linear_matmul.out, (A, B, out)) @pytest.mark.parametrize("threshold", [0.0, 6.0]) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_vectorwise_quant(self, threshold, device): A = torch.randn(10, 20, dtype=torch.float16, device=device) A[1][0] = 1000.0 @@ -71,10 +71,10 @@ def test_int8_vectorwise_quant(self, threshold, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_vectorwise_quant, (A, threshold)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) def test_int8_mm_dequant(self, device): A = torch.randint(-128, 127, (256, 256), dtype=torch.int32, device=device) row_stats = torch.randn(256, dtype=torch.float32, device=device) @@ -87,9 +87,9 @@ def test_int8_mm_dequant(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_mm_dequant, (A, row_stats, col_stats)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("has_bias", TRUE_FALSE) @@ -109,10 +109,10 @@ def test_int8_scaled_mm(self, device, dtype, has_bias): class TestInt8BlockwiseQuantOps: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_quantize_blockwise(self, device, dtype, blocksize): @@ -136,10 +136,10 @@ def test_quantize_blockwise(self, device, dtype, blocksize): torch.library.opcheck(torch.ops.bitsandbytes.quantize_blockwise, (A, code, blocksize)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_dequantize_blockwise(self, device, dtype, blocksize): @@ -163,10 +163,10 @@ def test_dequantize_blockwise(self, device, dtype, blocksize): class Test4bitBlockwiseQuantOps: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @@ -190,10 +190,10 @@ def test_quantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize torch.library.opcheck(torch.ops.bitsandbytes.quantize_4bit, (A, blocksize, quant_type, storage_dtype)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @@ -230,10 +230,10 @@ def test_dequantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksi torch.ops.bitsandbytes.dequantize_4bit.default, (A, absmax, blocksize, quant_type, shape, dtype) ) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize( + "device", + [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], + ) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) From 2cce3366b363de9499220a00a62e34e88183ced4 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:03:19 +0530 Subject: [PATCH 09/13] Update helpers.py --- tests/helpers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/helpers.py b/tests/helpers.py index fbc4af071..671ea39eb 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -7,6 +7,8 @@ import torch +from bitsandbytes.cextension import HIP_ENVIRONMENT + test_dims_rng = random.Random(42) @@ -21,7 +23,7 @@ def get_available_devices(): # If the environment variable is set, use it directly. return [os.environ["BNB_TEST_DEVICE"]] - devices = ["cpu"] + devices = [] if HIP_ENVIRONMENT else ["cpu"] if hasattr(torch, "accelerator"): # PyTorch 2.6+ - determine accelerator using agnostic API. From 5eb0316802d87c25e0c850a13c7cec77e9648583 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:15:09 +0530 Subject: [PATCH 10/13] Update test_functional.py --- tests/test_functional.py | 65 ++++++++-------------------------------- 1 file changed, 13 insertions(+), 52 deletions(-) diff --git a/tests/test_functional.py b/tests/test_functional.py index 571eea55f..a2964c733 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -89,10 +89,7 @@ def reset(self): class Test8BitBlockwiseQuantizeFunctional: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype) @pytest.mark.parametrize("nested", TRUE_FALSE, ids=id_formatter("nested")) @pytest.mark.parametrize( @@ -175,10 +172,7 @@ def test_blockwise_cpu_large(self, hidden, blocksize): # print(sum(diffs)/len(diffs)) # print(sum(reldiffs)/len(reldiffs)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("bits", range(2, 9), ids=id_formatter("bits")) @pytest.mark.parametrize("method", ["linear", "fp8", "dynamic", "quantile"]) def test_few_bit_quant(self, device, bits, method): @@ -236,10 +230,7 @@ def test_few_bit_quant(self, device, bits, method): else: torch.testing.assert_close(q1, q2) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) def test_fp8_quant(self, device): # TODO if device == "cpu": @@ -571,10 +562,7 @@ def test_ibmm(self, dim1, dim2, dim3, dim4, transpose): class TestLLMInt8Functional: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dim1", [128], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [256], ids=id_formatter("dim2")) @pytest.mark.parametrize("dim3", [499, 512], ids=id_formatter("dim3")) @@ -593,10 +581,7 @@ def test_int8_linear_matmul(self, device, dim1, dim2, dim3, dim4, dims, ldb): C2 = F.int8_linear_matmul(A, B) torch.testing.assert_close(C1, C2.float()) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dim1", [32], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [32], ids=id_formatter("dim2")) @pytest.mark.parametrize("dim3", [32], ids=id_formatter("dim3")) @@ -620,10 +605,7 @@ def test_int8_linear_matmul_half(self, device, dim1, dim2, dim3, dim4, dims): torch.testing.assert_close(C1.view(-1, C1.shape[-1]), output, atol=0.025, rtol=0.05) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dim1", (64, 256), ids=id_formatter("dim1")) @pytest.mark.parametrize("dim4", (64, 1024), ids=id_formatter("dim4")) @pytest.mark.parametrize("dims", (2,), ids=id_formatter("dims")) @@ -739,10 +721,7 @@ def test_int8_double_quant(self, dim1, dim2): torch.testing.assert_close(Srow.flatten().float(), statsA) torch.testing.assert_close(Scol.flatten().float(), statsAt) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize( ("dim1", "dim4", "inner"), ( @@ -784,10 +763,7 @@ def test_integrated_int8_linear_matmul(self, device, dim1, dim4, inner): err2 = torch.abs(out1 - out3).mean().item() assert err2 <= err1 * 1.025 - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) def test_coo_double_quant(self, device, dim1, dim2): @@ -807,10 +783,7 @@ def test_coo_double_quant(self, device, dim1, dim2): A2 = (CA.float() * statsA.unsqueeze(1) / 127).half() torch.testing.assert_close(A, A2, rtol=0.05, atol=1.5e-2) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dim1", [512, 2048], ids=id_formatter("dim1")) @pytest.mark.parametrize("dim2", [1024, 4096], ids=id_formatter("dim2")) def test_coo_int8_vectorwise_quant(self, device, dim1, dim2): @@ -1134,10 +1107,7 @@ def test_coo2csc(self): class TestQuantize4BitFunctional: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize( @@ -1176,10 +1146,7 @@ def test_4bit_quant(self, device, dtype, quant_type, blocksize): # 1024 => 0.8, 2048 => 0.88, 4096 => 0.96 assert err.item() < math.log2(blocksize) * 8e-2 - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128], ids=id_formatter("blocksize")) def test_4bit_compressed_stats(self, device, quant_type, blocksize): @@ -1249,10 +1216,7 @@ def test_bench_4bit_dequant(self, quant_type): @pytest.mark.skipif( HIP_ENVIRONMENT, reason="gemv 4bit tests are partially enabled on MI300, others being fixed for warpsize 64" ) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("double_quant", TRUE_FALSE, ids=lambda double_quant: f"DQ_{double_quant}") @pytest.mark.parametrize("storage_type", ["nf4", "fp4"]) @pytest.mark.parametrize("kind", ["fc1", "fc2", "attn", "attn_packed"]) @@ -1411,10 +1375,7 @@ def test_gemv_4bit(self, device, dim, dtype, storage_type, quant_storage, double assert relratio < 1.04 and relratio > 0.96 assert maxratio < 1.02 and maxratio > 0.98 - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("storage_type", ["nf4", "fp4"], ids=["nf4", "fp4"]) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=describe_dtype) @pytest.mark.parametrize("double_quant", [False], ids=["DQ_True"]) From dcdf2c54ffe023295a1b6f60edab18d60b073552 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:15:41 +0530 Subject: [PATCH 11/13] Update test_linear4bit.py --- tests/test_linear4bit.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/tests/test_linear4bit.py b/tests/test_linear4bit.py index ddc609616..60c163477 100644 --- a/tests/test_linear4bit.py +++ b/tests/test_linear4bit.py @@ -18,10 +18,7 @@ } -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +@pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("quant_storage", ["uint8", "float16", "bfloat16", "float32"]) @pytest.mark.parametrize("bias", TRUE_FALSE, ids=id_formatter("bias")) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @@ -185,10 +182,7 @@ def test_linear_serialization(device, quant_type, compress_statistics, bias, qua assert size_ratio < target_compression, ratio_error_msg -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +@pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @@ -213,10 +207,7 @@ def test_copy_param(device, quant_type, blocksize, compress_statistics): assert param.data.data_ptr() == shallow_copy_param.data.data_ptr() -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +@pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) @@ -248,10 +239,7 @@ def test_deepcopy_param(device, quant_type, blocksize, compress_statistics): assert dict_keys_before == dict_keys_copy -@pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], -) +@pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("quant_type", ["nf4", "fp4"]) @pytest.mark.parametrize("blocksize", [64, 128] if not HIP_ENVIRONMENT else [128]) @pytest.mark.parametrize("compress_statistics", TRUE_FALSE, ids=id_formatter("compress_statistics")) From 6bba74052813946d28b238755d43756ff0e6c4f5 Mon Sep 17 00:00:00 2001 From: MISHANMAURYA <118961433+MISHANMAURYA@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:16:22 +0530 Subject: [PATCH 12/13] Update test_ops.py --- tests/test_ops.py | 50 ++++++++++------------------------------------- 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/tests/test_ops.py b/tests/test_ops.py index 9d406b793..a433a0c4b 100644 --- a/tests/test_ops.py +++ b/tests/test_ops.py @@ -9,10 +9,7 @@ class TestLLMInt8Ops: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) def test_int8_linear_matmul(self, device): A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) @@ -24,10 +21,7 @@ def test_int8_linear_matmul(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_linear_matmul.default, (A, B)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) def test_int8_linear_matmul_out(self, device): A = torch.randint(-128, 127, (10, 20), dtype=torch.int8, device=device) B = torch.randint(-128, 127, (30, 20), dtype=torch.int8, device=device) @@ -42,10 +36,7 @@ def test_int8_linear_matmul_out(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_linear_matmul.out, (A, B, out)) @pytest.mark.parametrize("threshold", [0.0, 6.0]) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) def test_int8_vectorwise_quant(self, threshold, device): A = torch.randn(10, 20, dtype=torch.float16, device=device) A[1][0] = 1000.0 @@ -71,10 +62,7 @@ def test_int8_vectorwise_quant(self, threshold, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_vectorwise_quant, (A, threshold)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) def test_int8_mm_dequant(self, device): A = torch.randint(-128, 127, (256, 256), dtype=torch.int32, device=device) row_stats = torch.randn(256, dtype=torch.float32, device=device) @@ -87,10 +75,7 @@ def test_int8_mm_dequant(self, device): torch.library.opcheck(torch.ops.bitsandbytes.int8_mm_dequant, (A, row_stats, col_stats)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("has_bias", TRUE_FALSE) def test_int8_scaled_mm(self, device, dtype, has_bias): @@ -109,10 +94,7 @@ def test_int8_scaled_mm(self, device, dtype, has_bias): class TestInt8BlockwiseQuantOps: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_quantize_blockwise(self, device, dtype, blocksize): @@ -136,10 +118,7 @@ def test_quantize_blockwise(self, device, dtype, blocksize): torch.library.opcheck(torch.ops.bitsandbytes.quantize_blockwise, (A, code, blocksize)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("blocksize", [64, 128, 256, 512] if not HIP_ENVIRONMENT else [128, 256, 512]) def test_dequantize_blockwise(self, device, dtype, blocksize): @@ -163,10 +142,7 @@ def test_dequantize_blockwise(self, device, dtype, blocksize): class Test4bitBlockwiseQuantOps: - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @@ -190,10 +166,7 @@ def test_quantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksize torch.library.opcheck(torch.ops.bitsandbytes.quantize_4bit, (A, blocksize, quant_type, storage_dtype)) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) @@ -230,10 +203,7 @@ def test_dequantize_4bit(self, device, dtype, storage_dtype, quant_type, blocksi torch.ops.bitsandbytes.dequantize_4bit.default, (A, absmax, blocksize, quant_type, shape, dtype) ) - @pytest.mark.parametrize( - "device", - [d for d in get_available_devices() if not (HIP_ENVIRONMENT and d == "cpu")], - ) + @pytest.mark.parametrize("device", get_available_devices()) @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) @pytest.mark.parametrize("storage_dtype", [torch.uint8, torch.bfloat16], ids=id_formatter("storage_dtype")) @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) From bdd67545ed1c66d1fc7cf5b84118b6bba107755e Mon Sep 17 00:00:00 2001 From: MISHANMAUYRA Date: Wed, 11 Jun 2025 15:18:19 +0530 Subject: [PATCH 13/13] Lint --- tests/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/helpers.py b/tests/helpers.py index 671ea39eb..54eec95dc 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -7,7 +7,7 @@ import torch -from bitsandbytes.cextension import HIP_ENVIRONMENT +from bitsandbytes.cextension import HIP_ENVIRONMENT test_dims_rng = random.Random(42)