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
張量子類的形狀和資料型別表示張量子類的外部外觀,而與內部表示的型別或方向無關。
- 欄位
- tensor_impl (AQTTensorImpl): 用作量化資料的通用張量實現儲存的張量,
例如,根據裝置和運算子/核心儲存普通張量(int_data、scale、zero_point)或打包格式。
- block_size (Tuple[int, …]): 量化的粒度,這意味著共享相同 qparam 的張量元素的尺寸。
例如,當尺寸與輸入張量維度相同時,我們使用每張量量化。
shape (torch.Size): 原始高精度 Tensor 的形狀。
quant_min (Optional[int]): Tensor 的最小量化值,如果未指定,則將從 int_data 的資料型別中派生。
quant_max (Optional[int]): Tensor 的最大量化值,如果未指定,則將從 int_data 的資料型別中派生。
- zero_point_domain (ZeroPointDomain): zero_point 所在的域,應為整數或浮點數。
如果 zero_point 在整數域中,則在量化過程中將 zero_point 加到量化後的整數值上;如果 zero_point 在浮點域中,則在量化過程中從浮點值(未量化)中減去 zero_point。 預設值為 ZeroPointDomain.INT。
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[原始碼]¶
執行 Tensor 資料型別和/或裝置轉換。
torch.dtype和torch.device將從self.to(*args, **kwargs)的引數中推斷。注意
如果
selfTensor 已經具有正確torch.dtype和torch.device,則返回self。否則,返回的張量是self的副本,具有所需的torch.dtype和torch.device。注意
如果
self需要梯度 (requires_grad=True) 但指定的dtype是整數型別,則返回的張量將隱式設定requires_grad=False。這是因為只有具有浮點或複數資料型別的張量才能需要梯度。以下是呼叫
to的方法:- to(dtype, non_blocking=False, copy=False, memory_format=torch.preserve_format) Tensor[原始碼]
返回具有指定
dtype的 Tensor。- 引數 (Args)
memory_format (
torch.memory_format, optional): 返回的 Tensor 的所需記憶體格式。預設值:torch.preserve_format。
注意
根據 C++ 型別轉換規則,將浮點值轉換為整數型別將截斷小數部分。如果截斷後的值無法放入目標型別(例如,將
torch.inf轉換為torch.long),則行為未定義,結果可能因平臺而異。- to(device=None, dtype=None, non_blocking=False, copy=False, memory_format=torch.preserve_format) Tensor[原始碼]
返回具有指定
device和(可選)dtype的 Tensor。如果dtype為None,則推斷為self.dtype。當non_blocking設定為True時,如果可能,該函式會嘗試非同步執行轉換(相對於主機)。此非同步行為適用於固定記憶體和分頁記憶體。但是,在使用此功能時應謹慎。有關更多資訊,請參閱 關於正確使用 non_blocking 和 pin_memory 的教程。當copy設定為 True 時,即使張量已匹配所需的轉換,也會建立一個新的張量。- 引數 (Args)
memory_format (
torch.memory_format, optional): 返回的 Tensor 的所需記憶體格式。預設值:torch.preserve_format。
- to(other, non_blocking=False, copy=False) Tensor[原始碼]
返回與張量
other具有相同torch.dtype和torch.device的 Tensor。當non_blocking設定為True時,如果可能,該函式會嘗試非同步執行轉換(相對於主機)。此非同步行為適用於固定記憶體和分頁記憶體。但是,在使用此功能時應謹慎。有關更多資訊,請參閱 關於正確使用 non_blocking 和 pin_memory 的教程。當copy設定為 True 時,即使張量已匹配所需的轉換,也會建立一個新的張量。
示例
>>> 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')