Dequantize a tensor in blocks of values. The input tensor is dequantized by dividing it into blocks of `blocksize` values. The 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,
code: Optional[torch.Tensor] = None,
out: Optional[torch.Tensor] = None,
blocksize: int = 4096,
nested=False,
)
| 687 | |
| 688 | |
| 689 | def dequantize_blockwise( |
| 690 | A: torch.Tensor, |
| 691 | quant_state: Optional[QuantState] = None, |
| 692 | absmax: Optional[torch.Tensor] = None, |
| 693 | code: Optional[torch.Tensor] = None, |
| 694 | out: Optional[torch.Tensor] = None, |
| 695 | blocksize: int = 4096, |
| 696 | nested=False, |
| 697 | ) -> torch.Tensor: |
| 698 | """Dequantize a tensor in blocks of values. |
| 699 | |
| 700 | The input tensor is dequantized by dividing it into blocks of `blocksize` values. |
| 701 | The the absolute maximum value within these blocks is used for scaling |
| 702 | the non-linear dequantization. |
| 703 | |
| 704 | Args: |
| 705 | A (`torch.Tensor`): The quantized input tensor. |
| 706 | quant_state ([`QuantState`], *optional*): |
| 707 | The quantization state as returned by [`quantize_blockwise`]. |
| 708 | Required if `absmax` is not provided. |
| 709 | absmax (`torch.Tensor`, *optional*): |
| 710 | A tensor containing the scaling values. |
| 711 | Required if `quant_state` is not provided and ignored otherwise. |
| 712 | code (`torch.Tensor`, *optional*): |
| 713 | A mapping describing the low-bit data type. Defaults to a signed 8-bit dynamic type. |
| 714 | For more details, see (8-Bit Approximations for Parallelism in Deep Learning)[https://arxiv.org/abs/1511.04561]. |
| 715 | Ignored when `quant_state` is provided. |
| 716 | out (`torch.Tensor`, *optional*): A tensor to use to store the result. |
| 717 | blocksize (`int`, *optional*): |
| 718 | The size of the blocks. Defaults to 4096. |
| 719 | Valid values are 64, 128, 256, 512, 1024, 2048, and 4096. |
| 720 | Ignored when `quant_state` is provided. |
| 721 | |
| 722 | Raises: |
| 723 | ValueError: Raised when the input data type is not supported. |
| 724 | |
| 725 | Returns: |
| 726 | `torch.Tensor`: |
| 727 | The dequantized tensor. The datatype is indicated by `quant_state.dtype` and defaults to `torch.float32`. |
| 728 | """ |
| 729 | |
| 730 | if quant_state is None and absmax is None: |
| 731 | raise ValueError("dequantize_blockwise requires either quant_state or absmax") |
| 732 | if A.dtype != torch.uint8: |
| 733 | raise ValueError(f"A must be uint8, got {A.dtype}") |
| 734 | if code is None and quant_state is None: |
| 735 | if "dynamic" not in name2qmap: |
| 736 | name2qmap["dynamic"] = create_dynamic_map().to(A.device) |
| 737 | code = name2qmap["dynamic"] |
| 738 | |
| 739 | if quant_state is None: |
| 740 | quant_state = QuantState(absmax=absmax, code=code, blocksize=blocksize, dtype=torch.float32) |
| 741 | |
| 742 | if quant_state.blocksize <= 0: |
| 743 | raise ValueError(f"blocksize must be positive, got {quant_state.blocksize}") |
| 744 | |
| 745 | absmax = quant_state.absmax |
| 746 | if quant_state.nested: |
no test coverage detected