Fix fp32 r2c on large planes, make the fp64 adders IEEE-complete
Browse filesCopies of sm120/fft3d/ at humanfia/kda-kernels-internal commit dbb8bd6, with the file digests in metadata.json updated (not rebuilt with kernel-builder; provenance marked dirty).
- fp32 r2c returned garbage on 11,765 grids within 256 per axis (nx, ny >= 84): plane / y-split plans ran with half their rows. Those grids now take another plan.
- The fp64 integer-unit adders were exact only for finite normal operands and results; data near 1e-308 gave NaN everywhere. They now handle every IEEE case.
- fp64 inputs on a non-current CUDA device raised; calls now run under the input's device.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- README.md +35 -8
- build/torch-cuda/__init__.py +25 -5
- build/torch-cuda/_c2r_f64/kernel.py +7 -11
- build/torch-cuda/_c2r_f64/softfp.py +173 -234
- build/torch-cuda/_r2c_f32/kernel.py +4 -1
- build/torch-cuda/_r2c_f64/dsl_ops.py +95 -66
- build/torch-cuda/_r2c_f64/fft_passes.py +1 -1
- build/torch-cuda/_r2c_f64/kernel.py +4 -3
- build/torch-cuda/metadata.json +8 -8
README.md
CHANGED
|
@@ -89,12 +89,12 @@ fp64 kernels are IEEE float64 throughout, within 3.5e-16 to 5.9e-16 relative L2
|
|
| 89 |
of `torch.fft`'s float64 result. The RTX 5090 runs FP64 at 1/64 of the FP32
|
| 90 |
rate, so both fp64 kernels compute part of their float64 additions with a
|
| 91 |
software IEEE add (round to nearest even) on the integer units, which run
|
| 92 |
-
alongside the FP64 unit. It
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
Repeated calls give bitwise identical results.
|
| 100 |
|
|
@@ -141,14 +141,41 @@ timing and executed out of place; its C2R overwrites its input and is given a
|
|
| 141 |
copy. `torch.fft` is `rfftn` / `irfftn` on the same library. Speeds differ by a
|
| 142 |
few percent between RTX 5090 cards.
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
## Source and licensing
|
| 145 |
|
| 146 |
Written by Claude Opus 5.5 in four 12-hour runs of KDA (kernel design agents),
|
| 147 |
one per KDA task (`fft3d_r2c`, `fft3d_c2r`, `fft3d_r2c_f64`, `fft3d_c2r_f64`),
|
| 148 |
built from the requests in [NVlabs/kda#7](https://github.com/NVlabs/kda/pull/7),
|
| 149 |
and accepted by the KDA judge. Published from commit `e539498` of
|
| 150 |
-
`humanfia/kda-kernels-internal`, directory `rtx5090/fft3d/`
|
| 151 |
-
its tests and the fixes made after the
|
|
|
|
|
|
|
| 152 |
|
| 153 |
## Available functions
|
| 154 |
- `c2r`
|
|
|
|
| 89 |
of `torch.fft`'s float64 result. The RTX 5090 runs FP64 at 1/64 of the FP32
|
| 90 |
rate, so both fp64 kernels compute part of their float64 additions with a
|
| 91 |
software IEEE add (round to nearest even) on the integer units, which run
|
| 92 |
+
alongside the FP64 unit. It returns exactly what the FP64 unit returns for every
|
| 93 |
+
input, subnormal operands and results, overflow, signed zeros, Inf and NaN
|
| 94 |
+
included (2.5e10 operand pairs per routine compared with the native
|
| 95 |
+
instructions). Until 2026-10-24 it was exact only for finite normal operands and
|
| 96 |
+
results, and data near the bottom of the normal range came out wrong (see
|
| 97 |
+
[Changes](#changes)).
|
| 98 |
|
| 99 |
Repeated calls give bitwise identical results.
|
| 100 |
|
|
|
|
| 141 |
copy. `torch.fft` is `rfftn` / `irfftn` on the same library. Speeds differ by a
|
| 142 |
few percent between RTX 5090 cards.
|
| 143 |
|
| 144 |
+
## Changes
|
| 145 |
+
|
| 146 |
+
Updated on 2026-10-24 with fixes found by sweeping every planner signature over
|
| 147 |
+
nz, ny in 8..256 and nx in 16..256 (the requests' range), on a B300 with the RTX
|
| 148 |
+
5090's plans:
|
| 149 |
+
|
| 150 |
+
- fp32 r2c returned garbage, often NaN, on 11,765 grids of that range, all with
|
| 151 |
+
nx >= 84 and ny >= 84 (such as 8x126x140, 8x252x156, 70x104x224): their plane
|
| 152 |
+
or y-split plan needs a whole plane or sub-plane as one row block, and the x
|
| 153 |
+
pass halved the rows that did not fit in shared memory. Those grids now take
|
| 154 |
+
another plan; the plans of the eight grids in the tables above are unchanged.
|
| 155 |
+
- The fp64 software adders were exact only for finite normal operands and
|
| 156 |
+
results: fp64 r2c of a 96x96x144 grid with a quarter of its z planes at 1e-308
|
| 157 |
+
to 1e-300 returned NaN everywhere, and data scaled by 1e-300 or 1e-310,
|
| 158 |
+
subnormal bands of a spectrum and single NaN or Inf inputs failed on many
|
| 159 |
+
grids. They now handle every IEEE case, and fp64 c2r no longer routes 2x - y
|
| 160 |
+
through software on five tuned grids. On a B300 this costs up to 4% (r2c) and
|
| 161 |
+
10% (c2r); the RTX 5090 tables above were measured before it.
|
| 162 |
+
- fp64 inputs on a CUDA device other than the current one raised
|
| 163 |
+
(`ValueError`, `BufferError`); every call now runs under the input's device.
|
| 164 |
+
|
| 165 |
+
The changed files are copies of `sm120/fft3d/` at commit `dbb8bd6` of
|
| 166 |
+
`humanfia/kda-kernels-internal`, with the file digests in `metadata.json`
|
| 167 |
+
updated; the package was not rebuilt with `kernel-builder`.
|
| 168 |
+
|
| 169 |
## Source and licensing
|
| 170 |
|
| 171 |
Written by Claude Opus 5.5 in four 12-hour runs of KDA (kernel design agents),
|
| 172 |
one per KDA task (`fft3d_r2c`, `fft3d_c2r`, `fft3d_r2c_f64`, `fft3d_c2r_f64`),
|
| 173 |
built from the requests in [NVlabs/kda#7](https://github.com/NVlabs/kda/pull/7),
|
| 174 |
and accepted by the KDA judge. Published from commit `e539498` of
|
| 175 |
+
`humanfia/kda-kernels-internal`, directory `rtx5090/fft3d/` (now
|
| 176 |
+
`sm120/fft3d/`), which also holds its tests and the fixes made after the
|
| 177 |
+
judge's checks; updated from commit `dbb8bd6` (see [Changes](#changes)).
|
| 178 |
+
MIT-licensed.
|
| 179 |
|
| 180 |
## Available functions
|
| 181 |
- `c2r`
|
build/torch-cuda/__init__.py
CHANGED
|
@@ -16,6 +16,7 @@ form, so its launch() is r2c(x) and returns a new tensor each call.
|
|
| 16 |
Each (direction, dtype) is its own kernel package, imported on first use.
|
| 17 |
"""
|
| 18 |
|
|
|
|
| 19 |
import functools
|
| 20 |
import importlib
|
| 21 |
|
|
@@ -38,18 +39,37 @@ def _kernel(direction, tensor):
|
|
| 38 |
return importlib.import_module(f".{package}", __name__)
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def r2c(x):
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
def c2r(X):
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
def prepare_r2c(x):
|
| 50 |
-
|
| 51 |
-
return kernel.prepare(x) if hasattr(kernel, "prepare") else functools.partial(kernel.run, x)
|
| 52 |
|
| 53 |
|
| 54 |
def prepare_c2r(X):
|
| 55 |
-
return _kernel("c2r", X)
|
|
|
|
| 16 |
Each (direction, dtype) is its own kernel package, imported on first use.
|
| 17 |
"""
|
| 18 |
|
| 19 |
+
import contextlib
|
| 20 |
import functools
|
| 21 |
import importlib
|
| 22 |
|
|
|
|
| 39 |
return importlib.import_module(f".{package}", __name__)
|
| 40 |
|
| 41 |
|
| 42 |
+
def _on(tensor):
|
| 43 |
+
"""The input's device, current while a call runs (the kernels launch on the current device)."""
|
| 44 |
+
return torch.cuda.device(tensor.device) if tensor.is_cuda else contextlib.nullcontext()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
def r2c(x):
|
| 48 |
+
kernel = _kernel("r2c", x)
|
| 49 |
+
with _on(x):
|
| 50 |
+
return kernel.run(x)
|
| 51 |
|
| 52 |
|
| 53 |
def c2r(X):
|
| 54 |
+
kernel = _kernel("c2r", X)
|
| 55 |
+
with _on(X):
|
| 56 |
+
return kernel.run(X)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _prepare(kernel, tensor):
|
| 60 |
+
with _on(tensor):
|
| 61 |
+
launch = kernel.prepare(tensor) if hasattr(kernel, "prepare") else functools.partial(kernel.run, tensor)
|
| 62 |
+
|
| 63 |
+
def on_device():
|
| 64 |
+
with _on(tensor):
|
| 65 |
+
return launch()
|
| 66 |
+
|
| 67 |
+
return on_device
|
| 68 |
|
| 69 |
|
| 70 |
def prepare_r2c(x):
|
| 71 |
+
return _prepare(_kernel("r2c", x), x)
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
def prepare_c2r(X):
|
| 75 |
+
return _prepare(_kernel("c2r", X), X)
|
build/torch-cuda/_c2r_f64/kernel.py
CHANGED
|
@@ -22,9 +22,9 @@ Each FFT pass keeps a tile of sequences in shared memory and runs mixed-radix DI
|
|
| 22 |
order, launch details) are tuned per benchmark shape; other shapes use a heuristic planner.
|
| 23 |
|
| 24 |
All arithmetic is IEEE float64. Part of the additions run through softfp, a software
|
| 25 |
-
add.rn.f64 on the integer pipes, which execute concurrently with the slow FP64 unit; it
|
| 26 |
-
|
| 27 |
-
|
| 28 |
"""
|
| 29 |
|
| 30 |
# Nested ifs are deliberate: inside the kernels an outer compile-time if (cutlass.const_expr)
|
|
@@ -157,14 +157,10 @@ _ZPOL_SHAPES = {(256, 256, 128): "evict_first"}
|
|
| 157 |
# x-first shapes whose y pass visits the planes in reverse order of the x pass's completion
|
| 158 |
_YORD_SHAPES = {(256, 256, 128)}
|
| 159 |
|
| 160 |
-
# share of the fused-twiddle 2x-y operations computed in software, per x-first
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
(96, 96, 72): 1.0,
|
| 165 |
-
(96, 96, 48): 1.0,
|
| 166 |
-
(256, 256, 128): 1.0,
|
| 167 |
-
}
|
| 168 |
|
| 169 |
# integer-pipe cost of a software butterfly assumed by the soft/native balance, per shape
|
| 170 |
_INTCYC_SHAPES = {(192, 192, 96): 50.0, (256, 256, 128): 60.0}
|
|
|
|
| 22 |
order, launch details) are tuned per benchmark shape; other shapes use a heuristic planner.
|
| 23 |
|
| 24 |
All arithmetic is IEEE float64. Part of the additions run through softfp, a software
|
| 25 |
+
add.rn.f64 on the integer pipes, which execute concurrently with the slow FP64 unit; it
|
| 26 |
+
returns the bits of add.rn.f64 for every operand, subnormal, infinite and NaN ones included (a
|
| 27 |
+
NaN may carry another payload), so where an addition runs changes no result.
|
| 28 |
"""
|
| 29 |
|
| 30 |
# Nested ifs are deliberate: inside the kernels an outer compile-time if (cutlass.const_expr)
|
|
|
|
| 157 |
# x-first shapes whose y pass visits the planes in reverse order of the x pass's completion
|
| 158 |
_YORD_SHAPES = {(256, 256, 128)}
|
| 159 |
|
| 160 |
+
# share of the fused-twiddle 2x-y operations computed in software (softfp.stwominus), per x-first
|
| 161 |
+
# shape: none, since stwominus is exact for every operand the FMA is faster on every benchmark
|
| 162 |
+
# shape (measured on the B300)
|
| 163 |
+
_SOFT2M_SHAPES = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# integer-pipe cost of a software butterfly assumed by the soft/native balance, per shape
|
| 166 |
_INTCYC_SHAPES = {(192, 192, 96): 50.0, (256, 256, 128): 60.0}
|
build/torch-cuda/_c2r_f64/softfp.py
CHANGED
|
@@ -4,14 +4,17 @@ On the RTX 5090 the FP64 unit runs at 1/64 of the FP32 rate while the integer AL
|
|
| 4 |
during an FFT, and the two issue concurrently, so routing part of the float64 additions
|
| 5 |
through integer instructions (inline PTX) raises throughput.
|
| 6 |
|
| 7 |
-
Each operation returns the correctly rounded float64 result, bit-identical to add.rn.f64
|
| 8 |
-
for
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
Method: order |x| >= |y|
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
sadd, ssub a + b, a - b
|
| 17 |
sbfly3/4/5 butterfly (a + b, a - b) sharing ordering, alignment and sticky;
|
|
@@ -22,13 +25,14 @@ round to nearest even, repack.
|
|
| 22 |
from cutlass._mlir.dialects import llvm
|
| 23 |
from cutlass.cutlass_dsl import Float64, T, dsl_user_op
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
.reg .b32 ah, al, bh, bl, aa, ba, xh, xl, yh, yl, ex, ey, d, hx, hy, mxh, myh, nd, st, msk, ng, rl, rh, v, c, lzc, lz, nl, lsb, inc, ql, qh, lzs, e2, sgn, yal, yah, t;
|
| 28 |
.reg .b64 A, B, MX, MY, MYA, L, MYP, R, RN, RR, NG, INC;
|
| 29 |
-
.reg .pred psw, ps, phz, pz;
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
and.b32 aa, ah, 0x7FFFFFFF;
|
| 33 |
and.b32 ba, bh, 0x7FFFFFFF;
|
| 34 |
mov.b64 A, {al, aa};
|
|
@@ -40,11 +44,13 @@ selp.b32 yh, ah, bh, psw;
|
|
| 40 |
selp.b32 yl, al, bl, psw;
|
| 41 |
and.b32 ex, xh, 0x7FF00000;
|
| 42 |
and.b32 ey, yh, 0x7FF00000;
|
| 43 |
-
sub.u32 d, ex, ey;
|
| 44 |
-
shr.u32 d, d, 20;
|
| 45 |
-
min.u32 d, d, 63;
|
| 46 |
min.u32 hx, ex, 0x00100000;
|
| 47 |
min.u32 hy, ey, 0x00100000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
lop3.b32 mxh, xh, 0x000FFFFF, hx, 0xEA;
|
| 49 |
lop3.b32 myh, yh, 0x000FFFFF, hy, 0xEA;
|
| 50 |
mov.b64 MX, {xl, mxh};
|
|
@@ -72,6 +78,9 @@ selp.b32 v, rh, rl, phz;
|
|
| 72 |
clz.b32 c, v;
|
| 73 |
selp.b32 lzc, -1, 31, phz;
|
| 74 |
add.u32 lz, c, lzc;
|
|
|
|
|
|
|
|
|
|
| 75 |
shl.b64 RN, R, lz;
|
| 76 |
cvt.u32.u64 nl, RN;
|
| 77 |
bfe.u32 lsb, nl, 10, 1;
|
|
@@ -81,130 +90,103 @@ add.u64 RR, RN, INC;
|
|
| 81 |
shr.b64 RR, RR, 10;
|
| 82 |
mov.b64 {ql, qh}, RR;
|
| 83 |
shl.b32 lzs, lz, 20;
|
| 84 |
-
sub.u32 e2,
|
| 85 |
-
and.b32 sgn, xh, 0x80000000;
|
| 86 |
add.u32 qh, qh, e2;
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
selp.b32
|
| 90 |
-
selp.b32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
mov.b64 $0, {ql, qh};
|
| 92 |
}"""
|
|
|
|
| 93 |
|
| 94 |
# One difference: the sum with the sign of the second operand flipped.
|
| 95 |
_PTX_SUB = _PTX_ADD.replace("mov.b64 {bl, bh}, $2;", "mov.b64 {bl, bh}, $2;\nxor.b32 bh, bh, 0x80000000;")
|
| 96 |
|
| 97 |
-
# 2*x - y: x doubled
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
{
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# zero results are folded into two selects.
|
| 110 |
-
_PTX_BFLY3 = r"""{
|
| 111 |
-
.reg .b32 ah, al, bh, bl, aa, ba, ea, eb, d, nd, xh, xl, yh, yl, hx, hy, mxh, myh, yal, yah, exf, sa, sd, sh, sl, t, sn, snl, slsb, sinc, ph, pl, dh, dl, v, c, lzb, lz, dnl, dlsb, dinc, qh, ql, zh, zl, rh, rl;
|
| 112 |
-
.reg .b64 A, B, MX, MY, MYA, L, S, D, SN, SR, DN, DR, I1, I2;
|
| 113 |
-
.reg .pred sw, st, dhz, dz, xz, eq;
|
| 114 |
-
mov.b64 {al, ah}, $2;
|
| 115 |
-
mov.b64 {bl, bh}, $3;
|
| 116 |
and.b32 aa, ah, 0x7FFFFFFF;
|
| 117 |
and.b32 ba, bh, 0x7FFFFFFF;
|
| 118 |
mov.b64 A, {al, aa};
|
| 119 |
mov.b64 B, {bl, ba};
|
| 120 |
-
setp.gt.u64
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
selp.b32 xl, bl, al, sw;
|
| 128 |
-
selp.b32 yh, aa, ba, sw;
|
| 129 |
-
selp.b32 yl, al, bl, sw;
|
| 130 |
-
and.b32 exf, xh, 0x7FF00000;
|
| 131 |
-
and.b32 hy, yh, 0x7FF00000;
|
| 132 |
-
min.u32 hx, exf, 0x00100000;
|
| 133 |
-
min.u32 hy, hy, 0x00100000;
|
| 134 |
-
lop3.b32 mxh, xh, 0x000FFFFF, hx, 0xEA;
|
| 135 |
-
lop3.b32 myh, yh, 0x000FFFFF, hy, 0xEA;
|
| 136 |
-
mov.b64 MX, {xl, mxh};
|
| 137 |
-
shl.b64 MX, MX, 9;
|
| 138 |
-
mov.b64 MY, {yl, myh};
|
| 139 |
-
shl.b64 MY, MY, 9;
|
| 140 |
-
shr.b64 MYA, MY, d;
|
| 141 |
-
sub.u32 nd, 64, d;
|
| 142 |
-
shl.b64 L, MY, nd;
|
| 143 |
-
setp.ne.u64 st, L, 0;
|
| 144 |
-
mov.b64 {yal, yah}, MYA;
|
| 145 |
-
@st or.b32 yal, yal, 1;
|
| 146 |
-
mov.b64 MYA, {yal, yah};
|
| 147 |
-
add.u64 S, MX, MYA;
|
| 148 |
-
sub.u64 D, MX, MYA;
|
| 149 |
-
// ---- S: leading one at bit 62 or 61 -> to 62, round at bit 10 (RNE)
|
| 150 |
-
mov.b64 {sl, sh}, S;
|
| 151 |
-
shr.u32 t, sh, 30;
|
| 152 |
-
xor.b32 sn, t, 1;
|
| 153 |
-
shl.b64 SN, S, sn;
|
| 154 |
-
cvt.u32.u64 snl, SN;
|
| 155 |
-
bfe.u32 slsb, snl, 10, 1;
|
| 156 |
-
add.u32 sinc, slsb, 0x1FF;
|
| 157 |
-
cvt.u64.u32 I1, sinc;
|
| 158 |
-
add.u64 SR, SN, I1;
|
| 159 |
-
shr.b64 SR, SR, 10;
|
| 160 |
-
mov.b64 {pl, ph}, SR;
|
| 161 |
-
mad.lo.u32 ph, sn, 0xFFF00000, ph;
|
| 162 |
-
add.u32 ph, ph, exf;
|
| 163 |
-
// ---- D: general normalization to bit 62
|
| 164 |
-
mov.b64 {dl, dh}, D;
|
| 165 |
-
setp.ne.u32 dhz, dh, 0;
|
| 166 |
-
selp.b32 v, dh, dl, dhz;
|
| 167 |
-
clz.b32 c, v;
|
| 168 |
-
selp.b32 lzb, -1, 31, dhz;
|
| 169 |
-
add.u32 lz, c, lzb;
|
| 170 |
-
shl.b64 DN, D, lz;
|
| 171 |
-
cvt.u32.u64 dnl, DN;
|
| 172 |
-
bfe.u32 dlsb, dnl, 10, 1;
|
| 173 |
-
add.u32 dinc, dlsb, 0x1FF;
|
| 174 |
-
cvt.u64.u32 I2, dinc;
|
| 175 |
-
add.u64 DR, DN, I2;
|
| 176 |
-
shr.b64 DR, DR, 10;
|
| 177 |
-
mov.b64 {ql, qh}, DR;
|
| 178 |
-
mad.lo.u32 qh, lz, 0xFFF00000, qh;
|
| 179 |
-
add.u32 qh, qh, exf;
|
| 180 |
-
// ---- signs: S takes sign(a); D takes sign(a) ^ swapped; zeros -> +0
|
| 181 |
-
and.b32 sa, ah, 0x80000000;
|
| 182 |
-
selp.b32 sd, 0x80000000, 0, sw;
|
| 183 |
-
xor.b32 sd, sd, sa;
|
| 184 |
-
setp.eq.u32 xz, exf, 0;
|
| 185 |
-
setp.eq.u32 dz, lz, 63;
|
| 186 |
-
or.b32 ph, ph, sa;
|
| 187 |
-
or.b32 qh, qh, sd;
|
| 188 |
-
selp.b32 ph, 0, ph, xz;
|
| 189 |
-
selp.b32 pl, 0, pl, xz;
|
| 190 |
-
selp.b32 qh, 0, qh, dz;
|
| 191 |
-
// a+b = eq ? S : D ; a-b = eq ? D : S (eq: same signs)
|
| 192 |
xor.b32 t, ah, bh;
|
| 193 |
-
setp.
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
selp.b32
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
}"""
|
|
|
|
| 201 |
|
| 202 |
-
# Butterfly,
|
| 203 |
-
#
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
.reg .b64 A, B, MX, MY, MYA, L, S, D, SN, SR, DN, DR, I1, I2;
|
| 207 |
-
.reg .pred sw, st, dhz, dz,
|
|
|
|
|
|
|
| 208 |
mov.b64 {al, ah}, $2;
|
| 209 |
mov.b64 {bl, bh}, $3;
|
| 210 |
and.b32 aa, ah, 0x7FFFFFFF;
|
|
@@ -214,9 +196,12 @@ mov.b64 B, {bl, ba};
|
|
| 214 |
setp.gt.u64 sw, B, A;
|
| 215 |
shr.u32 ea, aa, 20;
|
| 216 |
shr.u32 eb, ba, 20;
|
|
|
|
|
|
|
| 217 |
sub.s32 d, ea, eb;
|
| 218 |
abs.s32 d, d;
|
| 219 |
min.u32 d, d, 63;
|
|
|
|
| 220 |
selp.b32 xh, ba, aa, sw;
|
| 221 |
selp.b32 xl, bl, al, sw;
|
| 222 |
selp.b32 yh, aa, ba, sw;
|
|
@@ -225,159 +210,105 @@ and.b32 exf, xh, 0x7FF00000;
|
|
| 225 |
and.b32 hy, yh, 0x7FF00000;
|
| 226 |
min.u32 hx, exf, 0x00100000;
|
| 227 |
min.u32 hy, hy, 0x00100000;
|
|
|
|
| 228 |
lop3.b32 mxh, xh, 0x000FFFFF, hx, 0xEA;
|
| 229 |
lop3.b32 myh, yh, 0x000FFFFF, hy, 0xEA;
|
| 230 |
mov.b64 MX, {xl, mxh};
|
| 231 |
shl.b64 MX, MX, 9;
|
| 232 |
mov.b64 MY, {yl, myh};
|
| 233 |
shl.b64 MY, MY, 9;
|
| 234 |
-
shr.b64 MYA, MY, d;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
shl.b64 L, MYA, d;
|
| 236 |
-
setp.ne.u64 st, L, MY;
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
mov.b64 {sl, sh}, S;
|
| 244 |
-
shr.u32 t, sh, 30;
|
| 245 |
-
xor.b32 sn, t, 1;
|
| 246 |
-
shl.b64 SN, S, sn;
|
| 247 |
-
cvt.u32.u64 snl, SN;
|
| 248 |
and.b32 t, snl, 0x400;
|
| 249 |
setp.ne.u32 sl1, t, 0;
|
| 250 |
-
selp.b32 sinc, 0x200, 0x1FF, sl1;
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
mad.lo.u32 ph, sn, 0xFFF00000, ph;
|
| 256 |
-
add.u32 ph, ph, exf;
|
| 257 |
-
// ---- D: general normalization to bit 62
|
| 258 |
-
mov.b64 {dl, dh}, D;
|
| 259 |
-
setp.ne.u32 dhz, dh, 0;
|
| 260 |
-
selp.b32 v, dh, dl, dhz;
|
| 261 |
-
clz.b32 c, v;
|
| 262 |
-
selp.b32 lzb, -1, 31, dhz;
|
| 263 |
-
add.u32 lz, c, lzb;
|
| 264 |
-
shl.b64 DN, D, lz;
|
| 265 |
-
cvt.u32.u64 dnl, DN;
|
| 266 |
and.b32 t, dnl, 0x400;
|
| 267 |
setp.ne.u32 dl1, t, 0;
|
| 268 |
-
selp.b32 dinc, 0x200, 0x1FF, dl1;
|
| 269 |
-
cvt.u64.u32 I2, dinc;
|
| 270 |
-
add.u64 DR, DN, I2;
|
| 271 |
-
shr.b64 DR, DR, 10;
|
| 272 |
-
mov.b64 {ql, qh}, DR;
|
| 273 |
-
mad.lo.u32 qh, lz, 0xFFF00000, qh;
|
| 274 |
-
add.u32 qh, qh, exf;
|
| 275 |
-
// ---- signs: S takes sign(a); D takes sign(a) ^ swapped; zero results -> +0
|
| 276 |
-
// (a zero S or D has a zero low word already: only the high word needs the select)
|
| 277 |
-
and.b32 sa, ah, 0x80000000;
|
| 278 |
-
selp.b32 sd, 0x80000000, 0, sw;
|
| 279 |
-
xor.b32 sd, sd, sa;
|
| 280 |
-
setp.eq.u32 xz, exf, 0;
|
| 281 |
-
setp.eq.u32 dz, lz, 63;
|
| 282 |
-
or.b32 ph, ph, sa;
|
| 283 |
-
or.b32 qh, qh, sd;
|
| 284 |
-
selp.b32 ph, 0, ph, xz;
|
| 285 |
-
selp.b32 qh, 0, qh, dz;
|
| 286 |
-
// a+b = eq ? S : D ; a-b = eq ? D : S (eq: same signs)
|
| 287 |
-
xor.b32 t, ah, bh;
|
| 288 |
-
setp.ge.s32 eq, t, 0;
|
| 289 |
-
selp.b32 zh, ph, qh, eq;
|
| 290 |
-
selp.b32 zl, pl, ql, eq;
|
| 291 |
-
selp.b32 rh, qh, ph, eq;
|
| 292 |
-
selp.b32 rl, ql, pl, eq;
|
| 293 |
-
mov.b64 $0, {zl, zh};
|
| 294 |
-
mov.b64 $1, {rl, rh};
|
| 295 |
-
}"""
|
| 296 |
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
and.b32 ba, bh, 0x7FFFFFFF;
|
| 306 |
-
mov.b64 A, {al, aa};
|
| 307 |
-
mov.b64 B, {bl, ba};
|
| 308 |
-
setp.gt.u64 sw, B, A;
|
| 309 |
-
shr.u32 ea, aa, 20;
|
| 310 |
-
shr.u32 eb, ba, 20;
|
| 311 |
-
sub.s32 d, ea, eb;
|
| 312 |
-
abs.s32 d, d;
|
| 313 |
-
min.u32 d, d, 63;
|
| 314 |
-
selp.b32 xh, ba, aa, sw;
|
| 315 |
-
selp.b32 xl, bl, al, sw;
|
| 316 |
-
selp.b32 yh, aa, ba, sw;
|
| 317 |
-
selp.b32 yl, al, bl, sw;
|
| 318 |
-
and.b32 exf, xh, 0x7FF00000;
|
| 319 |
-
and.b32 hy, yh, 0x7FF00000;
|
| 320 |
-
min.u32 hx, exf, 0x00100000;
|
| 321 |
-
min.u32 hy, hy, 0x00100000;
|
| 322 |
-
lop3.b32 mxh, xh, 0x000FFFFF, hx, 0xEA;
|
| 323 |
-
lop3.b32 myh, yh, 0x000FFFFF, hy, 0xEA;
|
| 324 |
-
mov.b64 MX, {xl, mxh};
|
| 325 |
-
shl.b64 MX, MX, 9;
|
| 326 |
-
mov.b64 MY, {yl, myh};
|
| 327 |
-
shl.b64 MY, MY, 9;
|
| 328 |
-
shr.b64 MYA, MY, d;
|
| 329 |
-
sub.u32 t, 64, d;
|
| 330 |
-
shl.b64 L, MY, t;
|
| 331 |
-
setp.ne.u64 st, L, 0;
|
| 332 |
mov.b64 {yal, yah}, MYA;
|
| 333 |
@st or.b32 yal, yal, 1;
|
| 334 |
mov.b64 MYA, {yal, yah};
|
| 335 |
add.u64 S, MX, MYA;
|
| 336 |
sub.u64 D, MX, MYA;
|
| 337 |
-
// ---- S: leading one at bit 62 or 61 -> to 62, round at bit 10
|
| 338 |
mov.b64 {sl, sh}, S;
|
| 339 |
shr.u32 t, sh, 30;
|
| 340 |
xor.b32 sn, t, 1;
|
| 341 |
shl.b64 SN, S, sn;
|
| 342 |
-
cvt.u32.u64 snl, SN;
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
selp.b32 sinc, 0x200, 0x1FF, sl1;
|
| 346 |
cvt.u64.u32 I1, sinc;
|
| 347 |
add.u64 SR, SN, I1;
|
| 348 |
shr.b64 SR, SR, 10;
|
| 349 |
mov.b64 {pl, ph}, SR;
|
| 350 |
mad.lo.u32 ph, sn, 0xFFF00000, ph;
|
| 351 |
-
add.u32 ph, ph,
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
| 353 |
mov.b64 {dl, dh}, D;
|
| 354 |
setp.ne.u32 dhz, dh, 0;
|
| 355 |
selp.b32 v, dh, dl, dhz;
|
| 356 |
clz.b32 c, v;
|
| 357 |
selp.b32 lzb, -1, 31, dhz;
|
| 358 |
add.u32 lz, c, lzb;
|
|
|
|
|
|
|
| 359 |
shl.b64 DN, D, lz;
|
| 360 |
-
cvt.u32.u64 dnl, DN;
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
selp.b32 dinc, 0x200, 0x1FF, dl1;
|
| 364 |
cvt.u64.u32 I2, dinc;
|
| 365 |
add.u64 DR, DN, I2;
|
| 366 |
shr.b64 DR, DR, 10;
|
| 367 |
mov.b64 {ql, qh}, DR;
|
| 368 |
mad.lo.u32 qh, lz, 0xFFF00000, qh;
|
| 369 |
-
add.u32 qh, qh,
|
| 370 |
-
// ---- signs: S takes sign(a); D takes sign(a) ^ swapped; zero
|
| 371 |
-
// (a zero S or D has a zero low word already: only the high word needs the select)
|
| 372 |
and.b32 sa, ah, 0x80000000;
|
| 373 |
selp.b32 sd, 0x80000000, 0, sw;
|
| 374 |
xor.b32 sd, sd, sa;
|
| 375 |
-
setp.eq.u32 xz, exf, 0;
|
| 376 |
-
setp.eq.u32 dz, lz, 63;
|
| 377 |
or.b32 ph, ph, sa;
|
| 378 |
or.b32 qh, qh, sd;
|
| 379 |
-
selp.b32 ph, 0, ph, xz;
|
| 380 |
selp.b32 qh, 0, qh, dz;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
// a+b = eq ? S : D ; a-b = eq ? D : S (eq: same signs)
|
| 382 |
xor.b32 t, ah, bh;
|
| 383 |
setp.ge.s32 eq, t, 0;
|
|
@@ -388,6 +319,14 @@ selp.b32 rl, ql, pl, eq;
|
|
| 388 |
mov.b64 $0, {zl, zh};
|
| 389 |
mov.b64 $1, {rl, rh};
|
| 390 |
}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
|
| 393 |
def _bits(x, loc, ip):
|
|
|
|
| 4 |
during an FFT, and the two issue concurrently, so routing part of the float64 additions
|
| 5 |
through integer instructions (inline PTX) raises throughput.
|
| 6 |
|
| 7 |
+
Each operation returns the correctly rounded float64 result, bit-identical to add.rn.f64
|
| 8 |
+
(fma.rn.f64 for stwominus) for every operand: subnormal operands and results (gradual
|
| 9 |
+
underflow), overflow to Inf, signed zeros, and Inf and NaN operands (a NaN result may carry
|
| 10 |
+
another payload than the FP64 unit's).
|
| 11 |
|
| 12 |
+
Method: order |x| >= |y| by bit pattern (NaN > Inf > finite), build 53-bit significands
|
| 13 |
+
shifted left by 9 (no hidden bit for zero or subnormal operands, whose exponent counts as
|
| 14 |
+
1), align y with a jammed sticky bit, add or subtract, normalize (one CLZ) to put the leading
|
| 15 |
+
one at bit 62 -- by at most the exponent of x, so that a result below 2^-1022 comes out
|
| 16 |
+
subnormal --, round to nearest even, repack; a result exponent of 0x7FF or more becomes Inf.
|
| 17 |
+
An Inf or NaN x passes through, or becomes NaN for Inf - Inf.
|
| 18 |
|
| 19 |
sadd, ssub a + b, a - b
|
| 20 |
sbfly3/4/5 butterfly (a + b, a - b) sharing ordering, alignment and sticky;
|
|
|
|
| 25 |
from cutlass._mlir.dialects import llvm
|
| 26 |
from cutlass.cutlass_dsl import Float64, T, dsl_user_op
|
| 27 |
|
| 28 |
+
_REGS = r"""
|
| 29 |
+
.reg .b32 ah, al, bh, bl, aa, ba, xh, xl, yh, yl, ex, exe, ey, d, hx, hy, mxh, myh, nd, st, msk, ng, rl, rh, v, c, lzc, lz, E, nl, lsb, inc, ql, qh, lzs, e2, sgn, yal, yah, t;
|
|
|
|
| 30 |
.reg .b64 A, B, MX, MY, MYA, L, MYP, R, RN, RR, NG, INC;
|
| 31 |
+
.reg .pred psw, ps, phz, pz, ovf, spx;"""
|
| 32 |
+
|
| 33 |
+
# ah:al + bh:bl -> magnitude qh:ql (Inf on overflow, 0 for a zero result); xh:xl is the operand
|
| 34 |
+
# of larger magnitude, ex its exponent field, pz flags a zero result.
|
| 35 |
+
_CORE = r"""
|
| 36 |
and.b32 aa, ah, 0x7FFFFFFF;
|
| 37 |
and.b32 ba, bh, 0x7FFFFFFF;
|
| 38 |
mov.b64 A, {al, aa};
|
|
|
|
| 44 |
selp.b32 yl, al, bl, psw;
|
| 45 |
and.b32 ex, xh, 0x7FF00000;
|
| 46 |
and.b32 ey, yh, 0x7FF00000;
|
|
|
|
|
|
|
|
|
|
| 47 |
min.u32 hx, ex, 0x00100000;
|
| 48 |
min.u32 hy, ey, 0x00100000;
|
| 49 |
+
max.u32 exe, ex, 0x00100000;
|
| 50 |
+
max.u32 ey, ey, 0x00100000;
|
| 51 |
+
sub.u32 d, exe, ey;
|
| 52 |
+
shr.u32 d, d, 20;
|
| 53 |
+
min.u32 d, d, 63;
|
| 54 |
lop3.b32 mxh, xh, 0x000FFFFF, hx, 0xEA;
|
| 55 |
lop3.b32 myh, yh, 0x000FFFFF, hy, 0xEA;
|
| 56 |
mov.b64 MX, {xl, mxh};
|
|
|
|
| 78 |
clz.b32 c, v;
|
| 79 |
selp.b32 lzc, -1, 31, phz;
|
| 80 |
add.u32 lz, c, lzc;
|
| 81 |
+
setp.eq.u32 pz, lz, 63;
|
| 82 |
+
shr.u32 E, exe, 20;
|
| 83 |
+
min.u32 lz, lz, E;
|
| 84 |
shl.b64 RN, R, lz;
|
| 85 |
cvt.u32.u64 nl, RN;
|
| 86 |
bfe.u32 lsb, nl, 10, 1;
|
|
|
|
| 90 |
shr.b64 RR, RR, 10;
|
| 91 |
mov.b64 {ql, qh}, RR;
|
| 92 |
shl.b32 lzs, lz, 20;
|
| 93 |
+
sub.u32 e2, exe, lzs;
|
|
|
|
| 94 |
add.u32 qh, qh, e2;
|
| 95 |
+
setp.ge.u32 ovf, qh, 0x7FF00000;
|
| 96 |
+
selp.b32 qh, 0x7FF00000, qh, ovf;
|
| 97 |
+
selp.b32 ql, 0, ql, ovf;
|
| 98 |
+
selp.b32 qh, 0, qh, pz;"""
|
| 99 |
+
|
| 100 |
+
# Sign: that of x; a zero result is +0 unless both operands are -0.
|
| 101 |
+
_SIGN = r"""
|
| 102 |
+
and.b32 sgn, xh, 0x80000000;
|
| 103 |
+
lop3.b32 t, xh, yh, 0x80000000, 0x80;
|
| 104 |
+
selp.b32 sgn, t, sgn, pz;
|
| 105 |
+
or.b32 qh, qh, sgn;"""
|
| 106 |
+
|
| 107 |
+
# One sum: $0 = $1 + $2 (all b64 bit patterns). An Inf or NaN x is the result, or NaN when the
|
| 108 |
+
# result is otherwise zero (Inf - Inf).
|
| 109 |
+
_PTX_ADD = (
|
| 110 |
+
"{"
|
| 111 |
+
+ _REGS
|
| 112 |
+
+ r"""
|
| 113 |
+
mov.b64 {al, ah}, $1;
|
| 114 |
+
mov.b64 {bl, bh}, $2;"""
|
| 115 |
+
+ _CORE
|
| 116 |
+
+ r"""
|
| 117 |
+
setp.eq.u32 spx, ex, 0x7FF00000;
|
| 118 |
+
selp.b32 t, 0x00080000, 0, pz;
|
| 119 |
+
or.b32 t, t, xh;
|
| 120 |
+
selp.b32 qh, t, qh, spx;
|
| 121 |
+
selp.b32 ql, xl, ql, spx;"""
|
| 122 |
+
+ _SIGN
|
| 123 |
+
+ r"""
|
| 124 |
mov.b64 $0, {ql, qh};
|
| 125 |
}"""
|
| 126 |
+
)
|
| 127 |
|
| 128 |
# One difference: the sum with the sign of the second operand flipped.
|
| 129 |
_PTX_SUB = _PTX_ADD.replace("mov.b64 {bl, bh}, $2;", "mov.b64 {bl, bh}, $2;\nxor.b32 bh, bh, 0x80000000;")
|
| 130 |
|
| 131 |
+
# 2*x - y: x doubled exactly (exponent increment, or the magnitude shifted left for subnormal x;
|
| 132 |
+
# the adder takes a doubled exponent field of 0x7FF as finite), then the sum with -y. Inf / NaN
|
| 133 |
+
# operands are resolved on x and -y themselves: the one of larger magnitude, NaN for Inf - Inf.
|
| 134 |
+
_PTX_TWOMINUS = (
|
| 135 |
+
"{"
|
| 136 |
+
+ _REGS
|
| 137 |
+
+ r"""
|
| 138 |
+
.reg .b32 ea, eb, zh, zl, h2;
|
| 139 |
+
.reg .pred spa, spb, pd, psub;
|
| 140 |
+
mov.b64 {al, ah}, $1;
|
| 141 |
+
mov.b64 {bl, bh}, $2;
|
| 142 |
+
xor.b32 bh, bh, 0x80000000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
and.b32 aa, ah, 0x7FFFFFFF;
|
| 144 |
and.b32 ba, bh, 0x7FFFFFFF;
|
| 145 |
mov.b64 A, {al, aa};
|
| 146 |
mov.b64 B, {bl, ba};
|
| 147 |
+
setp.gt.u64 psw, B, A;
|
| 148 |
+
selp.b32 zh, bh, ah, psw;
|
| 149 |
+
selp.b32 zl, bl, al, psw;
|
| 150 |
+
and.b32 ea, ah, 0x7FF00000;
|
| 151 |
+
and.b32 eb, bh, 0x7FF00000;
|
| 152 |
+
setp.eq.u32 spa, ea, 0x7FF00000;
|
| 153 |
+
setp.eq.u32 spb, eb, 0x7FF00000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
xor.b32 t, ah, bh;
|
| 155 |
+
setp.lt.s32 pd, t, 0;
|
| 156 |
+
and.pred pd, pd, spa;
|
| 157 |
+
and.pred pd, pd, spb;
|
| 158 |
+
selp.b32 t, 0x00080000, 0, pd;
|
| 159 |
+
or.b32 zh, zh, t;
|
| 160 |
+
or.pred spa, spa, spb;
|
| 161 |
+
setp.eq.u32 psub, ea, 0;
|
| 162 |
+
add.u32 t, ah, 0x00100000;
|
| 163 |
+
shf.l.clamp.b32 h2, al, ah, 1;
|
| 164 |
+
lop3.b32 h2, h2, ah, 0x80000000, 0xF8;
|
| 165 |
+
selp.b32 ah, h2, t, psub;
|
| 166 |
+
shl.b32 t, al, 1;
|
| 167 |
+
selp.b32 al, t, al, psub;"""
|
| 168 |
+
+ _CORE
|
| 169 |
+
+ _SIGN
|
| 170 |
+
+ r"""
|
| 171 |
+
selp.b32 qh, zh, qh, spa;
|
| 172 |
+
selp.b32 ql, zl, ql, spa;
|
| 173 |
+
mov.b64 $0, {ql, qh};
|
| 174 |
}"""
|
| 175 |
+
)
|
| 176 |
|
| 177 |
+
# Butterfly ($0, $1) = ($2 + $3, $2 - $3). With x the operand of larger magnitude,
|
| 178 |
+
# S = MX + MY and D = MX - MY are both formed; a+b takes S iff the signs agree and a-b
|
| 179 |
+
# takes the other one. S needs at most a 1-bit normalization (no CLZ). The exponent
|
| 180 |
+
# difference is taken from the operands directly (parallel to the magnitude swap), the
|
| 181 |
+
# sticky bit is jammed by a predicated or, S keeps sign(a), D gets sign(a) ^ swapped (+0 when
|
| 182 |
+
# zero), and an Inf / NaN x passes through (D: NaN when |a| == |b|, Inf - Inf). The three
|
| 183 |
+
# schedules below differ in the sticky and rounding-increment computations only.
|
| 184 |
+
_BF_REGS = r"""
|
| 185 |
+
.reg .b32 ah, al, bh, bl, aa, ba, ea, eb, d, nd, xh, xl, yh, yl, hx, hy, exf, exe, E, mxh, myh, yal, yah, sa, sd, sh, sl, t, sn, snl, slsb, sinc, ph, pl, dh, dl, v, c, lzb, lz, dnl, dlsb, dinc, qh, ql, zh, zl, rh, rl;
|
| 186 |
.reg .b64 A, B, MX, MY, MYA, L, S, D, SN, SR, DN, DR, I1, I2;
|
| 187 |
+
.reg .pred sw, st, dhz, dz, eq, sl1, dl1, ovf, spx;"""
|
| 188 |
+
|
| 189 |
+
_BF_HEAD = r"""
|
| 190 |
mov.b64 {al, ah}, $2;
|
| 191 |
mov.b64 {bl, bh}, $3;
|
| 192 |
and.b32 aa, ah, 0x7FFFFFFF;
|
|
|
|
| 196 |
setp.gt.u64 sw, B, A;
|
| 197 |
shr.u32 ea, aa, 20;
|
| 198 |
shr.u32 eb, ba, 20;
|
| 199 |
+
max.u32 ea, ea, 1;
|
| 200 |
+
max.u32 eb, eb, 1;
|
| 201 |
sub.s32 d, ea, eb;
|
| 202 |
abs.s32 d, d;
|
| 203 |
min.u32 d, d, 63;
|
| 204 |
+
max.u32 E, ea, eb;
|
| 205 |
selp.b32 xh, ba, aa, sw;
|
| 206 |
selp.b32 xl, bl, al, sw;
|
| 207 |
selp.b32 yh, aa, ba, sw;
|
|
|
|
| 210 |
and.b32 hy, yh, 0x7FF00000;
|
| 211 |
min.u32 hx, exf, 0x00100000;
|
| 212 |
min.u32 hy, hy, 0x00100000;
|
| 213 |
+
shl.b32 exe, E, 20;
|
| 214 |
lop3.b32 mxh, xh, 0x000FFFFF, hx, 0xEA;
|
| 215 |
lop3.b32 myh, yh, 0x000FFFFF, hy, 0xEA;
|
| 216 |
mov.b64 MX, {xl, mxh};
|
| 217 |
shl.b64 MX, MX, 9;
|
| 218 |
mov.b64 MY, {yl, myh};
|
| 219 |
shl.b64 MY, MY, 9;
|
| 220 |
+
shr.b64 MYA, MY, d;"""
|
| 221 |
+
|
| 222 |
+
# sticky: bits shifted out of MY (shift left by 64 - d), or the round trip of MYA
|
| 223 |
+
_ST_SHL = r"""
|
| 224 |
+
sub.u32 nd, 64, d;
|
| 225 |
+
shl.b64 L, MY, nd;
|
| 226 |
+
setp.ne.u64 st, L, 0;"""
|
| 227 |
+
_ST_TRIP = r"""
|
| 228 |
shl.b64 L, MYA, d;
|
| 229 |
+
setp.ne.u64 st, L, MY;"""
|
| 230 |
+
|
| 231 |
+
# RNE increment at bit 10: + 0x1FF + lsb, the lsb from a bit extract or from a predicate
|
| 232 |
+
_RND_S_BFE = r"""
|
| 233 |
+
bfe.u32 slsb, snl, 10, 1;
|
| 234 |
+
add.u32 sinc, slsb, 0x1FF;"""
|
| 235 |
+
_RND_S_PRED = r"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
and.b32 t, snl, 0x400;
|
| 237 |
setp.ne.u32 sl1, t, 0;
|
| 238 |
+
selp.b32 sinc, 0x200, 0x1FF, sl1;"""
|
| 239 |
+
_RND_D_BFE = r"""
|
| 240 |
+
bfe.u32 dlsb, dnl, 10, 1;
|
| 241 |
+
add.u32 dinc, dlsb, 0x1FF;"""
|
| 242 |
+
_RND_D_PRED = r"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
and.b32 t, dnl, 0x400;
|
| 244 |
setp.ne.u32 dl1, t, 0;
|
| 245 |
+
selp.b32 dinc, 0x200, 0x1FF, dl1;"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
|
| 248 |
+
def _bfly(sticky, rnd_s, rnd_d):
|
| 249 |
+
return (
|
| 250 |
+
"{"
|
| 251 |
+
+ _BF_REGS
|
| 252 |
+
+ _BF_HEAD
|
| 253 |
+
+ sticky
|
| 254 |
+
+ r"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
mov.b64 {yal, yah}, MYA;
|
| 256 |
@st or.b32 yal, yal, 1;
|
| 257 |
mov.b64 MYA, {yal, yah};
|
| 258 |
add.u64 S, MX, MYA;
|
| 259 |
sub.u64 D, MX, MYA;
|
| 260 |
+
// ---- S: leading one at bit 62 or 61 (lower only for subnormal x: E = 1) -> to 62, round at bit 10
|
| 261 |
mov.b64 {sl, sh}, S;
|
| 262 |
shr.u32 t, sh, 30;
|
| 263 |
xor.b32 sn, t, 1;
|
| 264 |
shl.b64 SN, S, sn;
|
| 265 |
+
cvt.u32.u64 snl, SN;"""
|
| 266 |
+
+ rnd_s
|
| 267 |
+
+ r"""
|
|
|
|
| 268 |
cvt.u64.u32 I1, sinc;
|
| 269 |
add.u64 SR, SN, I1;
|
| 270 |
shr.b64 SR, SR, 10;
|
| 271 |
mov.b64 {pl, ph}, SR;
|
| 272 |
mad.lo.u32 ph, sn, 0xFFF00000, ph;
|
| 273 |
+
add.u32 ph, ph, exe;
|
| 274 |
+
setp.ge.u32 ovf, ph, 0x7FF00000;
|
| 275 |
+
selp.b32 ph, 0x7FF00000, ph, ovf;
|
| 276 |
+
selp.b32 pl, 0, pl, ovf;
|
| 277 |
+
// ---- D: normalization to bit 62 by at most E bits (gradual underflow)
|
| 278 |
mov.b64 {dl, dh}, D;
|
| 279 |
setp.ne.u32 dhz, dh, 0;
|
| 280 |
selp.b32 v, dh, dl, dhz;
|
| 281 |
clz.b32 c, v;
|
| 282 |
selp.b32 lzb, -1, 31, dhz;
|
| 283 |
add.u32 lz, c, lzb;
|
| 284 |
+
setp.eq.u32 dz, lz, 63;
|
| 285 |
+
min.u32 lz, lz, E;
|
| 286 |
shl.b64 DN, D, lz;
|
| 287 |
+
cvt.u32.u64 dnl, DN;"""
|
| 288 |
+
+ rnd_d
|
| 289 |
+
+ r"""
|
|
|
|
| 290 |
cvt.u64.u32 I2, dinc;
|
| 291 |
add.u64 DR, DN, I2;
|
| 292 |
shr.b64 DR, DR, 10;
|
| 293 |
mov.b64 {ql, qh}, DR;
|
| 294 |
mad.lo.u32 qh, lz, 0xFFF00000, qh;
|
| 295 |
+
add.u32 qh, qh, exe;
|
| 296 |
+
// ---- signs: S takes sign(a); D takes sign(a) ^ swapped; a zero D is +0
|
|
|
|
| 297 |
and.b32 sa, ah, 0x80000000;
|
| 298 |
selp.b32 sd, 0x80000000, 0, sw;
|
| 299 |
xor.b32 sd, sd, sa;
|
|
|
|
|
|
|
| 300 |
or.b32 ph, ph, sa;
|
| 301 |
or.b32 qh, qh, sd;
|
|
|
|
| 302 |
selp.b32 qh, 0, qh, dz;
|
| 303 |
+
// ---- Inf / NaN x: S = x, D = x (NaN when D is zero: Inf - Inf)
|
| 304 |
+
setp.eq.u32 spx, E, 0x7FF;
|
| 305 |
+
or.b32 t, xh, sa;
|
| 306 |
+
selp.b32 ph, t, ph, spx;
|
| 307 |
+
selp.b32 pl, xl, pl, spx;
|
| 308 |
+
selp.b32 t, 0x00080000, 0, dz;
|
| 309 |
+
lop3.b32 t, t, xh, sd, 0xFE;
|
| 310 |
+
selp.b32 qh, t, qh, spx;
|
| 311 |
+
selp.b32 ql, xl, ql, spx;
|
| 312 |
// a+b = eq ? S : D ; a-b = eq ? D : S (eq: same signs)
|
| 313 |
xor.b32 t, ah, bh;
|
| 314 |
setp.ge.s32 eq, t, 0;
|
|
|
|
| 319 |
mov.b64 $0, {zl, zh};
|
| 320 |
mov.b64 $1, {rl, rh};
|
| 321 |
}"""
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
_PTX_BFLY3 = _bfly(_ST_SHL, _RND_S_BFE, _RND_D_BFE)
|
| 326 |
+
# v4: sticky from a round trip of the aligned operand, rounding increment from a predicate
|
| 327 |
+
_PTX_BFLY4 = _bfly(_ST_TRIP, _RND_S_PRED, _RND_D_PRED)
|
| 328 |
+
# v5: v4 with the v3 sticky (computed in parallel to the alignment shift)
|
| 329 |
+
_PTX_BFLY5 = _bfly(_ST_SHL, _RND_S_PRED, _RND_D_PRED)
|
| 330 |
|
| 331 |
|
| 332 |
def _bits(x, loc, ip):
|
build/torch-cuda/_r2c_f32/kernel.py
CHANGED
|
@@ -159,12 +159,14 @@ def _fast_ok(nz, ny, nx):
|
|
| 159 |
def _ys_fits(c, ny):
|
| 160 |
tw1 = sum(p.Ns * p.R * 8 for pl in (c.cx.plan, c.yplan) for p in pl.passes if p.Ns > 1) + c.M * 8 + 64
|
| 161 |
tw2 = sum(p.Ns * p.R * 8 for p in c.zplan.passes if p.Ns > 1) + ny * 8 + 64
|
|
|
|
| 162 |
return (
|
| 163 |
c.data1 + tw1 <= SMEM_LIMIT - 1024
|
| 164 |
and c.data2 + tw2 <= SMEM_LIMIT - 1024
|
| 165 |
and c.nt1 <= 1024
|
| 166 |
and c.cx.nt <= 1024
|
| 167 |
and c.nt2 <= 1024
|
|
|
|
| 168 |
)
|
| 169 |
|
| 170 |
|
|
@@ -209,7 +211,8 @@ def _default_strategy(nz, ny, nx):
|
|
| 209 |
try:
|
| 210 |
cp = Plane2Cfg(nz, ny, nx)
|
| 211 |
twb = sum(p.Ns * p.R * 8 for pl in (cp.cx.plan, cp.yplan) for p in pl.passes if p.Ns > 1) + cp.M * 8 + 64
|
| 212 |
-
|
|
|
|
| 213 |
return "plane2"
|
| 214 |
except (ValueError, AssertionError):
|
| 215 |
pass
|
|
|
|
| 159 |
def _ys_fits(c, ny):
|
| 160 |
tw1 = sum(p.Ns * p.R * 8 for pl in (c.cx.plan, c.yplan) for p in pl.passes if p.Ns > 1) + c.M * 8 + 64
|
| 161 |
tw2 = sum(p.Ns * p.R * 8 for p in c.zplan.passes if p.Ns > 1) + ny * 8 + 64
|
| 162 |
+
# kernel 1's x pass must take the whole sub-plane as one row block (XCfg halves rows that do not fit)
|
| 163 |
return (
|
| 164 |
c.data1 + tw1 <= SMEM_LIMIT - 1024
|
| 165 |
and c.data2 + tw2 <= SMEM_LIMIT - 1024
|
| 166 |
and c.nt1 <= 1024
|
| 167 |
and c.cx.nt <= 1024
|
| 168 |
and c.nt2 <= 1024
|
| 169 |
+
and c.cx.rows == c.nys
|
| 170 |
)
|
| 171 |
|
| 172 |
|
|
|
|
| 211 |
try:
|
| 212 |
cp = Plane2Cfg(nz, ny, nx)
|
| 213 |
twb = sum(p.Ns * p.R * 8 for pl in (cp.cx.plan, cp.yplan) for p in pl.passes if p.Ns > 1) + cp.M * 8 + 64
|
| 214 |
+
# the x pass must take the whole plane as one row block (XCfg halves rows that do not fit)
|
| 215 |
+
if cp.data + twb <= SMEM_LIMIT - 1024 and cp.nt <= 1024 and cp.cx.nt <= 1024 and cp.cx.rows == ny:
|
| 216 |
return "plane2"
|
| 217 |
except (ValueError, AssertionError):
|
| 218 |
pass
|
build/torch-cuda/_r2c_f64/dsl_ops.py
CHANGED
|
@@ -123,18 +123,28 @@ def bits_neg(x):
|
|
| 123 |
# instruction per 64 cycles per sub-partition and ptxas pads every DFMA with NOPs; an IEEE
|
| 124 |
# round-to-nearest-even add/sub pair (shared exponent alignment, 64-bit integer ops) runs on the
|
| 125 |
# ALU/FMA pipes in those idle issue slots, so unit-coefficient butterflies can bypass the FP64 pipe.
|
| 126 |
-
# Results are identical to DADD/DSUB for
|
| 127 |
-
# (
|
|
|
|
| 128 |
def _emu_ptx_addsub(half=False, A="{$r0}", B="{$r1}", S="{$w0}", D="{$w1}"):
|
| 129 |
-
"""PTX for S = A + B, D = A - B (
|
| 130 |
-
(9 guard bits, sticky jammed into bit 0); the add path
|
| 131 |
-
subtract path with a 64-bit count-leading-zeros
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
return f"""{{
|
| 134 |
.reg .b64 aa, ab, ax, ay, mx, my, ys, t64, mp, mm, np, nm;
|
| 135 |
-
.reg .b32 al, ah, bl, bh, axl, axh, ayl, ayh, ex, ey,
|
| 136 |
-
.reg .b32 pl, ph, ql, qh, rl, rh, sl, sh2,
|
| 137 |
-
.reg .pred sw, same,
|
| 138 |
mov.b64 {{al, ah}}, {A};
|
| 139 |
mov.b64 {{bl, bh}}, {B};
|
| 140 |
and.b32 t, ah, 0x7FFFFFFF;
|
|
@@ -146,21 +156,21 @@ def _emu_ptx_addsub(half=False, A="{$r0}", B="{$r1}", S="{$w0}", D="{$w1}"):
|
|
| 146 |
min.u64 ay, aa, ab;
|
| 147 |
mov.b64 {{axl, axh}}, ax;
|
| 148 |
mov.b64 {{ayl, ayh}}, ay;
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
mov.b64
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
min.u32 dd, dd, 63;
|
| 165 |
shr.b64 ys, my, dd;
|
| 166 |
shl.b64 t64, ys, dd;
|
|
@@ -169,15 +179,19 @@ def _emu_ptx_addsub(half=False, A="{$r0}", B="{$r1}", S="{$w0}", D="{$w1}"):
|
|
| 169 |
or.b64 ys, ys, t64;
|
| 170 |
add.s64 mp, mx, ys;
|
| 171 |
sub.s64 mm, mx, ys;
|
| 172 |
-
//
|
|
|
|
|
|
|
|
|
|
| 173 |
mov.b64 {{pl, ph}}, mp;
|
| 174 |
setp.lt.u32 p62, ph, 0x40000000;
|
| 175 |
-
shl.b64 t64, mp, 1;
|
| 176 |
selp.b64 np, t64, mp, p62;
|
| 177 |
-
selp.u32 sh,
|
| 178 |
// sub path: general normalization
|
| 179 |
clz.b64 lzm, mm;
|
| 180 |
sub.u32 shm, lzm, 1;
|
|
|
|
| 181 |
shl.b64 nm, mm, shm;
|
| 182 |
setp.eq.u64 zm, mm, 0;
|
| 183 |
// round both (RNE at bit 10) and pack
|
|
@@ -188,36 +202,43 @@ def _emu_ptx_addsub(half=False, A="{$r0}", B="{$r1}", S="{$w0}", D="{$w1}"):
|
|
| 188 |
addc.u32 ph, ph, 0;
|
| 189 |
shf.r.clamp.b32 rl, pl, ph, 10;
|
| 190 |
shr.u32 rh, ph, 10;
|
| 191 |
-
sub.u32
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
add.u32 rh, rh, e1;
|
| 195 |
-
selp.u32 rh, 0, rh, zx;
|
| 196 |
-
mov.b64 {{ql, qh}}, nm;
|
| 197 |
bfe.u32 t, ql, 10, 1;
|
| 198 |
add.u32 t, t, 0x1FF;
|
| 199 |
add.cc.u32 ql, ql, t;
|
| 200 |
addc.u32 qh, qh, 0;
|
| 201 |
shf.r.clamp.b32 sl, ql, qh, 10;
|
| 202 |
shr.u32 sh2, qh, 10;
|
| 203 |
-
|
| 204 |
-
sub.u32
|
| 205 |
-
|
| 206 |
-
add
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
selp.u32 sh2, 0, sh2, zm;
|
| 208 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
xor.b32 u, ah, bh;
|
| 210 |
setp.ge.s32 same, u, 0;
|
| 211 |
selp.b32 pl, rl, sl, same;
|
| 212 |
selp.b32 ph, rh, sh2, same;
|
| 213 |
selp.b32 ql, sl, rl, same;
|
| 214 |
selp.b32 qh, sh2, rh, same;
|
| 215 |
-
// signs: a+b takes the sign of the larger operand; a-b flips it when |b| > |a|
|
| 216 |
-
selp.b32 s1, bh, ah, sw;
|
| 217 |
-
selp.b32 swm, 0x80000000, 0, sw;
|
| 218 |
-
lop3.b32 ph, ph, s1, 0x80000000, 0xF8;
|
| 219 |
-
xor.b32 t, s1, swm;
|
| 220 |
-
lop3.b32 qh, qh, t, 0x80000000, 0xF8;
|
| 221 |
mov.b64 {S}, {{pl, ph}};
|
| 222 |
mov.b64 {D}, {{ql, qh}};
|
| 223 |
}}"""
|
|
@@ -245,29 +266,37 @@ def emu_addsub(x, y, half=False):
|
|
| 245 |
return _as_f64(s_), _as_f64(d_)
|
| 246 |
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
"and.b32 e, hi, 0x7FF00000;\n\t"
|
| 258 |
-
"setp.gt.u32 p, e, 0x00100000;\n\t"
|
| 259 |
-
"setp.eq.u32 q, e, 0x7FF00000;\n\t"
|
| 260 |
-
"sub.s32 h2, hi, 0x00100000;\n\t"
|
| 261 |
-
"and.b32 hs, hi, 0x80000000;\n\t"
|
| 262 |
-
"selp.b32 h3, h2, hs, p;\n\t"
|
| 263 |
-
"selp.b32 l3, lo, 0, p;\n\t"
|
| 264 |
-
"selp.b32 h3, hi, h3, q;\n\t"
|
| 265 |
-
"selp.b32 l3, lo, l3, q;\n\t"
|
| 266 |
-
"mov.b64 {$w0}, {l3, h3};\n\t}",
|
| 267 |
-
write_only_types=[cutlass.Int64],
|
| 268 |
-
read_only_args=[xi],
|
| 269 |
-
)
|
| 270 |
-
return Float64(arith.bitcast(Float64.mlir_type, yi.ir_value()))
|
| 271 |
|
| 272 |
|
| 273 |
def vload_i4(ptr):
|
|
|
|
| 123 |
# instruction per 64 cycles per sub-partition and ptxas pads every DFMA with NOPs; an IEEE
|
| 124 |
# round-to-nearest-even add/sub pair (shared exponent alignment, 64-bit integer ops) runs on the
|
| 125 |
# ALU/FMA pipes in those idle issue slots, so unit-coefficient butterflies can bypass the FP64 pipe.
|
| 126 |
+
# Results are bit-identical to DADD/DSUB for every operand: subnormal operands and results
|
| 127 |
+
# (gradual underflow), overflow to Inf, signed zeros, Inf and NaN operands (a NaN result may
|
| 128 |
+
# have another payload than DADD's).
|
| 129 |
def _emu_ptx_addsub(half=False, A="{$r0}", B="{$r1}", S="{$w0}", D="{$w1}"):
|
| 130 |
+
"""PTX for S = A + B, D = A - B (with `half`, (A + B) / 2 and (A - B) / 2, each rounded
|
| 131 |
+
once). Mantissas sit at bits [61:9] (9 guard bits, sticky jammed into bit 0); the add path
|
| 132 |
+
normalises by at most one bit, the subtract path with a 64-bit count-leading-zeros, both
|
| 133 |
+
limited so that the exponent stays >= 0 (subnormal results); RNE rounding on 32-bit
|
| 134 |
+
halves. Exponents are kept in place (bits 30:20 of the high word)."""
|
| 135 |
+
hdec = 0x00100000 if half else 0
|
| 136 |
+
p62_floor = "\tsetp.ne.and.u32 p62, emax, 0, p62;\n" if half else ""
|
| 137 |
+
# a + b can overflow to Inf, (a + b) / 2 cannot
|
| 138 |
+
ovf = (
|
| 139 |
+
""
|
| 140 |
+
if half
|
| 141 |
+
else "\tsetp.ge.u32 ovf, rh, 0x7FF00000;\n\tselp.b32 rh, 0x7FF00000, rh, ovf;\n\tselp.b32 rl, 0, rl, ovf;\n"
|
| 142 |
+
)
|
| 143 |
return f"""{{
|
| 144 |
.reg .b64 aa, ab, ax, ay, mx, my, ys, t64, mp, mm, np, nm;
|
| 145 |
+
.reg .b32 al, ah, bl, bh, axl, axh, ayl, ayh, ex, ey, hx, hy, exe, eye, ehd, emax, dd, mxh, myh, t, u, sh, lzm, shm;
|
| 146 |
+
.reg .b32 pl, ph, ql, qh, rl, rh, sl, sh2, sa, sd;
|
| 147 |
+
.reg .pred sw, same, st, p62, zm, ovf, spx;
|
| 148 |
mov.b64 {{al, ah}}, {A};
|
| 149 |
mov.b64 {{bl, bh}}, {B};
|
| 150 |
and.b32 t, ah, 0x7FFFFFFF;
|
|
|
|
| 156 |
min.u64 ay, aa, ab;
|
| 157 |
mov.b64 {{axl, axh}}, ax;
|
| 158 |
mov.b64 {{ayl, ayh}}, ay;
|
| 159 |
+
// exponent fields, hidden bits (none for zero / subnormal) and effective exponents (>= 1)
|
| 160 |
+
and.b32 ex, axh, 0x7FF00000;
|
| 161 |
+
and.b32 ey, ayh, 0x7FF00000;
|
| 162 |
+
min.u32 hx, ex, 0x00100000;
|
| 163 |
+
min.u32 hy, ey, 0x00100000;
|
| 164 |
+
max.u32 exe, ex, 0x00100000;
|
| 165 |
+
max.u32 eye, ey, 0x00100000;
|
| 166 |
+
lop3.b32 mxh, axh, 0x000FFFFF, hx, 0xEA;
|
| 167 |
+
lop3.b32 myh, ayh, 0x000FFFFF, hy, 0xEA;
|
| 168 |
+
mov.b64 mx, {{axl, mxh}};
|
| 169 |
+
mov.b64 my, {{ayl, myh}};
|
| 170 |
+
shl.b64 mx, mx, 9;
|
| 171 |
+
shl.b64 my, my, 9;
|
| 172 |
+
sub.u32 dd, exe, eye;
|
| 173 |
+
shr.u32 dd, dd, 20;
|
| 174 |
min.u32 dd, dd, 63;
|
| 175 |
shr.b64 ys, my, dd;
|
| 176 |
shl.b64 t64, ys, dd;
|
|
|
|
| 179 |
or.b64 ys, ys, t64;
|
| 180 |
add.s64 mp, mx, ys;
|
| 181 |
sub.s64 mm, mx, ys;
|
| 182 |
+
// result exponent base, and the largest normalising shift that keeps it >= 0
|
| 183 |
+
sub.u32 ehd, exe, {hdec};
|
| 184 |
+
shr.u32 emax, ehd, 20;
|
| 185 |
+
// add path: mp in [2^61, 2^63) (lower only for subnormal x) -> MSB to bit 62
|
| 186 |
mov.b64 {{pl, ph}}, mp;
|
| 187 |
setp.lt.u32 p62, ph, 0x40000000;
|
| 188 |
+
{p62_floor} shl.b64 t64, mp, 1;
|
| 189 |
selp.b64 np, t64, mp, p62;
|
| 190 |
+
selp.u32 sh, 0x00100000, 0, p62;
|
| 191 |
// sub path: general normalization
|
| 192 |
clz.b64 lzm, mm;
|
| 193 |
sub.u32 shm, lzm, 1;
|
| 194 |
+
min.u32 shm, shm, emax;
|
| 195 |
shl.b64 nm, mm, shm;
|
| 196 |
setp.eq.u64 zm, mm, 0;
|
| 197 |
// round both (RNE at bit 10) and pack
|
|
|
|
| 202 |
addc.u32 ph, ph, 0;
|
| 203 |
shf.r.clamp.b32 rl, pl, ph, 10;
|
| 204 |
shr.u32 rh, ph, 10;
|
| 205 |
+
sub.u32 t, ehd, sh;
|
| 206 |
+
add.u32 rh, rh, t;
|
| 207 |
+
{ovf} mov.b64 {{ql, qh}}, nm;
|
|
|
|
|
|
|
|
|
|
| 208 |
bfe.u32 t, ql, 10, 1;
|
| 209 |
add.u32 t, t, 0x1FF;
|
| 210 |
add.cc.u32 ql, ql, t;
|
| 211 |
addc.u32 qh, qh, 0;
|
| 212 |
shf.r.clamp.b32 sl, ql, qh, 10;
|
| 213 |
shr.u32 sh2, qh, 10;
|
| 214 |
+
shl.b32 t, shm, 20;
|
| 215 |
+
sub.u32 t, ehd, t;
|
| 216 |
+
add.u32 sh2, sh2, t;
|
| 217 |
+
// signs: the add path keeps sign(a); the sub path takes sign(a), flipped when |b| > |a|,
|
| 218 |
+
// and is +0 when |a| == |b|
|
| 219 |
+
and.b32 sa, ah, 0x80000000;
|
| 220 |
+
selp.b32 sd, 0x80000000, 0, sw;
|
| 221 |
+
xor.b32 sd, sd, sa;
|
| 222 |
+
or.b32 rh, rh, sa;
|
| 223 |
+
or.b32 sh2, sh2, sd;
|
| 224 |
selp.u32 sh2, 0, sh2, zm;
|
| 225 |
+
// Inf / NaN (x = the larger bit pattern, NaN > Inf > finite): the add path gives x, the sub
|
| 226 |
+
// path x too, or NaN when |a| == |b| (Inf - Inf)
|
| 227 |
+
setp.eq.u32 spx, ex, 0x7FF00000;
|
| 228 |
+
or.b32 t, axh, sa;
|
| 229 |
+
selp.b32 rh, t, rh, spx;
|
| 230 |
+
selp.b32 rl, axl, rl, spx;
|
| 231 |
+
selp.b32 t, 0x00080000, 0, zm;
|
| 232 |
+
lop3.b32 t, t, axh, sd, 0xFE;
|
| 233 |
+
selp.b32 sh2, t, sh2, spx;
|
| 234 |
+
selp.b32 sl, axl, sl, spx;
|
| 235 |
+
// assign: a+b takes the add path when the signs agree
|
| 236 |
xor.b32 u, ah, bh;
|
| 237 |
setp.ge.s32 same, u, 0;
|
| 238 |
selp.b32 pl, rl, sl, same;
|
| 239 |
selp.b32 ph, rh, sh2, same;
|
| 240 |
selp.b32 ql, sl, rl, same;
|
| 241 |
selp.b32 qh, sh2, rh, same;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
mov.b64 {S}, {{pl, ph}};
|
| 243 |
mov.b64 {D}, {{ql, qh}};
|
| 244 |
}}"""
|
|
|
|
| 266 |
return _as_f64(s_), _as_f64(d_)
|
| 267 |
|
| 268 |
|
| 269 |
+
# x * 0.5: exponent decrement (exponent field >= 2), Inf / NaN unchanged, and for subnormal
|
| 270 |
+
# results (exponent field 0 or 1, where the magnitude bits are the integer significand) the
|
| 271 |
+
# magnitude shifted right by one, rounded to nearest even.
|
| 272 |
+
_PTX_HALF = """{
|
| 273 |
+
.reg .b32 lo, hi, e, h2, hm, h3, l3, r;
|
| 274 |
+
.reg .pred p, q;
|
| 275 |
+
mov.b64 {lo, hi}, {$r0};
|
| 276 |
+
and.b32 e, hi, 0x7FF00000;
|
| 277 |
+
setp.gt.u32 p, e, 0x00100000;
|
| 278 |
+
setp.eq.u32 q, e, 0x7FF00000;
|
| 279 |
+
sub.s32 h2, hi, 0x00100000;
|
| 280 |
+
selp.b32 h2, hi, h2, q;
|
| 281 |
+
and.b32 hm, hi, 0x7FFFFFFF;
|
| 282 |
+
shf.r.clamp.b32 l3, lo, hm, 1;
|
| 283 |
+
shr.u32 h3, hm, 1;
|
| 284 |
+
and.b32 r, lo, l3;
|
| 285 |
+
and.b32 r, r, 1;
|
| 286 |
+
add.cc.u32 l3, l3, r;
|
| 287 |
+
addc.u32 h3, h3, 0;
|
| 288 |
+
lop3.b32 h3, h3, hi, 0x80000000, 0xF8;
|
| 289 |
+
selp.b32 h3, h2, h3, p;
|
| 290 |
+
selp.b32 l3, lo, l3, p;
|
| 291 |
+
mov.b64 {$w0}, {l3, h3};
|
| 292 |
+
}"""
|
| 293 |
|
| 294 |
+
|
| 295 |
+
def exact_half(x):
|
| 296 |
+
"""x * 0.5 on the integer pipes (no FP64 instruction), bit-identical to mul.rn.f64 by 0.5
|
| 297 |
+
for every x (subnormal results rounded to nearest even, Inf / NaN unchanged)."""
|
| 298 |
+
yi = cute.arch.inline_ptx(_PTX_HALF, write_only_types=[cutlass.Int64], read_only_args=[_as_i64(x)])
|
| 299 |
+
return _as_f64(yi)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
|
| 302 |
def vload_i4(ptr):
|
build/torch-cuda/_r2c_f64/fft_passes.py
CHANGED
|
@@ -6,7 +6,7 @@ shared by ``split`` warps), shared-memory layout and options (see tuned.py). Bu
|
|
| 6 |
build_r2c X pass, nx even: half-length complex FFT of each row (pairs of reals
|
| 7 |
as one complex), then the split post-processing with H_k = W_nx^k / 2
|
| 8 |
build_r2c_pack2 X pass: two real rows packed into one complex FFT of length nx; the
|
| 9 |
-
split costs 4 FMAs per k and halves
|
| 10 |
runs as halved integer-pipe add/sub pairs (``emu_split``)
|
| 11 |
build_colpass Y / Z pass: complex FFT along a strided axis, in place on the output
|
| 12 |
|
|
|
|
| 6 |
build_r2c X pass, nx even: half-length complex FFT of each row (pairs of reals
|
| 7 |
as one complex), then the split post-processing with H_k = W_nx^k / 2
|
| 8 |
build_r2c_pack2 X pass: two real rows packed into one complex FFT of length nx; the
|
| 9 |
+
split costs 4 FMAs per k and halves on the integer pipes (exact_half), or
|
| 10 |
runs as halved integer-pipe add/sub pairs (``emu_split``)
|
| 11 |
build_colpass Y / Z pass: complex FFT along a strided axis, in place on the output
|
| 12 |
|
build/torch-cuda/_r2c_f64/kernel.py
CHANGED
|
@@ -18,9 +18,10 @@ mixed-radix Stockham FFT through shared memory (fft_plan.py, fft_stages.py) buil
|
|
| 18 |
register-operand DFMAs (fft_codelets.py). sm_120 issues one FP64 warp instruction per 64
|
| 19 |
cycles per SM sub-partition, so a tuned fraction of the unit-coefficient add/sub pairs runs
|
| 20 |
as a bit-exact IEEE add/sub on the integer pipes instead (dsl_ops.emu_addsub; options emu /
|
| 21 |
-
emu_split), keeping both pipes busy. That adder
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
Pass configurations come from the tuned table (tuned.py) or a cost model (planner.py); each
|
| 26 |
shape's plan is built and compiled on first use and cached per (shape, device).
|
|
|
|
| 18 |
register-operand DFMAs (fft_codelets.py). sm_120 issues one FP64 warp instruction per 64
|
| 19 |
cycles per SM sub-partition, so a tuned fraction of the unit-coefficient add/sub pairs runs
|
| 20 |
as a bit-exact IEEE add/sub on the integer pipes instead (dsl_ops.emu_addsub; options emu /
|
| 21 |
+
emu_split), keeping both pipes busy. That adder, like the integer halving of the pack2 split
|
| 22 |
+
(dsl_ops.exact_half), returns the bits of the FP64 unit's result for every operand, subnormal,
|
| 23 |
+
infinite and NaN ones included (a NaN may carry another payload), so where an operation runs
|
| 24 |
+
changes no result. Axes with a prime factor above 13 use direct-DFT kernels (fft_direct.py).
|
| 25 |
|
| 26 |
Pass configurations come from the tuned table (tuned.py) or a cost model (planner.py); each
|
| 27 |
shape's plan is built and compiled on first use and cached per (shape, device).
|
build/torch-cuda/metadata.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
| 17 |
"algorithm": "sha256",
|
| 18 |
"files": {
|
| 19 |
"LICENSE.txt": "AIyEysQ0a1okjH0MEq1kTbdKj938zWEHbQJBfSkK23s=",
|
| 20 |
-
"__init__.py": "
|
| 21 |
"_c2r_f32/__init__.py": "XBy0pSUwgUTmssbhC7XEt/ELeTb4ayilaHy7B/V0pHw=",
|
| 22 |
"_c2r_f32/fftgen.py": "vlccjhLwsmKsoyZ+C8DPeR7fIe7LPMxEVsq1cDF5faE=",
|
| 23 |
"_c2r_f32/generic.py": "F902qd+wV64kuLP9J/ko0jIWWH/wApP3vh28C9iuiy8=",
|
|
@@ -27,8 +27,8 @@
|
|
| 27 |
"_c2r_f32/twopass.py": "zI/xziZlOyfjNByjxB9Lag16rgClQVEn9kwlcoKQXT0=",
|
| 28 |
"_c2r_f64/__init__.py": "6NiqCxU8/XZoiu7IY/nThfL5G3EovojIu+BSdpPGqEk=",
|
| 29 |
"_c2r_f64/fftgen.py": "1Fn09b4uYmDSwItnhAycTXZVDWcmvXmf28BdjtMjsEQ=",
|
| 30 |
-
"_c2r_f64/kernel.py": "
|
| 31 |
-
"_c2r_f64/softfp.py": "
|
| 32 |
"_c2r_f64/twiddles.py": "WXaDgblQUWoX/1Qoofd5mD5aUpG1EkjYsdKD3YAUPW4=",
|
| 33 |
"_ops.py": "jdrs11Llpuona5wZbbcr6MuX8ArEa+djv684JpYKENE=",
|
| 34 |
"_r2c_f32/__init__.py": "b+FuhAkJolHH1YTeeRAb4NMHe+7YQId1VYOQ+W+vqsI=",
|
|
@@ -39,18 +39,18 @@
|
|
| 39 |
"_r2c_f32/fft_xy.py": "1Y0pV8gRN+jk6Iriz6Nzpjv0SiCF7AOUn2UR9VpEL80=",
|
| 40 |
"_r2c_f32/fft_ys1w.py": "4Vske6f2ATSSTb//EkpoHS5E3s0m9xGWOLuH3KWJSC8=",
|
| 41 |
"_r2c_f32/fft_ysplit.py": "ukVz02AMlODEqFb/0qvSwAs5rA4CMFlqpI1YSRY0yqQ=",
|
| 42 |
-
"_r2c_f32/kernel.py": "
|
| 43 |
"_r2c_f32/plan.py": "kp8pVa1thunGU5fK5/OtqNF+7NqWYNPac/kwpfTQKTA=",
|
| 44 |
"_r2c_f32/ptx.py": "3mKM+qkQfpV6NCAXz4V4FLf6Z35LtiONgb5oSGF09fw=",
|
| 45 |
"_r2c_f32/stockham.py": "5lS6YIDy/WKT4gTUgsQsrJHBEaAWAyjELVhD6uHJysM=",
|
| 46 |
"_r2c_f64/__init__.py": "rHmKPjGcG8wbZnWqgo9OQD4lcZYRVFCcsgC3c5sFq3s=",
|
| 47 |
-
"_r2c_f64/dsl_ops.py": "
|
| 48 |
"_r2c_f64/fft_codelets.py": "Np96zEz++x7McIuE7+aUz6HGkIk81aNt+Zs2TyrvXKA=",
|
| 49 |
"_r2c_f64/fft_direct.py": "bImy5bLyOOWrUTeAmTJaeUsznyQzlljKDqMuKmTvLQQ=",
|
| 50 |
-
"_r2c_f64/fft_passes.py": "
|
| 51 |
"_r2c_f64/fft_plan.py": "OqPrkiahnt/XreodPMi90N7+kHinYgYbLuWAdVd8eqI=",
|
| 52 |
"_r2c_f64/fft_stages.py": "5ylt/t+bA+9ILsAG5P9CCJkwdJmfvvx/oB29w1LoO9A=",
|
| 53 |
-
"_r2c_f64/kernel.py": "
|
| 54 |
"_r2c_f64/planner.py": "VgtohOIirLvHoK/AAciicLFmPD5nJSOCYxOomeYQZEY=",
|
| 55 |
"_r2c_f64/tuned.py": "YtEsBZO9C63haDWFVnMBtIpTXwSKuvxAF6ffpz9wrd0="
|
| 56 |
}
|
|
@@ -63,7 +63,7 @@
|
|
| 63 |
},
|
| 64 |
"kernel": {
|
| 65 |
"commit": "cd430d9624d14da9768c2609c16f1aa44df7e760",
|
| 66 |
-
"dirty":
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
|
|
|
| 17 |
"algorithm": "sha256",
|
| 18 |
"files": {
|
| 19 |
"LICENSE.txt": "AIyEysQ0a1okjH0MEq1kTbdKj938zWEHbQJBfSkK23s=",
|
| 20 |
+
"__init__.py": "FpeC/pysTUfgktdzoT9ywiuLxGtBIQ2QqqfsEDvegvA=",
|
| 21 |
"_c2r_f32/__init__.py": "XBy0pSUwgUTmssbhC7XEt/ELeTb4ayilaHy7B/V0pHw=",
|
| 22 |
"_c2r_f32/fftgen.py": "vlccjhLwsmKsoyZ+C8DPeR7fIe7LPMxEVsq1cDF5faE=",
|
| 23 |
"_c2r_f32/generic.py": "F902qd+wV64kuLP9J/ko0jIWWH/wApP3vh28C9iuiy8=",
|
|
|
|
| 27 |
"_c2r_f32/twopass.py": "zI/xziZlOyfjNByjxB9Lag16rgClQVEn9kwlcoKQXT0=",
|
| 28 |
"_c2r_f64/__init__.py": "6NiqCxU8/XZoiu7IY/nThfL5G3EovojIu+BSdpPGqEk=",
|
| 29 |
"_c2r_f64/fftgen.py": "1Fn09b4uYmDSwItnhAycTXZVDWcmvXmf28BdjtMjsEQ=",
|
| 30 |
+
"_c2r_f64/kernel.py": "oO56oh0cqnRnEHg60hXOAGgsdvWMAUShwQCUwwmSfF0=",
|
| 31 |
+
"_c2r_f64/softfp.py": "B4zzVG3AA7dtfqYCCORehavfX7Xz8ypzqiML/W5hgBE=",
|
| 32 |
"_c2r_f64/twiddles.py": "WXaDgblQUWoX/1Qoofd5mD5aUpG1EkjYsdKD3YAUPW4=",
|
| 33 |
"_ops.py": "jdrs11Llpuona5wZbbcr6MuX8ArEa+djv684JpYKENE=",
|
| 34 |
"_r2c_f32/__init__.py": "b+FuhAkJolHH1YTeeRAb4NMHe+7YQId1VYOQ+W+vqsI=",
|
|
|
|
| 39 |
"_r2c_f32/fft_xy.py": "1Y0pV8gRN+jk6Iriz6Nzpjv0SiCF7AOUn2UR9VpEL80=",
|
| 40 |
"_r2c_f32/fft_ys1w.py": "4Vske6f2ATSSTb//EkpoHS5E3s0m9xGWOLuH3KWJSC8=",
|
| 41 |
"_r2c_f32/fft_ysplit.py": "ukVz02AMlODEqFb/0qvSwAs5rA4CMFlqpI1YSRY0yqQ=",
|
| 42 |
+
"_r2c_f32/kernel.py": "BDPmjQmWyhBCydZpf+B9YWbhb0wFBYGh+NGMxfks04c=",
|
| 43 |
"_r2c_f32/plan.py": "kp8pVa1thunGU5fK5/OtqNF+7NqWYNPac/kwpfTQKTA=",
|
| 44 |
"_r2c_f32/ptx.py": "3mKM+qkQfpV6NCAXz4V4FLf6Z35LtiONgb5oSGF09fw=",
|
| 45 |
"_r2c_f32/stockham.py": "5lS6YIDy/WKT4gTUgsQsrJHBEaAWAyjELVhD6uHJysM=",
|
| 46 |
"_r2c_f64/__init__.py": "rHmKPjGcG8wbZnWqgo9OQD4lcZYRVFCcsgC3c5sFq3s=",
|
| 47 |
+
"_r2c_f64/dsl_ops.py": "XmM8HcIg/vaNo6pdrTBgLdMBT6YsFo50OUpOXkApnLc=",
|
| 48 |
"_r2c_f64/fft_codelets.py": "Np96zEz++x7McIuE7+aUz6HGkIk81aNt+Zs2TyrvXKA=",
|
| 49 |
"_r2c_f64/fft_direct.py": "bImy5bLyOOWrUTeAmTJaeUsznyQzlljKDqMuKmTvLQQ=",
|
| 50 |
+
"_r2c_f64/fft_passes.py": "3NlJynzW5Q1J25x/BwvB8wS9rs/uaQ18FPt9GzPQZfk=",
|
| 51 |
"_r2c_f64/fft_plan.py": "OqPrkiahnt/XreodPMi90N7+kHinYgYbLuWAdVd8eqI=",
|
| 52 |
"_r2c_f64/fft_stages.py": "5ylt/t+bA+9ILsAG5P9CCJkwdJmfvvx/oB29w1LoO9A=",
|
| 53 |
+
"_r2c_f64/kernel.py": "FGzPJcf18SMJi2Yu7+fa8o0rnM1WL8gMeDb04fa+nfI=",
|
| 54 |
"_r2c_f64/planner.py": "VgtohOIirLvHoK/AAciicLFmPD5nJSOCYxOomeYQZEY=",
|
| 55 |
"_r2c_f64/tuned.py": "YtEsBZO9C63haDWFVnMBtIpTXwSKuvxAF6ffpz9wrd0="
|
| 56 |
}
|
|
|
|
| 63 |
},
|
| 64 |
"kernel": {
|
| 65 |
"commit": "cd430d9624d14da9768c2609c16f1aa44df7e760",
|
| 66 |
+
"dirty": true
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|