|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | +from ctypes import c_float, c_int32, cast, POINTER, pointer |
| 9 | +from typing import Tuple |
| 10 | + |
| 11 | +import hypothesis.strategies as st |
| 12 | +import numpy as np |
| 13 | +import torch |
| 14 | +from hypothesis import given, HealthCheck, settings |
| 15 | + |
| 16 | + |
| 17 | +try: |
| 18 | + # pyre-ignore[21] |
| 19 | + from fbgemm_gpu import open_source # noqa: F401 |
| 20 | + |
| 21 | +except Exception: |
| 22 | + if torch.version.hip: |
| 23 | + torch.ops.load_library("//deeplearning/fbgemm/fbgemm_gpu:sparse_ops_hip") |
| 24 | + else: |
| 25 | + torch.ops.load_library("//deeplearning/fbgemm/fbgemm_gpu:sparse_ops") |
| 26 | + |
| 27 | + torch.ops.load_library("//deeplearning/fbgemm/fbgemm_gpu:sparse_ops_cpu") |
| 28 | + |
| 29 | + |
| 30 | +class SparseNNOperatorsGPUTest(unittest.TestCase): |
| 31 | + # pyre-fixme[56]: Pyre was not able to infer the type of argument |
| 32 | + # `hypothesis.strategies.sampled_from(["BF16"])` to decorator factory |
| 33 | + # `hypothesis.given`. |
| 34 | + @given( |
| 35 | + precision=st.just("BF16"), |
| 36 | + batch_size=st.integers(min_value=1, max_value=256), |
| 37 | + k=st.integers(min_value=2, max_value=2), |
| 38 | + n=st.integers(min_value=2, max_value=2), |
| 39 | + ) |
| 40 | + def test_dense_mlp_quantize_ops( |
| 41 | + self, precision: str, batch_size: int, k: int, n: int |
| 42 | + ) -> None: |
| 43 | + if precision == "BF16": |
| 44 | + input_data = torch.rand((n, k), dtype=torch.float32) |
| 45 | + quantized_data = torch.ops.fbgemm.FloatToBfloat16Quantized(input_data) |
| 46 | + dequantized_data = torch.ops.fbgemm.Bfloat16QuantizedToFloat(quantized_data) |
| 47 | + torch.testing.assert_close( |
| 48 | + dequantized_data, input_data, rtol=1e-2, atol=1e-2 |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def bfloat_quantize(x_float: float) -> np.uint16: |
| 53 | + bits = cast(pointer(c_float(x_float)), POINTER(c_int32)).contents.value |
| 54 | + bits += 1 << 15 |
| 55 | + bits = bits >> 16 |
| 56 | + bits = np.uint16(bits) |
| 57 | + return bits |
| 58 | + |
| 59 | + |
| 60 | +def bfloat_dequantize(x_bfloat: np.uint16) -> float: |
| 61 | + bits = np.int32(x_bfloat) << 16 |
| 62 | + return cast(pointer(c_int32(bits)), POINTER(c_float)).contents.value |
| 63 | + |
| 64 | + |
| 65 | +class TestBfloat16QuantizationConversion(unittest.TestCase): |
| 66 | + # pyre-fixme[56]: Pyre was not able to infer the type of argument |
| 67 | + # `hypothesis.strategies.integers($parameter$min_value = 0, $parameter$max_value = |
| 68 | + # 100)` to decorator factory `hypothesis.given`. |
| 69 | + @given( |
| 70 | + nrows=st.integers(min_value=0, max_value=100), |
| 71 | + ncols=st.integers(min_value=0, max_value=100), |
| 72 | + ) |
| 73 | + @settings(deadline=10000, suppress_health_check=[HealthCheck.filter_too_much]) |
| 74 | + def test_quantize_op(self, nrows: int, ncols: int) -> None: |
| 75 | + input_data = torch.rand(nrows, ncols).float() |
| 76 | + quantized_data = torch.ops.fbgemm.FloatToBfloat16Quantized(input_data) |
| 77 | + if nrows == 0 or ncols == 0: |
| 78 | + assert quantized_data.numel() == 0 |
| 79 | + return |
| 80 | + f = np.vectorize(lambda x: bfloat_quantize(x)) |
| 81 | + reference = f(input_data.numpy()) |
| 82 | + quantized_data_uint16 = quantized_data.numpy() |
| 83 | + quantized_data_uint16.dtype = np.uint16 |
| 84 | + np.testing.assert_array_almost_equal(quantized_data_uint16, reference) |
| 85 | + |
| 86 | + if torch.cuda.is_available(): |
| 87 | + input_data_gpu = input_data.cuda() |
| 88 | + quantized_data_gpu = torch.ops.fbgemm.FloatToBfloat16Quantized( |
| 89 | + input_data_gpu |
| 90 | + ) |
| 91 | + quantized_data_numpy = quantized_data_gpu.cpu().numpy() |
| 92 | + quantized_data_numpy.dtype = np.uint16 |
| 93 | + np.testing.assert_allclose(quantized_data_numpy, reference) |
| 94 | + |
| 95 | + # pyre-fixme[56]: Pyre was not able to infer the type of argument |
| 96 | + # `hypothesis.strategies.integers($parameter$min_value = 0, $parameter$max_value = |
| 97 | + # 100)` to decorator factory `hypothesis.given`. |
| 98 | + @given( |
| 99 | + nrows=st.integers(min_value=0, max_value=100), |
| 100 | + ncols=st.integers(min_value=0, max_value=100), |
| 101 | + ) |
| 102 | + @settings(deadline=10000, suppress_health_check=[HealthCheck.filter_too_much]) |
| 103 | + def test_quantize_and_dequantize_op(self, nrows: int, ncols: int) -> None: |
| 104 | + input_data = torch.rand(nrows, ncols).float() |
| 105 | + quantized_data = torch.ops.fbgemm.FloatToBfloat16Quantized(input_data) |
| 106 | + dequantized_data = torch.ops.fbgemm.Bfloat16QuantizedToFloat(quantized_data) |
| 107 | + if nrows == 0 or ncols == 0: |
| 108 | + assert dequantized_data.numel() == 0 |
| 109 | + return |
| 110 | + f = np.vectorize(lambda x: bfloat_quantize(x)) |
| 111 | + ref_bfloat16 = f(input_data.numpy()) |
| 112 | + f = np.vectorize(lambda x: bfloat_dequantize(x)) |
| 113 | + ref_fp32 = torch.from_numpy(f(ref_bfloat16)).float() |
| 114 | + torch.testing.assert_close(dequantized_data, ref_fp32) |
| 115 | + |
| 116 | + if torch.cuda.is_available(): |
| 117 | + input_data_gpu = input_data.cuda() |
| 118 | + quantized_data_gpu = torch.ops.fbgemm.FloatToBfloat16Quantized( |
| 119 | + input_data_gpu |
| 120 | + ) |
| 121 | + dequantized_data_gpu = torch.ops.fbgemm.Bfloat16QuantizedToFloat( |
| 122 | + quantized_data_gpu |
| 123 | + ) |
| 124 | + # compare quantized data |
| 125 | + torch.testing.assert_close(dequantized_data_gpu.cpu(), ref_fp32) |
| 126 | + |
| 127 | + @unittest.skipIf(not torch.cuda.is_available(), "Skip when CUDA is not available") |
| 128 | + # pyre-fixme[56]: Pyre was not able to infer the type of argument |
| 129 | + # `hypothesis.strategies.sampled_from([(65540, 256), (256, 65540)])` to decorator |
| 130 | + # factory `hypothesis.given`. |
| 131 | + @given( |
| 132 | + ncols_nrows=st.sampled_from([(65540, 256), (256, 65540)]), |
| 133 | + ) |
| 134 | + @settings(deadline=10000, suppress_health_check=[HealthCheck.filter_too_much]) |
| 135 | + def test_quantize_and_dequantize_op_cuda_large_nrows_bf16( |
| 136 | + self, ncols_nrows: Tuple[int, int] |
| 137 | + ) -> None: |
| 138 | + ncols, nrows = ncols_nrows |
| 139 | + input_data = torch.rand(nrows, ncols).float() |
| 140 | + quantized_data = torch.ops.fbgemm.FloatToBfloat16Quantized(input_data) |
| 141 | + dequantized_data = torch.ops.fbgemm.Bfloat16QuantizedToFloat(quantized_data) |
| 142 | + |
| 143 | + if torch.cuda.is_available(): |
| 144 | + input_data_gpu = input_data.cuda() |
| 145 | + quantized_data_gpu = torch.ops.fbgemm.FloatToBfloat16Quantized( |
| 146 | + input_data_gpu |
| 147 | + ) |
| 148 | + dequantized_data_gpu = torch.ops.fbgemm.Bfloat16QuantizedToFloat( |
| 149 | + quantized_data_gpu |
| 150 | + ) |
| 151 | + # compare quantized data |
| 152 | + torch.testing.assert_close(dequantized_data_gpu.cpu(), dequantized_data) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + unittest.main() |
0 commit comments