← 返回 nvidia 的题目列表FP32 Tensor to INT8 Quantization (with Asymmetric Quantization Follow-ups)
类型:online_judge
Given an FP32 tensor x (treat it as a 1-D array / contiguous buffer) and a floating-point scale, quantize it into an INT8 tensor q.
Base version (symmetric)
For each element:
q[i] = (int8)( x[i] / scale )
Follow-up (asymmetric quantization)
Extend the base version to support asymmetric quantization with an additional integer parameter zero_point:
q[i] = (int8)( x[i] / scale + zero_point )
Requirements
Provide/implement a function interface (C++ or pseudocode) to perform the conversion.
Discuss why zero_point is needed for non-symmetric input distributions, and how scale + zero_point work together.
Sample tests
x=[0.0, 1.0, -1.0], scale=0.5, zero_point=0 → q=[0, 2, -2]
x=[0.0, 1.0, -1.0], scale=0.5, zero_point=10 → q=[10, 12, 8]
x=[2.5], scale=0.5, zero_point=0 → q=[5]
x=[-2.5], scale=0.5, zero_point=0 → q=[-5]
x=[0.2, 0.3], scale=0.1, zero_point=0 → q=[2, 3]
Constraints
Let n be the number of elements. 1 ≤ n ≤ 10^7 (implementation should be O(n) with a single pass).
Example
Input
3
0.0 1.0 -1.0
0.5
0
Output
0 2 -2