| 94 | |
| 95 | |
| 96 | class TestInt8BlockwiseQuantOps: |
| 97 | @pytest.mark.parametrize("device", get_available_devices()) |
| 98 | @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) |
| 99 | @pytest.mark.parametrize("blocksize", [64, 128, 256, 512]) |
| 100 | def test_quantize_blockwise(self, device, dtype, blocksize): |
| 101 | if device == "cpu" and blocksize != 256: |
| 102 | pytest.skip("CPU implementation is slow; only test blocksize=256") |
| 103 | |
| 104 | code = bitsandbytes.functional.create_dynamic_map().to(device) |
| 105 | A = torch.randn(1024, 1024, dtype=dtype, device=device) |
| 106 | out, absmax = torch.ops.bitsandbytes.quantize_blockwise(A, code, blocksize) |
| 107 | |
| 108 | assert out.shape == A.shape |
| 109 | assert out.dtype == torch.uint8 |
| 110 | assert out.device == A.device |
| 111 | |
| 112 | assert absmax.device == A.device |
| 113 | assert absmax.dtype == torch.float32 |
| 114 | |
| 115 | opcheck(torch.ops.bitsandbytes.quantize_blockwise, (A, code, blocksize)) |
| 116 | |
| 117 | @pytest.mark.parametrize("device", get_available_devices()) |
| 118 | @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32], ids=id_formatter("dtype")) |
| 119 | @pytest.mark.parametrize("blocksize", [64, 128, 256, 512]) |
| 120 | def test_dequantize_blockwise(self, device, dtype, blocksize): |
| 121 | A = torch.randint(0, 255, (1024, 1024), dtype=torch.uint8, device=device) |
| 122 | code = bitsandbytes.functional.create_dynamic_map().to(device, dtype=torch.float32) |
| 123 | |
| 124 | n = A.numel() |
| 125 | blocks = -(n // -blocksize) |
| 126 | absmax = torch.randn((blocks,), device=device, dtype=torch.float32) |
| 127 | |
| 128 | out = torch.ops.bitsandbytes.dequantize_blockwise.default(A, absmax, code, blocksize, dtype) |
| 129 | |
| 130 | assert out.shape == A.shape |
| 131 | assert out.dtype == dtype |
| 132 | assert out.device == A.device |
| 133 | |
| 134 | opcheck(torch.ops.bitsandbytes.dequantize_blockwise.default, (A, absmax, code, blocksize, dtype)) |
| 135 | |
| 136 | |
| 137 | class Test4bitBlockwiseQuantOps: |
nothing calls this directly
no outgoing calls
no test coverage detected