Symmetric INT8 Tensor Quantization
Updated
Problem statement
You are given a nonempty tensor as the array values. Quantize it to INT8 with one symmetric scale for the whole tensor. Return the integer codes in the same order as the input.
Pick the scale and round with these rules:
- Let
A = max(abs(values[i])), the largest absolute value in the tensor. WhenA > 0, the scale factor iss = A / 127and the zero-point is0. - For each value
x, find the size of its code asfloor(abs(x) / A * 127 + 0.5). Give the code the same sign asx, then clamp it to the range[-127,127]. So a result exactly halfway between two integers rounds away from zero. The code-128is never used. - If every value is zero, use scale
1and return only zero codes.
Each input is a finite FP32 number, and the floating-point array holds it exactly. Return a plain integer array, not a byte buffer. After a direct solution, improve it so the scale is worked out once and shared by the whole tensor.
Function
quantizeInt8(values: double[]) → int[]Examples
Example 1
values = [-2,-1,0,1,2]return = [-127,-64,0,64,127]The largest absolute value is 2, so the outer values get the largest codes. The inner nonzero values each work out to 63.5, which rounds away from zero to 64. The negative one keeps its minus sign, and zero stays zero.
Example 2
values = [0,0,0]return = [0,0,0]Every value is zero, so the scale is 1 and every code is zero.
Example 3
values = [-3,-1.5,0.75,3]return = [-127,-64,32,127]The scale is 3/127, because the largest absolute value sits at both ends. The second value lands exactly halfway, at -63.5, so it rounds away from zero to -64. The third value lands at 31.75, which rounds to 32.
Constraints
1 <= values.length <= 10000.- Every input is a finite FP32 value, stored exactly in the given double.
abs(values[i]) <= 10^6.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Streaming Entropy, Part 2: Numerically Stable EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Logistic Regression with Gradient DescentYahoo · PHONE SCREENYahoo interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Reachable Transfer PositionsCitadel · PHONE SCREENCitadel interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Minimum Operations to Make Every Array Element OneOtter.ai · PHONE SCREENOtter.ai interview questions
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
- Coin Game From The EndsTekion · PHONE SCREENTekion interview questions