DequantizedTensor¶
- class aimet_torch.quantization.DequantizedTensor(*args, **kwargs)[source]¶
Represents a tensor which has been quantized and subsequently dequantized. This object contains real floating point data as well as an
EncodingBase
object which holds information about the quantization parameters with which the data was quantized. With this, aDequantizedTensor
can be converted back to its quantized representation without further loss in information.- quantize()[source]¶
Quantizes
self
usingself.encoding
to produce aQuantizedTensor
with the same encoding information.Example: :rtype:
QuantizedTensor
>>> import aimet_torch.v2.quantization as Q >>> x = torch.tensor([[0.39, 51.0], [3.521, 9.41]]) >>> quant_dequant = Q.affine.QuantizeDequantize(shape=(), bitwidth=8, symmetric=False) >>> quant_dequant.set_range(-10, 41) >>> x_qdq = quant_dequant(x) >>> x_qdq DequantizedTensor([[ 0.4000, 41.0000], [ 3.6000, 9.4000]], grad_fn=<AliasBackward0>) >>> x_qdq.quantize() QuantizedTensor([[ 52., 255.], [ 68., 97.]], grad_fn=<AliasBackward0>)
- quantized_repr()[source]¶
Return the quantized representation of
self
as atorch.Tensor
with data typeself.encoding.dtype
. :rtype:Tensor
Note
The result of this function may not be able to carry a gradient depending on the quantized data type. Thus, it may be necessary to call this only within an autograd function to allow for backpropagation.
Example
>>> import aimet_torch.v2.quantization as Q >>> x = torch.tensor([[0.39, 51.0], [3.521, 9.41]]) >>> quant_dequant = Q.affine.QuantizeDequantize(shape=(), bitwidth=8, symmetric=False) >>> quant_dequant.set_range(-10, 41) >>> x_qdq = quant_dequant(x) >>> x_qdq DequantizedTensor([[ 0.4000, 41.0000], [ 3.6000, 9.4000]], grad_fn=<AliasBackward0>) >>> x_qdq.quantized_repr() tensor([[ 52, 255], [ 68, 97]], dtype=torch.uint8)