quantize¶
- nvtripy.quantize(input: Tensor, scale: Tensor | Number | Sequence[Number] | Sequence[Sequence[Number]], dtype: dtype, dim: int | None = None) Tensor[source]¶
Quantizes the input Tensor. The valid quantized data types are
nvtripy.int8,nvtripy.int4,nvtripy.float8.If
dtypeisnvtripy.int4, the result of this function cannot be printed asnvtripy.int4is an internal quantized data type. It must be dequantizeddequantize()to a higher precision first.If
dimis not given, this function will perform “per-tensor” or “block-wise” quantization.For “per-tensor” quantization, the
scalemust be a scalar tensor or a single python number.For “block-wise” quantization, the
dtypemust only benvtripy.int4. Theinputtensor must only have 2 dimensions, e.g.[D0, D1]. Thescalemust also be a 2-D tensor or a 2-D python sequence. The first dimension ofscalemust be able to divideD0, where “blocking” is performed. The second dimension ofscalemust equal toD1.
If
dimis given, this function will perform “per-channel” quantization. Thescalemust be a 1-D tensor or a python sequence both with size ofinput.shape[dim].- Parameters:
input (Tensor) – [dtype=T1] The input tensor.
scale (Tensor | Number | Sequence[Number] | Sequence[Sequence[Number]]) – [dtype=T1] The scale tensor. Must be a constant tensor.
dtype (dtype) – [dtype=T2] The quantization data type. Must be a valid quantized data type (see above).
dim (int | None) – The dimension for per-channel quantization
- Returns:
[dtype=T2] Quantized Tensor.
- Return type:
Example: Per-tensor quantization
1input = tp.reshape(tp.arange(6, tp.float32), (2, 3)) 2scale = 0.99872 3# output = tp.quantize(input, scale, tp.int8) 4 5# assert np.array_equal(cp.from_dlpack(output).get(), expected)
Local Variables¶>>> input tensor( [[0, 1, 2], [3, 4, 5]], dtype=float32, loc=gpu:0, shape=(2, 3))
Example: Per-channel quantization
1input = tp.Tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]) 2scale = [0.99872, 0.96125] 3output = tp.quantize(input, scale, tp.int8, dim=0)
Local Variables¶>>> input tensor( [[0, 1, 2], [3, 4, 5]], dtype=float32, loc=cpu:0, shape=(2, 3)) >>> output tensor( [[0, 1, 2], [3, 4, 5]], dtype=int8, loc=gpu:0, shape=(2, 3))
Example: Block-wise quantization
1input = tp.Tensor([[0.0, 1.0], [2.0, 3.0]]) 2scale = [[1.0, 1.0]] 3quant = tp.quantize(input, scale, tp.int4) 4output = tp.dequantize(quant, scale, tp.float32)
Local Variables¶>>> output tensor( [[0, 1], [2, 3]], dtype=float32, loc=gpu:0, shape=(2, 2))
See also