diff --git a/clean/rr_f/gpu/output.py b/clean/rr_f/gpu/output.py index f2e1c5bf..9bb29b65 100644 --- a/clean/rr_f/gpu/output.py +++ b/clean/rr_f/gpu/output.py @@ -42,8 +42,9 @@ def scratch_bytes(wr,hr,wh,hh): return lst+wr*hr*16+(wr*hr*4+15)//16*16+wr*hr*8+64 -def kernel_set(defines=None,fast=True,names=None,spec=False): - """{name: (dxil, NARGS)} of the output stage's kernels (fast path or rr_output_main).""" +def kernel_set(defines=None,fast=True,names=None,spec=False,fast_only=None): + """{name: (dxil, NARGS)} of the output stage's kernels (fast path or rr_output_main). fast_only: extra defines of the + 'fast' kernel alone (clean/rr-f-wave4 R4.3: the prep / fallback DXIL stay the same bytes).""" defines=dict(defines or {}) # spec: the runtime's fixed fast-path flags (kOutFastFlags) at compile time (OFX_SPEC, rr_output_fast.hlsli; same bits) if spec and fast: @@ -61,6 +62,7 @@ def kernel_set(defines=None,fast=True,names=None,spec=False): d['WG_BLOCK']=PREP_BLOCK; d['OF_PREPU']=1 if name=='fast': d['WG_WAVE']=int(d.pop('OF_FAST_WAVE',FAST_WAVE)) + d.update(fast_only or {}) else: d.pop('OF_FAST_WAVE',None) out[name]=(build.compile_kernel('rr_output.hlsl','rr_output_'+('prep' if name=='prepu' else name),d),NARGS) diff --git a/clean/rr_f/gpu/output_diff.py b/clean/rr_f/gpu/output_diff.py index a25ad459..3cd30c6d 100644 --- a/clean/rr_f/gpu/output_diff.py +++ b/clean/rr_f/gpu/output_diff.py @@ -95,7 +95,7 @@ def constant_sets(rng): sets = {"sequence-synthetic": SQ.model_synthetic().osym} try: sets["model-file"] = SQ.model_from_file(SQ.DEFAULT_MODEL, SQ.DEFAULT_MAP).osym - except Exception as e: # noqa: BLE001 - the model file is optional here + except (Exception, SystemExit) as e: # noqa: BLE001 - the model file is optional here (SystemExit: model_from_file refuses) print("model file not used:", e) base = sets["sequence-synthetic"] sets["random-table"] = replace(base, TR=table(rng)) diff --git a/clean/rr_f/gpu/output_prof.py b/clean/rr_f/gpu/output_prof.py index 7f84e208..c6be6218 100644 --- a/clean/rr_f/gpu/output_prof.py +++ b/clean/rr_f/gpu/output_prof.py @@ -22,7 +22,7 @@ def main(): ap.add_argument("--defines", default="") a = ap.parse_args() from . import sequence as SQ - c = SQ.model_from_file(SQ.DEFAULT_MODEL, SQ.DEFAULT_MAP).osym + c = (SQ.model_from_file(SQ.DEFAULT_MODEL, SQ.DEFAULT_MAP) if SQ.DEFAULT_MODEL else SQ.model_synthetic()).osym # no model file: synthetic constants r, o = a.size.split(":") render = tuple(int(v) for v in r.split("x")) output = tuple(int(v) for v in o.split("x")) diff --git a/clean/rr_f/gpu/stage.py b/clean/rr_f/gpu/stage.py index 2994d9ef..5e6b2280 100644 --- a/clean/rr_f/gpu/stage.py +++ b/clean/rr_f/gpu/stage.py @@ -419,7 +419,7 @@ def w64_stages(step="E"): return tuple(s for s in os.environ.get("WG_RR_W64", W64_DEFAULT).split(",") if s) -def kernel_for(name, zmode, isa=False, step="E", ldsw=1, direct=None, fold=None, dflow=0): +def kernel_for(name, zmode, isa=False, step="E", ldsw=1, direct=None, fold=None, dflow=0, extra=None): """direct (E1 only): the input layout the stage call was built with (StageCall.e1_direct); False keeps E1 on rr_block.hlsl with the regrouped input whatever the step (e.g. variant X instead of Y / Z / I on failed checks).""" r = route(name, ldsw, step) @@ -434,6 +434,9 @@ def kernel_for(name, zmode, isa=False, step="E", ldsw=1, direct=None, fold=None, d.update(route_def(r)[1]) if name == "E1" and src == "rr_block_fast.hlsl" and direct is not False and e1_direct(ldsw, step): d["RR_ADAPT"] = 2 + if extra: # clean/rr-f-wave4: A/B kernels (e.g. {"RR_OVL": 0}: the kernel before the LDS overlay of the relaxed body) + d.update(extra) + tag += "_" + "_".join(f"{k}{v}" for k, v in sorted(extra.items())) fm = (infold(step, ldsw) if fold is None else fold) if name == "IN0" and r == "c32" else 0 if fm: d["RR_INFOLD"] = fm @@ -458,6 +461,96 @@ def kernel_for(name, zmode, isa=False, step="E", ldsw=1, direct=None, fold=None, return build.compile_kernel(src, "rr_block", d, isa=isa, tag=tag) +# clean/rr-f-wave4b R4b.2: the level-1 stages of the relaxed body (E1, D1-F, D1-R: step E with RR_B32 in WG_RR_EDEFS) in wave64, +# design D of rr_block_c32.hlsl (rr_block_c64_w64.hlsl: one wave64 = two token tiles of the same window, every value +# computed by the same operation, so the outputs keep their bits). The runtime's WG_RR_W4B mask (bits 1 .. 3) selects them. +W4B_C64_STAGES = ("D1-R", "D1-F", "E1") + + +def kernel_c64_w64(name, zmode, step="E", ldsw=1, extra=None): + assert name in W4B_C64_STAGES and step == "E" and "RR_B32=1" in os.environ.get("WG_RR_EDEFS", "").split(",") + d = stage_defines(name, zmode, step, ldsw) + d.update({"WG_WAVE": 64}) + if name == "E1": + d["RR_ADAPT"] = 2 # reads IN0's y2 directly, as stagee.E1 + tag = "rsc64w64_" + name.replace("-", "") + "_z" + zmode + "_" + step + ("_l" if ldsw else "") + if extra: + d.update(extra) + tag += "_" + "_".join(f"{k}{v}" for k, v in sorted(extra.items())) + return build.compile_kernel("rr_block_c64_w64.hlsl", "rr_block", d, tag=tag) + + +# clean/rr-f-wave4c W4C.1: the wide stages of the relaxed body (E2, D2-F, D2-R, E3, D3-F, D3-R, D4-F, D4-R: step E with RR_B32 in +# WG_RR_EDEFS, RR_B32G4 for level 4) in wave64, design D (rr_block_wide_w64.hlsl: wave w = 2 g + tw holds the two token tiles of +# the wave32 kernel's waves 4 g + 2 tw and 4 g + 2 tw + 1, the weight fragments shared; the same bits as the wave32 kernel). The +# wave32 kernel's route gives the group count and operand-prefetch mask. E4 / E5 (RR_B32 off at G = 4) keep wave32. +W4C_WIDE_STAGES = ("E2", "D2-F", "D2-R", "E3", "D3-F", "D3-R", "D4-F", "D4-R") +W4C_WIDE_L2 = ("E2", "D2-F", "D2-R") +W4C_WIDE_L3 = ("E3", "D3-F", "D3-R") +W4C_WIDE_L4 = ("D4-F", "D4-R") + + +def kernel_wide_w64(name, zmode, step="E", ldsw=1, extra=None): + assert name in W4C_WIDE_STAGES and step == "E" and "RR_B32=1" in os.environ.get("WG_RR_EDEFS", "").split(",") + src, rd = route_def(route(name, ldsw, step)) + assert src == "rr_block_wide.hlsl", (name, src) + d = stage_defines(name, zmode, step, ldsw) + d.update(rd) + d["WG_WAVE"] = 64 + for k, v in W4C_WIDE_DEFS.get(name, {}).items(): # tuned operand-prefetch / chunk-count defines of the wave64 form + d.setdefault(k, v) + tag = "rsw64_" + name.replace("-", "") + "_z" + zmode + "_" + step + ("_l" if ldsw else "") + if extra: + d.update(extra) + tag += "_" + "_".join(f"{k}{v}" for k, v in sorted(extra.items())) + return build.compile_kernel("rr_block_wide_w64.hlsl", "rr_block", d, tag=tag) + + +W4C_WIDE_DEFS = {} # per stage: defines of the wave64 form beyond the wave32 route's (filled below once tuned) +# clean/rr-f-wave4c W4C.2: extra defines of stagee.IN0.w4c / stagee.D0-F.w4c (rr_block_c32.hlsl wave64): RR_DEFER 1 (deferred Q8), +# RR_W4C bit 1 (Dd / Wo weight prefetch), bit 2 (IN0's folded front-end MLP interleaved), RR_SMAJOR 2 (act2_4 there) +# RGA (gfx1201): IN0 with RR_DEFER 92 VGPRs (stagee.IN0 84), D0-F with RR_DEFER 104 / 105 (L01_DEFERFENCE 0) VGPRs, over the 96 tier +# (stagee.D0-F 90): D0-F takes the prefetch alone (90 VGPRs) +W4C_C32_DEFS = {"IN0": {"RR_DEFER": 1, "RR_W4C": 6, "RR_SMAJOR": 2}, "D0-F": {"RR_W4C": 2}} + + +# clean/rr-f-wave5b/5c (fast mode only; the runtime's WG_RR_W5 mask): the relaxed body stages keeping the exact f32 +# elementwise/attention chains (RR_B32 1, RR_WIL 1: 0% RMS error for all finite values in [-448, +448]) with branchless clamped Q8 +# (RR_Q8_FAST 1, rr_fast.hlsli: no slow path, no wave votes, no RR_DEFER duplication; convergent merge guard in c64_w64 / wide_w64 / +# wide keeps narrow-EXEC WMMAs at 0). Bit 4 = level 2 (E2 / D2-F / D2-R wave64), bit 5 = level 0 (IN0 / D0-F wave64), +# bit 6 = level 1 (E1 / D1-F / D1-R wave64), bit 7 = level 3 (E3 / D3-F / D3-R wave32), bit 8 = level 4/5 (E4 / E5 / D4-F / D4-R +# wave32 with RR_B32G4 1, RR_PRE 14: zero scratch/LDS spills, 26.6 KB LDS, 153-160 VGPRs). +W5_Q8 = {"RR_Q8_FAST": 1} +W5_C32_DEFS = {"IN0": dict(W5_Q8, RR_W4C=6, RR_SMAJOR=2), + "D0-F": dict(W5_Q8, RR_W4C=2, RR_OVL=1)} +W5_C64_DEFS = {"E1": dict(W5_Q8), "D1-F": dict(W5_Q8), "D1-R": dict(W5_Q8)} +W5_WIDE_DEFS = {"E2": dict(W5_Q8), "D2-F": dict(W5_Q8), "D2-R": dict(W5_Q8)} +W5_WIDE32_DEFS = {"E3": dict(W5_Q8), "D3-F": dict(W5_Q8), "D3-R": dict(W5_Q8), + "E4": dict(W5_Q8, RR_B32G4=1, RR_PRE=14), "E5": dict(W5_Q8, RR_B32G4=1, RR_PRE=14), + "D4-F": dict(W5_Q8, RR_PRE=14), "D4-R": dict(W5_Q8, RR_PRE=14)} +# runtime kernel name -> (stage, builder, defines) +W5_KERNELS = (("stagef.IN0.w64", "IN0", "c32", W5_C32_DEFS), ("stagef.D0-F.w64", "D0-F", "c32", W5_C32_DEFS), + ("stagef.E1.w64", "E1", "c64", W5_C64_DEFS), ("stagef.D1-F.w64", "D1-F", "c64", W5_C64_DEFS), + ("stagef.D1-R.w64", "D1-R", "c64", W5_C64_DEFS), ("stagef.E2.w64", "E2", "wide", W5_WIDE_DEFS), + ("stagef.D2-F.w64", "D2-F", "wide", W5_WIDE_DEFS), ("stagef.D2-R.w64", "D2-R", "wide", W5_WIDE_DEFS), + ("stagef.E3", "E3", "wide32", W5_WIDE32_DEFS), ("stagef.D3-F", "D3-F", "wide32", W5_WIDE32_DEFS), + ("stagef.D3-R", "D3-R", "wide32", W5_WIDE32_DEFS), ("stagef.E4", "E4", "wide32", W5_WIDE32_DEFS), + ("stagef.E5", "E5", "wide32", W5_WIDE32_DEFS), ("stagef.D4-F", "D4-F", "wide32", W5_WIDE32_DEFS), + ("stagef.D4-R", "D4-R", "wide32", W5_WIDE32_DEFS)) + + +def kernel_w5(name, zmode, step="E", ldsw=1, extra=None): + """The dxil of the stagef.[.w64] kernel (clean/rr-f-wave5b/5c); extra: A/B defines on top.""" + for _, st, kind, defs in W5_KERNELS: + if st != name: + continue + d = dict(defs[name], **(extra or {})) + if kind in ("c32", "wide32"): + return kernel_for(name, zmode, step=step, ldsw=ldsw, extra=d) + return (kernel_c64_w64 if kind == "c64" else kernel_wide_w64)(name, zmode, step=step, ldsw=ldsw, extra=d) + raise KeyError(name) + + def kernel_dxil(name, zmode, step="E", ldsw=1): """The dxil the body uses for a stage.""" return kernel_for(name, zmode, step=step, ldsw=ldsw) diff --git a/clean/rr_f/head/dx12/rr_d0r.hlsl b/clean/rr_f/head/dx12/rr_d0r.hlsl index e8ddd06a..8be45eec 100644 --- a/clean/rr_f/head/dx12/rr_d0r.hlsl +++ b/clean/rr_f/head/dx12/rr_d0r.hlsl @@ -278,6 +278,9 @@ v8f rr_r16(v8f a) { return rr_widen(wg_to_f16_ags(a)); } #define RR_HR16(a) rr_r16(a) #endif +#ifndef RR_HQ8S +#define RR_HQ8S 0 // clean/rr-f-wave4 R4.1a: step 1's q / v^T tiles quantised per head (see there); head.fast.rx only +#endif #if RR_STRICT_STEP == 1 #include "../../kernels/rr_exact_math.hlsli" v8f rr_head_step(uint2 a0, uint2 b0, uint2 a1, uint2 b1, v8f c) @@ -557,6 +560,32 @@ WG_KERNEL(rr_d0r_head) uint2 fq[8], fv[8]; [unroll] for (uint i0 = 0; i0 < 8; ++i0) { fq[i0] = rr_frag(F_Q + i0, lane); fv[i0] = rr_frag(F_V + i0, lane); } RR_AB(AX, xb0, xb1) +#if RR_HQ8S + // clean/rr-f-wave4 R4.1a: the q / v^T tiles of the two heads quantised in two groups of four (RR_Q8 decides its + // wave-uniform clamp test per group; both paths give Q8's bits for every value, so each code is the same as + // from the one group of eight) with head 0's tiles dead before head 1's are formed: 32 fewer live accumulator VGPRs + uint2 qv[8]; + [unroll] for (uint h0 = 0; h0 < 2; ++h0) { + v8f qvt[4]; // [0] q ft0, [1] q ft1, [2] vT ft0, [3] vT ft1 of head h0 + [unroll] for (uint f0 = 0; f0 < 2; ++f0) { + const uint b = (h0 * 2u + f0) * 2u; +#if RR_STRICT_STEP + v8f a = RR_HSTEP(RR_WAB(b >> 1), AX, fq[b], xb0, fq[b + 1u], xb1, sw_zero8()); + v8f c = RR_HSTEP(AX, RR_WAB(4u + (b >> 1)), xb0, fv[b], xb1, fv[b + 1u], sw_zero8()); +#else + v8f a = wg_wmma_fp8(fq[b], xb0, sw_zero8()); + a = wg_wmma_fp8(fq[b + 1u], xb1, a); + v8f c = wg_wmma_fp8(xb0, fv[b], sw_zero8()); + c = wg_wmma_fp8(xb1, fv[b + 1u], c); +#endif + qvt[f0] = RR_HR16(a); + qvt[2u + f0] = RR_HR16(c); + } + uint2 q4[4]; + RR_Q8(4u, qvt, q4); + [unroll] for (uint i7 = 0; i7 < 4; ++i7) qv[4u * h0 + i7] = q4[i7]; + } +#else v8f qvt[8]; // per head h: [4h] q ft0, [4h+1] q ft1, [4h+2] vT ft0, [4h+3] vT ft1 [unroll] for (uint h0 = 0; h0 < 2; ++h0) [unroll] for (uint f0 = 0; f0 < 2; ++f0) { @@ -578,6 +607,7 @@ WG_KERNEL(rr_d0r_head) } uint2 qv[8]; RR_Q8(8u, qvt, qv); +#endif [unroll] for (uint s0 = 0; s0 < 8; ++s0) SW_LDS_PUT((((s0 >> 2) * 4u + wv) * 4u + (s0 & 3u)) * 32u + lane, qv[s0]); wg_barrier_lds(); diff --git a/clean/rr_f/head/dx12/rr_d0r_w64.hlsl b/clean/rr_f/head/dx12/rr_d0r_w64.hlsl new file mode 100644 index 00000000..541b0b69 --- /dev/null +++ b/clean/rr_f/head/dx12/rr_d0r_w64.hlsl @@ -0,0 +1,766 @@ +// SPDX-License-Identifier: MIT +// clean/rr-f-wave4b R4b.1: D0-R of spec 55 in wave64 (design D of kernels/rr_block_c32.hlsl), the relaxed head of +// head.fast.rx only: rr_d0r.hlsl with RR_HVARL 7, RR_S9 61, RR_OUT_PREP 1, RR_HSTM 1, RR_HQ8S 1, RR_HEAD18 1, +// WG_BLOCK 128, WG_WAVE 64 (the file refuses any other configuration). Same arguments, buffer layouts and output bytes. +// +// One workgroup = one 8 x 8 window and two wave64 waves; wave w owns the window tokens of the wave32 waves wv = 2 w +// (tile 0) and 2 w + 1 (tile 1) of rr_d0r.hlsl (16 tokens each, token = 16 wv + r). Wave64 lane L = l + 32 hh holds, +// for each tile, half hh of wave32 lane l's fragment (l = 16 hf + r): fp8 A / B bytes 4 hh .. 4 hh + 3, accumulator +// elements 4 hh .. 4 hh + 3 (rr_block_c32.hlsl, the v_wmma wave64 layout). A v8f holds elements 0-3 of tile 0 and 0-3 +// of tile 1; a uint2 fp8 fragment: x = tile 0, y = tile 1; a uint4 of packed f16: x, y = tile 0, z, w = tile 1. Every +// value is computed by the operation rr_d0r.hlsl computes it with (wave64 v_wmma = wave32 v_wmma bit for bit, the +// conversions are per value), so the stored bytes are those of the wave32 kernel; what moves is only where a value +// sits: +// * the weight fragments (shared by both tiles) are loaded once, 4 bytes per lane; so are the key / V^T fragments +// of the q / v exchange (every wave reads the same data) and the biases; +// * the row sum Z is a sequential f32 dot2add chain over the 32 weights of a lane in wave32 (order J, then the 8 +// elements): a lane of wave32 holds elements 0-7 of every key tile, a wave64 lane only 4 of them for two tiles. +// The other half's words of the lane's own tile are fetched from lane ^ 32 (hh 0 runs tile 0's chain, hh 1 tile 1's) +// so the chain, and the z + z(lane ^ 16) that follows, are the wave32 operations on the wave32 operands, and the +// reciprocal of the other tile comes back through one more lane ^ 32 exchange; +// * the head tail (55.2 / 55.3, stores, render records) is rr_d0r.hlsl's text, run on each 32-lane half of the +// wave64 for its tile: before it the lane gets the y2h words of its tile that the other half holds (lane ^ 32), so +// Y0 = channels 8 hf .. 8 hf + 7 and Y1.x = channels 16 + 8 hf, 17 + 8 hf (the only y2h[1] word the tail reads) +// are the wave32 lane's words. +// Wave-uniform votes (the Q8 clamp test, the activation's rare path, the record flag) cover both tiles: both forms of +// those groups give the same bits for every value, and the flag word is an OR. +// +// clean/rr-f-wave5 RR_HFAST_ALU 1 (opt-in; fast mode only, the runtime kernel head.fast.rx.f.w64; NOT bit-exact to head.fast.rx.w64): +// "network arithmetic may be relaxed, history logic stays exact". +// * the 13 random 128 - 256 KB gathers of the tail (sig x 5, tanh x 2, exp2 x 4, rcp, rho) are bounded hardware f32 ALU work: sig +// = rcp(1 + exp2(clamp(-x, -80, 80) log2 e)) (finite at +-inf and for every |x|: +1 / 0), tanh = sign (1 - e) rcp(1 + e), e = +// exp2(-2 log2 e min(|x|, 10)) (always within [-1, 1], at most 1 f16 ulp), rho = that tanh of x * s2 (s2 = the f32 bits in +// the otherwise unused p_dz argument; the runtime derives it for model files without head.k_s2), CANDW = the 4-way softmax +// exp2((z - zmax) log2 e) / sum in f32. The Cnet history blend (55.2) is the text of head.fast.rx.w64, f16 roundings and fences kept. +// * Q8: every group is the clamp to +-448 and the driver conversion (no wave vote, no slow path, no kept pre-activations). +// * with a render-record scratch (p_oscr != 0, always for the runtime): no CANDW store (output.fast.f reads the record's W +// halves; OFX_NOCHK output.fallback.f never runs), and no subnormal-band check / flag word. Cnet, HID, PREC and GATES are stored as before. +#ifndef RR_HFAST_ALU +#define RR_HFAST_ALU 0 +#endif +#include "wg_platform.hlsli" +#if WG_WAVE != 64 || WG_BLOCK != 128 +#error "rr_d0r_w64.hlsl: WG_BLOCK 128, WG_WAVE 64 only" +#endif +#if !defined(RR_HVARL) || RR_HVARL != 7 || !defined(RR_S9) || RR_S9 != 61 || !defined(RR_OUT_PREP) || RR_OUT_PREP != 1 || \ + !defined(RR_HSTM) || RR_HSTM != 1 || !defined(RR_HQ8S) || RR_HQ8S != 1 +#error "rr_d0r_w64.hlsl: the head.fast.rx configuration only (RR_HVARL 7, RR_S9 61, RR_OUT_PREP 1, RR_HSTM 1, RR_HQ8S 1)" +#endif +#ifdef RR_STRICT_STEP +#if RR_STRICT_STEP != 0 +#error "rr_d0r_w64.hlsl: RR_STRICT_STEP 0 only" +#endif +#endif +#include "wg_ags.hlsli" +wg_frag wg_frag_v8f(v8f c) { wg_frag f; [unroll] for (uint i = 0; i < 8; ++i) f.v[i] = asuint(c.v[i]); return f; } +v8f wg_v8f_frag(wg_frag f) { v8f c; [unroll] for (uint i = 0; i < 8; ++i) c.v[i] = asfloat(f.v[i]); return c; } +wg_frag rr_w64f(uint a, uint b, uint c, uint d) // a wave64 fragment: the first 4 / 2 / 1 dwords are used +{ + wg_frag f; + f.v[0] = a; f.v[1] = b; f.v[2] = c; f.v[3] = d; + [unroll] for (uint i = 4; i < 8; ++i) f.v[i] = 0u; + return f; +} +v8f wg_wmma_fp8(uint2 a, uint2 b, v8f c) // tile 0: (a.x, b.x, c 0-3), tile 1: (a.y, b.y, c 4-7) +{ + const wg_frag r0 = wg_ags_mma(WG_AGS_FP8_F32, rr_w64f(a.x, 0u, 0u, 0u), rr_w64f(b.x, 0u, 0u, 0u), + rr_w64f(asuint(c.v[0]), asuint(c.v[1]), asuint(c.v[2]), asuint(c.v[3]))); + const wg_frag r1 = wg_ags_mma(WG_AGS_FP8_F32, rr_w64f(a.y, 0u, 0u, 0u), rr_w64f(b.y, 0u, 0u, 0u), + rr_w64f(asuint(c.v[4]), asuint(c.v[5]), asuint(c.v[6]), asuint(c.v[7]))); + v8f d; + [unroll] for (uint i = 0; i < 4; ++i) { d.v[i] = asfloat(r0.v[i]); d.v[4 + i] = asfloat(r1.v[i]); } + return d; +} +uint2 wg_pack_q8_nc_ags(v8f d) // v_cvt_pk_fp8_f32 per value pair, element i -> byte i of its tile's dword +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(asuint(d.v[0]), asuint(d.v[1]), asuint(d.v[2]), asuint(d.v[3])), + WG_AGS_T_F32_ACC, WG_AGS_T_FP8_B); + const wg_frag r1 = wg_ags_convert(rr_w64f(asuint(d.v[4]), asuint(d.v[5]), asuint(d.v[6]), asuint(d.v[7])), + WG_AGS_T_F32_ACC, WG_AGS_T_FP8_B); + return uint2(r0.v[0], r1.v[0]); +} +uint4 wg_to_f16_ags(v8f d) // v_cvt_f16_f32 per value (round to nearest even) +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(asuint(d.v[0]), asuint(d.v[1]), asuint(d.v[2]), asuint(d.v[3])), + WG_AGS_T_F32_ACC, WG_AGS_T_F16_ACC); + const wg_frag r1 = wg_ags_convert(rr_w64f(asuint(d.v[4]), asuint(d.v[5]), asuint(d.v[6]), asuint(d.v[7])), + WG_AGS_T_F32_ACC, WG_AGS_T_F16_ACC); + return uint4(r0.v[0], r0.v[1], r1.v[0], r1.v[1]); +} +v8f rr_d8(uint2 q) // e4m3 bytes -> f32 (exact), one conversion per tile +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(q.x, 0u, 0u, 0u), WG_AGS_T_FP8_B, WG_AGS_T_F32_ACC); + const wg_frag r1 = wg_ags_convert(rr_w64f(q.y, 0u, 0u, 0u), WG_AGS_T_FP8_B, WG_AGS_T_F32_ACC); + v8f d; + [unroll] for (uint i = 0; i < 4; ++i) { d.v[i] = asfloat(r0.v[i]); d.v[4 + i] = asfloat(r1.v[i]); } + return d; +} +v8f zero8() { return wg_splat(0.0f); } + +#ifndef RR_P55_ERRATUM +#define RR_P55_ERRATUM 1 +#endif +#define RR_HEAD18 1 +#define RR_HCVB 1 +#define RR_HTRIM 7 + +WG_ARGS_BEGIN + uint p_x, p_wf, p_bias, p_tb, p_hold, p_hc, p_tab, p_rho; // arena byte offsets (0 = null) + uint p_cnet, p_hid, p_gates, p_prec, p_candw; + uint p_dy, p_dimg, p_dz, p_zs; + uint Wt, Ht, Wr, Hr, sx, sy, nwx; + uint k_as, k_acl, k_acln, k_p1, k_p0, k_actcl, k_actcln, k_c1, k_c2, k_c3, k_ra, k_m, k_g; // f16 bits + uint k_s1; // f32 bits + uint p_flag; + uint p_wpl; + uint p_oscr, o_wh, o_hh, k_o2n, k_o2x2n, k_o4, k_o5, k_o7; // output scratch, history size, out.K2n / K2x2n / K4 / K5 / K7 (f32) +WG_ARGS_END + +// Weight fragments (256 B each = 32 lanes x 8 B; lane l, byte i = W[16 ks + 8 (l >> 4) + i][16 nt + (l & 15)]). +#define F_Q 0u // Wq [h][ft][ks] +#define F_V 8u // Wv [h][ft][ks] +#define F_O 16u // Wo [h][ct][ks] +#define F_1 24u // W1 [nt 8][ks 2] +#define F_2 40u // W2 [ct 2][ks 8] +// p_bias: f32 copies of bo [32] (byte 0), b1 [128] (byte 128), b2 [32] (byte 640) +// p_tab: sig of 51.4 (f16 [65536], byte 0), R(tanh) (f16 [65536], byte 131072); p_rho: rho (f32 [65536]) for this s2 +#define TAB_TANH 131072u +#define TAB_EXP2 262144u // 55-ERRATUM-2: R(2^t) for every f16 t (clean/rr-f-flow) +#define TAB_RCP16 393216u // 55-ERRATUM-2: R(1/v) for every f16 v + +uint rr_zero() { return 0u - (wg_grid_ >> 31); } // 0 with no known bits (README "Driver exceptions") +uint rr_fence(uint v) { return v ^ rr_zero(); } +uint4 rr_fence4(uint4 v) { return v ^ rr_zero(); } +half2 H2(uint v) { return wg_as_h2(v); } +uint U2(half2 h) { return wg_h2_bits(h); } +half2 HF(half2 h) { return H2(rr_fence(U2(h))); } // fenced f16 pair: a rounding point stays a rounding point +float FF(float f) { return asfloat(rr_fence(asuint(f))); } +uint rr_u(uint p) { return WaveReadLaneFirst(p); } // a pointer argument read as wave-uniform (store-merge hazard) +#if RR_HFAST_ALU >= 2 +uint rr_f2h(float x) { return (uint)asuint16((half)x); } +float rr_ofence(float x) { return x; } +float rr_ocf(float x) +{ + return log2(max(mad(x, asfloat(k_o4), 1.0f), asfloat(k_o5))) * (asfloat(0x3e9a209bu) * asfloat(k_o7)); +} +float rr_od255(uint b) { return (float)b * asfloat(0x3b808081u); } +#else +uint rr_f2h(float x) { return wg_ags_call(0x1cu, 0u, 1u, asuint(x), 0u); } // hardware RNE f32 -> f16 (op 0x1C imm 1) +// rr_output.hlsl / rr_output_fast.hlsli's fence, Cf, Mq and of_d255, operation for operation +float rr_ofence(float x) { return asfloat(asuint(x) ^ (0u - (wg_grid_ >> 31))); } +float rr_ocf(float x) +{ + return rr_ofence(rr_ofence(log2(max(mad(x, asfloat(k_o4), 1.0f), asfloat(k_o5))) * asfloat(0x3e9a209bu)) * asfloat(k_o7)); +} +float rr_od255(uint b) +{ + const float a = (float)b, rc = asfloat(0x3b808081u); + const float q = a * rc; + return mad(mad(-255.0f, q, a), rc, q); +} +#endif +float rr_omq(float a, float b, inout bool rare) +{ + const float r = rr_ofence(a * b); + [branch] if (r == 0) { + const float s2 = rr_ofence(a * asfloat(0x5f800000u)) * b; + rare = or(rare, and(abs(s2) >= asfloat(0x207ffff0u), abs(s2) <= asfloat(0x20800000u))); + } + return r; +} +half2 rr_c2(uint b) { return H2((b & 0xffffu) * 0x10001u); } + +v8f rr_widen(uint4 p) +{ + v8f r; + r.v[0] = f16tof32(p.x); r.v[1] = f16tof32(p.x >> 16); r.v[2] = f16tof32(p.y); r.v[3] = f16tof32(p.y >> 16); + r.v[4] = f16tof32(p.z); r.v[5] = f16tof32(p.z >> 16); r.v[6] = f16tof32(p.w); r.v[7] = f16tof32(p.w >> 16); + return r; +} + +// Border rule of 51.6 for extents >= 8 (level 0 and level 1 here: Wt, Ht >= 32): the single reflection. +int rr_refl(int v, int n) { const int a = abs(v); return a >= n ? 2 * (n - 1) - a : a; } + +// speed_swin.hlsli's Q8 helpers (the same text): the clamped pack, the wave-uniform skip of the clamp +uint2 sw_pack_q8_v2(v8f d) +{ + v8f c; + [unroll] for (uint i = 0; i < 8; ++i) c.v[i] = max(min(d.v[i], 448.0f), -448.0f); + const uint2 pc = wg_pack_q8_nc_ags(c), px = wg_pack_q8_nc_ags(d); + const uint2 m = px & ~pc & 0x80808080u; + return pc | m | (m >> 7); +} +float sw_absmax8(float m, v8f d) +{ + return max(max(max(m, abs(d.v[0])), max(abs(d.v[1]), abs(d.v[2]))), + max(max(abs(d.v[3]), abs(d.v[4])), max(max(abs(d.v[5]), abs(d.v[6])), abs(d.v[7])))); +} +bool sw_in448(float m) { return WaveActiveAllTrue(m <= 448.0f); } +// Q8 of 51.1 for groups of tiles of f16 values held in f32: saturation at +-448, RNE; one wave-uniform test skips the +// clamp when no value of the wave needs it (NaN -> 0xff, see DESIGN-55 section 4). +#if RR_HFAST_ALU +// RR_HFAST_ALU: every Q8 group is the clamp to +-448 (a NaN gives a bound) and the driver conversion +#define RR_Q8(N, src, dst) \ + { \ + [unroll] for (uint qj_ = 0; qj_ < N; ++qj_) { \ + v8f qc_; \ + [unroll] for (uint qi_ = 0; qi_ < 8; ++qi_) qc_.v[qi_] = max(min(src[qj_].v[qi_], 448.0f), -448.0f); \ + dst[qj_] = wg_pack_q8_nc_ags(qc_); \ + } \ + } +#else +#define RR_Q8(N, src, dst) \ + { \ + float am_ = 0.0f; \ + [unroll] for (uint qi_ = 0; qi_ < N; ++qi_) am_ = sw_absmax8(am_, src[qi_]); \ + if (sw_in448(am_)) { [unroll] for (uint qj_ = 0; qj_ < N; ++qj_) dst[qj_] = wg_pack_q8_nc_ags(src[qj_]); } \ + else { [unroll] for (uint qk_ = 0; qk_ < N; ++qk_) dst[qk_] = sw_pack_q8_v2(src[qk_]); } \ + } +#endif + +// 51.3 step 3 on a pair of f16 scores: u = R(s A_SCALE), clamp (a NaN gives A_CLAMPn), p = R(c R(A_P1 - c c) + A_P0), +// w = 2^(n-15) (1 + y - n), y = 32 (p - 1): for p in [1 + 1/32, 1 + 31/32) the f16 bits of w are (bits(p) << 5) & 0x7fe0. +uint rr_attw(uint sh, half2 as2, half2 acln2, half2 acl2, half2 p12, half2 p02) +{ + const half2 u = H2(sh) * as2; + const half2 c = min(max(u, acln2), acl2); + const half2 inner = mad(-c, c, p12); + const half2 p = mad(c, inner, p02); + const uint16_t2 pb = asuint16(p) << (uint16_t)5; + return ((uint)pb.x | ((uint)pb.y << 16)) & (0x7fe07fe0u & ~rr_zero()); +} +// Q8 of 8 packed f16 values known finite and within +-448 (P) through the driver's f16 -> fp8 conversion; per tile +// (h.xy = tile 0, h.zw = tile 1) +uint2 rr_q8h_small(uint4 h) +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(h.x, h.y, 0u, 0u), WG_AGS_T_F16_ACC, WG_AGS_T_FP8_B); + const wg_frag r1 = wg_ags_convert(rr_w64f(h.z, h.w, 0u, 0u), WG_AGS_T_F16_ACC, WG_AGS_T_FP8_B); + return uint2(r0.v[0], r1.v[0]); +} +// The same conversion for any f16 values, with RR_Q8's result (rr_d0r.hlsl rr_q8h) +uint2 rr_q8h(uint4 h, inout uint bad) +{ + const uint2 q = rr_q8h_small(h); + bad |= ((q.x & 0x7f7f7f7fu) + 0x01010101u) | ((q.y & 0x7f7f7f7fu) + 0x01010101u); + return q; +} + +// 51.3 step 8 activation on a pair: k = clamp, u = R(C2 - |k| C1), v = R(k u + C3), act = R(x v) +uint rr_act(uint xh, half2 cln2, half2 cl2, half2 c12, half2 c22, half2 c32) +{ + const half2 x = H2(xh); + const half2 k = min(max(x, cln2), cl2); + const half2 ak = max(k, -k); + const half2 u = mad(-ak, c12, c22); + const half2 v = mad(k, u, c32); + return U2(x * v); // consumed by rr_q8h (no fold into that conversion) or fenced before a widening +} + +// sigx(dd) / 2 = 0.5 / (1 + e^-dd) in f32 (CANDW, 55.3: within about 2 f32 ulp before the rounding to f16). +#if RR_HFAST_ALU +// the bounded f32 forms of the table lookups (RR_HFAST_ALU, head comment): sigmoid or tanh of x with one exp2 and one +// reciprocal either way; th selects tanh (a wave-uniform-free select, both lanes' halves run the same instructions) +float rr_sigtanh(float x, bool th) +{ + const float ax = min(abs(x), 10.0f); + const float a = th ? -2.8853900817779268f * ax : min(max(-x, -80.0f), 80.0f) * 1.4426950408889634f; + const float e = exp2(a); + const float r = rcp(1.0f + e); + const float t = (1.0f - e) * r; + return th ? asfloat(asuint(t) | (asuint(x) & 0x80000000u)) : r; // t >= 0: the sign of x (also -0) is one bit-or +} +float rr_tanhf(float x) { return rr_sigtanh(x, true); } +uint rr_sig16(uint h) { return rr_f2h(rr_sigtanh(f16tof32(h), false)); } +uint rr_tanh16(uint h) { return rr_f2h(rr_sigtanh(f16tof32(h), true)); } +// the three packed 55.2 clamp of a Q8 group of packed f16 values: v_pk_max_f16 / v_pk_min_f16 +uint rr_c448(uint v) { return U2(min(max(H2(v), half2(-448.0, -448.0)), half2(448.0, 448.0))); } +#endif +float rr_lhalf(float dd) +{ + const float t = min(max(-dd, -100.0f), 87.0f); // e^t stays finite; beyond, the f16 result is 1/2 or 0 anyway + const float L = asfloat(0x3fb8aa3bu), Llo = asfloat(0x32a57060u), LN2 = asfloat(0x3f317218u); + const float th = t * L; + const float tl = mad(t, L, -th) + t * Llo; // t log2(e) = th + tl + const float e0 = exp2(th); + const float e = mad(e0, tl * LN2, e0); + const float den = 1.0f + e; + float y = rcp(den); + y = mad(mad(-den, y, 1.0f), y, y); + const float v = 0.5f * y; + return isnan(dd) ? dd : v; +} + +// 8-bit stores of 55.3: unsigned normalised (NaN -> 0 by the max), HID (page 53.3 as quoted on 55.3) +uint rr_u8(uint h) { const float c = min(max(f16tof32(h), 0.0f), 1.0f); return (uint)round(c * 255.0f); } +uint rr_s8(uint h) +{ + const float f = f16tof32(h); + const float c = isnan(f) ? 0.0f : min(max(f, -1.0f), 1.0f); + return ((uint)(int)round(c * 127.0f)) & 0xffu; +} + +// the q / v exchange: the same 8192 bytes as rr_d0r.hlsl's ([head 2][wave 4][frag 4: q ft0, q ft1, vT ft0, vT ft1][lane +// 32] x 8 B), addressed per dword: wave32 slot i (8 bytes) = dwords 2 i, 2 i + 1; a wave64 lane holds dword 2 i + hh +groupshared uint rr_xq32[2048]; + +// per-lane offsets, fenced (the same value with no known bits) so that the compiler adds them to the base once and +// keeps the constant parts in the instructions' offset fields +static uint rr_lo; // lp * 8 + 4 hh: byte of a fragment-ordered 8-byte slot +static uint rr_xo; // 2 lp + hh: dword of an LDS slot row +static uint rr_t4; // 8 hf + 4 hh: byte within a 16-channel group of a token / an output tile row +uint wfrag1(uint base, uint f) { return wg_ld32(base + rr_lo, f * 256u); } +uint2 wfrag(uint base, uint f) { const uint w = wfrag1(base, f); return uint2(w, w); } // shared by both tiles +v8f rr_dup4(float4 a) +{ + v8f r; + r.v[0] = a.x; r.v[1] = a.y; r.v[2] = a.z; r.v[3] = a.w; r.v[4] = a.x; r.v[5] = a.y; r.v[6] = a.z; r.v[7] = a.w; + return r; +} +v8f rr_cat4(float4 a, float4 b) +{ + v8f r; + r.v[0] = a.x; r.v[1] = a.y; r.v[2] = a.z; r.v[3] = a.w; r.v[4] = b.x; r.v[5] = b.y; r.v[6] = b.z; r.v[7] = b.w; + return r; +} +v8f bias_tile(uint p, uint off) // per tile the same bias fragment: bytes off + 4 (8 hf + 4 hh) .. +{ + return rr_dup4(wg_ld4f(p + 4u * rr_t4, off)); +} +// per tile: this lane's dword of the 8-byte group at byte o0 (tile 0) / o1 (tile 1) +uint2 rr_tld(uint base, uint o0, uint o1) { return uint2(wg_ld32(base + rr_t4, o0), wg_ld32(base + rr_t4, o1)); } +uint rr_xqs(uint row) { return rr_xq32[rr_xo + 64u * row]; } // dword of the wave32 slot (row * 32 + lp): a shared operand + +WG_KERNEL(rr_d0r_head) +{ + WG_SETUP(); + const uint L = WaveGetLaneIndex(); + const uint lp = L & 31u, r = L & 15u, hf = (L >> 4) & 1u, hh = L >> 5; + rr_lo = rr_fence(lp * 8u + 4u * hh); + rr_xo = rr_fence(lp * 2u + hh); + rr_t4 = rr_fence(8u * hf + 4u * hh); + const uint wv0 = 2u * WaveReadLaneFirst(wg_lane_id() >> 6); // wave32 wave of tile 0 (tile 1: wv0 + 1) + const uint win = wg_block_id(); + const uint wj = win / nwx, wi = win - wj * nwx; + const int X = (int)(8u * wi + (r & 7u)) - (int)(sx & 0xffu); + const bool hmode = ((sx >> 8) & 1u) != 0u; // 18-channel reading (RR_HEAD18) + int Yt[2]; + uint tokt[2]; + [unroll] for (uint t0 = 0; t0 < 2u; ++t0) { + Yt[t0] = (int)(8u * wj + 2u * (wv0 + t0) + (r >> 3)) - (int)sy; + tokt[t0] = (uint)rr_refl(Yt[t0], (int)Ht) * Wt + (uint)rr_refl(X, (int)Wt); + } + + uint4 y2h[2]; + // ---------------------------------------------------------------- 51.3 step 1: q_h, v_h (both heads) + const uint2 xb0 = rr_tld(p_x, tokt[0] * 32u, tokt[1] * 32u), xb1 = rr_tld(p_x, tokt[0] * 32u + 16u, tokt[1] * 32u + 16u); + uint fq[8], fv[8]; + [unroll] for (uint i0 = 0; i0 < 8; ++i0) { fq[i0] = wfrag1(p_wf, F_Q + i0); fv[i0] = wfrag1(p_wf, F_V + i0); } + // the q / v^T tiles of the two heads quantised in two groups of four (rr_d0r.hlsl RR_HQ8S) + uint2 qv[8]; + [unroll] for (uint h0 = 0; h0 < 2; ++h0) { + v8f qvt[4]; // [0] q ft0, [1] q ft1, [2] vT ft0, [3] vT ft1 of head h0 + [unroll] for (uint f0 = 0; f0 < 2; ++f0) { + const uint b = (h0 * 2u + f0) * 2u; + v8f a = wg_wmma_fp8(uint2(fq[b], fq[b]), xb0, zero8()); + a = wg_wmma_fp8(uint2(fq[b + 1u], fq[b + 1u]), xb1, a); + v8f c = wg_wmma_fp8(xb0, uint2(fv[b], fv[b]), zero8()); + c = wg_wmma_fp8(xb1, uint2(fv[b + 1u], fv[b + 1u]), c); + qvt[f0] = a; + qvt[2u + f0] = c; + } + uint2 q4[4]; + RR_Q8(4u, qvt, q4); + [unroll] for (uint i7 = 0; i7 < 4; ++i7) qv[4u * h0 + i7] = q4[i7]; + } + // slot ((head 4 + wave 4) + f) 32 + lp of wave32 wave wv0 + t: this lane's dword of tile t's 8-byte slot + [unroll] for (uint s0 = 0; s0 < 8; ++s0) { + const uint rb = ((s0 >> 2) * 4u + wv0) * 4u + (s0 & 3u); + rr_xq32[rr_xo + 64u * rb] = qv[s0].x; + rr_xq32[rr_xo + 64u * (rb + 4u)] = qv[s0].y; + } + GroupMemoryBarrierWithGroupSync(); + + // ---------------------------------------------------------------- steps 2-5 per head + const half2 as2 = rr_c2(k_as), acl2 = rr_c2(k_acl), acln2 = rr_c2(k_acln), p12 = rr_c2(k_p1), p02 = rr_c2(k_p0); + const half2 one2 = H2(0x3c003c00u); + const bool tl1 = hh != 0u; +#if RR_HFAST_ALU >= 2 + uint2 of[4]; // quantise per head right after step 5 (drops 12 live VGPRs across head 1; bit-identical to post-loop RR_Q8!) +#else + v8f ot[4]; // O_h tiles before Q8: [2h + ft] +#endif + [unroll] for (uint h = 0; h < 2; ++h) { + uint kq[4][2], vt[4][2]; + v8f bc[4]; + [unroll] for (uint J = 0; J < 4; ++J) { + const uint rr0 = (h * 4u + J) * 4u; + kq[J][0] = rr_xqs(rr0); kq[J][1] = rr_xqs(rr0 + 1u); + vt[J][0] = rr_xqs(rr0 + 2u); vt[J][1] = rr_xqs(rr0 + 3u); + // the score bias, per tile (wave32 wave wv0 + t): 8 floats of the wave32 lane, this lane's half + const uint tb0 = (((h * 4u + wv0) * 4u + J) * 32u) * 32u, tb1 = tb0 + 4u * 32u * 32u; // wave wv0 + 1: 4 (J) x 32 lanes x 32 B on + bc[J] = rr_cat4(wg_ld4f(p_tb + 4u * rr_lo, tb0), wg_ld4f(p_tb + 4u * rr_lo, tb1)); + } + // scores (keys of tile J as rows, the wave's queries as columns), start value = the score bias, 51.3 step 2 + uint4 w4[4]; + v8f s4[4]; + [unroll] for (uint J7 = 0; J7 < 4; ++J7) s4[J7] = wg_wmma_fp8(uint2(kq[J7][0], kq[J7][0]), qv[4u * h], bc[J7]); + [unroll] for (uint J8 = 0; J8 < 4; ++J8) s4[J8] = wg_wmma_fp8(uint2(kq[J8][1], kq[J8][1]), qv[4u * h + 1u], s4[J8]); + [unroll] for (uint J9 = 0; J9 < 4; ++J9) { + const uint4 sh = wg_to_f16_ags(s4[J9]); + w4[J9] = uint4(rr_attw(sh.x, as2, acln2, acl2, p12, p02), rr_attw(sh.y, as2, acln2, acl2, p12, p02), + rr_attw(sh.z, as2, acln2, acl2, p12, p02), rr_attw(sh.w, as2, acln2, acl2, p12, p02)); + } + // step 4: Z in f32 (the spec's accepted form), r = R(1 / Z), P = Q8(R(w r)). Wave32's chain over the lane's 32 + // weights (J, then elements 0-7) on this lane's own tile (hh 0: tile 0, hh 1: tile 1): the other half's words of + // that tile come from lane ^ 32 (the lane sends the other tile's words of its half) + float z = 0.0f; + [unroll] for (uint J2 = 0; J2 < 4; ++J2) { + const uint4 w = w4[J2]; + const uint sx2 = tl1 ? w.x : w.z, sy2 = tl1 ? w.y : w.w; + const uint rx = wg_xor_u(sx2, 32u), ry = wg_xor_u(sy2, 32u); + const uint a0 = tl1 ? rx : w.x, a1 = tl1 ? ry : w.y, a2 = tl1 ? w.z : rx, a3 = tl1 ? w.w : ry; + z = dot2add(H2(a0), one2, z); z = dot2add(H2(a1), one2, z); + z = dot2add(H2(a2), one2, z); z = dot2add(H2(a3), one2, z); + } + z = z + wg_xor16(z); + const uint rh = rr_f2h(rcp(z)); + const uint rhx = wg_xor_u(rh, 32u); // the other tile's R(1 / Z) + const half2 r2a = H2((tl1 ? rhx : rh) * 0x10001u), r2b = H2((tl1 ? rh : rhx) * 0x10001u); // tile 0, tile 1 + uint2 pf[4]; + [unroll] for (uint J3 = 0; J3 < 4; ++J3) { + const uint4 pw = uint4(U2(H2(w4[J3].x) * r2a), U2(H2(w4[J3].y) * r2a), U2(H2(w4[J3].z) * r2b), + U2(H2(w4[J3].w) * r2b)); + pf[J3] = rr_q8h_small(pw); // P <= 1 + 2^-11: no saturation + } + // step 5: O_h = Q8(MM(P; v_h; 0)), keys 0-31 then 32-63 + [unroll] for (uint f1 = 0; f1 < 2; ++f1) { + v8f a = wg_wmma_fp8(uint2(vt[0][f1], vt[0][f1]), pf[0], zero8()); + a = wg_wmma_fp8(uint2(vt[1][f1], vt[1][f1]), pf[1], a); + a = wg_wmma_fp8(uint2(vt[2][f1], vt[2][f1]), pf[2], a); + a = wg_wmma_fp8(uint2(vt[3][f1], vt[3][f1]), pf[3], a); +#if RR_HFAST_ALU >= 2 + v8f qc_; + [unroll] for (uint qi_ = 0; qi_ < 8; ++qi_) qc_.v[qi_] = max(min(a.v[qi_], 448.0f), -448.0f); + of[2u * h + f1] = wg_pack_q8_nc_ags(qc_); +#else + ot[2u * h + f1] = a; +#endif + } + } +#if RR_HFAST_ALU < 2 + uint2 of[4]; + RR_Q8(4u, ot, of); +#endif + + // ---------------------------------------------------------------- step 6: Dp, chain over the heads, start bo + uint fo[8]; + [unroll] for (uint i1 = 0; i1 < 8; ++i1) fo[i1] = wfrag1(p_wf, F_O + i1); + v8f dpf[2]; + [unroll] for (uint c0 = 0; c0 < 2; ++c0) { + v8f a = bias_tile(p_bias, 64u * c0); + a = wg_wmma_fp8(uint2(fo[c0 * 2u], fo[c0 * 2u]), of[0], a); + a = wg_wmma_fp8(uint2(fo[c0 * 2u + 1u], fo[c0 * 2u + 1u]), of[1], a); + a = wg_wmma_fp8(uint2(fo[4u + c0 * 2u], fo[4u + c0 * 2u]), of[2], a); + a = wg_wmma_fp8(uint2(fo[4u + c0 * 2u + 1u], fo[4u + c0 * 2u + 1u]), of[3], a); + dpf[c0] = a; + } + // ---------------------------------------------------------------- step 7: y1 = Dp ra + d8(x), y1q + uint2 y1q[2]; + v8f y1f[2]; + { + const float ra = f16tof32(k_ra & 0xffffu); + const v8f x0 = rr_d8(xb0), x1 = rr_d8(xb1); // exact + [unroll] for (uint i5 = 0; i5 < 8; ++i5) { + y1f[0].v[i5] = mad(dpf[0].v[i5], ra, x0.v[i5]); + y1f[1].v[i5] = mad(dpf[1].v[i5], ra, x1.v[i5]); + } + RR_Q8(2u, y1f, y1q); + } + // ---------------------------------------------------------------- step 8: MLP in 4 chunks of 32 + const half2 cl2 = rr_c2(k_actcl), cln2 = rr_c2(k_actcln), c12 = rr_c2(k_c1), c22 = rr_c2(k_c2), c32 = rr_c2(k_c3); + v8f ddf[2]; + uint w1[4], w2[4]; + [unroll] for (uint i2 = 0; i2 < 4; ++i2) { w1[i2] = wfrag1(p_wf, F_1 + i2); w2[i2] = wfrag1(p_wf, F_2 + (i2 >> 1) * 8u + (i2 & 1u)); } + [unroll] for (uint j = 0; j < 4; ++j) { + uint n1[4] = { 0u, 0u, 0u, 0u }, n2[4] = n1; + if (j < 3u) { + [unroll] for (uint i3 = 0; i3 < 4; ++i3) { + n1[i3] = wfrag1(p_wf, F_1 + 4u * (j + 1u) + i3); + n2[i3] = wfrag1(p_wf, F_2 + (i3 >> 1) * 8u + 2u * (j + 1u) + (i3 & 1u)); + } + } +#if RR_HFAST_ALU + // RR_HFAST_ALU: the activation clamped to +-448 and converted, no kept pre-activations, no vote, no rare path + uint2 au[2]; + [unroll] for (uint k0 = 0; k0 < 2; ++k0) { + const uint nt = 2u * j + k0; + v8f a = bias_tile(p_bias, 128u + 64u * nt); + a = wg_wmma_fp8(uint2(w1[2u * k0], w1[2u * k0]), y1q[0], a); + a = wg_wmma_fp8(uint2(w1[2u * k0 + 1u], w1[2u * k0 + 1u]), y1q[1], a); + const uint4 h = wg_to_f16_ags(a); + au[k0] = rr_q8h_small(uint4(rr_c448(rr_act(h.x, cln2, cl2, c12, c22, c32)), rr_c448(rr_act(h.y, cln2, cl2, c12, c22, c32)), + rr_c448(rr_act(h.z, cln2, cl2, c12, c22, c32)), rr_c448(rr_act(h.w, cln2, cl2, c12, c22, c32)))); + } +#else + v8f du[2]; + uint4 ah[2]; + uint4 hh2[2]; + uint2 au[2]; + uint abad = 0u; + [unroll] for (uint k0 = 0; k0 < 2; ++k0) { + const uint nt = 2u * j + k0; + v8f a = bias_tile(p_bias, 128u + 64u * nt); + a = wg_wmma_fp8(uint2(w1[2u * k0], w1[2u * k0]), y1q[0], a); + a = wg_wmma_fp8(uint2(w1[2u * k0 + 1u], w1[2u * k0 + 1u]), y1q[1], a); + const uint4 h = wg_to_f16_ags(a); + hh2[k0] = h; + ah[k0] = uint4(rr_act(h.x, cln2, cl2, c12, c22, c32), rr_act(h.y, cln2, cl2, c12, c22, c32), + rr_act(h.z, cln2, cl2, c12, c22, c32), rr_act(h.w, cln2, cl2, c12, c22, c32)); + au[k0] = rr_q8h(ah[k0], abad); + } + if (WaveActiveAnyTrue((abad & 0x80808080u) != 0u)) { + // the same activation, recomputed from the kept f16 pre-activations (rare path) + [unroll] for (uint k1 = 0; k1 < 2; ++k1) { + const uint4 h1 = rr_fence4(hh2[k1]); + ah[k1] = uint4(rr_act(h1.x, cln2, cl2, c12, c22, c32), rr_act(h1.y, cln2, cl2, c12, c22, c32), + rr_act(h1.z, cln2, cl2, c12, c22, c32), rr_act(h1.w, cln2, cl2, c12, c22, c32)); + } + du[0] = rr_widen(rr_fence4(ah[0])); + du[1] = rr_widen(rr_fence4(ah[1])); + RR_Q8(2u, du, au); + } +#endif + [unroll] for (uint c1 = 0; c1 < 2; ++c1) { + v8f a; + if (j == 0u) a = bias_tile(p_bias, 640u + 64u * c1); + else a = ddf[c1]; + a = wg_wmma_fp8(uint2(w2[2u * c1], w2[2u * c1]), au[0], a); + a = wg_wmma_fp8(uint2(w2[2u * c1 + 1u], w2[2u * c1 + 1u]), au[1], a); + ddf[c1] = a; + } + if (j < 3u) { [unroll] for (uint i4 = 0; i4 < 4; ++i4) { w1[i4] = n1[i4]; w2[i4] = n2[i4]; } } + } + // ---------------------------------------------------------------- step 9: y2h = R(Dd m + y1) + { + const float m = f16tof32(k_m & 0xffffu); + [unroll] for (uint c3 = 0; c3 < 2; ++c3) { + v8f t; + [unroll] for (uint i6 = 0; i6 < 8; ++i6) t.v[i6] = mad(ddf[c3].v[i6], m, y1f[c3].v[i6]); + y2h[c3] = wg_to_f16_ags(t); // RNE, one rounding + } + } + + // ==================================================================================== the head (spec 55) + // From here the lanes l = lp of each 32-lane half run rr_d0r.hlsl's text for the half's tile (hh): lane (r, hf) + // holds y2h channels 8hf .. 8hf+7 (Y0) and 16+8hf .. of token r. A wave64 lane holds channels 8 hf + 4 hh .. + 3 of + // both tiles: the half's other 4 channels of the lane's own tile come from lane ^ 32 (its tile-hh words), and the + // word Y1.x (channels 16 + 8 hf, 17 + 8 hf: the hh = 0 lane's x word of y2h[1], tile 0, or z word, tile 1; the + // only word of y2h[1] the tail reads, as WT) likewise. + const uint hsx = tl1 ? y2h[0].x : y2h[0].z, hsy = tl1 ? y2h[0].y : y2h[0].w; // the other tile's words of Y0 + const uint hrx = wg_xor_u(hsx, 32u), hry = wg_xor_u(hsy, 32u); + const uint wsx = tl1 ? y2h[1].x : y2h[1].z; // hh 0 sends its tile-1 word + const uint wrx = wg_xor_u(wsx, 32u); + const int Y = tl1 ? Yt[1] : Yt[0]; + const uint tok = tl1 ? tokt[1] : tokt[0]; + const uint4 Y0 = tl1 ? uint4(hrx, hry, y2h[0].z, y2h[0].w) : uint4(y2h[0].x, y2h[0].y, hrx, hry); + const uint4 Y1 = uint4(tl1 ? wrx : y2h[1].x, 0u, 0u, 0u); + // natural numbering: hf0 holds y0 .. y7, y16 .. y23; hf1 y8 .. y15, y24 .. y31. Two lane swaps: + // hf0 WA = (y0, y1), YS = (y2, y3), W1 = (y8, y9) and W2 = (y10, -) from hf1, WT = y16; + // hf1 WA = (y4, y5) and YS = (y6, y7) from hf0, W1 = y12, W2 = y14. + const bool h0 = hf == 0u; + const uint X1 = wg_xor16_u(h0 ? Y0.z : Y0.x), X2 = wg_xor16_u(h0 ? Y0.w : Y0.y); + const uint WA = h0 ? Y0.x : X1, YS = h0 ? Y0.y : X2, W1 = h0 ? X1 : Y0.z, W2 = h0 ? X2 : Y0.w, WT = Y1.x; +#if RR_HFAST_ALU + // the table lookups as bounded f32 work (head comment): hf0 sig(g0, g1, g2, g3), sig(py), tanh(hb), rho; hf1 sig(G0, G1, G2, px), tanh(ha) + const uint S0 = rr_sig16(WA & 0xffffu), S1 = rr_sig16(WA >> 16); + const uint S2 = rr_sig16(YS & 0xffffu), S3 = rr_sig16(YS >> 16); + const uint S4 = rr_f2h(rr_sigtanh(f16tof32(h0 ? W1 : W2), !h0)); // hf0 sig, hf1 tanh: one exp2 and one reciprocal + const uint T2 = rr_tanh16(WT & 0xffffu); + const float RHO = rr_tanhf(f16tof32(W1 >> 16) * asfloat(p_dz)); // rho = tanh(f32(y17 s2)), s2 = the f32 bits of p_dz +#else + const uint ptab = rr_u(p_tab); + // table lookups: hf0 sig(g0, g1, g2, g3), sig(py), tanh(hb), rho(rho); hf1 sig(G0, G1, G2, px), tanh(ha) + const uint S0 = wg_ld16(ptab, (WA & 0xffffu) * 2u), S1 = wg_ld16(ptab, (WA >> 16) * 2u); + const uint S2 = wg_ld16(ptab, (YS & 0xffffu) * 2u), S3 = wg_ld16(ptab, (YS >> 16) * 2u); + const uint S4 = wg_ld16(ptab, h0 ? (W1 & 0xffffu) * 2u : TAB_TANH + (W2 & 0xffffu) * 2u); + const uint T2 = wg_ld16(ptab, TAB_TANH + (WT & 0xffffu) * 2u); + const float RHO = asfloat(wg_ld32(p_rho, (W1 >> 16) * 4u)); +#endif + // the two lanes of a token swap: hf0 -> (sig g2, sig g3), la; hf1 -> (sig px, lb) + const uint Q0 = wg_xor16_u(h0 ? (S2 | (S3 << 16)) : (S3 | ((W1 & 0xffffu) << 16))); + const uint Q1 = wg_xor16_u(W2 & 0xffffu); + // 18-channel reading: hf0 holds (y10, y11) in W2 and (y16, y17) in WT, hf1 (y12, y13) in W1 and (y14, y15) in W2. + // HID: hf0 values 2, 3 = tanh(y16), tanh(y17); hf1 values 0, 1 = tanh(y14), tanh(y15) (T3 = the high halves). + // CANDW (55-ERRATUM-2): W4.r..a from (y10, y11, y12, y13), every step binary16 (each lane gets the other lane's + // pair and computes all four): m = max; d = R(z - m); t = R(d R(log2 e)); e = R(2^t) (table, correctly rounded); + // S = R(R(e0 + e2) + R(e1 + e3)); q = R(1/S) (table, correctly rounded); W = R(e q). hf0 stores W0, W1, hf1 W2, W3. +#if RR_HFAST_ALU + // CANDW as f32 (head comment): W_i = exp2((z_i - zmax) log2 e) / sum, hf0 stores W0, W1, hf1 W2, W3 (packed, round toward zero: + // only the 8-bit codes of the values are used) + const uint T3 = rr_tanh16((h0 ? WT : W2) >> 16); + const uint P18 = wg_xor16_u(h0 ? W2 : W1); + const uint ZW01 = h0 ? W2 : P18, ZW23 = h0 ? P18 : W1; + const float zq0 = f16tof32(ZW01 & 0xffffu), zq1 = f16tof32(ZW01 >> 16), zq2 = f16tof32(ZW23 & 0xffffu), zq3 = f16tof32(ZW23 >> 16); + const float zmx = max(max(zq0, zq1), max(zq2, zq3)); + const float L2Ef = 1.4426950408889634f; + const float ee0 = exp2((zq0 - zmx) * L2Ef), ee1 = exp2((zq1 - zmx) * L2Ef), ee2 = exp2((zq2 - zmx) * L2Ef), ee3 = exp2((zq3 - zmx) * L2Ef); + const float qS = rcp((ee0 + ee1) + (ee2 + ee3)); + const uint WL = wg_h2((h0 ? ee0 : ee2) * qS, (h0 ? ee1 : ee3) * qS); + const uint cw18a = WL & 0xffffu, cw18b = WL >> 16; +#else + const uint T3 = wg_ld16(ptab, TAB_TANH + ((h0 ? WT : W2) >> 16) * 2u); + const uint P18 = wg_xor16_u(h0 ? W2 : W1); + const half2 Z01 = H2(h0 ? W2 : P18), Z23 = H2(h0 ? P18 : W1); + const half2 Zm = max(Z01, Z23); + const half Zmax = max(Zm.x, Zm.y); + const half2 Zmm = half2(Zmax, Zmax); + const half2 L2E = H2(0x3dc53dc5u); // R(log2 e) + // RR_HTRIM: subtract -> multiply (no fma form), and the product only feeds the table address (integer work) + const uint T01 = U2(((Z01 - Zmm) * L2E)), T23 = U2(((Z23 - Zmm) * L2E)); + const uint E01 = wg_ld16(ptab, TAB_EXP2 + (T01 & 0xffffu) * 2u) | (wg_ld16(ptab, TAB_EXP2 + (T01 >> 16) * 2u) << 16); + const uint E23 = wg_ld16(ptab, TAB_EXP2 + (T23 & 0xffffu) * 2u) | (wg_ld16(ptab, TAB_EXP2 + (T23 >> 16) * 2u) << 16); + // RR_HTRIM: an add feeding an add (no fma form), and SS only feeds the table address + const uint SP = U2((H2(E01) + H2(E23))); // (R(e0 + e2), R(e1 + e3)) + const uint SS = U2((H2(SP) + H2((SP >> 16) | (SP << 16)))); // R(R(e0 + e2) + R(e1 + e3)) in both halves + const half2 Q18 = H2(wg_ld16(ptab, TAB_RCP16 + (SS & 0xffffu) * 2u) * 0x10001u); + const uint WL = U2(HF(H2(h0 ? E01 : E23) * Q18)); + const uint cw18a = WL & 0xffffu, cw18b = WL >> 16; +#endif + + // ---------------------------------------------------------------- 55.2 Cnet (hf0: channels 0, 1; hf1: 2, 3) + const half2 g2 = rr_c2(k_g); + const uint zs = h0 ? S3 : (Q0 >> 16); // sig(g3) = z + const half2 z2 = H2(zs * 0x10001u); + const half2 omz = (H2(0x3c003c00u) - z2); // R(1 - z) (RR_HTRIM: a multiplicand of the fma below) + const half2 gate = HF(H2(h0 ? (S0 | (S1 << 16)) : Q0) * g2); // R(sig(Dh_j) g): hf0 j = 0, 1; hf1 j = 2 (low) + const int Mx = (int)(Wt >> 1), My = (int)(Ht >> 1); + const int xm = (X - 1) >> 1, ym = (Y - 1) >> 1; // floor((X - 1/2) / 2) + const uint rxm = (uint)rr_refl(xm, Mx), rxp = (uint)rr_refl(xm + 1, Mx); + const uint rym = (uint)rr_refl(ym, My), ryp = (uint)rr_refl(ym + 1, My); + const uint phc = rr_u(p_hc), cb = 4u * hf; + const uint cA = wg_ld32(phc, (rym * (uint)Mx + rxp) * 8u + cb), cB = wg_ld32(phc, (rym * (uint)Mx + rxm) * 8u + cb); + const uint cC = wg_ld32(phc, (ryp * (uint)Mx + rxm) * 8u + cb), cD = wg_ld32(phc, (ryp * (uint)Mx + rxp) * 8u + cb); + const uint hold = wg_ld32(p_hold, tok * 8u + cb); + const half2 wxp = H2((X & 1) != 0 ? 0x34003400u : 0x3a003a00u), wxm = H2((X & 1) != 0 ? 0x3a003a00u : 0x34003400u); + const half2 wyp = H2((Y & 1) != 0 ? 0x34003400u : 0x3a003a00u), wym = H2((Y & 1) != 0 ? 0x3a003a00u : 0x34003400u); + half2 bt = HF(HF(H2(cA) * wxp) * wym); + // RR_HTRIM: an fma result feeds the next fma's addend or a multiply (no merge); hh only feeds integer work (the + // select below) before it is the fma's addend + bt = (mad(HF(H2(cB) * wxm), wym, bt)); + bt = (mad(HF(H2(cC) * wxm), wyp, bt)); + const half2 uu = (mad(HF(H2(cD) * wxp), wyp, bt)); + uint hhn = U2((HF(uu * gate) * z2)); + hhn = h0 ? hhn : ((hhn & 0xffffu) | 0x80000000u); // hnew_3 = R(hold_3 omz): addend -0 + const uint hn = U2(mad(H2(hold), omz, H2(hhn))); + + // ---------------------------------------------------------------- 55.3 PREC, CANDW values (f32, then R) + const float s1 = asfloat(k_s1); + float cv = 0.0f; + [branch] if (!hmode) { + const uint la = h0 ? (W2 & 0xffffu) : Q1, lb = h0 ? (Q0 >> 16) : (W1 & 0xffffu); + const float d = f16tof32(la) - f16tof32(lb); // f32(la - lb), one rounding + cv = rr_lhalf(h0 ? d : -d); // hf0: w = sigx(d)/2, hf1: w' = sigx(-d)/2 + } +#if RR_HFAST_ALU + // PREC (f32 products of f32 values: no addend, nothing a fused form could change; the fences of the exact-arithmetic text are dropped) + const float po = f16tof32(h0 ? S4 : S3) * s1; // hf0: Py, hf1: Px + const float sq = po * po; // hf0: PREC 1, hf1: PREC 0 + const float px = f16tof32(Q0 & 0xffffu) * s1; // hf0: Px + const float p2 = (px * po) * RHO; // hf0: PREC 2 = (Px Py) rho +#else + const float po = FF(f16tof32(h0 ? S4 : S3) * s1); // hf0: Py, hf1: Px + const float sq = po * po; // hf0: PREC 1, hf1: PREC 0 + const float px = FF(f16tof32(Q0 & 0xffffu) * s1); // hf0: Px + const float p2 = FF(px * po) * RHO; // hf0: PREC 2 = (Px Py) rho +#endif +#if RR_HFAST_ALU >= 2 + const uint cv16 = rr_f2h(cv), sq16 = rr_f2h(sq), p216 = rr_f2h(p2); +#else + v8f cf = zero8(); + cf.v[0] = cv; cf.v[1] = sq; cf.v[2] = p2; + const uint4 c16 = wg_to_f16_ags(cf); + const uint cv16 = c16.x & 0xffffu, sq16 = c16.x >> 16, p216 = c16.y & 0xffffu; +#endif + + // ---------------------------------------------------------------- stores + const bool inT = X >= 0 && Y >= 0 && X < (int)Wt && Y < (int)Ht; + const bool inI = X >= 0 && Y >= 0 && X < (int)Wr && Y < (int)Hr; + const uint tt = (uint)Y * Wt + (uint)X, ti = (uint)Y * Wr + (uint)X; + const uint hv = h0 ? T2 : S4; // hf0: R(tanh hb) (values 2, 3), hf1: R(tanh ha) + const uint hv2 = hmode ? T3 : hv; // second value of the lane's HID pair + const uint cwa = hmode ? cw18a : cv16, cwb = hmode ? cw18b : cv16; // the lane's two CANDW values + // the 8-bit codes once, shared by the stores and the render record (same function of the same values) + const uint u8a = rr_u8(cwa), u8b = rr_u8(cwb), u80 = rr_u8(S0), u81 = rr_u8(S1), u82 = rr_u8(S2); + // each record whole from the hf0 lane: the hf1 lane's record parts reach it (xor 16); b64 / b32 / b32 / b64 / b32 stores + { + const uint cw16 = (u8a | (u8b << 8)) & 0xffffu, hd16 = (rr_s8(hv) | (rr_s8(hv2) << 8)) & 0xffffu; + const uint gt = u80 | (u81 << 8) | (u82 << 16); +#if RR_HFAST_ALU + const uint hnx = wg_xor16_u(hn), hdx = wg_xor16_u(hd16), sqx = wg_xor16_u(sq16), gtx = wg_xor16_u(gt); + uint cwx = 0u; // CANDW: read by output.fallback.f only (with a render-record scratch the fast output stage reads the record's W halves) + [branch] if (p_oscr == 0u) cwx = wg_xor16_u(cw16); +#else + const uint hnx = wg_xor16_u(hn), cwx = wg_xor16_u(cw16), hdx = wg_xor16_u(hd16), sqx = wg_xor16_u(sq16), gtx = wg_xor16_u(gt); +#endif + if (h0) { + if (inT) wg_st64(rr_u(p_cnet), tt * 8u, uint2(hn, hnx)); + if (inI) { +#if RR_HFAST_ALU + if (p_oscr == 0u) wg_st32(rr_u(p_candw), ti * 4u, cw16 | (cwx << 16)); +#else + wg_st32(rr_u(p_candw), ti * 4u, cw16 | (cwx << 16)); +#endif + wg_st32(rr_u(p_hid), ti * 4u, hdx | (hd16 << 16)); + wg_st64(rr_u(p_prec), ti * 8u, uint2((sqx & 0xffffu) | (sq16 << 16), p216)); + wg_st32(rr_u(p_gates), ti * 4u, gtx); + } + } + } + // the render record of token ti (rr_output_prep's a / b vectors): hf0 holds Cnet 0, 1, PREC 1 (Py), PREC 2, CANDW + // bytes 0, 1; hf1 Cnet 2, PREC 0 (Px), GATES, CANDW bytes 2, 3 (the stores above) + [branch] if (p_oscr != 0u) { +#if !RR_HFAST_ALU + bool orare = false; +#endif +#if RR_HFAST_ALU >= 2 + const uint4 o16 = uint4( + rr_f2h(rr_ocf(f16tof32(hn & 0xffffu))) | (rr_f2h(rr_ocf(f16tof32(hn >> 16))) << 16), + rr_f2h(f16tof32(sq16) * asfloat(k_o2n)) | (rr_f2h(h0 ? (f16tof32(p216) * asfloat(k_o2x2n)) : rr_od255(u8b)) << 16), + rr_f2h(rr_od255(u8a)) | (rr_f2h(rr_od255(h0 ? u8b : u80)) << 16), + rr_f2h(rr_od255(u81)) | (rr_f2h(rr_od255(u82)) << 16)); +#else + v8f e = zero8(); + e.v[0] = rr_ocf(f16tof32(hn & 0xffffu)); + e.v[1] = rr_ocf(f16tof32(hn >> 16)); +#if RR_HFAST_ALU + // no subnormal-band check, no flag word (output.fallback.f never runs under OFX_NOCHK) + e.v[2] = rr_ofence(f16tof32(sq16) * asfloat(k_o2n)); + e.v[3] = h0 ? rr_ofence(f16tof32(p216) * asfloat(k_o2x2n)) : rr_od255(u8b); // hf1: W half 3 +#else + e.v[2] = rr_omq(f16tof32(sq16), asfloat(k_o2n), orare); + e.v[3] = h0 ? rr_omq(f16tof32(p216), asfloat(k_o2x2n), orare) : rr_od255(u8b); // hf1: W half 3 +#endif + e.v[4] = rr_od255(u8a); + e.v[5] = rr_od255(h0 ? u8b : u80); // hf0: W half 1 + e.v[6] = rr_od255(u81); e.v[7] = rr_od255(u82); + const uint4 o16 = wg_to_f16_ags(e); +#endif + if (inI) { + const uint opw = (o_wh + 1u) / 2u, olb = 256u + opw * ((o_hh + 1u) / 2u) * 4u; + const uint ra = p_oscr + ((olb + 255u) & ~255u), rb = ra + Wr * Hr * 16u, rw = rb + ((Wr * Hr * 4u + 15u) & ~15u); + const uint w2 = (o16.z & 0xffffu) | ((h0 ? (o16.z >> 16) : (o16.y >> 16)) << 16); // W halves (a, b) + { + const uint a1 = (o16.x & 0xffffu) | (o16.z & 0xffff0000u), a2 = o16.w, a3 = o16.y & 0xffffu; // hf1: A 2-3, 4-5, 6 + const uint a1x = wg_xor16_u(a1), a2x = wg_xor16_u(a2), a3x = wg_xor16_u(a3), w2x = wg_xor16_u(w2); + if (h0) { + wg_st128(rr_u(ra), ti * 16u, uint4(o16.x, a1x, a2x, a3x | (o16.y << 16))); // A halves 0-7 + wg_st32(rr_u(rb), ti * 4u, o16.y >> 16); // B + wg_st64(rr_u(rw), ti * 8u, uint2(w2, w2x)); // W halves 0-3 + } + } + } +#if !RR_HFAST_ALU + if (WaveActiveAnyTrue(orare) && WaveIsFirstLane()) { uint o; wg_mem_gc.InterlockedOr(rr_u(p_oscr) + 4u, 1u, o); } +#endif + } +} diff --git a/clean/rr_f/kernels/rr_block_c32.hlsl b/clean/rr_f/kernels/rr_block_c32.hlsl index 5749d08f..5ff4c1ac 100644 --- a/clean/rr_f/kernels/rr_block_c32.hlsl +++ b/clean/rr_f/kernels/rr_block_c32.hlsl @@ -153,13 +153,28 @@ v8f rr_d8(uint2 q) // e4m3 bytes -> f32 (exact), one conversion per tile #if RR_QV1 && RR_WSRC == 0 #error "RR_QV1 needs RR_WSRC 1" #endif +// clean/rr-f-wave4 R4.2b RR_OVL: the exact body's overlay of the fusion up-projections in head 1's exchange buffer +// (round-11 item 3, below) for the relaxed body (RR_B32) too, and in the wave64 form. The lifetime argument is the one +// given below: UP is written and read before the head loop, head 1 first writes its buffer after head 0's barrier, which +// every wave reaches only after its last UP read; both bodies run the same loop (RR_QV1 / RR_DEFER are 0 with L01_ACC 0). +// RR_OVL 0 = the relaxed body's kernels as before (A/B; the runtime's WG_RR_W4 bit 1). Not with the dataflow form. +#ifndef RR_OVL +#if (RR_Z == 2 || (defined(RR_B32) && RR_B32 && !L01_ACC && !RR_DFLOW)) +#define RR_OVL 1 +#else +#define RR_OVL 0 +#endif +#endif #if WG_WAVE == 64 // the same LDS bytes as the wave32 form (rr_xq[1024] / rr_up[256] of uint64), addressed per dword: wave32 slot i // (8 bytes) = dwords 2 i, 2 i + 1; a wave64 lane holds dword 2 i + hh of each slot of its tiles groupshared uint rr_xq32[2048]; #define XB(h) ((h) * 512u) -#if RR_KIND == 2 +#if RR_KIND == 2 && RR_OVL && !RR_QV1 +#define RR_UP32(i) rr_xq32[1024u + (i)] // head 1's exchange buffer (dwords 1024 .. 2047) +#elif RR_KIND == 2 groupshared uint rr_up32[16 * HID / 4]; +#define RR_UP32(i) rr_up32[i] #endif #elif RR_WSRC == 0 // two unit buffers (QV_h / MLP chunk j even in buffer 0, Wo_h / MLP chunk j odd in buffer 1) and one q / v exchange @@ -173,7 +188,7 @@ groupshared uint64_t rr_xq[512]; // the window's quantised up-projections (fusion halves) in their own buffer. groupshared uint64_t rr_xq[1024]; #define XB(h) ((h) * 512u) -#if RR_KIND == 2 && RR_Z == 2 && !RR_QV1 +#if RR_KIND == 2 && RR_OVL && !RR_QV1 // clean/rr-f-s13x-x-l01 (exact body only; round-11 item 3): the up-projections (16 x HID / 8 = 256 slots) live in head // 1's exchange buffer. They are written and read before the head loop, and head 1 first writes its buffer after head // 0's barrier, which every wave reaches only after its last UP read. Same operations and bits; LDS 10240 -> 8192 B. @@ -894,19 +909,37 @@ WG_KERNEL(rr_block) // so the P codes, and everything after them, have the wave32 bits for every input; // * the Q8 votes (WaveActiveAnyTrue) cover both tiles: both forms of a Q8 group give rr_q8's bits for every input, // so which one runs is a speed choice only (as in wave32). -#if !RR_B32 || RR_WSRC != 1 || !RR_WIL || RR_QV1 || RR_DEFER || RR_DFLOW || RR_PFB || RR_MLA || RR_FOLD_B32 || !RR_STICKY +// clean/rr-f-wave4c W4C.2 (same output bits; opt-in defines of the wave64 kernel, the .w4c kernels of the runtime's WG_RR_W4C +// bits 3 / 4): RR_DEFER 1 = the deferred Q8 decision of the wave32 kernel above (the window runs once with the fast Q8 forms +// only, the two waves' bad-code flags meet in LDS, a flagged window reruns in the per-group form); RR_W4C bit 1 = the MLP Dd +// weight fragments loaded right after the chunk's hidden WMMAs and the Wo fragments before the PV Q8 (L01_MDPF style); +// bit 2 = IN0's folded front-end MLP with its chains WMMA-interleaved (L01_WILF form) and act2_4, the W2 fragments loaded +// before the Q8 of the first layer. Every chain keeps its WMMAs, roundings and operands, so the bits are unchanged. +#ifndef RR_W4C +#define RR_W4C 0 +#endif +#if !RR_B32 || RR_WSRC != 1 || !RR_WIL || RR_QV1 || RR_DFLOW || RR_PFB || RR_MLA || RR_FOLD_B32 || !RR_STICKY #error "rr_block_c32.hlsl wave64: the relaxed stagee configuration only" #endif #if RR_KIND == 0 && RR_INFOLD != 1 #error "rr_block_c32.hlsl wave64: IN0 with the folded variant-E front-end MLP only" #endif -#if RR_KIND == 0 && (L01_WILF || RR_SMAJOR >= 2) -#error "rr_block_c32.hlsl wave64: L01_WILF / RR_SMAJOR 2 forms not ported" +#if RR_KIND == 0 && ((L01_WILF && !(RR_W4C & 4)) || (RR_SMAJOR >= 2 && !(RR_W4C & 4))) +#error "rr_block_c32.hlsl wave64: L01_WILF / RR_SMAJOR 2 forms only through RR_W4C bit 2" #endif +#if RR_DEFER +void rr_window(const bool rr_fp) +{ +#else WG_KERNEL(rr_block) { WG_SETUP(); +#endif +#if RR_DEFER + rr_lane = WaveGetLaneIndex() RR_OPQ; +#else rr_lane = WaveGetLaneIndex(); +#endif rr_lp = rr_lane & 31u; rr_hh = rr_lane >> 5; rr_hf = (rr_lane >> 4) & 1u; @@ -916,13 +949,21 @@ WG_KERNEL(rr_block) rr_sat = false; rr_nan = false; DBG_INIT +#if RR_DEFER + const uint wv0 = 2u * WaveReadLaneFirst((wg_lane_id() RR_OPQ) >> 6); // wave32 wave of tile 0 (tile 1: wv0 + 1) +#else const uint wv0 = 2u * WaveReadLaneFirst(wg_lane_id() >> 6); // wave32 wave of tile 0 (tile 1: wv0 + 1) +#endif const uint n = rr_lane & 15u; #if RR_KIND == 2 rr_uo = rr_fence_u(32u * n + 2u * rr_hf + rr_hh); // dword of UP slot ((n HID + 16 ot + 8 hf) >> 3) - 4 ot rr_f2 = rr_fence_u(2u * rr_hf + rr_hh); #endif +#if RR_DEFER + const uint wid = RR_WBID() RR_OPQ; +#else const uint wid = RR_WBID(); +#endif const int wi = (int)(wid % (uint)nwx), wj = (int)(wid / (uint)nwx); const int lx = (int)(n & 7u); const int X = 8 * wi - sx + lx; @@ -971,8 +1012,8 @@ WG_KERNEL(rr_block) uint2 uq; Q8_TRY uq = q8f(acc, bad); Q8_ELSE uq = q8s(acc); Q8_END - rr_up32[rr_uo + 4u * ot0] = uq.x; - rr_up32[rr_uo + 4u * ot1] = uq.y; + RR_UP32(rr_uo + 4u * ot0) = uq.x; + RR_UP32(rr_uo + 4u * ot1) = uq.y; } GroupMemoryBarrierWithGroupSync(); const uint par0 = wv0 * 4u + (uint)(lx >> 1), par1 = par0 + 4u; @@ -982,7 +1023,7 @@ WG_KERNEL(rr_block) uint bad = 0u; [unroll] for (uint ct = 0; ct < KS; ++ct) { const uint2 sk = rr_tld(pX, tok[0] * RC + 16u * ct, tok[1] * RC + 16u * ct); - const uint2 up = uint2(rr_up32[ub0 + 4u * ct], rr_up32[ub1 + 4u * ct]); + const uint2 up = uint2(RR_UP32(ub0 + 4u * ct), RR_UP32(ub1 + 4u * ct)); xr[ct] = bv_mad(bv_d8(sk), f2x, bv_d8(up)); } Q8_TRY [unroll] for (uint c0 = 0; c0 < KS; ++c0) xf[c0] = bv_q8f(xr[c0], bad); @@ -996,6 +1037,26 @@ WG_KERNEL(rr_block) [unroll] for (uint ks = 0; ks < 4u; ++ks) f[ks] = rr_tld(pX, tok[0] * 64u + 16u * ks, tok[1] * 64u + 16u * ks); uint4 ah[2][2]; +#if RR_W4C & 4 + // W4C.2: per br the two feature tiles' chains WMMA-interleaved (each chain: bias, wmma, wmma, f16 round, wmma, wmma, packed + // f16 = MLP_STEP2 + MLP_STEP2H), the activation stage-major (act2_4, the same operations per value) + [unroll] for (uint br = 0; br < 2u; ++br) { + v8f c0 = bias_tile(pBup + br * 128u, 0u), c1 = bias_tile(pBup + br * 128u, 1u); + const uint w0 = (br * 2u) * 4u, w1 = (br * 2u + 1u) * 4u; + wil2(c0, c1, wfrag(pCin, w0 + 0u), f[0], wfrag(pCin, w0 + 1u), f[1], wfrag(pCin, w1 + 0u), f[0], wfrag(pCin, w1 + 1u), f[1]); + c0 = rr_r16(c0); c1 = rr_r16(c1); + wil2(c0, c1, wfrag(pCin, w0 + 2u), f[2], wfrag(pCin, w0 + 3u), f[3], wfrag(pCin, w1 + 2u), f[2], wfrag(pCin, w1 + 3u), f[3]); + ah[br][0] = act2_4(rr_h16(c0), wc); + ah[br][1] = act2_4(rr_h16(c1), wc); + } + uint2 w2f[8]; // the second layer's fragments, loaded before the first layer's Q8 group + [unroll] for (uint o2 = 0; o2 < 2u; ++o2) { + w2f[4u * o2 + 0u] = wfrag(pWup, 2u * o2); + w2f[4u * o2 + 1u] = wfrag(pWup, 2u * o2 + 1u); + w2f[4u * o2 + 2u] = wfrag(pWup, 4u + 2u * o2); + w2f[4u * o2 + 3u] = wfrag(pWup, 4u + 2u * o2 + 1u); + } +#else [unroll] for (uint br = 0; br < 2u; ++br) { [unroll] for (uint ft = 0; ft < 2u; ++ft) { const uint w = (br * 2u + ft) * 4u; @@ -1005,6 +1066,7 @@ WG_KERNEL(rr_block) ah[br][ft] = uint4(act2(h.x, wc), act2(h.y, wc), act2(h.z, wc), act2(h.w, wc)); } } +#endif uint2 a[2][2]; { uint bad = 0u; @@ -1015,12 +1077,25 @@ WG_KERNEL(rr_block) Q8_END } v8f o[2]; +#if RR_W4C & 4 + { + v8f z[2]; + [unroll] for (uint o0 = 0; o0 < 2u; ++o0) z[o0] = bias_tile(pHc, o0); + [unroll] for (uint o1 = 0; o1 < 2u; ++o1) z[o1] = wg_wmma_fp8(w2f[4u * o1 + 0u], a[0][0], z[o1]); + [unroll] for (uint o3 = 0; o3 < 2u; ++o3) z[o3] = wg_wmma_fp8(w2f[4u * o3 + 1u], a[0][1], z[o3]); + [unroll] for (uint o4 = 0; o4 < 2u; ++o4) z[o4] = rr_r16(z[o4]); + [unroll] for (uint o5 = 0; o5 < 2u; ++o5) z[o5] = wg_wmma_fp8(w2f[4u * o5 + 2u], a[1][0], z[o5]); + [unroll] for (uint o6 = 0; o6 < 2u; ++o6) z[o6] = wg_wmma_fp8(w2f[4u * o6 + 3u], a[1][1], z[o6]); + [unroll] for (uint o7 = 0; o7 < 2u; ++o7) o[o7] = rr_r16(z[o7]); + } +#else [unroll] for (uint ot = 0; ot < 2u; ++ot) { v8f z = bias_tile(pHc, ot); z = MLP_STEP2(wfrag(pWup, (0u * 2u + ot) * 2u), a[0][0], wfrag(pWup, (0u * 2u + ot) * 2u + 1u), a[0][1], z); o[ot] = MLP_STEP2(wfrag(pWup, (1u * 2u + ot) * 2u), a[1][0], wfrag(pWup, (1u * 2u + ot) * 2u + 1u), a[1][1], z); } +#endif { uint bad = 0u; Q8_TRY xf[0] = q8f(o[0], bad); xf[1] = q8f(o[1], bad); @@ -1085,12 +1160,19 @@ WG_KERNEL(rr_block) wil2(ov[0], ov[1], vt[0], pf[0], vt[2], pf[1], vt[1], pf[0], vt[3], pf[1]); wil2(ov[0], ov[1], vt[4], pf[2], vt[6], pf[3], vt[5], pf[2], vt[7], pf[3]); } +#if RR_W4C & 2 + const uint2 wo0 = wo_frag(h, 0u), wo1 = wo_frag(h, 1u), wo2 = wo_frag(h, 2u), wo3 = wo_frag(h, 3u); +#endif bad = 0u; uint2 of[2]; Q8_TRY of[0] = q8f(ov[0], bad); of[1] = q8f(ov[1], bad); Q8_ELSE of[0] = q8s(ov[0]); of[1] = q8s(ov[1]); Q8_END // step 6: Dp step h +#if RR_W4C & 2 + wil2(dpv[0], dpv[1], wo0, of[0], wo1, of[1], wo2, of[0], wo3, of[1]); +#else wil2(dpv[0], dpv[1], wo_frag(h, 0u), of[0], wo_frag(h, 1u), of[1], wo_frag(h, 2u), of[0], wo_frag(h, 3u), of[1]); +#endif if (h + 1u == RH) { dp[0] = dpv[0]; dp[1] = dpv[1]; } } @@ -1117,14 +1199,21 @@ WG_KERNEL(rr_block) [unroll] for (uint j = 0; j < NJ; ++j) { rrbv h0 = bias_tile(pB1, 2u * j), h1 = bias_tile(pB1, 2u * j + 1u); wil2(h0, h1, mlp_frag(j, 0u), y1q[0], mlp_frag(j, 1u), y1q[1], mlp_frag(j, KS), y1q[0], mlp_frag(j, KS + 1u), y1q[1]); +#if RR_W4C & 2 + const uint2 wd0 = mlp_frag(j, 2u * KS), wd1 = mlp_frag(j, 2u * KS + 1u), wd2 = mlp_frag(j, 2u * KS + 2u), wd3 = mlp_frag(j, 2u * KS + 3u); +#endif const rrbv a0h = bv_act(h0, BVW); const rrbv a1h = bv_act(h1, BVW); uint bad = 0u; uint2 au0, au1; Q8_TRY au0 = bv_q8f(a0h, bad); au1 = bv_q8f(a1h, bad); Q8_ELSE au0 = bv_q8s(a0h); au1 = bv_q8s(a1h); Q8_END +#if RR_W4C & 2 + wil2(ddv[0], ddv[1], wd0, au0, wd1, au1, wd2, au0, wd3, au1); +#else wil2(ddv[0], ddv[1], mlp_frag(j, 2u * KS), au0, mlp_frag(j, 2u * KS + 1u), au1, mlp_frag(j, 2u * KS + 2u), au0, mlp_frag(j, 2u * KS + 3u), au1); +#endif if (j + 1u == NJ) { dd[0] = ddv[0]; dd[1] = ddv[1]; } } @@ -1187,4 +1276,23 @@ WG_KERNEL(rr_block) #endif DBG_FLUSH } +#if RR_DEFER +// the deferred window (as the wave32 kernel above, two wave64 waves): the flags of the waves meet in LDS in two dwords the +// rerun does not write before its first barrier (head 1's exchange buffer, dwords 1024 ..; head 0's for the fusion kind, +// whose up-projections come first and live in head 1's buffer) +WG_KERNEL(rr_block) +{ + WG_SETUP(); + rr_badw = 0u; + rr_negoff = false; + rr_window(true); + const uint fb = (RR_KIND == 2 ? 0u : 1024u); + const bool wbad = WaveActiveAnyTrue((rr_badw & 0x80808080u) != 0u); + GroupMemoryBarrierWithGroupSync(); // every wave's last exchange read is done + if (WaveIsFirstLane()) rr_xq32[fb + (wg_lane_id() >> 6)] = wbad ? 1u : 0u; + GroupMemoryBarrierWithGroupSync(); + const bool any = (rr_xq32[fb] | rr_xq32[fb + 1u]) != 0u; + [branch] if (any && L01_DEFERNEG != 1) rr_window(false); +} +#endif #endif // WG_WAVE diff --git a/clean/rr_f/kernels/rr_block_c64_w64.hlsl b/clean/rr_f/kernels/rr_block_c64_w64.hlsl new file mode 100644 index 00000000..2f2e98e0 --- /dev/null +++ b/clean/rr_f/kernels/rr_block_c64_w64.hlsl @@ -0,0 +1,594 @@ +// SPDX-License-Identifier: MIT +// clean/rr-f-wave4b R4b.2: the spec 51 window block of the level-1 stages E1, D1-F and D1-R (C = 64, H = 2, MLP width +// 256) in wave64, design D of rr_block_c32.hlsl (the wave64 form of the C = 32 stages): the relaxed body's stagee +// kernels only (RR_B32, RR_VARL 1, step E, weights from memory, WMMA-interleaved chains), WG_BLOCK 128, WG_WAVE 64, +// the window text of rr_block_fast.hlsl operation by operation. Same arguments and buffer layouts; same output bits. +// +// One workgroup = one 8 x 8 window and two wave64 waves; wave w owns the window tokens of the wave32 waves wv = 2 w +// (tile 0) and 2 w + 1 (tile 1) of rr_block_fast.hlsl (16 tokens each, token = 16 wv + n, n = lane & 15). Wave64 lane +// L = l + 32 hh holds, for each tile, half hh of wave32 lane l's fragment (l = 16 hf + n): fp8 A / B bytes 4 hh .. +// 4 hh + 3, accumulator elements 4 hh .. 4 hh + 3 (the v_wmma wave64 layout, see rr_block_c32.hlsl). A v8f holds +// elements 0-3 of tile 0 and 0-3 of tile 1; a uint2 fp8 fragment: x = tile 0, y = tile 1; a uint4 of packed f16: +// x, y = tile 0, z, w = tile 1. Every value is computed by the operation the wave32 kernel computes it with (wave64 +// v_wmma = wave32 v_wmma bit for bit, the conversions are per value), so the outputs keep their bits; memory and LDS +// byte layouts are unchanged. What moves is where a value sits: +// * per-token quantities (coordinates, token loads / stores, the score bias, the hidden-state tail) once per tile; +// * shared operands (weights, biases, the k / v fragments of the exchange, the coarse fragments of D1-F, the merge's +// coarse tokens) loaded once, 4 bytes per lane, and used by both tiles' v_wmma (wg_wmma_fp8); +// * the fusion up-projection (D1-F) and the merge (E1) give the two tiles different output tiles (ot0, ot1 = ot0 + 1) +// of the same input: per-tile weights and biases, one shared B fragment; +// * the softmax sum Z, as rr_block_c32.hlsl: the wave32 lane's tree sum zs = A + B of its 8 elements becomes the +// bracket of the lane's 4 elements plus the other half's (lane ^ 32), A + B or B + A: IEEE addition is +// commutative, so the sums are equal unless the result is a NaN (payload / sign may differ), and rz = rcp(..) only +// feeds w * rz -> rr_q8_small (v_cvt_pk_fp8_f32), which gives 0xff for every NaN whatever its sign and payload; +// * the Q8 votes (WaveActiveAnyTrue) cover both tiles: both forms of a Q8 group give rr_q8's bits for every input. +// +// Defines: RR_KIND 0 (E1: adapter RR_ADAPT 2, merge RR_CN, merged hidden RR_HMERGE; the encoder hidden rule), 2 (D1-F: +// fusion half, RR_KC = 96) or 3 (D1-R: recurrent hidden state), RR_Z 0, RR_STEP 0, RR_VARL 1, RR_B32 1. +#include "wg_platform.hlsli" +#if WG_WAVE != 64 || WG_BLOCK != 128 +#error "rr_block_c64_w64.hlsl: WG_BLOCK 128, WG_WAVE 64 only" +#endif +#include "wg_ags.hlsli" +wg_frag wg_frag_v8f(v8f c) { wg_frag f; [unroll] for (uint i = 0; i < 8; ++i) f.v[i] = asuint(c.v[i]); return f; } +v8f wg_v8f_frag(wg_frag f) { v8f c; [unroll] for (uint i = 0; i < 8; ++i) c.v[i] = asfloat(f.v[i]); return c; } +wg_frag wg_frag2(uint2 a) { wg_frag f; f.v[0] = a.x; f.v[1] = a.y; [unroll] for (uint i = 2; i < 8; ++i) f.v[i] = 0u; return f; } +wg_frag rr_w64f(uint a, uint b, uint c, uint d) // a wave64 fragment: the first 4 / 2 / 1 dwords are used +{ + wg_frag f; + f.v[0] = a; f.v[1] = b; f.v[2] = c; f.v[3] = d; + [unroll] for (uint i = 4; i < 8; ++i) f.v[i] = 0u; + return f; +} +v8f wg_wmma_fp8(uint2 a, uint2 b, v8f c) // tile 0: (a.x, b.x, c 0-3), tile 1: (a.y, b.y, c 4-7) +{ + const wg_frag r0 = wg_ags_mma(WG_AGS_FP8_F32, rr_w64f(a.x, 0u, 0u, 0u), rr_w64f(b.x, 0u, 0u, 0u), + rr_w64f(asuint(c.v[0]), asuint(c.v[1]), asuint(c.v[2]), asuint(c.v[3]))); + const wg_frag r1 = wg_ags_mma(WG_AGS_FP8_F32, rr_w64f(a.y, 0u, 0u, 0u), rr_w64f(b.y, 0u, 0u, 0u), + rr_w64f(asuint(c.v[4]), asuint(c.v[5]), asuint(c.v[6]), asuint(c.v[7]))); + v8f d; + [unroll] for (uint i = 0; i < 4; ++i) { d.v[i] = asfloat(r0.v[i]); d.v[4 + i] = asfloat(r1.v[i]); } + return d; +} +uint2 wg_pack_q8_nc_ags(v8f d) // v_cvt_pk_fp8_f32 per value pair, element i -> byte i of its tile's dword +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(asuint(d.v[0]), asuint(d.v[1]), asuint(d.v[2]), asuint(d.v[3])), + WG_AGS_T_F32_ACC, WG_AGS_T_FP8_B); + const wg_frag r1 = wg_ags_convert(rr_w64f(asuint(d.v[4]), asuint(d.v[5]), asuint(d.v[6]), asuint(d.v[7])), + WG_AGS_T_F32_ACC, WG_AGS_T_FP8_B); + return uint2(r0.v[0], r1.v[0]); +} +uint4 wg_to_f16_ags(v8f d) // v_cvt_f16_f32 per value (round to nearest even) +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(asuint(d.v[0]), asuint(d.v[1]), asuint(d.v[2]), asuint(d.v[3])), + WG_AGS_T_F32_ACC, WG_AGS_T_F16_ACC); + const wg_frag r1 = wg_ags_convert(rr_w64f(asuint(d.v[4]), asuint(d.v[5]), asuint(d.v[6]), asuint(d.v[7])), + WG_AGS_T_F32_ACC, WG_AGS_T_F16_ACC); + return uint4(r0.v[0], r0.v[1], r1.v[0], r1.v[1]); +} +#define rr_d8 rr_d8_w32 // rr_common.hlsli's wave32 text, renamed; the wave64 rr_d8 follows the includes + +WG_ARGS_BEGIN + uint pX, pCin, pHold, pHc; + uint pWqv, pTb, pWo, pBo, pMlp, pB1, pB2, pWm, pBm, pWa, pBa, pWup, pBup, pWh, pBh; + uint pSig, pZ, pY2, pMerged, pHnew, pHmerged; + int Nx, Ny, sx, sy, nwx; + uint c_ra_m, c_f_g; + uint wc0, wc1, wc2, wc3, wc4; +WG_ARGS_END + +#define RR_L01BIT (RR_ADAPT ? 4 : RR_KIND == 2 ? 8 : 16) // this stage's bit in the L01_* masks (as rr_block_fast.hlsl) +#include "rr_common.hlsli" +#include "rr_step.hlsli" +#undef rr_d8 +v8f rr_d8(uint2 q) // e4m3 bytes -> f32 (exact), one conversion per tile +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(q.x, 0u, 0u, 0u), WG_AGS_T_FP8_B, WG_AGS_T_F32_ACC); + const wg_frag r1 = wg_ags_convert(rr_w64f(q.y, 0u, 0u, 0u), WG_AGS_T_FP8_B, WG_AGS_T_F32_ACC); + v8f d; + [unroll] for (uint i = 0; i < 4; ++i) { d.v[i] = asfloat(r0.v[i]); d.v[4 + i] = asfloat(r1.v[i]); } + return d; +} + +#ifndef RR_KIND +#define RR_KIND 0 +#endif +#ifndef RR_CN +#define RR_CN 0 +#endif +#ifndef RR_HMERGE +#define RR_HMERGE 0 +#endif +#ifndef RR_ADAPT +#define RR_ADAPT 0 +#endif +#ifndef RR_KC +#define RR_KC 0 +#endif +#ifndef RR_Z +#define RR_Z 0 +#endif +#ifndef RR_WSRC +#define RR_WSRC 1 +#endif +#if RR_C != 64 || RR_H != 2 +#error "rr_block_c64_w64.hlsl: C = 64, H = 2 only" +#endif +#define RC RR_C +#define RH RR_H +#define KS (RC / 16) // 4 channel fragments of a token +#define NS (RC / 32) // 2 +#define HID (4 * RC) // 256: MLP width +#define NJ (HID / 32) // 8 MLP chunks +#define HIDDEN (RR_KIND == 0 || RR_KIND == 3) +#define UNIT (64 * RC) // bytes of a weight unit: QV_h (Wq_h | Wv_h) or an MLP chunk (W1 | W2); Wo_h is half + +// ---- LDS: the same 8192 bytes as rr_block_fast.hlsl's rr_xq[1024] (two head buffers: q [wave][ft][lane], v^T after), +// addressed per dword: wave32 slot i (8 bytes) = dwords 2 i, 2 i + 1; a wave64 lane holds dword 2 i + hh of each slot of +// its tiles. RR_OVL (as rr_block_fast.hlsl): the merge buffer MY (E1: 64 tokens x 64 channels = 512 slots) lives in +// head 0's exchange buffer (its last reader is head 0's PV; head 1 uses buffer 1, and every wave passes head 1's +// barrier only after those reads) and the fusion up-projections UP (D1-F: 16 x HID / 8 = 512 slots) in head 1's (written and +// read before the head loop; head 1 first writes its buffer after head 0's barrier, which every wave reaches only +// after its last UP read). +groupshared uint rr_xq32[2048]; +#define XB(h) (((h) & 1u) * 512u) +#define MY32(i) rr_xq32[(i)] +#define RR_UP32(i) rr_xq32[1024u + (i)] +#if RR_WSRC != 1 +#error "rr_block_c64_w64.hlsl: RR_WSRC 1 only" +#endif + +static uint rr_lane; // lane in the wave +static uint rr_hf; // (lane >> 4) & 1 +static uint rr_lp; // wave32 lane whose fragment halves this lane holds (lane & 31) +static uint rr_hh; // lane >> 5: the half (fp8 bytes 4 hh .., accumulator elements 4 hh ..) +// per-lane offsets, fenced (rr_fence_u: the same value with no known bits) so that the compiler adds them to the base +// once and keeps the constant parts in the instructions' offset fields +static uint rr_lo; // lp * 8 + 4 hh: byte of a fragment-ordered 8-byte slot +static uint rr_xo; // 2 lp + hh: dword of an LDS slot row +static uint rr_t4; // 8 hf + 4 hh: byte within a 16-channel group of a token / an output tile row +static uint rr_uo; // D1-F: dword offset in the up-projection buffer +static uint rr_f2; // 2 hf + hh +uint wfrag1(uint base, uint f) { return wg_ld32(base + rr_lo, f * 256u); } +uint2 wfrag(uint base, uint f) { const uint w = wfrag1(base, f); return uint2(w, w); } // shared by both tiles +v8f zero8() { return wg_splat(0.0f); } +v8f rr_dup4(float4 a) +{ + v8f r; + r.v[0] = a.x; r.v[1] = a.y; r.v[2] = a.z; r.v[3] = a.w; r.v[4] = a.x; r.v[5] = a.y; r.v[6] = a.z; r.v[7] = a.w; + return r; +} +v8f rr_cat4(float4 a, float4 b) +{ + v8f r; + r.v[0] = a.x; r.v[1] = a.y; r.v[2] = a.z; r.v[3] = a.w; r.v[4] = b.x; r.v[5] = b.y; r.v[6] = b.z; r.v[7] = b.w; + return r; +} +v8f bias_tile(uint p, uint tile) { return rr_dup4(wg_ld4f(p + 4u * rr_t4, 64u * tile)); } +v8f bias_tile2(uint p, uint t0, uint t1) // per tile: output tile t0 for tile 0, t1 for tile 1 +{ + return rr_cat4(wg_ld4f(p + 4u * rr_t4, 64u * t0), wg_ld4f(p + 4u * rr_t4, 64u * t1)); +} +// per tile: this lane's dword of the 8-byte group at byte o0 (tile 0) / o1 (tile 1) (o = token row + 16 k) +uint2 rr_tld(uint base, uint o0, uint o1) { return uint2(wg_ld32(base + rr_t4, o0), wg_ld32(base + rr_t4, o1)); } +uint rr_xqr(uint r) { return rr_xo + 2u * r; } // this lane's dword of wave32 LDS slot r + lp (r = the slot row base) +uint2 rr_xqs(uint r) { const uint w = rr_xq32[rr_xqr(r)]; return uint2(w, w); } // a shared operand from LDS + +uint2 qv_frag(uint h, uint f) { return wfrag(pWqv + h * UNIT, f); } +uint2 wo_frag(uint h, uint f) { return wfrag(pWo + h * (UNIT / 2), f); } +uint2 mlp_frag(uint j, uint f) { return wfrag(pMlp + j * UNIT, f); } + +#include "rr_fast.hlsli" +#ifndef L01_WREV +#define L01_WREV 20 // a14 default (relaxed body): E1 and D1-R windows in reverse order +#endif +#if ((RR_ADAPT ? 4 : RR_KIND == 2 ? 8 : 16) & L01_WREV) != 0 +#define RR_WBID() (wg_grid_size() - 1u - wg_block_id()) // the same windows, the last one first (each window's work unchanged) +#else +#define RR_WBID() wg_block_id() +#endif +#define RR_FPP +#define RR_FPA +#if !RR_B32 || !RR_VARL || !RR_WIL || RR_Z != 0 || RR_STEP != 0 || !RR_STICKY +#error "rr_block_c64_w64.hlsl: the relaxed stagee configuration only" +#endif +#if RR_KIND == 0 && (RR_ADAPT != 2 || RR_CN != 96 || RR_HMERGE != 1) +#error "rr_block_c64_w64.hlsl: E1 with the direct adapter, merge 96 and the merged hidden only" +#endif +#if RR_KIND == 2 && RR_KC != 96 +#error "rr_block_c64_w64.hlsl: D1-F with RR_KC 96 only" +#endif +#if RR_KIND == 1 +#error "rr_block_c64_w64.hlsl: kinds 0, 2, 3" +#endif + +// One MLP chunk j (step 8) as rr_block_fast.hlsl's mlp_chunk (RR_WIL, RR_MDPF forms): Du_j (NS steps, start b1), Au_j = +// Q8(act(Du_j)), then one Dd step per channel tile. first: the Dd start values are the f32 bias tiles (the chunk-0 +// form); otherwise the widened packed dd. L01_MDPF: the Dd weight fragments are loaded right after the chunk's hidden +// WMMAs, before the activation and the Q8 branch. +void mlp_chunk(uint j, bool first, uint2 y1q[KS], inout rrbv dd[KS], RrWx wc) +{ + v8f u0 = bias_tile(pB1, 2u * j), u1 = bias_tile(pB1, 2u * j + 1u); + rrbv h0, h1; + [unroll] for (uint s = 0; s < NS; ++s) { + const uint2 a00 = mlp_frag(j, 2u * s), a01 = mlp_frag(j, 2u * s + 1u); + const uint2 a10 = mlp_frag(j, KS + 2u * s), a11 = mlp_frag(j, KS + 2u * s + 1u); + wil2(u0, u1, a00, y1q[2 * s], a01, y1q[2 * s + 1], a10, y1q[2 * s], a11, y1q[2 * s + 1]); + if (s + 1 == NS) { h0 = u0; h1 = u1; } + } + uint2 wd[2 * KS]; + [unroll] for (uint q = 0; q < 2 * KS; ++q) wd[q] = mlp_frag(j, 2u * KS + q); + const rrbv a0h = bv_act(h0, wc); + const rrbv a1h = bv_act(h1, wc); + uint bad = 0u; + uint2 au0, au1; + Q8_TRY au0 = bv_q8f(a0h, bad); au1 = bv_q8f(a1h, bad); + Q8_ELSE au0 = bv_q8s(a0h); au1 = bv_q8s(a1h); Q8_END + [unroll] for (uint c2 = 0; c2 < KS; c2 += 2u) { + v8f e0, e1; + if (first) { e0 = bias_tile(pB2, c2); e1 = bias_tile(pB2, c2 + 1u); } else { e0 = dd[c2]; e1 = dd[c2 + 1u]; } + wil2(e0, e1, wd[2u * c2], au0, wd[2u * c2 + 1u], au1, wd[2u * c2 + 2u], au0, wd[2u * c2 + 3u], au1); + dd[c2] = e0; dd[c2 + 1u] = e1; + } +} + +WG_KERNEL(rr_block) +{ + WG_SETUP(); + rr_lane = WaveGetLaneIndex(); + rr_lp = rr_lane & 31u; + rr_hh = rr_lane >> 5; + rr_hf = (rr_lane >> 4) & 1u; + rr_lo = rr_fence_u(rr_lp * 8u + 4u * rr_hh); + rr_xo = rr_fence_u(rr_lp * 2u + rr_hh); + rr_t4 = rr_fence_u(8u * rr_hf + 4u * rr_hh); + rr_f2 = rr_fence_u(2u * rr_hf + rr_hh); + rr_sat = false; + rr_nan = false; + DBG_INIT + const uint wv0 = 2u * WaveReadLaneFirst(wg_lane_id() >> 6); // wave32 wave of tile 0 (tile 1: wv0 + 1) + const uint n = rr_lane & 15u; +#if RR_KIND == 2 + rr_uo = rr_fence_u(64u * n + 2u * rr_hf + rr_hh); // dword of UP slot ((n HID + 16 ot + 8 hf) >> 3) - 4 ot +#endif + const uint wid = RR_WBID(); + const int wi = (int)(wid % (uint)nwx), wj = (int)(wid / (uint)nwx); + const int lx = (int)(n & 7u); + const int X = 8 * wi - sx + lx; + const bool r1 = min(Nx, Ny) >= 10; // one reflection suffices (rr_fast.hlsli rr_refl) + const int Xr = rr_refl(X, Nx, r1); + int Yt[2], Yrt[2]; + uint tok[2]; + bool real[2]; + [unroll] for (uint t0 = 0; t0 < 2u; ++t0) { + const int ly = (int)(2u * (wv0 + t0) + (n >> 3)); + Yt[t0] = 8 * wj - sy + ly; + Yrt[t0] = rr_refl(Yt[t0], Ny, r1); + real[t0] = X >= 0 && X < Nx && Yt[t0] >= 0 && Yt[t0] < Ny; + tok[t0] = (uint)(Yrt[t0] * Nx + Xr); + } + const RrWc wc = rr_wc(wc0, wc1, wc2, wc3, wc4); + const half2 ra2 = rr_splat_lo(c_ra_m), m2 = rr_splat_hi(c_ra_m); + const half2 f2 = rr_splat_lo(c_f_g), g2 = rr_splat_hi(c_f_g); + const RrWx wx = rr_wx(wc); + const float ra2x = (float)ra2.x, m2x = (float)m2.x, f2x = (float)f2.x; + const int Mx = Nx >> 1, My = Ny >> 1; + const int cx0 = (8 * wi - sx) >> 1, cy0 = (8 * wj - sy) >> 1; + + // ---- block input x and residual base ---------------------------------------------------------------------------- + uint2 xf[KS]; +#if RR_KIND == 2 + rrbv xr[KS]; // xf of 51.5 (f32), the first residual base +#endif +#if RR_ADAPT + { // E1: x = Q8(MM(in; Wa; ba)), in = the level-1 token's 128 channels as stored (51.5, 51.6); pX = IN0's y2 itself + // ([2 Ny][2 Nx][32]): child c = 2 dy + dx of the level-1 token is the level-0 token (2 Yr + dy, 2 Xr + dx); its 32 + // bytes are channels 32 c .. 32 c + 31 of the 128 + uint2 inf[8]; + [unroll] for (uint k = 0; k < 8; ++k) { + const uint tc0 = (2u * (uint)Yrt[0] + (k >> 2)) * (2u * (uint)Nx) + 2u * (uint)Xr + ((k >> 1) & 1u); + const uint tc1 = (2u * (uint)Yrt[1] + (k >> 2)) * (2u * (uint)Nx) + 2u * (uint)Xr + ((k >> 1) & 1u); + inf[k] = rr_tld(pX, tc0 * 32u + 16u * (k & 1u), tc1 * 32u + 16u * (k & 1u)); + } + [unroll] for (uint ct = 0; ct < KS; ++ct) { + uint2 a[8]; + [unroll] for (uint k2 = 0; k2 < 8; ++k2) a[k2] = wfrag(pWa, ct * 8u + k2); + v8f acc = bias_tile(pBa, ct); + [unroll] for (uint s = 0; s < 4; ++s) acc = step2l(s == 3u, a[2 * s], inf[2 * s], a[2 * s + 1], inf[2 * s + 1], acc); + uint bad = 0u; + Q8_TRY xf[ct] = q8f(acc, bad); + Q8_ELSE xf[ct] = q8s(acc); Q8_END + } + } +#elif RR_KIND == 2 + { // fusion half (51.5): the window's up-projection (16 output tiles of 16 x 16 coarse tokens; wave32 wave wv computes + // tiles wv + 4 t2) to LDS; then xf = R(d8(skip) f + d8(upq)) per fine token, x = Q8(xf), xr = xf. + const int crx = cx0 + (int)(n & 3u), cry = cy0 + (int)(n >> 2); + const uint ctok = (uint)(rr_refl(cry, My, r1) * Mx + rr_refl(crx, Mx, r1)); + uint2 cf[RR_KC / 16]; + [unroll] for (uint k = 0; k < RR_KC / 16; ++k) { + const uint c = wg_ld32(pCin + rr_t4, ctok * RR_KC + 16u * k); + cf[k] = uint2(c, c); + } + // L01_FPF: the next tile's weight fragments and bias are loaded before the current tile's WMMAs and Q8 group + uint2 an[RR_KC / 16]; + v8f accn = bias_tile2(pBup, wv0, wv0 + 1u); + [unroll] for (uint k0 = 0; k0 < RR_KC / 16; ++k0) + an[k0] = uint2(wfrag1(pWup, wv0 * (RR_KC / 16) + k0), wfrag1(pWup, (wv0 + 1u) * (RR_KC / 16) + k0)); + [unroll] for (uint t2 = 0; t2 < HID / 64; ++t2) { + const uint ot0 = wv0 + 4u * t2, ot1 = ot0 + 1u; + uint2 a[RR_KC / 16]; + [unroll] for (uint k1 = 0; k1 < RR_KC / 16; ++k1) a[k1] = an[k1]; + v8f acc = accn; + if (t2 + 1u < HID / 64) { + accn = bias_tile2(pBup, ot0 + 4u, ot1 + 4u); + [unroll] for (uint k3 = 0; k3 < RR_KC / 16; ++k3) + an[k3] = uint2(wfrag1(pWup, (ot0 + 4u) * (RR_KC / 16) + k3), wfrag1(pWup, (ot1 + 4u) * (RR_KC / 16) + k3)); + } + [unroll] for (uint s = 0; s < RR_KC / 32; ++s) + acc = step2l(s + 1 == RR_KC / 32, a[2 * s], cf[2 * s], a[2 * s + 1], cf[2 * s + 1], acc); + uint bad = 0u; + uint2 uq; + Q8_TRY uq = q8f(acc, bad); + Q8_ELSE uq = q8s(acc); Q8_END + RR_UP32(rr_uo + 4u * ot0) = uq.x; + RR_UP32(rr_uo + 4u * ot1) = uq.y; + } + GroupMemoryBarrierWithGroupSync(); + const uint par0 = wv0 * 4u + (uint)(lx >> 1), par1 = par0 + 4u; + const uint ch0 = 2u * ((uint)Yt[0] & 1u) + ((uint)X & 1u), ch1 = 2u * ((uint)Yt[1] & 1u) + ((uint)X & 1u); + // dword of UP slot ((par HID + ch RC + 16 ct + 8 hf) >> 3) held by this lane: 64 par + 16 ch + 4 ct + 2 hf + hh + const uint ub0 = rr_f2 + 64u * par0 + 16u * ch0, ub1 = rr_f2 + 64u * par1 + 16u * ch1; + uint bad = 0u; + [unroll] for (uint ct = 0; ct < KS; ++ct) { + const uint2 sk = rr_tld(pX, tok[0] * RC + 16u * ct, tok[1] * RC + 16u * ct); + const uint2 up = uint2(RR_UP32(ub0 + 4u * ct), RR_UP32(ub1 + 4u * ct)); + xr[ct] = bv_mad(bv_d8(sk), f2x, bv_d8(up)); + } + Q8_TRY [unroll] for (uint c0 = 0; c0 < KS; ++c0) xf[c0] = bv_q8f(xr[c0], bad); + Q8_ELSE [unroll] for (uint c = 0; c < KS; ++c) xf[c] = bv_q8s(xr[c]); Q8_END + } +#else + [unroll] for (uint ks = 0; ks < KS; ++ks) xf[ks] = rr_tld(pX, tok[0] * RC + 16u * ks, tok[1] * RC + 16u * ks); +#endif + + // ---- heads: steps 1-6 ------------------------------------------------------------------------------------------ + rrbv dp[KS]; + [unroll] for (uint h = 0; h < RH; ++h) { + // [A] q_h (features x tokens) and v_h^T (tokens x features) + v8f aq0 = zero8(), aq1 = zero8(), av0 = zero8(), av1 = zero8(); + [unroll] for (uint s = 0; s < NS; ++s) { + const uint2 q00 = qv_frag(h, 2u * s), q01 = qv_frag(h, 2u * s + 1u); + const uint2 q10 = qv_frag(h, KS + 2u * s), q11 = qv_frag(h, KS + 2u * s + 1u); + const uint2 v00 = qv_frag(h, 2u * KS + 2u * s), v01 = qv_frag(h, 2u * KS + 2u * s + 1u); + const uint2 v10 = qv_frag(h, 3u * KS + 2u * s), v11 = qv_frag(h, 3u * KS + 2u * s + 1u); + wil4(aq0, aq1, av0, av1, q00, xf[2 * s], q01, xf[2 * s + 1], q10, xf[2 * s], q11, xf[2 * s + 1], + xf[2 * s], v00, xf[2 * s + 1], v01, xf[2 * s], v10, xf[2 * s + 1], v11); + } + uint bad = 0u; + uint2 qf0, qf1, vf0, vf1; + Q8_TRY qf0 = q8f(aq0, bad); qf1 = q8f(aq1, bad); vf0 = q8f(av0, bad); vf1 = q8f(av1, bad); + Q8_ELSE qf0 = q8s(aq0); qf1 = q8s(aq1); vf0 = q8s(av0); vf1 = q8s(av1); Q8_END + rr_xq32[rr_xqr(XB(h) + (wv0 * 2u) * 32u)] = qf0.x; + rr_xq32[rr_xqr(XB(h) + (wv0 * 2u + 1u) * 32u)] = qf1.x; + rr_xq32[rr_xqr(XB(h) + 256u + (wv0 * 2u) * 32u)] = vf0.x; + rr_xq32[rr_xqr(XB(h) + 256u + (wv0 * 2u + 1u) * 32u)] = vf1.x; + rr_xq32[rr_xqr(XB(h) + (wv0 * 2u + 2u) * 32u)] = qf0.y; + rr_xq32[rr_xqr(XB(h) + (wv0 * 2u + 3u) * 32u)] = qf1.y; + rr_xq32[rr_xqr(XB(h) + 256u + (wv0 * 2u + 2u) * 32u)] = vf0.y; + rr_xq32[rr_xqr(XB(h) + 256u + (wv0 * 2u + 3u) * 32u)] = vf1.y; + GroupMemoryBarrierWithGroupSync(); + + // [B] scores (key tile J x the tile's queries), start value = the score bias (f32 fragment, per tile) + const uint tb0 = pTb + 4u * rr_lo + (h * 16u + wv0 * 4u) * 1024u, tb1 = tb0 + 4u * 1024u; + rrbv wt[4]; + { + uint2 k[8]; + [unroll] for (uint i8 = 0; i8 < 8; ++i8) k[i8] = rr_xqs(XB(h) + i8 * 32u); + v8f s0 = rr_cat4(wg_ld4f(tb0, 0u), wg_ld4f(tb1, 0u)), s1 = rr_cat4(wg_ld4f(tb0, 1024u), wg_ld4f(tb1, 1024u)); + v8f s2 = rr_cat4(wg_ld4f(tb0, 2048u), wg_ld4f(tb1, 2048u)), s3 = rr_cat4(wg_ld4f(tb0, 3072u), wg_ld4f(tb1, 3072u)); + wil4(s0, s1, s2, s3, k[0], qf0, k[1], qf1, k[2], qf0, k[3], qf1, k[4], qf0, k[5], qf1, k[6], qf0, k[7], qf1); + wt[0] = bv_attn(s0, BVW); wt[1] = bv_attn(s1, BVW); wt[2] = bv_attn(s2, BVW); wt[3] = bv_attn(s3, BVW); + } + // step 4: Z, r = 1 / Z, P = Q8(w r) (f32 softmax normalisation; Z as the wave32 tree, see the head comment) + v8f zt; + [unroll] for (uint i3 = 0; i3 < 8; ++i3) zt.v[i3] = (wt[0].v[i3] + wt[1].v[i3]) + (wt[2].v[i3] + wt[3].v[i3]); + const float zh0 = (zt.v[0] + zt.v[1]) + (zt.v[2] + zt.v[3]), zh1 = (zt.v[4] + zt.v[5]) + (zt.v[6] + zt.v[7]); + const float zs0 = zh0 + wg_xor(zh0, 32u), zs1 = zh1 + wg_xor(zh1, 32u); + const float rz0 = rcp(zs0 + wg_xor16(zs0)), rz1 = rcp(zs1 + wg_xor16(zs1)); + uint2 pf[4]; + [unroll] for (uint J4 = 0; J4 < 4; ++J4) { + v8f p; + [unroll] for (uint i4 = 0; i4 < 4; ++i4) { p.v[i4] = wt[J4].v[i4] * rz0; p.v[4 + i4] = wt[J4].v[4 + i4] * rz1; } + pf[J4] = rr_q8_small(p); + } + // step 5: O_h = Q8(MM(P; v_h)), keys 0-31 then 32-63 + v8f ov[2]; + { + uint2 vt[8]; + [unroll] for (uint i8 = 0; i8 < 8; ++i8) vt[i8] = rr_xqs(XB(h) + 256u + i8 * 32u); + ov[0] = zero8(); ov[1] = zero8(); + wil2(ov[0], ov[1], vt[0], pf[0], vt[2], pf[1], vt[1], pf[0], vt[3], pf[1]); + wil2(ov[0], ov[1], vt[4], pf[2], vt[6], pf[3], vt[5], pf[2], vt[7], pf[3]); + } + bad = 0u; + uint2 of[2]; + Q8_TRY of[0] = q8f(ov[0], bad); of[1] = q8f(ov[1], bad); + Q8_ELSE of[0] = q8s(ov[0]); of[1] = q8s(ov[1]); Q8_END + // step 6: Dp step h + [unroll] for (uint ct = 0; ct < KS; ++ct) { + v8f c; + if (h == 0u) c = bias_tile(pBo, ct); else c = bv_un(dp[ct]); + dp[ct] = c; + } + [unroll] for (uint c2 = 0; c2 < KS; c2 += 2u) + wil2(dp[c2], dp[c2 + 1u], wo_frag(h, 2u * c2), of[0], wo_frag(h, 2u * c2 + 1u), of[1], + wo_frag(h, 2u * c2 + 2u), of[0], wo_frag(h, 2u * c2 + 3u), of[1]); + } + + // ---- step 7: first residual ------------------------------------------------------------------------------------- + rrbv y1h[KS]; + uint2 y1q[KS]; + { + uint bad = 0u; + [unroll] for (uint c1 = 0; c1 < KS; ++c1) { +#if RR_KIND == 2 + y1h[c1] = bv_mad(dp[c1], ra2x, xr[c1]); +#else + y1h[c1] = bv_mad(dp[c1], ra2x, bv_d8(xf[c1])); +#endif + } + Q8_TRY [unroll] for (uint c0 = 0; c0 < KS; ++c0) y1q[c0] = bv_q8f(y1h[c0], bad); + Q8_ELSE [unroll] for (uint c = 0; c < KS; ++c) y1q[c] = bv_q8s(y1h[c]); Q8_END + } + + // ---- step 8: MLP (chunk 0 peeled: its Dd steps start from the f32 bias; the chunk loop fully unrolled) ------------ + rrbv dd[KS]; + mlp_chunk(0u, true, y1q, dd, BVW); + [unroll] for (uint j = 1; j < NJ; ++j) mlp_chunk(j, false, y1q, dd, BVW); + + // ---- step 9: second residual, block output y2 --------------------------------------------------------------------- + uint2 y2[KS]; + { + rrbv yh[KS]; + uint bad = 0u; + [unroll] for (uint c3 = 0; c3 < KS; ++c3) yh[c3] = bv_mad(dd[c3], m2x, y1h[c3]); + Q8_TRY [unroll] for (uint c0 = 0; c0 < KS; ++c0) y2[c0] = bv_q8f(yh[c0], bad); + Q8_ELSE [unroll] for (uint c = 0; c < KS; ++c) y2[c] = bv_q8s(yh[c]); Q8_END + } + [unroll] for (uint t4 = 0; t4 < 2u; ++t4) { + const uint otok = (uint)(Yt[t4] * Nx + X); + if (real[t4]) { + [unroll] for (uint c4 = 0; c4 < KS; ++c4) + wg_st32(pY2 + rr_t4, otok * RC + 16u * c4, t4 == 0u ? y2[c4].x : y2[c4].y); + } + } + + // ---- merge (51.4, E1) ---------------------------------------------------------------------------------------------- +#if RR_CN > 0 + { + // y2 of token t = 16 (wv0 + tile) + n into the merge buffer: dword 16 t + 4 c + 2 hf + hh of MY (slot 8 t + 2 c + hf) + const uint mt = 16u * wv0 + n; + [unroll] for (uint c5 = 0; c5 < KS; ++c5) { + MY32(16u * mt + rr_f2 + 4u * c5) = y2[c5].x; + MY32(16u * mt + 256u + rr_f2 + 4u * c5) = y2[c5].y; + } + GroupMemoryBarrierWithGroupSync(); + const uint rx = n & 3u, ry = n >> 2; + const int Xc = cx0 + (int)rx, Yc = cy0 + (int)ry; + const bool cin = Xc >= 0 && Xc < Mx && Yc >= 0 && Yc < My; + // wave32 wave wv computes output tiles ot = wv, wv + 4 (< RR_CN / 16): tile 0 = ot0, tile 1 = ot0 + 1; the second + // pair exists for wave64 wave 0 only (ot0 = 4); wave-uniform + [unroll] for (uint t2 = 0; t2 < 2u; ++t2) { + const uint ot0 = wv0 + 4u * t2, ot1 = ot0 + 1u; + [branch] if (ot0 < RR_CN / 16) { + v8f acc = bias_tile2(pBm, ot0, ot1); + // C = 64, HID = 256: the coarse token's four children: zi = 32 s + 16 e + 8 hf, cn = s >> 1, MY slot of (s, e) + // = 128 ry + 16 rx + hf + 64 (s >> 2) + 8 ((s >> 1) & 1) + 4 (s & 1) + 2 e; this lane's dword = 2 slot + hh + const uint mb = 256u * ry + 32u * rx + rr_f2; + [unroll] for (uint s = 0; s < HID / 32; ++s) { + uint2 b[2], a[2]; + [unroll] for (uint e = 0; e < 2; ++e) { + const uint w = MY32(mb + 128u * (s >> 2) + 16u * ((s >> 1) & 1u) + 8u * (s & 1u) + 4u * e); + b[e] = uint2(w, w); + a[e] = uint2(wfrag1(pWm, ot0 * (HID / 16) + 2u * s + e), wfrag1(pWm, ot1 * (HID / 16) + 2u * s + e)); + } + acc = step2l(s + 1 == HID / 32, a[0], b[0], a[1], b[1], acc); + } + uint bad = 0u; + uint2 mq; + Q8_TRY mq = q8f(acc, bad); + Q8_ELSE mq = q8s(acc); Q8_END +#if RR_Q8_FAST + // Convergent wave barrier on mq before branching on cin: prevents LLVM from hoisting + // `if (cin)` above the merge WMMAs (RDNA 4 v_wmma under narrow EXEC produces non-finite lanes). + const bool mq_ok = !WaveActiveAnyTrue(((mq.x | mq.y) & rr_zero()) != 0u); + if (and(mq_ok, cin)) { +#else + if (cin && rr_zero() == 0u) { +#endif + wg_st32(pMerged + rr_t4, (uint)(Yc * Mx + Xc) * RR_CN + 16u * ot0, mq.x); + wg_st32(pMerged + rr_t4, (uint)(Yc * Mx + Xc) * RR_CN + 16u * ot1, mq.y); + } + } + } + } +#endif + + // ---- hidden state (51.4) ----------------------------------------------------------------------------------------- +#if HIDDEN + { + // Rows 0-3 of the Wh tile (the 4 hidden channels) are in the hf = 0, hh = 0 lanes: dwords x, y for tile 0, z, w for + // tile 1. Tile 1's are moved to the hh = 1 lanes (lane ^ 32), so lanes 0-15 run tile 0's token n and lanes 32-47 + // tile 1's, with the operations of the wave32 lanes 0-15 (the other lanes compute unused values, as the wave32 + // hf = 1 lanes do). + v8f dh = rr_dup4(wg_ld4f(pBh + 4u * rr_t4, 0u)); + uint4 dhb; + [unroll] for (uint s = 0; s < NS; ++s) { + if (s + 1 < NS) dh = step2(wfrag(pWh, 2u * s), y2[2 * s], wfrag(pWh, 2u * s + 1u), y2[2 * s + 1], dh); + else dhb = step2h(wfrag(pWh, 2u * s), y2[2 * s], wfrag(pWh, 2u * s + 1u), y2[2 * s + 1], dh); + } + const uint z1 = wg_xor_u(dhb.z, 32u), w1 = wg_xor_u(dhb.w, 32u); + const bool t1 = rr_hh != 0u; + const uint dx = t1 ? z1 : dhb.x, dy = t1 ? w1 : dhb.y; + const uint tk = t1 ? tok[1] : tok[0]; + const int Yh = t1 ? Yt[1] : Yt[0]; + const bool rl = t1 ? real[1] : real[0]; + const uint s01 = wg_ld16(pSig, (dx & 0xffffu) * 2u) | (wg_ld16(pSig, (dx >> 16) * 2u) << 16); + const uint s23 = wg_ld16(pSig, (dy & 0xffffu) * 2u) | (wg_ld16(pSig, (dy >> 16) * 2u) << 16); + const uint2 hold = wg_ld64(pHold, tk * 8u); + uint2 hn; +#if RR_KIND == 0 + const uint G01 = rr_fence_u(rr_b2(rr_h2(s01) * g2)), G23 = rr_fence_u(rr_b2(rr_h2(s23) * g2)); + hn.x = rr_fence_u(rr_b2(rr_h2(hold.x) * rr_h2(G01))); + hn.y = rr_fence_u(rr_b2(rr_h2(hold.y) * rr_h2(G23))) & 0xffffu; +#else + const half2 z2 = rr_splat_hi(s23); + const uint omzb = rr_fence_u(rr_b2(half2(1.0, 1.0) - z2)); + const half2 omz = rr_h2(omzb); + const int xm = (X - 1) >> 1, ym = (Yh - 1) >> 1; + const int rxm = rr_refl(xm, Mx, r1), rxp = rr_refl(xm + 1, Mx, r1); + const int rym = rr_refl(ym, My, r1), ryp = rr_refl(ym + 1, My, r1); + const half2 wxp = (X & 1) ? half2(0.25, 0.25) : half2(0.75, 0.75); + const half2 wyp = (Yh & 1) ? half2(0.25, 0.25) : half2(0.75, 0.75); + const half2 wxm = (X & 1) ? half2(0.75, 0.75) : half2(0.25, 0.25); + const half2 wym = (Yh & 1) ? half2(0.75, 0.75) : half2(0.25, 0.25); + const uint2 cA = wg_ld64(pHc, (uint)(rym * Mx + rxp) * 8u), cB = wg_ld64(pHc, (uint)(rym * Mx + rxm) * 8u); + const uint2 cC = wg_ld64(pHc, (uint)(ryp * Mx + rxm) * 8u), cD = wg_ld64(pHc, (uint)(ryp * Mx + rxp) * 8u); + uint2 u; + [unroll] for (uint e = 0; e < 2; ++e) { + const uint a = e ? cA.y : cA.x, b = e ? cB.y : cB.x, c = e ? cC.y : cC.x, d = e ? cD.y : cD.x; + half2 tt = rr_h2(rr_fence_u(rr_b2(rr_h2(rr_fence_u(rr_b2(rr_h2(a) * wxp))) * wym))); + tt = rr_h2(rr_fence_u(rr_b2(mad(rr_h2(rr_fence_u(rr_b2(rr_h2(b) * wxm))), wym, tt)))); + tt = rr_h2(rr_fence_u(rr_b2(mad(rr_h2(rr_fence_u(rr_b2(rr_h2(c) * wxm))), wyp, tt)))); + tt = rr_h2(rr_fence_u(rr_b2(mad(rr_h2(rr_fence_u(rr_b2(rr_h2(d) * wxp))), wyp, tt)))); + if (e == 0) u.x = rr_b2(tt); else u.y = rr_b2(tt); + } + const uint gt01 = rr_fence_u(rr_b2(rr_h2(s01) * g2)), gt23 = rr_fence_u(rr_b2(rr_h2(s23) * g2)); + const uint ug01 = rr_fence_u(rr_b2(rr_h2(u.x) * rr_h2(gt01))), ug23 = rr_fence_u(rr_b2(rr_h2(u.y) * rr_h2(gt23))); + const uint hh01 = rr_fence_u(rr_b2(rr_h2(ug01) * z2)); + const uint hh23 = rr_fence_u(rr_b2(rr_h2(ug23) * z2)) & 0xffffu; + hn.x = rr_fence_u(rr_b2(mad(rr_h2(hold.x), omz, rr_h2(hh01)))); + hn.y = rr_fence_u(rr_b2(mad(rr_h2(hold.y), omz, rr_h2(hh23)))); +#endif + const uint otok = (uint)(Yh * Nx + X); + if (rl && rr_hf == 0u) wg_st64(pHnew, otok * 8u, hn); +#if RR_HMERGE + const half2 quarter = half2(0.25, 0.25); + const uint qx = rr_fence_u(rr_b2(rr_h2(hn.x) * quarter)), qy = rr_fence_u(rr_b2(rr_h2(hn.y) * quarter)); + const uint l0 = 2u * (n & 3u) + 32u * rr_hh; + const uint q0x = WaveReadLaneAt(qx, l0), q1x = WaveReadLaneAt(qx, l0 + 1u); + const uint q2x = WaveReadLaneAt(qx, l0 + 8u), q3x = WaveReadLaneAt(qx, l0 + 9u); + const uint q0y = WaveReadLaneAt(qy, l0), q1y = WaveReadLaneAt(qy, l0 + 1u); + const uint q2y = WaveReadLaneAt(qy, l0 + 8u), q3y = WaveReadLaneAt(qy, l0 + 9u); + const uint ax = rr_fence_u(rr_b2(rr_h2(q0x) + rr_h2(q1x))), bx = rr_fence_u(rr_b2(rr_h2(q2x) + rr_h2(q3x))); + const uint ay = rr_fence_u(rr_b2(rr_h2(q0y) + rr_h2(q1y))), by = rr_fence_u(rr_b2(rr_h2(q2y) + rr_h2(q3y))); + const uint2 hm = uint2(rr_b2(rr_h2(ax) + rr_h2(bx)), rr_b2(rr_h2(ay) + rr_h2(by))); + const int hXc = cx0 + (int)(n & 3u), hYc = cy0 + (int)(wv0 + rr_hh); + if (n < 4u && rr_hf == 0u && hXc >= 0 && hXc < Mx && hYc >= 0 && hYc < My) + wg_st64(pHmerged, (uint)(hYc * Mx + hXc) * 8u, hm); +#endif + } +#endif + DBG_FLUSH +} diff --git a/clean/rr_f/kernels/rr_block_fast.hlsl b/clean/rr_f/kernels/rr_block_fast.hlsl index fe49b301..04567fe6 100644 --- a/clean/rr_f/kernels/rr_block_fast.hlsl +++ b/clean/rr_f/kernels/rr_block_fast.hlsl @@ -41,7 +41,11 @@ WG_ARGS_END // Defaults with the relaxed body (RR_B32: modes accurate and fast; the exact body's kernels keep their DXIL): // L01_MRGU 4, L01_UNRALL 28, L01_MDPF 28, L01_FPF 8 (cap8 + cap48 byte-identical in accurate and fast; game_bench // 1440p Q accurate -0.059 / fast -0.056 ms, 6 interleaved rounds); L01_HPF and L01_MLPDEF stay opt-in (no gain). -#if defined(RR_B32) && RR_B32 +// clean/rr-mp2-level1 round 3 (exact body, same levers): the exact step-E body (RR_Z 2, RR_STEP 0: stageex.* / stagedx.*) +// takes the same loop-shape / issue-order defaults. They are structural (the same operations in the same order, so the +// output bits cannot change) and E1 / D1-F / D1-R sit at 112-120 VGPRs, far from the 192 bound. L01_MDPF is a no-op +// there (it needs RR_WIL, which is relaxed-body only). The step X / Z / XF kernels keep their DXIL. +#if (defined(RR_B32) && RR_B32) || (RR_Z == 2 && RR_STEP == 0) #define RR_L1R2 1 #else #define RR_L1R2 0 @@ -154,6 +158,17 @@ uint64_t rr_2q(uint2 v) { return (uint64_t)v.x | ((uint64_t)v.y << 32); } #error "RR_QV1 needs RR_WSRC 1 and H = 2" #endif // ---- LDS -------------------------------------------------------------------------------------------------------------- +// clean/rr-f-wave4 R4.2b RR_OVL: the exact body's LDS overlays (MY in head 0's exchange buffer, UP in head 1's; round-11) +// for the relaxed body (RR_B32) too. The lifetime argument below does not mention the row-sum mode: both bodies run the +// same head loop (one barrier per head, head h writes buffer h & 1) and no deferred rerun (RR_DEFER / RR_QV1 are 0 with +// L01_ACC 0). RR_OVL 0 = the relaxed body's kernels as before (A/B; the runtime's WG_RR_W4 bit 1). Not with the dataflow form. +#ifndef RR_OVL +#if (RR_Z == 2 || (defined(RR_B32) && RR_B32 && !L01_ACC && !RR_DFLOW)) +#define RR_OVL 1 +#else +#define RR_OVL 0 +#endif +#endif groupshared uint64_t rr_xq[1024]; // q / v exchange, two head buffers: q [wave][ft][lane], v^T after #define XB(h) (((h) & 1u) * 512u) #if RR_WSRC == 0 @@ -161,7 +176,7 @@ groupshared uint64_t rr_wb[2 * UNIT / 8]; #define MY(i) rr_wb[i] #define UP(i) rr_wb[UNIT / 8 + (i)] #else -#if RR_CN > 0 && RR_Z == 2 && RH == 2 && 64 * RC / 8 <= 512 && !RR_QV1 +#if RR_CN > 0 && RR_OVL && RH == 2 && 64 * RC / 8 <= 512 && !RR_QV1 // clean/rr-f-s13x-x-l01 (exact body only; round-11 item 5): the merge buffer lives in head 0's exchange buffer (head 1 // uses buffer 1). Its last reader is head 0's PV, and every wave passes head 1's barrier only after those reads. Same // operations and bits; LDS 12288 -> 8192 B (E1). @@ -170,7 +185,7 @@ groupshared uint64_t rr_wb[2 * UNIT / 8]; groupshared uint64_t rr_my[64 * RC / 8]; #define MY(i) rr_my[i] #endif -#if RR_KIND == 2 && RR_Z == 2 && RH == 2 && 16 * HID / 8 <= 512 && !RR_QV1 +#if RR_KIND == 2 && RR_OVL && RH == 2 && 16 * HID / 8 <= 512 && !RR_QV1 // clean/rr-f-s13x-x-l01 (exact body only): the fusion up-projections live in head 1's exchange buffer. They are // written and read before the head loop, and head 1 first writes its buffer after head 0's barrier, which every wave // reaches only after its last UP read. Same operations and bits; LDS 12288 -> 8192 B (D1-F). @@ -845,7 +860,7 @@ WG_KERNEL(rr_block) st_sync(); } #ifndef L01_UNR2MASK -#if defined(RR_B32) && RR_B32 // a14 default (relaxed body): 2x MLP chunk loop on E1, D1-F, D1-R +#if RR_L1R2 // a14 default (relaxed body, and since round 3 the exact step-E body): 2x MLP chunk loop on E1, D1-F, D1-R #define L01_UNR2MASK 28 #else #define L01_UNR2MASK 0 diff --git a/clean/rr_f/kernels/rr_block_wide.hlsl b/clean/rr_f/kernels/rr_block_wide.hlsl index 2c2008b3..4523e648 100644 --- a/clean/rr_f/kernels/rr_block_wide.hlsl +++ b/clean/rr_f/kernels/rr_block_wide.hlsl @@ -649,7 +649,14 @@ WG_KERNEL(rr_block) uint2 mq; Q8_TRY mq = q8f(acc, bad); Q8_ELSE mq = q8s(acc); Q8_END +#if RR_Q8_FAST + // Convergent wave barrier on mq before branching on cin: prevents LLVM from hoisting + // `if (cin)` above the merge WMMAs (RDNA 4 v_wmma under narrow EXEC produces non-finite lanes). + const bool mq_ok = !WaveActiveAnyTrue(((mq.x | mq.y) & rr_zero()) != 0u); + if (and(mq_ok, cin)) RR_PST64(pMerged, (uint)(Yc * Mx + Xc) * RR_CN + 16u * ot + 8u * rr_hf, mq); +#else if (cin && rr_zero() == 0u) RR_PST64(pMerged, (uint)(Yc * Mx + Xc) * RR_CN + 16u * ot + 8u * rr_hf, mq); +#endif } } #endif diff --git a/clean/rr_f/kernels/rr_block_wide_w64.hlsl b/clean/rr_f/kernels/rr_block_wide_w64.hlsl new file mode 100644 index 00000000..ae68f139 --- /dev/null +++ b/clean/rr_f/kernels/rr_block_wide_w64.hlsl @@ -0,0 +1,772 @@ +// SPDX-License-Identifier: MIT +// clean/rr-f-wave4c W4C.1: the spec 51 window block of the wide stages E2, E3, D2-F, D2-R, D3-F, D3-R, D4-F and D4-R (RR_G +// groups of four token tiles, RR_B32 1) in wave64, design D of rr_block_c32.hlsl: the same operations as rr_block_wide.hlsl +// (and so, value for value, the same output bits), arranged as rr_block_c64_w64.hlsl arranges the level-1 stages. +// +// One workgroup = one 8 x 8 window and 2 RR_G wave64 waves (WG_BLOCK 128 RR_G): wave w = 2 g + tw owns, for group g, the +// token tiles wv = 2 tw (tile 0) and 2 tw + 1 (tile 1) of the wave32 kernel's waves (4 g + wv). Group g computes the +// same heads / MLP chunks as the wave32 group g, with the same LDS buffers (byte layout and size unchanged: QV, OB, Y, AU, UP). +// Wave64 lane L = l + 32 hh holds, for each tile, half hh of wave32 lane l's fragment (l = 16 hf + n): fp8 A / B bytes +// 4 hh .. 4 hh + 3, accumulator elements 4 hh .. 4 hh + 3 (the v_wmma wave64 layout). A v8f holds elements 0-3 of tile 0 +// and 0-3 of tile 1; a uint2 fp8 fragment: x = tile 0, y = tile 1. Every value is computed by the operation the wave32 +// kernel computes it with (wave64 v_wmma = wave32 v_wmma bit for bit, conversions per value). What moves: +// * per-token quantities (coordinates, loads, stores, score biases, the hidden state) once per tile; +// * shared operands (weights, biases, the k / v fragments of the exchange, the coarse fragments of the fusion, the merge's +// coarse tokens) loaded once, 4 bytes per lane, used by both tiles (halving weight VMEM and VGPRs); +// * the fusion up-projection and the merge compute two output tiles (ot0, ot1 = ot0 + 1) per wave from one shared B; +// * the softmax sum Z as the wave32 tree (A + B or B + A over the two halves: IEEE addition commutes; only a NaN's +// payload could differ, and rz only feeds w * rz -> v_cvt_pk_fp8_f32, which gives 0xff for every NaN); +// * the hidden state: wave (g, tw) with g == tw covers all four tiles with the lane swap of rr_block_c64_w64.hlsl. +// The Q8 votes (WaveActiveAnyTrue) cover both tiles: both forms of a Q8 group give rr_q8's bits for every input. +// +// Defines as rr_block_wide.hlsl (RR_C, RR_H, RR_KIND 0 / 2 / 3, RR_CN, RR_HMERGE, RR_KC, RR_G, RR_PRE, RR_MLK, RR_B32G4), +// RR_B32 1, RR_VARL 1, RR_Z 0, RR_STEP 0, WG_BLOCK = 128 RR_G, WG_WAVE 64. No E1 adapter, no dataflow form. +#include "wg_platform.hlsli" +#if WG_WAVE != 64 || WG_BLOCK != 128 * RR_G +#error "rr_block_wide_w64.hlsl: WG_WAVE 64, WG_BLOCK = 128 RR_G only" +#endif +#include "wg_ags.hlsli" +wg_frag wg_frag_v8f(v8f c) { wg_frag f; [unroll] for (uint i = 0; i < 8; ++i) f.v[i] = asuint(c.v[i]); return f; } +v8f wg_v8f_frag(wg_frag f) { v8f c; [unroll] for (uint i = 0; i < 8; ++i) c.v[i] = asfloat(f.v[i]); return c; } +wg_frag wg_frag2(uint2 a) { wg_frag f; f.v[0] = a.x; f.v[1] = a.y; [unroll] for (uint i = 2; i < 8; ++i) f.v[i] = 0u; return f; } +wg_frag rr_w64f(uint a, uint b, uint c, uint d) // a wave64 fragment: the first 4 / 2 / 1 dwords are used +{ + wg_frag f; + f.v[0] = a; f.v[1] = b; f.v[2] = c; f.v[3] = d; + [unroll] for (uint i = 4; i < 8; ++i) f.v[i] = 0u; + return f; +} +v8f wg_wmma_fp8(uint2 a, uint2 b, v8f c) // tile 0: (a.x, b.x, c 0-3), tile 1: (a.y, b.y, c 4-7) +{ + const wg_frag r0 = wg_ags_mma(WG_AGS_FP8_F32, rr_w64f(a.x, 0u, 0u, 0u), rr_w64f(b.x, 0u, 0u, 0u), + rr_w64f(asuint(c.v[0]), asuint(c.v[1]), asuint(c.v[2]), asuint(c.v[3]))); + const wg_frag r1 = wg_ags_mma(WG_AGS_FP8_F32, rr_w64f(a.y, 0u, 0u, 0u), rr_w64f(b.y, 0u, 0u, 0u), + rr_w64f(asuint(c.v[4]), asuint(c.v[5]), asuint(c.v[6]), asuint(c.v[7]))); + v8f d; + [unroll] for (uint i = 0; i < 4; ++i) { d.v[i] = asfloat(r0.v[i]); d.v[4 + i] = asfloat(r1.v[i]); } + return d; +} +uint2 wg_pack_q8_nc_ags(v8f d) // v_cvt_pk_fp8_f32 per value pair, element i -> byte i of its tile's dword +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(asuint(d.v[0]), asuint(d.v[1]), asuint(d.v[2]), asuint(d.v[3])), + WG_AGS_T_F32_ACC, WG_AGS_T_FP8_B); + const wg_frag r1 = wg_ags_convert(rr_w64f(asuint(d.v[4]), asuint(d.v[5]), asuint(d.v[6]), asuint(d.v[7])), + WG_AGS_T_F32_ACC, WG_AGS_T_FP8_B); + return uint2(r0.v[0], r1.v[0]); +} +uint4 wg_to_f16_ags(v8f d) // v_cvt_f16_f32 per value (round to nearest even) +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(asuint(d.v[0]), asuint(d.v[1]), asuint(d.v[2]), asuint(d.v[3])), + WG_AGS_T_F32_ACC, WG_AGS_T_F16_ACC); + const wg_frag r1 = wg_ags_convert(rr_w64f(asuint(d.v[4]), asuint(d.v[5]), asuint(d.v[6]), asuint(d.v[7])), + WG_AGS_T_F32_ACC, WG_AGS_T_F16_ACC); + return uint4(r0.v[0], r0.v[1], r1.v[0], r1.v[1]); +} +#define rr_d8 rr_d8_w32 // rr_common.hlsli's wave32 text, renamed; the wave64 rr_d8 follows the includes + +WG_ARGS_BEGIN + uint pX, pCin, pHold, pHc; + uint pWqv, pTb, pWo, pBo, pMlp, pB1, pB2, pWm, pBm, pWa, pBa, pWup, pBup, pWh, pBh; + uint pSig, pZ, pY2, pMerged, pHnew, pHmerged; + int Nx, Ny, sx, sy, nwx; + uint c_ra_m, c_f_g; + uint wc0, wc1, wc2, wc3, wc4; +WG_ARGS_END + +#define RR_L01BIT (RR_KIND == 2 ? 8 : 16) // this stage's bit in the L01_* masks (as rr_block_fast.hlsl) +#include "rr_common.hlsli" +#include "rr_step.hlsli" +#undef rr_d8 +v8f rr_d8(uint2 q) // e4m3 bytes -> f32 (exact), one conversion per tile +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(q.x, 0u, 0u, 0u), WG_AGS_T_FP8_B, WG_AGS_T_F32_ACC); + const wg_frag r1 = wg_ags_convert(rr_w64f(q.y, 0u, 0u, 0u), WG_AGS_T_FP8_B, WG_AGS_T_F32_ACC); + v8f d; + [unroll] for (uint i = 0; i < 4; ++i) { d.v[i] = asfloat(r0.v[i]); d.v[4 + i] = asfloat(r1.v[i]); } + return d; +} + +#ifndef RR_KIND +#define RR_KIND 0 +#endif +#ifndef RR_CN +#define RR_CN 0 +#endif +#ifndef RR_HMERGE +#define RR_HMERGE 0 +#endif +#ifndef RR_KC +#define RR_KC 0 +#endif +#ifndef RR_Z +#define RR_Z 0 +#endif +#ifndef RR_G +#define RR_G 2 +#endif +#ifndef RR_MLK_CMAX +#define RR_MLK_CMAX 96 +#endif +// operand loads issued ahead of the matrix steps (bits): 1 heads, 2 MLP, 4 merge, 8 hidden (defaults as rr_block_wide.hlsl) +#ifndef RR_PRE +#if RR_G == 2 && RR_C == 128 +#define RR_PRE 14 +#else +#define RR_PRE 15 +#endif +#endif +#ifdef RR_ADAPT +#if RR_ADAPT +#error "rr_block_wide_w64.hlsl: no E1 adapter" +#endif +#endif + +#define RC RR_C +#define RH RR_H +#define G RR_G +#define KS (RC / 16) +#define NS (RC / 32) +#define HID (4 * RC) +#define NJ (HID / 32) +#define HIDDEN (RR_KIND == 0 || RR_KIND == 3) +#define UNIT (64 * RC) +#define OWN ((KS + G - 1) / G) // channel tiles per wave in the chains +#define HR (RH / G) // head rounds +#ifndef RR_MLK // MLP chunks per group per round (2: half the rounds, one barrier per round) +#if (RR_PRE & 2) && RR_G == 2 && ((4 * RR_C / 32) % 4) == 0 && RR_C <= RR_MLK_CMAX +#define RR_MLK 2 +#else +#define RR_MLK 1 +#endif +#endif +#define MR (NJ / (G * RR_MLK)) // MLP rounds +#if (RH % G) != 0 || (NJ % G) != 0 || WG_BLOCK != 128 * G +#error "rr_block_wide_w64.hlsl: RR_H and RR_C / 8 must be multiples of RR_G, WG_BLOCK = 128 RR_G" +#endif +#if (RC % 16) != 0 || ((HID / 16) % 2) != 0 || (KS % 1) != 0 +#error "rr_block_wide_w64.hlsl: RR_C must be a multiple of 16 and 4 RR_C / 16 even" +#endif +#if RR_CN > 0 && ((RR_CN / 16) % 2) != 0 +#error "rr_block_wide_w64.hlsl: RR_CN / 16 must be even (the merge's two output tiles per wave)" +#endif +uint mrg_k(uint z0) { const uint cn = z0 / RC; return (8u * (cn >> 1) + (cn & 1u)) * (RC / 8u) + (z0 % RC) / 8u; } + +// ---- LDS: the wave32 kernel's rr_l[LDS_N] (uint64 entries), addressed per dword: wave32 slot i = dwords 2 i, 2 i + 1; a +// wave64 lane holds dword 2 i + hh of each slot of its tiles. Layouts (slots) as rr_block_wide.hlsl: +// heads: QV [g][512] (q [tile][ft][lane], v^T at +256) | OB [g][tile][ft][lane] +// MLP: Y [token][C] bytes (y1q; later y2) | AU [buf][g][tile][f][lane] +// front: UP [coarse r][4C] bytes (fusion halves) +#define QV_OFF 0u +#define OB_OFF (512u * G) +#define Y_OFF 0u +#define Y_N (8u * RC) +#define AU_OFF Y_N +#define LDS_HEAD (768u * G) +#define LDS_MLP (Y_N + 512u * G * RR_MLK) +#define LDS_N (LDS_HEAD > LDS_MLP ? LDS_HEAD : LDS_MLP) +groupshared uint rr_l32[2 * LDS_N]; + +static uint rr_lane; // lane in the wave +static uint rr_hf; // (lane >> 4) & 1 +static uint rr_lp; // wave32 lane whose fragment halves this lane holds (lane & 31) +static uint rr_hh; // lane >> 5: the half (fp8 bytes 4 hh .., accumulator elements 4 hh ..) +// per-lane offsets, fenced (rr_fence_u: the same value with no known bits) so that the compiler adds them to the base +// once and keeps the constant parts in the instructions' offset fields +static uint rr_lo; // lp * 8 + 4 hh: byte of a fragment-ordered 8-byte slot +static uint rr_xo; // 2 lp + hh: dword of an LDS slot row +static uint rr_t4; // 8 hf + 4 hh: byte within a 16-channel group of a token / an output tile row +static uint rr_uo; // fusion: dword offset in the up-projection buffer +static uint rr_f2; // 2 hf + hh +uint wfrag1(uint base, uint f) { return wg_ld32(base + rr_lo, f * 256u); } +uint2 wfrag(uint base, uint f) { const uint w = wfrag1(base, f); return uint2(w, w); } // shared by both tiles +v8f zero8() { return wg_splat(0.0f); } +v8f rr_dup4(float4 a) +{ + v8f r; + r.v[0] = a.x; r.v[1] = a.y; r.v[2] = a.z; r.v[3] = a.w; r.v[4] = a.x; r.v[5] = a.y; r.v[6] = a.z; r.v[7] = a.w; + return r; +} +v8f rr_cat4(float4 a, float4 b) +{ + v8f r; + r.v[0] = a.x; r.v[1] = a.y; r.v[2] = a.z; r.v[3] = a.w; r.v[4] = b.x; r.v[5] = b.y; r.v[6] = b.z; r.v[7] = b.w; + return r; +} +v8f bias_tile(uint p, uint tile) { return rr_dup4(wg_ld4f(p + 4u * rr_t4, 64u * tile)); } +v8f bias_tile2(uint p, uint t0, uint t1) // per tile: output tile t0 for tile 0, t1 for tile 1 +{ + return rr_cat4(wg_ld4f(p + 4u * rr_t4, 64u * t0), wg_ld4f(p + 4u * rr_t4, 64u * t1)); +} +// per tile: this lane's dword of the 8-byte group at byte o0 (tile 0) / o1 (tile 1) (o = token row + 16 k) +uint2 rr_tld(uint base, uint o0, uint o1) { return uint2(wg_ld32(base + rr_t4, o0), wg_ld32(base + rr_t4, o1)); } +uint rr_xqr(uint r) { return rr_xo + 2u * r; } // this lane's dword of wave32 LDS slot r + lp (r = the slot row base) +uint2 rr_xqs(uint r) { const uint w = rr_l32[rr_xqr(r)]; return uint2(w, w); } // a shared operand from LDS +uint2 rr_xq2(uint r0, uint r1) { return uint2(rr_l32[rr_xqr(r0)], rr_l32[rr_xqr(r1)]); } // per tile +// Y [token][C] bytes: the dword of this lane's part of channel tile ct of token t (wave32 slot (t C + 16 ct + 8 hf) >> 3) +uint rr_yd(uint t, uint ct) { return 2u * Y_OFF + t * (RC / 4u) + 4u * ct + rr_f2; } + +uint2 qv_frag(uint h, uint f) { return wfrag(pWqv + h * UNIT, f); } +uint2 wo_frag(uint h, uint f) { return wfrag(pWo + h * (UNIT / 2), f); } +uint2 mlp_frag(uint j, uint f) { return wfrag(pMlp + j * UNIT, f); } + +#ifndef RR_B32G4 // RR_B32G4 1 keeps RR_B32 at G = 4 (D4-F / D4-R, see rr_block_wide.hlsl) +#define RR_B32G4 0 +#endif +#if RR_G == 4 && !RR_B32G4 +#error "rr_block_wide_w64.hlsl: RR_G 4 needs RR_B32G4 1" +#endif +#include "rr_fast.hlsli" +#define RR_FPP +#define RR_FPA +#if !RR_B32 || !RR_VARL || !RR_WIL || RR_Z != 0 || RR_STEP != 0 || !RR_STICKY || RR_COUNT +#error "rr_block_wide_w64.hlsl: the relaxed stagee configuration only" +#endif +#if RR_KIND == 2 && RR_KC == 0 +#error "rr_block_wide_w64.hlsl: the fusion kind needs RR_KC" +#endif +#if RR_KIND == 1 +#error "rr_block_wide_w64.hlsl: kinds 0, 2, 3" +#endif + +WG_KERNEL(rr_block) +{ + WG_SETUP(); + rr_lane = WaveGetLaneIndex(); + rr_lp = rr_lane & 31u; + rr_hh = rr_lane >> 5; + rr_hf = (rr_lane >> 4) & 1u; + rr_lo = rr_fence_u(rr_lp * 8u + 4u * rr_hh); + rr_xo = rr_fence_u(rr_lp * 2u + rr_hh); + rr_t4 = rr_fence_u(8u * rr_hf + 4u * rr_hh); + rr_f2 = rr_fence_u(2u * rr_hf + rr_hh); + rr_sat = false; + rr_nan = false; + DBG_INIT + const uint w = WaveReadLaneFirst(wg_lane_id() >> 6); // 0 .. 2 G - 1 + const uint g = w >> 1; // group + const uint tw = w & 1u; + const uint wv0 = 2u * tw; // wave32 token tile of tile 0 (tile 1: wv0 + 1) + const uint n = rr_lane & 15u; +#if RR_KIND == 2 + rr_uo = rr_fence_u(RC * n + 2u * rr_hf + rr_hh); // dword of UP slot ((n HID + 16 ot + 8 hf) >> 3) - 4 ot +#endif + const uint wid = wg_block_id(); + const int wi = (int)(wid % (uint)nwx), wj = (int)(wid / (uint)nwx); + const int lx = (int)(n & 7u); + const int X = 8 * wi - sx + lx; + const bool r1 = min(Nx, Ny) >= 10; + const int Xr = rr_refl(X, Nx, r1); + int Yt[2]; + uint tok[2]; + bool real[2]; + [unroll] for (uint t0 = 0; t0 < 2u; ++t0) { + const int ly = (int)(2u * (wv0 + t0) + (n >> 3)); + Yt[t0] = 8 * wj - sy + ly; + const int Yr = rr_refl(Yt[t0], Ny, r1); + real[t0] = X >= 0 && X < Nx && Yt[t0] >= 0 && Yt[t0] < Ny; + tok[t0] = (uint)(Yr * Nx + Xr); + } + const uint tt0 = 16u * wv0 + n, tt1 = 16u * (wv0 + 1u) + n; // token of the window (LDS rows), per tile + const RrWc wc = rr_wc(wc0, wc1, wc2, wc3, wc4); + const half2 ra2 = rr_splat_lo(c_ra_m), m2 = rr_splat_hi(c_ra_m); + const half2 f2 = rr_splat_lo(c_f_g), g2 = rr_splat_hi(c_f_g); + const RrWx wx = rr_wx(wc); + const float ra2x = (float)ra2.x, m2x = (float)m2.x, f2x = (float)f2.x; + const int Mx = Nx >> 1, My = Ny >> 1; + const int cx0 = (8 * wi - sx) >> 1, cy0 = (8 * wj - sy) >> 1; + uint cto[OWN]; // owned channel tiles + [unroll] for (uint i0 = 0; i0 < OWN; ++i0) cto[i0] = min(g + G * i0, KS - 1u); + + // ---- block input x (all channels, every wave) and the residual base of the owned tiles --------------------------- + uint2 xf[KS]; + rrbv rb[OWN]; +#if RR_KIND == 2 + { // fusion half (51.5): up-projection tiles over all waves: wave w computes the tiles 2 w + 4 G t2 (tile 0) and the + // next (tile 1); a wave past the last tile repeats it (the same bytes, same values: equal weights and biases) + const int crx = cx0 + (int)(n & 3u), cry = cy0 + (int)(n >> 2); + const uint ctok = (uint)(rr_refl(cry, My, r1) * Mx + rr_refl(crx, Mx, r1)); + uint2 cf[RR_KC / 16]; + [unroll] for (uint k = 0; k < RR_KC / 16; ++k) { + const uint c = wg_ld32(pCin + rr_t4, ctok * RR_KC + 16u * k); + cf[k] = uint2(c, c); + } + RR_LOOP for (uint t2 = 0; t2 < (HID / 16 + 4u * G - 1u) / (4u * G) + RR_ZL; ++t2) { + const uint ot0 = min(2u * w + 4u * G * t2, HID / 16 - 1u), ot1 = min(ot0 + 1u, HID / 16 - 1u); + uint2 a[RR_KC / 16]; + [unroll] for (uint k2 = 0; k2 < RR_KC / 16; ++k2) + a[k2] = uint2(wfrag1(pWup, ot0 * (RR_KC / 16) + k2), wfrag1(pWup, ot1 * (RR_KC / 16) + k2)); + v8f acc = bias_tile2(pBup, ot0, ot1); + [unroll] for (uint s = 0; s < RR_KC / 32; ++s) + acc = step2l(s + 1 == RR_KC / 32, a[2 * s], cf[2 * s], a[2 * s + 1], cf[2 * s + 1], acc); + uint bad = 0u; + uint2 uq; + Q8_TRY uq = q8f(acc, bad); + Q8_ELSE uq = q8s(acc); Q8_END + rr_l32[rr_uo + 4u * ot0] = uq.x; + rr_l32[rr_uo + 4u * ot1] = uq.y; + } + GroupMemoryBarrierWithGroupSync(); + const uint par0 = wv0 * 4u + (uint)(lx >> 1), par1 = par0 + 4u; + const uint ch0 = 2u * ((uint)Yt[0] & 1u) + ((uint)X & 1u), ch1 = 2u * ((uint)Yt[1] & 1u) + ((uint)X & 1u); + // dword of UP slot ((par HID + ch RC + 16 ct + 8 hf) >> 3) held by this lane: RC par + RC / 4 ch + 4 ct + 2 hf + hh + const uint ub0 = rr_f2 + RC * par0 + (RC / 4u) * ch0, ub1 = rr_f2 + RC * par1 + (RC / 4u) * ch1; + rrbv xr[KS]; + uint bad = 0u; + [unroll] for (uint ct = 0; ct < KS; ++ct) { + const uint2 sk = rr_tld(pX, tok[0] * RC + 16u * ct, tok[1] * RC + 16u * ct); + const uint2 up = uint2(rr_l32[ub0 + 4u * ct], rr_l32[ub1 + 4u * ct]); + xr[ct] = bv_mad(bv_d8(sk), f2x, bv_d8(up)); + } + Q8_TRY [unroll] for (uint c0 = 0; c0 < KS; ++c0) xf[c0] = bv_q8f(xr[c0], bad); + Q8_ELSE [unroll] for (uint c = 0; c < KS; ++c) xf[c] = bv_q8s(xr[c]); Q8_END + [unroll] for (uint i1 = 0; i1 < OWN; ++i1) { + const uint ct = cto[i1]; + const uint2 sk = rr_tld(pX, tok[0] * RC + 16u * ct, tok[1] * RC + 16u * ct); + const uint2 up = uint2(rr_l32[ub0 + 4u * ct], rr_l32[ub1 + 4u * ct]); + rb[i1] = bv_mad(bv_d8(sk), f2x, bv_d8(up)); + } + GroupMemoryBarrierWithGroupSync(); // UP overlays the head buffers + } +#else + [unroll] for (uint ks = 0; ks < KS; ++ks) xf[ks] = rr_tld(pX, tok[0] * RC + 16u * ks, tok[1] * RC + 16u * ks); + [unroll] for (uint i1 = 0; i1 < OWN; ++i1) rb[i1] = bv_d8(rr_tld(pX, tok[0] * RC + 16u * cto[i1], tok[1] * RC + 16u * cto[i1])); +#endif + + // ---- heads: steps 1-6, rounds of G heads ------------------------------------------------------------------------- + rrbv dp[OWN]; + [unroll] for (uint i2 = 0; i2 < OWN; ++i2) dp[i2] = bv_of(bias_tile(pBo, cto[i2])); + const uint qb = QV_OFF + g * 512u; + [unroll] for (uint r = 0; r < HR; ++r) { + const uint h = r * G + g; + const uint tb0 = pTb + 4u * rr_lo + (h * 16u + wv0 * 4u) * 1024u, tb1 = tb0 + 4u * 1024u; + v8f aq0 = zero8(), aq1 = zero8(), av0 = zero8(), av1 = zero8(); +#if RR_PRE & 1 + // every operand load of the round ahead (the driver keeps loads behind the matrix calls, which are UAV atomics) + uint2 wq[4 * KS]; + [unroll] for (uint f0 = 0; f0 < 4 * KS; ++f0) wq[f0] = qv_frag(h, f0); + [unroll] for (uint s = 0; s < NS; ++s) + wil4(aq0, aq1, av0, av1, wq[2 * s], xf[2 * s], wq[2 * s + 1], xf[2 * s + 1], + wq[KS + 2 * s], xf[2 * s], wq[KS + 2 * s + 1], xf[2 * s + 1], + xf[2 * s], wq[2 * KS + 2 * s], xf[2 * s + 1], wq[2 * KS + 2 * s + 1], + xf[2 * s], wq[3 * KS + 2 * s], xf[2 * s + 1], wq[3 * KS + 2 * s + 1]); + // the score biases and the round's Wo fragments (used after the exchanges) + v8f sb[4]; + [unroll] for (uint J5 = 0; J5 < 4; ++J5) sb[J5] = rr_cat4(wg_ld4f(tb0, J5 * 1024u), wg_ld4f(tb1, J5 * 1024u)); + uint2 wo[G][2 * OWN]; + [unroll] for (uint g3 = 0; g3 < G; ++g3) { + [unroll] for (uint i3 = 0; i3 < OWN; ++i3) { + wo[g3][2 * i3] = wo_frag(r * G + g3, 2u * cto[i3]); + wo[g3][2 * i3 + 1] = wo_frag(r * G + g3, 2u * cto[i3] + 1u); + } + } +#else + [unroll] for (uint s = 0; s < NS; ++s) { + const uint2 q00 = qv_frag(h, 2u * s), q01 = qv_frag(h, 2u * s + 1u); + const uint2 q10 = qv_frag(h, KS + 2u * s), q11 = qv_frag(h, KS + 2u * s + 1u); + const uint2 v00 = qv_frag(h, 2u * KS + 2u * s), v01 = qv_frag(h, 2u * KS + 2u * s + 1u); + const uint2 v10 = qv_frag(h, 3u * KS + 2u * s), v11 = qv_frag(h, 3u * KS + 2u * s + 1u); + wil4(aq0, aq1, av0, av1, q00, xf[2 * s], q01, xf[2 * s + 1], q10, xf[2 * s], q11, xf[2 * s + 1], + xf[2 * s], v00, xf[2 * s + 1], v01, xf[2 * s], v10, xf[2 * s + 1], v11); + } +#endif + uint bad = 0u; + uint2 qf0, qf1, vf0, vf1; + Q8_TRY qf0 = q8f(aq0, bad); qf1 = q8f(aq1, bad); vf0 = q8f(av0, bad); vf1 = q8f(av1, bad); + Q8_ELSE qf0 = q8s(aq0); qf1 = q8s(aq1); vf0 = q8s(av0); vf1 = q8s(av1); Q8_END + rr_l32[rr_xqr(qb + (wv0 * 2u) * 32u)] = qf0.x; + rr_l32[rr_xqr(qb + (wv0 * 2u + 1u) * 32u)] = qf1.x; + rr_l32[rr_xqr(qb + 256u + (wv0 * 2u) * 32u)] = vf0.x; + rr_l32[rr_xqr(qb + 256u + (wv0 * 2u + 1u) * 32u)] = vf1.x; + rr_l32[rr_xqr(qb + (wv0 * 2u + 2u) * 32u)] = qf0.y; + rr_l32[rr_xqr(qb + (wv0 * 2u + 3u) * 32u)] = qf1.y; + rr_l32[rr_xqr(qb + 256u + (wv0 * 2u + 2u) * 32u)] = vf0.y; + rr_l32[rr_xqr(qb + 256u + (wv0 * 2u + 3u) * 32u)] = vf1.y; + GroupMemoryBarrierWithGroupSync(); + + // [B] scores (key tile J x the tile's queries), start value = the score bias (f32 fragment, per tile) + uint2 kk[8]; + [unroll] for (uint J0 = 0; J0 < 8; ++J0) kk[J0] = rr_xqs(qb + J0 * 32u); +#if !(RR_PRE & 1) + v8f sb[4]; + [unroll] for (uint J5 = 0; J5 < 4; ++J5) sb[J5] = rr_cat4(wg_ld4f(tb0, J5 * 1024u), wg_ld4f(tb1, J5 * 1024u)); +#endif + rrbv wt[4]; + wil4(sb[0], sb[1], sb[2], sb[3], kk[0], qf0, kk[1], qf1, kk[2], qf0, kk[3], qf1, kk[4], qf0, kk[5], qf1, kk[6], qf0, kk[7], qf1); + [unroll] for (uint J = 0; J < 4; ++J) wt[J] = bv_attn(sb[J], BVW); + // step 4: Z, r = 1 / Z, P = Q8(w r) (f32 softmax normalisation; Z as the wave32 tree, see the head comment) + v8f zt; + [unroll] for (uint i3 = 0; i3 < 8; ++i3) zt.v[i3] = (wt[0].v[i3] + wt[1].v[i3]) + (wt[2].v[i3] + wt[3].v[i3]); + const float zh0 = (zt.v[0] + zt.v[1]) + (zt.v[2] + zt.v[3]), zh1 = (zt.v[4] + zt.v[5]) + (zt.v[6] + zt.v[7]); + const float zs0 = zh0 + wg_xor(zh0, 32u), zs1 = zh1 + wg_xor(zh1, 32u); + const float rz0 = rcp(zs0 + wg_xor16(zs0)), rz1 = rcp(zs1 + wg_xor16(zs1)); + uint2 pf[4]; + [unroll] for (uint J4 = 0; J4 < 4; ++J4) { + v8f p; + [unroll] for (uint i4 = 0; i4 < 4; ++i4) { p.v[i4] = wt[J4].v[i4] * rz0; p.v[4 + i4] = wt[J4].v[4 + i4] * rz1; } + pf[J4] = rr_q8_small(p); + } + // step 5: O_h = Q8(MM(P; v_h)), keys 0-31 then 32-63 + v8f ov[2]; + { + uint2 vt[8]; + [unroll] for (uint i8 = 0; i8 < 8; ++i8) vt[i8] = rr_xqs(qb + 256u + i8 * 32u); + ov[0] = zero8(); ov[1] = zero8(); + wil2(ov[0], ov[1], vt[0], pf[0], vt[2], pf[1], vt[1], pf[0], vt[3], pf[1]); + wil2(ov[0], ov[1], vt[4], pf[2], vt[6], pf[3], vt[5], pf[2], vt[7], pf[3]); + } + bad = 0u; + uint2 of[2]; + Q8_TRY of[0] = q8f(ov[0], bad); of[1] = q8f(ov[1], bad); + Q8_ELSE of[0] = q8s(ov[0]); of[1] = q8s(ov[1]); Q8_END + rr_l32[rr_xqr(OB_OFF + (g * 4u + wv0) * 64u)] = of[0].x; + rr_l32[rr_xqr(OB_OFF + (g * 4u + wv0 + 1u) * 64u)] = of[0].y; + rr_l32[rr_xqr(OB_OFF + (g * 4u + wv0) * 64u + 32u)] = of[1].x; + rr_l32[rr_xqr(OB_OFF + (g * 4u + wv0 + 1u) * 64u + 32u)] = of[1].y; + GroupMemoryBarrierWithGroupSync(); + + // Dp steps of the round's heads, in head order, for the owned channel tiles +#if RR_PRE & 1 + uint2 ob[G][2]; + [unroll] for (uint g4 = 0; g4 < G; ++g4) { + ob[g4][0] = rr_xq2(OB_OFF + (g4 * 4u + wv0) * 64u, OB_OFF + (g4 * 4u + wv0 + 1u) * 64u); + ob[g4][1] = rr_xq2(OB_OFF + (g4 * 4u + wv0) * 64u + 32u, OB_OFF + (g4 * 4u + wv0 + 1u) * 64u + 32u); + } + [unroll] for (uint gg = 0; gg < G; ++gg) { + [unroll] for (uint i4 = 0; i4 < OWN; ++i4) + dp[i4] = bv_step(wo[gg][2 * i4], ob[gg][0], wo[gg][2 * i4 + 1], ob[gg][1], bv_un(dp[i4])); + } +#else + [unroll] for (uint gg = 0; gg < G; ++gg) { + const uint hh = r * G + gg; + const uint2 o0 = rr_xq2(OB_OFF + (gg * 4u + wv0) * 64u, OB_OFF + (gg * 4u + wv0 + 1u) * 64u); + const uint2 o1 = rr_xq2(OB_OFF + (gg * 4u + wv0) * 64u + 32u, OB_OFF + (gg * 4u + wv0 + 1u) * 64u + 32u); + uint2 wa[OWN], wb[OWN]; + [unroll] for (uint i3 = 0; i3 < OWN; ++i3) { + wa[i3] = wo_frag(hh, 2u * cto[i3]); + wb[i3] = wo_frag(hh, 2u * cto[i3] + 1u); + } + [unroll] for (uint i4 = 0; i4 < OWN; ++i4) dp[i4] = bv_step(wa[i4], o0, wb[i4], o1, bv_un(dp[i4])); + } +#endif + } + GroupMemoryBarrierWithGroupSync(); // Y overlays the head buffers + + // ---- step 7: first residual of the owned tiles; Q8(y1) of every channel to LDS ---------------------------------- + rrbv y1h[OWN]; + { + uint2 yq[OWN]; + uint bad = 0u; + [unroll] for (uint i5 = 0; i5 < OWN; ++i5) { + y1h[i5] = bv_mad(dp[i5], ra2x, rb[i5]); + } + Q8_TRY [unroll] for (uint c0 = 0; c0 < OWN; ++c0) yq[c0] = bv_q8f(y1h[c0], bad); + Q8_ELSE [unroll] for (uint c = 0; c < OWN; ++c) yq[c] = bv_q8s(y1h[c]); Q8_END + [unroll] for (uint i6 = 0; i6 < OWN; ++i6) { + rr_l32[rr_yd(tt0, cto[i6])] = yq[i6].x; + rr_l32[rr_yd(tt1, cto[i6])] = yq[i6].y; + } + } + GroupMemoryBarrierWithGroupSync(); + uint2 y1q[KS]; + [unroll] for (uint c7 = 0; c7 < KS; ++c7) y1q[c7] = uint2(rr_l32[rr_yd(tt0, c7)], rr_l32[rr_yd(tt1, c7)]); + + // ---- step 8: MLP in rounds of G chunks --------------------------------------------------------------------------- + rrbv dd[OWN]; + [unroll] for (uint i7 = 0; i7 < OWN; ++i7) dd[i7] = bv_of(bias_tile(pB2, cto[i7])); +#if RR_MLK == 2 + // two chunks per group per round, (2 r2 + k) G + g; the round's 2 G Au tiles in AU [r2 & 1][k][g], one barrier, then + // the Dd chain over the round's chunks in chunk order (the same steps and operands as RR_MLK 1) + RR_LOOP for (uint r2 = 0; r2 < MR + RR_ZL; ++r2) { + const uint abase = AU_OFF + ((r2 & 1u) * 2u * G) * 256u; // + wv * 64 + lane (per tile) + [unroll] for (uint k = 0; k < 2; ++k) { + const uint j = (2u * r2 + k) * G + g; + uint2 w1[2 * KS]; + [unroll] for (uint f1 = 0; f1 < 2 * KS; ++f1) w1[f1] = mlp_frag(j, f1); + v8f u0 = bias_tile(pB1, 2u * j), u1 = bias_tile(pB1, 2u * j + 1u); + rrbv h0, h1; + [unroll] for (uint s = 0; s < NS; ++s) { + wil2(u0, u1, w1[2 * s], y1q[2 * s], w1[2 * s + 1], y1q[2 * s + 1], w1[KS + 2 * s], y1q[2 * s], w1[KS + 2 * s + 1], y1q[2 * s + 1]); + if (s + 1 == NS) { h0 = u0; h1 = u1; } + } + const rrbv a0h = bv_act(h0, BVW); + const rrbv a1h = bv_act(h1, BVW); + uint bad = 0u; + uint2 au0, au1; + Q8_TRY au0 = bv_q8f(a0h, bad); au1 = bv_q8f(a1h, bad); + Q8_ELSE au0 = bv_q8s(a0h); au1 = bv_q8s(a1h); Q8_END + const uint ak = abase + (k * G + g) * 256u; + rr_l32[rr_xqr(ak + wv0 * 64u)] = au0.x; + rr_l32[rr_xqr(ak + (wv0 + 1u) * 64u)] = au0.y; + rr_l32[rr_xqr(ak + wv0 * 64u + 32u)] = au1.x; + rr_l32[rr_xqr(ak + (wv0 + 1u) * 64u + 32u)] = au1.y; + } + uint2 w2[G][2 * OWN]; + [unroll] for (uint g5 = 0; g5 < G; ++g5) { + [unroll] for (uint i8 = 0; i8 < OWN; ++i8) { + w2[g5][2 * i8] = mlp_frag(2u * r2 * G + g5, 2u * KS + 2u * cto[i8]); + w2[g5][2 * i8 + 1] = mlp_frag(2u * r2 * G + g5, 2u * KS + 2u * cto[i8] + 1u); + } + } + GroupMemoryBarrierWithGroupSync(); + [unroll] for (uint hh = 0; hh < 2; ++hh) { + uint2 ab2[G][2]; + [unroll] for (uint g6 = 0; g6 < G; ++g6) { + const uint ak = abase + (hh * G + g6) * 256u; + ab2[g6][0] = rr_xq2(ak + wv0 * 64u, ak + (wv0 + 1u) * 64u); + ab2[g6][1] = rr_xq2(ak + wv0 * 64u + 32u, ak + (wv0 + 1u) * 64u + 32u); + } + uint2 w3[G][2 * OWN]; + [unroll] for (uint g7 = 0; g7 < G; ++g7) { + [unroll] for (uint i8 = 0; i8 < OWN; ++i8) { + w3[g7][2 * i8] = hh ? mlp_frag((2u * r2 + 1u) * G + g7, 2u * KS + 2u * cto[i8]) : w2[g7][2 * i8]; + w3[g7][2 * i8 + 1] = hh ? mlp_frag((2u * r2 + 1u) * G + g7, 2u * KS + 2u * cto[i8] + 1u) : w2[g7][2 * i8 + 1]; + } + } + [unroll] for (uint gg2 = 0; gg2 < G; ++gg2) { + [unroll] for (uint i9 = 0; i9 < OWN; ++i9) + dd[i9] = bv_step(w3[gg2][2 * i9], ab2[gg2][0], w3[gg2][2 * i9 + 1], ab2[gg2][1], bv_un(dd[i9])); + } + } + } +#else + RR_LOOP for (uint r2 = 0; r2 < MR + RR_ZL; ++r2) { + const uint j = r2 * G + g; +#if RR_PRE & 2 + uint2 w1[2 * KS]; + [unroll] for (uint f1 = 0; f1 < 2 * KS; ++f1) w1[f1] = mlp_frag(j, f1); +#define W1A(s) w1[2 * (s)] +#define W1B(s) w1[2 * (s) + 1] +#define W1C(s) w1[KS + 2 * (s)] +#define W1D(s) w1[KS + 2 * (s) + 1] +#else +#define W1A(s) mlp_frag(j, 2u * (s)) +#define W1B(s) mlp_frag(j, 2u * (s) + 1u) +#define W1C(s) mlp_frag(j, KS + 2u * (s)) +#define W1D(s) mlp_frag(j, KS + 2u * (s) + 1u) +#endif + v8f u0 = bias_tile(pB1, 2u * j), u1 = bias_tile(pB1, 2u * j + 1u); + rrbv h0, h1; + [unroll] for (uint s = 0; s < NS; ++s) { + wil2(u0, u1, W1A(s), y1q[2 * s], W1B(s), y1q[2 * s + 1], W1C(s), y1q[2 * s], W1D(s), y1q[2 * s + 1]); + if (s + 1 == NS) { h0 = u0; h1 = u1; } + } +#undef W1A +#undef W1B +#undef W1C +#undef W1D +#if RR_PRE & 2 + uint2 w2[G][2 * OWN]; + [unroll] for (uint g5 = 0; g5 < G; ++g5) { + [unroll] for (uint i8 = 0; i8 < OWN; ++i8) { + w2[g5][2 * i8] = mlp_frag(r2 * G + g5, 2u * KS + 2u * cto[i8]); + w2[g5][2 * i8 + 1] = mlp_frag(r2 * G + g5, 2u * KS + 2u * cto[i8] + 1u); + } + } +#endif + const rrbv a0h = bv_act(h0, BVW); + const rrbv a1h = bv_act(h1, BVW); + uint bad = 0u; + uint2 au0, au1; + Q8_TRY au0 = bv_q8f(a0h, bad); au1 = bv_q8f(a1h, bad); + Q8_ELSE au0 = bv_q8s(a0h); au1 = bv_q8s(a1h); Q8_END + const uint ab = AU_OFF + ((r2 & 1u) * G) * 256u; // + wv * 64 + lane (per tile) + rr_l32[rr_xqr(ab + g * 256u + wv0 * 64u)] = au0.x; + rr_l32[rr_xqr(ab + g * 256u + (wv0 + 1u) * 64u)] = au0.y; + rr_l32[rr_xqr(ab + g * 256u + wv0 * 64u + 32u)] = au1.x; + rr_l32[rr_xqr(ab + g * 256u + (wv0 + 1u) * 64u + 32u)] = au1.y; + GroupMemoryBarrierWithGroupSync(); +#if RR_PRE & 2 + uint2 ab2[G][2]; + [unroll] for (uint g6 = 0; g6 < G; ++g6) { + ab2[g6][0] = rr_xq2(ab + g6 * 256u + wv0 * 64u, ab + g6 * 256u + (wv0 + 1u) * 64u); + ab2[g6][1] = rr_xq2(ab + g6 * 256u + wv0 * 64u + 32u, ab + g6 * 256u + (wv0 + 1u) * 64u + 32u); + } + [unroll] for (uint gg2 = 0; gg2 < G; ++gg2) { + [unroll] for (uint i9 = 0; i9 < OWN; ++i9) + dd[i9] = bv_step(w2[gg2][2 * i9], ab2[gg2][0], w2[gg2][2 * i9 + 1], ab2[gg2][1], bv_un(dd[i9])); + } +#else + [unroll] for (uint gg2 = 0; gg2 < G; ++gg2) { + const uint jj = r2 * G + gg2; + const uint2 b0 = rr_xq2(ab + gg2 * 256u + wv0 * 64u, ab + gg2 * 256u + (wv0 + 1u) * 64u); + const uint2 b1 = rr_xq2(ab + gg2 * 256u + wv0 * 64u + 32u, ab + gg2 * 256u + (wv0 + 1u) * 64u + 32u); + uint2 wa[OWN], wb[OWN]; + [unroll] for (uint i8 = 0; i8 < OWN; ++i8) { + wa[i8] = mlp_frag(jj, 2u * KS + 2u * cto[i8]); + wb[i8] = mlp_frag(jj, 2u * KS + 2u * cto[i8] + 1u); + } + [unroll] for (uint i9 = 0; i9 < OWN; ++i9) dd[i9] = bv_step(wa[i9], b0, wb[i9], b1, bv_un(dd[i9])); + } +#endif + } +#endif + + // ---- step 9: second residual, y2 of the owned tiles (memory, LDS) ------------------------------------------------ + { + rrbv yh[OWN]; + uint2 y2o[OWN]; + uint bad = 0u; + [unroll] for (uint c3 = 0; c3 < OWN; ++c3) { + yh[c3] = bv_mad(dd[c3], m2x, y1h[c3]); + } + Q8_TRY [unroll] for (uint c0 = 0; c0 < OWN; ++c0) y2o[c0] = bv_q8f(yh[c0], bad); + Q8_ELSE [unroll] for (uint c = 0; c < OWN; ++c) y2o[c] = bv_q8s(yh[c]); Q8_END + const uint otok0 = (uint)(Yt[0] * Nx + X), otok1 = (uint)(Yt[1] * Nx + X); + [unroll] for (uint c4 = 0; c4 < OWN; ++c4) { + if (real[0]) wg_st32(pY2 + rr_t4, otok0 * RC + 16u * cto[c4], y2o[c4].x); + if (real[1]) wg_st32(pY2 + rr_t4, otok1 * RC + 16u * cto[c4], y2o[c4].y); + rr_l32[rr_yd(tt0, cto[c4])] = y2o[c4].x; + rr_l32[rr_yd(tt1, cto[c4])] = y2o[c4].y; + } + } + GroupMemoryBarrierWithGroupSync(); + + // ---- merge (51.4, E2 / E3): output tiles over all waves, two per wave --------------------------------------------- +#if RR_CN > 0 + { + const uint rx = n & 3u, ry = n >> 2; + const int Xc = cx0 + (int)rx, Yc = cy0 + (int)ry; + const bool cin = Xc >= 0 && Xc < Mx && Yc >= 0 && Yc < My; + // the merge operand row of zi = z0 + 8 hf + 4 hh (z0 a multiple of 16): LDS dword = lane base + 2 mrg_k(z0) + const uint mlb = 2u * Y_OFF + (16u * ry + 2u * rx) * (RC / 4u) + rr_f2; + RR_LOOP for (uint ot0 = 2u * w; ot0 < RR_CN / 16 + RR_ZL; ot0 += 4u * G) { + const uint ot1 = ot0 + 1u; +#if RR_PRE & 4 + // operands in batches of 8 fragments (4 steps), the next batch loaded before the current one's steps + uint2 am[2][8], bm[2][8]; + [unroll] for (uint f2i = 0; f2i < 8; ++f2i) + am[0][f2i] = uint2(wfrag1(pWm, ot0 * (HID / 16) + f2i), wfrag1(pWm, ot1 * (HID / 16) + f2i)); + v8f acc = bias_tile2(pBm, ot0, ot1); + [unroll] for (uint bt = 0; bt < HID / 128; ++bt) { + const uint cb = bt & 1u; + [unroll] for (uint f3 = 0; f3 < 8; ++f3) { + const uint wm = rr_l32[mlb + 2u * mrg_k(16u * (8u * bt + f3))]; + bm[cb][f3] = uint2(wm, wm); + } + if (bt + 1 < HID / 128) { + [unroll] for (uint f4 = 0; f4 < 8; ++f4) + am[cb ^ 1u][f4] = uint2(wfrag1(pWm, ot0 * (HID / 16) + 8u * (bt + 1u) + f4), + wfrag1(pWm, ot1 * (HID / 16) + 8u * (bt + 1u) + f4)); + } + [unroll] for (uint s = 0; s < 4; ++s) + acc = step2l(and(bt + 1 == HID / 128, s == 3u), am[cb][2 * s], bm[cb][2 * s], am[cb][2 * s + 1], bm[cb][2 * s + 1], acc); + } +#else + v8f acc = bias_tile2(pBm, ot0, ot1); + RR_LOOP for (uint s = 0; s < HID / 32 + RR_ZL; ++s) { + uint2 b[2], a[2]; + [unroll] for (uint e = 0; e < 2; ++e) { + const uint wm = rr_l32[mlb + 2u * mrg_k(32u * s + 16u * e)]; + b[e] = uint2(wm, wm); + a[e] = uint2(wfrag1(pWm, ot0 * (HID / 16) + 2u * s + e), wfrag1(pWm, ot1 * (HID / 16) + 2u * s + e)); + } + acc = step2l(s + 1 == HID / 32 + RR_ZL, a[0], b[0], a[1], b[1], acc); + } +#endif + uint bad = 0u; + uint2 mq; + Q8_TRY mq = q8f(acc, bad); + Q8_ELSE mq = q8s(acc); Q8_END +#if RR_Q8_FAST + // Convergent wave barrier on mq before branching on cin: prevents LLVM from hoisting + // `if (cin)` above the merge WMMAs (RDNA 4 v_wmma under narrow EXEC produces non-finite lanes). + const bool mq_ok = !WaveActiveAnyTrue(((mq.x | mq.y) & rr_zero()) != 0u); + if (and(mq_ok, cin)) { +#else + if (cin && rr_zero() == 0u) { +#endif + wg_st32(pMerged + rr_t4, (uint)(Yc * Mx + Xc) * RR_CN + 16u * ot0, mq.x); + wg_st32(pMerged + rr_t4, (uint)(Yc * Mx + Xc) * RR_CN + 16u * ot1, mq.y); + } + } + } +#endif + + // ---- hidden state (51.4): wave (g, tw) with g == tw covers all four token tiles (tile 0 / tile 1 of tw = 0 and 1); every + // group computed the same values in the wave32 kernel, so the others' copies are redundant (the guard is wave-uniform, + // holds no barrier). Rows 0-3 of the Wh tile (the 4 hidden channels) are in the hf = 0, hh = 0 lanes: dwords x, y for tile 0, + // z, w for tile 1; tile 1's are moved to the hh = 1 lanes (lane ^ 32), so lanes 0-15 run tile 0's token n and lanes 32-47 + // tile 1's, with the operations of the wave32 lanes 0-15 (the other lanes compute unused values, as the wave32 hf = 1 + // lanes do). +#if HIDDEN + if (g == tw) { + uint2 y2[KS]; + [unroll] for (uint c8 = 0; c8 < KS; ++c8) y2[c8] = uint2(rr_l32[rr_yd(tt0, c8)], rr_l32[rr_yd(tt1, c8)]); + uint2 wh[KS]; +#if RR_PRE & 8 + [unroll] for (uint f4 = 0; f4 < KS; ++f4) wh[f4] = wfrag(pWh, f4); +#endif + v8f dh = rr_dup4(wg_ld4f(pBh + 4u * rr_t4, 0u)); + uint4 dhb; + [unroll] for (uint s = 0; s < NS; ++s) { +#if !(RR_PRE & 8) + wh[2 * s] = wfrag(pWh, 2u * s); + wh[2 * s + 1] = wfrag(pWh, 2u * s + 1u); +#endif + if (s + 1 < NS) dh = step2(wh[2 * s], y2[2 * s], wh[2 * s + 1], y2[2 * s + 1], dh); + else dhb = step2h(wh[2 * s], y2[2 * s], wh[2 * s + 1], y2[2 * s + 1], dh); + } + const uint z1 = wg_xor_u(dhb.z, 32u), w1 = wg_xor_u(dhb.w, 32u); + const bool t1 = rr_hh != 0u; + const uint dx = t1 ? z1 : dhb.x, dy = t1 ? w1 : dhb.y; + const uint tk = t1 ? tok[1] : tok[0]; + const int Yh = t1 ? Yt[1] : Yt[0]; + const bool rl = t1 ? real[1] : real[0]; + const uint s01 = wg_ld16(pSig, (dx & 0xffffu) * 2u) | (wg_ld16(pSig, (dx >> 16) * 2u) << 16); + const uint s23 = wg_ld16(pSig, (dy & 0xffffu) * 2u) | (wg_ld16(pSig, (dy >> 16) * 2u) << 16); + const uint2 hold = wg_ld64(pHold, tk * 8u); + uint2 hn; +#if RR_KIND == 0 + const uint G01 = rr_fence_u(rr_b2(rr_h2(s01) * g2)), G23 = rr_fence_u(rr_b2(rr_h2(s23) * g2)); + hn.x = rr_fence_u(rr_b2(rr_h2(hold.x) * rr_h2(G01))); + hn.y = rr_fence_u(rr_b2(rr_h2(hold.y) * rr_h2(G23))) & 0xffffu; +#else + const half2 z2 = rr_splat_hi(s23); + const uint omzb = rr_fence_u(rr_b2(half2(1.0, 1.0) - z2)); + const half2 omz = rr_h2(omzb); + const int xm = (X - 1) >> 1, ym = (Yh - 1) >> 1; + const int rxm = rr_refl(xm, Mx, r1), rxp = rr_refl(xm + 1, Mx, r1); + const int rym = rr_refl(ym, My, r1), ryp = rr_refl(ym + 1, My, r1); + const half2 wxp = (X & 1) ? half2(0.25, 0.25) : half2(0.75, 0.75); + const half2 wyp = (Yh & 1) ? half2(0.25, 0.25) : half2(0.75, 0.75); + const half2 wxm = (X & 1) ? half2(0.75, 0.75) : half2(0.25, 0.25); + const half2 wym = (Yh & 1) ? half2(0.75, 0.75) : half2(0.25, 0.25); + const uint2 cA = wg_ld64(pHc, (uint)(rym * Mx + rxp) * 8u), cB = wg_ld64(pHc, (uint)(rym * Mx + rxm) * 8u); + const uint2 cC = wg_ld64(pHc, (uint)(ryp * Mx + rxm) * 8u), cD = wg_ld64(pHc, (uint)(ryp * Mx + rxp) * 8u); + uint2 u; + [unroll] for (uint e = 0; e < 2; ++e) { + const uint a = e ? cA.y : cA.x, b = e ? cB.y : cB.x, c = e ? cC.y : cC.x, d = e ? cD.y : cD.x; + half2 tt = rr_h2(rr_fence_u(rr_b2(rr_h2(rr_fence_u(rr_b2(rr_h2(a) * wxp))) * wym))); + tt = rr_h2(rr_fence_u(rr_b2(mad(rr_h2(rr_fence_u(rr_b2(rr_h2(b) * wxm))), wym, tt)))); + tt = rr_h2(rr_fence_u(rr_b2(mad(rr_h2(rr_fence_u(rr_b2(rr_h2(c) * wxm))), wyp, tt)))); + tt = rr_h2(rr_fence_u(rr_b2(mad(rr_h2(rr_fence_u(rr_b2(rr_h2(d) * wxp))), wyp, tt)))); + if (e == 0) u.x = rr_b2(tt); else u.y = rr_b2(tt); + } + const uint gt01 = rr_fence_u(rr_b2(rr_h2(s01) * g2)), gt23 = rr_fence_u(rr_b2(rr_h2(s23) * g2)); + const uint ug01 = rr_fence_u(rr_b2(rr_h2(u.x) * rr_h2(gt01))), ug23 = rr_fence_u(rr_b2(rr_h2(u.y) * rr_h2(gt23))); + const uint hh01 = rr_fence_u(rr_b2(rr_h2(ug01) * z2)); + const uint hh23 = rr_fence_u(rr_b2(rr_h2(ug23) * z2)) & 0xffffu; + hn.x = rr_fence_u(rr_b2(mad(rr_h2(hold.x), omz, rr_h2(hh01)))); + hn.y = rr_fence_u(rr_b2(mad(rr_h2(hold.y), omz, rr_h2(hh23)))); +#endif + const uint otok = (uint)(Yh * Nx + X); + if (rl && rr_hf == 0u) wg_st64(pHnew, otok * 8u, hn); +#if RR_HMERGE + const half2 quarter = half2(0.25, 0.25); + const uint qx = rr_fence_u(rr_b2(rr_h2(hn.x) * quarter)), qy = rr_fence_u(rr_b2(rr_h2(hn.y) * quarter)); + const uint l0 = 2u * (n & 3u) + 32u * rr_hh; + const uint q0x = WaveReadLaneAt(qx, l0), q1x = WaveReadLaneAt(qx, l0 + 1u); + const uint q2x = WaveReadLaneAt(qx, l0 + 8u), q3x = WaveReadLaneAt(qx, l0 + 9u); + const uint q0y = WaveReadLaneAt(qy, l0), q1y = WaveReadLaneAt(qy, l0 + 1u); + const uint q2y = WaveReadLaneAt(qy, l0 + 8u), q3y = WaveReadLaneAt(qy, l0 + 9u); + const uint ax = rr_fence_u(rr_b2(rr_h2(q0x) + rr_h2(q1x))), bx = rr_fence_u(rr_b2(rr_h2(q2x) + rr_h2(q3x))); + const uint ay = rr_fence_u(rr_b2(rr_h2(q0y) + rr_h2(q1y))), by = rr_fence_u(rr_b2(rr_h2(q2y) + rr_h2(q3y))); + const uint2 hm = uint2(rr_b2(rr_h2(ax) + rr_h2(bx)), rr_b2(rr_h2(ay) + rr_h2(by))); + const int hXc = cx0 + (int)(n & 3u), hYc = cy0 + (int)(wv0 + rr_hh); + if (n < 4u && rr_hf == 0u && hXc >= 0 && hXc < Mx && hYc >= 0 && hYc < My) + wg_st64(pHmerged, (uint)(hYc * Mx + hXc) * 8u, hm); +#endif + } +#endif + DBG_FLUSH +} diff --git a/clean/rr_f/kernels/rr_fast.hlsli b/clean/rr_f/kernels/rr_fast.hlsli index bf74d79b..0ad709b2 100644 --- a/clean/rr_f/kernels/rr_fast.hlsli +++ b/clean/rr_f/kernels/rr_fast.hlsli @@ -10,6 +10,13 @@ #ifndef RR_SMAJOR #define RR_SMAJOR 1 #endif +// clean/rr-f-wave5 RR_Q8_FAST 1 (opt-in, NOT exact for values beyond +-448 / NaN; fast mode only): branchless Q8. Every group is +// the clamp to +-448 (v_max / v_min; a NaN gives one of the bounds) and the driver conversion, with no bad codes collected +// and no slow path: Q8_TRY / Q8_ELSE / Q8_END expand to `{ fast } if (false) { slow }`, so rr_sat, rr_nan, q8s, q8h_slow and +// the wave votes leave the IR (and their live ranges the registers). 0 (default): the text of every kernel before wave 5. +#ifndef RR_Q8_FAST +#define RR_Q8_FAST 0 +#endif // ---- Q8 fast path ---------------------------------------------------------------------------------------------------- // The unclamped driver conversion equals Q8 (rr_q8) for every finite |x| <= 464 (values in (448, 464] round to @@ -21,6 +28,11 @@ #endif uint2 q8f(v8f d, inout uint bad) { +#if RR_Q8_FAST + v8f c; + [unroll] for (uint i = 0; i < 8; ++i) c.v[i] = min(max(d.v[i], -448.0f), 448.0f); + return wg_pack_q8_nc_ags(c); +#else #if defined(RR_L01BIT) && (L01_Q8CLAMP & RR_L01BIT) != 0 // A46 q8-clamp-nocheck: clamp to +-448 (v_maxmin; a NaN gives one of the bounds, not 0x7f / 0xff) then the driver // conversion; no bad codes collected, so the group's slow path is never taken. NOT exact for NaN inputs. @@ -31,6 +43,7 @@ uint2 q8f(v8f d, inout uint bad) const uint2 q = wg_pack_q8_nc_ags(d); bad |= ((q.x & 0x7f7f7f7fu) + 0x01010101u) | ((q.y & 0x7f7f7f7fu) + 0x01010101u); return q; +#endif } #ifndef RR_COUNT #define RR_COUNT 0 // 1: counting build (isa_stats --dyn): fast paths only, loops unrolled; never run @@ -52,6 +65,21 @@ bool q8_slow(uint bad) { return WaveActiveAnyTrue((bad & 0x80808080u) != 0u); } #if WG_WAVE == 64 // clean/rr-mp2-level0 L0a (rr_block_c32.hlsl design D): one conversion per tile (h.xy = tile 0, h.zw = tile 1; the // wave64 f16 fragment is 2 dwords), the same per-value conversion and byte order +#if RR_Q8_FAST +// RR_Q8_FAST: the conversion of packed f16 values known finite and within +-448 (no clamp); P and the clamped groups below +uint2 q8hf_nc(uint4 h) +{ + const wg_frag r0 = wg_ags_convert(rr_w64f(h.x, h.y, 0u, 0u), WG_AGS_T_F16_ACC, WG_AGS_T_FP8_B); + const wg_frag r1 = wg_ags_convert(rr_w64f(h.z, h.w, 0u, 0u), WG_AGS_T_F16_ACC, WG_AGS_T_FP8_B); + return uint2(r0.v[0], r1.v[0]); +} +uint2 q8hf(uint4 h, inout uint bad) +{ + const half2 lo = half2(-448.0, -448.0), hi = half2(448.0, 448.0); // packed v_pk_max_f16 / v_pk_min_f16: a NaN gives a bound + return q8hf_nc(uint4(rr_b2(min(max(rr_h2(h.x), lo), hi)), rr_b2(min(max(rr_h2(h.y), lo), hi)), + rr_b2(min(max(rr_h2(h.z), lo), hi)), rr_b2(min(max(rr_h2(h.w), lo), hi)))); +} +#else uint2 q8hf(uint4 h, inout uint bad) { const wg_frag r0 = wg_ags_convert(rr_w64f(h.x, h.y, 0u, 0u), WG_AGS_T_F16_ACC, WG_AGS_T_FP8_B); @@ -60,6 +88,23 @@ uint2 q8hf(uint4 h, inout uint bad) bad |= ((q.x & 0x7f7f7f7fu) + 0x01010101u) | ((q.y & 0x7f7f7f7fu) + 0x01010101u); return q; } +#endif +#else +#if RR_Q8_FAST +uint2 q8hf_nc(uint4 h) +{ + wg_frag f; + f.v[0] = h.x; f.v[1] = h.y; f.v[2] = h.z; f.v[3] = h.w; + [unroll] for (uint i = 4; i < 8; ++i) f.v[i] = 0u; + const wg_frag r = wg_ags_convert(f, WG_AGS_T_F16_ACC, WG_AGS_T_FP8_B); + return uint2(r.v[0], r.v[1]); +} +uint2 q8hf(uint4 h, inout uint bad) +{ + const half2 lo = half2(-448.0, -448.0), hi = half2(448.0, 448.0); + return q8hf_nc(uint4(rr_b2(min(max(rr_h2(h.x), lo), hi)), rr_b2(min(max(rr_h2(h.y), lo), hi)), + rr_b2(min(max(rr_h2(h.z), lo), hi)), rr_b2(min(max(rr_h2(h.w), lo), hi)))); +} #else uint2 q8hf(uint4 h, inout uint bad) { @@ -72,6 +117,7 @@ uint2 q8hf(uint4 h, inout uint bad) return q; } #endif +#endif // Q8 groups (several Q8 calls sharing one wave-uniform slow-path decision): // uint bad = 0u; Q8_TRY Q8_ELSE Q8_END // Both forms give rr_q8's bits for every input, so which one runs is a speed choice only. RR_STICKY 1 (default): once a @@ -92,7 +138,12 @@ static uint rr_cnt0, rr_cnt1, rr_cnt2; #define DBG_INIT #define DBG_FLUSH #endif -#if RR_STICKY +#if RR_Q8_FAST +// every group is the clamped fast form: no per-group vote, no sticky slow path (the slow form is dead code) +#define Q8_TRY { +#define Q8_ELSE } if (false) { +#define Q8_END } +#elif RR_STICKY #define Q8_TRY [branch] if (!rr_sat) { #define Q8_ELSE rr_sat = q8_slow(bad); if (!rr_sat) { DBG_C(0) } } [branch] if (rr_sat) { DBG_C(1) #define Q8_END } @@ -142,8 +193,12 @@ uint2 q8s(v8f d) uint2 q8h_slow(uint4 h) { return q8s(rr_unh(fence4(h))); } uint2 q8h_small(uint4 h) // values known finite and within +-448 (P) { +#if RR_Q8_FAST + return q8hf_nc(h); +#else uint bad = 0u; return q8hf(h, bad); +#endif } // 51.3 step 3 (rr_attn_w2) with the same operations; the bit step as a packed 16-bit shift and one 32-bit mask: diff --git a/clean/rr_f/kernels/rr_input.hlsl b/clean/rr_f/kernels/rr_input.hlsl index 6caf232f..05451b54 100644 --- a/clean/rr_f/kernels/rr_input.hlsl +++ b/clean/rr_f/kernels/rr_input.hlsl @@ -362,6 +362,9 @@ Texture2D rr_in_srv[16] : register(t0, space1); #ifndef RR_IN_SRVC #define RR_IN_SRVC 0 #endif +#ifndef RR_IN_SRV_ONLY +#define RR_IN_SRV_ONLY 0 // clean/rr-f-wave5c (Fast mode only, VG_OUT): CPU-guarded all attached roles via SRV +#endif struct Tex { uint off, w, h, ch; int ox, oy; uint srv, r; }; Tex tex(uint r) { @@ -379,6 +382,9 @@ Tex tex(uint r) } float tx(Tex t, int x, int y, uint c) { +#if RR_IN_SRV && RR_IN_SRV_ONLY + return in_ftz(rr_in_srv[t.r].Load(int3(x + t.ox, y + t.oy, 0))[c]); +#else #if RR_IN_SRV // clean/release-060 crash-cp2077: always the compile-time role's descriptor (every caller passes a constant role, and // the runtime's word 7 is role + 1 whenever nonzero: the same descriptor). The index read from word 7 (the old @@ -389,6 +395,7 @@ float tx(Tex t, int x, int y, uint c) #endif const uint idx = ((uint)(y + t.oy) * t.w + (uint)(x + t.ox)) * t.ch + c; return in_ftz(asfloat(wg_ld32(t.off, idx * 4u))); +#endif } // ---- per-frame values (uniform) -------------------------------------------------------------------------------- diff --git a/clean/rr_f/kernels/rr_output_fast.hlsli b/clean/rr_f/kernels/rr_output_fast.hlsli index 439ebc1c..515b2849 100644 --- a/clean/rr_f/kernels/rr_output_fast.hlsli +++ b/clean/rr_f/kernels/rr_output_fast.hlsli @@ -49,14 +49,26 @@ #ifndef OFX_QST #define OFX_QST 0 // D17: Q texels of two neighbour lanes as one 12-byte store (even history width) #endif +#ifndef OFX_W5_ALU +#define OFX_W5_ALU 0 // clean/rr-f-wave5b (Fast mode only, VG_OUT): bounded ALU fast path (no fences, no AGS 0x1c, 3-way max for OF_REPROJ_MAX=1, no rare/fallback) +#endif +#ifndef OFX_W5C +#define OFX_W5C 0 // clean/rr-f-wave5c (Fast mode only, VG_OUT): live-range reduction (<= 64 VGPR target) in of_reproj + 16-tap loop +#endif #define OF_TX 32 #define OF_TY 8 #define OF_LW 30 #define OF_LH 12 groupshared uint4 of_ra[OF_LW * OF_LH]; groupshared uint of_rb[OF_LW * OF_LH]; +// clean/rr-f-wave4c W4C.3: the PH1 tile copy (of_lin, of_rowrare: 3,104 bytes) is only read by the groupshared PH1 below; with +// OFX_PH1W on wave64 it is declared nowhere, so the kernel's LDS is of_ra + of_rb alone (7,200 bytes) +#if !(OFX_PH1W && WG_WAVE == 64) groupshared float3 of_lin[OF_TX * OF_TY]; +#if !OFX_W5_ALU groupshared uint of_rowrare[OF_TY]; +#endif +#endif // scratch (bytes from the scratch argument): word 0 the fallback count (cleared by prep), word 1 the prep flags (bit 0: // every quad falls back, bit 1: the TR table has nonzero entries below 2^-26; cleared by the fallback kernel), words @@ -72,6 +84,18 @@ uint of_zb() { return 0u - (wg_grid_ >> 31); } #if WG_WAVE == 64 #include "wg_ags.hlsli" #endif +#if OFX_W5_ALU +uint of_cvt(float x) { return (uint)asuint16((half)x); } +half of_hh(uint b) { return asfloat16((uint16_t)(b & 0xffffu)); } +uint4 of_to_f16(v8f d) +{ + return uint4(of_cvt(d.v[0]) | (of_cvt(d.v[1]) << 16), of_cvt(d.v[2]) | (of_cvt(d.v[3]) << 16), + of_cvt(d.v[4]) | (of_cvt(d.v[5]) << 16), of_cvt(d.v[6]) | (of_cvt(d.v[7]) << 16)); +} +half of_fh(half x) { return x; } +half2 of_fh2(half2 x) { return x; } +float of_f(half x) { return (float)x; } +#else uint of_cvt(float x) { return wg_ags_call(0x1cu, 0u, 1u, asuint(x), 0u); } // v_cvt_f16_f32 (RNE) = H's bits // OF_FAST_WAVE=64 (exact, same bits; host define, gpu/output.py): rr_output_fast as wave64. The VALU-bound kernel // then issues its FP32 operations two halves per wave instruction; the driver's matrix conversion (wave32 only) is @@ -90,6 +114,7 @@ uint4 of_to_f16(v8f d) { return wg_to_f16_ags(d); } half of_fh(half x) { return asfloat16((uint16_t)(asuint16(x) ^ (uint16_t)of_zb())); } half2 of_fh2(half2 x) { return wg_as_h2(wg_h2_bits(x) ^ of_zb()); } // keeps the last fma f16 (else v_fma_mix_f32) float of_f(half x) { return f16tof32((uint)asuint16(x)); } +#endif // wg_div((float)b, 255) for every byte b (clean/rr-f-r6-tail): q = b * RN(1/255), one fma correction step; checked with // exact rationals for all 256 inputs (results 0 or >= 1/255: no denormal, no special operand), and on the GPU. float of_d255(uint b) @@ -108,6 +133,13 @@ float of_d255(uint b) #endif #define OF_NO_FALLBACK 0 // negative control only: never list a quad #endif +#if OFX_W5_ALU +float fence2(float x) { return x; } +float Mn(float a, float b) { return a * b; } +float Mf(float a, float b) { return a * b; } +float An(float a, float b) { return a + b; } +float Fn(float a, float b, float c) { return mad(a, b, c); } +#else // A second opaque zero: the driver splits a fused mad(a, b, c) into a * b + c when it can share the product a * b with // another use (seen in of_blend), so products of the same operands get distinct fences (fence / fence2). float fence2(float x) { return asfloat(asuint(x) ^ (0u - (wg_group_base_ >> 31))); } @@ -122,6 +154,7 @@ float Mf(float a, float b) { return fence(a * b); } #endif float An(float a, float b) { return a + b; } float Fn(float a, float b, float c) { return mad(a, b, c); } +#endif // A zero result is checked when the exact result, scaled by 2^64 (exact for every operand set whose exact result is // below 2^-126), lies in [2^-62 (1 - 2^-20), 2^-62]: a superset of the band; exact cancellations and products far below // it (which M / F also flush) are not listed. @@ -134,7 +167,10 @@ bool of_win(float s2) { return and(abs(s2) >= asfloat(0x207ffff0u), abs(s2) <= a #ifndef OFX_NOCHK #define OFX_NOCHK 0 // D31, FAST MODE ONLY (not exact): no band check (results in the band flush like the hardware) #endif -#if OFX_NOCHK +#if OFX_W5_ALU +float Mq(float a, float b, inout bool rare) { return a * b; } +float Fq(float a, float b, float c, inout bool rare) { return mad(a, b, c); } +#elif OFX_NOCHK float Mq(float a, float b, inout bool rare) { return Mf(a, b); } float Fq(float a, float b, float c, inout bool rare) { return fence(mad(a, b, c)); } #elif OFX_MQB @@ -229,6 +265,12 @@ bool of_insane(float E, float Ep, float invE) return of_small(K12, lo) || of_small(invE, lo) || of_small(Ep, lo) || abs(Ep) > asfloat(0x4e800000u) || of_small(E, lo) || of_small(K7, lo) || of_small(K11, asfloat(0x21800000u)) || K13 > asfloat(0x4e800000u); } +#if OFX_W5_ALU +float Cf(float x) { return log2(max(mad(x, K4, 1.0f), K5)) * (asfloat(0x3e9a209bu) * K7); } +float of_dt(float x) { return min(max(x, K5), K10); } +float of_dtail(float r) { return exp2(r) - 1.0f; } +float Df(float x, inout bool rare) { return exp2(min(max(x, K5), K10) * (K8 * asfloat(0x40549a78u))) - 1.0f; } +#else // C: F(x, K4, 1) cannot be a nonzero value below 2^-126 (x K4 + 1 is 0, or >= 2^-48, or near 1); |log2| >= 2^-30, so // with |K7| >= 2^-30 both products stay >= 2^-62 (or 0). float Cf(float x) { return Mf(Mf(log2(max(Fn(x, K4, 1), K5)), asfloat(0x3e9a209bu)), K7); } @@ -240,24 +282,56 @@ float Df(float x, inout bool rare) // M(M(t, K8), K9): K9 > 1 cannot make a normal (or zero) first product small return An(exp2(Mn(Mq(t, K8, rare), asfloat(0x40549a78u))), -1); } +#endif // sample4 of rr_output.hlsl, one axis. p (a position) is 0 or >= 2^-25 in magnitude (a half-integer plus a value) and // R >= 2^-16, so s = M(p, R) and M(s, n) need no check; a is then a multiple of 2^-25 (or large), so f and 1 - f are // 0 or multiples of 2^-25 and the bilinear weights multiples of 2^-50: their products with binary16 values (multiples // of 2^-24) never reach the band. The same holds for the candidate taps (s = M(u, 1 / wr), u a multiple of 2^-41). -struct OfAxis { float f, g; int c0, c1; uint o0, o1; }; // o0 / o1: byte offsets c0 / c1 * stride (+ base) // clean/rr-f-s10-tail: 24-bit products (v_mul_u32_u24 / v_mad_u32_u24, full rate, instead of the quarter-rate // v_mul_lo_u32) for index arithmetic whose operands are below 2^24 (coordinates and pitches of images whose sides are // below 2^21): the low 32 bits of the product are the same, so every address is the same. +#if OFX_W5_ALU && OFX_SPEC +uint of_m24(uint a, uint b) { return (a & 0xffffffu) * (b & 0xffffffu); } +struct OfAxis { float f; uint o0, o1; }; +OfAxis of_axis_s(float s, uint n, uint stride = 0u, uint base = 0u, bool m24 = false) +{ + OfAxis r; + s = isnan(s) ? 0 : s; + const float a = mad(s, (float)n, -0.5f), i = floor(a); + r.f = a - i; + const uint c0 = (uint)clamp(i, 0.0f, (float)(n - 1)), c1 = (uint)clamp(i + 1.0f, 0.0f, (float)(n - 1)); + r.o0 = (m24 ? of_m24(c0, stride) : c0 * stride) + base; + r.o1 = (m24 ? of_m24(c1, stride) : c1 * stride) + base; + return r; +} +OfAxis of_axis(float p, float R, uint n, uint stride, uint base, bool m24 = false) { return of_axis_s(p * R, n, stride, base, m24); } +struct OfTaps { uint ox0, ox1, oy0, oy1; float wa[4]; }; +OfTaps of_taps(OfAxis ax, OfAxis ay) +{ + OfTaps r; + const float ag = 1.0f - ax.f, bg = 1.0f - ay.f; + r.ox0 = ax.o0; r.ox1 = ax.o1; r.oy0 = ay.o0; r.oy1 = ay.o1; + r.wa[0] = ag * bg; r.wa[1] = ax.f * bg; r.wa[2] = ag * ay.f; r.wa[3] = ax.f * ay.f; + return r; +} +#else +struct OfAxis { float f, g; int c0, c1; uint o0, o1; }; // o0 / o1: byte offsets c0 / c1 * stride (+ base) uint of_m24(uint a, uint b) { return (a & 0xffffffu) * (b & 0xffffffu); } OfAxis of_axis_s(float s, uint n, uint stride = 0u, uint base = 0u, bool m24 = false) { OfAxis r; s = isnan(s) ? 0 : s; +#if OFX_W5_ALU + const float a = mad(s, (float)n, -0.5f), i = floor(a); + r.f = a - i; r.g = 1.0f - r.f; + r.c0 = (int)clamp(i, 0.0f, (float)(n - 1)); r.c1 = (int)clamp(i + 1.0f, 0.0f, (float)(n - 1)); +#else const float a = An(Mn(s, n), -.5f), i = floor(a); // fenced: the driver turns a - floor(a) into v_fract_f32, which differs when a + 1 rounds up to 1 (a = -2^-26) r.f = An(a, -fence(i)); r.g = An(1, -r.f); r.c0 = (int)clamp(i, 0.0f, (float)(n - 1)); r.c1 = (int)clamp(An(i, 1), 0.0f, (float)(n - 1)); +#endif r.o0 = (m24 ? of_m24((uint)r.c0, stride) : (uint)r.c0 * stride) + base; r.o1 = (m24 ? of_m24((uint)r.c1, stride) : (uint)r.c1 * stride) + base; return r; @@ -271,6 +345,7 @@ OfTaps of_taps(OfAxis ax, OfAxis ay) r.wa[0] = Mn(ax.g, ay.g); r.wa[1] = Mn(ax.f, ay.g); r.wa[2] = Mn(ax.g, ay.f); r.wa[3] = Mn(ax.f, ay.f); return r; } +#endif float of_bil(OfTaps q, float v0, float v1, float v2, float v3) { return An(An(An(Mn(q.wa[0], v0), Mn(q.wa[1], v1)), Mn(q.wa[2], v2)), Mn(q.wa[3], v3)); @@ -285,6 +360,13 @@ bool of_hhalf() { return true; } bool of_half() { return (packed_head & 4u) != 0u; } bool of_hhalf() { return (packed_head & 32u) != 0u; } #endif +#if OFX_W5_ALU +float of_mhf(float w, half h) { return w * (float)h; } +float of_bilf(OfTaps q, half h0, half h1, half h2, half h3) +{ + return mad(q.wa[3], (float)h3, mad(q.wa[2], (float)h2, mad(q.wa[1], (float)h1, q.wa[0] * (float)h0))); +} +#else // M(w, h) of a binary16 value h as fma(w, h, -0): the same rounding, and the driver reads h as f16 (v_fma_mix_f32). // The addend is an opaque -0: with a literal -0 the driver dropped it and fused the product into the next addition. float of_mhf(float w, half h) { return mad(w, (float)h, asfloat(0x80000000u ^ of_zb())); } @@ -292,6 +374,7 @@ float of_bilf(OfTaps q, half h0, half h1, half h2, half h3) { return An(An(An(of_mhf(q.wa[0], h0), of_mhf(q.wa[1], h1)), of_mhf(q.wa[2], h2)), of_mhf(q.wa[3], h3)); } +#endif float3 of_hist(OfTaps q) { if (of_hhalf()) { // binary16 rgba texels (8 bytes) @@ -315,10 +398,15 @@ float3 of_hist(OfTaps q) return float3(of_bilf(q, a0.x, a1.x, b0.x, b1.x), of_bilf(q, a0.y, a1.y, b0.y, b1.y), of_bilf(q, a0.z, a1.z, b0.z, b1.z)); #endif } +#if OFX_W5_ALU && OFX_SPEC + const uint r0 = 0u, r1 = 0u; + return float3(0, 0, 0); +#else const uint r0 = of_m24((uint)q.y0, wh), r1 = of_m24((uint)q.y1, wh); const float3 v0 = asfloat(wg_mem.Load3(hprev + (r0 + (uint)q.x0) * 16u)), v1 = asfloat(wg_mem.Load3(hprev + (r0 + (uint)q.x1) * 16u)); const float3 v2 = asfloat(wg_mem.Load3(hprev + (r1 + (uint)q.x0) * 16u)), v3 = asfloat(wg_mem.Load3(hprev + (r1 + (uint)q.x1) * 16u)); return float3(of_bil(q, v0.x, v1.x, v2.x, v3.x), of_bil(q, v0.y, v1.y, v2.y, v3.y), of_bil(q, v0.z, v1.z, v2.z, v3.z)); +#endif } // reproj of rr_output.hlsl. With TR entries 0 or >= 2^-26 in magnitude (prep flag bit 1 clear) the weights are 0 or @@ -326,9 +414,14 @@ float3 of_hist(OfTaps q) // none lies in the band. Otherwise (fine table) its products / fmas are checked. float3 of_fallback(float3 current, float Ep, float invE) { +#if OFX_W5_ALU + const float fbs = (Ep * invE) * K12; + return float3(Cf(current.x * fbs), Cf(current.y * fbs), Cf(current.z * fbs)); +#else float3 f; [unroll] for (uint c = 0; c < 3; c++) f[c] = Cf(Mf(Ep, Mf(invE, Mf(fence(current[c]), K12)))); // >= 2^-114 (of_insane) return f; +#endif } // (clean/rr-f-r6-tail) the fallback colour is computed only for a candidate that falls (waves without such a lane skip it) float3 of_reproj(uint x, uint y, float2 mv, float3 current, float Ep, float invE, out uint fell, float Rwh, float Rhh, bool fine, inout bool rare) @@ -347,6 +440,22 @@ float3 of_reproj(uint x, uint y, float2 mv, float3 current, float Ep, float invE // the five positions use three distinct x and three distinct y coordinates // byte offsets of the binary16 texels: column * 8, row * wh * 8 + hprev const uint ys = wh * 8u; +#if OFX_W5C && OFX_W5_ALU + const OfAxis X0 = of_axis(An(kx, tx.w), Rwh, wh, 8u, 0u); + const OfAxis Y1 = of_axis(An(ky, ty.w), Rhh, hh, ys, hprev, true); + // Rolled 5-tap loop (opaque upper bound prevents LLVM hoisting all 20 b64 loads into one 40-VGPR batch: 74 -> 55 VGPR) + const uint k_end = 5u + (wg_grid_ >> 31); + [loop] for (uint k = 0; k < k_end; ++k) { + OfAxis qx = X0, qy = Y1; + if (k == 1u) qx = of_axis(An(kx, -1), Rwh, wh, 8u, 0u); + else if (k == 4u) qx = of_axis(An(kx, 2), Rwh, wh, 8u, 0u); + if (k == 0u) qy = of_axis(An(ky, -1), Rhh, hh, ys, hprev, true); + else if (k == 3u) qy = of_axis(An(ky, 2), Rhh, hh, ys, hprev, true); + const float3 tap = of_hist(of_taps(qx, qy)); + const float wk = weights[k]; + [unroll] for (uint c = 0; c < 3; c++) acc[c] = k == 0u ? tap[c] * wk : mad(tap[c], wk, acc[c]); + } +#else const OfAxis X0 = of_axis(An(kx, tx.w), Rwh, wh, 8u, 0u), X1 = of_axis(An(kx, -1), Rwh, wh, 8u, 0u), X2 = of_axis(An(kx, 2), Rwh, wh, 8u, 0u); const OfAxis Y0 = of_axis(An(ky, -1), Rhh, hh, ys, hprev, true), Y1 = of_axis(An(ky, ty.w), Rhh, hh, ys, hprev, true), Y2 = of_axis(An(ky, 2), Rhh, hh, ys, hprev, true); @@ -358,15 +467,22 @@ float3 of_reproj(uint x, uint y, float2 mv, float3 current, float Ep, float invE if (k == 3) qy = Y2; const OfTaps q = of_taps(qx, qy); const float3 tap = of_hist(q); +#if OFX_W5_ALU + [unroll] for (uint c = 0; c < 3; c++) acc[c] = k == 0 ? tap[c] * weights[k] : mad(tap[c], weights[k], acc[c]); +#else if (fine) { [unroll] for (uint c = 0; c < 3; c++) acc[c] = k == 0 ? Mq(tap[c], weights[k], rare) : Fq(tap[c], weights[k], acc[c], rare); } else { [unroll] for (uint c = 0; c < 3; c++) acc[c] = k == 0 ? Mn(tap[c], weights[k]) : Fn(tap[c], weights[k], acc[c]); } +#endif } +#endif float total = An(weights[4], An(weights[3], An(weights[2], An(weights[1], weights[0])))); float Rt = Rf(total); -#if OF_GZ +#if OFX_W5_ALU + acc = acc * Rt; +#elif OF_GZ float3 r3; [unroll] for (uint c = 0; c < 3; c++) r3[c] = Mf(acc[c], Rt); [branch] if (of_z3(r3.x, r3.y, r3.z) == 0) @@ -404,10 +520,14 @@ float3 of_blend(float3 current, float3 hist, float alpha, out uint guarded, inou // (s >= 2^-11 when p >= .5, so e is a multiple of 2^-34, never flushed). uint of_code10(float x) { +#if OFX_W5_ALU + return min(1023u, (uint)round(saturate(x) * 1023.0f) + BAD_CODE); +#else const float s = min(max(x, 0.0f), 1.0f), p = s * 1023.0f, e = mad(fence(s), 1023.0f, -p), fl = floor(p); // fence: see fence2 float a = round(p); a = p - fl == .5f ? (e > 0 ? fl + 1 : (e < 0 ? fl : a)) : a; return min(1023u, (uint)a + BAD_CODE); +#endif } // ---- prep: one lane per render pixel ------------------------------------------------------------------------------ @@ -520,9 +640,14 @@ WG_KERNEL(rr_output_fast) const int rx0 = of_cx(tx0, sx) - 1, ry0 = of_cya(ty0, sy) - 1; const int W = of_cx(xl, sx) + 2 - rx0 + 1, Hn = of_cya(yl, sy) + 2 - ry0 + 1; const bool fits = W <= OF_LW && Hn <= OF_LH; +#if OFX_W5_ALU + const bool fine = false; + bool rare = false; +#else const uint pflags = of_u1(wg_ld32(scratch, 4u)); const bool fine = (pflags & 2u) != 0u; bool rare = !fits || (pflags & 1u) != 0u || insane; +#endif if (fits) { const uint ra = of_rec_a(), rb = of_rec_b(); const uint cc = lane & 31u; // W <= 30: one lane per record column, WG_BLOCK / 32 rows per step @@ -545,9 +670,43 @@ WG_KERNEL(rr_output_fast) // u = F(x + .5, sx, jx): (x + .5) sx is a multiple of 2^-40 and jx of 2^-41 where they can cancel: never small const float u = Fn(x + .5f, sx, jx), v = Fn(y + .5f, sy, jy), va = Fn((y / 2) * 2 + .5f, sy, jy); const int cx = (int)floor(An(u, -.5f)), cy = (int)floor(An(v, -.5f)), cya = (int)floor(An(va, -.5f)); +#if OFX_W5_ALU + const half hx = (half)((float)cx + 0.5f - u), hy0 = (half)((float)cy + 0.5f - v); + const half hy = cy != cya ? (hy0 - (half)1.0) : hy0; +#else const uint hxy = of_cvt(fence(An(cx + .5f, -u))) | (of_cvt(fence(An(cy + .5f, -v))) << 16); const half hx = of_hh(hxy), hy0 = of_hh(hxy >> 16); const half hy = cy != cya ? of_fh(hy0 + (half)-1.0) : hy0; +#endif +#if OFX_W5C && OFX_W5_ALU && OFX_FUSE + half oy[4], oyy[4]; + [unroll] for (int m0 = 0; m0 < 4; m0++) { + oy[m0] = hy + (half)(m0 - 1); + oyy[m0] = oy[m0] * oy[m0]; + } + const int Wl = fits ? W : 0, lbase = fits ? (cya - 1 - ry0) * W + (cx - 1 - rx0) : 0; + float total = 0; + half2 s0 = half2(0, 0), s1 = half2(0, 0), s2 = half2(0, 0); + [unroll] for (int k = 0; k < 4; k++) { + const half oxk = hx + (half)(k - 1); + const half oxxk = oxk * oxk; + [unroll] for (int m = 0; m < 4; m++) { + const int li = lbase + m * Wl + k; + const uint4 ra = of_ra[li]; + const uint pb = of_rb[li]; + const half ax = of_hh(ra.w), ay = of_hh(ra.w >> 16), axy = of_hh(pb); + const half t1 = oxxk * ax; + const half t2 = mad(oyy[m], ay, t1); + const half oxy = oxk * oy[m]; + const half e = mad(oxy, axy, t2); + const half w = exp2(e); + total += (float)w; + const half2 ww = half2(w, w); + s0 = mad(ww, wg_as_h2(ra.x), s0); s1 = mad(ww, wg_as_h2(ra.y), s1); s2 = mad(ww, wg_as_h2(ra.z), s2); + } + } + OF_CLK(2); +#else half ox[4], oy[4], oxx[4], oyy[4]; [unroll] for (int k = 0; k < 4; k++) { // (a folded hx + 0 could only change the sign of a zero offset, which no product below can show) @@ -558,7 +717,27 @@ WG_KERNEL(rr_output_fast) #ifndef OFX_FUSE #define OFX_FUSE 0 // clean/rr-f-a14-tail D4: 1 = phases 1 and 2 in one k-outer m-inner loop (the same operations and order; #endif // each weight converted by 0x1c = H on its own, one b128 + one b32 LDS read per tap) -#if OFX_FUSE +#if OFX_W5_ALU && OFX_FUSE + float total = 0; + half2 s0 = half2(0, 0), s1 = half2(0, 0), s2 = half2(0, 0); + [unroll] for (int k = 0; k < 4; k++) { + [unroll] for (int m = 0; m < 4; m++) { + const int li = lbase + m * Wl + k; + const uint4 ra = of_ra[li]; + const uint pb = of_rb[li]; + const half ax = of_hh(ra.w), ay = of_hh(ra.w >> 16), axy = of_hh(pb); + const half t1 = oxx[k] * ax; + const half t2 = mad(oyy[m], ay, t1); + const half oxy = ox[k] * oy[m]; + const half e = mad(oxy, axy, t2); + const half w = exp2(e); + total += (float)w; + const half2 ww = half2(w, w); + s0 = mad(ww, wg_as_h2(ra.x), s0); s1 = mad(ww, wg_as_h2(ra.y), s1); s2 = mad(ww, wg_as_h2(ra.z), s2); + } + } + OF_CLK(2); +#elif OFX_FUSE float total = 0; half2 s0 = half2(0, 0), s1 = half2(0, 0), s2 = half2(0, 0); [unroll] for (int k = 0; k < 4; k++) { @@ -609,6 +788,7 @@ WG_KERNEL(rr_output_fast) const half2 ww = half2(w, w); s0 = mad(ww, wg_as_h2(r.x), s0); s1 = mad(ww, wg_as_h2(r.y), s1); s2 = mad(ww, wg_as_h2(r.z), s2); } +#endif #endif // A NaN payload of these sums (the only possible difference to HF) never reaches an output: sums 0-2 enter D (a // NaN gives D's own NaN constant), sum 3 and the total enter S / max (a NaN gives 0 / K13), sums 4-5 make alpha @@ -617,10 +797,16 @@ WG_KERNEL(rr_output_fast) const float accum[6] = {of_f(s0.x), of_f(s0.y), of_f(s1.x), of_f(s1.y), of_f(s2.x), of_f(s2.y)}; // norm = accum / max(total, K13): accum is 0 or >= 2^-24, so only a reciprocal below 2^-102 can reach the band const float Rn = Rf(max(total, K13)); +#if !OFX_W5_ALU rare = or(rare, of_small(Rn, asfloat(0x0d800000u))); +#endif float norm[6]; [unroll] for (uint c = 0; c < 6; c++) norm[c] = Mn(accum[c], Rn); -#if OF_GZ +#if OFX_W5_ALU + const float coverage = saturate(norm[3] * (total * K1)); + const float ah = mad(coverage, 1.0f - norm[4], norm[4]), ao = mad(coverage, 1.0f - norm[5], norm[5]); + const float3 current = float3(Df(norm[0], rare), Df(norm[1], rare), Df(norm[2], rare)); +#elif OF_GZ // shipped site set: Mq(total, K1) unchecked (lost to the inout copy-out in the per-site form) const float cm1 = Mf(total, K1), cm2 = Mf(norm[3], cm1); const float coverage = min(max(cm2, 0.0f), 1.0f); @@ -640,6 +826,15 @@ WG_KERNEL(rr_output_fast) #endif const float s = Mf(u, Rwr), t = Mf(v, Rhr); // candidate weights: bilinear of the decoded binary16 W4 records +#if OFX_W5_ALU && OFX_SPEC + const OfTaps qc = of_taps(of_axis_s(s, wr, 8u, 0u), of_axis_s(t, hr, wr * 8u, 0u, true)); + const uint rw = of_rec_w(); + const uint2 c0 = wg_mem.Load2(rw + qc.oy0 + qc.ox0), c1 = wg_mem.Load2(rw + qc.oy0 + qc.ox1), + c2 = wg_mem.Load2(rw + qc.oy1 + qc.ox0), c3 = wg_mem.Load2(rw + qc.oy1 + qc.ox1); + float4 weights; + [unroll] for (uint c = 0; c < 4; c++) { const uint sh = (c & 1u) * 16u; + weights[c] = of_bilf(qc, of_hh((c < 2 ? c0.x : c0.y) >> sh), of_hh((c < 2 ? c1.x : c1.y) >> sh), of_hh((c < 2 ? c2.x : c2.y) >> sh), of_hh((c < 2 ? c3.x : c3.y) >> sh)); } +#else const OfTaps qc = of_taps(of_axis_s(s, wr), of_axis_s(t, hr)); const uint rw = of_rec_w(); // (byte pitches in the 24-bit products: the compiler distributes a byte scale over the index sum) @@ -655,6 +850,7 @@ WG_KERNEL(rr_output_fast) c2 = wg_mem.Load(rw + qr1 + qc.x0 * 8), c3 = wg_mem.Load(rw + qr1 + qc.x1 * 8); float4 weights; [unroll] for (uint c = 0; c < 4; c++) weights[c] = of_bilf(qc, c0[c], c1[c], c2[c], c3[c]); +#endif #endif const int fu = (int)floor(u), fv = (int)floor(v); const bool inr = and(and(fu >= 0, fv >= 0), and(fu < (int)wr, fv < (int)hr)); @@ -674,6 +870,14 @@ WG_KERNEL(rr_output_fast) pair = inr ? float4(pm) : float4(0, 0, 0, 0); ero = inr ? float2(pe) : float2(0, 0); #endif +#if OFX_W5_ALU && OFX_SPEC + const uint n0 = wg_mem.Load(motion_non + (qc.oy0 + qc.ox0) / 2u), n1 = wg_mem.Load(motion_non + (qc.oy0 + qc.ox1) / 2u), + n2 = wg_mem.Load(motion_non + (qc.oy1 + qc.ox0) / 2u), n3 = wg_mem.Load(motion_non + (qc.oy1 + qc.ox1) / 2u); + non = float2(of_bilf(qc, of_hh(n0), of_hh(n1), of_hh(n2), of_hh(n3)), of_bilf(qc, of_hh(n0 >> 16), of_hh(n1 >> 16), of_hh(n2 >> 16), of_hh(n3 >> 16))); + } else { + pair = float4(0, 0, 0, 0); ero = float2(0, 0); non = float2(0, 0); + } +#else const uint qh0 = qr0 / 2u, qh1 = qr1 / 2u; #if OFX_LD2 const uint n0 = wg_mem.Load(motion_non + qh0 + qc.x0 * 4), n1 = wg_mem.Load(motion_non + qh0 + qc.x1 * 4), @@ -691,6 +895,36 @@ WG_KERNEL(rr_output_fast) n2 = asfloat(wg_ld64(motion_non, qr1 + qc.x0 * 8)), n3 = asfloat(wg_ld64(motion_non, qr1 + qc.x1 * 8)); non = float2(of_bil(qc, n0.x, n1.x, n2.x, n3.x), of_bil(qc, n0.y, n1.y, n2.y, n3.y)); } +#endif +#if OFX_W5_ALU && OF_REPROJ_MAX == 1 + // 3-comparison max preserving exact !(wa < wb) tie/NaN winner of the 5-CAS network; single reprojection, explicit zero-weight guard + float w0 = weights.x, w1 = weights.y, w2 = weights.z, w3 = weights.w; + float2 m0 = pair.xy, m1 = non, m2 = ero, m3 = pair.zw; + if (w0 < w1) { w0 = w1; m0 = m1; } + if (w2 < w3) { w2 = w3; m2 = m3; } + if (w0 < w2) { w0 = w2; m0 = m2; } + uint fell; + const float3 tap = of_reproj(x, y, m0, current, Ep, invE, fell, Rwh, Rhh, false, rare); + const float hist_scale = (E * K12) * REp; + const float d_scale = K8 * asfloat(0x40549a78u); + const float3 hist = float3( + (exp2(min(max(tap.x, K5), K10) * d_scale) - 1.0f) * hist_scale, + (exp2(min(max(tap.y, K5), K10) * d_scale) - 1.0f) * hist_scale, + (exp2(min(max(tap.z, K5), K10) * d_scale) - 1.0f) * hist_scale); + const float3 curK12 = current * K12; + const bool valid_w = (w0 > 0.0f); + float3 bh = mad(ah, curK12 - hist, hist); + float3 bo = mad(ao, curK12 - hist, hist); + if (!valid_w || isnan(bh.x + bh.y + bh.z)) bh = curK12; + if (!valid_w || isnan(bo.x + bo.y + bo.z)) bo = curK12; + const uint4 hq = uint4( + of_cvt(Cf(bh.x)) | (of_cvt(Cf(bh.y)) << 16), + of_cvt(Cf(bh.z)) | (of_cvt(Cf(bo.x)) << 16), + of_cvt(Cf(bo.y)) | (of_cvt(Cf(bo.z)) << 16), + 0u); + const uint4 ord = uint4(0u, 1u, 2u, 3u); + const uint fallmask = fell, usedmask = 1u, gh = 0u, go = 0u; +#else // order() of rr_output.hlsl on (weight, index, motion) triples: cmp keeps (a, b) iff w[a] >= w[b] or either is NaN, // i.e. iff !(w[a] < w[b]); the five compare-exchanges carry the values instead of indexing them float w0 = weights.x, w1 = weights.y, w2 = weights.z, w3 = weights.w; @@ -787,6 +1021,7 @@ WG_KERNEL(rr_output_fast) [unroll] for (uint c = 0; c < 3; c++) { cq.v[c] = Cf(bh[c]); cq.v[3 + c] = Cf(bo[c]); } cq.v[6] = 0; cq.v[7] = 0; const uint4 hq = of_to_f16(cq); +#endif // packed_head bit 3: no diagnostic outputs (linear f32 and trace; the fallback still writes them for its quads) const bool diag = !OFX_SPEC && (packed_head & 8u) == 0u; if (valid) { @@ -816,7 +1051,45 @@ WG_KERNEL(rr_output_fast) #endif OF_CLK(6); // ---- PH1: the tile's 16 x 4 quads (pixels outside the image count as 0, as rr_output_pool's fetch) +#if OFX_W5_ALU #if OFX_PH1W && WG_WAVE == 64 + { + const float3 vt = valid ? bh : float3(0, 0, 0); + const uint wl = lane & 63u, wx = wl ^ 32u; + const float3 vb = float3(WaveReadLaneAt(vt.x, wx), WaveReadLaneAt(vt.y, wx), WaveReadLaneAt(vt.z, wx)); + const float3 vtr = float3(QuadReadAcrossX(vt.x), QuadReadAcrossX(vt.y), QuadReadAcrossX(vt.z)); + const float3 vbr = float3(QuadReadAcrossX(vb.x), QuadReadAcrossX(vb.y), QuadReadAcrossX(vb.z)); + if (wl < 32u && (lx & 1u) == 0u) { + const uint gx = tx0 + lx, gy = ty0 + ly; + const uint count = min(2u, wh - min(gx, wh)) * min(2u, hh - min(gy, hh)); + const float Rc = rcp((float)count); + uint code[3]; + [unroll] for (uint c = 0; c < 3; c++) code[c] = of_code10(Cf(((vt[c] + vb[c]) + (vtr[c] + vbr[c])) * Rc) * K11); + if (gx < wh && gy < hh) { + const uint pidx = of_m24(gy / 2u, of_pw()) + gx / 2u; + wg_st64(ph, pidx * 8u, uint2(code[0] | (code[1] << 16), code[2] | (3u << 16))); + } + } + } +#else + of_lin[lane] = valid ? bh : float3(0, 0, 0); + GroupMemoryBarrierWithGroupSync(); + if (lane < 64u) { + const uint qx = lane & 15u, qy = lane >> 4, gx = tx0 + 2u * qx, gy = ty0 + 2u * qy; + const uint l0 = (2u * qy) * OF_TX + 2u * qx; + const uint count = of_m24(min(2u, wh - min(gx, wh)), min(2u, hh - min(gy, hh))); + const float Rc = rcp((float)count); + const float3 tl = of_lin[l0], tr = of_lin[l0 + 1u], bl = of_lin[l0 + OF_TX], br = of_lin[l0 + OF_TX + 1u]; + uint code[3]; + [unroll] for (uint c = 0; c < 3; c++) code[c] = of_code10(Cf(((tl[c] + bl[c]) + (tr[c] + br[c])) * Rc) * K11); + if (gx < wh && gy < hh) { + const uint pidx = of_m24(gy / 2u, of_pw()) + gx / 2u; + wg_st64(ph, pidx * 8u, uint2(code[0] | (code[1] << 16), code[2] | (3u << 16))); + } + } +#endif + if (gid == 0u && lane == 0u) wg_st32(scratch, 4u, 0u); +#elif OFX_PH1W && WG_WAVE == 64 // clean/rr-f-a14-tail D7 (same bits): a quad's 4 pixels are lanes l, l + 1, l + 32, l + 33 of one wave64 (rows ly, // ly + 1): each wave pools its own 16 quads from lane reads (the same An(An(tl, bl), An(tr, br)) order), no // groupshared copy, no barrier diff --git a/clean/rr_f/kernels/rr_pool.hlsl b/clean/rr_f/kernels/rr_pool.hlsl index 31a0c07b..a8a901c4 100644 --- a/clean/rr_f/kernels/rr_pool.hlsl +++ b/clean/rr_f/kernels/rr_pool.hlsl @@ -24,6 +24,9 @@ WG_ARGS_END #ifndef RR_POOL_TPL #define RR_POOL_TPL 4 // PH2 texels per lane (the tables are staged once per workgroup) #endif +#ifndef RR_POOL_W5 +#define RR_POOL_W5 0 // clean/rr-f-wave5c (Fast mode only, VG_OUT): hardware f32 mad / mul with the exact tables (0 FP64) +#endif uint PU(uint i) { return WaveReadLaneFirst(wg_ld32(prm, i * 4u)); } float PF(uint i) { return asfloat(PU(i)); } @@ -127,6 +130,27 @@ WG_KERNEL(rr_pool_main) [unroll] for (uint r = 0; r < RR_POOL_TPL; ++r) { const uint idx = (wg_block_id() * RR_POOL_TPL + r) * WG_BLOCK + wg_lane_id(); if (idx >= w2 * h2) break; +#if RR_POOL_W5 + const uint yu = idx / w2, xu = idx - yu * w2; + const int x = (int)xu, y = (int)yu; + float3 acc = 0.0f; + uint cnt = 0u; + [unroll] for (uint t = 0; t < 4u; ++t) { + const int tx = 2 * x + (int)(t & 1u), ty = 2 * y + (int)(t >> 1); + if (tx < (int)ph.w && ty < (int)ph.h) { + const uint2 cw = wg_ld64(ph.off, (((uint)ty & 0xffffffu) * (ph.pitch & 0xffffffu) + (uint)tx) * 8u); + acc.x = mad(gs_term[cw.x & 1023u], K12, acc.x); + acc.y = mad(gs_term[(cw.x >> 16) & 1023u], K12, acc.y); + acc.z = mad(gs_term[cw.y & 1023u], K12, acc.z); + cnt += 1u; + } + } + const float rn = cnt == 4u ? 0.25f : (cnt == 2u ? 0.5f : 1.0f); + const uint c0 = table_code(rn * acc.x, inc, cnan), c1 = table_code(rn * acc.y, inc, cnan); + const uint c2 = table_code(rn * acc.z, inc, cnan); + if (x < (int)pq.w && y < (int)pq.h) + wg_st64(pq.off, (((uint)y & 0xffffffu) * (pq.pitch & 0xffffffu) + (uint)x) * 8u, uint2(c0 | (c1 << 16), c2 | (3u << 16))); +#else const int x = (int)(idx % w2), y = (int)(idx / w2); float3 acc = 0.0f; uint cnt = 0u; @@ -146,6 +170,7 @@ WG_KERNEL(rr_pool_main) const uint c2 = table_code(M(rn, acc.z), inc, cnan); if (x < (int)pq.w && y < (int)pq.h) wg_st64(pq.off, ((uint)y * pq.pitch + (uint)x) * 8u, uint2(c0 | (c1 << 16), c2 | (3u << 16))); +#endif } } diff --git a/clean/rr_f/kernels/rr_resolve_fast_main.hlsli b/clean/rr_f/kernels/rr_resolve_fast_main.hlsli index 619a01da..eae4b5fb 100644 --- a/clean/rr_f/kernels/rr_resolve_fast_main.hlsli +++ b/clean/rr_f/kernels/rr_resolve_fast_main.hlsli @@ -2,6 +2,15 @@ #ifndef RFX_QLD #define RFX_QLD 0 // clean/rr-f-a14-tail D17: the Q texel as one aligned b64 + shifts (same words; measured +0.001 ms) #endif +#ifndef RFX_W5_ALU +#define RFX_W5_ALU 0 // clean/rr-f-wave5b (Fast mode only, VG_OUT): bounded ALU fast path for RF_SPEC 2/3 (no fp64 uniforms, no slow/fixup, FP16 3x3 clamp before E, uint2 half4 taps, branchless bilinear, native half stores) +#endif +#ifndef RFX_W5C +#define RFX_W5C 0 // clean/rr-f-wave5c (Fast mode only, VG_OUT): QN=28 (QO=896, wave-uniform s1==3 skip), RFX_RP=16 -> RF_RS=1 (12.8 KB LDS -> 5 WGs/CU), 32-bit MV load +#endif +#ifndef RFX_LLDS +#define RFX_LLDS 0 // clean/rr-f-wave5c (Fast mode only, VG_OUT): read layer bilinear taps from LDS render tile (CPU-guarded L.w==Wr, L.h==Hr, lbx==0, lby==0, |jitter|<=0.55) +#endif // rr_resolve_fast: page 54b.1-54b.4 (the work of rr_resolve_main) with the same bits for every input. // Included by rr_resolve_fast.hlsl after rr_resolve.hlsl (helpers of the verified kernel) and the exp2 table. // @@ -42,6 +51,12 @@ // upscaling (any reset / bounded / exposure-link flags), no opacity; L, MV, TH, // OUT binary16 x4. As 1 without the alpha work: the render tile holds only the // layer (no P / G / alpha source loads), no alpha taps / history / stores. + // 3: the PLAIN configuration (exact, same bits as RF_SPEC 0 for those dispatches): + // neither extended nor layer nor opacity nor alpha upscaling (flags & 15 == 0; the + // reset / bounded / exposure-link bits are free), OUT binary16 x4. As 2 without + // any render-size work: no layer / history / alpha reads, no render tile, no + // format switches; the colour is the Q passes + decode, alpha 1 (rf_uniforms checks + // it; otherwise every pixel goes to the fixup kernel). #endif #ifndef RF_HOIST // round 13 (RF_SPEC, RF_EARLY; the same addresses and words, issued earlier): 1 the #define RF_HOIST 0 // motion-vector and layer reads at the tile start, the history reads after the Q pass @@ -51,8 +66,20 @@ #define RF_HOIST 0 #endif #define RF_T 16u // tile side (output pixels) +#if RFX_W5C +#ifndef RFX_RP +#define RFX_RP 20 +#endif +#define RF_QN 28u // Q tile side bound (proven <= 28 for every 16-px tile: ((6*15+3)>>2)+4+1 = 28) +#if RFX_RP == 16 +#define RF_RN 16u // ratio-limited render tile (nrx, nry <= 16; CPU-guarded) +#else +#define RF_RN 20u +#endif +#else #define RF_QN 29u // Q tile side bound (16 output pixels need at most 28 Q texels per axis) #define RF_RN 20u // render tile side bound (render / output <= 1 needs at most 20) +#endif #define RF_NR (RF_RN * RF_RN) #define RF_TINY asfloat(0x33800000u) // 2^-24: smallest magnitude of an "ordinary" nonzero input @@ -67,7 +94,7 @@ groupshared uint2 rf_g[RF_NR]; // phase 2: G halves (g0 | g1 << 1 groupshared uint2 rf_q[RF_QN * RF_QN]; #define RF_QW(e, v) rf_q[e] = (v).xy #define RF_QR(i) rf_q[i] -#elif RFX_NB == 3 && RF_SPEC == 2 // 3 (RF_SPEC 2 only): the Q tile (b64 texels) and the layer tile (2 x b64 per texel) +#elif RFX_NB == 3 && RF_SPEC >= 2 // 3 (RF_SPEC 2 and 3): the Q tile (b64 texels) and the layer tile (2 x b64 per texel) // ---- clean/rr-mp1-resolve (same bits): tile bookkeeping by layout (mpeak probe P6) ---------------------------------- #ifndef RFX_TB // default 23 (wave64, 256 lanes: the runtime's resolve.fast2 / resolve.fast2.x); 0 = the old code #if WG_BLOCK == 256 && WG_WAVE == 64 @@ -87,12 +114,18 @@ groupshared uint2 rf_q[RF_QN * RF_QN]; #endif // instead of 6 at <= 96 VGPRs); the 3x3 reads form E * widen(h) at each read with rf_slot's // own operation. 0 = the old layout (E * L.rgb, L.a as f32 in two b64 per texel) #if RFX_TB & 1 +#if RFX_W5C +#define RF_QO 896u // Q region: 28 x 32 b64 entries at pitch RF_QP (rows 0..27; waves 2..3 skip s=3) +#else #define RF_QO 1024u // Q region: 32 x 32 b64 entries at pitch RF_QP +#endif #define RF_QP 32u #else #define RF_QO (RF_QN * RF_QN) #endif -#if RFX_LR && (RFX_TB & 1) +#if RF_SPEC == 3 +groupshared uint2 rf_w2[RF_QO]; // RF_SPEC 3: the Q tile only (no layer / render tile) +#elif RFX_LR && (RFX_TB & 1) groupshared uint2 rf_w2[RF_QO + RF_NR]; // RFX_LR: the Q tile and the raw layer tile (one b64 per texel) #else groupshared uint2 rf_w2[RF_QO + 2u * RF_NR]; // side by side in one b64 array (LDS 20.1 -> 16.8 KB), no end barrier @@ -115,8 +148,8 @@ groupshared uint2 rf_w2[RF_QO + 2u * RF_NR]; // side by side in one b64 array #ifndef RFX_RP #define RFX_RP 20 #endif -#if RFX_TB && !(RFX_NB == 3 && RF_SPEC == 2) -#error RFX_TB needs RF_SPEC 2 and RFX_NB 3 +#if RFX_TB && !(RFX_NB == 3 && RF_SPEC >= 2) +#error RFX_TB needs RF_SPEC 2 or 3 and RFX_NB 3 #endif #if !(RF_SPEC == 2 && (RFX_TB & 1)) #undef RFX_LR @@ -220,6 +253,14 @@ float4 rf_h4(Res r, int x, int y) return float4(select(ins, v.x, 0.0f), select(ins, v.y, 0.0f), select(ins, v.z, 0.0f), select(ins, v.w, 0.0f)); } float rf_h1i(Res r, int x, int y) { return f16tof32(wg_ld16(r.off, (rf_m24((uint)y, r.pitch) + (uint)x) * 2u)); } +#if RFX_W5C +float2 rf_h2(Res r, int x, int y) +{ + bool ins = and(and(x >= 0, y >= 0), and(x < (int)r.w, y < (int)r.h)); + uint h = wg_ld32(r.off, (rf_m24((uint)clamp(y, 0, (int)r.h - 1), r.pitch) + (uint)clamp(x, 0, (int)r.w - 1)) * 8u); + return float2(select(ins, f16tof32(h), 0.0f), select(ins, f16tof32(h >> 16), 0.0f)); +} +#endif // One channel ach of rf_texel (the alpha source read of 54b.4). float rf_texel1(Res r, int x, int y, uint ach, inout bool tiny) @@ -277,6 +318,49 @@ void rf_store(Res r, int x, int y, float4 v) } // ---- bilinear sample of 52.1.4 (bilin of rr_resolve.hlsl): coordinates once, applied to any texture of that size -- +#if RFX_W5_ALU +struct RfBil { int xa, xb, ya, yb; float fx, fy; }; +RfBil rf_bil(float u, float v, uint W, uint H) +{ + RfBil b; + u = saturate(u); v = saturate(v); + const float s = mad(u, (float)W, -0.5f), t = mad(v, (float)H, -0.5f); + const float i0 = floor(s), j0 = floor(t); + b.fx = s - i0; b.fy = t - j0; + b.xa = clamp((int)i0, 0, (int)W - 1); b.xb = clamp((int)i0 + 1, 0, (int)W - 1); + b.ya = clamp((int)j0, 0, (int)H - 1); b.yb = clamp((int)j0 + 1, 0, (int)H - 1); + return b; +} +uint2 rf_h4i_raw(Res r, int x, int y) { return wg_ld64(r.off, (rf_m24((uint)y, r.pitch) + (uint)x) * 8u); } +float rf_bh4(float w00, float w10, float w01, float w11, uint a, uint b, uint c, uint d) +{ + return mad(w11, (float)asfloat16((uint16_t)d), + mad(w01, (float)asfloat16((uint16_t)c), + mad(w10, (float)asfloat16((uint16_t)b), w00 * (float)asfloat16((uint16_t)a)))); +} +float4 rf_bil4_raw(RfBil b, uint2 t00, uint2 t10, uint2 t01, uint2 t11) +{ + const float gx = 1.0f - b.fx, gy = 1.0f - b.fy; + const float w00 = gx * gy, w10 = b.fx * gy, w01 = gx * b.fy, w11 = b.fx * b.fy; + return float4( + rf_bh4(w00, w10, w01, w11, t00.x, t10.x, t01.x, t11.x), + rf_bh4(w00, w10, w01, w11, t00.x >> 16, t10.x >> 16, t01.x >> 16, t11.x >> 16), + rf_bh4(w00, w10, w01, w11, t00.y, t10.y, t01.y, t11.y), + rf_bh4(w00, w10, w01, w11, t00.y >> 16, t10.y >> 16, t01.y >> 16, t11.y >> 16)); +} +float rf_bil1w(RfBil b, float t00, float t10, float t01, float t11) +{ + const float gx = 1.0f - b.fx, gy = 1.0f - b.fy; + return mad(b.fy, mad(b.fx, t11, gx * t01), gy * mad(b.fx, t10, gx * t00)); +} +float4 rf_bil4(RfBil b, float4 t00, float4 t10, float4 t01, float4 t11) +{ + const float gx = 1.0f - b.fx, gy = 1.0f - b.fy; + const float w00 = gx * gy, w10 = b.fx * gy, w01 = gx * b.fy, w11 = b.fx * b.fy; + return mad(w11, t11, mad(w01, t01, mad(w10, t10, w00 * t00))); +} +float rf_rcp1(float x) { const float y = rcp(x); return mad(mad(-x, y, 1.0f), y, y); } +#else struct RfBil { int xa, xb, ya, yb; float fx, gx, fy, gy, s, t; bool nx, ny; }; RfBil rf_bil(float u, float v, uint W, uint H) { @@ -326,6 +410,7 @@ float rf_bil1w(RfBil b, float t00, float t10, float t01, float t11) else o = b.gy * (b.gx * t00 + b.fx * t10) + b.fy * (b.gx * t01 + b.fx * t11); return o; } +#endif // ---- per-dispatch values ------------------------------------------------------------------------------------------- struct RfU { @@ -346,6 +431,20 @@ RfU rf_uniforms() U.E = WaveReadLaneFirst(U.E); Eprev = WaveReadLaneFirst(Eprev); U.jx = PF(11); U.jy = PF(12); float fWe = (float)U.We, fHe = (float)U.He, fWr = (float)U.Wr, fHr = (float)U.Hr; +#if RFX_W5_ALU + // Pure FP32 1-Newton-step reciprocals (no binary64 v_rcp_f64); host guarantees RF_SPEC 2/3 & fits preconditions + U.rWe = rf_rcp1(fWe); U.rHe = rf_rcp1(fHe); + U.sxr = fWr * U.rWe; U.syr = fHr * U.rHe; + U.sxh = fWe * rf_rcp1((float)U.Wh); U.syh = fHe * rf_rcp1((float)U.Hh); + U.jxp = (fWe * rf_rcp1(fWr)) * U.jx; U.jyp = (fHe * rf_rcp1(fHr)) * U.jy; + U.fac = U.E * rf_rcp1(max(Eprev, PF(34))); + U.rE = rf_rcp1(U.E); + U.TBW = PF(35); U.TBW2 = U.TBW * U.TBW; U.w1a = 1.0f - U.TBW; U.w1b = 1.0f - U.TBW2; + U.K1 = PF(22); U.K2n = PF(23); U.K2x2n = PF(24); U.K5 = PF(26); U.K8 = PF(28); U.K10 = PF(29); U.K12 = PF(31); + U.K13 = PF(32); + U.ok = true; + return U; +#else // DIV / RCP of the verified kernel (binary64), one quotient per lane of the wave (lane k computes quotient k), // then read from those lanes. DIV(a, b) = rounded(a / b) for the finite nonzero sizes; RCP(x) as rr_resolve_math. uint k = WaveGetLaneIndex(); @@ -411,6 +510,16 @@ RfU rf_uniforms() f = and(f, and(U.We <= 32768u, U.He <= 32768u)); #endif #endif +#elif RF_SPEC == 3 + bool f = (flags & 15u) == 0u; // plain: not extended, no layer / opacity / alpha upscaling + f = and(f, and(O.fmt == 1u, O.ch == 4u)); +#if RFX_TB & 1 + f = and(f, and(and(q.w >= U.Wh, q.h >= U.Hh), and(q.ch == 3u, q.pitch < (1u << 21)))); + f = and(f, and(and(U.We <= (1u << 20), U.He <= (1u << 20)), and(U.Wh < 65536u, U.Hh < 65536u))); +#if RFX_TB & 16 + f = and(f, and(U.We <= 32768u, U.He <= 32768u)); +#endif +#endif #else bool f = (flags & 11u) == 11u; f = and(f, and(and(P.fmt == 1u, P.ch == 4u), and(L.fmt == 1u, L.ch == 4u))); @@ -425,10 +534,18 @@ RfU rf_uniforms() #endif U.ok = !bad; return U; +#endif } // DECODE of rr_resolve.hlsl for the three channels: (exp2((c'*K8)*K9) - 1) * K12. The exp2 of an input the two fast // tiers leave undecided (1314 f32 inputs in all) is EXP2_CR itself, at one call site for the three channels. +#if RFX_W5_ALU +float3 rf_decode3(float3 c, RfU U) +{ + const float ks = U.K8 * RR_K9; + return (exp2(clamp(c, U.K5, U.K10) * ks) - 1.0f) * U.K12; +} +#else float rf_dec_t(float c, RfU U) { bool nc = nanf(c); @@ -448,6 +565,7 @@ float3 rf_decode3(float3 c, RfU U) if (any(hd)) rf_exp2_fix3(t, e, hd); return float3((e.x + (-1.0f)) * U.K12, (e.y + (-1.0f)) * U.K12, (e.z + (-1.0f)) * U.K12); } +#endif // ---- the kernel ---------------------------------------------------------------------------------------------------- #if RF_SPEC @@ -516,7 +634,11 @@ RfG rf_gunpack(RfU U, uint4 t, uint k) // k wave-uniform: 4 v_readlan uint2 rf_gpack2(RfU U, uint tile, uint ntiles, bool need_r) { RfG g = rf_geom(U, min(tile, ntiles - 1u), need_r, false); +#if RF_SPEC == 3 + const uint nrx = g.fits ? 1u : 0u, nry = g.fits ? 1u : 0u; // RF_SPEC 3: no render footprint (need_r false): a 1 x 1 placeholder marks "fits" +#else const uint nrx = g.fits ? g.nrx : 0u, nry = g.fits ? g.nry : 0u; +#endif const uint rx0 = g.fits ? (uint)g.rx0 : 0u, ry0 = g.fits ? (uint)g.ry0 : 0u; return uint2(rx0 | (ry0 << 16), (g.tx0 >> 4) | ((g.ty0 >> 4) << 11) | (nrx << 22) | (nry << 27)); } @@ -561,13 +683,20 @@ void rf_rsplit(uint e, out uint q, out uint r) // Cross-tile prefetch (RF_SPEC): the raw words of the next tile's Q and render footprints are loaded into registers // while the current tile's pixels are computed, then written to groupshared at the next tile's phases 1 and 3. #define RF_QS 4u // Q texels per lane (29 x 29 <= 4 x 256) +#if RFX_W5C && RFX_RP == 16 +#define RF_RS 1u // render texels per lane (16 x 16 == 256 == WG_BLOCK) +#else #define RF_RS 2u // render texels per lane (20 x 20 <= 2 x 256) +#endif struct RfPf { uint q[2u * RF_QS]; uint r[6u * RF_RS]; }; RfPf rf_prefetch(RfG g, uint lane, Res Q, Res Pr, Res Ar, Res Gr, Res L, int lbx, int lby, bool ok) { // ok = false (the dispatch takes the verified path): the resources may be absent, every load reads arena word 0 RfPf pf; [unroll] for (uint s = 0; s < RF_QS; ++s) { +#if RFX_W5C && (RFX_TB & 1) + if (s < 3u || lane < 128u) { +#endif #if RFX_TB & 1 // T1 (a): slot s of lane l = Q-tile entry (row (l >> 5) + 8 s, column l & 31) = LDS index l + 256 s. Entries // outside nqc x nqr load a clamped texel of the footprint (never read). Screens: gx <= qc1 <= Wh - 1 < Q.w, @@ -588,6 +717,11 @@ RfPf rf_prefetch(RfG g, uint lane, Res Q, Res Pr, Res Ar, Res Gr, Res L, int lbx #else pf.q[2u * s] = wg_ld16(a, 0u) | (wg_ld16(a, 2u) << 16); pf.q[2u * s + 1u] = wg_ld16(a, 4u); +#endif +#if RFX_W5C && (RFX_TB & 1) + } else { + pf.q[2u * s] = 0u; pf.q[2u * s + 1u] = 0u; + } #endif } [unroll] for (uint s2 = 0; s2 < RF_RS; ++s2) { @@ -606,6 +740,9 @@ RfPf rf_prefetch(RfG g, uint lane, Res Q, Res Pr, Res Ar, Res Gr, Res L, int lbx #if RF_SPEC == 2 uint2 pw = uint2(0u, 0u), lw = wg_ld64(al, 0u); // the layer only (no alpha upscaling) uint aw = 0u, gw = 0u; +#elif RF_SPEC == 3 + uint2 pw = uint2(0u, 0u), lw = uint2(0u, 0u); // plain: no render-size reads + uint aw = 0u, gw = 0u; #else uint2 pw = wg_ld64(ap, 0u), lw = wg_ld64(al, 0u); uint aw = wg_ld32(aa, 0u), gw = wg_ld32(ag, 0u); @@ -730,13 +867,19 @@ WG_KERNEL(rr_resolve_fast) rf_load_table(PU(38)); RfU U = rf_uniforms(); const uint lane = wg_lane_id(), flags = U.flags; - const bool ext = RF_SPEC || (flags & 1u) != 0u; - const bool lay = RF_SPEC || (flags & 3u) == 3u, alp = RF_SPEC == 1 || (RF_SPEC == 0 && (flags & 9u) == 9u); + const bool ext = RF_SPEC == 3 ? false : (RF_SPEC || (flags & 1u) != 0u); // RF_SPEC 3: plain, no render-size work + const bool lay = RF_SPEC == 3 ? false : (RF_SPEC || (flags & 3u) == 3u), alp = RF_SPEC == 1 || (RF_SPEC == 0 && (flags & 9u) == 9u); const bool lay3 = lay && (flags & 32u) == 0u, alp3 = alp && (flags & 64u) == 0u; - const bool lay3c = RF_SPEC || lay3, alp3c = RF_SPEC == 1 || alp3; // computed (RF_SPEC: always, selected below) + const bool lay3c = RF_SPEC == 3 ? false : (RF_SPEC || lay3), alp3c = RF_SPEC == 1 || alp3; // computed (RF_SPEC: always, selected below) const uint ntiles = U.ntx * ((U.He + RF_T - 1u) / RF_T); half we[5]; [unroll] for (uint k = 0; k < 5u; ++k) we[k] = asfloat16((uint16_t)(PU(17u + k) & 0xffffu)); +#if RFX_W5_ALU + const half2 hz2 = half2(0.0h, 0.0h); + const half hz = 0.0h; + const float fz = 0.0f; + const float mzv = 0.0f; +#else const uint zb = 0u - (wg_grid_ >> 31); // 0 (the grid is below 2^31), opaque to the compiler const half2 hz2 = wg_as_h2(zb); // +0 the compiler cannot see (no mad(a, b, 0) -> a * b) const half hz = hz2.x; @@ -746,13 +889,15 @@ WG_KERNEL(rr_resolve_fast) rf_mz = asfloat(0x80000000u ^ zb); #else const float mzv = 0.0f; // unused +#endif #endif Res Q = res(40u), Pr = res(96u), Gr = res(104u), Ar = res(80u), L = res(64u); Res mvr = res(88u), TH = res(112u), AH = res(144u); const Res THW = res(120u), AHW = res(152u), OUTR = res(48u), AOR = res(56u); // read once, not per tile const int Wo = (int)PU(6), Ho = (int)PU(7); -#if RF_SPEC == 2 - const bool gst = PU(16) == 1u; +#if RF_SPEC >= 2 + const bool gst = PU(16) != 0u; // 1: the texel also in the arena (WG_RR_ALIAS=0 test runs), 2: the game's texture only + const bool gonly = PU(16) == 2u; // (the fixup kernel always writes the arena texel it recomputes) const uint2 gorg = uint2(PU(39) & 0xffffu, PU(39) >> 16); #endif const uint bmap = PU(37); @@ -761,7 +906,9 @@ WG_KERNEL(rr_resolve_fast) const bool need_r = ext && (alp || lay3c); // render tile used #if RF_SPEC -#if RF_SPEC == 2 +#if RF_SPEC == 3 + const bool allin = true; // (no render tile) +#elif RF_SPEC == 2 const bool allin = and(and(lbx >= 0, lby >= 0), and(lbx + U.Wr <= (int)L.w, lby + U.Hr <= (int)L.h)); #else const bool allin = and(and(and(Pr.w >= (uint)U.Wr, Pr.h >= (uint)U.Hr), and(Ar.ox >= 0, Ar.oy >= 0)), @@ -797,7 +944,11 @@ WG_KERNEL(rr_resolve_fast) const int qc0 = G.qc0, qr0 = G.qr0, rx0 = G.rx0, ry0 = G.ry0; const int rx1 = rx0 + (int)G.nrx - 1, ry1 = ry0 + (int)G.nry - 1; const uint nqc = G.nqc, nqr = G.nqr, nrx = G.nrx, nry = G.nry; +#if RFX_W5_ALU + const bool tok = true; +#else const bool tok = U.ok && G.fits; +#endif bool slow = !tok; #if RFX_TB & 2 // T5: wave-uniform interior tests on the tile's extreme indices (SALU) @@ -850,6 +1001,9 @@ WG_KERNEL(rr_resolve_fast) #if RF_SPEC [unroll] for (uint s1 = 0; s1 < RF_QS; ++s1) { #if RFX_TB & 1 +#if RFX_W5C + if (s1 < 3u || lane < 128u) +#endif RF_QW(lane + s1 * WG_BLOCK, uint4(pf.q[2u * s1], pf.q[2u * s1 + 1u], 0u, 0u)); // T1: qin true (screens) #else uint e = lane + s1 * WG_BLOCK; @@ -919,6 +1073,9 @@ WG_KERNEL(rr_resolve_fast) if (need_r && (RF_PROF & 8) == 0) { const float K2n = U.K2n, K2x2n = U.K2x2n, E = U.E; #if RF_SPEC +#if RFX_W5C && RFX_LLDS && RFX_LR && RFX_RP == 16 && RF_SPEC == 2 + rf_w2[RF_QO + lane] = uint2(pf.r[4], pf.r[5]); +#else [unroll] for (uint s3 = 0; s3 < RF_RS; ++s3) { uint e = lane + s3 * WG_BLOCK; #if RFX_TB & 1 @@ -935,6 +1092,7 @@ WG_KERNEL(rr_resolve_fast) else rf_slot(e, tx, ty, w6, true, Pr, Ar, L, lbx, lby, K2n, K2x2n, E, mzv); } } +#endif #else for (uint e = lane; e < nrx * nry; e += WG_BLOCK) { int tx = rx0 + (int)(e % nrx), ty = ry0 + (int)(e / nrx); @@ -1021,39 +1179,66 @@ WG_KERNEL(rr_resolve_fast) const float xf = (float)x, yf = (float)y, xh = xf + 0.5f, yh = yf + 0.5f; int kx = 0, ky = 0; float mxo = 0.0f, myo = 0.0f; +#if RFX_W5_ALU + uint2 l00r = uint2(0u, 0u), l10r = uint2(0u, 0u), l01r = uint2(0u, 0u), l11r = uint2(0u, 0u); + uint2 h00r = uint2(0u, 0u), h10r = uint2(0u, 0u), h01r = uint2(0u, 0u), h11r = uint2(0u, 0u); + float4 l00 = 0, l10 = 0, l01 = 0, l11 = 0; + float4 h00 = 0, h10 = 0, h01 = 0, h11 = 0; +#else float4 l00 = 0, l10 = 0, l01 = 0, l11 = 0; +#endif RfBil lb = (RfBil)0; float w = 0.0f, w1 = 0.0f; if (ext) { // the global reads first (their latency under the work below) kx = (int)rf_rnd(mad(xh, U.sxr, -0.5f) + U.jx); ky = (int)rf_rnd(mad(yh, U.syr, -0.5f) + U.jy); if (mvr.off != 0u) { -#if RF_SPEC +#if RFX_W5C && RF_SPEC + float2 mv = rf_h2(mvr, clamp(kx, 0, U.Wr - 1) + mvr.ox, clamp(ky, 0, U.Hr - 1) + mvr.oy); +#elif RF_SPEC float4 mv = rf_h4(mvr, clamp(kx, 0, U.Wr - 1) + mvr.ox, clamp(ky, 0, U.Hr - 1) + mvr.oy); #else float4 mv = rf_texel(mvr, clamp(kx, 0, U.Wr - 1) + mvr.ox, clamp(ky, 0, U.Hr - 1) + mvr.oy, tiny); #endif mxo = mv.x * U.sxh; myo = mv.y * U.syh; } +#if !(RFX_W5_ALU && RFX_LLDS && RFX_LR) if (lay) { const float a = xf + U.jxp, b = yf + U.jyp; +#if RFX_W5_ALU + const bool sflag = and(abs(round(a) - a) < 0.5f, abs(round(b) - b) < 0.5f); +#else const bool sflag = and(abs(rf_rnd(a) + (-a)) < 0.5f, abs(rf_rnd(b) + (-b)) < 0.5f); +#endif w = select(sflag, U.TBW, U.TBW2); w1 = select(sflag, U.w1a, U.w1b); const float u = (xh + U.jxp) * U.rWe, v = (yh + U.jyp) * U.rHe; lb = rf_bil(u, v, L.w, L.h); #if RF_EARLY +#if RFX_W5_ALU + l00r = rf_h4i_raw(L, lb.xa, lb.ya); l10r = rf_h4i_raw(L, lb.xb, lb.ya); + l01r = rf_h4i_raw(L, lb.xa, lb.yb); l11r = rf_h4i_raw(L, lb.xb, lb.yb); +#else l00 = RF_RD4(L, lb.xa, lb.ya); l10 = RF_RD4(L, lb.xb, lb.ya); l01 = RF_RD4(L, lb.xa, lb.yb); l11 = RF_RD4(L, lb.xb, lb.yb); +#endif #endif } +#endif } const float hu = ((xf + mxo) + 0.5f) * U.rWe, hv = ((yf + myo) + 0.5f) * U.rHe; RfBil hb = rf_bil(hu, hv, select(lay3c, TH.w, AH.w), select(lay3c, TH.h, AH.h)); +#if !RFX_W5_ALU float4 h00 = 0, h10 = 0, h01 = 0, h11 = 0; +#endif float a00 = 0, a10 = 0, a01 = 0, a11 = 0; #if RF_EARLY if (ext && lay && lay3c) { +#if RFX_W5_ALU + h00r = rf_h4i_raw(TH, hb.xa, hb.ya); h10r = rf_h4i_raw(TH, hb.xb, hb.ya); + h01r = rf_h4i_raw(TH, hb.xa, hb.yb); h11r = rf_h4i_raw(TH, hb.xb, hb.yb); +#else h00 = RF_RD4(TH, hb.xa, hb.ya); h10 = RF_RD4(TH, hb.xb, hb.ya); h01 = RF_RD4(TH, hb.xa, hb.yb); h11 = RF_RD4(TH, hb.xb, hb.yb); +#endif } if (ext && alp && alp3c) { a00 = RF_RD1(AH, hb.xa, hb.ya); a10 = RF_RD1(AH, hb.xb, hb.ya); @@ -1096,6 +1281,7 @@ WG_KERNEL(rr_resolve_fast) // T5 (intR): kx in [kxa, kxb] (the same monotone expression as rf_geom's), rx0 = min(fxa - 1, kxa - 1) > 0 and // rx1 = max(fxb + 2, kxb + 1) < Wr - 1 (neither clamp bound), so the clamps are identities and the window // lies inside the render footprint: no check +#if !(RFX_W5C && RFX_W5_ALU && RF_SPEC == 2) if ((lay3c || alp3c) && !intR) { int lo = clamp(kx - 1, 0, U.Wr - 1), hi = clamp(kx + 1, 0, U.Wr - 1); int lo2 = clamp(ky - 1, 0, U.Hr - 1), hi2 = clamp(ky + 1, 0, U.Hr - 1); @@ -1106,9 +1292,11 @@ WG_KERNEL(rr_resolve_fast) o3x[k3] = clamp(kx + k3 - 1, 0, U.Wr - 1) - rx0; o3y[k3] = (clamp(ky + k3 - 1, 0, U.Hr - 1) - ry0) * (int)rp; } +#endif RF_TICK(4); // ---- alpha upscaling (54b.4), taps first: their work hides the history reads float ah = 0.0f, oa = 0.0f; +#if !(RFX_W5C && RFX_W5_ALU && RF_SPEC == 2) if (alp && (RF_PROF & 1) == 0) { const float px = mad(xh, U.sxr, U.jx), py = mad(yh, U.syr, U.jy); const float fxf = floor(px + (-0.5f)), fyf = floor(py + (-0.5f)); @@ -1237,9 +1425,101 @@ WG_KERNEL(rr_resolve_fast) oa = select(nanf(oa), a_up, oa); if ((flags & 16u) != 0u) oaSep = oa; else alphaC = oa; } +#endif RF_TICK(5); // ---- transparency layer (54b.3) if (lay && (RF_PROF & 2) == 0) { +#if RFX_W5_ALU && RFX_LR +#if RFX_LLDS + { + const float a = xf + U.jxp, b = yf + U.jyp; + const bool sflag = and(abs(round(a) - a) < 0.5f, abs(round(b) - b) < 0.5f); + w = select(sflag, U.TBW, U.TBW2); w1 = select(sflag, U.w1a, U.w1b); + const float u = (xh + U.jxp) * U.rWe, v = (yh + U.jyp) * U.rHe; + lb = rf_bil(u, v, (uint)U.Wr, (uint)U.Hr); + const uint lx0 = (uint)(lb.xa - rx0), lx1 = (uint)(lb.xb - rx0); + const uint ly0 = (uint)(lb.ya - ry0) * rp, ly1 = (uint)(lb.yb - ry0) * rp; + l00r = rf_w2[RF_QO + ly0 + lx0]; l10r = rf_w2[RF_QO + ly0 + lx1]; + l01r = rf_w2[RF_QO + ly1 + lx0]; l11r = rf_w2[RF_QO + ly1 + lx1]; + } +#endif + float4 cur = rf_bil4_raw(lb, l00r, l10r, l01r, l11r); + cur.xyz *= U.E; + float4 hist = cur; + if (lay3c) { + // Consume h00r..h11r + hb BEFORE the 3x3 LDS clamp loop to drop 6 live VGPRs through the peak! + float4 Hp = rf_bil4_raw(hb, h00r, h10r, h01r, h11r); + Hp.xyz *= U.fac; + // 3x3 min/max in packed FP16 BEFORE single U.E multiply (since U.E >= 0: min/max(E*L) == E*min/max(L)) + half2 mn01 = half2(65504.0h, 65504.0h), mx01 = half2(-65504.0h, -65504.0h); + half2 mn23 = mn01, mx23 = mx01; +#if RFX_TB & 2 + if (intR) { + const uint b3 = (uint)((ky - 1 - ry0) * (int)rp + (kx - 1 - rx0)); +#if RFX_W5C + const uint j_end = 3u + (wg_grid_ >> 31); + [loop] for (uint j = 0; j < j_end; ++j) { + const uint rowb = RF_QO + b3 + j * rp; + [unroll] for (uint i = 0; i < 3u; ++i) { + const uint2 lh = rf_w2[rowb + i]; + const half2 h01 = wg_as_h2(lh.x), h23 = wg_as_h2(lh.y); + mn01 = min(mn01, h01); mx01 = max(mx01, h01); + mn23 = min(mn23, h23); mx23 = max(mx23, h23); + } + } +#else + [unroll] for (uint j = 0; j < 3u; ++j) [unroll] for (uint i = 0; i < 3u; ++i) { + const uint2 lh = rf_w2[RF_QO + b3 + j * rp + i]; + const half2 h01 = wg_as_h2(lh.x), h23 = wg_as_h2(lh.y); + mn01 = min(mn01, h01); mx01 = max(mx01, h01); + mn23 = min(mn23, h23); mx23 = max(mx23, h23); + } +#endif + } else +#endif + { +#if RFX_W5C && RFX_W5_ALU && RF_SPEC == 2 + int o3x[3], o3y[3]; + [unroll] for (int k3 = 0; k3 < 3; ++k3) { + o3x[k3] = clamp(kx + k3 - 1, 0, U.Wr - 1) - rx0; + o3y[k3] = (clamp(ky + k3 - 1, 0, U.Hr - 1) - ry0) * (int)rp; + } + const uint j_end2 = 3u + (wg_grid_ >> 31); + [loop] for (uint j = 0; j < j_end2; ++j) { + const int ry_j = o3y[j]; + [unroll] for (int i = 0; i < 3; ++i) { + const uint2 lh = rf_w2[RF_QO + (uint)(ry_j + o3x[i])]; + const half2 h01 = wg_as_h2(lh.x), h23 = wg_as_h2(lh.y); + mn01 = min(mn01, h01); mx01 = max(mx01, h01); + mn23 = min(mn23, h23); mx23 = max(mx23, h23); + } + } +#else + [unroll] for (int j = 0; j < 3; ++j) [unroll] for (int i = 0; i < 3; ++i) { + const uint2 lh = rf_w2[RF_QO + (uint)(o3y[j] + o3x[i])]; + const half2 h01 = wg_as_h2(lh.x), h23 = wg_as_h2(lh.y); + mn01 = min(mn01, h01); mx01 = max(mx01, h01); + mn23 = min(mn23, h23); mx23 = max(mx23, h23); + } +#endif + } + const float4 mn = float4((float)mn01.x * U.E, (float)mn01.y * U.E, (float)mn23.x * U.E, (float)mn23.y); + const float4 mx = float4((float)mx01.x * U.E, (float)mx01.y * U.E, (float)mx23.x * U.E, (float)mx23.y); + hist = lay3 ? clamp(Hp, mn, mx) : cur; + } + const float4 T = mad(w, cur, w1 * hist); + const half4 Th = (half4)T; + if (own) { + const Res tw = THW; + if (tw.off != 0u && x < (int)tw.w && y < (int)tw.h) + wg_st64(tw.off, (rf_m24((uint)y, tw.pitch) + (uint)x) * 8u, + uint2((uint)asuint16(Th.x) | ((uint)asuint16(Th.y) << 16), + (uint)asuint16(Th.z) | ((uint)asuint16(Th.w) << 16))); + } + const float4 Ts = (float4)Th; + const float oms = 1.0f - Ts.w; + col = mad(col, oms, Ts.xyz); +#else #if !RF_EARLY l00 = RF_RD4(L, lb.xa, lb.ya); l10 = RF_RD4(L, lb.xb, lb.ya); l01 = RF_RD4(L, lb.xa, lb.yb); l11 = RF_RD4(L, lb.xb, lb.yb); @@ -1305,6 +1585,7 @@ WG_KERNEL(rr_resolve_fast) slow = or(slow, and(r == 0.0f, Ts[c2] != 0.0f)); col[c2] = r; } +#endif } if (alp && (RF_PROF & 1) == 0 && own && !slow) { const Res aw = AHW; @@ -1312,6 +1593,31 @@ WG_KERNEL(rr_resolve_fast) wg_st16(aw.off, (rf_m24((uint)y, aw.pitch) + (uint)x) * 2u, rf_f2h(ah)); // ch 1 (rf_uniforms) } } +#if RFX_W5_ALU + lin = float4(U.rE * col, alphaC); + } // tok + RF_TICK(6); + if (own) { + bool inside = (flags & 128u) == 0u || (x < Wo && y < Ho); + if (inside) { + const Res o = OUTR; + int ox = x + o.ox, oy = y + o.oy; + if (o.off != 0u && ox >= 0 && oy >= 0 && ox < (int)o.w && oy < (int)o.h) { +#if RF_SPEC >= 2 + if (!gonly) { + const half4 lh = (half4)lin; + const uint2 ow = uint2((uint)asuint16(lh.x) | ((uint)asuint16(lh.y) << 16), + (uint)asuint16(lh.z) | ((uint)asuint16(lh.w) << 16)); + wg_st64(o.off, (rf_m24((uint)oy, o.pitch) + (uint)ox) * 8u, ow); + } + if (gst) rf_gout[uint2(gorg.x + (uint)ox, gorg.y + (uint)oy)] = lin; +#else + rf_store(o, ox, oy, lin); +#endif + } + } + } +#else [unroll] for (uint c = 0; c < 3u; ++c) { lin[c] = U.rE * col[c]; slow = or(slow, and(lin[c] == 0.0f, and(col[c] != 0.0f, U.rE != 0.0f))); @@ -1339,17 +1645,21 @@ WG_KERNEL(rr_resolve_fast) int ox = x + o.ox, oy = y + o.oy; if (o.off != 0u && ox >= 0 && oy >= 0 && ox < (int)o.w && oy < (int)o.h) { const uint2 ow = uint2(rf_f2h(lin.x) | (rf_f2h(lin.y) << 16), rf_f2h(lin.z) | (rf_f2h(lin.w) << 16)); - wg_st64(o.off, (rf_m24((uint)oy, o.pitch) + (uint)ox) * 8u, ow); -#if RF_SPEC == 2 +#if RF_SPEC >= 2 + if (!gonly) wg_st64(o.off, (rf_m24((uint)oy, o.pitch) + (uint)ox) * 8u, ow); // gonly: the texture only (nothing reads the arena texel) if (gst) rf_gstore(gorg, ox, oy, ow); +#else + wg_st64(o.off, (rf_m24((uint)oy, o.pitch) + (uint)ox) * 8u, ow); #endif } +#if RF_SPEC != 3 if ((flags & 25u) == 25u) { const Res ao = AOR; int ax = x + ao.ox, ay = y + ao.oy; if (ao.off != 0u && ax >= 0 && ay >= 0 && ax < (int)ao.w && ay < (int)ao.h) wg_st32(ao.off, (rf_m24((uint)ay, ao.pitch) + (uint)ax) * 4u, asuint(oaSep)); } +#endif #else rf_store(o, x + o.ox, y + o.oy, lin); if ((flags & 25u) == 25u) { const Res ao = AOR; rf_store(ao, x + ao.ox, y + ao.oy, float4(oaSep, 0.0f, 0.0f, 0.0f)); } @@ -1357,7 +1667,8 @@ WG_KERNEL(rr_resolve_fast) } } } -#if !(RFX_NB == 1 || RFX_NB == 2 || (RFX_NB == 3 && RF_SPEC == 2)) +#endif +#if !(RFX_NB == 1 || RFX_NB == 2 || (RFX_NB == 3 && RF_SPEC >= 2)) GroupMemoryBarrierWithGroupSync(); #endif RF_TICK(7); @@ -1377,7 +1688,7 @@ void rf_fix_px(uint pix, uint We) { const int x = (int)(pix % We), y = (int)(pix / We); rr_resolve_px(x, y); - if (PU(16) == 1u) { + if (PU(16) != 0u) { // 1 (also in the arena) or 2 (the texture only) const Res o = res(48u); const bool inside = (PU(8) & 128u) == 0u || (x < (int)PU(6) && y < (int)PU(7)); if (inside && o.off != 0u && x < (int)o.w && y < (int)o.h) diff --git a/clean/rr_f/kernels/rr_resolve_fast_math.hlsli b/clean/rr_f/kernels/rr_resolve_fast_math.hlsli index e2495ea5..bb2f844f 100644 --- a/clean/rr_f/kernels/rr_resolve_fast_math.hlsli +++ b/clean/rr_f/kernels/rr_resolve_fast_math.hlsli @@ -192,7 +192,11 @@ float rf_accx(float w, float v, float acc) // ---- conversions --------------------------------------------------------------------------------------------------- // f32 -> binary16 bits, round to nearest even (= wg_f2h for every f32): the driver channel's hardware conversion // (op 0x1C, immediate 1: v_cvt_f16_f32 in the default rounding mode; any control flow). +#if defined(RFX_W5_ALU) && RFX_W5_ALU +uint rf_f2h(float x) { return (uint)asuint16((half)x); } +#else uint rf_f2h(float x) { return wg_ags_call(0x1Cu, 0u, 1u, asuint(x), 0u) & 0xffffu; } +#endif float rf_h(float x) { return f16tof32(rf_f2h(x)); } // = H(x) // Unorm read code / maxc rounded once to f32 (= cv / wg_div for every code 0 <= code <= maxc of the formats): diff --git a/clean/rr_f/runtime/README.md b/clean/rr_f/runtime/README.md index e04320d5..3f7ba921 100644 --- a/clean/rr_f/runtime/README.md +++ b/clean/rr_f/runtime/README.md @@ -105,9 +105,112 @@ original. ## Exact tricks of round 10 (same bits in every mode) - body (clean/rr-f-s10-body): D4-F / D4-R of the relaxed body (RR_B32) run on four groups per window (`RR_B32G4`, `WG_RR_EROUTE` build knob). +- wave64 forms (clean/rr-f-wave4b, same bits, design D of `kernels/rr_block_c32.hlsl`: one wave64 = two token tiles of the same window, + every weight / bias / key-value fragment loaded once for both): `head.fast.rx.w64` (`head/dx12/rr_d0r_w64.hlsl`) and the relaxed + body's `stagee.D1-R.w64` / `stagee.D1-F.w64` / `stagee.E1.w64` (`kernels/rr_block_c64_w64.hlsl`). `WG_RR_W4B` at creation (hex mask, + default `0xf`; bit 0 head, bit 1 D1-R, bit 2 D1-F, bit 3 E1); a clear bit selects the wave32 kernel of the `WG_RR_W4` mask, so + `WG_RR_W4B=0` is the wave-4 frame (A/B and byte checks); bit 0 does not depend on `WG_RR_W4` bit 0, bits 1-3 need its bit 1 (the + default). Paired timing at 1440p Q (layer / plain, `fast`): `0xf` -178 / -175 us, bit 0 -71 / -71, bit 1 -45 / -44, bit 2 -42 / -39, + bit 3 -25 / -23; all four byte-identical. The exact body (mode exact) and the dataflow forms keep wave32. +- wave-4c levers (clean/rr-f-wave4c, same bits by construction, GPU timing pending): the relaxed body's wide stages in wave64 + (`kernels/rr_block_wide_w64.hlsl`: `stagee.{E2, D2-F, D2-R, E3, D3-F, D3-R, D4-F, D4-R}.w64`, wave `2 g + tw` holds the two token tiles + of the wave32 waves `4 g + 2 tw`, `4 g + 2 tw + 1`; E4 / E5 stay wave32), `stagee.IN0.w4c` / `stagee.D0-F.w4c` (the wave64 form of + `rr_block_c32.hlsl` with the deferred Q8 decision `RR_DEFER`, the Dd / Wo weight prefetch and, for IN0, the interleaved front-end + MLP), and `output.fast.f_ph1w` (OFX_PH1W alone, no OFX_LD3 / OFX_QST). `WG_RR_W4C` at creation (hex mask, default `0x19` = bits 0, 3, 4, -29 us at 1440p Q layer and plain; bits 1, 2, 5 are opt-in, they measured +23, +6 / -1 and +8 us; bit 0 + level 2, bit 1 level 3, bit 2 level-4 decoder, bit 3 IN0, bit 4 D0-F (needs `WG_RR_W4` bit 1), bit 5 `output.fast.f_ph1w` when + `WG_RR_W4` bit 2 is off); `WG_RR_W4C=0` is the wave-4b frame. The exact body and the dataflow forms keep wave32. +- wave-5b/5c levers (clean/rr-f-wave5c, **rr_mode fast only, not byte-identical**; network/tail arithmetic relaxed while keeping temporal + history/reprojection logic exact): `head.fast.rx.f.w64` (`head/dx12/rr_d0r_w64.hlsl`, `RR_HFAST_ALU=2`: bounded f32 ALU head tail, + per-head Q8 cutting VGPR 96->84, branchless Q8, no dead CANDW store, exact Cnet history), `output.fast.w5` (`OFX_W5_ALU=1` on + `output.fast.f0` base) & `output.fast.w5c` (`OFX_W5C=1, OFX_PH1W=1, OFX_LD3=1`: rolled `of_reproj` dropping VGPR 74->55 [+50% occ], + 7.2 KB LDS, BAR 4->2, zero fallback pass/barrier when tile fits), `resolve.fast2/3.w5` (`RFX_W5_ALU=1`) & `resolve.fast2/3.w5c` + (`RFX_W5C=1`: `QN=28`/`QO=896`, wave-uniform `s1==3` skip; `fast2` adds `RFX_RP=16 -> RF_RS=1`, 12.8 KB LDS [5 WGs/CU], `RFX_LLDS=1` + reading layer bilinear taps from LDS, `RFX_QLD=1`, 32-bit MV load, rolled 3x3), `input.features.w5c` (`input.features.lite` wave64 + + `RR_IN_SRV_ONLY=1`: bit-identical to `input.features.lite`, CPU-guarded direct SRV, -17% INST / -65% VLD / -41% IMG, 57 VGPR, 5120 B LDS), + `stagef.{IN0, D0-F, E1, D1-F, D1-R, E2, D2-F, D2-R}.w64` & `stagef.{E3, D3-F, D3-R, E4, E5, D4-F, D4-R}` (pure f32 `RR_B32=1` + + branchless clamped `RR_Q8_FAST=1`, `RR_B32G4=1` + `RR_PRE=14` at L4/L5 eliminating all LDS/scratch spills, convergent merge guard + `NAR=0`), and `pool.main.w5` (`RR_POOL_W5=1`: hardware f32 mad/mul with exact tables, 0 FP64, -69% INST, bit-identical to `quarter_pool`). + `WG_RR_W5` at creation (hex mask, default `0xaff` = every bit but 8 and 10: bit 0 head, bit 1 output.w5, bit 2 resolve.w5, bit 3 input.w5c, bit 4 body L2, + bit 5 body L0, bit 6 body L1, bit 7 body L3, bit 8 body L4/L5 [opt-in, see "Wave 5c results"], bit 9 pool.main.w5, bit 10 resolve.w5c [opt-in], + bit 11 output.w5c); effective + mask is 0 for exact and accurate (all 24 kernels are VG_OUT alternatives: only Fast compiles them in Pass 1), `WG_RR_W5=0` reproduces + the wave-4c frame. - tail (clean/rr-f-s10-tail): the resolve reads the exp2 table from the arena instead of copying it into LDS; 24-bit multiplies for addressing in the output stage; the main output's store is folded into resolve.fast2 / resolve.fixup (`WG_RR_FUSESTORE=0` at creation brings the separate store pass back). Also used by the exact-resolve (`_X`) kernels of mode exact. +- tail (clean/rr-f-300, same bits): the plain resolve (no layer, opacity or alpha upscaling) takes `resolve.fast3` / `resolve.fast3.x` + (`RF_SPEC 3`: no render-size reads, no format switches; 43 / 55 VGPRs instead of resolve.fast's 162 / 191) and folds its store like + resolve.fast2 (`WG_RR_RES3=0` at creation: resolve.fast / rt.store as before); with the store folded the fast resolve no longer also + writes the main output's arena texels (kept for `WG_RR_ALIAS=0` test runs; `WG_RR_RESARENA=1` brings it back). + +## Wave 5c results (clean/rr-f-wave5c, RX 9070 XT, D3D12, rr_mode fast; GPU-verified) + +Default `WG_RR_W5=0xaff` (every bit but 8 and 10). Timings are the runtime's GPU time per frame (`WG_RR_TIMING_WINDOW`) on structured, +temporally coherent synthetic scenes through `rr_test_host` (plain: `perf`; layer: `perfcap`), paired ABBA, one host process per run +(warm-up run + measured run), no `dxc` / `clang++` running. A = the Wave 4c build (`13a53c9`), B = this build with no `WG_RR_W5` set. + +1440p Quality (1707x960 -> 2560x1440), 6 ABBA rounds (12 runs per side), median: + +| | Wave 4c | Wave 5c `0xaff` | delta | speedup | +|---|---|---|---|---| +| plain | 6.369 ms | **5.760 ms** | -609 us | x1.106 | +| layer | 6.600 ms | **5.963 ms** | -637 us | x1.107 | + +Per-stage GPU ms at 1440p Q (mean of the measured runs; the body is one stage, the host has no per-level hook): + +| | pre | input | body | head | output | resolve | total | +|---|---|---|---|---|---|---|---| +| plain, Wave 4c | 0.044 | 0.548 | 3.817 | 1.003 | 0.730 | 0.228 | 6.370 | +| plain, Wave 5b `0x077` | 0.043 | 0.550 | 3.558 | 0.780 | 0.666 | 0.212 | 5.810 | +| plain, Wave 5c `0xaff` | 0.045 | 0.536 | 3.554 | 0.790 | 0.639 | 0.200 | 5.764 | +| layer, Wave 4c | 0.044 | 0.563 | 3.829 | 1.003 | 0.727 | 0.444 | 6.610 | +| layer, Wave 5b `0x077` | 0.045 | 0.564 | 3.570 | 0.779 | 0.661 | 0.413 | 6.034 | +| layer, Wave 5c `0xaff` | 0.046 | 0.565 | 3.548 | 0.787 | 0.634 | 0.398 | 5.978 | + +Scaling (medians of 3 ABBA rounds, `WG_RR_W5=0xaff`; the 1440p Q row is the 6-round default-build figure above): + +| cell | plain 4c -> 5c (ms) | delta | layer 4c -> 5c (ms) | delta | +|---|---|---|---|---| +| 1080p Q (1280x720 -> 1920x1080) | 3.699 -> 3.343 | -356 us (x1.107) | 3.806 -> 3.446 | -360 us (x1.104) | +| 1440p Perf (1280x720 -> 2560x1440) | 4.144 -> 3.742 | -402 us (x1.107) | 4.328 -> 3.894 | -435 us (x1.112) | +| 1440p Q (1707x960 -> 2560x1440) | 6.369 -> 5.760 | -609 us (x1.106) | 6.600 -> 5.963 | -637 us (x1.107) | +| 1440p DLAA (2560x1440 -> 2560x1440) | 12.727 -> 11.632 | -1095 us (x1.094) | 13.104 -> 12.014 | -1090 us (x1.091) | +| 4K Q (2560x1440 -> 3840x2160) | 14.032 -> 12.794 | -1239 us (x1.097) | 14.609 -> 13.368 | -1241 us (x1.093) | + +Per-bit timing at 1440p Q (3 ABBA rounds against `WG_RR_W5=0` in the same process; single bit, then cumulative / leave-one-out), us plain / layer: +bit 3 -12 / -2, bit 7 -40 / -41, bit 8 -52 / -43, bit 9 -4 / -9, bit 10 -15 / -1, bit 11 -96 / -96 on their own; `0x077` -537 / -562, +`0x07f` -552 / -567, `0x0ff` -584 / -608, `0x1ff` -626 / -647, `0x3ff` -639 / -660, `0x7ff` -637 / -662, `0xfff` -649 / -672, +`0xeff` -607 / -634, `0xaff` -607 / -635. Bit 10 is timing-neutral inside the default (`0xaff` vs `0xeff`, 6 rounds: +1.5 / +5.9 us and +-1.9 / +0.2 us in the two orders), so it stays opt-in. + +Exactness against the Wave 4c build (same synthetic scenes, 12 small and medium configurations at binary16 and binary32 outputs, plus +1440p Q and 1080p Q, plain and layer, six frames with a reset on frame 3; every output and every history / arena byte): +- `exact`, `accurate`, and `fast` with `WG_RR_W5=0`: 0 byte differences everywhere, except one known pre-existing 4-byte difference in the + work (scratch) region on 1440p Q reset-frame sequences (two atomic-append slots swapped; it also appears between two runs of the + Wave 4c build itself, never in an output or a history buffer). +- Bits 3, 4, 5, 6, 7 and 9 are bit-identical to the Wave 4c frame (0 byte differences; `0x008`, `0x018`, `0x020`, `0x040`, `0x080`, + `0x0f8` and `0x200` each checked against it on a 480x270 scene). Bit 11 writes the same bytes as bit 1 (`0xaff` vs `0x0ff`: 0 byte + differences on every scene above), so the output stage differs from Wave 4c only by the Wave 5b `output.fast.w5` arithmetic. Bit 10 at + 1440p Q is bit-identical to Wave 4c on layer scenes and 81.8 dB (tone-mapped) from it on plain scenes. +- **Bit 8 (`0x100`, `stagef.{E4, E5, D4-F, D4-R}`) is not bit-identical** and is therefore opt-in: deterministic (two runs match), but + 34.7 .. 38.5 dB tone-mapped PSNR from the Wave 4c fast frame on a small scene, 37.0 / 36.9 dB on the 1440p Q reset frames and 37.2 dB + (steady minimum, plain) against `accurate`, which fails the reset (>= 72 dB) and steady (>= 38.5 dB plain) gates. +- With no `WG_RR_W5` set, the output is byte-identical to `WG_RR_W5=0xaff`. + +Quality of the default against `accurate` (1440p Q, six frames, reset on frames 0 and 3; tone map `x / (1 + x)` on the RGB, negatives +clamped to 0; PSNR over the RGB with peak 1; SSIM on the Rec. 709 luma of the tone-mapped image, 8x8 box window, L = 1, k1 = 0.01, +k2 = 0.03). 0 NaN / Inf in every output and history buffer of every frame (also at 1440p DLAA, with `0xaff`, `0xeff` and `0xfff`): + +| | reset frames | steady frames (min .. max) | SSIM (steady min) | +|---|---|---|---| +| plain | 72.85 / 72.85 dB | 38.71 .. 38.87 dB | 0.9551 | +| layer | 73.91 / 73.93 dB | 43.15 .. 43.54 dB | 0.9763 | + +Against the Wave 4c `fast` frame: plain 72.85 dB on the reset frames, 38.76 .. 38.87 steady; layer 73.91 dB, 43.12 .. 43.53. These are +the numbers of Wave 5b `0x077` (bit 3, 7, 9, 11 add no output difference). At 1080p Q the plain steady frames are 38.4 .. 39.2 dB and +the layer ones 42.8 .. 43.9 dB against `accurate`, the same as the Wave 4c fast frame itself (38.39 dB on one plain frame); at 1440p DLAA +Wave 5c is 72.5 .. 72.9 dB from the Wave 4c fast frame on the reset frames and 39.9 .. 40.2 dB (plain) / 44.3 .. 45.0 dB (layer) on the +steady ones; the Wave 4c fast frame is itself 42.8 dB (frame 0) and 38.6 .. 39.7 dB (the others) from `accurate` there, Wave 5c the same. ## Round 11, tail (clean/rr-f-s11-tail) diff --git a/clean/rr_f/runtime/src/rr_pack.cpp b/clean/rr_f/runtime/src/rr_pack.cpp index 2e973ac7..b8682852 100644 --- a/clean/rr_f/runtime/src/rr_pack.cpp +++ b/clean/rr_f/runtime/src/rr_pack.cpp @@ -490,6 +490,7 @@ bool pack_wgcm(const std::vector& groups, Model* out, std::string* er for (auto& k : kHk) sc[std::string("head.") + k[0]] = bits16(*KS(k[1])); sc["head.k_g"] = bits16(*T(Hd, "g")); sc["head.k_s1"] = fbits(s1); + sc["head.k_s2"] = fbits(s2); // clean/rr-f-wave5: head.fast.rx.f.w64 computes rho = tanh(f32(y17 s2)) (no head.rho gather) sc["K1"] = bits32(*T(Hd, "K1")); } // input stage (52): front-end MLP fragments, dither, read tables diff --git a/clean/rr_f/runtime/src/rr_runtime.cpp b/clean/rr_f/runtime/src/rr_runtime.cpp index 12b6fcd9..86829b68 100644 --- a/clean/rr_f/runtime/src/rr_runtime.cpp +++ b/clean/rr_f/runtime/src/rr_runtime.cpp @@ -296,6 +296,44 @@ enum Kid { // clean/rr-f-a14-frame E24: read-only cache warm-up (rr_touch.hlsl) K_TOUCH = K_DXSTAGE0 + 15, K_BLKCOPY, // E25 step 2: argument block upload ring -> arena (root SRV 6) + // clean/rr-f-300 R2: the fast resolve specialised for the plain configuration (RF_SPEC 3: no layer / opacity / alpha + // upscaling; same bits as resolve.fast), hardware elementary functions (Fast) and correctly rounded (.x) + K_RES_FAST3, K_RES_FAST3_X, + // clean/rr-f-wave4: the kernels before the wave-4 levers, for the A/B mask WG_RR_W4 (bit 0 = head R4.1, bit 1 = body R4.2, + // bit 2 = output R4.3): head.fast.rx0, output.fast.f0, stagee0.{E1, D1-F, D0-F} (RR_OVL 0); opt-in kernels + K_HEAD_FAST_RX0, K_OUT_FAST_F0, K_ESTAGEB0, + // clean/rr-f-wave4b: the wave64 forms (design D of rr_block_c32.hlsl: one wave64 = two token tiles of the same window, shared + // weight loads; the same bits) for the A/B mask WG_RR_W4B (default 0xf): head.fast.rx.w64 (bit 0), stagee.{D1-R, D1-F, E1}.w64 + // (bits 1, 2, 3). Bit 0 selects the wave64 head whatever WG_RR_W4 bit 0 says (it replaces the wave32 head of both); bits 1-3 need WG_RR_W4 bit 1 + // (the body lever); WG_RR_W4B=0 reproduces the wave-4 frame. Paired timing at 1440p Q (layer / plain): 0xf -178 / -175 us, bit 0 -71 / -71, bit 1 -45 / -44, bit 2 -42 / -39, bit 3 -25 / -23. + K_HEAD_FAST_RXW = K_ESTAGEB0 + 3, K_ESTAGEW0, + // clean/rr-f-wave4c: the wave64 forms of the wide stages (rr_block_wide_w64.hlsl; E2, D2-F, D2-R, E3, D3-F, D3-R, D4-F, D4-R, + // the same bits as stagee.), IN0 / D0-F with the deferred Q8 decision, the Dd / Wo weight prefetch and (IN0) the interleaved + // front-end MLP (rr_block_c32.hlsl wave64, the .w4c kernels), output.fast.f with OFX_PH1W alone; for the A/B mask WG_RR_W4C + // (default 0x19): bit 0 level 2 (E2, D2-F, D2-R), bit 1 level 3 (E3, D3-F, D3-R), bit 2 level 4 decoder (D4-F, D4-R), bit 3 + // IN0.w4c, bit 4 D0-F.w4c, bit 5 output.fast.f_ph1w (Fast mode, when WG_RR_W4 bit 2 is off). All bits are byte-identical; paired ABBA + // at 1440p Q, 8 rounds (layer / plain, us vs WG_RR_W4C=0): bit 0 -20 / -20, bit 1 +23 / +24, bit 2 +6 / -1, bit 3 -6 / -8, bit 4 -1 / -2, + // bit 5 +8 / +9, all six +7 / +1; 0x19 (bits 0, 3, 4) -29 / -29 (8 of 8 rounds). Bits 1, 2 and 5 are opt-in (not wins). + K_ESTAGEC0 = K_ESTAGEW0 + 3, + K_IN0_W4C = K_ESTAGEC0 + 8, K_D0F_W4C, K_OUT_FAST_F_PH1W, + // clean/rr-f-wave5b/5c (Fast mode only; WG_RR_W5, default 0xaff = all bits but 8 and 10, bits 0 .. 11; GPU-verified on RX 9070 XT D3D12): + // bit 0 (0x001): head.fast.rx.f.w64 (RR_HFAST_ALU=2: bounded f32 ALU head tail, per-head Q8 96->84 VGPR, branchless Q8, exact Cnet history) + // bit 1 (0x002): output.fast.w5 (OFX_W5_ALU=1 on output.fast.f0 base, zero fallback pass/barrier when tile fits) + // bit 2 (0x004): resolve.fast2.w5 / resolve.fast3.w5 (RFX_W5_ALU=1, FP16 3x3 clamp before E, zero fixup pass when RF_SPEC 2/3 & fits) + // bit 3 (0x008): input.features.w5c (input.features.lite wave64 + RR_IN_SRV_ONLY=1: bit-identical to input.features.lite, CPU-guarded direct SRV) + // bits 4..8 (0x010 L2, 0x020 L0, 0x040 L1, 0x080 L3, 0x100 L4/L5): stagef.* (pure f32 RR_B32=1 + branchless clamped RR_Q8_FAST=1, NAR=0, 0 spills) + // bits 4..7 are byte-identical to the wave-4c frame; bit 8 (0x100) is OPT-IN, not byte-identical: 34.7 .. 38.5 dB tone-mapped PSNR vs the wave-4c + // fast frame, 37 .. 41 dB on reset frames (below the 72 dB gate) and 37.2 dB steady plain (below 38.5), for -43 / -52 us at 1440p Q + // bit 9 (0x200): pool.main.w5 (RR_POOL_W5=1: hardware f32 mad/mul with exact tables, 0 FP64, bit-identical to quarter_pool) + // bit 10 (0x400): resolve.fast2.w5c / resolve.fast3.w5c (RFX_W5C=1: QN=28/QO=896, RFX_RP=16 -> RF_RS=1, 12.8 KB LDS, RFX_LLDS=1, RFX_QLD=1, 32-bit MV, rolled 3x3) + // OPT-IN: clean but timing-neutral (+/-2 us paired at 1440p Q plain and layer), so not in the default + // bit 11 (0x800): output.fast.w5c (OFX_W5_ALU=1, OFX_W5C=1, OFX_PH1W=1, OFX_LD3=1: rolled of_reproj 74->55 VGPR, 7.2 KB LDS, BAR 4->2). + // All 24 kernels sit in VG_OUT's alternatives, so only Fast mode compiles them in Pass 1 and dispatches them; Accurate and Exact never start them. + K_HEAD_FAST_RXF = K_OUT_FAST_F_PH1W + 1, + K_OUT_FAST_W5, K_RES_FAST2_W5, K_RES_FAST3_W5, K_IN_FEAT_W5C, + K_FSTAGE0, + K_POOL_MAIN_W5 = K_FSTAGE0 + 15, + K_RES_FAST2_W5C, K_RES_FAST3_W5C, K_OUT_FAST_W5C, K_COUNT }; const char* const kKernelNames[] = { @@ -321,7 +359,16 @@ const char* const kKernelNames[] = { "staged.D3-F", "staged.D3-R", "staged.D2-F", "staged.D2-R", "staged.D1-F", "staged.D1-R", "staged.D0-F", "stagedx.IN0", "stagedx.E1", "stagedx.E2", "stagedx.E3", "stagedx.E4", "stagedx.E5", "stagedx.D4-F", "stagedx.D4-R", "stagedx.D3-F", "stagedx.D3-R", "stagedx.D2-F", "stagedx.D2-R", "stagedx.D1-F", "stagedx.D1-R", "stagedx.D0-F", - "rt.touch", "rt.blkcopy"}; + "rt.touch", "rt.blkcopy", "resolve.fast3", "resolve.fast3.x", + "head.fast.rx0", "output.fast.f0", "stagee0.E1", "stagee0.D1-F", "stagee0.D0-F", + "head.fast.rx.w64", "stagee.D1-R.w64", "stagee.D1-F.w64", "stagee.E1.w64", + "stagee.E2.w64", "stagee.D2-F.w64", "stagee.D2-R.w64", "stagee.E3.w64", "stagee.D3-F.w64", "stagee.D3-R.w64", + "stagee.D4-F.w64", "stagee.D4-R.w64", "stagee.IN0.w4c", "stagee.D0-F.w4c", "output.fast.f_ph1w", + "head.fast.rx.f.w64", "output.fast.w5", "resolve.fast2.w5", "resolve.fast3.w5", "input.features.w5c", + "stagef.IN0.w64", "stagef.D0-F.w64", "stagef.E1.w64", "stagef.D1-F.w64", "stagef.D1-R.w64", + "stagef.E2.w64", "stagef.D2-F.w64", "stagef.D2-R.w64", + "stagef.E3", "stagef.D3-F", "stagef.D3-R", "stagef.E4", "stagef.E5", "stagef.D4-F", "stagef.D4-R", + "pool.main.w5", "resolve.fast2.w5c", "resolve.fast3.w5c", "output.fast.w5c"}; static_assert(sizeof(kKernelNames) / sizeof(kKernelNames[0]) == K_COUNT, "kKernelNames and Kid disagree"); // clean/rr-f-flow: kernels only the opt-in variants dispatch (exact / strict / debug head, body steps X and Z, // WG_RR_PATHS=verified); the default frame (head fast, body E, fast paths) never uses them @@ -330,6 +377,7 @@ Kid input_features_kernel(int mode) { return mode == 1 ? K_IN_FEAT_LITE : (mode bool kernel_opt_in(int k) { if (k >= K_IN0 && k <= K_D0F) return true; // body step X if (k >= K_ZSTAGE0 && k < K_ZFIX0 + 15) return true; // body step Z and its fixup + if (k >= K_HEAD_FAST_RX0 && k < K_HEAD_FAST_RXW) return true; // clean/rr-f-wave4: the A/B kernels of WG_RR_W4 switch (k) { case K_HEAD: case K_HEAD_DEBUG: case K_HEAD_WPL: case K_HEAD_FIXUP: case K_HEAD_STRICT: case K_OUT_MAIN: case K_OUT_POOL: case K_RES_MAIN: case K_RES_POOL: @@ -348,14 +396,22 @@ int variant_group(int k, bool* alt) { *alt = false; if (k == K_HEAD_FAST) return VG_HEAD; if (k == K_HEAD_FAST_RX) { *alt = true; return VG_HEAD; } + if (k == K_HEAD_FAST_RXW) { *alt = true; return VG_HEAD; } // clean/rr-f-wave4b: the wave64 relaxed head + if (k >= K_ESTAGEW0 && k < K_ESTAGEW0 + 3) return VG_BODY; // clean/rr-f-wave4b: the relaxed body's level-1 stages in wave64 + if (k >= K_ESTAGEC0 && k <= K_D0F_W4C) return VG_BODY; // clean/rr-f-wave4c: the wide stages in wave64, IN0 / D0-F .w4c + if (k == K_OUT_FAST_F_PH1W) { *alt = true; return VG_OUT; } // clean/rr-f-wave4c: Fast mode's output.fast.f with OFX_PH1W alone + // clean/rr-f-wave5b/5c: all 24 Fast-only kernels sit in VG_OUT's alternatives (the bit only Fast mode has, rr_modes.cpp + // modes_self_check): kernel_in_default_frame() is true for them in a runtime that starts in Fast and false otherwise, so Accurate + // and Exact compile them in the second pass (a later switch to Fast) and never in their first + if (k >= K_HEAD_FAST_RXF && k < K_COUNT) { *alt = true; return VG_OUT; } if (k >= K_ESTAGE0 && k < K_ESTAGE0 + 15) return VG_BODY; if (k >= K_DSTAGE0 && k < K_DSTAGE0 + 15) return VG_BODY; // clean/rr-f-s12-dflow: the relaxed body's dataflow form if (k >= K_ESTAGEX0 && k < K_ESTAGEX0 + 15) { *alt = true; return VG_BODY; } if (k >= K_DXSTAGE0 && k < K_DXSTAGE0 + 15) { *alt = true; return VG_BODY; } // clean/rr-f-s13x-x-flow: the exact body's if (k == K_OUT_PREP || k == K_OUT_FAST || k == K_OUT_FALLBACK || k == K_OUT_PREPU) return VG_OUT; if (k >= K_OUT_PREP_F && k <= K_OUT_PREPU_F) { *alt = true; return VG_OUT; } - if (k == K_RES_FAST || k == K_RES_FAST2) return VG_RES; - if (k == K_RES_FAST_X || k == K_RES_FAST2_X) { *alt = true; return VG_RES; } + if (k == K_RES_FAST || k == K_RES_FAST2 || k == K_RES_FAST3) return VG_RES; + if (k == K_RES_FAST_X || k == K_RES_FAST2_X || k == K_RES_FAST3_X) { *alt = true; return VG_RES; } return -1; } // kernel k belongs to the default frame of the variant set vbits (no opt-in kernel, and its variant is the one in use) @@ -1068,6 +1124,31 @@ void append_tables(Model* m) { add("in.EXPT", expt, sizeof expt); } +// clean/rr-f-wave5: the head's s2 (page 55.3, the f32 bits as scalar head.k_s2) for a model file packed before the scalar existed: the +// fast head's ALU form (head.fast.rx.f.w64) computes rho = tanh(f32(v s2)) instead of reading head.rho. rho[v] = tanh(f32(v s2)) for every +// binary16 v, so s2 = atanh(rho[v]) / v at a small normal v: v = 0.0625 (entry 0x2c00: 16 atanh(rho)), else 0.015625 (0x2400), 2^-7, 2^-8 +// when tanh already saturates (|rho| > 0.99). Same container bytes, no new blob; the derived value only feeds the relaxed head. +void derive_head_s2(Model* m) { + if (!m->ok || m->has_scalar("head.k_s2")) return; + const uint64_t off = m->blob("head.rho"); + if (off == UINT64_MAX || off + 65536ull * 4ull > m->image.size()) return; + static const uint32_t kIdx[] = {0x2c00u, 0x2400u, 0x2000u, 0x1c00u}; + float s2 = 0.0f; + for (uint32_t idx : kIdx) { + float r; + memcpy(&r, m->image.data() + off + 4ull * idx, 4); + const float v = ldexpf(1.0f + (float)(idx & 0x3ffu) / 1024.0f, (int)((idx >> 10) & 31u) - 15); + if (std::isfinite(r) && fabsf(r) > 0.0f && (fabsf(r) <= 0.99f || idx == kIdx[3])) { + s2 = atanhf(fminf(fmaxf(r, -0.999999f), 0.999999f)) / v; + break; + } + } + uint32_t u; + memcpy(&u, &s2, 4); + m->scalars["head.k_s2"] = u; + log_line(fmt("model: head.k_s2 derived from head.rho (s2 = %g; the model file has no such scalar)", (double)s2)); +} + void init_once() { Runtime& rt = R(); if (rt.init_done) return; @@ -1098,6 +1179,7 @@ void init_once() { rt.model_err = std::string("model lacks the symbol ") + n; log_line(std::string("model not loaded: lacks the symbol ") + n); } + if (rt.model.ok) derive_head_s2(&rt.model); // clean/rr-f-wave5: head.k_s2 for models packed before it existed if (rt.model.ok) log_line(fmt("model loaded: %s (%s, %zu bytes device image, %zu blobs, %zu scalars, %s%s, sha256 %s%s)", rt.model.path.c_str(), rt.model.kind == "wgcm" ? "converter output, groups' keyed digests checked" @@ -1659,6 +1741,13 @@ struct Instance { bool fuse_store = true; // clean/rr-f-s10-tail (WG_RR_FUSESTORE=0 at creation: rt.store as before): with the // RF_SPEC 2 resolve and a binary16 main output, resolve.fast2 / resolve.fixup store // it into the game's texture themselves (rt.store's conversion, same bits) + bool res3 = true; // clean/rr-f-300 R2 (WG_RR_RES3=0 at creation: the plain configuration on resolve.fast as + // before): the plain resolve (no layer / opacity / alpha upscaling, binary16 x4 output) + // takes resolve.fast3 (RF_SPEC 3, same bits) and, like the RF_SPEC 2 resolve, stores the + // main output into the game's texture itself (no rt.store) + bool res_skip_arena = true; // clean/rr-f-300 R1b (WG_RR_RESARENA=1 at creation: the old behaviour): with the store fused + // the fast resolve does not also write the main output's arena texels (nothing reads them + // but the WG_RR_ALIAS=0 test runs, which keep the write) int ringest = 4; // clean/rr-f-s13x-x-flow: the body stage the resolve's ingest joins (kRIngestStage / // kRIngestStageFast by mode, apply_dflow; WG_RR_RINGEST=<0..15>, A/B) bool host_sched = true; // clean/rr-f-s9-host (WG_RR_HOSTSCHED=0 at creation: the base recording order): @@ -1677,6 +1766,10 @@ struct Instance { // deferred switch is logged once uint64_t hold_modes = 0; int deferred_logged = -1; + unsigned w4 = 2; // WG_RR_W4 at creation (hex mask, default 2): clean/rr-f-wave4 levers (bit 0 head R4.1, bit 1 body R4.2, bit 2 output R4.3); 0 bits select the *0 kernels. Paired timing at 1440p Q: only bit 1 wins (-78 us layer / -83 us plain); bit 0 is neutral (+2 us), bit 2 loses (+10 us output) + unsigned w4b = 0xf; // WG_RR_W4B at creation (hex mask, default 0xf): clean/rr-f-wave4b wave64 forms on (bit 0 head.fast.rx.w64, bit 1 stagee.D1-R, bit 2 stagee.D1-F, bit 3 stagee.E1); 0 bits select the wave32 kernels of WG_RR_W4 + unsigned w4c = 0x19; // WG_RR_W4C at creation (hex mask, default 0x19 = bits 0, 3, 4): clean/rr-f-wave4c levers on (bit 0 wide stages of level 2 in wave64, bit 1 level 3, bit 2 level 4 decoder D4-F / D4-R, bit 3 stagee.IN0.w4c, bit 4 stagee.D0-F.w4c, bit 5 output.fast.f_ph1w); 0 bits select the kernels of WG_RR_W4 / WG_RR_W4B. Same bits as without (byte-identical kernels, written arguments in rr_block_wide_w64.hlsl / rr_block_c32.hlsl / rr_output_fast.hlsli) + unsigned w5 = 0xaff; // WG_RR_W5 at creation (hex mask, default 0xaff = every bit but 8 and 10, which are opt-in): clean/rr-f-wave5b/5c levers, rr_mode fast ONLY (forced off for exact and accurate: w5_effective()): bit 0 head.fast.rx.f.w64, bit 1 output.fast.w5, bit 2 resolve.fast2/3.w5, bit 3 input.features.w5c (wave64 SRV-only, bit-identical to input.features.lite), bit 4 body L2, bit 5 body L0, bit 6 body L1, bit 7 body L3 (stagef.{E3,D3-F,D3-R}), bit 8 body L4/L5 (stagef.{E4,E5,D4-F,D4-R}, RR_B32G4=1, RR_PRE=14, 0 spills), bit 9 pool.main.w5 (0 FP64), bit 10 resolve.fast2/3.w5c (QN=28, RP=16, LLDS, QLD1, 32-bit MV, rolled 3x3), bit 11 output.fast.w5c (rolled of_reproj 55 VGPR + PH1W + LD3); 0 selects the wave-4c frame (13a53c9). unsigned vbits = 0; // variant set (mode_vbits, per-knob overrides applied): head relaxed / body exact / output Fast / resolve exact bool infold = false; // stagee.IN0 computes the front-end MLP itself (relaxed body in a build with kRrInFold) uint32_t twin = kTimingWindow; // WG_RR_TIMING_WINDOW=N at creation: frames per timing log line (benches) @@ -2047,6 +2140,12 @@ void plan_frame(const Instance& in, const FrameShape& f, WorkPlan* P, FrameBufs* // split | copy, copy = exact through f32 copies, any other value = exact; clean/rr-f-s9-inx, owner 2026-10-28: the // history samples stay exact in the default), the front-end MLP variant and its fold. Returns the overrides text of the // log line. Creation-only items (history size, WG_RR_HSCALE, the arena) are not touched. +// clean/rr-f-wave5: the WG_RR_W5 levers that run: only in rr_mode fast (in->mode == MODE_FAST, the one mode whose variant set has the +// VG_OUT bit, rr_modes.cpp) and with the output variant of Fast in use; 0 for exact and accurate whatever WG_RR_W5 says, so +// those modes dispatch exactly the kernels of wave 4c +unsigned w5_effective(const Instance* in) { + return (in->mode == MODE_FAST && ((in->vbits >> VG_OUT) & 1u) != 0u) ? (in->w5 & 0xfffu) : 0u; +} std::string apply_mode(Instance* in, Mode m) { in->mode = m; in->vbits = mode_vbits(m); @@ -2187,6 +2286,16 @@ uint32_t create_locked(const WgRrCreate* d, void** out) { const bool have_iv = GetEnvironmentVariableA("WG_RR_INPUT", iv, sizeof iv) != 0; in->input_copy = have_iv && strcmp(iv, "copy") == 0; // WG_RR_INPUT=copy: exact through f32 copies (diagnostic) char av[8] = {}; + { + char w4v[16] = {}; + if (GetEnvironmentVariableA("WG_RR_W4", w4v, sizeof w4v)) in->w4 = (unsigned)strtoul(w4v, nullptr, 16) & 7u; + char w4bv[16] = {}; + if (GetEnvironmentVariableA("WG_RR_W4B", w4bv, sizeof w4bv)) in->w4b = (unsigned)strtoul(w4bv, nullptr, 16) & 0xfu; + char w4cv[16] = {}; + if (GetEnvironmentVariableA("WG_RR_W4C", w4cv, sizeof w4cv)) in->w4c = (unsigned)strtoul(w4cv, nullptr, 16) & 0x3fu; + char w5v[16] = {}; + if (GetEnvironmentVariableA("WG_RR_W5", w5v, sizeof w5v)) in->w5 = (unsigned)strtoul(w5v, nullptr, 16) & 0xfffu; + } in->alias = !(GetEnvironmentVariableA("WG_RR_ALIAS", av, sizeof av) && strcmp(av, "0") == 0); char dxv[8] = {}; in->diag_inx = GetEnvironmentVariableA("WG_RR_DIAG_INX", dxv, sizeof dxv) && strcmp(dxv, "1") == 0; @@ -2197,6 +2306,8 @@ uint32_t create_locked(const WgRrCreate* d, void** out) { if (GetEnvironmentVariableA("WG_RR_RINGEST", hsv, sizeof hsv)) in->ringest = std::max(0, std::min(15, atoi(hsv))); char fsv[8] = {}; in->fuse_store = !(GetEnvironmentVariableA("WG_RR_FUSESTORE", fsv, sizeof fsv) && strcmp(fsv, "0") == 0); + in->res3 = !(GetEnvironmentVariableA("WG_RR_RES3", fsv, sizeof fsv) && strcmp(fsv, "0") == 0); + in->res_skip_arena = !(GetEnvironmentVariableA("WG_RR_RESARENA", fsv, sizeof fsv) && strcmp(fsv, "1") == 0); char pv[16] = {}; in->fast_paths = !(GetEnvironmentVariableA("WG_RR_PATHS", pv, sizeof pv) && strcmp(pv, "verified") == 0); char bv[8] = {}; @@ -2275,6 +2386,22 @@ uint32_t create_locked(const WgRrCreate* d, void** out) { } apply_dflow(in); // i15: the dataflow body of the mode (shared with the next-frame switch) log_line(fmt("instance %u: ingest after body stage %d", in->id, in->ringest)); + log_line(fmt("instance %u: wave-4 levers (WG_RR_W4) 0x%x: head %s, body %s, output %s", in->id, in->w4, (in->w4 & 1u) ? "on" : "off", + (in->w4 & 2u) ? "on" : "off", (in->w4 & 4u) ? "on" : "off")); + log_line(fmt("instance %u: wave-4b wave64 forms (WG_RR_W4B) 0x%x: head %s, D1-R %s, D1-F %s, E1 %s (D1-R / D1-F / E1 also need the WG_RR_W4 body lever, bit 1)", in->id, + in->w4b, (in->w4b & 1u) ? "on" : "off", (in->w4b & 2u) ? "on" : "off", (in->w4b & 4u) ? "on" : "off", + (in->w4b & 8u) ? "on" : "off")); + log_line(fmt("instance %u: wave-4c levers (WG_RR_W4C) 0x%x: wide level 2 %s, level 3 %s, level 4 decoder %s, IN0 %s, D0-F %s, output ph1w %s (output also needs WG_RR_W4 bit 2 off)", in->id, + in->w4c, (in->w4c & 1u) ? "on" : "off", (in->w4c & 2u) ? "on" : "off", (in->w4c & 4u) ? "on" : "off", + (in->w4c & 8u) ? "on" : "off", (in->w4c & 16u) ? "on" : "off", (in->w4c & 32u) ? "on" : "off")); + { // clean/rr-f-wave5b: the Fast-mode-only levers (the effective mask is 0 in every other mode) + const unsigned w5e = w5_effective(in); + log_line(fmt("instance %u: wave-5c levers (WG_RR_W5) 0x%x, effective 0x%x (rr_mode fast only): head %s, out %s, res %s, in5c %s, L2 %s, L0 %s, L1 %s, L3 %s, L45 %s, pool %s, res5c %s, out5c %s", in->id, + in->w5, w5e, (w5e & 1u) ? "on" : "off", (w5e & 2u) ? "on" : "off", (w5e & 4u) ? "on" : "off", + (w5e & 8u) ? "on" : "off", (w5e & 0x10u) ? "on" : "off", (w5e & 0x20u) ? "on" : "off", + (w5e & 0x40u) ? "on" : "off", (w5e & 0x80u) ? "on" : "off", (w5e & 0x100u) ? "on" : "off", + (w5e & 0x200u) ? "on" : "off", (w5e & 0x400u) ? "on" : "off", (w5e & 0x800u) ? "on" : "off")); + } log_line(fmt("instance %u: rr_mode=%s (%s%s): head %s, input %s, body %s, output %s, resolve %s", in->id, mode_name(in->mode), msrc, vover.c_str(), (in->vbits >> VG_HEAD) & 1 ? "relaxed (RR_HVARL 7)" : "exact", in->input_lite ? "lite" : (in->input_split ? "split (history exact)" : (in->input_copy ? "exact (copy)" : "exact")), @@ -2834,6 +2961,7 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) const bool th_created = th_new || oh_new; // 54b.6: a history created this frame (zero-filled; reset for 54b.3) uint32_t rflags = 0; bool res_spec2 = false; // the frame's resolve matches RF_SPEC 2 (resolve.fast2; its own check repeats this) + bool res_spec3 = false; // ... or RF_SPEC 3, the plain resolve (resolve.fast3; its own check repeats this) bool fuse_store = false; // the main output's store is done by resolve.fast2 / resolve.fixup (no rt.store for it) { // resolve parameters (kernels/rr_resolve.hlsl; gpu/resolve.py params) and the PH2 pool's @@ -2865,13 +2993,16 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) ((reset || in->ah_created) ? 64u : 0u) | ((f->settings & WG_RR_SET_BOUNDED_STORES) ? 128u : 0u) | 256u; p[8] = rflags; res_spec2 = (rflags & 15u) == 3u && sl.fmt == 1u && oi[0].fmt == 1u && lay.th_on; + // R2: plain (no layer, opacity or alpha upscaling: rflags & 15 == 0); the kernel's screens (Q, output formats) send every + // other dispatch to resolve.fixup, which is the verified pixel code resolve.fast's plain path also falls back to + res_spec3 = in->res3 && (rflags & 15u) == 0u && oi[0].fmt == 1u; { // clean/rr-f-s10-tail: rt.store's texels (We x He, bounded: Wo x Ho inside it) at the output origin const WgRrImage& t = img[WG_ROLE_OUTPUT]; const bool bnd = (f->settings & WG_RR_SET_BOUNDED_STORES) != 0; - fuse_store = in->fuse_store && in->fast_paths && res_spec2 && oi[0].on && oi[0].fmt == 1u && oi[0].ch == 4u && + fuse_store = in->fuse_store && in->fast_paths && (res_spec2 || res_spec3) && oi[0].on && oi[0].fmt == 1u && oi[0].ch == 4u && !oi[0].srgb && (!bnd || (in->Wo <= in->We && in->Ho <= in->He)) && t.origin_x < 65536u && t.origin_y < 65536u; - p[16] = fuse_store ? 1u : 0u; + p[16] = fuse_store ? (in->res_skip_arena && in->alias ? 2u : 1u) : 0u; // 2: the texture only (R1b) p[39] = fuse_store ? ((uint32_t)t.origin_x | ((uint32_t)t.origin_y << 16)) : 0u; } p[11] = fb(f->jitter[0]); @@ -3117,9 +3248,10 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) in->tail_ready = false; if (!hs) uav_barrier(l, A); // PH2 quarter pool (54b.5), non-reset frames only (rr_resolve_pool: PH1 of the previous frame -> PH2) + const Kid pool_k = (w5_effective(in) & 0x200u) != 0u ? K_POOL_MAIN_W5 : K_POOL_MAIN; if (!reset && !use_tail) { - if (in->fast_paths) // through the exact tables (pool.main; the same bits as resolve.pool) - dispatch(l, K_POOL_MAIN, (w2 * h2 + kPoolLanes - 1) / kPoolLanes, {(uint32_t)(blk + kBlkPool), (uint32_t)lay.ptab, 0u, 0u}); + if (in->fast_paths) // through the exact tables (pool.main / pool.main.w5; the same bits as resolve.pool) + dispatch(l, pool_k, (w2 * h2 + kPoolLanes - 1) / kPoolLanes, {(uint32_t)(blk + kBlkPool), (uint32_t)lay.ptab, 0u, 0u}); else dispatch(l, K_RES_POOL, (w2 * h2 + 63) / 64, {(uint32_t)(blk + kBlkPool), 0u, 0u}); if (!hs) uav_barrier(l, A); @@ -3226,7 +3358,11 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) if (!nsk) { if (in->dflow) fill(P.off(Bf.dfcnt), (uint64_t)(15 * kDfRows + 64) * 4, 0); // clean/rr-f-s12-dflow: row counters l->SetComputeRootShaderResourceView(6, ring_slot + kBlkInput); // I1: the input block's view (RR_IN_KCB) - dispatch(l, input_features_kernel(in->input_lite ? 1 : (in->input_split ? 2 : 0)), fgroups, {(uint32_t)(blk + kBlkInput)}); + bool all_in_srv = !in->input_copy; + for (int r = 0; r < (int)kRoles; ++r) + if (ing[r].on && ing[r].copy) all_in_srv = false; + const bool inw5 = (w5_effective(in) & 8u) != 0u && in->input_lite && all_in_srv; + dispatch(l, inw5 ? K_IN_FEAT_W5C : input_features_kernel(in->input_lite ? 1 : (in->input_split ? 2 : 0)), fgroups, {(uint32_t)(blk + kBlkInput)}); if (!(in->dprobe2 & 1)) uav_barrier(l, A); stamp(q0 + kTs + 1); // clean/rr-mp2-input I4f: input lite (input.features.lite, RR_IN_LITE 1) appends no token to the replay list, so its @@ -3390,7 +3526,34 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) a.insert(a.end(), {(uint32_t)P.off(Bf.dfcnt), (uint32_t)s, q[0], q[1], q[2]}); dispatch(l, (Kid)(((in->vbits >> VG_BODY) & 1 ? K_DXSTAGE0 : K_DSTAGE0) + s), nwx * nwy, a); } else { - dispatch(l, in->body_e ? (Kid)(((in->vbits >> VG_BODY) & 1 ? K_ESTAGEX0 : K_ESTAGE0) + s) : d.k, nwx * nwy, a); + // clean/rr-f-wave4 R4.2: the relaxed body's E1 / D1-F / D0-F before the LDS overlay (WG_RR_W4 bit 1 = 0) + const int s0 = (in->w4 & 2u) || ((in->vbits >> VG_BODY) & 1) ? -1 : (s == 1 ? 0 : s == 12 ? 1 : s == 14 ? 2 : -1); + // clean/rr-f-wave4b R4b.2: the relaxed body's D1-R / D1-F / E1 in wave64 (WG_RR_W4B bits 1 / 2 / 3, with the body lever on) + const int sw = ((in->w4 & 2u) && !((in->vbits >> VG_BODY) & 1)) ? (s == 13 ? 0 : s == 12 ? 1 : s == 1 ? 2 : -1) : -1; + const bool w64 = sw >= 0 && ((in->w4b >> (sw + 1)) & 1u) != 0u; + // clean/rr-f-wave4c: the relaxed body's wide stages (E2, E3, D4-F, D4-R, D3-F, D3-R, D2-F, D2-R: s = 2, 3, 6 .. 11) in wave64 (WG_RR_W4C + // bits 0 / 1 / 2 by level), IN0 / D0-F with the deferred Q8 decision and prefetch (bits 3 / 4; D0-F with the WG_RR_W4 body lever) + const bool rlx = in->body_e && !((in->vbits >> VG_BODY) & 1); + const int wc4 = (s == 2 || s == 10 || s == 11) ? 0 : (s == 3 || s == 8 || s == 9) ? 1 : (s == 6 || s == 7) ? 2 : -1; + const int c4k = (s == 2) ? 0 : (s == 10) ? 1 : (s == 11) ? 2 : (s == 3) ? 3 : (s == 8) ? 4 : (s == 9) ? 5 : (s == 6) ? 6 : 7; + Kid c4 = K_COUNT; + if (rlx && wc4 >= 0 && ((in->w4c >> wc4) & 1u) != 0u) c4 = (Kid)(K_ESTAGEC0 + c4k); + else if (rlx && s == 0 && (in->w4c & 8u)) c4 = K_IN0_W4C; + else if (rlx && s == 14 && (in->w4c & 16u) && (in->w4 & 2u)) c4 = K_D0F_W4C; + // clean/rr-f-wave5b/5c (Fast mode only, WG_RR_W5 bits 4..8: bit 4 = L2 E2/D2-F/D2-R, bit 5 = L0 IN0/D0-F, bit 6 = L1 E1/D1-F/D1-R, + // bit 7 = L3 E3/D3-F/D3-R wave32, bit 8 = L4/L5 E4/E5/D4-F/D4-R wave32): + // pure-f32 RR_B32=1 + branchless clamped RR_Q8_FAST=1 stagef.* kernels (0 spills, NAR=0) replace every wave-4 form of these stages + { + const unsigned w5e = w5_effective(in); + const int f5 = (s == 0) ? 0 : (s == 14) ? 1 : (s == 1) ? 2 : (s == 12) ? 3 : (s == 13) ? 4 + : (s == 2) ? 5 : (s == 10) ? 6 : (s == 11) ? 7 + : (s == 3) ? 8 : (s == 8) ? 9 : (s == 9) ? 10 + : (s == 4) ? 11 : (s == 5) ? 12 : (s == 6) ? 13 : (s == 7) ? 14 : -1; + const int f5bit = (s == 2 || s == 10 || s == 11) ? 4 : (s == 0 || s == 14) ? 5 : (s == 1 || s == 12 || s == 13) ? 6 + : (s == 3 || s == 8 || s == 9) ? 7 : (s >= 4 && s <= 7) ? 8 : -1; + if (rlx && f5 >= 0 && ((w5e >> f5bit) & 1u) != 0u) c4 = (Kid)(K_FSTAGE0 + f5); + } + dispatch(l, in->body_e ? (c4 != K_COUNT ? c4 : (w64 ? (Kid)(K_ESTAGEW0 + sw) : (s0 >= 0 ? (Kid)(K_ESTAGEB0 + s0) : (Kid)(((in->vbits >> VG_BODY) & 1 ? K_ESTAGEX0 : K_ESTAGE0) + s)))) : d.k, nwx * nwy, a); } } // clean/rr-f-s9-host: the resolve's game-input ingest (game textures -> layer / opacity / alpha copies, read @@ -3444,7 +3607,11 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) if (in->prepu_at < 0) rec_prepu(); // no barrier: the head does not touch its words if (nsk) { } else if (!in->head_exact) { - dispatch(l, in->head_debug ? K_HEAD_DEBUG : in->head_fast ? ((in->vbits >> VG_HEAD) & 1 ? K_HEAD_FAST_RX : K_HEAD_FAST) : K_HEAD_STRICT, + // clean/rr-f-wave5 bit 0 (Fast mode only): head.fast.rx.f.w64 takes s2 of page 55.3 (the f32 bits) in the root constant the other head + // kernels pass as 0 (p_dz, argument 15): rho = tanh(f32(y17 s2)) is computed, not read from head.rho + const bool hw5 = (w5_effective(in) & 1u) != 0u && in->head_fast && !in->head_debug && ((in->vbits >> VG_HEAD) & 1) != 0; + if (hw5) a[15] = M.scalar("head.k_s2"); + dispatch(l, in->head_debug ? K_HEAD_DEBUG : in->head_fast ? ((in->vbits >> VG_HEAD) & 1 ? (hw5 ? K_HEAD_FAST_RXF : ((in->w4b & 1u) ? K_HEAD_FAST_RXW : ((in->w4 & 1u) ? K_HEAD_FAST_RX : K_HEAD_FAST_RX0))) : K_HEAD_FAST) : K_HEAD_STRICT, nwx * nwy, a); if (!(in->dprobe2 & 4)) uav_barrier(l, A); } else { // exact head: weight planes, the integer pass (marks windows it cannot take), strict fixup of those @@ -3468,10 +3635,20 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) uav_barrier(l, A); } if (tail_rec && in->hiddec_early) decode1(2); // a14 E7 f: HID is final after the head barrier - dispatch(l, (in->vbits >> VG_OUT) & 1 ? K_OUT_FAST_F : K_OUT_FAST, ((in->Wh + 31) / 32) * ((in->Hh + 7) / 8), a); + // clean/rr-f-wave5b/5c bit 1 (output.fast.w5) & bit 11 (output.fast.w5c: rolled of_reproj 55 VGPR + PH1W + LD3, 7.2 KB LDS, BAR 2): + // when every tile is guaranteed to fit in LDS (W <= 30, Hn <= 12); zeroes scratch[4] itself and marks no fallback quad, so output.fallback.f + UAV barrier are skipped + const unsigned w5e_out = w5_effective(in); + const bool out_fits = ((in->vbits >> VG_OUT) & 1u) != 0u && + rw > 0u && rh > 0u && in->Wh > 0u && in->Hh > 0u && + (uint64_t)31u * rw <= (uint64_t)25u * in->Wh && (uint64_t)6u * rh <= (uint64_t)7u * in->Hh; + const bool outw5c = (w5e_out & 0x800u) != 0u && out_fits; + const bool outw5 = outw5c || ((w5e_out & 2u) != 0u && out_fits); + dispatch(l, outw5c ? K_OUT_FAST_W5C : outw5 ? K_OUT_FAST_W5 : ((in->vbits >> VG_OUT) & 1 ? ((in->w4 & 4u) ? K_OUT_FAST_F : ((in->w4c & 32u) ? K_OUT_FAST_F_PH1W : K_OUT_FAST_F0)) : K_OUT_FAST), ((in->Wh + 31) / 32) * ((in->Hh + 7) / 8), a); if (!(in->dprobe2 & 8)) uav_barrier(l, A); - dispatch(l, (in->vbits >> VG_OUT) & 1 ? K_OUT_FALLBACK_F : K_OUT_FALLBACK, kOutFallbackGroups, a); - if (!(in->dprobe2 & 16)) uav_barrier(l, A); + if (!outw5) { + dispatch(l, (in->vbits >> VG_OUT) & 1 ? K_OUT_FALLBACK_F : K_OUT_FALLBACK, kOutFallbackGroups, a); + if (!(in->dprobe2 & 16)) uav_barrier(l, A); + } } else { a.push_back(3u); // packed head buffers, E / Ep / invE from the exposure result a.push_back(0u); @@ -3492,14 +3669,30 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) const uint32_t tiles = ((in->We + kResTile - 1) / kResTile) * ((in->He + kResTile - 1) / kResTile); const bool rx = (in->vbits >> VG_RES) & 1; if (fuse_store) l->SetComputeRootDescriptorTable(4, gpu(store_pair[0])); // u2: the main output (rf_gout) - dispatch(l, res_spec2 ? (rx ? K_RES_FAST2_X : K_RES_FAST2) : (rx ? K_RES_FAST_X : K_RES_FAST), std::min(tiles, in->res_groups), {(uint32_t)(blk + kBlkResolve), 0u, 0u}); + // clean/rr-f-wave5b/5c bit 2 (resolve.fast2/3.w5) & bit 10 (resolve.fast2/3.w5c: QN=28, RP=16, LLDS, QLD1, 32-bit MV, rolled 3x3): + // marks no slow pixel, so resolve.fixup is skipped (keeping the barrier before decode1(1) when tail_rec runs pool.main[.w5]) + const unsigned w5e_res = w5_effective(in); + const bool res_precond = !rx && (res_spec2 || res_spec3) && + in->We <= 32768u && in->He <= 32768u && in->Wh < 65536u && in->Hh < 65536u && + rw < 65536u && rh < 65536u && (!res_spec2 || (rw <= in->We && rh <= in->He)); + const bool resw5c2 = res_precond && (w5e_res & 0x400u) != 0u && res_spec2 && + sl.w == rw && sl.h == rh && f->layer_origin_x == 0 && f->layer_origin_y == 0 && + (uint64_t)15u * rw <= (uint64_t)11u * in->We && (uint64_t)15u * rh <= (uint64_t)11u * in->He && + fabsf(f->jitter[0]) <= 0.55f && fabsf(f->jitter[1]) <= 0.55f; + const bool resw5c3 = res_precond && (w5e_res & 0x400u) != 0u && res_spec3; + const bool resw5c = resw5c2 || resw5c3; + const bool resw5 = resw5c || (res_precond && (w5e_res & 4u) != 0u); + dispatch(l, resw5c ? (res_spec2 ? K_RES_FAST2_W5C : K_RES_FAST3_W5C) + : resw5 ? (res_spec2 ? K_RES_FAST2_W5 : K_RES_FAST3_W5) + : (res_spec2 ? (rx ? K_RES_FAST2_X : K_RES_FAST2) : res_spec3 ? (rx ? K_RES_FAST3_X : K_RES_FAST3) : (rx ? K_RES_FAST_X : K_RES_FAST)), + std::min(tiles, in->res_groups), {(uint32_t)(blk + kBlkResolve), 0u, 0u}); if (tail_rec) { // the next frame's history prep: PH1 -> PH2 pool, PH1 / HID decodes (PH1, HID final here) - dispatch(l, K_POOL_MAIN, (w2 * h2 + kPoolLanes - 1) / kPoolLanes, {(uint32_t)(blk + kBlkPool), (uint32_t)lay.ptab, 0u, 0u}); + dispatch(l, pool_k, (w2 * h2 + kPoolLanes - 1) / kPoolLanes, {(uint32_t)(blk + kBlkPool), (uint32_t)lay.ptab, 0u, 0u}); decode1(0); if (!in->hiddec_early) decode1(2); } - uav_barrier(l, A); - dispatch(l, K_RES_FIXUP, (uint32_t)(((eb + 31) / 32 + 255) / 256), {(uint32_t)(blk + kBlkResolve), 0u, 0u}); + if (!resw5 || tail_rec) uav_barrier(l, A); + if (!resw5) dispatch(l, K_RES_FIXUP, (uint32_t)(((eb + 31) / 32 + 255) / 256), {(uint32_t)(blk + kBlkResolve), 0u, 0u}); if (tail_rec) { // PH2 decode after the pool's barrier; dec_* are next read by the next frame's features decode1(1); in->tail_ready = true; @@ -3824,7 +4017,7 @@ bool instance_needs_modes(const Instance* in) { return in->vbits != R().start_vb bool instance_needs_opt_in(const Instance* in) { // the three input-features kernels (exact, lite, split) are all part of the default frame (small, compiled first) // clean/rr-f-s10-modes: also an instance whose variants (mode, per-knob overrides) differ from the first pass's - return !in->head_fast || !in->body_e || !in->fast_paths; + return !in->head_fast || !in->body_e || !in->fast_paths || in->w4 != 7u; } uint32_t a_evaluate(void*, void* inst, const WgRrFrame* f, WgRrFrameResult* res) { Runtime& rt = R(); diff --git a/clean/rr_f/runtime/tools/build.py b/clean/rr_f/runtime/tools/build.py index 401da13f..39091789 100644 --- a/clean/rr_f/runtime/tools/build.py +++ b/clean/rr_f/runtime/tools/build.py @@ -42,8 +42,17 @@ ROOT = os.path.dirname(os.path.dirname(RRF)) DEFAULT_OUT = os.path.join(os.environ.get("WG_BUILD_ROOT", os.path.join(ROOT, "build")), "rr-f-rt-int", "out") -FAST_OUT_DEFINES = {"OF_HWRCP": 1, "OF_REPROJ_MAX": 1, # Fast mode's output stage (clean/rr-f-s10-modes) - "OF_UNIF": 1} # clean/rr-f-lw-fold: uniform scratch words (exact, same bits; faster in output.fast.f only) +EXACT_OUT_DEFINES = {"OFX_FUSE": 1, "OF_UNIF": 1} # exact, same bits (output_diff PASS): fused 16-tap loop + uniform scratch words +FAST_OUT_DEFINES = {"OF_HWRCP": 1, "OF_REPROJ_MAX": 1, # Fast mode's output stage (clean/rr-f-s10-modes; tier B, NOT exact) + "OF_UNIF": 1, "OFX_FUSE": 1, "OFX_NOCHK": 1} # fused 16-tap loop + uniform scratch + no subnormal-band check (fast mode only) +# clean/rr-f-wave4 R4.3 (same bits as without; rr_output_fast.hlsli, written arguments there): extra defines of output.fast.f +# alone (output.prep.f / output.fallback.f keep their DXIL): OFX_PH1W (the PH1 pool inside each wave64: no groupshared +# copy, no barrier), OFX_LD3 (the motion pair / eroded texels as one b64 / b32 load), OFX_QST (two neighbour Q texels as +# one 12-byte store). WG_RR_OUTF_FASTDEFS (JSON object, build time) replaces them; the runtime kernel output.fast.f0 is +# the same kernel without them (WG_RR_W4 bit 2 = 0 selects it). +FAST_OUT_FAST_ONLY = {"OFX_PH1W": 1, "OFX_LD3": 1, "OFX_QST": 1} +# clean/rr-f-wave4c W4C.3: output.fast.f_ph1w = output.fast.f0 plus OFX_PH1W alone +FAST_OUT_PH1W_ONLY = {"OFX_PH1W": 1} class _env: @@ -72,6 +81,11 @@ def __exit__(self, *a): # invalidate the head kernels' cache keys) OWN_KEY_SKIP = ("rr_input.hlsl", "rr_input_mlp.hlsl", "rr_block.hlsl", "rr_block_c32.hlsl", "rr_block_fast.hlsl", "rr_block_wide.hlsl", "rr_fast.hlsli", "rr_mlp_step.hlsli", "rr_dflow.hlsli", # (s12-dflow) + # clean/rr-f-wave4b: the wave64 forms (the head's own file is never included by the other head kernels; + # listing them keeps every existing head kernel's cache key) + "rr_d0r_w64.hlsl", "rr_block_c64_w64.hlsl", + # clean/rr-f-wave4c: the wave64 form of the wide stages (never included by the head) + "rr_block_wide_w64.hlsl", "rr_in_math.hlsli", # a14-frame (request of track C): included by rr_input.hlsl only # clean/rr-f-s10-tail: the output / resolve / pool / exposure kernels are never included by the head "rr_output.hlsl", "rr_output_fast.hlsli", "rr_output_probe.hlsl", "rr_resolve.hlsl", "rr_resolve_fast.hlsl", @@ -110,6 +124,27 @@ def kernels(out): # the default step E on the struct routes (opt-in WG_RR_BODY=E; E1 reads IN0's y2 directly, as for step X) assert stage.e1_direct(1, "E") == stage.e1_direct(1, "X") res += [("stagee." + n, 37, stage.kernel_for(n, ez, step="E", ldsw=1)) for n in STAGES] + # clean/rr-f-wave4 R4.2b: stagee.E1 / D1-F / D0-F share the exact body's LDS overlay (rr_block_fast.hlsl / rr_block_c32.hlsl + # RR_OVL); stagee0.* = the same kernels without it (RR_OVL 0, the kernels of fc16c9d; runtime WG_RR_W4 bit 1 = 0) + res += [("stagee0." + n, 37, stage.kernel_for(n, ez, step="E", ldsw=1, extra={"RR_OVL": 0})) for n in ("E1", "D1-F", "D0-F")] + # clean/rr-f-wave4b R4b.2: the level-1 stages in wave64 (rr_block_c64_w64.hlsl, design D; the same bits as stagee.E1 / + # D1-F / D1-R; runtime WG_RR_W4B bits 3 / 2 / 1). Relaxed body only (the exact body keeps wave32). + if b32: + res += [("stagee." + n + ".w64", 37, stage.kernel_c64_w64(n, ez, step="E", ldsw=1)) for n in stage.W4B_C64_STAGES] + else: # (a non-relaxed library: the names stay, with stagee.'s own DXIL) + res += [("stagee." + n + ".w64", 37, stage.kernel_for(n, ez, step="E", ldsw=1)) for n in stage.W4B_C64_STAGES] + # clean/rr-f-wave4c W4C.1: the wide stages (E2, D2-F, D2-R, E3, D3-F, D3-R, D4-F, D4-R) in wave64 (rr_block_wide_w64.hlsl, the + # same bits as stagee.; runtime WG_RR_W4C bits 0 / 1 / 2 = level 2 / level 3 / level 4 decoder). E4 / E5 keep wave32. + if b32: + res += [("stagee." + n + ".w64", 37, stage.kernel_wide_w64(n, ez, step="E", ldsw=1)) for n in stage.W4C_WIDE_STAGES] + else: + res += [("stagee." + n + ".w64", 37, stage.kernel_for(n, ez, step="E", ldsw=1)) for n in stage.W4C_WIDE_STAGES] + # clean/rr-f-wave4c W4C.2: stagee.IN0.w4c / stagee.D0-F.w4c = stagee.IN0 / D0-F (rr_block_c32.hlsl wave64) with the deferred Q8 decision + # (RR_DEFER), the Dd / Wo weight prefetch and, for IN0, the interleaved front-end MLP (stage.W4C_C32_DEFS); same bits. Runtime + # WG_RR_W4C bits 3 / 4. The non-relaxed library repeats stagee.'s DXIL under the names. + for n in ("IN0", "D0-F"): + res.append(("stagee." + n + ".w4c", 37, stage.kernel_for(n, ez, step="E", ldsw=1, extra=stage.W4C_C32_DEFS[n]) if b32 + else stage.kernel_for(n, ez, step="E", ldsw=1))) # clean/rr-f-s12-dflow: staged.* = stagee.* in the dataflow form (rr_dflow.hlsli: row counters instead of the UAV # barriers; opt-in WG_RR_DFLOW=1). Only the rr_block_wide.hlsl stages have that form; the others repeat stagee.*'s # DXIL (kRrDflowMask: the stages that have it) @@ -203,11 +238,19 @@ def kernels(out): for n in ("main", "pool"): res.append((f"output.{n}", GO.NARGS, ck("rr_output.hlsl", f"rr_output_{n}", {"WG_BLOCK": 32, "RR_RT": 1}, tag=f"rr_output_{n}_rt"))) - for n, (dxil, nargs) in GO.kernel_set(fast=True, names=GO.FAST_KERNELS + ("prepu",), spec=True).items(): # prep, fast, fallback, prepu + for n, (dxil, nargs) in GO.kernel_set(defines=EXACT_OUT_DEFINES, fast=True, names=GO.FAST_KERNELS + ("prepu",), spec=True).items(): # prep, fast, fallback, prepu res.append((f"output.{n}", nargs, dxil)) - # clean/rr-f-s10-modes: Fast mode's output stage (OF_HWRCP + OF_REPROJ_MAX; tier B, NOT exact), same four kernels - for n, (dxil, nargs) in GO.kernel_set(defines=FAST_OUT_DEFINES, fast=True, names=GO.FAST_KERNELS + ("prepu",), spec=True).items(): + # clean/rr-f-s10-modes: Fast mode's output stage (OF_HWRCP + OF_REPROJ_MAX + OFX_FUSE + OFX_NOCHK; tier B, NOT exact), same four kernels + for n, (dxil, nargs) in GO.kernel_set(defines=FAST_OUT_DEFINES, fast=True, names=GO.FAST_KERNELS + ("prepu",), spec=True, + fast_only=json.loads(os.environ.get("WG_RR_OUTF_FASTDEFS", json.dumps(FAST_OUT_FAST_ONLY)))).items(): res.append((f"output.{n}.f", nargs, dxil)) + # clean/rr-f-wave4: output.fast.f before R4.3 (A/B kernel of the runtime's WG_RR_W4 mask) + res.append(("output.fast.f0", GO.NARGS, GO.kernel_set(defines=FAST_OUT_DEFINES, fast=True, names=("fast",), spec=True)["fast"][0])) + # clean/rr-f-wave4c W4C.3 (same bits; rr_output_fast.hlsli): output.fast.f with OFX_PH1W alone (the PH1 pool inside each + # wave64, no groupshared tile copy: LDS 10,304 -> 7,200 bytes) without OFX_LD3 / OFX_QST, which Wave 4a mixed into the same + # kernel. Runtime WG_RR_W4C bit 5 (when WG_RR_W4 bit 2 is off). + res.append(("output.fast.f_ph1w", GO.NARGS, GO.kernel_set(defines=FAST_OUT_DEFINES, fast=True, names=("fast",), spec=True, + fast_only=json.loads(os.environ.get("WG_RR_OUTF_PH1W_DEFS", json.dumps(FAST_OUT_PH1W_ONLY))))["fast"][0])) for n in ("main", "pool"): res.append((f"resolve.{n}", 3, ck("rr_resolve.hlsl", f"rr_resolve_{n}", {"WG_BLOCK": 64, "RR_ELEM_EXACT": 1, "RR_MUT": 0}, @@ -227,6 +270,10 @@ def kernels(out): # clean/rr-f-s10-modes: mode exact's resolve (correctly rounded exp2 / reciprocal: WG_RR_RF_DEFINES={}) res.append(("resolve.fast.x", GRF.NARGS, GRF.fast_kernel(spec=0, extra={}))) res.append(("resolve.fast2.x", GRF.NARGS, GRF.fast_kernel(spec=2, extra={}))) + # clean/rr-f-300 R2: the plain configuration (RF_SPEC 3: not extended, no layer / opacity / alpha upscaling; same bits as + # resolve.fast / resolve.fast.x for those dispatches), hardware exp2 / reciprocal for Fast (rfd), correctly rounded for the .x + res.append(("resolve.fast3", GRF.NARGS, GRF.fast_kernel(spec=3, extra=rfd))) + res.append(("resolve.fast3.x", GRF.NARGS, GRF.fast_kernel(spec=3, extra={}))) for n, (dxil, nargs) in GP.kernels(exact=True).items(): # pool-tables, pool-main (exact tables) res.append((n.replace("-", "."), nargs, dxil)) @@ -274,9 +321,21 @@ def own(src, entry, defines, includes, tag): # object, A/B builds only) = extra defines of head.fast.rx # clean/rr-mp1-head (owner decision 2026-11-01): accurate mode runs head.fast.rx too (registry mode row, gate-checked); # head.fast (exact) remains mode exact's fast head. - res.append(("head.fast.rx", 48, own(head, "rr_d0r_head", dict(hz, RR_OUT_PREP=1, RR_HVARL=7, RR_S9=61, - **json.loads(os.environ.get("WG_RR_HEADRX_DEFS", "{}"))), + # clean/rr-f-wave4 R4.1a (same bits as without; rr_d0r.hlsl): RR_HSTM 1 (each record whole from the hf0 lane: b128 / b64 / + # b32 stores) and RR_HQ8S 1 (step 1's q / v^T tiles quantised per head: 99 -> 95 VGPRs, under the 96 cliff, 0 spills). + # head.fast.rx0 = head.fast.rx of fc16c9d (runtime WG_RR_W4 bit 0 = 0). WG_RR_HEADRX_DEFS replaces nothing: it adds to both + hrx = dict(hz, RR_OUT_PREP=1, RR_HVARL=7, RR_S9=61, **json.loads(os.environ.get("WG_RR_HEADRX_DEFS", "{}"))) + res.append(("head.fast.rx", 48, own(head, "rr_d0r_head", dict(hrx, RR_HSTM=1, RR_HQ8S=1, + **json.loads(os.environ.get("WG_RR_HEADRX_W4_DEFS", "{}"))), hinc, "rr_d0r_head_zexact_oprep"))) + res.append(("head.fast.rx0", 48, own(head, "rr_d0r_head", hrx, hinc, "rr_d0r_head_zexact_oprep"))) + # clean/rr-f-wave4b R4b.1 (same bytes as head.fast.rx; head/dx12/rr_d0r_w64.hlsl, written arguments there): the relaxed head + # in wave64 (design D of rr_block_c32.hlsl: one wave64 = two token tiles of the window, the weight fragments shared). + # Runtime WG_RR_W4B bit 0; head.fast.rx stays as it is (bit 0 = 0 selects it). + res.append(("head.fast.rx.w64", 48, own(os.path.join(RRF, "head", "dx12", "rr_d0r_w64.hlsl"), "rr_d0r_head", + dict(hrx, WG_WAVE=64, RR_HSTM=1, RR_HQ8S=1, + **json.loads(os.environ.get("WG_RR_HEADRX_W4_DEFS", "{}"))), + hinc, "rr_d0r_head_w64_zexact_oprep"))) rk = os.path.join(RT, "kernels", "rr_runtime.hlsl") for name, entry, blk in (("rt.ingest", "rr_rt_ingest", 64), ("rt.fill", "rr_rt_fill", 64), ("rt.store", "rr_rt_store", 64), ("rt.selftest", "rr_rt_selftest", 32), @@ -285,6 +344,40 @@ def own(src, entry, defines, includes, tag): # clean/rr-f-a14-frame E24: the read-only cache warm-up pass (WG_RR_TOUCH at creation; not dispatched by default) res.append(("rt.touch", 24, own(os.path.join(RT, "kernels", "rr_touch.hlsl"), "rr_rt_touch", {"WG_BLOCK": 256, "WG_WAVE": 32}, [], "rr_rt_touch"))) res.append(("rt.blkcopy", 24, own(os.path.join(RT, "kernels", "rr_touch.hlsl"), "rr_rt_blkcopy", {"WG_BLOCK": 256, "WG_WAVE": 32}, [], "rr_rt_blkcopy"))) + # clean/rr-f-wave5b/5c (fast mode only; runtime WG_RR_W5, default 0xaff; NOT exact, network/tail arithmetic relaxed, history logic exact): + # bit 0 head.fast.rx.f.w64 (RR_HFAST_ALU=2: bounded f32 ALU head tail, per-head Q8 reducing VGPR 96->84, branchless Q8, no dead CANDW) + # bit 1 output.fast.w5 (OFX_W5_ALU=1 on output.fast.f0 base: native f16, FMA bilinear, 16-tap half exp2, 3-cmp max, no fallback pass) + # bit 2 resolve.fast2.w5 / resolve.fast3.w5 (RFX_W5_ALU=1: f32 uniforms, uint2 half4 taps, FP16 3x3 clamp before E, no fixup pass) + # bit 3 input.features.w5c (input.features.lite wave64 + RR_IN_SRV_ONLY=1: bit-identical to input.features.lite, CPU-guarded direct SRV, -17% INST / -65% VLD / -41% IMG, 57 VGPR, 5120 B LDS) + # bits 4..8 stagef.* (gpu/stage.py W5_KERNELS: pure f32 RR_B32=1 + branchless clamped RR_Q8_FAST=1, zero narrow-EXEC WMMAs, L0/L1/L2 wave64 + L3/L4/L5 wave32) + # bit 9 pool.main.w5 (RR_POOL_W5=1: hardware f32 mad/mul with exact tables, 0 FP64, -69% INST, bit-identical to quarter_pool) + # bit 10 resolve.fast2.w5c / resolve.fast3.w5c (RFX_W5C=1: QN=28 / QO=896, wave-uniform s1==3 skip; fast2 adds RFX_RP=16 -> RF_RS=1, 12.8 KB LDS 5 WGs/CU, RFX_LLDS=1 eliminating 4x b64 L reads, RFX_QLD=1, 32-bit MV load, rolled 3x3) + # bit 11 output.fast.w5c (OFX_W5_ALU=1, OFX_W5C=1, OFX_PH1W=1, OFX_LD3=1: rolled of_reproj 74->55 VGPR [+50% occ], 7.2 KB LDS, BAR 4->2). + # Appended after every wave-4c kernel; all 172 wave-4c kernels above keep bit-identical cache keys and DXIL. + res.append(("head.fast.rx.f.w64", 48, own(os.path.join(RRF, "head", "dx12", "rr_d0r_w64.hlsl"), "rr_d0r_head", + dict(hrx, WG_WAVE=64, RR_HSTM=1, RR_HQ8S=1, RR_HFAST_ALU=2, + **json.loads(os.environ.get("WG_RR_HEADRX_W4_DEFS", "{}"))), + hinc, "rr_d0r_head_w64_f_zexact_oprep"))) + res.append(("output.fast.w5", GO.NARGS, + GO.kernel_set(defines=dict(FAST_OUT_DEFINES, OFX_W5_ALU=1), fast=True, names=("fast",), spec=True)["fast"][0])) + res.append(("resolve.fast2.w5", GRF.NARGS, GRF.fast_kernel(spec=2, extra=dict(rfd, RFX_W5_ALU=1)))) + res.append(("resolve.fast3.w5", GRF.NARGS, GRF.fast_kernel(spec=3, extra=dict(rfd, RFX_W5_ALU=1)))) + res.append(("input.features.w5c", 1, + GI.features_kernel("lite+RR_IN_FAST=31+RR_IN_LDST=0+RR_IN_KCB=1+RR_IN_SRV_ONLY=1", srv=1))) + for name, st, kind, defs in stage.W5_KERNELS: + # (a non-relaxed library repeats stagee.'s DXIL under the names, as the wave-4 kernels) + res.append((name, 37, stage.kernel_w5(st, ez, step="E", ldsw=1) if b32 else stage.kernel_for(st, ez, step="E", ldsw=1))) + res.append(("pool.main.w5", GP.NARGS, + build.compile_kernel("rr_pool.hlsl", "rr_pool_main", + {"WG_BLOCK": GP.BLOCK, "WG_WAVE": 32, "RR_ELEM_EXACT": 1, "RR_POOL_TPL": GP.TPL, "RR_POOL_W5": 1}, + tag="rr_pool_main_w5"))) + res.append(("resolve.fast2.w5c", GRF.NARGS, + GRF.fast_kernel(spec=2, extra=dict(rfd, RFX_W5_ALU=1, RFX_W5C=1, RFX_RP=16, RFX_LLDS=1, RFX_QLD=1)))) + res.append(("resolve.fast3.w5c", GRF.NARGS, + GRF.fast_kernel(spec=3, extra=dict(rfd, RFX_W5_ALU=1, RFX_W5C=1)))) + res.append(("output.fast.w5c", GO.NARGS, + GO.kernel_set(defines=dict(FAST_OUT_DEFINES, OFX_W5_ALU=1, OFX_W5C=1, OFX_PH1W=1, OFX_LD3=1), + fast=True, names=("fast",), spec=True)["fast"][0])) return res diff --git a/clean/rr_f/runtime/tools/pack_model.py b/clean/rr_f/runtime/tools/pack_model.py index dc6f954a..147044b8 100644 --- a/clean/rr_f/runtime/tools/pack_model.py +++ b/clean/rr_f/runtime/tools/pack_model.py @@ -328,6 +328,7 @@ def pack(m): sc[f"head.{a}"] = sc_f16(K[n]) sc["head.k_g"] = sc_f16(H["g"]) sc["head.k_s1"] = f32bits(s1) + sc["head.k_s2"] = f32bits(s2) # clean/rr-f-wave5: head.fast.rx.f.w64 computes rho = tanh(f32(y17 s2)) (no head.rho gather) sc["K1"] = f32bits(np.float32(H["K1"][0])) # input stage (52): front-end MLP fragments (gpu/input_stage.py), dither, read tables IN0 = G["IN0"]