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。
- 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.dtype
和torch.device
将从self.to(*args, **kwargs)
的参数中推断出来。注意
如果
self
张量已经具有正确的torch.dtype
和torch.device
,则返回self
。否则,返回的张量是self
的副本,具有所需的torch.dtype
和torch.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
的张量。如果dtype
为None
,则推断为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.dtype
和torch.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')