Quantize tensor A in blocks of 4-bit values. Quantizes tensor A by dividing it into blocks which are independently quantized. Args: A (`torch.Tensor`): The input tensor. Supports `float16`, `bfloat16`, or `float32` datatypes. absmax (`torch.Tensor`, *optional*): A tensor to
(
A: torch.Tensor,
absmax: Optional[torch.Tensor] = None,
out: Optional[torch.Tensor] = None,
blocksize=None,
compress_statistics=False,
quant_type="fp4",
quant_storage=torch.uint8,
)
| 882 | |
| 883 | |
| 884 | def quantize_4bit( |
| 885 | A: torch.Tensor, |
| 886 | absmax: Optional[torch.Tensor] = None, |
| 887 | out: Optional[torch.Tensor] = None, |
| 888 | blocksize=None, |
| 889 | compress_statistics=False, |
| 890 | quant_type="fp4", |
| 891 | quant_storage=torch.uint8, |
| 892 | ) -> tuple[torch.Tensor, QuantState]: |
| 893 | """Quantize tensor A in blocks of 4-bit values. |
| 894 | |
| 895 | Quantizes tensor A by dividing it into blocks which are independently quantized. |
| 896 | |
| 897 | Args: |
| 898 | A (`torch.Tensor`): The input tensor. Supports `float16`, `bfloat16`, or `float32` datatypes. |
| 899 | absmax (`torch.Tensor`, *optional*): A tensor to use to store the absmax values. |
| 900 | out (`torch.Tensor`, *optional*): A tensor to use to store the result. |
| 901 | blocksize (`int`, *optional*): |
| 902 | The size of the blocks. Defaults to 64. |
| 903 | Valid values are 32, 64, 128, 256, 512, 1024, 2048, and 4096. |
| 904 | compress_statistics (`bool`, *optional*): Whether to additionally quantize the absmax values. Defaults to False. |
| 905 | quant_type (`str`, *optional*): The data type to use: `nf4` or `fp4`. Defaults to `fp4`. |
| 906 | quant_storage (`torch.dtype`, *optional*): The dtype of the tensor used to store the result. Defaults to `torch.uint8`. |
| 907 | |
| 908 | Raises: |
| 909 | ValueError: Raised when the input data type is not supported. |
| 910 | |
| 911 | Returns: |
| 912 | Tuple[`torch.Tensor`, `QuantState`]: A tuple containing the quantization results. |
| 913 | - `torch.Tensor`: The quantized tensor with packed 4-bit values. |
| 914 | - [`QuantState`]: The state object used to undo the quantization. |
| 915 | """ |
| 916 | |
| 917 | if blocksize is None: |
| 918 | blocksize = 64 |
| 919 | |
| 920 | if blocksize not in (32, 64, 128, 256, 512, 1024, 2048, 4096): |
| 921 | raise ValueError(f"invalid blocksize {blocksize}") |
| 922 | if quant_type not in ("nf4", "fp4"): |
| 923 | raise ValueError(f"quant_type must be 'nf4' or 'fp4', got {quant_type!r}") |
| 924 | if A.dtype not in (torch.bfloat16, torch.float16, torch.float32): |
| 925 | raise ValueError(f"Blockwise 4bit quantization only supports 16/32-bit floats, but got {A.dtype}") |
| 926 | |
| 927 | input_shape = A.shape |
| 928 | |
| 929 | _out, _absmax = torch.ops.bitsandbytes.quantize_4bit.default( |
| 930 | A, |
| 931 | blocksize, |
| 932 | quant_type, |
| 933 | quant_storage, |
| 934 | ) |
| 935 | |
| 936 | code = get_4bit_type(quant_type, device=A.device) |
| 937 | |
| 938 | if compress_statistics: |
| 939 | offset = _absmax.mean() |
| 940 | qabsmax, state2 = quantize_blockwise(_absmax - offset, blocksize=256) |
| 941 | del _absmax |
no test coverage detected