快捷方式

AffineQuantizedTensor

class torchao.dtypes.AffineQuantizedTensor(tensor_impl: AQTTensorImpl, block_size: Tuple[int, ...], shape: Size, quant_min: Optional[Union[int, float]] = None, quant_max: Optional[Union[int, float]] = None, zero_point_domain: ZeroPointDomain = ZeroPointDomain.INT, dtype=None, strides=None)[源代码]

仿射量化张量子类。仿射量化意味着我们使用仿射变换对浮点张量进行量化:quantized_tensor = float_tensor / scale + zero_point

要了解在 choose_qparams、量化和反量化过程中仿射量化会发生什么,请查看 https://github.com/pytorch/ao/blob/main/torchao/quantization/quant_primitives.py,并检查三个量化原语操作:choose_qparams_affine、quantize_affine 和 dequantize_affine。

张量子类的形状和 dtype 表示张量子类在外部的外观,而不考虑内部表示的类型或方向。

fields
  • tensor_impl (AQTTensorImpl): 作为量化数据的通用张量实现存储的张量,

    例如,存储普通张量(int_data、scale、zero_point)或根据设备和运算符/内核打包的格式。

  • block_size (Tuple[int, …]): 量化的粒度,这意味着共享相同 qparam 的张量元素的尺寸。

    例如,当尺寸与输入张量维度相同时,我们使用每张量量化。

  • shape (torch.Size): 原始高精度张量的形状。

  • quant_min (Optional[int]): 张量的最小量化值,如果未指定,则将从 int_data 的 dtype 中派生。

  • quant_max (Optional[int]): 张量的最大量化值,如果未指定,则将从 int_data 的 dtype 中派生。

  • zero_point_domain (ZeroPointDomain): zero_point 所在的域,应为整数或浮点数。

    如果 zero_point 在整数域中,则在量化期间将 zero point 添加到量化整数值中。如果 zero_point 在浮点域中,则在量化期间将 zero point 从浮点值(未量化)中减去。默认为 ZeroPointDomain.INT。

  • dtype: 原始高精度张量的 dtype,例如 torch.float32。

dequantize() Tensor[源代码]

给定一个量化张量,对其进行去量化并返回去量化的浮点张量。

classmethod from_hp_to_floatx(input_float: Tensor, block_size: Tuple[int, ...], target_dtype: dtype, _layout: Layout, scale_dtype: Optional[dtype] = None)[源代码]

将高精度张量转换为 float8 量化张量。

classmethod from_hp_to_floatx_static(input_float: Tensor, scale: Tensor, block_size: Tuple[int, ...], target_dtype: dtype, _layout: Layout, scale_dtype: dtype = torch.float32)[源代码]

使用静态参数从高精度张量创建 float8 AffineQuantizedTensor。

classmethod from_hp_to_fpx(input_float: Tensor, _layout: Layout)[源代码]

从高精度张量创建 floatx AffineQuantizedTensor。Floatx 由 ebits 和 mbits 表示,并支持 float1-float7 的表示。

classmethod from_hp_to_intx(input_float: Tensor, mapping_type: MappingType, block_size: Tuple[int, ...], target_dtype: dtype, quant_min: Optional[int] = None, quant_max: Optional[int] = None, eps: Optional[float] = None, scale_dtype: Optional[dtype] = None, zero_point_dtype: Optional[dtype] = None, preserve_zero: bool = True, zero_point_domain: ZeroPointDomain = ZeroPointDomain.INT, _layout: Layout = PlainLayout(), use_hqq: bool = False)[源代码]

将高精度张量转换为整数仿射量化张量。

classmethod from_hp_to_intx_static(input_float: Tensor, scale: Tensor, zero_point: Optional[Tensor], block_size: Tuple[int, ...], target_dtype: dtype, quant_min: Optional[int] = None, quant_max: Optional[int] = None, zero_point_domain: ZeroPointDomain = ZeroPointDomain.INT, _layout: Layout = PlainLayout())[源代码]

使用静态参数从高精度张量创建整数 AffineQuantizedTensor。

to(*args, **kwargs) Tensor[源代码]

执行张量 dtype 和/或设备转换。torch.dtypetorch.device 将从 self.to(*args, **kwargs) 的参数中推断出来。

注意

如果 self 张量已经具有正确的 torch.dtypetorch.device,则返回 self。否则,返回的张量是 self 的副本,具有所需的 torch.dtypetorch.device

以下是调用 to 的方式:

to(dtype, non_blocking=False, copy=False, memory_format=torch.preserve_format) Tensor[源代码]

返回具有指定 dtype 的张量。

参数 (Args)

memory_format (torch.memory_format, optional): 返回张量的所需内存格式。默认值:torch.preserve_format

to(device=None, dtype=None, non_blocking=False, copy=False, memory_format=torch.preserve_format) Tensor[源代码]

返回具有指定 device 和(可选)dtype 的张量。如果 dtypeNone,则推断为 self.dtype。当 non_blocking 设置为 True 时,该函数尝试与主机异步执行转换(如果可能)。此异步行为适用于固定内存和页面内存。但是,在使用此功能时应谨慎。有关更多信息,请参阅 关于正确使用 non_blocking 和 pin_memory 的教程。当 copy 设置为 True 时,即使张量已满足所需的转换,也会创建一个新张量。

参数 (Args)

memory_format (torch.memory_format, optional): 返回张量的所需内存格式。默认值:torch.preserve_format

to(other, non_blocking=False, copy=False) Tensor[源]

返回与张量 other 具有相同 torch.dtypetorch.device 的张量。当 non_blocking 设置为 True 时,如果可能,该函数会尝试与主机进行异步转换。此异步行为适用于固定内存和分页内存。但是,使用此功能时请谨慎。有关更多信息,请参阅 关于 non_blocking 和 pin_memory 的正确使用教程。当 copy 被设置时,即使张量已经匹配所需的转换,也会创建一个新张量。

示例

>>> tensor = torch.randn(2, 2)  # Initially dtype=float32, device=cpu
>>> tensor.to(torch.float64)
tensor([[-0.5044,  0.0005],
        [ 0.3310, -0.0584]], dtype=torch.float64)

>>> cuda0 = torch.device('cuda:0')
>>> tensor.to(cuda0)
tensor([[-0.5044,  0.0005],
        [ 0.3310, -0.0584]], device='cuda:0')

>>> tensor.to(cuda0, dtype=torch.float64)
tensor([[-0.5044,  0.0005],
        [ 0.3310, -0.0584]], dtype=torch.float64, device='cuda:0')

>>> other = torch.randn((), dtype=torch.float64, device=cuda0)
>>> tensor.to(other, non_blocking=True)
tensor([[-0.5044,  0.0005],
        [ 0.3310, -0.0584]], dtype=torch.float64, device='cuda:0')

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源