Series contents · Engineering and Delivery · 阅读中文版
“The function name is right, and the input shape is right. Why is the answer so different?”
A colleague reduces the failing sample to two negative numbers. What first looked like a small error suddenly becomes a sign reversal. Eventually, the team discovers that the two sides implemented different functions.
Nonlinear operations often appear under short names: Pow, Sigmoid, Softmax, Norm. The answer actually depends on the domain, formula, axes, constants, approximation method, and intermediate precision behind that name.
Ordinary power and signed power differ in meaning, not precision
Ordinary squaring gives (-3)^2=9. A sign-preserving power can instead be defined as:
1 | signed_power(x,p) = sign(x) * |x|^p |
At x=-3 and p=2, it produces -9. Both may be abbreviated as “power” in a document, but their mathematics is different.
General real exponentiation also needs rules for negative bases with noninteger exponents, special cases at zero, and whether the exponent must be a compile-time constant. A similarly named hardware function is not automatically a valid replacement.
The historical specification material contained mathematical names and candidate mappings that needed further clarification. That motivates a design question: before selecting a hardware path, is there an implementation-independent function contract? It is not proof that a particular implementation is defective.
Hypot: representable output does not guarantee safe intermediates
The square root of a sum of squares can be written directly:
1 | sqrt(x*x + y*y) |
For large x and y, squaring can overflow first. For very small values, it can underflow. A representable final result does not guarantee that this evaluation path has safe intermediates.
For finite real values, let m=max(|x|,|y|). When m≠0, a teaching derivation gives:
1 | m * sqrt((x/m)^2 + (y/m)^2) |
This explains a scaling strategy. It is not a complete production implementation covering infinities, NaNs, rounding, and every fixed-point boundary. Integer and quantized forms also give division and scaling their own errors and costs.
Likewise, low-bit-width integer inputs and outputs do not establish that an accumulator can use the same width. Range analysis must follow the computation rather than work backward from interface types.
A lookup table must know which real interval it approximates
Lookup tables and piecewise approximations can reduce the cost of Sigmoid, GELU, and other smooth functions, but several independent questions remain:
- How is an input integer interpreted as a real value?
- Which interval does the table cover, and what happens outside it?
- How are samples distributed, and what precision is used for interpolation?
- How is the output requantized, and where do rounding and saturation occur?
Suppose the input uses x=s(q-z). Changing s changes the real position represented by the same q. If table indexing still uses the old scale, the defect need not look like random noise. It may stretch the whole function along its horizontal axis.
GELU also has different approximation forms. ReLU and Clip with finite bounds are not the same function merely because both truncate values. The approximation method belongs in a contract or traceable configuration. Otherwise, numerical regressions reveal a change without making it clear whether that change was permitted.
Periodic functions have a phase that is easy to forget
Some activations contain a periodic term such as sin²(αx). Here α controls the input’s phase scale, or oscillation frequency. If the complete formula also includes a coefficient such as 1/α, its effect on amplitude must be analyzed separately.
When a fixed-point kernel maps real angles into an integer period, quantization scale, period normalization, and modulo range must agree. Applying the input scale only to a linear term while omitting it from the periodic term introduces position-dependent systematic error.
Structured tests are especially helpful: sample several periods at equal intervals, including zeros, points near peaks, and period boundaries. Mean error is one metric, but also inspect phase shifts, preserved symmetry, and discontinuities at joins.
These validation ideas follow from mathematical structure.
Softmax stabilization changes the evaluation path
Along a selected axis, Softmax computes:
1 | p_i = exp(x_i) / sum_j exp(x_j) |
In exact real arithmetic, subtracting the same constant from every input in one reduction group leaves the result unchanged. A common choice is that group’s maximum, making the largest exponential argument zero and helping avoid overflow.
The phrase “one group” matters. A wrong reduction axis that changes the summation groups, or different subtracted constants within one intended group, changes semantics. Sharing a global maximum across groups still preserves the result in exact arithmetic, but a wider value span can worsen underflow and resolution loss in finite precision. Separately quantizing exponentials, sums, and division adds rounding that can affect the probability sum and resolution of small probabilities.
Useful properties include invariance to a common shift within a group, nonnegative outputs, a probability sum near 1, and sensible behavior when one value is much larger than the others. Finite-precision implementations may satisfy some properties only within an error budget. State tolerances and input ranges explicitly.
These are general checks, not evidence that the historical documents established a complete Softmax implementation.
Over which elements does “Norm” compute statistics?
Inference-time BatchNorm can usually use fixed statistics and become a channelwise affine transform. LayerNorm typically computes dynamic statistics over a group of elements in the current input. RMS-style normalization omits mean centering and uses a denominator related to the root mean square.
These operations may share multiplication, addition, reduction, and reciprocal-square-root components. A common “Norm” suffix does not make them interchangeable.
For centered variance:
1 | mean = average(x) |
The exact-arithmetic equivalent average(x*x)-mean*mean can suffer different cancellation errors in finite precision. Inputs with a large common offset and small variation make the latter expression especially important to evaluate carefully.
The contract must also specify epsilon placement, reduction axes, gamma/beta, and the variance convention. A custom multi-axis RMS form should not be equated without qualification to a framework’s standard last-axis operator.
Even a shift needs a mathematical definition
Does right-shifting a negative integer propagate its sign bit or fill with zeros? If the intent is division by a power of two, does the operation round toward negative infinity, truncate toward zero, or apply another rounding rule? Similar names do not align the behavior of language expressions and hardware instructions.
In quantized computation, a shift often works with a multiplier to implement rescaling. A small difference at one shift can become persistent bias through multiple layers. Tests should include positive and negative values, values just to either side of halfway cases, and saturation boundaries—not just random positive integers.
Unspecified shift behavior should remain a verification question. Filling the gap with an answer that “seems reasonable” merely passes uncertainty to the next implementer.
Write a contract an implementation cannot misread
A usable nonlinear or normalization specification should contain at least:
| Contract part | Required information |
|---|---|
| Mathematical definition | Formula, parameters, axes, domain, and special values |
| Numerical representation | Input/output quantization, intermediate ranges, and rounding locations |
| Implementation limits | Supported shapes, static-parameter requirements, and rejection conditions |
| Approximation guarantees | Applicable interval, error metrics, and validation method |
| Evidence status | What design notes, implementations, and tests each establish |
Historical material is valuable both for its answers and for details that code and tests have not yet confirmed. A public article should explain those questions instead of inventing a complete story for unknown hardware fields.
In the discussion, the first addition was not a denser lookup table. It was a mathematical definition. Until both sides agree on the correct answer, improving approximation precision can merely compute the wrong function more accurately.
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. All the images used in the blog are my original works or AI works, if you want to take it,don't hesitate. Thank you !