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
QScheme
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 |
cuda |
Move to a CUDA device and stay quantized, as |
to_device |
Specific device handling. |
write_in_file |
Called at NNEF write time. |
xpu |
Move to an XPU device and stay quantized, as |
cpu
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
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.
write_in_file
Called at NNEF write time.
Each specific inference engine format should implement the file dump prefered.
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 a u8 tensor (into u8).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
tensor to be compressed with dtype torch.uint8 |
required |
Return: compressed tensor with dtype torch.uint8
decompress
abstractmethod
Decompress an u8 torch tensor (into u8).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
compressed tensor with dtype torch.uint8 |
required |
Return: tensor decompressed with dtype torch.uint8
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 |
|---|---|---|---|
|
Tensor
|
float tensor to calibrate. |
required |
|
int
|
number of bits per quantized element. |
required |
|
int
|
number of elements sharing a scale. |
required |
|
float
|
absolute value percentile used as max per group. |
1.0
|
|
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] )