Skip to content

torch_to_nnef.tensor.quant.base

Classes:

Name Description
QScalePerGroupF16

f16 scale only per group.

QScheme
QTensor

Common interface for all Compressed storage.

U8Compressor

Abstract class to add u8 compression methods.

Functions:

Name Description
qscale_per_group_f16_min_max_calibration

Build QScalePerGroupF16 and calibrate requested float tensor.

QScalePerGroupF16

QScalePerGroupF16(group_size: int, scale: Tensor, n_bits: int, restricted_range: bool = False)

Bases: QScheme

f16 scale only per group.

Tract aligned using negative scales.

QScheme

Bases: ABC

Methods:

Name Description
to_device

Specific device handling.

to_device

to_device(new_device)

Specific device handling.

Each QScheme may implement support for specific device switching for internal quant/dequant (like GPU, ...) allowing faster computation

QTensor

QTensor(fp_tensor: Tensor, qscheme: QScheme, dequant_to_dtype=torch.float32, u8_compressors: Optional[List[U8Compressor]] = None)

Bases: OpaqueTensor

Common interface for all Compressed storage.

Methods:

Name Description
cpu

Move to host memory and stay quantized, as to("cpu") does.

cuda

Move to a CUDA device and stay quantized, as to(device) does.

to_device

Specific device handling.

write_in_file

Called at NNEF write time.

xpu

Move to an XPU device and stay quantized, as to(device) does.

cpu

cpu(memory_format=torch.preserve_format)

Move to host memory and stay quantized, as to("cpu") does.

Without this override the call reaches __torch_function__, which returns the dequantized torch.Tensor instead, so nn.Module.cpu cannot move a quantized parameter. memory_format is accepted for signature compatibility only; the packed codes keep their layout.

cuda

cuda(device=None, non_blocking=False, memory_format=torch.preserve_format)

Move to a CUDA device and stay quantized, as to(device) does.

device is a device, an index or None for the current CUDA device. non_blocking and memory_format are accepted for signature compatibility only.

to_device

to_device(new_device)

Specific device handling.

write_in_file

write_in_file(dirpath: Union[str, Path], label: str)

Called at NNEF write time.

Each specific inference engine format should implement the file dump prefered.

xpu

xpu(device=None, non_blocking=False, memory_format=torch.preserve_format)

Move to an XPU device and stay quantized, as to(device) does.

device is a device, an index or None for the current XPU device. non_blocking and memory_format are accepted for signature compatibility only.

U8Compressor

Abstract class to add u8 compression methods.

This can be used to

Apply bitpack elements bellow 8bit Apply classic compression algorithm

Warning !! .shape of u8_tensor compressed must be same as .shape once decompressed

Methods:

Name Description
compress

Compress a u8 tensor (into u8).

decompress

Decompress an u8 torch tensor (into u8).

to_device

Specific device handling.

compress abstractmethod

compress(u8_tensor) -> torch.Tensor

Compress a u8 tensor (into u8).

Parameters:

Name Type Description Default

u8_tensor

tensor to be compressed with dtype torch.uint8

required

Return: compressed tensor with dtype torch.uint8

decompress abstractmethod

decompress(u8_tensor) -> torch.Tensor

Decompress an u8 torch tensor (into u8).

Parameters:

Name Type Description Default

u8_tensor

compressed tensor with dtype torch.uint8

required

Return: tensor decompressed with dtype torch.uint8

to_device

to_device(new_device)

Specific device handling.

Each compressor may implement support for specific device (like GPU, ...)

Allowing faster computation

qscale_per_group_f16_min_max_calibration

qscale_per_group_f16_min_max_calibration(fp_tensor: Tensor, n_bits: int, group_size: int, percentile: float = 1.0, restricted_range: bool = False) -> QScalePerGroupF16

Build QScalePerGroupF16 and calibrate requested float tensor.

Parameters:

Name Type Description Default

fp_tensor

Tensor

float tensor to calibrate.

required

n_bits

int

number of bits per quantized element.

required

group_size

int

number of elements sharing a scale.

required

percentile

float

absolute value percentile used as max per group.

1.0

restricted_range

bool

use [-(2(n_bits-1) - 1), 2(n_bits-1) - 1] instead of full range (GGML Q8 convention).

False
Return

Tuple( QScalePerGroupF16 qscheme, torch.Tensor[uint8] )