快捷方式

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。

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[原始碼]

執行 Tensor 資料型別和/或裝置轉換。torch.dtypetorch.device 將從 self.to(*args, **kwargs) 的引數中推斷。

注意

如果 self Tensor 已經具有正確 torch.dtypetorch.device,則返回 self。否則,返回的張量是 self 的副本,具有所需的 torch.dtypetorch.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。如果 dtypeNone,則推斷為 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.dtypetorch.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')

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源