diff --git a/engine/include/strata/hip_compat/dp4a_rdna1.hpp b/engine/include/strata/hip_compat/dp4a_rdna1.hpp index 4ecf9c9474..2e8dd78d49 100644 --- a/engine/include/strata/hip_compat/dp4a_rdna1.hpp +++ b/engine/include/strata/hip_compat/dp4a_rdna1.hpp @@ -1,35 +1,35 @@ -// dp4a for RDNA1 (gfx1010 / gfx1012): the signed four-lane byte dot product, c + sum_k (signed byte k of a) * (signed byte k of b), -// accumulated modulo 2^32, with the same semantics as STRATA_DP4A in kernels/dp4a.hpp. These chips have no dot instruction. -// -// The body is plain integer C++ (plus empty asm statements as fusion barriers). The AMDGPU back end turns the byte extractions into -// sub-dword (SDWA) operand selects of the 24-bit integer multiply and adds the products with three-input adds; a and b may be the -// same register and c is only an input. No dot instruction is used. The function exists only when compiling for gfx1010 or gfx1012. -// -// intrinsics.hpp calls this from its gfx1010 / gfx1012 branch (integrator: see P2-RESULTS.md, packet P-1). -#pragma once - -#include - -#if defined(__gfx1010__) || defined(__gfx1012__) - -namespace strata::hip_compat { - -__device__ __forceinline__ int dp4a_rdna1(int a, int b, int c) { - const uint32_t ua = (uint32_t)a, ub = (uint32_t)b; - int p0 = (int)(int8_t)(ua) * (int)(int8_t)(ub); - int p1 = (int)(int8_t)(ua >> 8) * (int)(int8_t)(ub >> 8); - int p2 = (int)(int8_t)(ua >> 16) * (int)(int8_t)(ub >> 16); - int p3 = (int)(int8_t)(ua >> 24) * (int)(int8_t)(ub >> 24); - // An empty asm on each product keeps the back end from fusing the four products into a dot instruction (gfx1012 would - // otherwise get one): the byte multiplies stay SDWA multiplies and the sum stays three-input adds. - asm("" : "+v"(p0)); - asm("" : "+v"(p1)); - asm("" : "+v"(p2)); - asm("" : "+v"(p3)); - // unsigned adds keep the wrap-around defined - return (int)((uint32_t)c + (uint32_t)p0 + (uint32_t)p1 + (uint32_t)p2 + (uint32_t)p3); -} - -} // namespace strata::hip_compat - -#endif +// dp4a for RDNA1 (gfx1010 / gfx1012): the signed four-lane byte dot product, c + sum_k (signed byte k of a) * (signed byte k of b), +// accumulated modulo 2^32, with the same semantics as STRATA_DP4A in kernels/dp4a.hpp. These chips have no dot instruction. +// +// The body is plain integer C++ (plus empty asm statements as fusion barriers). The AMDGPU back end turns the byte extractions into +// sub-dword (SDWA) operand selects of the 24-bit integer multiply and adds the products with three-input adds; a and b may be the +// same register and c is only an input. No dot instruction is used. The function exists only when compiling for gfx1010 or gfx1012. +// +// intrinsics.hpp calls this from its gfx1010 / gfx1012 branch. +#pragma once + +#include + +#if defined(__gfx1010__) || defined(__gfx1012__) + +namespace strata::hip_compat { + +__device__ __forceinline__ int dp4a_rdna1(int a, int b, int c) { + const uint32_t ua = (uint32_t)a, ub = (uint32_t)b; + int p0 = (int)(int8_t)(ua) * (int)(int8_t)(ub); + int p1 = (int)(int8_t)(ua >> 8) * (int)(int8_t)(ub >> 8); + int p2 = (int)(int8_t)(ua >> 16) * (int)(int8_t)(ub >> 16); + int p3 = (int)(int8_t)(ua >> 24) * (int)(int8_t)(ub >> 24); + // An empty asm on each product keeps the back end from fusing the four products into a dot instruction (gfx1012 would + // otherwise get one): the byte multiplies stay SDWA multiplies and the sum stays three-input adds. + asm("" : "+v"(p0)); + asm("" : "+v"(p1)); + asm("" : "+v"(p2)); + asm("" : "+v"(p3)); + // unsigned adds keep the wrap-around defined + return (int)((uint32_t)c + (uint32_t)p0 + (uint32_t)p1 + (uint32_t)p2 + (uint32_t)p3); +} + +} // namespace strata::hip_compat + +#endif diff --git a/engine/include/strata/hip_compat/intrinsics.hpp b/engine/include/strata/hip_compat/intrinsics.hpp index 49558dcc85..f81575240e 100644 --- a/engine/include/strata/hip_compat/intrinsics.hpp +++ b/engine/include/strata/hip_compat/intrinsics.hpp @@ -7,6 +7,8 @@ #include +#include "dp4a_rdna1.hpp" + namespace strata::hip_compat { __device__ __forceinline__ int signed_byte(uint32_t word, int lane) { @@ -33,18 +35,8 @@ __device__ __forceinline__ int dp4a(int a, int b, int c) { // operands directly, avoiding separate shift/mask/sign-extension work. gfx1010 takes this since #4855 (Keoian; no card here // runs it): the community switch, cmake -DSTRATA_AMD_COMMUNITY_TUNING=OFF, defines STRATA_AMD_COMMUNITY_DOT_OFF and // gfx1010 gets the portable loop again (strata/core/amd_community.hpp). - // Adapted from pinned llama.cpp ggml-cuda/common.cuh (MIT; see - // third_party/ggml/LICENSE), same modulo-2^32 accumulation contract. - int lo, hi; - asm("v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0 src1_sel:BYTE_0\n" - "v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:BYTE_1\n" - "v_add3_u32 %0, %1, %2, %0\n" - "v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2 src1_sel:BYTE_2\n" - "v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_3 src1_sel:BYTE_3\n" - "v_add3_u32 %0, %1, %2, %0\n" - // c changes before the high-byte reads; it must not alias a or b. - : "+&v"(c), "=&v"(lo), "=&v"(hi) : "v"(a), "v"(b)); - return c; + // same modulo-2^32 accumulation contract as the other branches (hip_compat/dp4a_rdna1.hpp, no dot instruction). + return strata::hip_compat::dp4a_rdna1(a, b, c); #else const uint32_t ua = static_cast(a); const uint32_t ub = static_cast(b); diff --git a/engine/include/strata/kernels/dp4a.hpp b/engine/include/strata/kernels/dp4a.hpp index 5ac6047f07..097016c1d6 100644 --- a/engine/include/strata/kernels/dp4a.hpp +++ b/engine/include/strata/kernels/dp4a.hpp @@ -1,55 +1,43 @@ -// include/strata/kernels/dp4a.hpp - the two instructions the kernels use that Pascal (sm_60) does not have. +// STRATA_DP4A(a, b, c): the signed four-lane byte dot product with an int32 accumulator, for device code, plus strata_spin_pause(). // -// The engine is written for compute capability 7.5 and newer and refuses older cards at CMake configure time -// (a Pascal build passes -DSTRATA_EXPERIMENTAL_SM60=ON). A GP100-class card is compute capability 6.0, which is -// missing exactly two things the i-quant kernels rely on: +// STRATA_DP4A(a, b, c) == c + sum over k = 0..3 of (signed byte k of a) * (signed byte k of b), modulo 2^32. // -// * `__dp4a`, a byte-wise dot product, available from 6.1 (Pascal GP10x/GV11x). The reference for its -// fallback is llama.cpp's own (`ggml/src/ggml-cuda/common.cuh`), since the kernels in this directory are -// transcribed from llama.cpp's vecdotq.cuh and that wrapper's operands are the same sites: it reinterprets -// the operands as SIGNED bytes and accumulates in int32, which is what `__dp4a` does for these calls. The -// fallback below computes exactly that with four PTX `vmad` byte-select instructions, so it is bit-exact -// rather than merely close (tools/sm60_dp4a_check.cu checks it against the byte-wise form). -// * `__nanosleep`, available from 7.0 (Volta). It only paces single-thread doorbell waits, so a loop that -// spins without it is correct, merely busier. +// Where the hardware has the instruction (CUDA compute capability >= 6.1, and HIP through hip_compat) it is __dp4a. Below 6.1 the +// same value is formed in registers: each operand byte is sign-extended, multiplied, and the four products are added to c. The +// operands are plain values, so a and b may be the same register and c may share a register with either of them. // -// Both are compile-time selections on `__CUDA_ARCH__`, so one source tree builds for Pascal and for RTX -// 20/30/40/50 without a #define at the call sites. A HIP build never takes either fallback: `__CUDA_ARCH__` is -// undefined there, so `STRATA_DP4A` is `__dp4a` (which `hip_compat/intrinsics.hpp` provides) and the pause is -// HIP's own `__nanosleep`. #pragma once #include + #if defined(STRATA_HIP_GFX906) -#include // gfx906: the compat layer (__forceinline__, __nanosleep, __dp4a) +#include // the HIP compat layer: __dp4a, __nanosleep #endif -#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 610 +#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 610) + +// Software form for sm_60 and older: four chained PTX `vmad` instructions, one per byte lane. vmad with the .s32 types and a byte +// selector (.b0 .. .b3) sign-extends the selected bytes of both sources, multiplies, and adds the third operand modulo 2^32 (no .sat). +// Each lane is its own single-instruction asm statement, so an output register may share a register with any input without harm. __device__ __forceinline__ int strata_dp4a(const int a, const int b, const int c) { - // PTX vmad with byte selectors: compiles to four VMAD.S8.S8 (cuobjdump -sass), no byte extraction. - // Bit-exact with the byte-wise form it replaces: 1.07e9 cases (edge bytes, every per-lane byte pair, random), - // 0 mismatches, ~2.2x faster in isolation on a Tesla P100 (tools/sm60_dp4a_check.cu). - int r = c; - asm("vmad.s32.s32.s32 %0, %1.b0, %2.b0, %0;\n\t" - "vmad.s32.s32.s32 %0, %1.b1, %2.b1, %0;\n\t" - "vmad.s32.s32.s32 %0, %1.b2, %2.b2, %0;\n\t" - "vmad.s32.s32.s32 %0, %1.b3, %2.b3, %0;" - // The first instruction writes r before the remaining instructions - // have read a/b. Equal-valued operands must not share its register. - : "+&r"(r) : "r"(a), "r"(b)); + int r; + asm("vmad.s32.s32.s32 %0, %1.b0, %2.b0, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c)); + asm("vmad.s32.s32.s32 %0, %1.b1, %2.b1, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(r)); + asm("vmad.s32.s32.s32 %0, %1.b2, %2.b2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(r)); + asm("vmad.s32.s32.s32 %0, %1.b3, %2.b3, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(r)); return r; } #define STRATA_DP4A(a, b, c) strata_dp4a((a), (b), (c)) + #else + #define STRATA_DP4A(a, b, c) __dp4a((a), (b), (c)) + #endif -/// Yield the thread while a doorbell flag is being polled. The length of the pause is a backoff hint, not a -/// contract, and only pre-Volta CUDA has no `__nanosleep` at all. +// Yield the thread while a flag is polled. Below compute capability 7.0 there is no sleep instruction and a plain spin is correct. __device__ __forceinline__ void strata_spin_pause() { -#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700 - // sm_6x: the loop spins. Every call site is a single-thread doorbell wait, so nothing else is delayed. -#else - __nanosleep(100); +#if !(defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 700)) + __nanosleep(100); // every target but CUDA below 7.0 (HIP's compat layer has __nanosleep too) #endif } diff --git a/engine/include/strata/kernels/dp4a_clean.hpp b/engine/include/strata/kernels/dp4a_clean.hpp deleted file mode 100644 index 061ce596d0..0000000000 --- a/engine/include/strata/kernels/dp4a_clean.hpp +++ /dev/null @@ -1,44 +0,0 @@ -// STRATA_DP4A(a, b, c): the signed four-lane byte dot product with an int32 accumulator, for device code, plus strata_spin_pause(). -// -// STRATA_DP4A(a, b, c) == c + sum over k = 0..3 of (signed byte k of a) * (signed byte k of b), modulo 2^32. -// -// Where the hardware has the instruction (CUDA compute capability >= 6.1, and HIP through hip_compat) it is __dp4a. Below 6.1 the -// same value is formed in registers: each operand byte is sign-extended, multiplied, and the four products are added to c. The -// operands are plain values, so a and b may be the same register and c may share a register with either of them. -// -// This header replaces include/strata/kernels/dp4a.hpp (integrator: see P2-RESULTS.md, packet P-1). -#pragma once - -#include - -#if defined(STRATA_HIP_GFX906) -#include // the HIP compat layer: __dp4a, __nanosleep -#endif - -#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 610) - -// Software form for sm_60 and older: four chained PTX `vmad` instructions, one per byte lane. vmad with the .s32 types and a byte -// selector (.b0 .. .b3) sign-extends the selected bytes of both sources, multiplies, and adds the third operand modulo 2^32 (no .sat). -// Each lane is its own single-instruction asm statement, so an output register may share a register with any input without harm. -__device__ __forceinline__ int strata_dp4a(const int a, const int b, const int c) { - int r; - asm("vmad.s32.s32.s32 %0, %1.b0, %2.b0, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c)); - asm("vmad.s32.s32.s32 %0, %1.b1, %2.b1, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(r)); - asm("vmad.s32.s32.s32 %0, %1.b2, %2.b2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(r)); - asm("vmad.s32.s32.s32 %0, %1.b3, %2.b3, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(r)); - return r; -} -#define STRATA_DP4A(a, b, c) strata_dp4a((a), (b), (c)) - -#else - -#define STRATA_DP4A(a, b, c) __dp4a((a), (b), (c)) - -#endif - -// Yield the thread while a flag is polled. Below compute capability 7.0 there is no sleep instruction and a plain spin is correct. -__device__ __forceinline__ void strata_spin_pause() { -#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 700) - __nanosleep(100); -#endif -} diff --git a/engine/sycl/include/strata/kernels/dp4a.hpp b/engine/sycl/include/strata/kernels/dp4a.hpp index 7084bcb5d4..dda5842425 100644 --- a/engine/sycl/include/strata/kernels/dp4a.hpp +++ b/engine/sycl/include/strata/kernels/dp4a.hpp @@ -1,55 +1,36 @@ -// include/strata/kernels/dp4a.hpp - the two instructions the kernels use that Pascal (sm_60) does not have. +// STRATA_DP4A(a, b, c): the signed four-lane byte dot product with an int32 accumulator (SYCL spelling), plus strata_spin_pause(). // -// The engine is written for compute capability 7.5 and newer and refuses older cards at CMake configure time -// (a Pascal build passes -DSTRATA_EXPERIMENTAL_SM60=ON). A GP100-class card is compute capability 6.0, which is -// missing exactly two things the i-quant kernels rely on: +// STRATA_DP4A(a, b, c) == c + sum over k = 0..3 of (signed byte k of a) * (signed byte k of b), modulo 2^32. // -// * `__dp4a`, a byte-wise dot product, available from 6.1 (Pascal GP10x/GV11x). Its fallback below is -// llama.cpp's own (`ggml/src/ggml-cuda/common.cuh`), which is the reference for the kernels in this -// directory: they are transcribed from llama.cpp's vecdotq.cuh, and that wrapper's operands are the same -// sites. It reinterprets the operands as SIGNED bytes and accumulates in int32, which is what `__dp4a` -// does for these calls, so the fallback is bit-exact rather than merely close. -// * `__nanosleep`, available from 7.0 (Volta). It only paces single-thread doorbell waits, so a loop that -// spins without it is correct, merely busier. +// With dpct compatibility level 610 or higher it is dpct::dp4a; below that the same value is formed in registers (each operand byte is +// sign-extended, multiplied, and the four products are added to c). The operands are plain values, so a and b may be the same register. +// SYCL has no nanosleep: strata_spin_pause() is empty (every wait is bounded elsewhere by strata::kSpinMax). // -// Both are compile-time selections on `__CUDA_ARCH__`, so one source tree builds for Pascal and for RTX -// 20/30/40/50 without a #define at the call sites. A HIP build never takes either fallback: `__CUDA_ARCH__` is -// undefined there, so `STRATA_DP4A` is `__dp4a` (which `hip_compat/intrinsics.hpp` provides) and the pause is -// HIP's own `__nanosleep`. #pragma once #define DPCT_PROFILING_ENABLED #include #include + #include -#include -#if defined(STRATA_HIP_GFX906) -#include // gfx906: the compat layer (__forceinline__, __nanosleep, __dp4a) -#endif +#if defined(DPCT_COMPATIBILITY_TEMP) && (DPCT_COMPATIBILITY_TEMP < 610) -#if defined(DPCT_COMPATIBILITY_TEMP) && DPCT_COMPATIBILITY_TEMP < 610 -__device__ __forceinline__ int strata_dp4a(const int a, const int b, const int c) { - const int8_t* a8 = (const int8_t*) &a; - const int8_t* b8 = (const int8_t*) &b; - return c + a8[0] * b8[0] + a8[1] * b8[1] + a8[2] * b8[2] + a8[3] * b8[3]; +__dpct_inline__ int strata_dp4a(const int a, const int b, const int c) { + const uint32_t ua = (uint32_t)a, ub = (uint32_t)b; + uint32_t acc = (uint32_t)c; + acc += (uint32_t)((int)(int8_t)(ua) * (int)(int8_t)(ub)); + acc += (uint32_t)((int)(int8_t)(ua >> 8) * (int)(int8_t)(ub >> 8)); + acc += (uint32_t)((int)(int8_t)(ua >> 16) * (int)(int8_t)(ub >> 16)); + acc += (uint32_t)((int)(int8_t)(ua >> 24) * (int)(int8_t)(ub >> 24)); + return (int)acc; } #define STRATA_DP4A(a, b, c) strata_dp4a((a), (b), (c)) + #else + #define STRATA_DP4A(a, b, c) dpct::dp4a((a), (b), (c)) + #endif -/// Yield the thread while a doorbell flag is being polled. The length of the pause is a backoff hint, not a -/// contract, and only pre-Volta CUDA has no `__nanosleep` at all. -__dpct_inline__ void strata_spin_pause() { -#if defined(DPCT_COMPATIBILITY_TEMP) && DPCT_COMPATIBILITY_TEMP < 700 - // sm_6x: the loop spins. Every call site is a single-thread doorbell wait, so nothing else is delayed. -#else - /* - DPCT1008: __nanosleep function is not defined in SYCL. This is a - hardware-specific feature. Consult with your hardware vendor to find a - replacement. - */ - // SYCL port: no __nanosleep; the doorbell waits are bounded by strata::kSpinMax instead -#endif -} +__dpct_inline__ void strata_spin_pause() {} diff --git a/engine/sycl/include/strata/kernels/dp4a_clean.hpp b/engine/sycl/include/strata/kernels/dp4a_clean.hpp deleted file mode 100644 index d74c52f009..0000000000 --- a/engine/sycl/include/strata/kernels/dp4a_clean.hpp +++ /dev/null @@ -1,37 +0,0 @@ -// STRATA_DP4A(a, b, c): the signed four-lane byte dot product with an int32 accumulator (SYCL spelling), plus strata_spin_pause(). -// -// STRATA_DP4A(a, b, c) == c + sum over k = 0..3 of (signed byte k of a) * (signed byte k of b), modulo 2^32. -// -// With dpct compatibility level 610 or higher it is dpct::dp4a; below that the same value is formed in registers (each operand byte is -// sign-extended, multiplied, and the four products are added to c). The operands are plain values, so a and b may be the same register. -// SYCL has no nanosleep: strata_spin_pause() is empty (every wait is bounded elsewhere by strata::kSpinMax). -// -// This header replaces engine/sycl/include/strata/kernels/dp4a.hpp (integrator: see P2-RESULTS.md, packet P-1). -#pragma once - -#define DPCT_PROFILING_ENABLED -#include -#include - -#include - -#if defined(DPCT_COMPATIBILITY_TEMP) && (DPCT_COMPATIBILITY_TEMP < 610) - -__dpct_inline__ int strata_dp4a(const int a, const int b, const int c) { - const uint32_t ua = (uint32_t)a, ub = (uint32_t)b; - uint32_t acc = (uint32_t)c; - acc += (uint32_t)((int)(int8_t)(ua) * (int)(int8_t)(ub)); - acc += (uint32_t)((int)(int8_t)(ua >> 8) * (int)(int8_t)(ub >> 8)); - acc += (uint32_t)((int)(int8_t)(ua >> 16) * (int)(int8_t)(ub >> 16)); - acc += (uint32_t)((int)(int8_t)(ua >> 24) * (int)(int8_t)(ub >> 24)); - return (int)acc; -} -#define STRATA_DP4A(a, b, c) strata_dp4a((a), (b), (c)) - -#else - -#define STRATA_DP4A(a, b, c) dpct::dp4a((a), (b), (c)) - -#endif - -__dpct_inline__ void strata_spin_pause() {}