From 19bfc545cd84739aebd29af4b3380fc70c0fd735 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:03:21 -0500 Subject: [PATCH 1/7] Model K (D3D12): coalesced 128-bit loads and exact I/O speedups (-0.173 ms at 1440p Q, byte-identical) --- clean/k/dx12/hlsl/k_frame.hlsli | 44 +++++- clean/k/dx12/hlsl/k_in_common.hlsli | 67 +++++++- clean/k/dx12/hlsl/k_out.hlsli | 38 ++++- clean/k/dx12/kernels/k_in.hlsl | 134 ++++++++++------ clean/k/dx12/kernels/k_out.hlsl | 229 +++++++++++++--------------- clean/k/tests/test_k_alpha.py | 3 +- 6 files changed, 329 insertions(+), 186 deletions(-) diff --git a/clean/k/dx12/hlsl/k_frame.hlsli b/clean/k/dx12/hlsl/k_frame.hlsli index 23e3281c..0307fcbb 100644 --- a/clean/k/dx12/hlsl/k_frame.hlsli +++ b/clean/k/dx12/hlsl/k_frame.hlsli @@ -213,8 +213,25 @@ float kio_depth(KFrameIn F, int ix, int iy, int dx, int dy) // spec 42 5.2 [MVlo]: offset of the nearest-depth motion pixel void kio_nearest(KFrameIn F, int ix, int iy, out int odx, out int ody) { - const float c = kio_depth(F, ix, iy, 0, 0), a = kio_depth(F, ix, iy, -1, -1), b = kio_depth(F, ix, iy, 1, -1); - const float d = kio_depth(F, ix, iy, -1, 1), e = kio_depth(F, ix, iy, 1, 1); + float c, a, b, d, e; +#if !WG_DIRECT_TEXTURES + [branch] if (ix >= 1 && ix <= F.rw - 2 && iy >= 1 && iy <= F.rh - 2) { + const uint zp = (uint)F.zpitch * 4u; + const uint voff = ((uint)(F.zby + iy - 1) * (uint)F.zpitch + (uint)(F.zbx + ix - 1)) * 4u; + c = wg_ldf(F.depth + zp + 4u, voff); + a = wg_ldf(F.depth, voff); + b = wg_ldf(F.depth + 8u, voff); + d = wg_ldf(F.depth + 2u * zp, voff); + e = wg_ldf(F.depth + 2u * zp + 8u, voff); + } else +#endif + { + c = kio_depth(F, ix, iy, 0, 0); + a = kio_depth(F, ix, iy, -1, -1); + b = kio_depth(F, ix, iy, 1, -1); + d = kio_depth(F, ix, iy, -1, 1); + e = kio_depth(F, ix, iy, 1, 1); + } const float k = kf_bits(0x3A83126Fu); const bool inv = (F.flags & KIO_INV) != 0; bool edge; @@ -271,11 +288,26 @@ float4 kio_bilinear4(uint img, int w, int h, float cx, float cy) // (min / max are IEEE minNum / maxNum), so only NaN needs the select const int x0 = isnan(fx0) ? 0 : (int)kf_max(kf_min(fx0, (float)w), -1.0f); const int y0 = isnan(fy0) ? 0 : (int)kf_max(kf_min(fy0, (float)h), -1.0f); - const int xa = ki_max(ki_min(x0, w - 1), 0), xb = ki_max(ki_min(x0 + 1, w - 1), 0); - const int ya = ki_max(ki_min(y0, h - 1), 0), yb = ki_max(ki_min(y0 + 1, h - 1), 0); + uint2 q00u, q10u, q01u, q11u; + [branch] if ((uint)x0 <= (uint)(w - 2) && (uint)y0 <= (uint)(h - 2)) { + const uint voff = ((uint)y0 * (uint)w + (uint)x0) * 8u, r1 = img + (uint)w * 8u; + q00u = wg_ld64(img, voff); + q10u = wg_ld64(img + 8u, voff); + q01u = wg_ld64(r1, voff); + q11u = wg_ld64(r1 + 8u, voff); + } else { + const int xa = ki_max(ki_min(x0, w - 1), 0), xb = ki_max(ki_min(x0 + 1, w - 1), 0); + const int ya = ki_max(ki_min(y0, h - 1), 0), yb = ki_max(ki_min(y0 + 1, h - 1), 0); + q00u = wg_ld64(img, ((uint)ya * (uint)w + (uint)xa) * 8u); + q10u = wg_ld64(img, ((uint)ya * (uint)w + (uint)xb) * 8u); + q01u = wg_ld64(img, ((uint)yb * (uint)w + (uint)xa) * 8u); + q11u = wg_ld64(img, ((uint)yb * (uint)w + (uint)xb) * 8u); + } const float omfy = 1.0f - fy; - const float4 q00 = kio_texel_h4(img, (uint)ya * (uint)w + (uint)xa), q10 = kio_texel_h4(img, (uint)ya * (uint)w + (uint)xb); - const float4 q01 = kio_texel_h4(img, (uint)yb * (uint)w + (uint)xa), q11 = kio_texel_h4(img, (uint)yb * (uint)w + (uint)xb); + const float4 q00 = float4(wg_h2f_lo(q00u.x), wg_h2f_hi(q00u.x), wg_h2f_lo(q00u.y), wg_h2f_hi(q00u.y)); + const float4 q10 = float4(wg_h2f_lo(q10u.x), wg_h2f_hi(q10u.x), wg_h2f_lo(q10u.y), wg_h2f_hi(q10u.y)); + const float4 q01 = float4(wg_h2f_lo(q01u.x), wg_h2f_hi(q01u.x), wg_h2f_lo(q01u.y), wg_h2f_hi(q01u.y)); + const float4 q11 = float4(wg_h2f_lo(q11u.x), wg_h2f_hi(q11u.x), wg_h2f_lo(q11u.y), wg_h2f_hi(q11u.y)); const float4 t0 = q00 + kf_opq4(q10 - q00) * fx; const float4 t1 = q01 + kf_opq4(q11 - q01) * fx; return kf_opq4(t0 * omfy) + kf_opq4(t1 * fy); diff --git a/clean/k/dx12/hlsl/k_in_common.hlsli b/clean/k/dx12/hlsl/k_in_common.hlsli index db34b284..107ad866 100644 --- a/clean/k/dx12/hlsl/k_in_common.hlsli +++ b/clean/k/dx12/hlsl/k_in_common.hlsli @@ -11,6 +11,18 @@ #ifndef KI_SGPR #define KI_SGPR 0 // k_in.hlsl defines it for the exact direct tiled form #endif +#ifndef KI_HW16 +#define KI_HW16 1 +#endif +#if KI_HW16 +#if WG_WAVE != 32 +#include "wg_ags.hlsli" +#endif +uint ki_f2h(float x) { return wg_ags_call(0x1cu, 0u, 1u, asuint(x) ^ kf_opq_z_, 0u); } +#else +uint ki_f2h(float x) { return kh_round_bits(x); } +#endif + #if KI_HWMATH // CLOSE-APPROXIMATION variant (opt-in, never the default): the transcendental unit instead of the correctly rounded // sequences: v_log_f32 / v_exp_f32 (about 1 FP32 ulp), v_rcp_f32 and a product for the divisions. The results meet the @@ -31,9 +43,56 @@ float ki_div(float a, float b) #else float ki_log2(float x) { return kf_log2(x); } float ki_exp2(float x) { return kf_exp2(x); } -float ki_rcp(float x) { return kf_div(1.0f, x); } +// Correctly rounded reciprocal: one Newton step on v_rcp_f32 in [2^-32, 2^32] (exhaustively GPU-checked bit-equal to +// wg_div(1, x) in k_out.hlsli ko_rcp_refined), else kf_div(1, x). +float ki_rcp(float x) +{ + [branch] if (x >= asfloat(0x2f800000u) && x <= asfloat(0x4f800000u)) { + const float r = rcp(x); + return mad(mad(-x, r, 1.0f), r, r); + } + return kf_div(1.0f, x); +} float ki_div(float a, float b) { return kf_div(a, b); } #endif + +struct KfDivPrep { + float b, bs, y; + uint eb; + bool b_norm; +}; +KfDivPrep kf_div_prep(float b) +{ + KfDivPrep p; + p.b = b; + const uint ub = asuint(b); + p.eb = (ub >> 23) & 255u; + p.b_norm = (p.eb - 1u < 254u); + p.bs = asfloat((ub & 0x807fffffu) | 0x3f800000u); + float y = rcp(p.bs); + y = mad(mad(-p.bs, y, 1.0f), y, y); + p.y = y; + return p; +} +float kf_div_prepared(float a, KfDivPrep p) +{ +#if KI_HWMATH + return ki_div(a, p.b); +#else + if (a == 0.0f && p.b_norm && p.b > 0.0f) return a; + const uint ua = asuint(a); + const uint ea = (ua >> 23) & 255u; + const float as = asfloat((ua & 0x807fffffu) | 0x3f800000u); + float q = as * p.y; + q = mad(mad(-p.bs, q, as), p.y, q); + q = mad(mad(-p.bs, q, as), p.y, q); + const int d = (int)ea - (int)p.eb; + const uint en = ((asuint(q) >> 23) & 255u) + (uint)d; + [branch] if (ea - 1u < 254u && p.b_norm && en - 1u < 254u) + return asfloat(asuint(q) + ((uint)d << 23)); + return wg_div(a, p.b); +#endif +} // spec 42 decode factor D(t) float ki_decode(float t) { @@ -65,7 +124,7 @@ float ki_S(float z) uint ki_tiled_S_bits(float z, uint table) { const float zp = kf_min(kf_max(z, 0.0f), 1.0f); - if (!(zp > kf_bits(0x3B4D2E1Cu))) return kh_round_bits(kf_bits(0x414EB852u) * zp); + if (!(zp > kf_bits(0x3B4D2E1Cu))) return ki_f2h(kf_bits(0x414EB852u) * zp); const uint u = asuint(zp); // Every reachable 4,096-code bucket is flat or makes one +1 transition // (clean.k.tests.prove_ki_s_breakpoints). The cut is 4,096 for flat buckets. @@ -147,13 +206,13 @@ float3 ki_rgb(KFrameIn F, float4 c) { return kf_opq3(F.kappa * max(c.xyz, 0.0f)) void ki_store_x0(uint x0, int id, int k, float c0, float c1, float c2, float c3) { wg_st64(x0, ((uint)id * 16u + 4u * (uint)k) * 2u, - uint2(kh_round_bits(c0) | (kh_round_bits(c1) << 16), kh_round_bits(c2) | (kh_round_bits(c3) << 16))); + uint2(ki_f2h(c0) | (ki_f2h(c1) << 16), ki_f2h(c2) | (ki_f2h(c3) << 16))); } #if defined(KI_TILED_PACKED_S) void ki_store_tiled_x0(uint x0, int id, int k, uint c0, uint c1, float c2, float c3) { wg_st64(x0, ((uint)id * 16u + 4u * (uint)k) * 2u, - uint2(c0 | (c1 << 16), kh_round_bits(c2) | (kh_round_bits(c3) << 16))); + uint2(c0 | (c1 << 16), ki_f2h(c2) | (ki_f2h(c3) << 16))); } #endif diff --git a/clean/k/dx12/hlsl/k_out.hlsli b/clean/k/dx12/hlsl/k_out.hlsli index 5390f4f1..b87edec3 100644 --- a/clean/k/dx12/hlsl/k_out.hlsli +++ b/clean/k/dx12/hlsl/k_out.hlsli @@ -159,23 +159,41 @@ float ko_div(float a, float b) const float q = a * r; return mad(mad(-bs, q, a), r, q) * s; } +float ko_div_inrange(float a, float b) { return ko_div(a, b); } #else -float ko_div(float a, float b) +float ko_div_inrange(float a, float b) { - const uint ua = asuint(a), ub = asuint(b); - const uint ea = (ua >> 23) & 255u, eb = (ub >> 23) & 255u; - const float bs = asfloat((ub & 0x807fffffu) | 0x3f800000u); + const uint eb = (asuint(b) >> 23) & 255u; + const float bs = asfloat((asuint(b) & 0x807fffffu) | 0x3f800000u); float ys = rcp(bs); ys = mad(mad(-bs, ys, 1.0f), ys, ys); const float y = ys * asfloat((254u - eb) << 23); float q = a * y; q = mad(mad(-b, q, a), y, q); - q = mad(mad(-b, q, a), y, q); + return mad(mad(-b, q, a), y, q); +} +float ko_div(float a, float b) +{ + const uint ea = (asuint(a) >> 23) & 255u, eb = (asuint(b) >> 23) & 255u; + float q = ko_div_inrange(a, b); [branch] if (ea - 92u > 70u || eb - 92u > 70u) q = wg_div(a, b); return q; } #endif -half kh_div(half a, half b) { return kh_round(ko_div((float)a, (float)b)); } +// Exhaustively proven 0-ULP FP32 exact across all 2047 x 2047 nonzero finite FP16 significand pairs (even under +/-1 ULP +// v_rcp_f32 error); [branch] on the rare non-finite/zero guard also keeps the compiler from hoisting the second +// sub-sample pair's E[12] across the first (+10 VGPRs without it). +half kh_div(half a, half b) +{ + const float fa = (float)a, fb = (float)b; + const float r = rcp(fb); + const float q = fa * r; + const uint ua = asuint(fa), ub = asuint(fb); + float res = mad(mad(-fb, q, fa), r, q); + [branch] if (((ua & 0x7f800000u) - 1u >= 0x7f7fffffu) || ((ub & 0x7f800000u) - 1u >= 0x7f7fffffu)) + res = q; + return kh_round(res); +} // ---- binary64 log / 2^x (k_fp.h kd_*) --------------------------------------------------------------------------------- // HIP's kf_ln / kf_exp2 (FP32 double-word path, binary64 path for undecided inputs) equal (float)kd_log((double)x) / @@ -280,12 +298,16 @@ float kf_ln(float x) { return kd_to_f(kd_log((double)x)); } // spec 42 decode factor / spec 43 encode (k_io.hip kio_decode / kio_encode) #define kKioDecodeNear kf_bits(0x3F7FBE88u) #define kKioEncodeNear 1000.0f -float kio_decode(float t) +float kio_decode_far(float t) { - [branch] if (t < kKioDecodeNear) return ko_div(1.0f, 1.0f - t); const float e = kof(kof((t + kf_bits(0x3C4FAE29u)) * 9.0f) * kf_bits(0x3FB8AA3Bu)); return wg_div(kf_exp2(e) - 1.0f, 9.0f * t); } +float kio_decode(float t) +{ + [branch] if (t < kKioDecodeNear) return ko_div(1.0f, 1.0f - t); + return kio_decode_far(t); +} float kio_encode(float Y) { [branch] if (Y < kKioEncodeNear) return ko_div(1.0f, Y + 1.0f); diff --git a/clean/k/dx12/kernels/k_in.hlsl b/clean/k/dx12/kernels/k_in.hlsl index 27e63d3f..30e8392b 100644 --- a/clean/k/dx12/kernels/k_in.hlsl +++ b/clean/k/dx12/kernels/k_in.hlsl @@ -53,9 +53,9 @@ #ifndef KI_HWMATH #define KI_HWMATH 0 #endif -// KI_SGPR (the exact direct form only): the frame's uniforms (KiHost and the direct motion factors) are computed once -// and held in SGPRs (v_readfirstlane), not in VGPRs: kx/k_in_tiled_direct 83 -> <= 72 VGPR (5 workgroups per CU) -#define KI_SGPR (WG_DIRECT_TEXTURES && !KI_HWMATH) +// KI_SGPR: hoist frame-uniform motion factors (sp_x, sp_y, of_x, of_y) once per wave in SGPRs instead of +// recomputing dfold (KIO_M16 / direct) 4x inside ki_fine. +#define KI_SGPR (!KI_HWMATH) #include "k_fp.hlsli" #include "k_frame.hlsli" @@ -119,17 +119,26 @@ float4 ki_tiled_transpose(float4 v, int lane) } #if KI_SGPR -// ki_host(F) with the direct form's motion factors (sp = ki_dsp, of = ki_dmo: the values ki_fine computed per fine -// position) and every field forced to a scalar register: all are wave-uniform (root constants and b1 only). The -// operations and their order are unchanged, so the values are bit-identical. -KiHost ki_host_sgpr(KFrameIn F) +// Wave-uniform motion factors folded once per wave and held in SGPRs (same operations & order as ki_fine). +KiHost ki_host_fold_sgpr(KFrameIn F, KiHost k) { - KiHost k = ki_host(F); +#if WG_DIRECT_TEXTURES const bool mvlo = (F.flags & KIO_MVLO) != 0; k.sp_x = ki_dsp(F.sx, nd_msx, F.W, mvlo ? F.rw : F.W); k.sp_y = ki_dsp(F.sy, nd_msy, F.H, mvlo ? F.rh : F.H); k.of_x = ki_dmo(F.mox, nd_mox, nd_msx); k.of_y = ki_dmo(F.moy, nd_moy, nd_msy); +#else + k.of_x = F.mox; + k.of_y = F.moy; + [branch] if ((F.flags & KIO_M16) != 0) { + const bool mvlo = (F.flags & KIO_MVLO) != 0; + k.sp_x = ki_dsp(F.sx, F.rmsx, F.W, mvlo ? F.rw : F.W); + k.sp_y = ki_dsp(F.sy, F.rmsy, F.H, mvlo ? F.rh : F.H); + k.of_x = ki_dmo(F.mox, F.rmox, F.rmsx); + k.of_y = ki_dmo(F.moy, F.rmoy, F.rmsy); + } +#endif k.xi_x = WaveReadLaneFirst(k.xi_x); k.xi_y = WaveReadLaneFirst(k.xi_y); k.a_x = WaveReadLaneFirst(k.a_x); @@ -162,22 +171,29 @@ WG_KERNEL(k_in_tiled) KInParams P0; K_LOAD_IN_PARAMS(P0) const KInParams P = ki_params(P0); - const KFrameIn F = P.f; + KFrameIn F = P.f; + F.kappa = WaveReadLaneFirst(F.kappa); + F.rho = WaveReadLaneFirst(F.rho); + F.lam = WaveReadLaneFirst(F.lam); #if K_UNIFORM - KiHost hk; - K_LOAD_HOST(hk) + KiHost hk0; + K_LOAD_HOST(hk0) +#if KI_SGPR + const KiHost hk = ki_host_fold_sgpr(F, hk0); +#else + const KiHost hk = hk0; +#endif #elif KI_SGPR - const KiHost hk = ki_host_sgpr(F); + const KiHost hk = ki_host_fold_sgpr(F, ki_host(F)); #else const KiHost hk = ki_host(F); #endif const int lane = (int)wg_lane_id(); const int tiles_x = (F.Bw + 7) / 8, tile = (int)wg_block_id(); - const int bx0 = (tile % tiles_x) * 8, by0 = (tile / tiles_x) * 8; - const int k = lane & 3, local = lane >> 2; - const int bx = bx0 + (local & 7), by = by0 + (local >> 3); - const bool active = bx < F.Bw && by < F.Bh; + const int bx0 = WaveReadLaneFirst((tile % tiles_x) * 8), by0 = WaveReadLaneFirst((tile / tiles_x) * 8); + const int bx = bx0 + ((lane >> 2) & 7), by = by0 + (lane >> 5); const int id = by * F.Bw + bx; + const bool active = bx < F.Bw && by < F.Bh; const bool hdr = (F.flags & KIO_HDR) != 0; const float c1e4 = kf_bits(0x38D1B717u); KI_TILED_CHANNEL ch0 = 0, ch1 = 0; @@ -185,6 +201,7 @@ WG_KERNEL(k_in_tiled) float4 hid = 0.0f; if ((F.flags & KIO_RESET) != 0) { if (active) { + const int k = lane & 3; const int u = 2 * bx + (k & 1), v = 2 * by + (k >> 1); const int ofx = 2 * u + 1 - (v & 1), ofy = 2 * v + (u & 1); const int rx = (int)kf_R(F.jx + kf_fma((float)ofx + 0.5f, hk.a_x, -0.5f)); @@ -194,9 +211,9 @@ WG_KERNEL(k_in_tiled) } return; } - const int tx0 = ki_max(ki_min((int)kf_R(F.jx + kf_fma((float)(4 * bx0 + 1), hk.a_x, -0.5f)) - 1, F.rw - 1), 0); - const int ty0 = ki_max(ki_min((int)kf_R(F.jy + kf_fma((float)(4 * by0 + 1), hk.a_y, -0.5f)) - 1, F.rh - 1), 0); - for (int q = lane; q < 24 * 24; q += 256) { + const int tx0 = WaveReadLaneFirst(ki_max(ki_min((int)kf_R(F.jx + kf_fma((float)(4 * bx0 + 1), hk.a_x, -0.5f)) - 1, F.rw - 1), 0)); + const int ty0 = WaveReadLaneFirst(ki_max(ki_min((int)kf_R(F.jy + kf_fma((float)(4 * by0 + 1), hk.a_y, -0.5f)) - 1, F.rh - 1), 0)); + [loop] for (int q = lane; q < 24 * 24 + (int)kf_opq_z_; q += 256) { const int raw_x = tx0 + q % 24, raw_y = ty0 + q / 24; const int rx = ki_min(raw_x, F.rw - 1), ry = ki_min(raw_y, F.rh - 1); const KiTiledTap value = ki_tiled_tap_from_raw(F, kio_color(F, rx, ry)); @@ -205,55 +222,83 @@ WG_KERNEL(k_in_tiled) } wg_barrier_lds(); if (active) { - const int u = 2 * bx + (k & 1), v = 2 * by + (k >> 1); + const int u = 2 * bx + (lane & 1), v = 2 * by + ((lane >> 1) & 1); // colour neighbourhood first, then each fine position scored as it is produced (the same per-position // operations and the same order of the D sum as HIP; nothing of a fine position stays live but the own // sample's Yh, nx, ny) const int cx = (int)kf_R(F.jx + kf_fma((float)(2 * u + 1), hk.a_x, -0.5f)); const int cy = (int)kf_R(F.jy + kf_fma((float)(2 * v + 1), hk.a_y, -0.5f)); float3 A = 0.0f, lo = 0.0f, hi = 0.0f; - [unroll] for (int ddy = -1; ddy <= 1; ++ddy) { - [unroll] for (int ddx = -1; ddx <= 1; ++ddx) { - const int rx = kio_tap(cx, ddx, 0, F.rw), ry = kio_tap(cy, ddy, 0, F.rh); - const int lx = rx - tx0, ly = ry - ty0; - const bool cached = (uint)lx < 24u && (uint)ly < 24u; - float4 cv; - float om; - [branch] if (cached) { - cv = tile_cv[ly * 24 + lx]; - om = tile_om[ly * 24 + lx]; - } else { // defensive, as HIP: a footprint wider than the tile (the runtime's guard prevents it) - const KiTiledTap fb = ki_tiled_tap(F, rx, ry); - cv = fb.cv; - om = fb.om; + const bool all_cached = (uint)(cx - 1 - tx0) < 22u && (uint)(cy - 1 - ty0) < 22u && + cx >= 1 && cx <= F.rw - 2 && cy >= 1 && cy <= F.rh - 2; + [branch] if (all_cached) { + const int l0 = (cy - ty0) * 24 + (cx - tx0); + [unroll] for (int ddy = -1; ddy <= 1; ++ddy) { + [unroll] for (int ddx = -1; ddx <= 1; ++ddx) { + const int idx_l = l0 + ddy * 24 + ddx; + const float4 cv = tile_cv[idx_l]; + const float om = tile_om[idx_l]; + A = float3(kf_fma(om, cv.x, A.x), kf_fma(om, cv.y, A.y), kf_fma(om, cv.z, A.z)); + const float3 val = cv.xyz * cv.w; + const bool first = ddx == -1 && ddy == -1; + lo = first ? val : min(lo, val); + hi = first ? val : max(hi, val); + } + } + } else { + [loop] for (int ddy = -1; ddy <= 1 + (int)kf_opq_z_; ++ddy) { + [loop] for (int ddx = -1; ddx <= 1 + (int)kf_opq_z_; ++ddx) { + const int rx = kio_tap(cx, ddx, 0, F.rw), ry = kio_tap(cy, ddy, 0, F.rh); + const int lx = rx - tx0, ly = ry - ty0; + const bool cached = (uint)lx < 24u && (uint)ly < 24u; + float4 cv; + float om; + [branch] if (cached) { + cv = tile_cv[ly * 24 + lx]; + om = tile_om[ly * 24 + lx]; + } else { // defensive, as HIP: a footprint wider than the tile (the runtime's guard prevents it) + const KiTiledTap fb = ki_tiled_tap(F, rx, ry); + cv = fb.cv; + om = fb.om; + } + A = float3(kf_fma(om, cv.x, A.x), kf_fma(om, cv.y, A.y), kf_fma(om, cv.z, A.z)); // LDR: om = 1 + const float3 val = cv.xyz * cv.w; + const bool first = ddx == -1 && ddy == -1; + lo = first ? val : min(lo, val); + hi = first ? val : max(hi, val); } - A = float3(kf_fma(om, cv.x, A.x), kf_fma(om, cv.y, A.y), kf_fma(om, cv.z, A.z)); // LDR: om = 1 - const float3 val = cv.xyz * cv.w; - const bool first = ddx == -1 && ddy == -1; - lo = first ? val : min(lo, val); - hi = first ? val : max(hi, val); } } float3 mu = A * kf_bits(0x3DE38E39u); // rounded before 1 - mu (the ISA keeps the products: no kf_opq) if (hdr) { const float den = kf_max(1.0f - mu.x, c1e4); - mu = float3(ki_div(mu.x, den), ki_div(mu.y, den), ki_div(mu.z, den)); + const KfDivPrep den_p = kf_div_prep(den); + mu = float3(kf_div_prepared(mu.x, den_p), kf_div_prepared(mu.y, den_p), kf_div_prepared(mu.z, den_p)); } const float pm = ki_phi(mu.x, hdr); const float3 mp = kf_opq3(mu * pm); const float3 eta = 0.5f * (hi - lo); const float3 nu = float3(kf_fma(hi.x - lo.x, 8.0f, 1.0f), kf_fma(hi.y - lo.y, 8.0f, 1.0f), kf_fma(hi.z - lo.z, 8.0f, 1.0f)); + const bool nu_pos_fin = (((asuint(nu.x) >> 23) & 255u) - 1u < 254u) && + (((asuint(nu.y) >> 23) & 255u) - 1u < 254u) && + (((asuint(nu.z) >> 23) & 255u) - 1u < 254u) && + nu.x > 0.0f && nu.y > 0.0f && nu.z > 0.0f; const int oq = (1 - (v & 1)) + 2 * (u & 1); // (2u + 1 - (v mod 2), 2v + (u mod 2)) as a fine position index // fine positions of the display position, in the order (2u, 2v), (2u+1, 2v), (2u, 2v+1), (2u+1, 2v+1) bool valid = true; float D = 0.0f, own_hx = 0.0f, own_nx = 0.0f, own_ny = 0.0f; - [unroll] for (int q = 0; q < 4; ++q) { + [loop] for (int q = 0; q < 4 + (int)kf_opq_z_; ++q) { const KiFine fq = ki_fine(P, hk, 2 * u + (q & 1), 2 * v + (q >> 1)); valid = valid && fq.valid; const float ph = ki_phi(fq.h.x, hdr); const float3 g = max(abs(mp - kf_opq3(fq.h * ph)) - eta, 0.0f); - const float3 dl = float3(ki_div(g.x, nu.x), ki_div(g.y, nu.y), ki_div(g.z, nu.z)); - const float score = kf_min(kf_fma(dl.z, 0.5f, kf_fma(dl.x, 2.0f, 0.5f * dl.y)), 1.0f); + float score = 0.0f; + [branch] if (!(g.x == 0.0f && g.y == 0.0f && g.z == 0.0f && nu_pos_fin)) { + const float dx = (g.x == 0.0f && nu_pos_fin) ? g.x : ki_div(g.x, nu.x); + const float dy = (g.y == 0.0f && nu_pos_fin) ? g.y : ki_div(g.y, nu.y); + const float dz = (g.z == 0.0f && nu_pos_fin) ? g.z : ki_div(g.z, nu.z); + score = kf_min(kf_fma(dz, 0.5f, kf_fma(dx, 2.0f, 0.5f * dy)), 1.0f); + } D = D + score; if (q == oq) { own_hx = fq.h.x; @@ -278,5 +323,6 @@ WG_KERNEL(k_in_tiled) if (!active) return; float hsum = (hs.x + hs.w) + (hs.y + hs.z); hsum = hsum * 0.25f; - KI_TILED_STORE(P.x0, id, k, ch0, ch1, kf_finite(hsum) ? hsum : 0.0f, ch3); + const int k_out = lane & 3; + KI_TILED_STORE(P.x0, id, k_out, ch0, ch1, kf_finite(hsum) ? hsum : 0.0f, ch3); } diff --git a/clean/k/dx12/kernels/k_out.hlsl b/clean/k/dx12/kernels/k_out.hlsl index 692713bb..1277060e 100644 --- a/clean/k/dx12/kernels/k_out.hlsl +++ b/clean/k/dx12/kernels/k_out.hlsl @@ -100,13 +100,13 @@ float4 ko_bilinear_hc(int w, int h, float cx, float cy) x0 = fx0 != fx0 ? 0 : x0; y0 = fy0 != fy0 ? 0 : y0; uint2 q00, q10, q01, q11; - // HIP KO_ADDR_FAST: all four corners inside (no clamp): one byte offset, the corners at +0 / +8 bytes on two rows + // HIP KO_ADDR_FAST: all four corners inside (no clamp): two 128-bit row loads instead of four 64-bit loads [branch] if ((uint)x0 <= (uint)(w - 2) && (uint)y0 <= (uint)(h - 2)) { const uint voff = ((uint)y0 * (uint)w + (uint)x0) * 8u, r1 = p_Hc + (uint)w * 8u; - q00 = wg_ld64(p_Hc, voff); - q10 = wg_ld64(p_Hc + 8u, voff); - q01 = wg_ld64(r1, voff); - q11 = wg_ld64(r1 + 8u, voff); + const uint4 row0 = wg_ld128(p_Hc, voff); + const uint4 row1 = wg_ld128(r1, voff); + q00 = row0.xy; q10 = row0.zw; + q01 = row1.xy; q11 = row1.zw; } else { const int xa = ko_clampi(x0, w), xb = ko_clampi(x0 + 1, w); const int ya = ko_clampi(y0, h), yb = ko_clampi(y0 + 1, h); @@ -116,21 +116,21 @@ float4 ko_bilinear_hc(int w, int h, float cx, float cy) q11 = wg_ld64(p_Hc, ((uint)yb * (uint)w + (uint)xb) * 8u); } const float omfy = 1.0f - fy; - const float4 v00 = float4(wg_h2f_lo(q00.x), wg_h2f_hi(q00.x), wg_h2f_lo(q00.y), wg_h2f_hi(q00.y)); - const float4 v10 = float4(wg_h2f_lo(q10.x), wg_h2f_hi(q10.x), wg_h2f_lo(q10.y), wg_h2f_hi(q10.y)); - const float4 v01 = float4(wg_h2f_lo(q01.x), wg_h2f_hi(q01.x), wg_h2f_lo(q01.y), wg_h2f_hi(q01.y)); - const float4 v11 = float4(wg_h2f_lo(q11.x), wg_h2f_hi(q11.x), wg_h2f_lo(q11.y), wg_h2f_hi(q11.y)); + const float4 v00 = float4(f16tof32(q00.x), wg_h2f_hi(q00.x), f16tof32(q00.y), wg_h2f_hi(q00.y)); + const float4 v10 = float4(f16tof32(q10.x), wg_h2f_hi(q10.x), f16tof32(q10.y), wg_h2f_hi(q10.y)); + const float4 v01 = float4(f16tof32(q01.x), wg_h2f_hi(q01.x), f16tof32(q01.y), wg_h2f_hi(q01.y)); + const float4 v11 = float4(f16tof32(q11.x), wg_h2f_hi(q11.x), f16tof32(q11.y), wg_h2f_hi(q11.y)); const float4 t0 = v00 + (v10 - v00) * fx; const float4 t1 = v01 + (v11 - v01) * fx; return t0 * omfy + t1 * fy; } // E of the 12 taps (index 3 l + tc) for a sub-sample pair at (us, vs), in the spec's operation order (HIP each_E); FP16 -// pairs as packed words (kp_*) -void ko_each_E(uint us, uint vs, half lines[4], half cols[4], half tA, half tB, half tC, out uint E[12]) +// pairs as packed words (kp_*). lines[l] = v0 + (half)l to avoid holding 4 extra half VGPRs. +void ko_each_E(uint us, uint vs, half v0, half cols[4], half tA, half tB, half tC, out uint E[12]) { [unroll] for (int l = 0; l < 4; ++l) { - const uint dv = kp_rsub_s(lines[l], vs); + const uint dv = kp_rsub_s(v0 + (half)l, vs); const uint t2v = kp_mul_s_n(kp_mul_n(dv, dv), tA); const uint t3v = kp_mul_s(dv, tB); uint du = kp_rsub_s(cols[l], us); @@ -145,26 +145,22 @@ void ko_each_E(uint us, uint vs, half lines[4], half cols[4], half tA, half tB, } } -// 3.6 luma history value of the four sub-samples from the (soft-limited) 5 x 5 window: row cubics (unit: taps known -// to lie in [0, 1), ko_sat dropped), the column cubic, the clamp to the 2 x 2 neighbourhood +// 3.6 luma history value of the four sub-samples from the (soft-limited) 5 x 5 window: 10 unique row cubics computed +// one column j at a time (5 VGPRs rc[5] instead of 10, dropping 6 duplicate row cubics = 30 FMAs, 100% bit-identical). void ko_ellv(float Lw[5][5], KoAxis axx, KoAxis axy, bool unit, out float ellv[4]) { - [unroll] for (int i = 0; i < 2; ++i) - [unroll] for (int j = 0; j < 2; ++j) { - float rows[4]; - [unroll] for (int mr = 0; mr < 4; ++mr) - rows[mr] = unit ? ko_cubic_unit(Lw[i + mr][j], Lw[i + mr][j + 1], Lw[i + mr][j + 2], Lw[i + mr][j + 3], axx) - : ko_cubic(Lw[i + mr][j], Lw[i + mr][j + 1], Lw[i + mr][j + 2], Lw[i + mr][j + 3], axx); - const float val = ko_cubic(rows[0], rows[1], rows[2], rows[3], axy); - float lo = Lw[i + 1][j + 1], hi = Lw[i + 1][j + 1]; - lo = min(lo, Lw[i + 1][j + 2]); - hi = max(hi, Lw[i + 1][j + 2]); - lo = min(lo, Lw[i + 2][j + 1]); - hi = max(hi, Lw[i + 2][j + 1]); - lo = min(lo, Lw[i + 2][j + 2]); - hi = max(hi, Lw[i + 2][j + 2]); + [unroll] for (int j = 0; j < 2; ++j) { + float rc[5]; + [unroll] for (int r = 0; r < 5; ++r) + rc[r] = unit ? ko_cubic_unit(Lw[r][j], Lw[r][j + 1], Lw[r][j + 2], Lw[r][j + 3], axx) + : ko_cubic(Lw[r][j], Lw[r][j + 1], Lw[r][j + 2], Lw[r][j + 3], axx); + [unroll] for (int i = 0; i < 2; ++i) { + const float val = ko_cubic(rc[i], rc[i + 1], rc[i + 2], rc[i + 3], axy); + const float lo = min(min(Lw[i + 1][j + 1], Lw[i + 1][j + 2]), min(Lw[i + 2][j + 1], Lw[i + 2][j + 2])); + const float hi = max(max(Lw[i + 1][j + 1], Lw[i + 1][j + 2]), max(Lw[i + 2][j + 1], Lw[i + 2][j + 2])); ellv[j + 2 * i] = min(max(val, lo), hi); } + } } #if defined(WG_DIRECT_TEXTURES) @@ -192,13 +188,13 @@ WG_KERNEL(k_out_pixel_c) const bool active = x_raw < W16; const int x = active ? x_raw : W16 - 1, y = (tile / tiles_x) * 2 + wg_uniform(lane >> 6); const bool hdr = (f_flags & KIO_HDR) != 0, mvlo = (f_flags & KIO_MVLO) != 0; - const float px = (float)x + 0.5f, py = (float)y + 0.5f; - const float rho = ko_rho(); + const float px = (float)x + 0.5f, py = WaveReadLaneFirst((float)y + 0.5f); + const float rho = WaveReadLaneFirst(ko_rho()); // 3.1 network channels - const int u = (int)trunc(f_s * (float)x), v = (int)trunc(f_s * (float)y); + const int u = (int)trunc(f_s * (float)x), v = WaveReadLaneFirst((int)trunc(f_s * (float)y)); const int k = (u & 1) + 2 * (v & 1); - const int bxn = ki_min(u >> 1, f_Bw - 1), byn = ki_min(v >> 1, f_Bh - 1); + const int bxn = ki_min(u >> 1, f_Bw - 1), byn = WaveReadLaneFirst(ki_min(v >> 1, f_Bh - 1)); #if KO_COMPACT const uint2 nvw = wg_ld64(p_N, (((uint)(2 * byn + (k >> 1)) * (2u * (uint)f_Bw) + (uint)(2 * bxn + (k & 1))) * 4u) * 2u); const half nv0 = kh_bits(nvw.x & 0xffffu), nv1 = kh_bits(nvw.x >> 16), nv9 = kh_bits(nvw.y & 0xffffu); @@ -209,10 +205,10 @@ WG_KERNEL(k_out_pixel_c) // 4.1 local origin, 4.3 current-colour taps const float o_x = kf_R(mad((float)(16 * (x / 16)) + 0.5f, u_rhox, u_jmx)) - 4.0f; - const float o_y = kf_R(mad((float)(16 * (y / 16)) + 0.5f, u_rhoy, u_jmy)) - 4.0f; - const float Pxf = mad(px, u_rhox, u_jmx) - o_x, Pyf = mad(py, u_rhoy, u_jmy) - o_y; - const half Ux = kh_round(Pxf), Uy = kh_round(Pyf); - half cbar0, cbar1, cbar2, cbar3; + const float o_y = WaveReadLaneFirst(kf_R(mad((float)(16 * (y / 16)) + 0.5f, u_rhoy, u_jmy)) - 4.0f); + const float Pxf = mad(px, u_rhox, u_jmx) - o_x, Pyf = WaveReadLaneFirst(mad(py, u_rhoy, u_jmy) - o_y); + const half Ux = kh_round(Pxf), Uy = kh_bits(WaveReadLaneFirst(ko_f2h(Pyf))); + uint cbar_lo, cbar_hi; { // ko_cbar_load / ko_cbar_math const half flx = floor(Ux), fly = floor(Uy); const half fx = kh_sat(Ux - flx), fy = kh_sat(Uy - fly); @@ -222,12 +218,15 @@ WG_KERNEL(k_out_pixel_c) w[1] = kh_mul(fx, one - fy); w[2] = kh_mul(one - fx, fy); w[3] = kh_mul(fx, fy); - const int bx = (int)(o_x + (float)flx), by = (int)(o_y + (float)fly); + const int bx = (int)(o_x + (float)flx), by = WaveReadLaneFirst((int)(o_y + (float)fly)); uint2 tq[4]; - // HIP KO_ADDR_FAST: the 2 x 2 taps inside the render image (no clamp): one byte offset, +0 / +8 bytes on two rows + // HIP KO_ADDR_FAST: the 2 x 2 taps inside the render image (no clamp): two 128-bit row loads [branch] if (bx >= 0 && bx <= f_rw - 2 && by >= 0 && by <= f_rh - 2) { const uint voff = ((uint)by * (uint)f_rw + (uint)bx) * 8u; - [unroll] for (int q = 0; q < 4; ++q) tq[q] = wg_ld64(p_ctap + (uint)(q >> 1) * (uint)f_rw * 8u + 8u * (uint)(q & 1), voff); + const uint4 cr0 = wg_ld128(p_ctap, voff); + const uint4 cr1 = wg_ld128(p_ctap + (uint)f_rw * 8u, voff); + tq[0] = cr0.xy; tq[1] = cr0.zw; + tq[2] = cr1.xy; tq[3] = cr1.zw; } else { [unroll] for (int q = 0; q < 4; ++q) { const int tx = ko_clampi(bx + (q & 1), f_rw), ty = ko_clampi(by + (q >> 1), f_rh); @@ -242,23 +241,19 @@ WG_KERNEL(k_out_pixel_c) } // FP16 results converted to FP32 below pass the opaque (kp_opq / koh): the driver folds an FP16 fma / add // followed by the conversion into one v_fma_mix_f32 (no FP16 rounding in between) - lo = kp_opq(lo); - hi = kp_opq(hi); - cbar0 = kp_lo(lo); - cbar1 = kp_hi(lo); - cbar2 = kp_lo(hi); - cbar3 = kp_hi(hi); + cbar_lo = kp_opq(lo); + cbar_hi = kp_opq(hi); } // 3.3 motion float mvx, mvy; if (mvlo) { - const int ix = (int)kf_R(mad(px, u_rhox, u_jmx)), iy = (int)kf_R(mad(py, u_rhoy, u_jmy)); + const int ix = (int)kf_R(mad(px, u_rhox, u_jmx)), iy = WaveReadLaneFirst((int)kf_R(mad(py, u_rhoy, u_jmy))); int dx, dy; ko_nearest(ix, iy, dx, dy); ko_motion(ko_clampi(ix + dx, f_rw), ko_clampi(iy + dy, f_rh), mvx, mvy); } else { - const int mx = (int)kf_R(mad(px, 1.0f, -0.5f)), my = (int)kf_R(mad(py, 1.0f, -0.5f)); + const int mx = (int)kf_R(mad(px, 1.0f, -0.5f)), my = WaveReadLaneFirst((int)kf_R(mad(py, 1.0f, -0.5f))); ko_motion(ko_clampi(mx, f_W), ko_clampi(my, f_H), mvx, mvy); } // the texture's motion scale folded into the factor and its offset in raw units, as the buffer form gets them in @@ -284,53 +279,35 @@ WG_KERNEL(k_out_pixel_c) wy * u_invH <= 1.0f; // F2: Ybar of the aligned 2 x 2 group through LDS (lanes lane, lane ^ 1, lane ^ 64, lane ^ 65) - lds_ybar[lane] = kh_u(cbar0); - - // 3.5 colour history - float4 Hh = ko_bilinear_hc(W16, H16, wx * u_invW16, wy * u_invH16); + lds_ybar[lane] = cbar_lo & 0xffffu; - // 3.6 luma history: the 5 x 5 window of the four sub-samples + // 3.6 luma history: the 5 x 5 window of the four sub-samples (25 two-byte loads on interior fast path, 10 unique + // row cubics in ko_ellv; Lmax16 < 0x3becu is exact-equivalent to Lmax32 < 0x3F7D8000u on all 65,536 FP16 patterns; + // computed before Hh so Hh[4] does not stay live across Lw[5][5]) const KoAxis axx = ko_axis(wx), axy = ko_axis(wy); const int LW = 2 * W16, LH = 2 * H16; float Lw[5][5]; - // largest FP32 bit pattern of the widened window (as unsigned): the widening keeps the order of the non-negative - // FP16 values and maps -0, negatives (sign bit) and NaN / inf above every finite non-negative value, so - // "below 0x3F7D8000 (= (float)h(0x3bec), 0.990234375)" is the FP16 test "bits below 0x3bec" for every window - // (the 16-bit form zero-extended every d16 load with a v_cvt_u32_u16) - uint Lmax = 0u; - // HIP KO_WIN_FAST: window inside the image (no index clamped; every lane but those at the borders): one per-lane - // byte offset, the 25 texels at uniform row offsets + 2 c bytes. (u32)(n - 1) <= size - 5 is 0 <= n - 1 and - // n + 3 <= size - 1. (Five 12-byte row loads + shifts instead of the 25 two-byte loads measured slower.) + uint Lmax16 = 0u; [branch] if ((uint)(axx.n - 1) <= (uint)(LW - 5) && (uint)(axy.n - 1) <= (uint)(LH - 5)) { const uint voff = ((uint)(axy.n - 1) * (uint)LW + (uint)(axx.n - 1)) * 2u; [unroll] for (int r = 0; r < 5; ++r) [unroll] for (int c = 0; c < 5; ++c) { - Lw[r][c] = wg_h2f(wg_ld16(p_L + (uint)(r * LW) * 2u + 2u * (uint)c, voff)); - Lmax = max(Lmax, asuint(Lw[r][c])); + const uint h = wg_ld16(p_L + (uint)(r * LW) * 2u + 2u * (uint)c, voff); + Lmax16 = max(Lmax16, h); + Lw[r][c] = f16tof32(h); } } else { [unroll] for (int r = 0; r < 5; ++r) { const uint rb = (uint)ko_clampi(axy.n - 1 + r, LH) * (uint)LW; [unroll] for (int c = 0; c < 5; ++c) { - Lw[r][c] = wg_h2f(wg_ld16(p_L, (rb + (uint)ko_clampi(axx.n - 1 + c, LW)) * 2u)); - Lmax = max(Lmax, asuint(Lw[r][c])); + const uint h = wg_ld16(p_L, (rb + (uint)ko_clampi(axx.n - 1 + c, LW)) * 2u); + Lmax16 = max(Lmax16, h); + Lw[r][c] = f16tof32(h); } } } - - // 3.2 blend logit - const float t = 0.5f * (float)nv9; - const float t2 = t * t; - const float g = ko_div(t * (t2 + 27.0f), mad(t2, 9.0f, 27.0f)); - const float alpha = t < -3.0f ? 0.0f : (t > 3.0f ? 1.0f : mad(g, 0.5f, 0.5f)); - - // Every tap in [0, 0.99) (FP16 bits below 0x3bec: +0 .. 0.98974609375, the usual window): no soft limit, and - // ko_sat of a row cubic's taps is the identity, so the row cubics run without the clamps (ko_cubic_unit, the same - // operations otherwise). Any other window (a tap >= 0.99, NaN, inf, negative or -0): the soft limit on every tap - // (ko_ell is the identity below 0.99, so applying it to such a window equals HIP's needs_soft test) and the - // clamped cubics. float ellv[4]; - [branch] if (Lmax < 0x3F7D8000u) { + [branch] if (Lmax16 < 0x3becu) { ko_ellv(Lw, axx, axy, true, ellv); } else { [unroll] for (int r = 0; r < 5; ++r) @@ -338,6 +315,22 @@ WG_KERNEL(k_out_pixel_c) ko_ellv(Lw, axx, axy, false, ellv); } + // 3.5 colour history (after Lw[5][5] is dead) + float4 Hh = ko_bilinear_hc(W16, H16, wx * u_invW16, wy * u_invH16); + + // 3.2 blend logit (|t| <= 3 keeps 9 t^2 + 27 in [27, 108]: ko_div_inrange is exact without a wg_div cold branch) + const float t = 0.5f * (float)nv9; + float alpha; + if (t < -3.0f) { + alpha = 0.0f; + } else if (t > 3.0f) { + alpha = 1.0f; + } else { + const float t2 = t * t; + const float g = (t == t) ? ko_div_inrange(t * (t2 + 27.0f), mad(t2, 9.0f, 27.0f)) : t; + alpha = mad(g, 0.5f, 0.5f); + } + // 3.8 / 3.9 history luma float Hl[4]; if (hdr) { @@ -353,7 +346,7 @@ WG_KERNEL(k_out_pixel_c) [unroll] for (int s = 0; s < 4; ++s) Hl[s] = lin[s] * ko_rcp_output_bounded(lin[s] + 1.0f); } [branch] if (!near) { - [loop] for (int s = 0; s < 4; ++s) { + [loop] for (int s = 0; s < 4 + (int)ko_z; ++s) { const float lin = rho * kof(ellv[s] * kio_decode(ellv[s])); Hl[s] = lin * kio_encode(lin); } @@ -371,6 +364,10 @@ WG_KERNEL(k_out_pixel_c) } const float m0 = mad(alphap, 1.0f - bp, bp); const float m1 = mad(bp, 1.0f - alphap * alphap, alphap * alphap); + const bool F0 = (float)kh_bits(0x251fu) > m0; // (float)(h16)0.02f + const bool F1 = m0 < 0.25f && alpha < 0.25f; + // Pack mh16 and age16 together so m0, m1, and alpha are dead before 5.1-5.6 + const uint ma_r = ko_f2h(m0) | (ko_f2h(min(max(ko_div(m1, m1 + 1.0f), 0.01f), 1.0f)) << 16); // F2 (Ybar of the group, stored above) GroupMemoryBarrierWithGroupSync(); @@ -386,10 +383,8 @@ WG_KERNEL(k_out_pixel_c) const int qv = (int)(float)round(kh_mul(z, (half)8)) + 8; qs[c] = ki_max(ki_min(qv, 15), 0); } - const bool F0 = (float)kh_bits(0x251fu) > m0; // (float)(h16)0.02f - const bool F1 = m0 < 0.25f && alpha < 0.25f; const int tix = (16 * qs[0] + qs[1]) + 256 * ((int)F0 + 2 * (int)F1 + 4 * (int)F2); - const uint4 ent = wg_ld128(p_table, (uint)tix * 16u); + const uint4 ent = wg_mem.Load4((p_table + (uint)tix * 16u) & ~15u); const half tA = kh_bits(ent.x & 0xffffu), tC = kh_bits(ent.x >> 16), tB = kh_bits(ent.y & 0xffffu); const half tS = kh_bits(ent.y >> 16), tK = kh_bits(ent.z >> 16), tk6 = kh_bits(ent.w & 0xffffu); const bool O = (ent.z & 0xffffu) != 0u; @@ -398,37 +393,30 @@ WG_KERNEL(k_out_pixel_c) half Xs[2], Ys[2]; [unroll] for (int s = 0; s < 2; ++s) { Xs[s] = kh_round(mad((float)(2 * x + s) + 0.5f, u_rho2x, u_jmx) - o_x); - Ys[s] = kh_round(mad((float)(2 * y + s) + 0.5f, u_rho2y, u_jmy) - o_y); + Ys[s] = kh_bits(WaveReadLaneFirst(ko_f2h(mad((float)(2 * y + s) + 0.5f, u_rho2y, u_jmy) - o_y))); } const half uP = O ? Uy : Ux, vP = O ? Ux : Uy; - // 5.3 taps + // 5.3 taps (keep T[12] as half so driver doesn't unpack, drop lines[4] via v0 + (half)l, drop us2/vs2/gam[4]) const half one = (half)1; const half v0 = floor(vP) - one; half cl = uP + kh_mul(v0 - vP, tS); - half lines[4], cols[4]; + half cols[4]; half T[12]; const float o_ln = O ? o_x : o_y, o_cl = O ? o_y : o_x; const int n_ln = O ? f_rw : f_rh, n_cl = O ? f_rh : f_rw; int as_[4]; - [unroll] for (int l = 0; l < 4; ++l) { - if (l > 0) cl = cl + tS; - lines[l] = v0 + (half)l; - cols[l] = round(cl) - one; - as_[l] = ko_clampi((int)(o_ln + (float)lines[l]), n_ln); - } - // HIP KO_TAP_FAST: where no column index of the lane is clamped (|cols| <= 2045: the FP16 sums cols + tc are exact - // integers; c0 = (int)(o_cl + cols) in [0, n_cl - 3]: the FP32 sums o_cl + cols + tc are exact too, |o_cl| < 2^15), - // tap tc is column c0 + tc unclamped: one address per line, the taps +0, +1, +2 texels along the column direction - // (one row of the buffer apart for O). NaN / inf columns fail the first test, a NaN origin the second. bool tfast = abs(o_cl) < 32768.0f; int cb[4]; - [unroll] for (int l3 = 0; l3 < 4; ++l3) { - cb[l3] = (int)(o_cl + (float)cols[l3]); - tfast = tfast && abs((float)cols[l3]) <= 2045.0f && cb[l3] >= 0 && cb[l3] <= n_cl - 3; + [unroll] for (int l = 0; l < 4; ++l) { + if (l > 0) cl = cl + tS; + const half ln_l = v0 + (half)l; + const half col_l = round(cl) - one; + cols[l] = col_l; + as_[l] = ko_clampi((int)(o_ln + (float)ln_l), n_ln); + cb[l] = (int)(o_cl + (float)col_l); + tfast = tfast && abs((float)col_l) <= 2045.0f && cb[l] >= 0 && cb[l] <= n_cl - 3; } - // the two forms give the 12 tap byte offsets; the loads follow the branch (loaded inside both, the T values met - // in one 16-bit join that the driver resolved with a zero extension per tap and register swaps) uint toff[12]; [branch] if (tfast) { const uint st = O ? (uint)f_rw * 2u : 2u; @@ -437,33 +425,31 @@ WG_KERNEL(k_out_pixel_c) [unroll] for (int tc = 0; tc < 3; ++tc) toff[3 * l2 + tc] = b + (uint)tc * st; } } else { - [unroll] for (int l2 = 0; l2 < 4; ++l2) + [unroll] for (int l2 = 0; l2 < 4; ++l2) { + const half col_l = cols[l2]; [unroll] for (int tc = 0; tc < 3; ++tc) { - const half colv = cols[l2] + (half)tc; + const half colv = col_l + (half)tc; const int c = ko_clampi((int)(o_cl + (float)colv), n_cl); const uint row = (uint)(O ? c : as_[l2]), col = (uint)(O ? as_[l2] : c); toff[3 * l2 + tc] = (row * (uint)f_rw + col) * 2u; } + } } [unroll] for (int n2 = 0; n2 < 12; ++n2) T[n2] = kh_bits(wg_ld16(p_ctapT, toff[n2])); // 5.4 - 5.6 filtered values, 6.1 blend weights (sub-samples (i, 0), (i, 1) as the halves of half2 values) const half kux = kh_bits(u_kuxkvy & 0xffffu), kvy = kh_bits(u_kuxkvy >> 16); const half k_u = O ? kvy : kux, k_v = O ? kux : kvy; - half Fv[4], gam[4]; + half Fv[4]; const uint xs2 = kp_pack(Xs[0], Xs[1]); const uint m12 = 0xca00ca00u; // (h16)-12 twice bool plain = true; - uint us2[2], vs2[2], gam2[2]; + uint gam2[2]; [unroll] for (int i2 = 0; i2 < 2; ++i2) { const uint ys2 = kp_splat(Ys[i2]); const uint us = O ? ys2 : xs2, vs = O ? xs2 : ys2; - us2[i2] = us; - vs2[i2] = vs; uint E[12]; - ko_each_E(us, vs, lines, cols, tA, tB, tC, E); - // tap 0 starts the sums (peeled: DXC kept the loop with an n == 0 test rolled, E / T then lived in indexed - // register arrays) + ko_each_E(us, vs, v0, cols, tA, tB, tC, E); uint gg = kh2_u(exp2(kh2_h(E[0]))); // ko_exp2h2: two v_exp_f16 uint sgT = kp_mul_s(gg, T[0]), sg = gg, maxE = E[0]; [unroll] for (int n = 1; n < 12; ++n) { @@ -484,18 +470,13 @@ WG_KERNEL(k_out_pixel_c) const uint wu = kp_sat_nz(kp_fma_ss(kp_abs(duf), k_u, c45)); const uint wv = kp_sat_nz(kp_fma_ss(kp_abs(dvf), k_v, c45)); gam2[i2] = kp_mul(kp_fma_ss(wv, tK, tk6), wu); - gam[2 * i2] = kp_lo(gam2[i2]); - gam[2 * i2 + 1] = kp_hi(gam2[i2]); } [branch] if (!plain) { [unroll] for (int i3 = 0; i3 < 2; ++i3) { + const uint ys2_o = kp_splat(Ys[i3]); + const uint us_o = (O ? ys2_o : xs2) ^ (ko_z * 0x10001u), vs_o = (O ? xs2 : ys2_o) ^ (ko_z * 0x10001u); uint E[12]; - // the opaque inputs keep these E from being merged with the main loop's (whose values would then stay - // live up to here) - ko_each_E(us2[i3] ^ (ko_z * 0x10001u), vs2[i3] ^ (ko_z * 0x10001u), lines, cols, tA, tB, tC, E); - // HIP's kh2_gtmask selection per half: keep the best where h(E[n] - best) is negative and not NaN (FP16 bits - // in [0x8000, 0xfc00]); the T taps as half values (no splatted words: a 32-bit use of T here made the - // driver unpack the hot path's T registers) + ko_each_E(us_o, vs_o, v0, cols, tA, tB, tC, E); uint bestE = E[0]; half bTl = T[0], bTh = T[0]; [unroll] for (int n = 1; n < 12; ++n) { @@ -509,8 +490,8 @@ WG_KERNEL(k_out_pixel_c) if (kp_hi(bestE) < kp_hi(m12)) Fv[2 * i3 + 1] = bTh; } } - const half gbar = kh_mul((half)0.25, (gam[0] + gam[2]) + (gam[1] + gam[3])); - const half mh = kh_round(m0); + const half gbar = kh_mul((half)0.25, (kp_lo(gam2[0]) + kp_lo(gam2[1])) + (kp_hi(gam2[0]) + kp_hi(gam2[1]))); + const half mh = kh_bits(ma_r & 0xffffu); const half MU = kh_bits(0x27aeu), NU = kh_bits(0x3c20u); // (h16)0.03f, (h16)(1.0 / 0.97) const half rr = kh_bits(0x2e66u) + kh_mul(kh_bits(0x3b33u), mh); // 0.1 + 0.9 mh const half d0 = kh_mul(mh - MU, NU); @@ -518,7 +499,8 @@ WG_KERNEL(k_out_pixel_c) const half beta_c = koh(kh_sat(mad(gbar, rr - d1, d1))); // (converted to FP32 in 6.5) // 6.2 guard - half Ybar = cbar0, Cobar = cbar1, Cgbar = cbar2; + half Ybar = kp_lo(cbar_lo), Cobar = kp_hi(cbar_lo), Cgbar = kp_lo(cbar_hi); + const half cbar3 = kp_hi(cbar_hi); const float guard = kof(kof(kof((float)Ybar * (float)Fv[0]) * (float)Fv[1]) * (float)Fv[2]) * (float)Fv[3]; if (!kf_finite(guard)) { [unroll] for (int s = 0; s < 4; ++s) Fv[s] = (half)0; @@ -564,18 +546,19 @@ WG_KERNEL(k_out_pixel_c) const float s2 = ((Coo - Cgo) - eps < -s1) ? (Cgo - Coo) + eps : s1; const float s3 = ((-Coo - Cgo) - eps < -s2) ? (Coo + Cgo) + eps : s2; const float kk = s3 <= Yp ? 1.0f : ko_div(Yp, s3); - const float age = min(max(ko_div(m1, m1 + 1.0f), 0.01f), 1.0f); // kf_sat of a value >= 0.01 (maxNum: never NaN) if (!active) return; wg_st64(p_Hcout, ((uint)y * (uint)W16 + (uint)x) * 8u, - uint2(ko_f2h(Yp) | (ko_f2h(Coo * kk) << 16), ko_f2h(Cgo * kk) | (ko_f2h(age) << 16))); + uint2(ko_f2h(Yp) | (ko_f2h(Coo * kk) << 16), ko_f2h(Cgo * kk) | ((ma_r >> 16) << 16))); // 6.7 game output if (x < f_W && y < f_H) { float Yg = Yo, Cog = Coo, Cgg = Cgo; if (hdr) { - const float d = Yo < kKioDecodeNear ? ko_rcp_refined(1.0f - Yo) : kio_decode(Yo); - const float kinv = ko_kinv(); + float d; + [branch] if (Yo < kKioDecodeNear) d = ko_rcp_refined(1.0f - Yo); + else d = kio_decode_far(Yo); + const float kinv = WaveReadLaneFirst(ko_kinv()); Yg = kinv * kof(Yo * d); Cog = kinv * kof(Coo * d); Cgg = kinv * kof(Cgo * d); diff --git a/clean/k/tests/test_k_alpha.py b/clean/k/tests/test_k_alpha.py index 62ed38d0..1c6abd47 100644 --- a/clean/k/tests/test_k_alpha.py +++ b/clean/k/tests/test_k_alpha.py @@ -252,7 +252,8 @@ def test_d3d12_output_kernel(self): # the stored tap's A: RN16 of the colour's fourth value; the bilinear: RN16(w0 a0), then three FP16 fmas self.assertIn("tap = uint2(hY | (hCo << 16), hCg | (ko_f2h(c.w) << 16));", oi) self.assertIn("hi = q == 0 ? kp_mul_s(t.y, w[q]) : kp_fma_s(t.y, w[q], hi);", o) - self.assertIn("cbar3 = kp_hi(hi);", o) + self.assertIn("cbar_hi = kp_opq(hi);", o) + self.assertIn("cbar3 = kp_hi(cbar_hi);", o) # every output form stores cbar3 unchanged self.assertIn("ko_f2h(B) | (kh_u(cbar3) << 16)", o) # RGBA16F rows self.assertIn("ko_store_out(x, y, R, G, B, cbar3);", o) # the direct texture From 182cc43c990b7a4feb50766ddfe2f75103e15c97 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:14:53 -0500 Subject: [PATCH 2/7] Model K (D3D12): exact FMA barriers, d1 at 191 VGPRs and output colour taps from the tiled input kernel (-40 us at 1440p Q, byte-identical) Three exact speedups on the native D3D12 device, none of which changes a value: - the output stage and the stage kernels keep each exact FMA chain behind a barrier so the driver does not regroup it; - k_stage2_d1 compiles at 191 VGPRs (8 workgroups per WGP); - k_color_fuse (default 1): the exact tiled input kernel also writes the output colour taps (kx/k_in_tiled_color[_uniform|_direct]), so the separate k_out_color launch is gone. k_color_fuse=0 brings it back. Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, HDR, auto-exposure, RGBA16F rows, 6 interleaved ABBA rounds, medians of 100 frames after warm-up): frame span 1540.0 -> 1498.7 us (-39.7 us, every round between -36.6 and -43.8 us); with k_color_fuse=0 1512.7 us (-25.6 us from the first two changes, -14.0 us from the colour taps). v0.6.5: 1721.7 us. Every output, intermediate and history buffer is byte-identical to the previous tip and to v0.6.5 (HDR value / texel / auto-exposure, LDR, rows and dense output, luma >= 1000 colours and out-of-range alpha, 1440p Quality and a small size, k_color_fuse 0 and 1, and the round-to-odd softmax form). k_smx_selfcheck.h: only kSourceDigest changes, the 133 reference hashes are the same. --- clean/dx12/kernel_sets.json | 12 ++++ clean/k/README.md | 3 +- clean/k/dx12/hlsl/k_block.hlsli | 14 ++++- clean/k/dx12/hlsl/k_common.hlsli | 32 ++++++++--- clean/k/dx12/hlsl/k_out.hlsli | 10 +++- clean/k/dx12/hlsl/k_wide.hlsli | 20 +++++-- clean/k/dx12/kernels/k_block.hlsl | 6 +- clean/k/dx12/kernels/k_block_split.hlsl | 2 +- clean/k/dx12/kernels/k_block_wide.hlsl | 2 +- clean/k/dx12/kernels/k_in.hlsl | 73 +++++++++++++++++++++++++ clean/k/runtime/k_smx_selfcheck.h | 2 +- clean/k/runtime/runtime_k.cpp | 20 ++++++- clean/k/runtime/runtime_k.h | 1 + 13 files changed, 172 insertions(+), 25 deletions(-) diff --git a/clean/dx12/kernel_sets.json b/clean/dx12/kernel_sets.json index b13ec118..575adda3 100644 --- a/clean/dx12/kernel_sets.json +++ b/clean/dx12/kernel_sets.json @@ -624,6 +624,18 @@ "kx/k_in_tiled__a1", "k_io_exact.co" ], + [ + "kx/k_in_tiled_color", + "k_io_exact.co" + ], + [ + "kx/k_in_tiled_color_direct", + "k_io_exact.co" + ], + [ + "kx/k_in_tiled_color_uniform", + "k_io_exact.co" + ], [ "kx/k_in_tiled_direct", "k_io_exact.co" diff --git a/clean/k/README.md b/clean/k/README.md index 57b33782..034d7c4d 100644 --- a/clean/k/README.md +++ b/clean/k/README.md @@ -210,7 +210,8 @@ Model K ships in the same `wronggpu_runtime.dll` as model M; nothing changes for first checks that `dx12/manifest.json` has a port of every kernel the context can launch and refuses it, naming the missing kernels, before the model is loaded. `k_in_uniforms=0` (diagnostic): the tiled input kernel that computes its frame values on the - GPU instead of taking the host's. `k_fill=<0..255>` (test hook, changes no value a frame writes): every buffer a frame writes and both history copies start with this byte after allocation, so that a test with a different byte per backend proves first-frame and reset-only writes. Latency options of the D3D12 devices that change no value: `k_color_at=` + GPU instead of taking the host's. `k_color_fuse=0` (native device; changes no value): the separate `k_out_color` launch instead of the + exact tiled input kernel's own output colour taps (`kx/k_in_tiled_color[_uniform|_direct]`, the default). `k_fill=<0..255>` (test hook, changes no value a frame writes): every buffer a frame writes and both history copies start with this byte after allocation, so that a test with a different byte per backend proves first-frame and reset-only writes. Latency options of the D3D12 devices that change no value: `k_color_at=` (`k_out_color` runs unordered right after that stage; default: before the input stage, HIP's order) and `k_warm=` / `off` (backend 3: `k_warm`, a cache warm-up of the weight blobs of the stages after it, launched unordered after that stage, default `e0`; `k_warm_to`, `k_warm_line`, `k_warm_groups`). The D3D12 executor of the interop (the game's own diff --git a/clean/k/dx12/hlsl/k_block.hlsli b/clean/k/dx12/hlsl/k_block.hlsli index ecf3e5b4..2633c543 100644 --- a/clean/k/dx12/hlsl/k_block.hlsli +++ b/clean/k/dx12/hlsl/k_block.hlsli @@ -611,14 +611,24 @@ void k_block(uint hidden, uint blk, uint bias32 = 0u, uint compact_head = 0u) [unroll] for (int g = 0; g < G; ++g) p[g] = wgk_mma(w1t[kt], a[g][kt], p[g]); uint4 hv[G]; [unroll] for (int g = 0; g < G; ++g) hv[g] = KB_ACT8W(wgk_round8(p[g])); - if (KB_W2LATE) // W2's tiles just before their multiplies (fewer live registers across the first GEMM) +#ifndef KB_W2SEQ +#define KB_W2SEQ 0 // with KB_W2LATE: one W2 tile at a time, loaded just before its own products (not all KT before the first) +#endif + if (KB_W2LATE && !KB_W2SEQ) // W2's tiles just before their multiplies (fewer live registers across the first GEMM) #if KB_BASE_FORM [unroll] for (int o4 = 0; o4 < KT; ++o4) w2t[o4] = kb_weight128(wb_w2, (uint)((ht * KT + o4) * 512)); #else [unroll] for (int o4 = 0; o4 < KT; ++o4) w2t[o4] = wgk_ld8(P_w2, (uint)((ht * KT + o4) * 256) + lane * 8u); #endif - [unroll] for (int ot = 0; ot < KT; ++ot) + [unroll] for (int ot = 0; ot < KT; ++ot) { + if (KB_W2LATE && KB_W2SEQ) +#if KB_BASE_FORM + w2t[ot] = kb_weight128(wb_w2, (uint)((ht * KT + ot) * 512)); +#else + w2t[ot] = wgk_ld8(P_w2, (uint)((ht * KT + ot) * 256) + lane * 8u); +#endif [unroll] for (int g = 0; g < G; ++g) acc[g][ot] = wgk_mma(w2t[ot], hv[g], acc[g][ot]); + } } // 5. x2, stored for the real positions (the stores' base pinned to every x2 fragment: products before the branch) diff --git a/clean/k/dx12/hlsl/k_common.hlsli b/clean/k/dx12/hlsl/k_common.hlsli index 9cffe3b6..e95bc6da 100644 --- a/clean/k/dx12/hlsl/k_common.hlsli +++ b/clean/k/dx12/hlsl/k_common.hlsli @@ -37,6 +37,15 @@ uint4 wgk_opq4(uint4 v) { return v ^ (wgk_zero() * 0x10001u); } half2 wgk_h2(uint u) { return asfloat16(uint16_t2((uint16_t)(u & 0xffffu), (uint16_t)(u >> 16))); } uint wgk_u(half2 h) { uint16_t2 v = asuint16(h); return (uint)v.x | ((uint)v.y << 16); } half2 wgk_c2(uint h) { return wgk_h2((h & 0xffffu) * 0x10001u); } // splatted FP16 constant (bits; no cast rounding) +// The exact FP16 product behind the opaque barrier in ONE instruction: fma(a, b, -0.0 ^ zero) per half. At run time the +// addend is -0.0 in both halves (the opaque zero is 0), and fma(a, b, -0) = RN(a b) for every input: the exact product +// is rounded once as the multiply rounds it, a zero product keeps its sign (+0 + -0 = +0, -0 + -0 = -0), NaN, infinities +// and denormals are the multiply's. The compiler cannot see the addend: it can neither turn the instruction back into a +// multiply nor fuse it into a following add or multiply (the barrier the xor of wgk_opq gave, without the xor). Never +// feed its result straight into an FP16 -> FP32 conversion: the driver folds an FP16 fma followed by the conversion into +// one v_fma_mix_f32 with no FP16 rounding in between (that is why wgk_sumsq8 keeps the multiply + xor). +half2 wgk_nz2() { return wgk_h2(0x80008000u ^ (wgk_zero() * 0x10001u)); } +half2 wgk_fm(half2 a, half2 b) { return mad(a, b, wgk_nz2()); } // FP16 element i (0..7) of a fragment, as bits / as FP32 (exact) uint wgk_el(uint4 v, uint i) { return (v[i >> 1] >> ((i & 1u) * 16u)) & 0xffffu; } float wgk_elf(uint4 v, uint i) { return f16tof32(wgk_el(v, i)); } @@ -96,6 +105,13 @@ uint4 wgk_sub8(uint4 a, uint4 b) return r; } uint4 wgk_mul8(uint4 a, uint4 b) +{ + uint4 r; + [unroll] for (uint i = 0; i < 4u; ++i) r[i] = wgk_u(wgk_fm(wgk_h2(a[i]), wgk_h2(b[i]))); + return r; +} +// the multiply behind the opaque xor, for a product converted to FP32 straight away (wgk_sumsq8: no fma form there) +uint4 wgk_mul8x(uint4 a, uint4 b) { uint4 r; [unroll] for (uint i = 0; i < 4u; ++i) r[i] = wgk_opq(wgk_u(wgk_h2(a[i]) * wgk_h2(b[i]))); @@ -174,11 +190,11 @@ uint wgk_act2(uint xu) const half2 x = wgk_h2(xu); const half2 t = min(max(x, wgk_c2(WGK_H_M2)), wgk_c2(WGK_H_2)); const half2 at = abs(t); - const half2 bt = wgk_h2(wgk_opq(wgk_u(wgk_c2(WGK_ACT_B) * at))); - const half2 p = wgk_h2(wgk_opq(wgk_u(wgk_c2(WGK_ACT_A) - bt))); - const half2 tp = wgk_h2(wgk_opq(wgk_u(t * p))); - const half2 q = wgk_h2(wgk_opq(wgk_u(wgk_c2(WGK_H_HALF) + tp))); - return wgk_opq(wgk_u(x * q)); + const half2 bt = wgk_fm(wgk_c2(WGK_ACT_B), at); // the two products that feed an addition, and the result: the + const half2 p = wgk_c2(WGK_ACT_A) - bt; // exact fma form; a - b|t| feeds a multiply and 0.5 + t p the last + const half2 tp = wgk_fm(t, p); // multiply: an addition cannot be fused into those (as wgk_act2n) + const half2 q = wgk_c2(WGK_H_HALF) + tp; + return wgk_u(wgk_fm(x, q)); } uint4 wgk_act8(uint4 x) { @@ -192,9 +208,9 @@ uint wgk_act2n(uint xu) const half2 x = wgk_h2(xu); const half2 t = min(max(x, wgk_c2(WGK_H_M2)), wgk_c2(WGK_H_2)); const half2 at = abs(t); - const half2 bt = wgk_h2(wgk_opq(wgk_u(wgk_c2(WGK_ACT_B) * at))); + const half2 bt = wgk_fm(wgk_c2(WGK_ACT_B), at); const half2 p = wgk_c2(WGK_ACT_A) - bt; - const half2 tp = wgk_h2(wgk_opq(wgk_u(t * p))); + const half2 tp = wgk_fm(t, p); const half2 q = wgk_c2(WGK_H_HALF) + tp; return wgk_u(x * q); } @@ -225,7 +241,7 @@ void wgk_softmax_weights(uint4 gg[4], out uint4 pa[4]) // FP16 squares, then v_fma_mix_f32 x2_half * 1 + ss = the FP32 addition of the exact FP16 value). float wgk_sumsq8(uint4 x, float ss) { - const uint4 q = wgk_mul8(x, x); + const uint4 q = wgk_mul8x(x, x); [unroll] for (uint j = 0; j < 8u; ++j) ss = ss + wgk_elf(q, j); return ss; } diff --git a/clean/k/dx12/hlsl/k_out.hlsli b/clean/k/dx12/hlsl/k_out.hlsli index b87edec3..721a14d4 100644 --- a/clean/k/dx12/hlsl/k_out.hlsli +++ b/clean/k/dx12/hlsl/k_out.hlsli @@ -76,8 +76,12 @@ half kp_lo(uint u) { return asfloat16((uint16_t)(u & 0xffffu)); } half kp_hi(uint u) { return asfloat16((uint16_t)(u >> 16)); } uint kp_add(uint a, uint b) { return kh2_u(kh2_h(a) + kh2_h(b)); } uint kp_sub(uint a, uint b) { return kh2_u(kh2_h(a) - kh2_h(b)); } -// product rounded to FP16 as its own instruction (the opaque on the packed word: no fusion into a following add) -uint kp_mul(uint a, uint b) { return kh2_u(kh2_h(a) * kh2_h(b)) ^ (ko_z * 0x10001u); } +// product rounded to FP16 as its own instruction, as fma(a, b, -0.0 ^ opaque zero): the addend is -0.0 in both halves at +// run time (the same bits as the multiply: one rounding of the exact product, a zero product keeps its sign, NaN / +// infinities / denormals as the multiply), unknown to the compiler: no fusion into a following add and no regrouping, +// without the xor of the packed word (k_common.hlsli wgk_fm). Never directly into an FP16 -> FP32 conversion (kp_opq). +half2 kp_nz2() { return kh2_h(0x80008000u ^ (ko_z * 0x10001u)); } +uint kp_mul(uint a, uint b) { return kh2_u(mad(kh2_h(a), kh2_h(b), kp_nz2())); } // product without the opaque, for products that feed only a multiply or a multiplicand (no add can absorb them; each use // checked in the ISA: the same v_mul_f16 / v_fma_f16 counts, no v_fma_mix*) uint kp_mul_n(uint a, uint b) { return kh2_u(kh2_h(a) * kh2_h(b)); } @@ -90,7 +94,7 @@ uint kp_opq(uint a) { return a ^ (ko_z * 0x10001u); } // the same with one operand a scalar FP16 value used in both halves (no splatted register) uint kp_rsub_s(half s, uint a) { return kh2_u((half2)s - kh2_h(a)); } uint kp_add_s(uint a, half s) { return kh2_u(kh2_h(a) + (half2)s); } -uint kp_mul_s(uint a, half s) { return kh2_u(kh2_h(a) * (half2)s) ^ (ko_z * 0x10001u); } +uint kp_mul_s(uint a, half s) { return kh2_u(mad(kh2_h(a), (half2)s, kp_nz2())); } // (fma form: see kp_mul) uint kp_mul_s_n(uint a, half s) { return kh2_u(kh2_h(a) * (half2)s); } // (no opaque: see kp_mul_n) uint kp_fma_s(uint a, half s, uint c) { return kh2_u(mad(kh2_h(a), (half2)s, kh2_h(c))); } uint kp_fma_ss(uint a, half s, half c) { return kh2_u(mad(kh2_h(a), (half2)s, (half2)c)); } diff --git a/clean/k/dx12/hlsl/k_wide.hlsli b/clean/k/dx12/hlsl/k_wide.hlsli index aa6cac1d..1279dd65 100644 --- a/clean/k/dx12/hlsl/k_wide.hlsli +++ b/clean/k/dx12/hlsl/k_wide.hlsli @@ -80,6 +80,11 @@ uint kw_opq(uint v) { return v ^ kw_zero(); } uint4 kw_opq4(uint4 v) { return v ^ kw_zero(); } half2 kw_h(uint v) { return wg_as_h2(v); } uint kw_u(half2 h) { return wg_h2_bits(h); } +// An FP16 product behind the opaque barrier in one instruction: fma(a, b, -0.0 ^ zero) per half (the addend is -0.0 at +// run time, and fma(a, b, -0) = RN(a b) on every input: one rounding of the exact product, a zero product keeps its +// sign, NaN / infinities / denormals as the multiply). The compiler cannot see the addend: no multiply to regroup, no +// add to fuse it into (k_common.hlsli wgk_fm; the barrier of kw_opq without its xor). +half2 kw_fm(half2 a, half2 b) { return mad(a, b, kw_h(0x80008000u ^ kw_zero())); } uint4 kw_add8(uint4 a, uint4 b) { @@ -87,6 +92,13 @@ uint4 kw_add8(uint4 a, uint4 b) [unroll] for (uint i = 0; i < 4; ++i) r[i] = kw_u(kw_h(a[i]) + kw_h(b[i])); return r; } +// the product behind the barrier (kw_opq4(kw_mul8(a, b))) for a result that feeds a multiply or an add: the fma form +uint4 kw_mulo8(uint4 a, uint4 b) +{ + uint4 r; + [unroll] for (uint i = 0; i < 4; ++i) r[i] = kw_u(kw_fm(kw_h(a[i]), kw_h(b[i]))); + return r; +} uint4 kw_mul8(uint4 a, uint4 b) { uint4 r; @@ -106,9 +118,9 @@ uint4 kw_act8(uint4 x) [unroll] for (uint i = 0; i < 4; ++i) { const half2 xv = kw_h(x[i]); const half2 tv = min(max(xv, lo), hi); - const half2 bt = kw_h(kw_opq(kw_u(B * abs(tv)))); + const half2 bt = kw_fm(B, abs(tv)); const half2 p = A - bt; - const half2 tp = kw_h(kw_opq(kw_u(tv * p))); + const half2 tp = kw_fm(tv, p); const half2 q = half_ + tp; r[i] = kw_u(xv * q); } @@ -123,8 +135,8 @@ uint4 kw_softpoly8(uint4 s) uint4 r; [unroll] for (uint i = 0; i < 4; ++i) { const half2 u = min(max(kw_h(s[i]), nc), c); - const half2 u2 = kw_h(kw_opq(kw_u(u * u))); - const half2 k2 = kw_h(kw_opq(kw_u(kap * u2))); + const half2 u2 = kw_fm(u, u); + const half2 k2 = kw_fm(kap, u2); const half2 o = one - k2; r[i] = kw_u(u * o); } diff --git a/clean/k/dx12/kernels/k_block.hlsl b/clean/k/dx12/kernels/k_block.hlsl index f70f0c5b..fd6ecbbd 100644 --- a/clean/k/dx12/kernels/k_block.hlsl +++ b/clean/k/dx12/kernels/k_block.hlsl @@ -25,7 +25,9 @@ // @wg-kernel k_stage2_e0 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage2_e1 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage_d2 co=k_block.co block=128 wave=32 args=s184:p19.b32 -// @wg-kernel k_stage2_d1 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 +// KB_X1LDS / KB_W2LATE / KB_W2SEQ (hlsl/k_block.hlsli): x1 waits in LDS during the MLP and each W2 tile is loaded just before its +// own products: 231 -> 191 VGPRs (RGA gfx1201, no spill), under the 192 of four workgroups per CU; remove the three defines to undo. +// @wg-kernel k_stage2_d1 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1,KB_X1LDS=1,KB_W2LATE=1,KB_W2SEQ=1 // @wg-kernel k_stage_e0 co=k_block.co block=128 wave=32 args=s184:p19.b32 defines=KB_LDS_HALFS=6144 // @wg-kernel k_stage_e1 co=k_block.co block=128 wave=32 args=s184:p19.b32 defines=KB_LDS_HALFS=4096 // @wg-kernel k_stage_d1 co=k_block.co block=128 wave=32 args=s184:p19.b32 @@ -35,7 +37,7 @@ // native D3D12 runtime when the mix-form probe (kernels/k_mix_probe.hlsl) fails on the adapter's driver. // @wg-kernel k_stage_e2__rodd co=k_block.co entry=k_stage_e2 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 // @wg-kernel k_stage_d2__rodd co=k_block.co entry=k_stage_d2 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 -// @wg-kernel k_stage2_d1__rodd co=k_block.co entry=k_stage2_d1 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1 +// @wg-kernel k_stage2_d1__rodd co=k_block.co entry=k_stage2_d1 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1,KB_X1LDS=1,KB_W2LATE=1,KB_W2SEQ=1 #define KB_LD_A16 1 // 16-byte aligned loads (hlsl/k_common.hlsli) #include "wg_platform.hlsli" #include "k_blkparams.hlsli" diff --git a/clean/k/dx12/kernels/k_block_split.hlsl b/clean/k/dx12/kernels/k_block_split.hlsl index ccef0bd1..7050d8b3 100644 --- a/clean/k/dx12/kernels/k_block_split.hlsl +++ b/clean/k/dx12/kernels/k_block_split.hlsl @@ -155,7 +155,7 @@ WG_KERNEL(ks_attn) const uint r16 = (s16 & 0x7fffu) == 0u ? 0u : wg_f2h(rsqrt(f16tof32(s16))); const uint4 rv = kw_splat8(r16); [unroll] for (int kt = 0; kt < KT; ++kt) - n[kt] = kw_mul8(n[kt], kw_opq4(kw_mul8(rv, g1v[kt]))); + n[kt] = kw_mul8(n[kt], kw_mulo8(rv, g1v[kt])); } // q, k, v^T of head hd (k_block_wide.hlsl step 2, one round's group) diff --git a/clean/k/dx12/kernels/k_block_wide.hlsl b/clean/k/dx12/kernels/k_block_wide.hlsl index 64793be0..f20b501b 100644 --- a/clean/k/dx12/kernels/k_block_wide.hlsl +++ b/clean/k/dx12/kernels/k_block_wide.hlsl @@ -201,7 +201,7 @@ WG_KERNEL(kw_stage) const uint r16 = (s16 & 0x7fffu) == 0u ? 0u : wg_f2h(rsqrt(f16tof32(s16))); const uint4 rv = kw_splat8(r16); [unroll] for (int kt = 0; kt < KT; ++kt) - n[kt] = kw_mul8(n[kt], kw_opq4(kw_mul8(rv, g1v[kt]))); + n[kt] = kw_mul8(n[kt], kw_mulo8(rv, g1v[kt])); } // 2. token mixing in rounds of NG heads; yacc (owned tiles) accumulates the output projection diff --git a/clean/k/dx12/kernels/k_in.hlsl b/clean/k/dx12/kernels/k_in.hlsl index 30e8392b..8c1b8249 100644 --- a/clean/k/dx12/kernels/k_in.hlsl +++ b/clean/k/dx12/kernels/k_in.hlsl @@ -31,6 +31,12 @@ // @wg-kernel kx/k_in_tiled_uniform co=k_io_exact.co block=256 wave=64 args=s248:p4.b128.p5.b48 defines=K_UNIFORM=1 // @wg-kernel kx/k_in_tiled co=k_io_exact.co block=256 wave=64 args=s200:p4.b128.p5 // @wg-kernel kx/k_in_tiled_direct co=k_io_exact.co entry=k_in_tiled block=256 wave=64 args=s200:p4.b128.p5 rs=direct defines=WG_DIRECT_TEXTURES=1 +// The _color forms (HIP k_in_tiled_color[_uniform]) also write the output colour taps (stored tap and filter-tap value T of +// spec 43 4.2 / 5.3: k_out_color's results) for the render pixels the workgroup owns, from the colour texels the tile +// stage loads anyway: the runtime then skips the separate k_out_color launch. K_COLOR; same bits as k_out_color (below). +// @wg-kernel kx/k_in_tiled_color_uniform co=k_io_exact.co entry=k_in_tiled_uniform block=256 wave=64 args=s264:p4.b128.p7.b48 defines=K_UNIFORM=1,K_COLOR=1 +// @wg-kernel kx/k_in_tiled_color co=k_io_exact.co entry=k_in_tiled block=256 wave=64 args=s216:p4.b128.p7 defines=K_COLOR=1 +// @wg-kernel kx/k_in_tiled_color_direct co=k_io_exact.co entry=k_in_tiled block=256 wave=64 args=s216:p4.b128.p7 rs=direct defines=WG_DIRECT_TEXTURES=1,K_COLOR=1 // The kxs tag is the pre-breakpoint exact S fallback. Its HIP forms are identical to those under kx. // @wg-kernel kxs/k_in_tiled_uniform co=k_io_exact.co block=256 wave=64 args=s248:p4.b128.p5.b48 defines=K_UNIFORM=1,KI_EXACT_S=1 // @wg-kernel kxs/k_in_tiled co=k_io_exact.co block=256 wave=64 args=s200:p4.b128.p5 defines=KI_EXACT_S=1 @@ -47,6 +53,9 @@ #ifndef WG_DIRECT_TEXTURES #define WG_DIRECT_TEXTURES 0 #endif +#ifndef K_COLOR +#define K_COLOR 0 +#endif #if WG_DIRECT_TEXTURES #include "native_direct.hlsli" #endif @@ -62,6 +71,9 @@ WG_ARGS_BEGIN K_ARGS_FRAME_IN K_ARGS_IN_PTRS +#if K_COLOR + uint P_ctap, P_ctapT; // KInColorParams: the output colour taps (stored tap [rh][rw] x 8 bytes, T [rh][rw] x 2 bytes) +#endif #if K_UNIFORM K_ARGS_HOST #endif @@ -101,6 +113,47 @@ KiTiledTap ki_tiled_tap_from_raw(KFrameIn F, float4 c) return r; } KiTiledTap ki_tiled_tap(KFrameIn F, int rx, int ry) { return ki_tiled_tap_from_raw(F, kio_color(F, rx, ry)); } +#if K_COLOR +// The colour texel of the tile stage with its alpha: the buffer forms' kio_color already holds it; the direct form's +// kio_texel returns alpha 0 (the input stage reads RGB only), the output colour taps need the texel's own alpha +// (k_out.hlsl: k_out_color_direct's Load, a missing alpha channel reads 1). Same texel as kio_color (rx, ry: render coordinates). +float4 ki_color_texel(KFrameIn F, int rx, int ry) +{ +#if WG_DIRECT_TEXTURES + return native_direct_color_tex.Load(int3((int)nd_rx + kio_tap(rx, 0, F.cbx, F.rw), (int)nd_ry + kio_tap(ry, 0, F.cby, F.rh), 0)); +#else + return kio_color(F, rx, ry); +#endif +} +// k_out.hlsli ko_color_tap (k_out_color) on a raw colour texel, operation for operation: the stored tap (Y, Co, Cg, A) as +// 2 x 2 FP16 bits and the filter-tap value T. The FP32 products by the uniform kappa and the encode factor e keep their +// opaque barriers (kf_opq: so the shared factors are not regrouped), every FP32 -> FP16 step is the hardware conversion +// (ki_f2h, nearest even; k_out.hlsli ko_f2h): outside every divergent branch, as the driver channel requires. This runs +// for every lane of the tile loop (a wave either runs the loop or not: uniform), the store alone is predicated. +void ki_color_tap(KFrameIn F, float4 c, out uint2 tap, out uint tT) +{ + const bool hdr = (F.flags & KIO_HDR) != 0; + float Y, Co, Cg; + kio_ycocg(max(c.x, 0.0f), max(c.y, 0.0f), max(c.z, 0.0f), Y, Co, Cg); + const float kY = kf_opq(F.kappa * Y), kCo = kf_opq(F.kappa * Co), kCg = kf_opq(F.kappa * Cg); + uint hY, hCo, hCg; + [branch] if (hdr) { + const float e = kio_encode(kY); + hY = ki_f2h(kY * e); + hCo = ki_f2h(kCo * e); + hCg = ki_f2h(kCg * e); + } else { + hY = ki_f2h(kY); + hCo = ki_f2h(kCo); + hCg = ki_f2h(kCg); + } + tap = uint2(hY | (hCo << 16), hCg | (ki_f2h(c.w) << 16)); + const float kL = kf_opq(F.kappa * kio_luma_fma(max(c.x, 0.0f), max(c.y, 0.0f), max(c.z, 0.0f))); + float vT = kL; + [branch] if (hdr) vT = kL * kio_encode(kL); + tT = ki_f2h(vT); +} +#endif KI_TILED_CHANNEL ki_tiled_ch0(KFrameIn F, int rx, int ry, uint table) { const float3 rgb = ki_rgb(F, kio_color(F, rx, ry)); @@ -213,10 +266,30 @@ WG_KERNEL(k_in_tiled) } const int tx0 = WaveReadLaneFirst(ki_max(ki_min((int)kf_R(F.jx + kf_fma((float)(4 * bx0 + 1), hk.a_x, -0.5f)) - 1, F.rw - 1), 0)); const int ty0 = WaveReadLaneFirst(ki_max(ki_min((int)kf_R(F.jy + kf_fma((float)(4 * by0 + 1), hk.a_y, -0.5f)) - 1, F.rh - 1), 0)); +#if K_COLOR + // HIP ki_tiled_body: the render pixels this workgroup owns are those below the next workgroup's tile origin + // (consecutive origins partition the render image); the raw producer coordinates are tested, so the replicated clamp + // loads never store twice + const int own_x1 = WaveReadLaneFirst(bx0 + 8 < F.Bw ? ki_max(ki_min((int)kf_R(F.jx + kf_fma((float)(4 * (bx0 + 8) + 1), hk.a_x, -0.5f)) - 1, F.rw - 1), 0) : F.rw); + const int own_y1 = WaveReadLaneFirst(by0 + 8 < F.Bh ? ki_max(ki_min((int)kf_R(F.jy + kf_fma((float)(4 * (by0 + 8) + 1), hk.a_y, -0.5f)) - 1, F.rh - 1), 0) : F.rh); +#endif [loop] for (int q = lane; q < 24 * 24 + (int)kf_opq_z_; q += 256) { const int raw_x = tx0 + q % 24, raw_y = ty0 + q / 24; const int rx = ki_min(raw_x, F.rw - 1), ry = ki_min(raw_y, F.rh - 1); +#if K_COLOR + const float4 craw = ki_color_texel(F, rx, ry); + const KiTiledTap value = ki_tiled_tap_from_raw(F, craw); + uint2 ctap; + uint ctT; + ki_color_tap(F, craw, ctap, ctT); + if (raw_x < own_x1 && raw_y < own_y1) { + const uint cid = (uint)(raw_y * F.rw + raw_x); + wg_st64(P_ctap, cid * 8u, ctap); + wg_st16(P_ctapT, cid * 2u, ctT); + } +#else const KiTiledTap value = ki_tiled_tap_from_raw(F, kio_color(F, rx, ry)); +#endif tile_cv[q] = value.cv; tile_om[q] = value.om; } diff --git a/clean/k/runtime/k_smx_selfcheck.h b/clean/k/runtime/k_smx_selfcheck.h index c6085970..d32ad3f2 100644 --- a/clean/k/runtime/k_smx_selfcheck.h +++ b/clean/k/runtime/k_smx_selfcheck.h @@ -8,7 +8,7 @@ namespace k_smx_selfcheck { -constexpr char kSourceDigest[] = "c7a083f24edaa314370dd3d27ce234398ceae9148942e6ee9d3bb9506d72d474"; +constexpr char kSourceDigest[] = "4ddd6d8fb0a86649d43b766e0c8c496a30e144659c0336e371256d9481bf4a93"; constexpr int kCount = 133; constexpr const char* kNames[kCount] = { "k_stage_e2@512x384/o4,4", diff --git a/clean/k/runtime/runtime_k.cpp b/clean/k/runtime/runtime_k.cpp index 92e8d103..c144a16e 100644 --- a/clean/k/runtime/runtime_k.cpp +++ b/clean/k/runtime/runtime_k.cpp @@ -504,6 +504,12 @@ bool KRuntime::create(const WgRtConfig& c, Device* dev, AllocFn alloc_fn, bool e const std::string kuni = option(c, "k_in_uniforms"); if (!kuni.empty() && kuni != "0" && kuni != "1") return fail("model K: k_in_uniforms must be 0 or 1"); input_uniforms_ = kuni != "0"; + // k_color_fuse (1, the default: on the native D3D12 device the exact tiled input kernel also writes the output colour + // taps, kx/k_in_tiled_color[_uniform|_direct], and the separate k_out_color launch is skipped; 0: the separate + // k_out_color as before. The same taps either way: a timing switch) + const std::string kcf = option(c, "k_color_fuse"); + if (!kcf.empty() && kcf != "0" && kcf != "1") return fail("model K: k_color_fuse must be 0 or 1"); + color_fuse_ = kcf != "0"; const std::string diag = option(c, "k_diag"); if (!diag.empty() && diag != "input2" && diag != "copyin" && diag != "input2+copyin" && diag != "copyin+input2") return fail("model K: unknown k_diag " + diag + " (input2, copyin, input2+copyin)"); @@ -1444,8 +1450,14 @@ bool KRuntime::frame(const WgRtFrame& fr, const FrameIo* io) { a1_legacy_noted_ = true; frame_note_ = "model K: k_approx=a1 applies to the tiled input stage only; this context's frames on the legacy input path (a render size above 0.68 x the output, a jitter outside [-0.5, 0.5], or input_tiled=0) run the standard input stage (this line once per context)"; } - // k_in_exact: the output colour taps by k_out_color (KFrame's exact plan), not by the tiled input kernel - const bool use_tiled_color = use_tiled && !in_exact_ && !reset && 4LL * Bw_ >= W_ && 4LL * Bh_ >= H_; + // The output colour taps by the tiled input kernel (HIP k_in_tiled_color[_uniform]) instead of a separate k_out_color: + // the fast input stage on every backend; the exact input stage only on the native D3D12 device (backend 3), whose + // kx/k_in_tiled_color[_uniform|_direct] compute the taps of k_out_color from the colour texels the tile stage loads + // anyway (same operations, same bits: clean/k/dx12/kernels/k_in.hlsl ki_color_tap). The exact input stage on HIP keeps + // k_out_color (KFrame's exact plan); the pre-breakpoint S fallback (kxs/), k_approx=a1 and k_approx=a2 (its k_out_color__a2 + // taps are the approximate ones) have no fused forms; k_color_fuse=0 turns it off. + const bool exact_color_fuse = color_fuse_ && in_exact_ && cfg_.backend == 3 && !approx_a1_ && !approx_a2_ && !s_breakpoints_fallback_; + const bool use_tiled_color = use_tiled && (!in_exact_ || exact_color_fuse) && !reset && 4LL * Bw_ >= W_ && 4LL * Bh_ >= H_; KiHost input_host = {}; const bool use_uniforms = use_tiled && !native_direct_ && input_uniforms_ && input_uniforms_safe(fin, &input_host); auto input_stage = [&]() { @@ -1618,6 +1630,10 @@ std::vector KRuntime::d3d12_kernels() const { const std::string suffix = approx_a1_ ? "__a1" : ""; if (!native_direct_ && input_uniforms_) v.push_back(tag + "k_in_tiled_uniform" + suffix); v.push_back(tag + "k_in_tiled" + d + suffix); // input_uniforms_safe refusing the host uniforms + if (cfg_.backend == 3 && color_fuse_ && !approx_a1_ && !approx_a2_ && !s_breakpoints_fallback_) { // the fused forms with the output colour taps (use_tiled_color) + if (!native_direct_ && input_uniforms_) v.push_back(tag + "k_in_tiled_color_uniform"); + v.push_back(tag + "k_in_tiled_color" + d); + } } if (approx_a2_) { // k_approx=a2 (opt-in) v.push_back("k_out_color" + d + "__a2"); diff --git a/clean/k/runtime/runtime_k.h b/clean/k/runtime/runtime_k.h index 311ccd2a..a78901b4 100644 --- a/clean/k/runtime/runtime_k.h +++ b/clean/k/runtime/runtime_k.h @@ -151,6 +151,7 @@ class KRuntime { uint64_t tiled_launches_ = 0, tiled_color_launches_ = 0, input_uniform_launches_ = 0; uint64_t legacy_color_launches_ = 0, legacy_base_launches_ = 0; bool input_uniforms_ = true; // runtime option k_in_uniforms (0: always k_in_tiled, a port-check diagnostic) + bool color_fuse_ = true; // runtime option k_color_fuse (0: native exact path keeps the separate k_out_color) bool diag_input2_ = false, diag_copyin_ = false; // runtime option k_diag (timing diagnostics) // d0 hands the head to k_out_pixel_c as the compact pixel head (k_stage2*_d0_hp; default). Runtime option // k_head=full: the 40-channel head map and k_out_pixel as before (same output bits; buffer "k.N" needs it) From 8e1833f68a13407a92fe9520ffe99479f0a32751 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:57:16 -0500 Subject: [PATCH 3/7] Model K (D3D12): one-fma exact products in the input stage and scalar-masked 128-bit loads in the stage kernels (-7.9 us at 1440p Q, byte-identical) Two exact speedups on the native D3D12 device, neither changes a value: - kf_pm(a, b) = fma(a, b, -0.0 ^ opaque zero), bit for bit RN(a b) (the exact product plus -0.0 rounds once; a zero product keeps its sign, NaN / infinities / FTZ as the multiply): one v_fma_f32 with an SGPR addend instead of the multiply and the xor of kf_opq(a * b). The input stage (k_in*) and its shared helpers use it; the addend is unknown to the compiler, so the product is still never fused into a following addition. KF_PM = 0 brings back kf_opq(a * b); - KB_LD_A16 / KW_LD_A16 = 2: the 16-byte alignment of the stage kernels' 128-bit loads is proven on the scalar base only ((base & ~15) + off, the same addresses) and the driver emits the loads without the per-lane v_and_b32 -16 (RGA gfx1201). 1 = the whole address, 0 = none. k_smx_selfcheck.h: only kSourceDigest changes, the 133 reference hashes are the same. Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, HDR, auto-exposure, RGBA16F rows, 6 interleaved ABBA rounds, medians of 100 frames after warm-up): frame span 1491.7 -> 1482.2 us (-7.9 us paired, every round between -3.8 and -20.7 us); input stage -5.6 us, network -3.0 us, output +0.8 us, exposure unchanged. Every stage kernel gets shorter in the per-dispatch profile (-2.7 to -7.1 us each, k_stage2_e1 139.2 -> 135.0 us). v0.6.5: 1714.9 us. Every output, intermediate and history buffer is byte-identical to the previous tip (9 scenarios x 5 frames x 18 buffers = 810, HDR value / texel / auto-exposure, LDR, rows and dense output, luma >= 1000 colours and out-of-range alpha, 1440p Quality and a small size), and so are the 270 buffers of the round-to-odd softmax form (k_smx_test=mix_fail). The create-time softmax self-check passes (smx=mix). CPU: the Model K tests and the kernel-set test pass. --- clean/k/dx12/hlsl/k_common.hlsli | 7 +++++++ clean/k/dx12/hlsl/k_fp.hlsli | 25 +++++++++++++++++++++---- clean/k/dx12/hlsl/k_frame.hlsli | 8 ++++---- clean/k/dx12/hlsl/k_in_common.hlsli | 6 +++--- clean/k/dx12/hlsl/k_phi_log2.hlsli | 6 +++--- clean/k/dx12/hlsl/k_wide.hlsli | 4 ++++ clean/k/dx12/kernels/k_block.hlsl | 4 +++- clean/k/dx12/kernels/k_block_split.hlsl | 4 +++- clean/k/dx12/kernels/k_block_wide.hlsl | 4 +++- clean/k/dx12/kernels/k_in.hlsl | 8 ++++---- clean/k/runtime/k_smx_selfcheck.h | 2 +- clean/k/tests/test_k_phi_log2.py | 2 +- 12 files changed, 57 insertions(+), 23 deletions(-) diff --git a/clean/k/dx12/hlsl/k_common.hlsli b/clean/k/dx12/hlsl/k_common.hlsli index e95bc6da..565dcc74 100644 --- a/clean/k/dx12/hlsl/k_common.hlsli +++ b/clean/k/dx12/hlsl/k_common.hlsli @@ -67,7 +67,14 @@ uint wgk_pin(uint v) { return WaveActiveBitOr(v & wgk_zero()); } #ifndef KB_LD_A16 #define KB_LD_A16 0 #endif +// KB_LD_A16 = 2: the low 4 bits are cleared on the scalar base only ((base & ~15) + off: the same addresses; no per-lane +// v_and_b32 -16 on the D3D12 driver, which emits the 128-bit loads without the lane mask, RGA gfx1201). 1: the whole address +// (RADV's ACO needs the proof), 0: none. +#if KB_LD_A16 == 2 +uint4 wgk_ld128_a16(uint base, uint off) { return wg_mem.Load4((base & ~15u) + off); } +#else uint4 wgk_ld128_a16(uint base, uint off) { return wg_mem.Load4((base + off) & (KB_LD_A16 ? ~15u : ~0u)); } +#endif uint4 wgk_ld8(uint base, uint off_h) { return wgk_ld128_a16(base, off_h * 2u); } void wgk_st8(uint base, uint off_h, uint4 v) { wg_st128(base, off_h * 2u, v); } uint4 wgk_zero8() { return uint4(0u, 0u, 0u, 0u); } diff --git a/clean/k/dx12/hlsl/k_fp.hlsli b/clean/k/dx12/hlsl/k_fp.hlsli index 6cbbcb97..a755b190 100644 --- a/clean/k/dx12/hlsl/k_fp.hlsli +++ b/clean/k/dx12/hlsl/k_fp.hlsli @@ -38,10 +38,27 @@ #include "k_fp_tables.hlsli" static uint kf_opq_z_; // 0, opaque to the compiler (KF_SETUP) -#define KF_SETUP() (kf_opq_z_ = wg_grid_ >> 31) +static float kf_nz_; // -0.0 ^ the opaque zero: -0.0 at run time, unknown to the compiler (KF_SETUP, one SGPR) +#define KF_SETUP() (kf_opq_z_ = wg_grid_ >> 31, kf_nz_ = asfloat(0x80000000u ^ (wg_grid_ >> 31))) float kf_opq(float x) { return asfloat(asuint(x) ^ kf_opq_z_); } float2 kf_opq2(float2 x) { return asfloat(asuint(x) ^ kf_opq_z_); } float4 kf_opq4(float4 x) { return asfloat(asuint(x) ^ kf_opq_z_); } +// A product rounded as its own instruction: kf_pm(a, b) = RN(a b) = fma(a, b, -0.0 ^ opaque zero) bit for bit (the exact +// product plus -0.0 rounds once: a zero product keeps its sign, NaN / infinities / FTZ as the multiply), the addend +// unknown to the compiler: no fusion into a following addition, no regrouping. One v_fma_f32 with the SGPR addend instead +// of the multiply and the xor of kf_opq(a * b) (k_common.hlsli wgk_fm is the FP16 twin). KF_PM = 0: kf_opq(a * b). +#ifndef KF_PM +#define KF_PM 1 +#endif +#if KF_PM +float kf_pm(float a, float b) { return mad(a, b, kf_nz_); } +float3 kf_pm3(float3 a, float3 b) { return mad(a, b, (float3)kf_nz_); } +float4 kf_pm4(float4 a, float4 b) { return mad(a, b, (float4)kf_nz_); } +#else +float kf_pm(float a, float b) { return kf_opq(a * b); } +float3 kf_pm3(float3 a, float3 b) { return asfloat(asuint(a * b) ^ kf_opq_z_); } +float4 kf_pm4(float4 a, float4 b) { return kf_opq4(a * b); } +#endif // ---- elementary steps ----------------------------------------------------------------------------------------------- @@ -282,7 +299,7 @@ kf2 kf2_fast_two_sum(float a, float b) // | } kf2 kf2_two_prod(float a, float b) { - const float p = kf_opq(a * b); + const float p = kf_pm(a, b); return kf2_make(p, kf_fma(a, b, -p)); } kf2 kf2_add(kf2 a, kf2 b) @@ -349,8 +366,8 @@ kf2 kf2_exp2(float y) const kf2 x = kf2_mul(kf2_make(r, 0.0f), kf2_make(KF2_LN2_H, KF2_LN2_L)); // |x| < 0.0055 // e^x = 1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120; HIP's order ((xh xh) xh) poly, each product rounded const float xh = x.h; - const float x2 = kf_opq(xh * xh), x3 = kf_opq(x2 * xh); - const float tail = kf_opq(x3 * kf_fma(xh, kf_fma(xh, KF_1_120, KF_1_24), KF_SIXTH)); // not fused into q.h + tail + const float x2 = kf_pm(xh, xh), x3 = kf_pm(x2, xh); + const float tail = kf_pm(x3, kf_fma(xh, kf_fma(xh, KF_1_120, KF_1_24), KF_SIXTH)); // not fused into q.h + tail kf2 q = kf2_two_prod(xh, xh); q.h = q.h * 0.5f; q.l = kf_fma(xh, x.l, q.l * 0.5f); diff --git a/clean/k/dx12/hlsl/k_frame.hlsli b/clean/k/dx12/hlsl/k_frame.hlsli index 0307fcbb..cd2b2dcc 100644 --- a/clean/k/dx12/hlsl/k_frame.hlsli +++ b/clean/k/dx12/hlsl/k_frame.hlsli @@ -236,7 +236,7 @@ void kio_nearest(KFrameIn F, int ix, int iy, out int odx, out int ody) const bool inv = (F.flags & KIO_INV) != 0; bool edge; if (inv) - edge = (c < 0.5f * (a + e) - kf_opq(k * c)) || (c < 0.5f * (b + d) - kf_opq(k * c)); + edge = (c < 0.5f * (a + e) - kf_pm(k, c)) || (c < 0.5f * (b + d) - kf_pm(k, c)); else edge = (c > kf_fma(c, k, 0.5f * (a + e))) || (c > kf_fma(c, k, 0.5f * (b + d))); int bx = 0, by = 0; @@ -263,7 +263,7 @@ void kio_nearest(KFrameIn F, int ix, int iy, out int odx, out int ody) float kio_decode(float t) { if (t < K_KIO_DECODE_NEAR) return wg_div(1.0f, 1.0f - t); - const float e = kf_opq((t + kf_bits(0x3C4FAE29u)) * 9.0f) * kf_bits(0x3FB8AA3Bu); + const float e = kf_pm(t + kf_bits(0x3C4FAE29u), 9.0f) * kf_bits(0x3FB8AA3Bu); return wg_div(kf_exp2(e) - 1.0f, 9.0f * t); } float kio_encode(float Y) @@ -281,7 +281,7 @@ float4 kio_texel_h4(uint img, uint idx) } float4 kio_bilinear4(uint img, int w, int h, float cx, float cy) { - const float px = kf_opq(cx * (float)w) - 0.5f, py = kf_opq(cy * (float)h) - 0.5f; + const float px = kf_pm(cx, (float)w) - 0.5f, py = kf_pm(cy, (float)h) - 0.5f; const float fx0 = floor(px), fy0 = floor(py); const float fx = px - fx0, fy = py - fy0; // HIP: finite ? (int)max(min(f, w), -1) : (+inf: w, -inf: -1, NaN: 0). The clamp already gives w / -1 for +-inf @@ -310,7 +310,7 @@ float4 kio_bilinear4(uint img, int w, int h, float cx, float cy) const float4 q11 = float4(wg_h2f_lo(q11u.x), wg_h2f_hi(q11u.x), wg_h2f_lo(q11u.y), wg_h2f_hi(q11u.y)); const float4 t0 = q00 + kf_opq4(q10 - q00) * fx; const float4 t1 = q01 + kf_opq4(q11 - q01) * fx; - return kf_opq4(t0 * omfy) + kf_opq4(t1 * fy); + return kf_pm4(t0, (float4)omfy) + kf_pm4(t1, (float4)fy); } // (Y, Co, Cg) of max(RGB, 0) scaled by kappa first (spec 42 6.2) or not (spec 43 4.2: kappa after) diff --git a/clean/k/dx12/hlsl/k_in_common.hlsli b/clean/k/dx12/hlsl/k_in_common.hlsli index 107ad866..e2e85060 100644 --- a/clean/k/dx12/hlsl/k_in_common.hlsli +++ b/clean/k/dx12/hlsl/k_in_common.hlsli @@ -97,7 +97,7 @@ float kf_div_prepared(float a, KfDivPrep p) float ki_decode(float t) { if (t < kf_bits(0x3F7FBE88u)) return ki_rcp(1.0f - t); - const float e = kf_opq((t + kf_bits(0x3C4FAE29u)) * 9.0f) * kf_bits(0x3FB8AA3Bu); + const float e = kf_pm(t + kf_bits(0x3C4FAE29u), 9.0f) * kf_bits(0x3FB8AA3Bu); return ki_div(ki_exp2(e) - 1.0f, 9.0f * t); } #if defined(KI_TILED_PHI_LOG2) @@ -192,7 +192,7 @@ KiFine ki_fine(KInParams P, KiHost k, int fx, int fy) const float4 s = kio_bilinear4(P.Hc, W16, H16, wx * k.h_x, wy * k.h_y); if ((F.flags & KIO_HDR) != 0) { const float f = ki_decode(s.x); - r.h = F.rho * kf_opq3(s.xyz * f); + r.h = F.rho * kf_pm3(s.xyz, (float3)f); } else { r.h = s.xyz; } @@ -202,7 +202,7 @@ KiFine ki_fine(KInParams P, KiHost k, int fx, int fy) } // kappa max(RGB, 0), each product rounded (kf_opq: no merging with the 0.25 / 0.5 factors that follow) -float3 ki_rgb(KFrameIn F, float4 c) { return kf_opq3(F.kappa * max(c.xyz, 0.0f)); } +float3 ki_rgb(KFrameIn F, float4 c) { return kf_pm3((float3)F.kappa, max(c.xyz, 0.0f)); } void ki_store_x0(uint x0, int id, int k, float c0, float c1, float c2, float c3) { wg_st64(x0, ((uint)id * 16u + 4u * (uint)k) * 2u, diff --git a/clean/k/dx12/hlsl/k_phi_log2.hlsli b/clean/k/dx12/hlsl/k_phi_log2.hlsli index a9beb82b..3101bcb3 100644 --- a/clean/k/dx12/hlsl/k_phi_log2.hlsli +++ b/clean/k/dx12/hlsl/k_phi_log2.hlsli @@ -12,18 +12,18 @@ float ki_phi_log2(float x) const float e = (float)((int)(bits >> 23) - 127 + (int)fold); const int j = (int)round((m - 1.0f) * 128.0f) + 32; const float inv = asfloat(KF_LOG_INV[j]); - const float p = kf_opq(m * inv); + const float p = kf_pm(m, inv); const float t = p - 1.0f; const float dt = kf_fma(m, inv, -p); // log2(1 + t + dt) through t^5, with the first two terms of its derivative for dt. - const float lead = kf_opq(t * KF2_INV_LN2_H); + const float lead = kf_pm(t, KF2_INV_LN2_H); const float lead_tail = kf_fma(t, KF2_INV_LN2_H, -lead); const float q = kf_fma(t, kf_fma(t, kf_fma(t, 0.2885390082f, -0.3606737602f), 0.4808983470f), -0.7213475204f); const float dt_scaled = kf_fma(dt, kf_fma(-t, KF2_INV_LN2_H, KF2_INV_LN2_H), kf_fma(t, KF2_INV_LN2_L, lead_tail)); - const float corr = kf_fma(kf_opq(t * t), q, dt_scaled + asfloat(KF_LOG_L[j])); + const float corr = kf_fma(kf_pm(t, t), q, dt_scaled + asfloat(KF_LOG_L[j])); const kf2 v = kf2_fast_two_sum(asfloat(KF_LOG_H[j]), lead); const kf2 s = kf2_fast_two_sum(e, v.h); const kf2 r = kf2_fast_two_sum(s.h, (v.l + corr) + s.l); diff --git a/clean/k/dx12/hlsl/k_wide.hlsli b/clean/k/dx12/hlsl/k_wide.hlsli index 1279dd65..d8e81ed6 100644 --- a/clean/k/dx12/hlsl/k_wide.hlsli +++ b/clean/k/dx12/hlsl/k_wide.hlsli @@ -172,7 +172,11 @@ uint4 kw_pin(uint4 v) #ifndef KW_LD_A16 #define KW_LD_A16 0 #endif +#if KW_LD_A16 == 2 // as KB_LD_A16 = 2 (k_common.hlsli): the scalar base only +uint4 kw_ld128(uint base, uint off) { return wg_mem.Load4((base & ~15u) + off); } +#else uint4 kw_ld128(uint base, uint off) { return wg_mem.Load4((base + off) & (KW_LD_A16 ? ~15u : ~0u)); } +#endif uint4 kw_ldg(uint p, uint e) { return kw_ld128(p, e * 2u); } void kw_stg(uint p, uint e, uint4 v) { wg_st128(p, e * 2u, v); } diff --git a/clean/k/dx12/kernels/k_block.hlsl b/clean/k/dx12/kernels/k_block.hlsl index fd6ecbbd..7ad79a14 100644 --- a/clean/k/dx12/kernels/k_block.hlsl +++ b/clean/k/dx12/kernels/k_block.hlsl @@ -38,7 +38,9 @@ // @wg-kernel k_stage_e2__rodd co=k_block.co entry=k_stage_e2 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 // @wg-kernel k_stage_d2__rodd co=k_block.co entry=k_stage_d2 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 // @wg-kernel k_stage2_d1__rodd co=k_block.co entry=k_stage2_d1 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1,KB_X1LDS=1,KB_W2LATE=1,KB_W2SEQ=1 -#define KB_LD_A16 1 // 16-byte aligned loads (hlsl/k_common.hlsli) +#ifndef KB_LD_A16 +#define KB_LD_A16 2 // 16-byte loads, alignment proven on the scalar base only (no per-lane v_and -16; 1 = whole address, 0 = none) (hlsl/k_common.hlsli) +#endif #include "wg_platform.hlsli" #include "k_blkparams.hlsli" diff --git a/clean/k/dx12/kernels/k_block_split.hlsl b/clean/k/dx12/kernels/k_block_split.hlsl index 7050d8b3..cb2a7595 100644 --- a/clean/k/dx12/kernels/k_block_split.hlsl +++ b/clean/k/dx12/kernels/k_block_split.hlsl @@ -28,7 +28,9 @@ // @wg-kernel k_split_m_wo co=k_block_wide.co block=128 wave=32 args=s184:p19.b32,p,p,p,p entry=ks_wo defines=KS_PHASE=2,KW_C=160,KW_HEADS=8,KW_IN=0,KW_CIN=160,KW_CNEXT=0,KW_NG=4 // @wg-kernel k_split_m_w1 co=k_block_wide.co block=128 wave=32 args=s184:p19.b32,p,p,p,p entry=ks_w1 defines=KS_PHASE=3,KW_C=160,KW_HEADS=8,KW_IN=0,KW_CIN=160,KW_CNEXT=0,KW_NG=4 // @wg-kernel k_split_m_w2 co=k_block_wide.co block=128 wave=32 args=s184:p19.b32,p,p,p,p entry=ks_w2 defines=KS_PHASE=4,KW_C=160,KW_HEADS=8,KW_IN=0,KW_CIN=160,KW_CNEXT=0,KW_NG=4 -#define KW_LD_A16 1 // 16-byte aligned loads (hlsl/k_wide.hlsli) +#ifndef KW_LD_A16 +#define KW_LD_A16 2 // 16-byte loads, alignment proven on the scalar base only (no per-lane v_and -16; 1 = whole address, 0 = none) (hlsl/k_wide.hlsli) +#endif #include "wg_platform.hlsli" WG_ARGS_BEGIN diff --git a/clean/k/dx12/kernels/k_block_wide.hlsl b/clean/k/dx12/kernels/k_block_wide.hlsl index f20b501b..acad45b8 100644 --- a/clean/k/dx12/kernels/k_block_wide.hlsl +++ b/clean/k/dx12/kernels/k_block_wide.hlsl @@ -20,7 +20,9 @@ // @wg-kernel k_stagew4_d4__rodd co=k_block_wide.co block=512 wave=32 args=s184:p19.b32 entry=kw_stage defines=K_SMX=1,KW_C=128,KW_HEADS=4,KW_IN=1,KW_CIN=160,KW_CNEXT=0,KW_NG=4,KW_RD=12 // @wg-kernel k_stagew2_d4__rodd co=k_block_wide.co block=256 wave=32 args=s184:p19.b32 entry=kw_stage defines=K_SMX=1,KW_C=128,KW_HEADS=4,KW_IN=1,KW_CIN=160,KW_CNEXT=0,KW_NG=2 // @wg-kernel k_stagew2_d3__rodd co=k_block_wide.co block=256 wave=32 args=s184:p19.b32 entry=kw_stage defines=K_SMX=1,KW_C=96,KW_HEADS=4,KW_IN=1,KW_CIN=128,KW_CNEXT=0,KW_NG=2,KW_RD=4 -#define KW_LD_A16 1 // 16-byte aligned loads (hlsl/k_wide.hlsli) +#ifndef KW_LD_A16 +#define KW_LD_A16 2 // 16-byte loads, alignment proven on the scalar base only (no per-lane v_and -16; 1 = whole address, 0 = none) (hlsl/k_wide.hlsli) +#endif #include "wg_platform.hlsli" WG_ARGS_BEGIN diff --git a/clean/k/dx12/kernels/k_in.hlsl b/clean/k/dx12/kernels/k_in.hlsl index 8c1b8249..43b63c26 100644 --- a/clean/k/dx12/kernels/k_in.hlsl +++ b/clean/k/dx12/kernels/k_in.hlsl @@ -135,7 +135,7 @@ void ki_color_tap(KFrameIn F, float4 c, out uint2 tap, out uint tT) const bool hdr = (F.flags & KIO_HDR) != 0; float Y, Co, Cg; kio_ycocg(max(c.x, 0.0f), max(c.y, 0.0f), max(c.z, 0.0f), Y, Co, Cg); - const float kY = kf_opq(F.kappa * Y), kCo = kf_opq(F.kappa * Co), kCg = kf_opq(F.kappa * Cg); + const float kY = kf_pm(F.kappa, Y), kCo = kf_pm(F.kappa, Co), kCg = kf_pm(F.kappa, Cg); uint hY, hCo, hCg; [branch] if (hdr) { const float e = kio_encode(kY); @@ -148,7 +148,7 @@ void ki_color_tap(KFrameIn F, float4 c, out uint2 tap, out uint tT) hCg = ki_f2h(kCg); } tap = uint2(hY | (hCo << 16), hCg | (ki_f2h(c.w) << 16)); - const float kL = kf_opq(F.kappa * kio_luma_fma(max(c.x, 0.0f), max(c.y, 0.0f), max(c.z, 0.0f))); + const float kL = kf_pm(F.kappa, kio_luma_fma(max(c.x, 0.0f), max(c.y, 0.0f), max(c.z, 0.0f))); float vT = kL; [branch] if (hdr) vT = kL * kio_encode(kL); tT = ki_f2h(vT); @@ -349,7 +349,7 @@ WG_KERNEL(k_in_tiled) mu = float3(kf_div_prepared(mu.x, den_p), kf_div_prepared(mu.y, den_p), kf_div_prepared(mu.z, den_p)); } const float pm = ki_phi(mu.x, hdr); - const float3 mp = kf_opq3(mu * pm); + const float3 mp = kf_pm3(mu, (float3)pm); const float3 eta = 0.5f * (hi - lo); const float3 nu = float3(kf_fma(hi.x - lo.x, 8.0f, 1.0f), kf_fma(hi.y - lo.y, 8.0f, 1.0f), kf_fma(hi.z - lo.z, 8.0f, 1.0f)); const bool nu_pos_fin = (((asuint(nu.x) >> 23) & 255u) - 1u < 254u) && @@ -364,7 +364,7 @@ WG_KERNEL(k_in_tiled) const KiFine fq = ki_fine(P, hk, 2 * u + (q & 1), 2 * v + (q >> 1)); valid = valid && fq.valid; const float ph = ki_phi(fq.h.x, hdr); - const float3 g = max(abs(mp - kf_opq3(fq.h * ph)) - eta, 0.0f); + const float3 g = max(abs(mp - kf_pm3(fq.h, (float3)ph)) - eta, 0.0f); float score = 0.0f; [branch] if (!(g.x == 0.0f && g.y == 0.0f && g.z == 0.0f && nu_pos_fin)) { const float dx = (g.x == 0.0f && nu_pos_fin) ? g.x : ki_div(g.x, nu.x); diff --git a/clean/k/runtime/k_smx_selfcheck.h b/clean/k/runtime/k_smx_selfcheck.h index d32ad3f2..990d0164 100644 --- a/clean/k/runtime/k_smx_selfcheck.h +++ b/clean/k/runtime/k_smx_selfcheck.h @@ -8,7 +8,7 @@ namespace k_smx_selfcheck { -constexpr char kSourceDigest[] = "4ddd6d8fb0a86649d43b766e0c8c496a30e144659c0336e371256d9481bf4a93"; +constexpr char kSourceDigest[] = "4625de35ec19cb6f14f23e40d8dbe825aa824646b94ce8f69f0f4229c2318bea"; constexpr int kCount = 133; constexpr const char* kNames[kCount] = { "k_stage_e2@512x384/o4,4", diff --git a/clean/k/tests/test_k_phi_log2.py b/clean/k/tests/test_k_phi_log2.py index c807c600..e2a59599 100644 --- a/clean/k/tests/test_k_phi_log2.py +++ b/clean/k/tests/test_k_phi_log2.py @@ -11,7 +11,7 @@ K_ROOT = Path(__file__).resolve().parents[1] -PROOF_INPUT_SHA256 = "73c7d70d22da9099f80b74c6c53ebbfb5655caae27cd2a3a40ee85344aeaa209" +PROOF_INPUT_SHA256 = "4006deb20e730e6084c6ba42cc0014633b40ccfc3f3974526ad0400696aec176" def proof_input_digest(source, shader): From 5c88a67a25640d69a8bf593b22b33b020019a003 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:55:34 -0500 Subject: [PATCH 4/7] Model K (D3D12): whole-address 16-byte load masks again in the stage kernels (RADV keeps its 128-bit loads, byte-identical) KB_LD_A16 / KW_LD_A16 = 2 (8e1833f) masked the scalar base only. DXC writes base + (lane << 4) + c as (base & ~15) + (lane << 4 | c) there, and RADV's ACO does not read that as 16-byte aligned: the stage kernels' loads fell back to dword loads (+6 .. +16 % instructions in k_stage2_d1 / e1 / e0 / d0_hp and k_stage_e2 / d2, +0.024 ms at 1440p Q). The kernels' default is 1 again (the whole address masked, as in 19bfc54); 2 stays selectable with --define. The exact FMA barriers (wgk_fm), d1 at 191 VGPRs and the one-fma input stage are unchanged. k_smx_selfcheck.h: only kSourceDigest changes, the reference hashes are the same. --- clean/k/dx12/hlsl/k_common.hlsli | 9 ++++++--- clean/k/dx12/hlsl/k_wide.hlsli | 2 +- clean/k/dx12/kernels/k_block.hlsl | 2 +- clean/k/dx12/kernels/k_block_split.hlsl | 2 +- clean/k/dx12/kernels/k_block_wide.hlsl | 2 +- clean/k/runtime/k_smx_selfcheck.h | 2 +- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/clean/k/dx12/hlsl/k_common.hlsli b/clean/k/dx12/hlsl/k_common.hlsli index 565dcc74..e4969dcd 100644 --- a/clean/k/dx12/hlsl/k_common.hlsli +++ b/clean/k/dx12/hlsl/k_common.hlsli @@ -67,9 +67,12 @@ uint wgk_pin(uint v) { return WaveActiveBitOr(v & wgk_zero()); } #ifndef KB_LD_A16 #define KB_LD_A16 0 #endif -// KB_LD_A16 = 2: the low 4 bits are cleared on the scalar base only ((base & ~15) + off: the same addresses; no per-lane -// v_and_b32 -16 on the D3D12 driver, which emits the 128-bit loads without the lane mask, RGA gfx1201). 1: the whole address -// (RADV's ACO needs the proof), 0: none. +// KB_LD_A16 = 1 (the kernels' default): the whole address is masked, (base + off) & ~15: RADV's ACO takes the 128-bit +// alignment from exactly this mask on each load address. 2: the low 4 bits are cleared on the scalar base only ((base & ~15) +// + off: the same addresses): RGA gfx1201 then shows no per-lane v_and_b32 -16 (-40 .. -100 of them per stage kernel), but +// DXC writes base + (lane << 4) + c as (base & ~15) + (lane << 4 | c), and ACO does not take that as 16-byte aligned +// (RADV, measured: +6 .. +16 % instructions in k_stage2_d1 / e1 / e0 / d0_hp and k_stage_e2 / d2, the loads split). +// ((base & ~15) + (off & ~15) is no remedy: DXC folds the second mask away, the DXIL is that of 2.) 0: none. #if KB_LD_A16 == 2 uint4 wgk_ld128_a16(uint base, uint off) { return wg_mem.Load4((base & ~15u) + off); } #else diff --git a/clean/k/dx12/hlsl/k_wide.hlsli b/clean/k/dx12/hlsl/k_wide.hlsli index d8e81ed6..5a659cdb 100644 --- a/clean/k/dx12/hlsl/k_wide.hlsli +++ b/clean/k/dx12/hlsl/k_wide.hlsli @@ -172,7 +172,7 @@ uint4 kw_pin(uint4 v) #ifndef KW_LD_A16 #define KW_LD_A16 0 #endif -#if KW_LD_A16 == 2 // as KB_LD_A16 = 2 (k_common.hlsli): the scalar base only +#if KW_LD_A16 == 2 // as KB_LD_A16 = 2 (k_common.hlsli, which says why it is not the default): the scalar base only uint4 kw_ld128(uint base, uint off) { return wg_mem.Load4((base & ~15u) + off); } #else uint4 kw_ld128(uint base, uint off) { return wg_mem.Load4((base + off) & (KW_LD_A16 ? ~15u : ~0u)); } diff --git a/clean/k/dx12/kernels/k_block.hlsl b/clean/k/dx12/kernels/k_block.hlsl index 7ad79a14..0427174c 100644 --- a/clean/k/dx12/kernels/k_block.hlsl +++ b/clean/k/dx12/kernels/k_block.hlsl @@ -39,7 +39,7 @@ // @wg-kernel k_stage_d2__rodd co=k_block.co entry=k_stage_d2 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 // @wg-kernel k_stage2_d1__rodd co=k_block.co entry=k_stage2_d1 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1,KB_X1LDS=1,KB_W2LATE=1,KB_W2SEQ=1 #ifndef KB_LD_A16 -#define KB_LD_A16 2 // 16-byte loads, alignment proven on the scalar base only (no per-lane v_and -16; 1 = whole address, 0 = none) (hlsl/k_common.hlsli) +#define KB_LD_A16 1 // 16-byte aligned loads: the whole address masked (hlsl/k_common.hlsli; 2 = scalar base only: loses RADV's b128 loads) #endif #include "wg_platform.hlsli" #include "k_blkparams.hlsli" diff --git a/clean/k/dx12/kernels/k_block_split.hlsl b/clean/k/dx12/kernels/k_block_split.hlsl index cb2a7595..39614d91 100644 --- a/clean/k/dx12/kernels/k_block_split.hlsl +++ b/clean/k/dx12/kernels/k_block_split.hlsl @@ -29,7 +29,7 @@ // @wg-kernel k_split_m_w1 co=k_block_wide.co block=128 wave=32 args=s184:p19.b32,p,p,p,p entry=ks_w1 defines=KS_PHASE=3,KW_C=160,KW_HEADS=8,KW_IN=0,KW_CIN=160,KW_CNEXT=0,KW_NG=4 // @wg-kernel k_split_m_w2 co=k_block_wide.co block=128 wave=32 args=s184:p19.b32,p,p,p,p entry=ks_w2 defines=KS_PHASE=4,KW_C=160,KW_HEADS=8,KW_IN=0,KW_CIN=160,KW_CNEXT=0,KW_NG=4 #ifndef KW_LD_A16 -#define KW_LD_A16 2 // 16-byte loads, alignment proven on the scalar base only (no per-lane v_and -16; 1 = whole address, 0 = none) (hlsl/k_wide.hlsli) +#define KW_LD_A16 1 // 16-byte aligned loads: the whole address masked (hlsl/k_wide.hlsli; 2 = scalar base only: loses RADV's b128 loads) #endif #include "wg_platform.hlsli" diff --git a/clean/k/dx12/kernels/k_block_wide.hlsl b/clean/k/dx12/kernels/k_block_wide.hlsl index acad45b8..c7938f4d 100644 --- a/clean/k/dx12/kernels/k_block_wide.hlsl +++ b/clean/k/dx12/kernels/k_block_wide.hlsl @@ -21,7 +21,7 @@ // @wg-kernel k_stagew2_d4__rodd co=k_block_wide.co block=256 wave=32 args=s184:p19.b32 entry=kw_stage defines=K_SMX=1,KW_C=128,KW_HEADS=4,KW_IN=1,KW_CIN=160,KW_CNEXT=0,KW_NG=2 // @wg-kernel k_stagew2_d3__rodd co=k_block_wide.co block=256 wave=32 args=s184:p19.b32 entry=kw_stage defines=K_SMX=1,KW_C=96,KW_HEADS=4,KW_IN=1,KW_CIN=128,KW_CNEXT=0,KW_NG=2,KW_RD=4 #ifndef KW_LD_A16 -#define KW_LD_A16 2 // 16-byte loads, alignment proven on the scalar base only (no per-lane v_and -16; 1 = whole address, 0 = none) (hlsl/k_wide.hlsli) +#define KW_LD_A16 1 // 16-byte aligned loads: the whole address masked (hlsl/k_wide.hlsli; 2 = scalar base only: loses RADV's b128 loads) #endif #include "wg_platform.hlsli" diff --git a/clean/k/runtime/k_smx_selfcheck.h b/clean/k/runtime/k_smx_selfcheck.h index 990d0164..7f934756 100644 --- a/clean/k/runtime/k_smx_selfcheck.h +++ b/clean/k/runtime/k_smx_selfcheck.h @@ -8,7 +8,7 @@ namespace k_smx_selfcheck { -constexpr char kSourceDigest[] = "4625de35ec19cb6f14f23e40d8dbe825aa824646b94ce8f69f0f4229c2318bea"; +constexpr char kSourceDigest[] = "d8059bfdef15c13e683a7e90a9eb823884e06b3f3b55acce7b897affad68ae01"; constexpr int kCount = 133; constexpr const char* kNames[kCount] = { "k_stage_e2@512x384/o4,4", From b7d033b8cf3fd67efcb7d1964cfb051a1aab5e08 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:26:46 -0500 Subject: [PATCH 5/7] Model K (D3D12): row loads for the 5 x 5 luma window and the row-major filter taps, colour taps skipped by halo-only waves (byte-identical by construction, RGA) k_out_pixel_c (all forms): the interior 5 x 5 p_L window as one 12-byte load per row (buffer_load_b96) instead of 25 16-bit loads, and, when a whole wave has row-major taps on the fast path, each line's three ctapT values from one 8-byte load (buffer_load_b64) instead of three 16-bit loads. The same bytes go into the same values; no arithmetic changes. RGA gfx1201: VMEM loads in the listing 110 -> 94, hot path 37 -> 9 for these taps, 80 VGPRs, no spills. k_in_tiled_color (all forms): a wave whose lanes own no render pixel in a pass of the tile loop skips ki_color_tap (the test is wave-uniform, so the conversions stay outside divergent branches); stored taps unchanged. GPU byte-identity and timing check still to run. --- clean/k/dx12/kernels/k_in.hlsl | 24 +++++++++++++-------- clean/k/dx12/kernels/k_out.hlsl | 37 ++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/clean/k/dx12/kernels/k_in.hlsl b/clean/k/dx12/kernels/k_in.hlsl index 43b63c26..6fbe57b0 100644 --- a/clean/k/dx12/kernels/k_in.hlsl +++ b/clean/k/dx12/kernels/k_in.hlsl @@ -128,8 +128,9 @@ float4 ki_color_texel(KFrameIn F, int rx, int ry) // k_out.hlsli ko_color_tap (k_out_color) on a raw colour texel, operation for operation: the stored tap (Y, Co, Cg, A) as // 2 x 2 FP16 bits and the filter-tap value T. The FP32 products by the uniform kappa and the encode factor e keep their // opaque barriers (kf_opq: so the shared factors are not regrouped), every FP32 -> FP16 step is the hardware conversion -// (ki_f2h, nearest even; k_out.hlsli ko_f2h): outside every divergent branch, as the driver channel requires. This runs -// for every lane of the tile loop (a wave either runs the loop or not: uniform), the store alone is predicated. +// (ki_f2h, nearest even; k_out.hlsli ko_f2h): outside every divergent branch, as the driver channel requires. A wave +// runs it on all its lanes or not at all (the tile loop and the wave's ownership test are uniform), the store alone is +// predicated. void ki_color_tap(KFrameIn F, float4 c, out uint2 tap, out uint tT) { const bool hdr = (F.flags & KIO_HDR) != 0; @@ -279,13 +280,18 @@ WG_KERNEL(k_in_tiled) #if K_COLOR const float4 craw = ki_color_texel(F, rx, ry); const KiTiledTap value = ki_tiled_tap_from_raw(F, craw); - uint2 ctap; - uint ctT; - ki_color_tap(F, craw, ctap, ctT); - if (raw_x < own_x1 && raw_y < own_y1) { - const uint cid = (uint)(raw_y * F.rw + raw_x); - wg_st64(P_ctap, cid * 8u, ctap); - wg_st16(P_ctapT, cid * 2u, ctT); + // the stored taps only in waves that own a pixel of this pass (a wave of halo pixels alone computed them and + // dropped them all); the test is wave-uniform, so ki_color_tap's conversions stay outside divergent branches + const bool own = raw_x < own_x1 && raw_y < own_y1; + [branch] if (WaveActiveAnyTrue(own)) { + uint2 ctap; + uint ctT; + ki_color_tap(F, craw, ctap, ctT); + if (own) { + const uint cid = (uint)(raw_y * F.rw + raw_x); + wg_st64(P_ctap, cid * 8u, ctap); + wg_st16(P_ctapT, cid * 2u, ctT); + } } #else const KiTiledTap value = ki_tiled_tap_from_raw(F, kio_color(F, rx, ry)); diff --git a/clean/k/dx12/kernels/k_out.hlsl b/clean/k/dx12/kernels/k_out.hlsl index 1277060e..73053d12 100644 --- a/clean/k/dx12/kernels/k_out.hlsl +++ b/clean/k/dx12/kernels/k_out.hlsl @@ -281,7 +281,7 @@ WG_KERNEL(k_out_pixel_c) // F2: Ybar of the aligned 2 x 2 group through LDS (lanes lane, lane ^ 1, lane ^ 64, lane ^ 65) lds_ybar[lane] = cbar_lo & 0xffffu; - // 3.6 luma history: the 5 x 5 window of the four sub-samples (25 two-byte loads on interior fast path, 10 unique + // 3.6 luma history: the 5 x 5 window of the four sub-samples (five 12-byte row loads on interior fast path, 10 unique // row cubics in ko_ellv; Lmax16 < 0x3becu is exact-equivalent to Lmax32 < 0x3F7D8000u on all 65,536 FP16 patterns; // computed before Hh so Hh[4] does not stay live across Lw[5][5]) const KoAxis axx = ko_axis(wx), axy = ko_axis(wy); @@ -289,13 +289,21 @@ WG_KERNEL(k_out_pixel_c) float Lw[5][5]; uint Lmax16 = 0u; [branch] if ((uint)(axx.n - 1) <= (uint)(LW - 5) && (uint)(axy.n - 1) <= (uint)(LH - 5)) { - const uint voff = ((uint)(axy.n - 1) * (uint)LW + (uint)(axx.n - 1)) * 2u; - [unroll] for (int r = 0; r < 5; ++r) + // each row's five FP16 values from the three aligned dwords that hold them (one Load3 per row instead of five + // 16-bit loads; the same bytes: a word-aligned start takes halves 0..4 of the dwords, an odd-half start 1..5) + const uint a0 = p_L + ((uint)(axy.n - 1) * (uint)LW + (uint)(axx.n - 1)) * 2u; + const bool oddh = (a0 & 2u) != 0u; + [unroll] for (int r = 0; r < 5; ++r) { + const uint3 d = wg_mem.Load3((a0 + (uint)(r * LW) * 2u) & ~3u); + const uint e0 = oddh ? (d.x >> 16) | (d.y << 16) : d.x; + const uint e1 = oddh ? (d.y >> 16) | (d.z << 16) : d.y; + const uint e2 = oddh ? d.z >> 16 : d.z; + const uint hv[5] = { e0 & 0xffffu, e0 >> 16, e1 & 0xffffu, e1 >> 16, e2 & 0xffffu }; [unroll] for (int c = 0; c < 5; ++c) { - const uint h = wg_ld16(p_L + (uint)(r * LW) * 2u + 2u * (uint)c, voff); - Lmax16 = max(Lmax16, h); - Lw[r][c] = f16tof32(h); + Lmax16 = max(Lmax16, hv[c]); + Lw[r][c] = f16tof32(hv[c]); } + } } else { [unroll] for (int r = 0; r < 5; ++r) { const uint rb = (uint)ko_clampi(axy.n - 1 + r, LH) * (uint)LW; @@ -435,7 +443,22 @@ WG_KERNEL(k_out_pixel_c) } } } - [unroll] for (int n2 = 0; n2 < 12; ++n2) T[n2] = kh_bits(wg_ld16(p_ctapT, toff[n2])); + // row-major taps (O == 0: three consecutive FP16 values per line) of a whole wave: one Load2 of the two aligned + // dwords that hold each line's three values instead of three 16-bit loads (the same bytes); wave-uniform test, so + // no wave runs both forms + [branch] if (WaveActiveAllTrue(tfast && !O)) { + [unroll] for (int l3 = 0; l3 < 4; ++l3) { + const uint a = p_ctapT + toff[3 * l3]; + const uint2 d = wg_mem.Load2(a & ~3u); + const uint e0 = (a & 2u) != 0u ? (d.x >> 16) | (d.y << 16) : d.x; + const uint e1 = (a & 2u) != 0u ? d.y >> 16 : d.y; + T[3 * l3] = kh_bits(e0 & 0xffffu); + T[3 * l3 + 1] = kh_bits(e0 >> 16); + T[3 * l3 + 2] = kh_bits(e1 & 0xffffu); + } + } else { + [unroll] for (int n2 = 0; n2 < 12; ++n2) T[n2] = kh_bits(wg_ld16(p_ctapT, toff[n2])); + } // 5.4 - 5.6 filtered values, 6.1 blend weights (sub-samples (i, 0), (i, 1) as the halves of half2 values) const half kux = kh_bits(u_kuxkvy & 0xffffu), kvy = kh_bits(u_kuxkvy >> 16); From 3a35b73d87abff3bb470a445d9ebf099e53f8be7 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:45:07 -0500 Subject: [PATCH 6/7] Model K (D3D12): d1 one window per workgroup (k_stage_d1; -19.4 us at 1440p Q vs 5c88a67, byte-identical) At 1440p Quality d1 has 1,025 windows: as 513 two-window workgroups (k_stage2_d1) they fill the 256 slots of four workgroups per CU for two rounds and one more (a single workgroup). k_stage_d1, the one-window form HIP's k_block.co already has (K_STAGES), runs the same per-window maths at 1,025 workgroups: 100-105 us instead of 134 us (WG_D3D12_PROFILE). Measured, the e0 / e1 one-window forms are slower (139 vs 120 us, 81 vs 78 us), so they stay two-window. Applies to every backend (stage_kernel_base); d0 is unchanged. - k_stage_d1__rodd: the round-to-odd form of the self-check / mix-probe fallback; isa_checks.json covers both forms (64 / 0 mix folds), kernel_sets.json updated. - softmax self-check: the d1 case is k_stage_d1 (smx_selfcheck_digest.py BASE); k_smx_selfcheck.h regenerated on HIP (gen_k_smx_selfcheck.py): the d1 hash is unchanged, only its name. - 0/6 diffs, 0 non-finite against 5c88a67: 1440p Q HDR pi 1.0 / 1.25 / 2.0, LDR pi 1.0, 645x363 -> 967x545 HDR. - 6-round ABBA against 5c88a67 (RX 9070 XT): pi 1.0 1.4623 -> 1.4430 ms, pi 1.25 1.4700 -> 1.4505, pi 2.0 1.4795 -> 1.4609. --- clean/dx12/isa_checks.json | 4 +++- clean/dx12/kernel_sets.json | 4 ++++ clean/k/dx12/kernels/k_block.hlsl | 1 + clean/k/dx12/smx_selfcheck_digest.py | 2 +- clean/k/runtime/k_smx_selfcheck.h | 4 ++-- clean/k/runtime/runtime_k.cpp | 10 ++++++---- clean/k/tests/test_k_runtime_d3d12.py | 4 ++-- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/clean/dx12/isa_checks.json b/clean/dx12/isa_checks.json index 44840ee6..5491fbb0 100644 --- a/clean/dx12/isa_checks.json +++ b/clean/dx12/isa_checks.json @@ -1,8 +1,9 @@ { - "_doc": "build_dx12.py ISA check: for each kernel listed, the RGA (gfx1201) ISA must contain exactly the given number of instructions whose mnemonic matches each pattern (a regular expression, matched at the start of the mnemonic). Model K's softmax weights (clean/k/dx12/hlsl/k_smx.hlsli) are exact only when the driver folds the mix form p + e into one v_fma_mixlo/hi_f16 rounding per weight: 32 weights per lane per softmax round, i.e. 32 x heads x windows per workgroup for the narrow stages (e2 / d2: 2 heads, 1 window; d1: 2 heads, 2 windows) and 32 x heads / NG for the wide stages (e3 / d3 and the NG = 2 e4 / d4: 2 rounds; NG = 4 e4 / d4: 1; m: 8 heads / NG 4 = 2; the phase-split m attention k_split_m_attn: one head per workgroup = 1). A missing fold leaves v_cvt_f16_f32 after an FP32 add (a second rounding) and fails here. The __rodd ports (round to odd) must have none. The native D3D12 runtime checks the same fold on the player's driver at create (k_mix_probe). WG_DX12_ISA_CHECK=0 skips the check.", + "_doc": "build_dx12.py ISA check: for each kernel listed, the RGA (gfx1201) ISA must contain exactly the given number of instructions whose mnemonic matches each pattern (a regular expression, matched at the start of the mnemonic). Model K's softmax weights (clean/k/dx12/hlsl/k_smx.hlsli) are exact only when the driver folds the mix form p + e into one v_fma_mixlo/hi_f16 rounding per weight: 32 weights per lane per softmax round, i.e. 32 x heads x windows per workgroup for the narrow stages (e2 / d2 / d1: 2 heads, 1 window; the two-window d1: 2 heads, 2 windows) and 32 x heads / NG for the wide stages (e3 / d3 and the NG = 2 e4 / d4: 2 rounds; NG = 4 e4 / d4: 1; m: 8 heads / NG 4 = 2; the phase-split m attention k_split_m_attn: one head per workgroup = 1). A missing fold leaves v_cvt_f16_f32 after an FP32 add (a second rounding) and fails here. The __rodd ports (round to odd) must have none. The native D3D12 runtime checks the same fold on the player's driver at create (k_mix_probe). WG_DX12_ISA_CHECK=0 skips the check.", "k_stage_e2": {"v_fma_mix(lo|hi)_f16": 64}, "k_stage_d2": {"v_fma_mix(lo|hi)_f16": 64}, "k_stage2_d1": {"v_fma_mix(lo|hi)_f16": 128}, + "k_stage_d1": {"v_fma_mix(lo|hi)_f16": 64}, "k_stagew2_e3": {"v_fma_mix(lo|hi)_f16": 64}, "k_stagew4_e4": {"v_fma_mix(lo|hi)_f16": 32}, "k_stagew2_e4": {"v_fma_mix(lo|hi)_f16": 64}, @@ -18,6 +19,7 @@ "k_stage_e2__rodd": {"v_fma_mix(lo|hi)_f16": 0}, "k_stage_d2__rodd": {"v_fma_mix(lo|hi)_f16": 0}, "k_stage2_d1__rodd": {"v_fma_mix(lo|hi)_f16": 0}, + "k_stage_d1__rodd": {"v_fma_mix(lo|hi)_f16": 0}, "k_stagew2_e3__rodd": {"v_fma_mix(lo|hi)_f16": 0}, "k_stagew4_e4__rodd": {"v_fma_mix(lo|hi)_f16": 0}, "k_stagew2_e4__rodd": {"v_fma_mix(lo|hi)_f16": 0}, diff --git a/clean/dx12/kernel_sets.json b/clean/dx12/kernel_sets.json index 575adda3..c1d729c0 100644 --- a/clean/dx12/kernel_sets.json +++ b/clean/dx12/kernel_sets.json @@ -512,6 +512,10 @@ "k_stage_d1", "k_block.co" ], + [ + "k_stage_d1__rodd", + "k_block.co" + ], [ "k_stage_d2", "k_block.co" diff --git a/clean/k/dx12/kernels/k_block.hlsl b/clean/k/dx12/kernels/k_block.hlsl index 0427174c..56edbe70 100644 --- a/clean/k/dx12/kernels/k_block.hlsl +++ b/clean/k/dx12/kernels/k_block.hlsl @@ -31,6 +31,7 @@ // @wg-kernel k_stage_e0 co=k_block.co block=128 wave=32 args=s184:p19.b32 defines=KB_LDS_HALFS=6144 // @wg-kernel k_stage_e1 co=k_block.co block=128 wave=32 args=s184:p19.b32 defines=KB_LDS_HALFS=4096 // @wg-kernel k_stage_d1 co=k_block.co block=128 wave=32 args=s184:p19.b32 +// @wg-kernel k_stage_d1__rodd co=k_block.co entry=k_stage_d1 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 // @wg-kernel k_stage2_d0_hp co=k_block.co block=128 wave=32 args=s184:p19.b32,p,p,p defines=KB_HIDDEN_ARG=1,KB_COMPACT_HEAD_ARG=1,KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage2_d0_hidden co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_HIDDEN_ARG=1 // The __rodd forms: the same stages with the round-to-odd softmax weights (hlsl/k_smx.hlsli K_SMX 1), launched by the diff --git a/clean/k/dx12/smx_selfcheck_digest.py b/clean/k/dx12/smx_selfcheck_digest.py index c4542eb3..b5090333 100644 --- a/clean/k/dx12/smx_selfcheck_digest.py +++ b/clean/k/dx12/smx_selfcheck_digest.py @@ -27,7 +27,7 @@ GENERATED_BY = os.path.join("clean", "k", "dx12", "gen_k_fp_tables.py") RUNTIME_FILES = [os.path.join("clean", "k", "runtime", "runtime_k.cpp"), os.path.join("clean", "k", "runtime", "model_k.cpp")] # the softmax stage kernels the self-check runs (runtime_k.cpp kSmxCases), both forms -BASE = ["k_stage_e2", "k_stagew2_e3", "k_stagew2_d3", "k_stage_d2", "k_stage2_d1", "k_stagew4_e4", "k_stagew4_d4", +BASE = ["k_stage_e2", "k_stagew2_e3", "k_stagew2_d3", "k_stage_d2", "k_stage_d1", "k_stagew4_e4", "k_stagew4_d4", "k_stagew2_e4", "k_stagew2_d4", "k_stagew4_m"] # the phase kernels the native D3D12 device runs in place of k_stagew4_m[__rodd] (runtime option k_split, default on # backend 3; KRuntime::launch redirects stage m, so the self-check's m case runs them: kernels/k_block_split.hlsl) diff --git a/clean/k/runtime/k_smx_selfcheck.h b/clean/k/runtime/k_smx_selfcheck.h index 7f934756..ce9b055a 100644 --- a/clean/k/runtime/k_smx_selfcheck.h +++ b/clean/k/runtime/k_smx_selfcheck.h @@ -8,14 +8,14 @@ namespace k_smx_selfcheck { -constexpr char kSourceDigest[] = "d8059bfdef15c13e683a7e90a9eb823884e06b3f3b55acce7b897affad68ae01"; +constexpr char kSourceDigest[] = "944f9b6e380c6343f501a29d58a1780b460ac8ed3ff400b73781baa24a910030"; constexpr int kCount = 133; constexpr const char* kNames[kCount] = { "k_stage_e2@512x384/o4,4", "k_stagew2_e3@512x384/o4,4", "k_stagew2_d3@512x384/o4,4", "k_stage_d2@512x384/o4,4", - "k_stage2_d1@512x384/o4,4", + "k_stage_d1@512x384/o4,4", "k_stagew4_e4@512x384*8/o0,0", "k_stagew4_e4@512x384*8/o0,2", "k_stagew4_e4@512x384*8/o0,4", diff --git a/clean/k/runtime/runtime_k.cpp b/clean/k/runtime/runtime_k.cpp index c144a16e..1c8ebd12 100644 --- a/clean/k/runtime/runtime_k.cpp +++ b/clean/k/runtime/runtime_k.cpp @@ -365,10 +365,12 @@ KBlkParams block_params(const KStageInfo& st, DevPtr blob, DevPtr x, DevPtr y, D // k_stage_; *ng = NG (0: not wide), *w2 = two windows per workgroup. std::string stage_kernel_base(const KStageInfo& st, bool f16, bool has_y, int nwin, int* ng, bool* w2) { *ng = wide_ng(st.name, f16, nwin); - // e0, e1, d1, d0: two windows per workgroup (k_stage2_*, k_launch.py WINDOWS2_STAGES; k_net.py WINDOWS2_VARIANTS; - // d1's needs its block output map p.y, which the network always gives it) + // e0, e1, d0: two windows per workgroup (k_stage2_*, k_launch.py WINDOWS2_STAGES; k_net.py WINDOWS2_VARIANTS). + // d1 runs one window per workgroup (k_stage_d1): at 1440p Quality its 1,025 windows as 513 two-window workgroups + // fill the 256 slots of four workgroups per CU for two rounds and one more, measured on D3D12 (RX 9070 XT) -14.6 us + // per frame with one window per workgroup, the same bytes (the same per-window maths) *w2 = !*ng && (std::strcmp(st.name, "e0") == 0 || std::strcmp(st.name, "e1") == 0 || - (std::strcmp(st.name, "d1") == 0 && has_y) || std::strcmp(st.name, "d0") == 0); + std::strcmp(st.name, "d0") == 0); const std::string h = f16 ? "h" : ""; return (*ng ? "k_stagew" + std::to_string(*ng) : std::string(*w2 ? "k_stage2" : "k_stage")) + h + "_" + st.name; } @@ -846,7 +848,7 @@ struct SmxCase { int wb, hb; // base map size int sets; // independent synthetic data sets, the outputs of all hashed together }; -// e2, e3, d3, d2, d1 (k_stage_e2, k_stagew2_e3, k_stagew2_d3, k_stage_d2, k_stage2_d1) and e4 / d4 with NG = 4 (at most 24 +// e2, e3, d3, d2, d1 (k_stage_e2, k_stagew2_e3, k_stagew2_d3, k_stage_d2, k_stage_d1) and e4 / d4 with NG = 4 (at most 24 // windows: 20 at 512 x 384; on 8 data sets) at a 512 x 384 base map; e4 / d4 with NG = 2 (above 24 windows) and m // (k_stagew4_m, 42 or more windows) at 1536 x 1280. Sized so that a one-stage wrong-rounding mutant of either form // changes its case's outputs (the mutant runs in the #254 evidence). diff --git a/clean/k/tests/test_k_runtime_d3d12.py b/clean/k/tests/test_k_runtime_d3d12.py index 8bdc5e68..b1d56551 100644 --- a/clean/k/tests/test_k_runtime_d3d12.py +++ b/clean/k/tests/test_k_runtime_d3d12.py @@ -78,7 +78,7 @@ def tonemapped_psnr(a, b): STATE = ["exposure.measured", "k.Hc", "k.L", "k.Hh"] # kernels a HDR / LDR frame of the default path launches (the D3D12 ports must run every one of them in the hybrid device) STAGES = ["k_stage2_e0", "k_stage2_e1", "k_stage_e2", "k_stagew2_e3", "k_stagew4_e4", "k_stagew4_m", "k_stagew4_d4", - "k_stagew2_d3", "k_stage_d2", "k_stage2_d1", "k_stage2_d0_hp"] + "k_stagew2_d3", "k_stage_d2", "k_stage_d1", "k_stage2_d0_hp"] ALWAYS = ["k_expo_scalars", "k_out_pixel_c"] TILED = ["kx/k_in_tiled", "kx/k_in_tiled_uniform"] LEGACY = ["kx/k_in_color", "kx/k_in_base"] # (+ kx/k_in_reset on a reset frame) @@ -379,7 +379,7 @@ def expect_fallback_trace(self, on_dx, big, split): k_split_m_wo / w1 / w2 have no softmax: they are the same launches in both forms, so only the attention phase has a mix form to forbid.""" ng = "k_stagew2_" if big else "k_stagew4_" - stages = ["k_stage_e2", "k_stagew2_e3", ng + "e4", ng + "d4", "k_stagew2_d3", "k_stage_d2", "k_stage2_d1"] + stages = ["k_stage_e2", "k_stagew2_e3", ng + "e4", ng + "d4", "k_stagew2_d3", "k_stage_d2", "k_stage_d1"] if split: rodd, mix = ["k_split_m_attn__rodd", "k_split_m_wo", "k_split_m_w1", "k_split_m_w2"], ["k_split_m_attn"] else: From c22762d25b0ad0848c2e7c67c4240b376b722e5a Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:25:51 -0500 Subject: [PATCH 7/7] Model K (D3D12): best-E retry folded into the output tap loop, uniform-O tap forms, FP32 bias planes for d1 / e2 / d2 (-140 us at 1440p Q vs 5c88a67, byte-identical) k_out_pixel_c (every form, __a2 included), 537 -> 448 us at 1440p Q (WG_D3D12_PROFILE, RX 9070 XT): - HIP's non-plain best-E retry recomputed both pairs' E and ran in most waves (on the test frames 87 % of the waves have a sub-sample whose best E is below -12). It is now a per-sub-sample best-E tap tracked in the 12-tap loop: tap n replaces the best unless E[n] < best. Same result for every input, NaN included (the argument is in the source): without a NaN the sequential best is the last maximum, so best < -12 only when max E < -12; with a NaN the weight sum is NaN (the retry ran) and both forms end on tap 11; the two rules differ only at E[n] = -0, best = +0, a best that stays >= -0. -70 us on its own. - The taps' E are formed tap by tap for both sub-sample pairs (each pair's sums in tap order). A wave whose lanes all have the same O forms the pair-independent values once (ko_line_E modes 1 / 2: one FP16 value per pair where both halves held the same one, du / dv shared by the pairs). A test-only build that ran every lane through the uniform form of its own O (both forms on every pixel) gave 0/6 diffs on all five scenarios and 64/64 in the port check. - The 4.3 bilinear's four taps written out (the [unroll] loop with the q == 0 select stayed a rolled four-iteration loop of selects in the ISA, -10 us); kk = Yp / s3 in a branch (it was a select: the division ran on every pixel). - The 5 x 5 luma window and the 12 taps' T held as FP16 pairs: 84 -> 71 VGPRs (neutral at 1440p Q). kx/k_in_tiled_color* (k_frame.hlsli kio_encode, near branch): 1 / (Y + 1) as kio_rcp, the v_rcp_f32 + Newton step of ki_rcp / ko_rcp_refined for x in [2^-32, 2^32], wg_div elsewhere (= wg_div on every input): 217 -> 214 us. k_stage_d1 / k_stage_e2 / k_stage_d2 (and their __rodd forms): KB_BIAS32 + KB_BASE_FORM, the runtime uploads their FP32 bias planes (narrow_bias32_kernel): d1 99 -> 90 us, e2 / d2 within noise. Runtime: d3d12_kernels() names k_stage_d1, not k_stage2_d1 (prepared at create, checked against the payload), and the bias-plane ports required at create are the launched ones (test_hybrid_refuses_missing_compact_head_port failed at 3a35b73 on k_stage2_d1; passes, run alone). - k_smx_selfcheck.h regenerated on HIP (gen_k_smx_selfcheck.py): the 133 reference hashes are unchanged, only kSourceDigest. k_port_check.py: the three bias-plane kernels. test_k_alpha.py: the written-out bilinear. - 0/6 diffs, 0 non-finite against 5c88a67: 1440p Q HDR pi 1.0 / 1.25 / 2.0, LDR pi 1.0, 645x363 -> 967x545 HDR. - k_port_check.py (HIP against D3D12, special values): narrow 132/132, output 32/32, direct k_out / k_expo 28/28. The input family (kx/k_in_tiled) fails 32/32 the same way with 3a35b73's source: a harness issue, not traced here. - test_k_runtime_d3d12 (D3D12 against HIP, frame by frame): 37/38 in one run, the 38th (the refusal above, which reads WG_DX12_KERNELS at library load) passes run alone. Full build: ISA checks pass, kernel sets unchanged. - 6-round ABBA against 5c88a67 (RX 9070 XT): pi 1.0 1.4681 -> 1.3277 ms, pi 1.25 1.4735 -> 1.3348, pi 2.0 1.4791 -> 1.3400; against 3a35b73 at pi 1.0: 1.4594 -> 1.3393. --- clean/k/dx12/hlsl/k_frame.hlsli | 12 +- clean/k/dx12/k_port_check.py | 3 +- clean/k/dx12/kernels/k_block.hlsl | 27 ++- clean/k/dx12/kernels/k_out.hlsl | 287 ++++++++++++++++++++---------- clean/k/runtime/k_smx_selfcheck.h | 2 +- clean/k/runtime/runtime_k.cpp | 22 ++- clean/k/tests/test_k_alpha.py | 4 +- 7 files changed, 246 insertions(+), 111 deletions(-) diff --git a/clean/k/dx12/hlsl/k_frame.hlsli b/clean/k/dx12/hlsl/k_frame.hlsli index cd2b2dcc..39972a78 100644 --- a/clean/k/dx12/hlsl/k_frame.hlsli +++ b/clean/k/dx12/hlsl/k_frame.hlsli @@ -266,9 +266,19 @@ float kio_decode(float t) const float e = kf_pm(t + kf_bits(0x3C4FAE29u), 9.0f) * kf_bits(0x3FB8AA3Bu); return wg_div(kf_exp2(e) - 1.0f, 9.0f * t); } +// 1 / x correctly rounded (= wg_div(1, x) on every input): one Newton step on v_rcp_f32 for x in [2^-32, 2^32] (the +// exhaustively GPU-checked reciprocal of k_out.hlsli ko_rcp_refined / k_in_common.hlsli ki_rcp), wg_div elsewhere +float kio_rcp(float x) +{ + [branch] if (x >= asfloat(0x2f800000u) && x <= asfloat(0x4f800000u)) { + const float r = rcp(x); + return mad(mad(-x, r, 1.0f), r, r); + } + return wg_div(1.0f, x); +} float kio_encode(float Y) { - if (Y < K_KIO_ENCODE_NEAR) return wg_div(1.0f, Y + 1.0f); + if (Y < K_KIO_ENCODE_NEAR) return kio_rcp(Y + 1.0f); return wg_div(wg_div(kf_ln(kf_fma(Y, 9.0f, 1.0f)), 9.0f) - kf_bits(0x3C4FAE29u), Y); } diff --git a/clean/k/dx12/k_port_check.py b/clean/k/dx12/k_port_check.py index b1e086f4..ac1a822c 100644 --- a/clean/k/dx12/k_port_check.py +++ b/clean/k/dx12/k_port_check.py @@ -361,7 +361,8 @@ def check_mixprobe(items, read): "k_stage2_e1": ("e1", "@w2", None), "k_stage2_d1": ("d1", "@w2", None), "k_stage2_d1__rodd": ("d1", "@w2", None), "k_stage2_d0_hp": ("d0", "@w2", "hp"), "k_stage2_d0_hidden": ("d0", "@w2", "hidden")} -NARROW_BIAS32 = {"k_stage2_e0", "k_stage2_e1", "k_stage2_d1", "k_stage2_d1__rodd", "k_stage2_d0_hp"} +NARROW_BIAS32 = {"k_stage2_e0", "k_stage2_e1", "k_stage2_d1", "k_stage2_d1__rodd", "k_stage2_d0_hp", "k_stage_e2", + "k_stage_d2", "k_stage_d1"} # block-level sizes (W, H) and window offsets (spec 44: even): grids with fewer than a workgroup's windows among them NARROW_SIZES = [(8, 8), (24, 16), (17, 13), (40, 24), (9, 33), (12, 20)] NARROW_OFFSETS = [(0, 0), (2, 2), (4, 0), (0, 6), (6, 4)] diff --git a/clean/k/dx12/kernels/k_block.hlsl b/clean/k/dx12/kernels/k_block.hlsl index 56edbe70..8d8a561c 100644 --- a/clean/k/dx12/kernels/k_block.hlsl +++ b/clean/k/dx12/kernels/k_block.hlsl @@ -18,26 +18,27 @@ // Basis: our HIP kernels (spec 41 block, spec 44 windows, spec 43 6.8 hidden history, spec 40 table); exactness and the // forms that differ (bit-identical) are listed in hlsl/k_block.hlsli and hlsl/k_common.hlsli. Arguments as HIP: // KBlkParams by value (token s184:p19.b32, 27 constants), the d0 stages also take the hidden history pointer. -// The narrow two-window ports take a FP32 bias plane, and d0_hp takes a separate compact head weight pointer. +// The narrow two-window ports and the one-window e2 / d2 / d1 take a FP32 bias plane (KB_BIAS32, with the lane-offset weight +// bases of KB_BASE_FORM), and d0_hp takes a separate compact head weight pointer. // Launch as HIP: 128 lanes (4 waves of 32) per workgroup, one workgroup per window (G = 1) or per two windows (G = 2). // -// @wg-kernel k_stage_e2 co=k_block.co block=128 wave=32 args=s184:p19.b32 +// @wg-kernel k_stage_e2 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage2_e0 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage2_e1 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 -// @wg-kernel k_stage_d2 co=k_block.co block=128 wave=32 args=s184:p19.b32 +// @wg-kernel k_stage_d2 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 // KB_X1LDS / KB_W2LATE / KB_W2SEQ (hlsl/k_block.hlsli): x1 waits in LDS during the MLP and each W2 tile is loaded just before its // own products: 231 -> 191 VGPRs (RGA gfx1201, no spill), under the 192 of four workgroups per CU; remove the three defines to undo. // @wg-kernel k_stage2_d1 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1,KB_X1LDS=1,KB_W2LATE=1,KB_W2SEQ=1 // @wg-kernel k_stage_e0 co=k_block.co block=128 wave=32 args=s184:p19.b32 defines=KB_LDS_HALFS=6144 // @wg-kernel k_stage_e1 co=k_block.co block=128 wave=32 args=s184:p19.b32 defines=KB_LDS_HALFS=4096 -// @wg-kernel k_stage_d1 co=k_block.co block=128 wave=32 args=s184:p19.b32 -// @wg-kernel k_stage_d1__rodd co=k_block.co entry=k_stage_d1 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 +// @wg-kernel k_stage_d1 co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_BIAS32=1,KB_BASE_FORM=1 +// @wg-kernel k_stage_d1__rodd co=k_block.co entry=k_stage_d1 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage2_d0_hp co=k_block.co block=128 wave=32 args=s184:p19.b32,p,p,p defines=KB_HIDDEN_ARG=1,KB_COMPACT_HEAD_ARG=1,KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage2_d0_hidden co=k_block.co block=128 wave=32 args=s184:p19.b32,p defines=KB_HIDDEN_ARG=1 // The __rodd forms: the same stages with the round-to-odd softmax weights (hlsl/k_smx.hlsli K_SMX 1), launched by the // native D3D12 runtime when the mix-form probe (kernels/k_mix_probe.hlsl) fails on the adapter's driver. -// @wg-kernel k_stage_e2__rodd co=k_block.co entry=k_stage_e2 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 -// @wg-kernel k_stage_d2__rodd co=k_block.co entry=k_stage_d2 block=128 wave=32 args=s184:p19.b32 defines=K_SMX=1 +// @wg-kernel k_stage_e2__rodd co=k_block.co entry=k_stage_e2 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1 +// @wg-kernel k_stage_d2__rodd co=k_block.co entry=k_stage_d2 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1 // @wg-kernel k_stage2_d1__rodd co=k_block.co entry=k_stage2_d1 block=128 wave=32 args=s184:p19.b32,p defines=K_SMX=1,KB_BIAS32=1,KB_BASE_FORM=1,KB_X1LDS=1,KB_W2LATE=1,KB_W2SEQ=1 #ifndef KB_LD_A16 #define KB_LD_A16 1 // 16-byte aligned loads: the whole address masked (hlsl/k_common.hlsli; 2 = scalar base only: loses RADV's b128 loads) @@ -81,7 +82,11 @@ WG_KERNEL(k_stage2_d0_hidden) WG_KERNEL(k_stage_e2) { WG_SETUP(); +#if KB_BIAS32 + k_block<64, 2, KMIX_SOFTMAX, KIN_MAP, 64, 96, false, 1, false, false, false>(0u, wg_block_id(), P_b32); +#else k_block<64, 2, KMIX_SOFTMAX, KIN_MAP, 64, 96, false, 1, false, false, false>(0u, wg_block_id()); +#endif } WG_KERNEL(k_stage2_e0) { @@ -104,7 +109,11 @@ WG_KERNEL(k_stage2_e1) WG_KERNEL(k_stage_d2) { WG_SETUP(); +#if KB_BIAS32 + k_block<64, 2, KMIX_SOFTMAX, KIN_EXPAND, 96, 0, false, 1, false, false, false>(0u, wg_block_id(), P_b32); +#else k_block<64, 2, KMIX_SOFTMAX, KIN_EXPAND, 96, 0, false, 1, false, false, false>(0u, wg_block_id()); +#endif } WG_KERNEL(k_stage2_d1) { @@ -128,6 +137,10 @@ WG_KERNEL(k_stage_e1) WG_KERNEL(k_stage_d1) { WG_SETUP(); +#if KB_BIAS32 + k_block<64, 2, KMIX_SOFTMAX, KIN_EXPAND, 64, 0, false, 1, false, false, false>(0u, wg_block_id(), P_b32); +#else k_block<64, 2, KMIX_SOFTMAX, KIN_EXPAND, 64, 0, false, 1, false, false, false>(0u, wg_block_id()); +#endif } #endif diff --git a/clean/k/dx12/kernels/k_out.hlsl b/clean/k/dx12/kernels/k_out.hlsl index 73053d12..4c84cee5 100644 --- a/clean/k/dx12/kernels/k_out.hlsl +++ b/clean/k/dx12/kernels/k_out.hlsl @@ -35,9 +35,11 @@ // 0x3bec, the same order on every input) (below 0x3bec = every tap in [0, 0.99): no soft // limit and unclamped row cubics, HIP's ko_cubic_unit; any other window: soft limit and clamped cubics, the same // values as HIP's per-tap decision). -// * Loops whose first iteration differs are peeled (DXC kept the 12-tap loop with an n == 0 test rolled, the taps then -// lived in indexed register arrays); the non-plain best-E retry recomputes E from opaque inputs (merged with the -// main loop's E it kept all of them live: 143 VGPRs). +// * Loops whose first iteration differs are peeled or written out (DXC kept the 12-tap loop with an n == 0 test rolled, +// the taps then lived in indexed register arrays; the 4.3 bilinear's four taps likewise). HIP's non-plain best-E +// retry is folded into the 12-tap loop as a per-sub-sample best-E tap (k_out_pixel_c, 5.4 - 5.6: the same result +// for every input, NaN included); the taps' E are formed once per tap for both sub-sample pairs, the pair-independent +// values once in waves whose lanes share O (ko_line_E). // * FP32 denormals flush on D3D12 (wg_math.hlsli); HIP keeps them. The stage's values (FP16 data, FP32 colour) do // not produce FP32 denormals; the port check covers the runtime's cases. // @@ -125,39 +127,138 @@ float4 ko_bilinear_hc(int w, int h, float cx, float cy) return t0 * omfy + t1 * fy; } -// E of the 12 taps (index 3 l + tc) for a sub-sample pair at (us, vs), in the spec's operation order (HIP each_E); FP16 -// pairs as packed words (kp_*). lines[l] = v0 + (half)l to avoid holding 4 extra half VGPRs. -void ko_each_E(uint us, uint vs, half v0, half cols[4], half tA, half tB, half tC, out uint E[12]) +// E of line l's three taps (index 3 l + tc) for both sub-sample pairs (E0: i2 = 0, E1: i2 = 1), in the spec's operation +// order (HIP each_E: dv = line - vs, t2v = (dv dv) tA, t3v = dv tB, du = col - us, mm = du t3v + t2v, then per tap +// du += 1, mm += t3v, E = (du du) tC + mm); FP16 pairs as packed words (kp_*), the line v0 + (half)l. mode 0: per-lane O +// (us, vs = Ys[i2] in both halves or xs2, chosen per lane). mode 1: a wave whose lanes all have O == 0: vs = Ys[i2] in +// both halves, so dv, (dv dv) tA and dv tB are one FP16 value per pair (the same operation on the same value as in each +// half), and us = xs2 for both pairs, so du and du du are formed once; the line is v0u + l (v0u = floor(Uy) - 1: v0 of +// an O == 0 lane, wave-uniform). mode 2: all O == 1: us = Ys[i2] in both halves (du, du du one value per pair), vs = xs2 +// for both pairs (dv and its products formed once). Every value is the one ko_each_E formed, half by half. +uint kp_fma_s1(half a, uint b, uint c) { return kh2_u(mad((half2)a, kh2_h(b), kh2_h(c))); } // a in both halves +uint kp_fma_ssp(half a, half s, uint c) { return kh2_u(mad((half2)a, (half2)s, kh2_h(c))); } +half kh_nz() { return asfloat16((uint16_t)(0x8000u ^ ko_z)); } // -0.0 through the opaque zero (as kp_nz2's halves) +void ko_line_E(int mode, int l, uint xs2, half Ys0, half Ys1, bool O, half v0, half v0u, half col, half tA, half tB, + half tC, out uint E0[3], out uint E1[3]) { - [unroll] for (int l = 0; l < 4; ++l) { - const uint dv = kp_rsub_s(v0 + (half)l, vs); + const half one = (half)1; + if (mode == 0) { + [unroll] for (int i = 0; i < 2; ++i) { + const uint ys2 = kp_splat(i == 0 ? Ys0 : Ys1); + const uint us = O ? ys2 : xs2, vs = O ? xs2 : ys2; + const uint dv = kp_rsub_s(v0 + (half)l, vs); + const uint t2v = kp_mul_s_n(kp_mul_n(dv, dv), tA); + const uint t3v = kp_mul_s(dv, tB); + uint du = kp_rsub_s(col, us); + uint mm = kp_fma(du, t3v, t2v); + [unroll] for (int tc = 0; tc < 3; ++tc) { + if (tc > 0) { + du = kp_add_s(du, one); + mm = kp_add(mm, t3v); + } + const uint e = kp_fma_s(kp_mul_n(du, du), tC, mm); + if (i == 0) E0[tc] = e; else E1[tc] = e; + } + } + } else if (mode == 1) { + const half ln = v0u + (half)l; + const half dv0 = ln - Ys0, dv1 = ln - Ys1; + const half t2v0 = (dv0 * dv0) * tA, t2v1 = (dv1 * dv1) * tA; + const half t3v0 = mad(dv0, tB, kh_nz()), t3v1 = mad(dv1, tB, kh_nz()); + uint du = kp_rsub_s(col, xs2); + uint mm0 = kp_fma_ss(du, t3v0, t2v0), mm1 = kp_fma_ss(du, t3v1, t2v1); + [unroll] for (int tc = 0; tc < 3; ++tc) { + if (tc > 0) { + du = kp_add_s(du, one); + mm0 = kp_add_s(mm0, t3v0); + mm1 = kp_add_s(mm1, t3v1); + } + const uint dd = kp_mul_n(du, du); + E0[tc] = kp_fma_s(dd, tC, mm0); + E1[tc] = kp_fma_s(dd, tC, mm1); + } + } else { + const uint dv = kp_rsub_s(v0 + (half)l, xs2); const uint t2v = kp_mul_s_n(kp_mul_n(dv, dv), tA); const uint t3v = kp_mul_s(dv, tB); - uint du = kp_rsub_s(cols[l], us); - uint mm = kp_fma(du, t3v, t2v); + half du0 = col - Ys0, du1 = col - Ys1; + uint mm0 = kp_fma_s1(du0, t3v, t2v), mm1 = kp_fma_s1(du1, t3v, t2v); [unroll] for (int tc = 0; tc < 3; ++tc) { if (tc > 0) { - du = kp_add_s(du, (half)1); - mm = kp_add(mm, t3v); + du0 = du0 + one; + du1 = du1 + one; + mm0 = kp_add(mm0, t3v); + mm1 = kp_add(mm1, t3v); } - E[3 * l + tc] = kp_fma_s(kp_mul_n(du, du), tC, mm); + E0[tc] = kp_fma_ssp(du0 * du0, tC, mm0); + E1[tc] = kp_fma_ssp(du1 * du1, tC, mm1); + } + } +} + +// 5.4 / 5.5 accumulation of one sub-sample pair over the taps in order: weights 2^E (two v_exp_f16), sum of weight x T, +// sum of weights, and HIP's best-E tap per half (its non-plain retry, folded in: see k_out_pixel_c) +struct KoAcc { + uint sgT, sg; + half bEl, bEh, bTl, bTh; +}; +void ko_acc(inout KoAcc a, int n, uint E, half T) +{ + const uint gg = kh2_u(exp2(kh2_h(E))); // ko_exp2h2 + const half el = kp_lo(E), eh = kp_hi(E); + if (n == 0) { + a.sgT = kp_mul_s(gg, T); + a.sg = gg; + a.bEl = el; a.bEh = eh; a.bTl = T; a.bTh = T; + } else { + a.sgT = kp_fma_s(gg, T, a.sgT); + a.sg = kp_add(gg, a.sg); + const bool tl = !(el < a.bEl), th = !(eh < a.bEh); // tap n replaces the best unless E[n] < best + a.bEl = tl ? el : a.bEl; + a.bTl = tl ? T : a.bTl; + a.bEh = th ? eh : a.bEh; + a.bTh = th ? T : a.bTh; + } +} +// tap n's filter value T of the packed pairs Tp (T[2 k] in the low half of Tp[k], T[2 k + 1] in the high half) +half ko_tap(uint Tp[6], int n) { return (n & 1) ? kp_hi(Tp[n >> 1]) : kp_lo(Tp[n >> 1]); } +void ko_taps(int mode, uint xs2, half Ys0, half Ys1, bool O, half v0, half v0u, half cols[4], half tA, half tB, + half tC, uint Tp[6], inout KoAcc A0, inout KoAcc A1) +{ + [unroll] for (int l = 0; l < 4; ++l) { + uint E0[3], E1[3]; + ko_line_E(mode, l, xs2, Ys0, Ys1, O, v0, v0u, cols[l], tA, tB, tC, E0, E1); + [unroll] for (int tc = 0; tc < 3; ++tc) { + ko_acc(A0, 3 * l + tc, E0[tc], ko_tap(Tp, 3 * l + tc)); + ko_acc(A1, 3 * l + tc, E1[tc], ko_tap(Tp, 3 * l + tc)); } } } +// a window tap as the 3.6 cubics read it: its FP16 value widened (unit window: every tap in [0, 0.99)), else the soft +// limit of it (ko_ell; HIP's per-tap form: the FP32 value of the FP16 bits, rounded back exactly, then ko_ell) +float ko_lw(uint h, bool unit) { return unit ? f16tof32(h) : ko_ell(kh_exact(f16tof32(h))); } // 3.6 luma history value of the four sub-samples from the (soft-limited) 5 x 5 window: 10 unique row cubics computed // one column j at a time (5 VGPRs rc[5] instead of 10, dropping 6 duplicate row cubics = 30 FMAs, 100% bit-identical). -void ko_ellv(float Lw[5][5], KoAxis axx, KoAxis axy, bool unit, out float ellv[4]) +// The window is held as its FP16 bits (two per register: Lh[r][c] in the low or high half of Lp[r][c / 2]) and each tap +// is widened (and soft-limited) where it is read: the same FP32 values as a widened 5 x 5 FP32 window, 13 registers +// instead of 25 (that window set the kernel's register count). +uint ko_lh(uint Lp[5][3], int r, int c) { return (c & 1) ? Lp[r][c >> 1] >> 16 : Lp[r][c >> 1] & 0xffffu; } +void ko_ellv(uint Lp[5][3], KoAxis axx, KoAxis axy, bool unit, out float ellv[4]) { [unroll] for (int j = 0; j < 2; ++j) { float rc[5]; - [unroll] for (int r = 0; r < 5; ++r) - rc[r] = unit ? ko_cubic_unit(Lw[r][j], Lw[r][j + 1], Lw[r][j + 2], Lw[r][j + 3], axx) - : ko_cubic(Lw[r][j], Lw[r][j + 1], Lw[r][j + 2], Lw[r][j + 3], axx); + [unroll] for (int r = 0; r < 5; ++r) { + const float a = ko_lw(ko_lh(Lp, r, j), unit), b = ko_lw(ko_lh(Lp, r, j + 1), unit); + const float c = ko_lw(ko_lh(Lp, r, j + 2), unit), d = ko_lw(ko_lh(Lp, r, j + 3), unit); + rc[r] = unit ? ko_cubic_unit(a, b, c, d, axx) : ko_cubic(a, b, c, d, axx); + } [unroll] for (int i = 0; i < 2; ++i) { const float val = ko_cubic(rc[i], rc[i + 1], rc[i + 2], rc[i + 3], axy); - const float lo = min(min(Lw[i + 1][j + 1], Lw[i + 1][j + 2]), min(Lw[i + 2][j + 1], Lw[i + 2][j + 2])); - const float hi = max(max(Lw[i + 1][j + 1], Lw[i + 1][j + 2]), max(Lw[i + 2][j + 1], Lw[i + 2][j + 2])); + const float w00 = ko_lw(ko_lh(Lp, i + 1, j + 1), unit), w01 = ko_lw(ko_lh(Lp, i + 1, j + 2), unit); + const float w10 = ko_lw(ko_lh(Lp, i + 2, j + 1), unit), w11 = ko_lw(ko_lh(Lp, i + 2, j + 2), unit); + const float lo = min(min(w00, w01), min(w10, w11)); + const float hi = max(max(w00, w01), max(w10, w11)); ellv[j + 2 * i] = min(max(val, lo), hi); } } @@ -213,32 +314,33 @@ WG_KERNEL(k_out_pixel_c) const half flx = floor(Ux), fly = floor(Uy); const half fx = kh_sat(Ux - flx), fy = kh_sat(Uy - fly); const half one = (half)1; - half w[4]; - w[0] = kh_mul(one - fx, one - fy); - w[1] = kh_mul(fx, one - fy); - w[2] = kh_mul(one - fx, fy); - w[3] = kh_mul(fx, fy); + const half w0 = kh_mul(one - fx, one - fy); + const half w1 = kh_mul(fx, one - fy); + const half w2 = kh_mul(one - fx, fy); + const half w3 = kh_mul(fx, fy); const int bx = (int)(o_x + (float)flx), by = WaveReadLaneFirst((int)(o_y + (float)fly)); - uint2 tq[4]; + uint2 tq0, tq1, tq2, tq3; // HIP KO_ADDR_FAST: the 2 x 2 taps inside the render image (no clamp): two 128-bit row loads [branch] if (bx >= 0 && bx <= f_rw - 2 && by >= 0 && by <= f_rh - 2) { const uint voff = ((uint)by * (uint)f_rw + (uint)bx) * 8u; const uint4 cr0 = wg_ld128(p_ctap, voff); const uint4 cr1 = wg_ld128(p_ctap + (uint)f_rw * 8u, voff); - tq[0] = cr0.xy; tq[1] = cr0.zw; - tq[2] = cr1.xy; tq[3] = cr1.zw; + tq0 = cr0.xy; tq1 = cr0.zw; + tq2 = cr1.xy; tq3 = cr1.zw; } else { - [unroll] for (int q = 0; q < 4; ++q) { - const int tx = ko_clampi(bx + (q & 1), f_rw), ty = ko_clampi(by + (q >> 1), f_rh); - tq[q] = wg_ld64(p_ctap, ((uint)ty * (uint)f_rw + (uint)tx) * 8u); - } - } - uint lo = 0u, hi = 0u; - [unroll] for (int q = 0; q < 4; ++q) { - const uint2 t = tq[q]; - lo = q == 0 ? kp_mul_s(t.x, w[q]) : kp_fma_s(t.x, w[q], lo); - hi = q == 0 ? kp_mul_s(t.y, w[q]) : kp_fma_s(t.y, w[q], hi); + const int txa = ko_clampi(bx, f_rw), txb = ko_clampi(bx + 1, f_rw); + const uint rya = (uint)ko_clampi(by, f_rh) * (uint)f_rw, ryb = (uint)ko_clampi(by + 1, f_rh) * (uint)f_rw; + tq0 = wg_ld64(p_ctap, (rya + (uint)txa) * 8u); + tq1 = wg_ld64(p_ctap, (rya + (uint)txb) * 8u); + tq2 = wg_ld64(p_ctap, (ryb + (uint)txa) * 8u); + tq3 = wg_ld64(p_ctap, (ryb + (uint)txb) * 8u); } + // the four taps written out (a [unroll] loop with the q == 0 select and indexed tq[q] / w[q] stayed rolled in + // the ISA: a 4-iteration loop of selects); the same operations in the same order + uint lo = kp_mul_s(tq0.x, w0), hi = kp_mul_s(tq0.y, w0); + lo = kp_fma_s(tq1.x, w1, lo); hi = kp_fma_s(tq1.y, w1, hi); + lo = kp_fma_s(tq2.x, w2, lo); hi = kp_fma_s(tq2.y, w2, hi); + lo = kp_fma_s(tq3.x, w3, lo); hi = kp_fma_s(tq3.y, w3, hi); // FP16 results converted to FP32 below pass the opaque (kp_opq / koh): the driver folds an FP16 fma / add // followed by the conversion into one v_fma_mix_f32 (no FP16 rounding in between) cbar_lo = kp_opq(lo); @@ -286,7 +388,7 @@ WG_KERNEL(k_out_pixel_c) // computed before Hh so Hh[4] does not stay live across Lw[5][5]) const KoAxis axx = ko_axis(wx), axy = ko_axis(wy); const int LW = 2 * W16, LH = 2 * H16; - float Lw[5][5]; + uint Lp[5][3]; // the window's FP16 bits, row r, columns 2 k (low half) and 2 k + 1 (high half) in Lp[r][k] uint Lmax16 = 0u; [branch] if ((uint)(axx.n - 1) <= (uint)(LW - 5) && (uint)(axy.n - 1) <= (uint)(LH - 5)) { // each row's five FP16 values from the three aligned dwords that hold them (one Load3 per row instead of five @@ -299,31 +401,32 @@ WG_KERNEL(k_out_pixel_c) const uint e1 = oddh ? (d.y >> 16) | (d.z << 16) : d.y; const uint e2 = oddh ? d.z >> 16 : d.z; const uint hv[5] = { e0 & 0xffffu, e0 >> 16, e1 & 0xffffu, e1 >> 16, e2 & 0xffffu }; - [unroll] for (int c = 0; c < 5; ++c) { - Lmax16 = max(Lmax16, hv[c]); - Lw[r][c] = f16tof32(hv[c]); - } + [unroll] for (int c = 0; c < 5; ++c) Lmax16 = max(Lmax16, hv[c]); + Lp[r][0] = e0; + Lp[r][1] = e1; + Lp[r][2] = e2 & 0xffffu; } } else { [unroll] for (int r = 0; r < 5; ++r) { const uint rb = (uint)ko_clampi(axy.n - 1 + r, LH) * (uint)LW; + uint hv[5]; [unroll] for (int c = 0; c < 5; ++c) { - const uint h = wg_ld16(p_L, (rb + (uint)ko_clampi(axx.n - 1 + c, LW)) * 2u); - Lmax16 = max(Lmax16, h); - Lw[r][c] = f16tof32(h); + hv[c] = wg_ld16(p_L, (rb + (uint)ko_clampi(axx.n - 1 + c, LW)) * 2u); + Lmax16 = max(Lmax16, hv[c]); } + Lp[r][0] = hv[0] | (hv[1] << 16); + Lp[r][1] = hv[2] | (hv[3] << 16); + Lp[r][2] = hv[4]; } } float ellv[4]; [branch] if (Lmax16 < 0x3becu) { - ko_ellv(Lw, axx, axy, true, ellv); + ko_ellv(Lp, axx, axy, true, ellv); } else { - [unroll] for (int r = 0; r < 5; ++r) - [unroll] for (int c = 0; c < 5; ++c) Lw[r][c] = ko_ell(kh_exact(Lw[r][c])); - ko_ellv(Lw, axx, axy, false, ellv); + ko_ellv(Lp, axx, axy, false, ellv); } - // 3.5 colour history (after Lw[5][5] is dead) + // 3.5 colour history (after the window is dead) float4 Hh = ko_bilinear_hc(W16, H16, wx * u_invW16, wy * u_invH16); // 3.2 blend logit (|t| <= 3 keeps 9 t^2 + 27 in [27, 108]: ko_div_inrange is exact without a wg_div cold branch) @@ -405,12 +508,12 @@ WG_KERNEL(k_out_pixel_c) } const half uP = O ? Uy : Ux, vP = O ? Ux : Uy; - // 5.3 taps (keep T[12] as half so driver doesn't unpack, drop lines[4] via v0 + (half)l, drop us2/vs2/gam[4]) + // 5.3 taps (T as FP16 pairs Tp, drop lines[4] via v0 + (half)l, drop us2/vs2/gam[4]) const half one = (half)1; const half v0 = floor(vP) - one; half cl = uP + kh_mul(v0 - vP, tS); half cols[4]; - half T[12]; + uint Tp[6]; // the 12 taps' T as FP16 pairs (ko_tap): six registers instead of twelve const float o_ln = O ? o_x : o_y, o_cl = O ? o_y : o_x; const int n_ln = O ? f_rw : f_rh, n_cl = O ? f_rh : f_rw; int as_[4]; @@ -447,17 +550,23 @@ WG_KERNEL(k_out_pixel_c) // dwords that hold each line's three values instead of three 16-bit loads (the same bytes); wave-uniform test, so // no wave runs both forms [branch] if (WaveActiveAllTrue(tfast && !O)) { + uint e0[4], e1[4]; [unroll] for (int l3 = 0; l3 < 4; ++l3) { const uint a = p_ctapT + toff[3 * l3]; const uint2 d = wg_mem.Load2(a & ~3u); - const uint e0 = (a & 2u) != 0u ? (d.x >> 16) | (d.y << 16) : d.x; - const uint e1 = (a & 2u) != 0u ? d.y >> 16 : d.y; - T[3 * l3] = kh_bits(e0 & 0xffffu); - T[3 * l3 + 1] = kh_bits(e0 >> 16); - T[3 * l3 + 2] = kh_bits(e1 & 0xffffu); + e0[l3] = (a & 2u) != 0u ? (d.x >> 16) | (d.y << 16) : d.x; // T[3 l], T[3 l + 1] + e1[l3] = (a & 2u) != 0u ? d.y >> 16 : d.y; // T[3 l + 2] (low half) } + // T pairs (2 k, 2 k + 1) across the lines' triples + Tp[0] = e0[0]; + Tp[1] = (e1[0] & 0xffffu) | (e0[1] << 16); + Tp[2] = (e0[1] >> 16) | (e1[1] << 16); + Tp[3] = e0[2]; + Tp[4] = (e1[2] & 0xffffu) | (e0[3] << 16); + Tp[5] = (e0[3] >> 16) | (e1[3] << 16); } else { - [unroll] for (int n2 = 0; n2 < 12; ++n2) T[n2] = kh_bits(wg_ld16(p_ctapT, toff[n2])); + [unroll] for (int k2 = 0; k2 < 6; ++k2) + Tp[k2] = wg_ld16(p_ctapT, toff[2 * k2]) | (wg_ld16(p_ctapT, toff[2 * k2 + 1]) << 16); } // 5.4 - 5.6 filtered values, 6.1 blend weights (sub-samples (i, 0), (i, 1) as the halves of half2 values) @@ -465,27 +574,36 @@ WG_KERNEL(k_out_pixel_c) const half k_u = O ? kvy : kux, k_v = O ? kux : kvy; half Fv[4]; const uint xs2 = kp_pack(Xs[0], Xs[1]); - const uint m12 = 0xca00ca00u; // (h16)-12 twice - bool plain = true; + const half m12 = kh_bits(0xca00u); // (h16)-12 uint gam2[2]; + // the taps of both sub-sample pairs, tap by tap (each pair's sums in tap order); waves whose lanes all have the same O + // share the pair-independent values (ko_line_E modes 1 / 2) + KoAcc acc[2]; + acc[0] = (KoAcc)0; + acc[1] = (KoAcc)0; + const half v0u = floor(Uy) - one; + [branch] if (WaveActiveAllTrue(!O)) { + ko_taps(1, xs2, Ys[0], Ys[1], O, v0, v0u, cols, tA, tB, tC, Tp, acc[0], acc[1]); + } else [branch] if (WaveActiveAllTrue(O)) { + ko_taps(2, xs2, Ys[0], Ys[1], O, v0, v0u, cols, tA, tB, tC, Tp, acc[0], acc[1]); + } else { + ko_taps(0, xs2, Ys[0], Ys[1], O, v0, v0u, cols, tA, tB, tC, Tp, acc[0], acc[1]); + } [unroll] for (int i2 = 0; i2 < 2; ++i2) { const uint ys2 = kp_splat(Ys[i2]); const uint us = O ? ys2 : xs2, vs = O ? xs2 : ys2; - uint E[12]; - ko_each_E(us, vs, v0, cols, tA, tB, tC, E); - uint gg = kh2_u(exp2(kh2_h(E[0]))); // ko_exp2h2: two v_exp_f16 - uint sgT = kp_mul_s(gg, T[0]), sg = gg, maxE = E[0]; - [unroll] for (int n = 1; n < 12; ++n) { - gg = kh2_u(exp2(kh2_h(E[n]))); - sgT = kp_fma_s(gg, T[n], sgT); - sg = kp_add(gg, sg); - maxE = kp_max(maxE, E[n]); - } - const uint sgTo = kp_opq(sgT), sgo = kp_opq(sg); // (converted to FP32 by kh_div) + const KoAcc a = acc[i2]; + const uint sgTo = kp_opq(a.sgT), sgo = kp_opq(a.sg); // (converted to FP32 by kh_div) Fv[2 * i2] = kh_div(kp_lo(sgTo), kp_lo(sgo)); Fv[2 * i2 + 1] = kh_div(kp_hi(sgTo), kp_hi(sgo)); - plain = plain && kp_lo(maxE) >= kp_lo(m12) && kp_hi(maxE) >= kp_hi(m12) && kp_lo(sg) == kp_lo(sg) && - kp_hi(sg) == kp_hi(sg); + // HIP (each_E best retry): if any of the four sub-samples has max E < -12 or a NaN weight sum, every sub-sample + // whose sequential best E (tap n replaces the best when E[n] - best is positive, +0 or NaN) is below -12 takes + // that tap's T. Folded per sub-sample without the plain test: with no NaN the sequential best is the maximum (the + // last one), so best < -12 only when max E < -12; with a NaN the sum is NaN (the retry runs) and both forms end + // on tap 11. "Unless E[n] < best" differs from "E[n] - best positive, +0 or NaN" only at E[n] = -0, best = +0, + // a best that stays >= -0 (no effect) unless a later NaN, after which both take every tap. + if (a.bEl < m12) Fv[2 * i2] = a.bTl; + if (a.bEh < m12) Fv[2 * i2 + 1] = a.bTh; const uint dvf = kp_sub(kp_rni(vs), vs); const uint up = kp_fma_s(dvf, tS, us); const uint duf = kp_sub(kp_rni(up), up); @@ -494,25 +612,6 @@ WG_KERNEL(k_out_pixel_c) const uint wv = kp_sat_nz(kp_fma_ss(kp_abs(dvf), k_v, c45)); gam2[i2] = kp_mul(kp_fma_ss(wv, tK, tk6), wu); } - [branch] if (!plain) { - [unroll] for (int i3 = 0; i3 < 2; ++i3) { - const uint ys2_o = kp_splat(Ys[i3]); - const uint us_o = (O ? ys2_o : xs2) ^ (ko_z * 0x10001u), vs_o = (O ? xs2 : ys2_o) ^ (ko_z * 0x10001u); - uint E[12]; - ko_each_E(us_o, vs_o, v0, cols, tA, tB, tC, E); - uint bestE = E[0]; - half bTl = T[0], bTh = T[0]; - [unroll] for (int n = 1; n < 12; ++n) { - const uint d = kp_sub(E[n], bestE); - const bool kl = (d & 0xffffu) - 0x8000u <= 0x7c00u, kh = (d >> 16) - 0x8000u <= 0x7c00u; - bestE = (kl ? bestE & 0xffffu : E[n] & 0xffffu) | (kh ? bestE & 0xffff0000u : E[n] & 0xffff0000u); - bTl = kl ? bTl : T[n]; - bTh = kh ? bTh : T[n]; - } - if (kp_lo(bestE) < kp_lo(m12)) Fv[2 * i3] = bTl; - if (kp_hi(bestE) < kp_hi(m12)) Fv[2 * i3 + 1] = bTh; - } - } const half gbar = kh_mul((half)0.25, (kp_lo(gam2[0]) + kp_lo(gam2[1])) + (kp_hi(gam2[0]) + kp_hi(gam2[1]))); const half mh = kh_bits(ma_r & 0xffffu); const half MU = kh_bits(0x27aeu), NU = kh_bits(0x3c20u); // (h16)0.03f, (h16)(1.0 / 0.97) @@ -568,7 +667,9 @@ WG_KERNEL(k_out_pixel_c) const float s1 = (Cgo - eps < -Yp) ? eps - Cgo : Yp; const float s2 = ((Coo - Cgo) - eps < -s1) ? (Cgo - Coo) + eps : s1; const float s3 = ((-Coo - Cgo) - eps < -s2) ? (Coo + Cgo) + eps : s2; - const float kk = s3 <= Yp ? 1.0f : ko_div(Yp, s3); + // kk = s3 <= Yp ? 1 : Yp / s3, with the division in a branch (as a select it ran on every pixel; s3 <= Yp on most) + float kk = 1.0f; + [branch] if (!(s3 <= Yp)) kk = ko_div(Yp, s3); if (!active) return; wg_st64(p_Hcout, ((uint)y * (uint)W16 + (uint)x) * 8u, diff --git a/clean/k/runtime/k_smx_selfcheck.h b/clean/k/runtime/k_smx_selfcheck.h index ce9b055a..823bc69f 100644 --- a/clean/k/runtime/k_smx_selfcheck.h +++ b/clean/k/runtime/k_smx_selfcheck.h @@ -8,7 +8,7 @@ namespace k_smx_selfcheck { -constexpr char kSourceDigest[] = "944f9b6e380c6343f501a29d58a1780b460ac8ed3ff400b73781baa24a910030"; +constexpr char kSourceDigest[] = "76a4895c8a74cdda6d610801827912b1e6a84123085abab5269f9549fb6e7c32"; constexpr int kCount = 133; constexpr const char* kNames[kCount] = { "k_stage_e2@512x384/o4,4", diff --git a/clean/k/runtime/runtime_k.cpp b/clean/k/runtime/runtime_k.cpp index 1c8ebd12..730048d9 100644 --- a/clean/k/runtime/runtime_k.cpp +++ b/clean/k/runtime/runtime_k.cpp @@ -704,13 +704,15 @@ bool KRuntime::create(const WgRtConfig& c, Device* dev, AllocFn alloc_fn, bool e wdev_[kv.first] = p; } if (!f16_ && (c.backend == 2 || c.backend == 3)) { - for (const char* name : {"e0", "e1", "d1", "d0"}) { + // the FP32 bias planes of the narrow stages whose D3D12 kernels take one (KB_BIAS32): the two-window e0 / e1 / d0 and + // the one-window e2 / d2 / d1 (network() launches them so, stage_kernel_base) + for (const char* name : {"e0", "e1", "d1", "d0", "e2", "d2"}) { const KStageInfo& st = *k_stage(name); if (f8data_.count(name) || (st.head && !compact_head_)) continue; - const std::string kernel = std::string("k_stage2_") + name + (st.head ? "_hp" : ""); + const bool one = std::strcmp(name, "e0") != 0 && std::strcmp(name, "e1") != 0 && !st.head; + const std::string kernel = std::string(one ? "k_stage_" : "k_stage2_") + name + (st.head ? "_hp" : ""); if (!dev_->require_d3d12_port(kernel, st.head ? "s184:p19.b32,p,p,p" : "s184:p19.b32,p")) return dfail(); - if (c.backend == 3 && std::strcmp(name, "d1") == 0 && - !dev_->require_d3d12_port(kernel + "__rodd", "s184:p19.b32,p")) return dfail(); + if (c.backend == 3 && one && !dev_->require_d3d12_port(kernel + "__rodd", "s184:p19.b32,p")) return dfail(); const auto& weight = blobs_.at(name); std::vector compact; if (st.head) { @@ -916,6 +918,12 @@ std::string KRuntime::smx_case_name(int i, int ox, int oy, bool rodd) const { (c.sets > 1 ? "*" + std::to_string(c.sets) : std::string()) + "/o" + std::to_string(ox) + "," + std::to_string(oy); } +// the narrow stage kernels (D3D12) that take the FP32 bias plane narrow_bias_[stage] as their second argument (KB_BIAS32) +bool narrow_bias32_kernel(const std::string& k) { + const std::string b = k.size() > 6 && k.compare(k.size() - 6, 6, "__rodd") == 0 ? k.substr(0, k.size() - 6) : k; + return b == "k_stage2_e0" || b == "k_stage2_e1" || b == "k_stage_d1" || b == "k_stage_e2" || b == "k_stage_d2"; +} + // One form of every case at its offsets (this model's: all = false; every possible one: all = true): (case name of the // mix form, the outputs' SHA-256, first 8 bytes big-endian) into *out. bool KRuntime::smx_check(bool rodd, bool all, std::vector>* out) { @@ -963,7 +971,7 @@ bool KRuntime::smx_check(bool rodd, bool all, std::vector& after) { } else { const std::string kernel = stage_kernel(k, st.name); const auto bias_plane = narrow_bias_.find(st.name); - const bool use_bias = k == "k_stage2_e0" || k == "k_stage2_e1" || k == "k_stage2_d1"; + const bool use_bias = narrow_bias32_kernel(k); void* bias = use_bias && bias_plane != narrow_bias_.end() ? &bias_plane->second : nullptr; if (!launch(kernel.c_str(), grid, (unsigned)(kThreads * (ng ? ng : 1)), &p, bias)) return false; } @@ -1657,7 +1665,7 @@ std::vector KRuntime::d3d12_kernels() const { std::string k; if (st.head) k = compact_head_ ? "k_stage2_d0_hp" : "k_stage2_d0_hidden"; else if (ng) k = "k_stagew" + std::to_string(ng) + "_" + n; - else k = (n == "e0" || n == "e1" || n == "d1" ? "k_stage2_" : "k_stage_") + n; + else k = (n == "e0" || n == "e1" ? "k_stage2_" : "k_stage_") + n; // d1: k_stage_d1 (stage_kernel_base) if (std::find(v.begin(), v.end(), k) == v.end()) v.push_back(k); // the round-to-odd form the create-time self-check can select (KRuntime::smx_select) if (!st.head && n != "e0" && n != "e1" && std::find(v.begin(), v.end(), k + "__rodd") == v.end()) diff --git a/clean/k/tests/test_k_alpha.py b/clean/k/tests/test_k_alpha.py index 1c6abd47..2048d1e3 100644 --- a/clean/k/tests/test_k_alpha.py +++ b/clean/k/tests/test_k_alpha.py @@ -251,7 +251,9 @@ def test_d3d12_output_kernel(self): self.assertNotIn("KO_ALPHA", o) # the stored tap's A: RN16 of the colour's fourth value; the bilinear: RN16(w0 a0), then three FP16 fmas self.assertIn("tap = uint2(hY | (hCo << 16), hCg | (ko_f2h(c.w) << 16));", oi) - self.assertIn("hi = q == 0 ? kp_mul_s(t.y, w[q]) : kp_fma_s(t.y, w[q], hi);", o) + self.assertIn("uint lo = kp_mul_s(tq0.x, w0), hi = kp_mul_s(tq0.y, w0);", o) + for q in (1, 2, 3): + self.assertIn("hi = kp_fma_s(tq%d.y, w%d, hi);" % (q, q), o) self.assertIn("cbar_hi = kp_opq(hi);", o) self.assertIn("cbar3 = kp_hi(cbar_hi);", o) # every output form stores cbar3 unchanged