// r4d_dequant_w4_bf16.hip - r4d_dequant_w4_bf16: the packed 4-bit weight back to a plain row-major // bf16 [N, K]. // // WHY THIS EXISTS. W4A8 buys nothing at prefill: the iu8 and fp8 matrix rates are within a couple // of percent of each other and the shape is compute-bound. But once the weights are STORED at 4 // bits there is no fp8 copy to fall back on, so the requirement at prefill is "do not regress", not // "win" -- and the skinny W4A8 GEMM cannot meet even that, because a block owns 64 rows and // therefore re-reads the whole weight slice once per 64 output rows. Its rate is flat in M for // that reason; it is structural and no choice of geometry fixes it. Dequantising one layer into a // scratch buffer and handing it to the tuned bf16 GEMM costs one extra write and read of the bf16 // weight, a few percent of the GEMM it feeds. // // TWO ENCODINGS SHARE THESE BYTES AND THEY DIFFER BY BIT 3, so the caller has to say which. The // symmetric grid stores a TWO'S COMPLEMENT code -- that is what lets the int8 GEMM read the // high-nibble byte signed and get 16*q directly -- and its value is (n^8)-8, i.e. n=8 means -8. // The asymmetric grid stores an UNSIGNED nibble and subtracts a per-group zero, which is what lets // the same GEMM read it unsigned and correct with an activation row-sum. Feeding one to the other // is silently wrong rather than obviously wrong: it agrees for n<8 and is off by 16 above. // // It is very nearly lossless: the stored value is a 4-bit code times an f16 scale, and bf16's // 8-bit mantissa holds that to within a ULP. // // UNDOING THE FRAGMENT ORDER. radiance_w4.pack lays a dword out as [t][kb][lh][r][s] with eight // nibbles along i, where t = n/16, r = n%16, kb = k/64, s = (k%64)/16, and the k inside the step is // 4*lh + _KOFF[i] with _KOFF = {0,8,1,9,2,10,3,11}. One thread per dword scatters its eight // nibbles; the scale dword lives at [t][g][r] and carries the f16 scale low and -(1024+zero) high, // so the same routine serves the symmetric grid (zero == 8) and the asymmetric one. #include "r4d.h" #include "r4d_gemm_args.h" #include #include #include #include #define R4D_DQ_KPB 64 #define R4D_DQ_THREADS 256 __constant__ int r4d_dq_koff[8] = {0, 8, 1, 9, 2, 10, 3, 11}; __global__ __launch_bounds__(R4D_DQ_THREADS) void r4d_dequant_w4_bf16_kernel( const unsigned* __restrict__ Wq, const unsigned* __restrict__ Ws, __hip_bfloat16* __restrict__ Out, int N, int K, int group, int twos) { const long total = (long)N * K / 8; // one thread per packed dword const int nkb = K / R4D_DQ_KPB, ng = K / group; for (long idx = blockIdx.x * (long)blockDim.x + threadIdx.x; idx < total; idx += (long)gridDim.x * blockDim.x) { // [t][kb][lh][r][s] const int s = (int)(idx & 3); const int r = (int)((idx >> 2) & 15); const int lh = (int)((idx >> 6) & 1); const int kb = (int)((idx >> 7) % nkb); const int t = (int)((idx >> 7) / nkb); const int row_n = t * 16 + r; const unsigned w = Wq[idx]; const int kbase = kb * R4D_DQ_KPB + s * 16 + 4 * lh; const unsigned sz = Ws[(size_t)(t * ng + kbase / group) * 16 + r]; const float sc = __half2float(__ushort_as_half((unsigned short)(sz & 0xFFFFu))); const float nz = __half2float(__ushort_as_half((unsigned short)(sz >> 16))); const float zero = -nz - 1024.f; // exact for zero in 0..15 #pragma unroll for (int i = 0; i < 8; i++) { const int n = (int)((w >> (4 * i)) & 0xF); const float q = twos ? (float)((n ^ 8) - 8) : (float)n; // 4*lh is part of the k, not just of the scale lookup: position (lh, i) holds // kk = 4*lh + _KOFF[i], so lh=0 covers {0,8,1,9,2,10,3,11} and lh=1 the other eight. const int k = kbase + r4d_dq_koff[i]; Out[(size_t)row_n * K + k] = __float2bfloat16((q - (twos ? 0.f : zero)) * sc); } } } void r4d_dequant_w4_bf16(long wq, long ws, long out, int N, int K, int group, int twos, long stream) { if (N % 16) throw std::runtime_error("r4d dequant_w4_bf16: N must be a multiple of 16"); if (K % R4D_DQ_KPB) throw std::runtime_error("r4d dequant_w4_bf16: K must be a multiple of 64"); if (group < 1 || K % group) throw std::runtime_error( "r4d dequant_w4_bf16: group must divide K"); const long total = (long)N * K / 8; const int blocks = (int)((total + R4D_DQ_THREADS - 1) / R4D_DQ_THREADS); hipLaunchKernelGGL(r4d_dequant_w4_bf16_kernel, dim3(blocks < 65535 ? blocks : 65535), dim3(R4D_DQ_THREADS), 0, (hipStream_t)stream, (const unsigned*)wq, (const unsigned*)ws, (__hip_bfloat16*)out, N, K, group, twos); } // ============================================================== the plugin ABI entry points // // The operand reader and the split law are in r4d_gemm_args.h. See libr4d/r4d_args.h for why an // entry point takes the ABI's own argument list directly rather than going through an adapter. /* ------------------------------------------------------------------ dequant */ enum { D_WQ = 0, D_WS, D_Y }; extern "C" int r4d_rad_dequant_w4(const RadArgs* a, RadStream s) { const RadTensor* wq = r4d_opt(a, D_WQ); const RadTensor* ws = r4d_opt(a, D_WS); const RadTensor* y = r4d_opt(a, D_Y); if (!wq || !ws || !y) return R4D_NO(RAD_E_INVAL); if (y->rank != 2 || !r4d_contig(y)) return R4D_NO(RAD_E_STRIDE); const long long N = y->shape[0]; const long long K = y->shape[1]; const long long group = r4d_geti_or(a, "group", R4D_W4_GROUP); const long long twos = r4d_geti_or(a, "twos", 1); if (N % 16 || K % 64) return R4D_NO(RAD_E_SHAPE); if (group < 1 || K % group) return R4D_NO(RAD_E_SHAPE); if (twos != 0 && twos != 1) return R4D_NO(RAD_E_INVAL); R4D_TRY("dequant_w4_bf16", r4d_dequant_w4_bf16(r4d_addr(wq), r4d_addr(ws), r4d_addr(y), (int)N, (int)K, (int)group, (int)twos, (long)(uintptr_t)s)); return RAD_OK; }