Mathematics
How the absolute error of a GPU's sine and cosine reaches the closed forms of the removable singularities and their derivatives, measured on a wgpu adapter; the error the cutoffs are designed for and how a device is admitted to it; and the conditions a cutoff and its series must meet so that single precision on the GPU agrees with double precision as it does on the CPU.
The problem#
The rotations’ exponential, logarithm and Jacobians, and the exponential test sphere’s log-volume, are smooth at the identity, but their closed forms divide by a power of the angle there. Each is computed, as Removable singularities has it, by a truncated series below a cutoff and by its closed form above it. The cutoffs were chosen so that the closed form just above the cutoff, its value and its first and second derivatives, agrees in single precision with double precision, and that choice was measured on the CPU. On a wgpu adapter three checks of that agreement fail: the second-order pass of the pose’s left Jacobian in the rotations’ tests, and the second-order map of the exponential sphere’s log-volume at in the core’s. Every other check passes there.
This writeup finds why, by measuring the adapter’s elementary functions, shows how their error reaches a closed form and its derivatives, states the error the design assumes of a device and how a device is admitted to it, and states the conditions a cutoff and its series must meet for single precision on the GPU to agree with double precision as closely as it does on the CPU. It decides no cutoff: as the rotations’ mathematics left them, the numbers are settled by the implementation, against measurements, under the conditions found here.
Setting#
A computed value carries an error, the difference between what single precision returns and the exact value at the same, already rounded, input. Write for single precision’s unit roundoff. A function has relative error when its computed value is with , and absolute error when it is with . The two differ only where is small, which is exactly where a removable singularity is computed: near .
The coefficients are those of the rotations’ table, each a function of : ; ; ; , with ; and the motions’ and , whose closed forms divide by and . The sphere’s is with , from the measures’ exponential sphere. Their derivatives are taken by the core’s passes: each primitive’s derivative is a formula (, ) evaluated at the computed values, so a pass differentiates the formula as written, never the rounding.
The adapter’s elementary functions#
Measured on an NVIDIA GeForce RTX 4070 Ti through wgpu, against double precision at the same single-precision inputs, beside burn’s Flex device on the CPU in single precision:
| function | inputs | CPU, single | GPU, single |
|---|---|---|---|
| relative | absolute , relative up to | ||
| relative | absolute , relative | ||
| relative | absolute | ||
| relative | absolute | ||
| absolute | absolute | ||
| , | , | relative | absolute near , relative below elsewhere |
| relative | relative |
The CPU’s sine is accurate relative to its value, as a library function is. The GPU’s is accurate only to an absolute on , whatever the argument, and worse beyond, where its argument is reduced. Its cosine is likewise absolute, at , which near is a relative error of a few roundings and hardly differs from the CPU’s. Its exponential, logarithm and are relative, within a few roundings. The WGSL specification promises less still for the sine and cosine, an absolute error of on , and nothing outside it: a bound under which single precision agrees with double precision to nowhere, not even in a rotation’s matrix.
How the error reaches a closed form#
Every closed form here is
with a polynomial whose own partial derivatives in its second and third arguments do not vanish at : has and , has and . The numerator cancels to ; the error of and does not. With the computed and , to first order in the errors,
so that under the GPU’s absolute errors , with bounded near and , while under the CPU’s relative ones, and , the sine’s part is smaller by the factor .
A pass computes the -th derivative of (1) in as another expression of the same form, a polynomial in , and over , since each derivative in is and lowers the order of the numerator’s cancellation by two. Its error is therefore
The GPU loses, against the CPU, the factor , about , in every closed form whose numerator holds a sine, and nothing in those that hold only a cosine. The measurements bear both out. At , the errors of the closed forms’ second derivatives in , against double precision, are:
| coefficient | holds | CPU | GPU | ||
|---|---|---|---|---|---|
| , of | |||||
| , |
The last column is the scale of (2) without its constant. and , whose numerators carry a sine with a coefficient that does not cancel, lose a factor of eight to ten on the GPU, as (2) predicts at ; and , of cosines alone, do not. These are the coefficients of the pose’s Jacobian, whose second-order pass is the one that fails. A coefficient reaches a map multiplied by powers of , which is why errors of in ’s second derivative are tolerable on the CPU, but the factor of ten is not, and it carries the pass past the agreement of .
The same factor explains the sphere. Its divides a sine by and its series is used below ; the check at compares the second-order map, taken from the series, with central differences of the cotangent map at single precision’s step of , where and the closed form is used, and on the GPU the closed form’s derivatives there are wrong by , far beyond the check’s tolerance. On the CPU the same derivatives are smaller by the factor , about . At its second derivative’s error on the GPU is against a value of .
Maps without a cancelling numerator are not affected: a rotation’s matrix, its action, the exponential’s , the elementary rotations of Euler angles, each carries the absolute into its value once, below . The logarithm’s divides by the angle of a point, which the GPU computes to its relative accuracy, so it loses nothing either.
The error designed for#
The cutoffs cannot be designed for the specification’s bound. Under an absolute error of no map of a rotation agrees with double precision to , closed form or not, and not even a rotation’s matrix. Nor can they be designed for the one adapter measured, since other adapters, and other drivers of this one, may differ. They are designed instead for a stated bound on the absolute error of the sine and the cosine,
about times the error measured on and twice the error measured between and . The two bounds differ because only the first is amplified. A closed form evaluates its sine and cosine beyond only where , where the factor of (2) is below and amplifies nothing. There, as in a map without a cancelling numerator, the error reaches a value about once, and leaves room under the agreement of .
The sine’s and cosine’s parts of the errors in (2) are linear in , and the rest does not depend on it. At , then, the GPU’s errors are at most times those measured, with the measured error. That factor, applied to the measurements, is what the conditions below are checked against. The constant is left out of the bound, and the measurements stand in for it.
The exponential, the logarithm and need no bound of their own, because the cancelling numerators do not amplify their errors as they amplify the sine’s. A closed form’s is computed with a relative error , of a few roundings, and so is a nearby argument . Where the numerator and the power are both formed from , the closed form is evaluated consistently there, and its error is , bounded near . Where a power is formed from the exact instead, the numerator’s is left uncancelled. Divided by , it gives an error of order . That is the order of the CPU’s own sine error, , larger only by the few roundings , and never by the factor . Either way, the design asks of them only the relative accuracy measured.
Admission. The checks that compare single precision with double precision rest on (3), so a device must show that it meets it. With a finite set of single-precision inputs, and the exact values taken in double precision, whose own error of is negligible, the device is admitted when
and the same maximum over beyond is at most . A device that is not admitted fails there. The failure names and the bound it exceeds, rather than a coefficient’s second derivative several steps later. Admission is necessary, not sufficient: is a maximum over a sample and can miss the worst input. The room between the bound and the error measured is what absorbs the inputs the sample misses, on an adapter like the one measured. therefore covers the arguments the closed forms and the maps evaluate, near as densely as near , as the measurements above did.
Conditions on a cutoff#
The coefficient’s closed form is used above its cutoff and its series below it. Two conditions decide , and the first is new.
The closed form above the cutoff. Take each coefficient , each order , and each map that uses , and let be the factor by which the map multiplies ’s -th derivative. Then
where is the share of the agreement of single with double precision that the coefficient may spend. The bound decreases in , so a larger cutoff always meets it. By (2), it is also met on the CPU wherever it is met on the GPU.
The scaling of (2) suggests how far a cutoff holding a sine must rise for its error on the GPU at to be no larger than its error on the CPU at the present . The factor is . At it is about for , giving , and for , giving . The factor is largest where is smallest. It is only a guide: it drops and the CPU’s errors from sources other than the sine, and for , where it predicts , the measurements show the GPU’s error already below the CPU’s. The measurements decide. At the GPU’s errors in the second derivatives in were for , for and for . Scaled to , they are at most , two to three orders below what the CPU reaches at the present cutoff.
The series below it. The series truncated after terms, with remainder , must meet double precision’s rounding at the cutoff, in its value and its first two derivatives:
The series of , , , and are entire, with terms falling like , so every cutoff can be met with enough terms: at , about ten. ’s series is that of . It converges for , with terms falling by . ’s series, , converges for , with terms falling by . Either radius bounds the cutoff away from it, and the number of terms grows as the cutoff nears it. A series costs a product and a sum per term, on every device.
Every coefficient has a cutoff meeting both. For , , , and , meets (5) at by the measurements just scaled, and (6) with about ten terms. , of sines and cosines of with , was measured no worse on the GPU than on the CPU at . Its series at falls by a term, so it reaches double precision’s rounding in about a dozen. The sphere needs its cutoff moved furthest. Its present one, , leaves the closed form in use at the check’s , where its second derivative’s error is of the order of . At the error measured, , is at most at . That is well inside the check’s , which is relative to the larger of the value and one and so, for values this small, absolute. There the series falls by a term and reaches double precision’s rounding in about eight. Below such a cutoff the check’s central differences, at , are taken from the series, as the second-order map is.
One cutoff still serves every target, since a composite sees neither its precision nor its device. The conditions are met for the worst of them: (3) in single precision for (5), and double precision for (6). Every other target, the CPU’s in either precision and an admitted GPU’s in double, meets them a fortiori. The archive’s cutoff for single precision, , with its series through , was of this size.
In skoro’s terms#
Nothing random or estimated changes, and no map changes its value in exact arithmetic: the series and the closed form agree, and only the point where one replaces the other moves. What changes is the computation of a composite’s value, and of its derivatives of both orders, on a target whose elementary functions have absolute error. The core’s passes inherit that error by evaluating each primitive’s derivative formula at the computed values. The checks that compare single precision with double precision, on both sides of each cutoff, hold on the GPU once two things are true: the cutoffs meet (5) at , and the device is admitted by (4). The bounds of (3) are a property of the target that the design assumes, stated once. They are not a tolerance of any one check.
Built on by
- Accuracy on the GPU: Implementation The wgpu tests admit their adapter against the stated error of its sine and cosine before they run; the rotations' coefficients move their cutoff to θ² = 1 with series long enough for it; the test sphere's exponential and log-volume move theirs to r² = 0.1.