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)[source]¶
- 仿射量化张量子类。仿射量化意味着我们使用仿射变换量化浮点张量
量化张量 = 浮点张量 / 比例 + 零点
要查看在 choose_qparams、量化和反量化期间仿射量化会发生什么,请查看 https://github.com/pytorch/ao/blob/main/torchao/quantization/quant_primitives.py 并检查三个量化原语操作:choose_qparams_affine、quantize_affine 和 dequantize_affine
张量子类的形状和 dtype 表示张量子类在外部的呈现方式,无论内部表示的类型或方向如何。
- 字段
- tensor_impl (AQTTensorImpl): 用作量化数据通用张量 impl 存储的张量,
例如,根据设备和运算符/内核存储普通张量(int_data、比例、零点)或打包格式
- 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): 零点所在的域,应为整数或浮点数
如果零点在整数域中,则在量化期间将零点添加到量化的整数值;如果零点在浮点域中,则在量化期间从浮点(未量化)值中减去零点;默认值为 ZeroPointDomain.INT
dtype: 原始高精度张量的 dtype,例如 torch.float32
- to(*args, **kwargs) Tensor [source]¶
执行张量 dtype 和/或设备转换。
torch.dtype
和torch.device
是从self.to(*args, **kwargs)
的参数中推断出来的。注意
如果
self
张量已具有正确的torch.dtype
和torch.device
,则返回self
。 否则,返回的张量是具有所需torch.dtype
和torch.device
的self
的副本。以下是调用
to
的方法- to(dtype, non_blocking=False, copy=False, memory_format=torch.preserve_format) Tensor [source]
返回具有指定
dtype
的张量- 参数
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 [source]
返回具有指定
device
和(可选)dtype
的张量。 如果dtype
为None
,则推断为self.dtype
。 当non_blocking
时,如果可能,尝试相对于主机异步转换,例如,将具有固定内存的 CPU 张量转换为 CUDA 张量。 当设置copy
时,即使张量已与所需的转换匹配,也会创建一个新张量。- 参数
memory_format (
torch.memory_format
, optional): 返回张量的所需内存格式。 默认值:torch.preserve_format
。
- to(other, non_blocking=False, copy=False) Tensor [source]
返回与张量
other
具有相同torch.dtype
和torch.device
的张量。 当non_blocking
时,如果可能,尝试相对于主机异步转换,例如,将具有固定内存的 CPU 张量转换为 CUDA 张量。 当设置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')