Home

Awesome

BitBLAS

BitBLAS is a library to support mixed-precision BLAS operations on GPUs, for example, the $W_{wdtype}A_{adtype}$ mixed-precision matrix multiplication where $C_{cdtype}[M, N] = A_{adtype}[M, K] \times W_{wdtype}[N, K]$. BitBLAS aims to support efficient mixed-precision DNN model deployment, especially the $W_{wdtype}A_{adtype}$ quantization in large language models (LLMs), for example, the $W_{UINT4}A_{FP16}$ in GPTQ, the $W_{INT2}A_{FP16}$ in BitDistiller, the $W_{INT2}A_{INT8}$ in BitNet-b1.58. BitBLAS is based on techniques from our paper "Ladder: Enabling Efficient Low-Precision Deep Learning Computing through Hardware-aware Tensor Transformation" at OSDI'24.

Some of the key features of BitBLAS include:

Latest News

Integration Example of FasterTransformer with BitBLAS

FasterTransformer Integration

Benchmark Summary

BitBLAS achieves exceptional performance across a variety of computational patterns. Below are selected results showcasing its capabilities:

For more detailed information on benchmark sets with other formats (NF4/FP4) and other devices (RTX 3090), please refer to the benchmark.

Support Matrix

A_dtypeW_dtypeAccum_dtypeOut_dtypeBitBLAS SupportTested Platform
BF16BF16FP32FP16√A100(SM_80)/A6000(SM_86)
BF16FP4_E2M1FP32FP16√A100(SM_80)/A6000(SM_86)
BF16FP8_E4M3FP32FP16√A100(SM_80)/A6000(SM_86)
BF16INT8FP32FP16√A100(SM_80)/A6000(SM_86)
BF16UINT4/INT4FP32FP16√A100(SM_80)/A6000(SM_86)
BF16UINT2/INT2FP32FP16√A100(SM_80)/A6000(SM_86)
BF16UINT1FP32FP16√A100(SM_80)/A6000(SM_86)
BF16NF4FP32FP16√A100(SM_80)/A6000(SM_86)
FP16FP16FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP16FP4_E2M1FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP16FP8_E4M3FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP16INT8FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP16UINT4/INT4FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP16UINT2/INT2FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP16UINT1FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP16NF4FP32/FP16FP16√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
INT8INT8INT32FP32/INT32/FP16/INT8√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
INT8UINT4/INT4INT32FP32/INT32/FP16/INT8√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
INT8UINT2/INT2INT32FP32/INT32/FP16/INT8√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
INT8UINT1INT32FP32/INT32/FP16/INT8√V100(SM_70)/A100(SM_80)/A6000(SM_86)/RTX 4090(SM_89)
FP8_E4M3FP8_E4M3FP32FP32/FP16√RTX 4090(SM_89)
FP8_E5M2FP8_E5M2FP32FP32/FP16√RTX 4090(SM_89)

We are continuously expanding the support matrix. If you have any specific requirements, please feel free to open an issue or PR.

Getting Started with an Example

Installing with pip

Prerequisites for installation via wheel or PyPI

The easiest way to install BitBLAS is direcly from the PyPi using pip. To install the latest version, run the following command in your terminal.

pip install bitblas

Alternatively, to install the latest version of BitBLAS from the github repository, you can run the following command:

pip install git+https://github.com/microsoft/BitBLAS.git

After installing BitBLAS, you can verify the installation by running:

python -c "import bitblas; print(bitblas.__version__)"  

Note: Currently, BitBLAS whl is only supported on Ubuntu 20.04 or later version as we build the whl files on this platform. Currently we only provide whl files for CUDA>=11.0 and with Python>=3.8. If you are using a different platform or environment, you may need to build BitBLAS from source. More installation methods can be found in the installation document.

Example: $W_{INT4}A_{FP16}$ mixed-precision matrix multiplication

BitBLAS provides two Python APIs to perform mixed-precision matrix multiplication:

Here is an example for a $W_{INT4}A_{FP16}$ mixed-precision matrix multiplication: $out_{FP16}[M, N] = A_{FP16}[M, K] \times W_{INT4}[N, K]$, this example includes the creation of input matrices, quantization of weight matrices, and execution of the matrix multiplication with the bitblas.Matmul API. The result is then compared against a reference result obtained through conventional methods to ensure accuracy.

import bitblas
import torch

# uncomment to enable debug output
# bitblas.set_log_level("Debug")

matmul_config = bitblas.MatmulConfig(
    M=1,  # M dimension
    N=2048,  # N dimension
    K=1024,  # K dimension
    A_dtype="float16",  # activation A dtype
    W_dtype="int4",  # weight W dtype
    accum_dtype="float16",  # accumulation dtype
    out_dtype="float16",  # output dtype
    layout="nt",  # matrix layout, "nt" indicates the layout of A is non-transpose and the layout of W is transpose
    with_bias=False,  # bias
    # configs for weight only quantization
    group_size=None,  # setting for grouped quantization
    with_scaling=False,  # setting for scaling factor
    with_zeros=False,  # setting for zeros
    zeros_mode=None,  # setting for how to calculating zeros
)

matmul = bitblas.Matmul(config=matmul_config)

# Create input matrices
input_tensor = torch.rand((1, 1024), dtype=torch.float16).cuda()
weight_tensor = torch.randint(0, 7, (2048, 1024), dtype=torch.int8).cuda()

# Transform weight tensor to int4 data type
weight_tensor_int4 = matmul.transform_weight(weight_tensor)

# Perform mixed-precision matrix multiplication
output_tensor = matmul(input_tensor, weight_tensor_int4)

# Reference result using PyTorch matmul for comparison
ref_result = torch.matmul(input_tensor, weight_tensor.t().to(torch.float16))
# Assert that the results are close within a specified tolerance, note that the int4 randint value is a little bigger than the float16 value, so we set the atol to 1.0
print("Ref output:", ref_result)
print("BitBLAS output:", output_tensor)
torch.testing.assert_close(output_tensor, ref_result, rtol=1e-2, atol=1e-0)

Note: More examples can be found in the QuickStart document.

Documents

Reference

Please cite BitBLAS/Ladder in your publications if it helps your research:

@inproceedings {ladder-osdi24,
author = {Lei Wang and Lingxiao Ma and Shijie Cao and Quanlu Zhang and Jilong Xue and Yining Shi and Ningxin Zheng and Ziming Miao and Fan Yang and Ting Cao and Yuqing Yang and Mao Yang},
title = {Ladder: Enabling Efficient Low-Precision Deep Learning Computing through Hardware-aware Tensor Transformation},
booktitle = {18th USENIX Symposium on Operating Systems Design and Implementation (OSDI 24)},
year = {2024},
isbn = {978-1-939133-40-3},
address = {Santa Clara, CA},
pages = {307--323},
url = {https://www.usenix.org/conference/osdi24/presentation/wang-lei},
publisher = {USENIX Association},
month = jul
}

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.