Dequantizes a packed 4-bit quantized tensor. The input tensor is dequantized by dividing it into blocks of `blocksize` values. The absolute maximum value within these blocks is used for scaling the non-linear dequantization. Args: A (`torch.Tensor`): The quantized input ten
(
A: torch.Tensor,
quant_state: Optional[QuantState] = None,
absmax: Optional[torch.Tensor] = None,
out: Optional[torch.Tensor] = None,
blocksize: Optional[int] = None,
quant_type="fp4",
)
| 990 | |
| 991 | |
| 992 | def dequantize_4bit( |
| 993 | A: torch.Tensor, |
| 994 | quant_state: Optional[QuantState] = None, |
| 995 | absmax: Optional[torch.Tensor] = None, |
| 996 | out: Optional[torch.Tensor] = None, |
| 997 | blocksize: Optional[int] = None, |
| 998 | quant_type="fp4", |
| 999 | ) -> torch.Tensor: |
| 1000 | """Dequantizes a packed 4-bit quantized tensor. |
| 1001 | |
| 1002 | The input tensor is dequantized by dividing it into blocks of `blocksize` values. |
| 1003 | The absolute maximum value within these blocks is used for scaling |
| 1004 | the non-linear dequantization. |
| 1005 | |
| 1006 | Args: |
| 1007 | A (`torch.Tensor`): The quantized input tensor. |
| 1008 | quant_state ([`QuantState`], *optional*): |
| 1009 | The quantization state as returned by [`quantize_4bit`]. |
| 1010 | Required if `absmax` is not provided. |
| 1011 | absmax (`torch.Tensor`, *optional*): |
| 1012 | A tensor containing the scaling values. |
| 1013 | Required if `quant_state` is not provided and ignored otherwise. |
| 1014 | out (`torch.Tensor`, *optional*): A tensor to use to store the result. |
| 1015 | blocksize (`int`, *optional*): |
| 1016 | The size of the blocks. Defaults to 64. |
| 1017 | Valid values are 32, 64, 128, 256, 512, 1024, 2048, and 4096. |
| 1018 | quant_type (`str`, *optional*): The data type to use: `nf4` or `fp4`. Defaults to `fp4`. |
| 1019 | |
| 1020 | Raises: |
| 1021 | ValueError: Raised when the input data type or blocksize is not supported. |
| 1022 | |
| 1023 | Returns: |
| 1024 | `torch.Tensor`: The dequantized tensor. |
| 1025 | """ |
| 1026 | |
| 1027 | if blocksize is None: |
| 1028 | blocksize = 64 |
| 1029 | |
| 1030 | if quant_state is None: |
| 1031 | if absmax is None or out is None: |
| 1032 | raise ValueError("dequantize_4bit requires both absmax and out when quant_state is not provided") |
| 1033 | |
| 1034 | quant_state = QuantState( |
| 1035 | absmax=absmax, |
| 1036 | shape=out.shape, |
| 1037 | dtype=out.dtype, |
| 1038 | blocksize=blocksize, |
| 1039 | quant_type=quant_type, |
| 1040 | ) |
| 1041 | |
| 1042 | else: |
| 1043 | absmax = quant_state.absmax |
| 1044 | |
| 1045 | if quant_state.blocksize not in (32, 64, 128, 256, 512, 1024, 2048, 4096): |
| 1046 | raise ValueError(f"invalid blocksize {quant_state.blocksize}") |
| 1047 | if quant_state.quant_type not in ("nf4", "fp4"): |
| 1048 | raise ValueError(f"quant_type must be 'nf4' or 'fp4', got {quant_state.quant_type!r}") |
| 1049 | if quant_state.dtype not in (torch.bfloat16, torch.float16, torch.float32): |
no test coverage detected