diff --git a/engine/cmake/strata_poly.cmake b/engine/cmake/strata_poly.cmake index 11a4ab043..2910f89dd 100644 --- a/engine/cmake/strata_poly.cmake +++ b/engine/cmake/strata_poly.cmake @@ -400,6 +400,15 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA set_tests_properties(poly_mmq_paths_test_blas PROPERTIES ENVIRONMENT "STRATA_POLY_PROMPT_BLAS=1") set_tests_properties(poly_mmq_paths_test_x2 PROPERTIES ENVIRONMENT "STRATA_PREFILL_NVFP4=w4a4x2") set_tests_properties(poly_mmq_paths_test_w4a8 PROPERTIES ENVIRONMENT "STRATA_PREFILL_NVFP4=w4a8") + # the int8 prompt kernel by dp4a (Turing's code on a GTX 16) against its MMAs, bit for bit; needs sm_75 code (77 without) + if(NOT STRATA_POLY_MMQ) + add_executable(poly_i8m_dp4a_test src/poly/i8m_dp4a_test.cu) + target_include_directories(poly_i8m_dp4a_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories} + ${_strata_ggml_root}/ggml/include) + target_link_libraries(poly_i8m_dp4a_test PRIVATE strata_poly_mmq ggml-base ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target}) + add_test(NAME poly_i8m_dp4a_test COMMAND poly_i8m_dp4a_test) + set_tests_properties(poly_i8m_dp4a_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900) + endif() endif() # LFM2's short convolution against the same arithmetic on the host; needs a card, so it is run by hand add_executable(poly_shortconv_test src/poly/shortconv_test.cu src/poly/kernels.cu) diff --git a/engine/include/strata/kernels/iq_kernels.hpp b/engine/include/strata/kernels/iq_kernels.hpp index dad973cfc..c87b63c79 100644 --- a/engine/include/strata/kernels/iq_kernels.hpp +++ b/engine/include/strata/kernels/iq_kernels.hpp @@ -215,6 +215,13 @@ bool gemm_iq_i8_takes(int ty, int K); /// Off by default, where Turing's products are the halves' (mmq_own.cu); a model's engine turns it on (Inkling 2 /// with --prompt-int8). void gemm_iq_i8_turing(bool on); +/// Turing's code (sm_75) on a card without tensor cores (GeForce GTX 16) runs the int8 kernel's products by dp4a in +/// place of the MMAs, which are slow there: the same int32 sums and float sums, so the same bits (i8_mma.cuh, +/// i8m_dp4a_pick: the card's name, then a probe of the two). mode -1: that pick (the default); 0: the MMAs; 1: dp4a on +/// any Turing code (a test of the bits on a card with tensor cores). gemm_iq_i8_dp4a_on: the current card takes the +/// int8 kernel and runs it by dp4a. +void gemm_iq_i8_dp4a(int mode); +bool gemm_iq_i8_dp4a_on(); /// The int8 kernel also takes type ty, from now on in this process (on false: no longer), for a type it takes only when /// asked: IQ1_S (19) and MXFP4 (39). A model's engine asks for them at load (Inkling 2: IQ1_S with /// STRATA_PREFILL_IQ1S=int8, MXFP4 unless STRATA_PREFILL_MXFP4=fp16), so that other products stay on the FP16 kernel diff --git a/engine/src/kernels/cuda/i8_mma.cu b/engine/src/kernels/cuda/i8_mma.cu index 2199f268d..46ab46ed3 100644 --- a/engine/src/kernels/cuda/i8_mma.cu +++ b/engine/src/kernels/cuda/i8_mma.cu @@ -19,6 +19,7 @@ #include #include #include +#include #include #define ggml_cuda_dp4a(a, b, c) STRATA_DP4A((a), (b), (c)) @@ -49,6 +50,11 @@ bool gemm_iq_i8_takes(int ty, int K) { } void gemm_iq_i8_turing(bool on) { i8m_turing.store(on, std::memory_order_relaxed); } +void gemm_iq_i8_dp4a(int mode) { i8m_dp4a_mode.store(mode < 0 ? -1 : mode > 0 ? 1 : 0, std::memory_order_relaxed); } +bool gemm_iq_i8_dp4a_on() { + const I8mCard c = i8m_card(); + return c.on && c.dp4a && c.arch == 750; +} void gemm_iq_i8_allow(int ty, bool on) { if (!i8m_asked_type(ty)) return; if (on) i8m_allowed.fetch_or(uint64_t{1} << ty, std::memory_order_relaxed); diff --git a/engine/src/kernels/cuda/i8_mma.cuh b/engine/src/kernels/cuda/i8_mma.cuh index e3f18b060..35f4f9251 100644 --- a/engine/src/kernels/cuda/i8_mma.cuh +++ b/engine/src/kernels/cuda/i8_mma.cuh @@ -118,6 +118,36 @@ __device__ __forceinline__ void i8m_mma16(int (&c)[4], uint32_t a0, uint32_t a1, i8m_mma8(c[2], c[3], a1, b0); #endif } +// A Turing card without tensor cores (GTX 16: TU116, TU117) runs mma.sync's int8 MMAs slowly; it has dp4a +// (i8m_card().dp4a). There an MMA's four sums are made by dp4a, read out of the same shared memory: c[0] / c[1] are +// weight row g (wa) times activation rows 2 tg / 2 tg + 1 (x0, x1), c[2] / c[3] weight row g + 8 (wb), over N words +// (4 values each) from word k. Integer sums of the same products: the same int32 as the MMA's, to the bit. (The +// idea of int8 tiles in shared memory multiplied by dp4a where there are no int8 tensor cores is llama.cpp's MMQ.) +template +__device__ __forceinline__ void i8m_dot(int (&c)[4], const uint32_t (&wa)[N], const uint32_t (&wb)[N], const uint32_t* x0, + const uint32_t* x1) { +#pragma unroll + for (int q = 0; q < N; q += 4) { + const uint4 u = *(const uint4*) (x0 + q), v = *(const uint4*) (x1 + q); + const uint32_t uu[4] = {u.x, u.y, u.z, u.w}, vv[4] = {v.x, v.y, v.z, v.w}; +#pragma unroll + for (int k = 0; k < 4; ++k) { + c[0] = __dp4a((int) wa[q + k], (int) uu[k], c[0]); + c[1] = __dp4a((int) wa[q + k], (int) vv[k], c[1]); + c[2] = __dp4a((int) wb[q + k], (int) uu[k], c[2]); + c[3] = __dp4a((int) wb[q + k], (int) vv[k], c[3]); + } + } +} +// N words of shared memory from p (16-byte aligned), into registers +template +__device__ __forceinline__ void i8m_lds(uint32_t (&r)[N], const uint32_t* p) { +#pragma unroll + for (int q = 0; q < N; q += 4) { + const uint4 u = *(const uint4*) (p + q); + r[q] = u.x; r[q + 1] = u.y; r[q + 2] = u.z; r[q + 3] = u.w; + } +} // Four 8 x 16-byte pieces of shared memory, a lane's 16 bytes at p (lanes 8 m to 8 m + 7: the rows of piece m): word // (lane & 3) of row (lane >> 2) of each piece, which is an MMA's operand as the tensor cores take it. One instruction // for what took four 32-bit reads. @@ -410,7 +440,7 @@ __global__ void __launch_bounds__(I8M_NT) i8m_quantize4_kernel(const float* __re } } -template +template __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8_t* __restrict__ W, size_t mat_bytes, size_t row_bytes, int n_out, int K, const uint8_t* __restrict__ X, const float* __restrict__ XD, @@ -418,6 +448,9 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 const int32_t* __restrict__ ids, const int32_t* __restrict__ from, const long long* __restrict__ at, float* __restrict__ Y, long long ldy) { #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750 // (a pass for an older card compiles this empty: the host asks i8m_arch_kernel first) +#if __CUDA_ARCH__ != 750 + if constexpr (DP) return; // DP: only Turing's code runs it (i8m_launch_type) +#endif using SM = I8mSmem; constexpr int MT = SM::MT, LDS = SM::LDS, LDB = SM::LDB, XQ = SM::XQ, XSTR = SM::XSTR; // NT threads. A warp multiplies NA tiles of 16 weight rows (two where the activation rows are many: each piece of @@ -787,6 +820,123 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 } }; + // DP (i8m_card().dp4a): the same products by dp4a out of the same shared memory (i8m_dot), and the same float + // sums in the same order (each sum's terms by block, then by half of it), so the same bits as multiply()'s. A + // thread's weight rows are read once for all its activation tiles, its activation rows once for its weight tiles. + const uint32_t* const wa_row = s_w + (i0 + g) * I8M_LD; // weight row i0 + g (+ 16 a, + 8) + const uint32_t* const xb_row = s_x + (8 * jp + 2 * tg) * I8M_LD; // activation row j0 + 2 tg (+ I8M_LD: the next) + const auto multiply_dp = [&]() { + if constexpr (TWO) { +#pragma unroll + for (int kq = 0; kq < 16; kq += 4) { + float w0[4][NA], w1[4][NA], m0[4][NA], m1[4][NA]; +#pragma unroll + for (int a = 0; a < NA; ++a) +#pragma unroll + for (int h = 0; h < 4; ++h) { + w0[h][a] = sa[16 * a * LDS + kq + h]; + w1[h][a] = sa[(16 * a + 8) * LDS + kq + h]; + m0[h][a] = m1[h][a] = 0.0f; + if constexpr (MIN16) { + m0[h][a] = s_m16[(i0 + 16 * a + g) * LDM + kq + h]; + m1[h][a] = s_m16[(i0 + 16 * a + g + 8) * LDM + kq + h]; + } + } +#pragma unroll + for (int h = 0; h < 4; ++h) { + uint32_t WA[NA][4], WB[NA][4]; +#pragma unroll + for (int a = 0; a < NA; ++a) { + i8m_lds<4>(WA[a], wa_row + 16 * a * I8M_LD + (kq + h) * 4); + i8m_lds<4>(WB[a], wa_row + (16 * a + 8) * I8M_LD + (kq + h) * 4); + } +#pragma unroll + for (int n = 0; n < NJW; ++n) { + const int j0 = 8 * (CG * n + jp); + if (j0 >= ntok) break; + const uint32_t* const xr = xb_row + 8 * CG * n * I8M_LD + (kq + h) * 4; + float xa, xb, s0, s1; + scales((kq + h) >> 1, j0, xa, xb, s0, s1); + float2 sm16 = make_float2(0.0f, 0.0f); + if constexpr (MIN16) sm16 = *(const float2*) (s_x16 + (kq + h) * S16 + j0 + 2 * tg); +#pragma unroll + for (int a = 0; a < NA; ++a) { + int c[4] = {0, 0, 0, 0}; + i8m_dot<4>(c, WA[a], WB[a], xr, xr + I8M_LD); + acc[a][n][0] = fmaf((float) c[0], w0[h][a] * xa, acc[a][n][0]); + acc[a][n][1] = fmaf((float) c[1], w0[h][a] * xb, acc[a][n][1]); + acc[a][n][2] = fmaf((float) c[2], w1[h][a] * xa, acc[a][n][2]); + acc[a][n][3] = fmaf((float) c[3], w1[h][a] * xb, acc[a][n][3]); + if constexpr (MIN16) { + acc[a][n][0] = fmaf(m0[h][a], sm16.x, acc[a][n][0]); + acc[a][n][1] = fmaf(m0[h][a], sm16.y, acc[a][n][1]); + acc[a][n][2] = fmaf(m1[h][a], sm16.x, acc[a][n][2]); + acc[a][n][3] = fmaf(m1[h][a], sm16.y, acc[a][n][3]); + } + } + } + } + } + } else { +#pragma unroll + for (int kb = 0; kb < 8; kb += 2) { + float w0[2][NA], w1[2][NA], m0[2][NA], m1[2][NA]; +#pragma unroll + for (int a = 0; a < NA; ++a) { + if constexpr (SM::PK) { + const float4 v0 = *(const float4*) (sa + 16 * a * LDS + 2 * kb); + const float4 v1 = *(const float4*) (sa + (16 * a + 8) * LDS + 2 * kb); + w0[0][a] = v0.x; m0[0][a] = v0.y; w0[1][a] = v0.z; m0[1][a] = v0.w; + w1[0][a] = v1.x; m1[0][a] = v1.y; w1[1][a] = v1.z; m1[1][a] = v1.w; + } else { +#pragma unroll + for (int h = 0; h < 2; ++h) { + m0[h][a] = m1[h][a] = 0.0f; + w0[h][a] = sa[16 * a * LDS + kb + h]; + w1[h][a] = sa[(16 * a + 8) * LDS + kb + h]; + if constexpr (HAS_MIN) { + m0[h][a] = sm[16 * a * LDB + kb + h]; + m1[h][a] = sm[(16 * a + 8) * LDB + kb + h]; + } + } + } + } +#pragma unroll + for (int h = 0; h < 2; ++h) { + uint32_t WA[NA][8], WB[NA][8]; +#pragma unroll + for (int a = 0; a < NA; ++a) { + i8m_lds<8>(WA[a], wa_row + 16 * a * I8M_LD + (kb + h) * 8); + i8m_lds<8>(WB[a], wa_row + (16 * a + 8) * I8M_LD + (kb + h) * 8); + } +#pragma unroll + for (int n = 0; n < NJW; ++n) { + const int j0 = 8 * (CG * n + jp); + if (j0 >= ntok) break; + const uint32_t* const xr = xb_row + 8 * CG * n * I8M_LD + (kb + h) * 8; + float x0, x1, s0, s1; + scales(kb + h, j0, x0, x1, s0, s1); +#pragma unroll + for (int a = 0; a < NA; ++a) { + int c[4] = {0, 0, 0, 0}; + i8m_dot<8>(c, WA[a], WB[a], xr, xr + I8M_LD); + acc[a][n][0] = fmaf((float) c[0], w0[h][a] * x0, acc[a][n][0]); + acc[a][n][1] = fmaf((float) c[1], w0[h][a] * x1, acc[a][n][1]); + acc[a][n][2] = fmaf((float) c[2], w1[h][a] * x0, acc[a][n][2]); + acc[a][n][3] = fmaf((float) c[3], w1[h][a] * x1, acc[a][n][3]); + if constexpr (HAS_MIN) { + acc[a][n][0] = fmaf(m0[h][a], s0, acc[a][n][0]); + acc[a][n][1] = fmaf(m0[h][a], s1, acc[a][n][1]); + acc[a][n][2] = fmaf(m1[h][a], s0, acc[a][n][2]); + acc[a][n][3] = fmaf(m1[h][a], s1, acc[a][n][3]); + } + } + } + } + } + } + }; + const int stages = (K + 255) / 256; fetch(0); for (int S = 0; S < stages; ++S) { @@ -795,7 +945,8 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 put(S); __syncthreads(); if (S + 1 < stages) fetch(S + 1); // read while this stage is multiplied - multiply(); + if constexpr (DP) multiply_dp(); + else multiply(); } // a thread holds weight rows i0 + 16 a + g and 8 more for activation rows j0 + 2 tg and j0 + 2 tg + 1 @@ -830,12 +981,88 @@ __global__ void i8m_arch_kernel(int* out) { struct I8mCard { bool read = false, on = false; + bool dp4a = false; // the products by dp4a (multiply_dp): Turing's code on a card without tensor cores int arch = 0; // the architecture of the code it runs (i8m_arch_kernel) size_t shared = 0, shared_sm = 0; // a block's most, a multiprocessor's }; // gemm_iq_i8_turing(): the kernel also on Turing's code (sm_75), whose products a model asks for std::atomic i8m_turing{false}; +// gemm_iq_i8_dp4a(): -1 the card's pick (i8m_dp4a_pick), 0 the MMAs, 1 dp4a wherever Turing's code runs +std::atomic i8m_dp4a_mode{-1}; + +// The probe of i8m_dp4a_pick: a warp's int8 multiply-adds by m8n8k16 MMAs (MMA) or by dp4a, as much work either way +// (an MMA is 8 x 8 x 16 = 1,024 of them, 32 a lane: 8 dp4a), in 4 independent chains, `iters` times. +template +__global__ void i8m_probe_kernel(int iters, int* out) { + int c[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + uint32_t a = 0x01020304u + threadIdx.x, b = 0x05060708u ^ (uint32_t) blockIdx.x; + for (int i = 0; i < iters; ++i) { + if constexpr (MMA) { +#pragma unroll + for (int k = 0; k < 4; ++k) i8m_mma8(c[2 * k], c[2 * k + 1], a + k, b); + } else { +#pragma unroll + for (int k = 0; k < 4; ++k) +#pragma unroll + for (int j = 0; j < 8; ++j) c[2 * k + (j & 1)] = __dp4a((int) (a + k), (int) (b + j), c[2 * k + (j & 1)]); + } + a = a * 1664525u + 1013904223u; + } + int t = 0; +#pragma unroll + for (int k = 0; k < 8; ++k) t += c[k]; + if (t == 0x7fffffff) out[0] = t; // (never: it keeps the sums) +} + +// dp4a in place of the MMAs on this card? Only Turing's code (sm_75), only a GeForce GTX 16 (TU116 / TU117: no tensor +// cores, where mma.sync runs slowly) and never an RTX, Quadro, Tesla or TITAN card (T4, Quadro RTX: tensor cores); +// then a probe times the MMAs against dp4a for the same work and takes dp4a only where the MMAs are not faster (a +// GTX 1660 SUPER's MMAs took 10 times as long as its dp4a), or, where the probe cannot run, the name alone. +// STRATA_I8M_DP4A=1: dp4a on any Turing code (a test of the bits on a card with tensor cores), 0: never; +// STRATA_I8M_DP4A_LOG=1 prints the probe (run on any Turing card then, the pick unchanged). Once per card and +// process: a few milliseconds. +bool i8m_dp4a_pick(int dev, int arch) { + if (arch != 750) return false; + static const int env = [] { + const char* v = std::getenv("STRATA_I8M_DP4A"); + return v == nullptr || v[0] == 0 ? -1 : v[0] == '1' ? 1 : 0; + }(); + if (env >= 0) return env == 1; + cudaDeviceProp p{}; + if (cudaGetDeviceProperties(&p, dev) != cudaSuccess) return false; + const std::string name = p.name; + const bool gtx16 = name.find("GTX 16") != std::string::npos && name.find("RTX") == std::string::npos && + name.find("Quadro") == std::string::npos && name.find("Tesla") == std::string::npos && + name.find("TITAN") == std::string::npos; + static const bool log = std::getenv("STRATA_I8M_DP4A_LOG") != nullptr; // (and the probe on any Turing card) + if (!gtx16 && !log) return false; + float ms[2] = {0.0f, 0.0f}; + bool timed = false; + int* d = nullptr; + cudaEvent_t e0 = nullptr, e1 = nullptr; + if (cudaMalloc(&d, sizeof(int)) == cudaSuccess && cudaEventCreate(&e0) == cudaSuccess && cudaEventCreate(&e1) == cudaSuccess) { + const int blocks = 4 * (p.multiProcessorCount > 0 ? p.multiProcessorCount : 1), iters = 512; + timed = true; + for (int rep = 0; rep < 2 && timed; ++rep) // (the first round warms the card up) + for (int k = 0; k < 2 && timed; ++k) { + timed = cudaEventRecord(e0) == cudaSuccess; + if (k == 0) i8m_probe_kernel<<>>(iters, d); + else i8m_probe_kernel<<>>(iters, d); + timed = timed && cudaGetLastError() == cudaSuccess && cudaEventRecord(e1) == cudaSuccess && + cudaEventSynchronize(e1) == cudaSuccess && cudaEventElapsedTime(&ms[k], e0, e1) == cudaSuccess; + } + } + if (e0) cudaEventDestroy(e0); + if (e1) cudaEventDestroy(e1); + if (d) cudaFree(d); + (void) cudaGetLastError(); + const bool pick = gtx16 && (timed && ms[1] > 0.0f ? ms[0] >= ms[1] : true); + if (log) + std::fprintf(stderr, "i8m: %s, MMAs %.3f ms, dp4a %.3f ms%s: %s\n", p.name, ms[0], ms[1], timed ? "" : " (not timed)", + pick ? "dp4a" : "MMAs"); + return pick; +} // the current card: int8 tensor cores in the code it runs (Ampere's, or Turing's when asked), and the shared memory // it gives a block @@ -859,25 +1086,27 @@ I8mCard i8m_card() { } (void) cudaGetLastError(); c.arch = arch; + c.dp4a = i8m_dp4a_pick(dev, arch); known[dev] = c; } I8mCard c = known[dev]; c.on = c.arch >= 800 || (c.arch >= 750 && i8m_turing.load(std::memory_order_relaxed)); + if (const int mode = i8m_dp4a_mode.load(std::memory_order_relaxed); mode >= 0) c.dp4a = mode == 1 && c.arch == 750; return c; } -template +template bool i8m_launch(int dev, dim3 grid, cudaStream_t s, const uint8_t* W, size_t mat_bytes, size_t row_bytes, int n_out, int K, const uint8_t* X, const float* XD, const float* XS, const int32_t* bounds, int ch, const int32_t* ids, const int32_t* from, const long long* at, float* Y, long long ldy) { constexpr size_t bytes = I8mSmem::bytes; static bool attr[64]; // the attribute belongs to a card (see gemm_iq_f16_grouped) if (!attr[dev]) { - cudaFuncSetAttribute(i8m_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) bytes); + cudaFuncSetAttribute(i8m_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) bytes); attr[dev] = true; } - i8m_kernel<<>>(W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, ch, - ids, from, at, Y, ldy); + i8m_kernel<<>>(W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, + ch, ids, from, at, Y, ldy); return cudaGetLastError() == cudaSuccess; } @@ -918,7 +1147,10 @@ bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t const dim3 grid((unsigned) ((n_out + wr - 1) / wr), (unsigned) gy); #define STRATA_I8M_GO(NJ_, WR_) \ if (nj == NJ_ && wr == WR_) \ - return raw ? i8m_launch(dev, grid, s, W, mat_bytes, row_bytes, n_out, K, X, XD, XS, \ + return card.dp4a && card.arch == 750 \ + ? i8m_launch(dev, grid, s, W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, (int) ch, \ + ids, from, at, Y, ldy) \ + : raw ? i8m_launch(dev, grid, s, W, mat_bytes, row_bytes, n_out, K, X, XD, XS, \ bounds, (int) ch, ids, from, at, Y, ldy) \ : i8m_launch(dev, grid, s, W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, (int) ch, \ ids, from, at, Y, ldy); diff --git a/engine/src/kernels/cuda/iq_kernels.cu b/engine/src/kernels/cuda/iq_kernels.cu index 390fdde8d..44535046b 100644 --- a/engine/src/kernels/cuda/iq_kernels.cu +++ b/engine/src/kernels/cuda/iq_kernels.cu @@ -8465,6 +8465,8 @@ bool gemm_iq_f16_grouped(int ty, const void* W, size_t expert_bytes, int n_out, namespace strata::kernels { bool gemm_iq_i8_takes(int, int) { return false; } void gemm_iq_i8_turing(bool) {} +void gemm_iq_i8_dp4a(int) {} +bool gemm_iq_i8_dp4a_on() { return false; } void gemm_iq_i8_allow(int, bool) {} void gemm_iq_i8_quantize(const float*, const int32_t*, int64_t, int64_t, int, void*, void*) {} bool gemm_iq_i8_grouped(int, const void*, size_t, int, int, const void*, int64_t, const int32_t*, int, int, const int32_t*, float*, diff --git a/engine/src/poly/i8m_dp4a_test.cu b/engine/src/poly/i8m_dp4a_test.cu new file mode 100644 index 000000000..72f77cebf --- /dev/null +++ b/engine/src/poly/i8m_dp4a_test.cu @@ -0,0 +1,167 @@ +// src/poly/i8m_dp4a_test.cu - the int8 prompt kernel's products by dp4a (gemm_iq_i8_dp4a(1), i8_mma.cuh's multiply_dp: +// Turing's code on a GeForce GTX 16, which has no tensor cores) against the same kernel's MMAs, bit for bit, for every +// type the kernel takes, with rows of 2,048 values and of 1,152 (Inkling 2 Mini's experts' downs), each tile width +// (16 to 128 activation rows a block: launches of up to 10, 30, 60 and 300 rows a matrix), matrices of 200 outputs (a +// last tile of 8 rows) and 512 (IQ3_XXS's 128-row tile), experts with uneven and empty row ranges, products scattered +// to other rows. Each way is also timed (microseconds a launch, the card's warm best of 10). The weights are ggml's +// quantization of random values; the activations normal values with a few channels 30 times larger. +// +// usage: poly_i8m_dp4a_test (exit 0: every product the same bits both ways; 1: one differs; 77: no card, or no +// Turing code on it: dp4a is taken only there.) STRATA_I8M_DP4A_LOG=1 prints the card's own pick's probe. +#include "ggml.h" +#include "strata/kernels/iq_kernels.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace kn = strata::kernels; + +namespace { + +bool ok(cudaError_t e, const char* what) { + if (e != cudaSuccess) std::printf("%s: %s\n", what, cudaGetErrorString(e)); + return e == cudaSuccess; +} + +template T* upload(const std::vector& v) { + T* d = nullptr; + if (cudaMalloc(&d, v.size() * sizeof(T)) != cudaSuccess) return nullptr; + cudaMemcpy(d, v.data(), v.size() * sizeof(T), cudaMemcpyHostToDevice); + return d; +} + +} // namespace + +int main() { + int cards = 0; + if (cudaGetDeviceCount(&cards) != cudaSuccess || cards == 0) { + std::puts("poly_i8m_dp4a_test: no card, skipped"); + return 77; + } + cudaDeviceProp prop{}; + cudaGetDeviceProperties(&prop, 0); + kn::gemm_iq_i8_turing(true); + kn::gemm_iq_i8_allow(19, true); + kn::gemm_iq_i8_allow(39, true); + kn::gemm_iq_i8_dp4a(-1); + const bool pick = kn::gemm_iq_i8_dp4a_on(); + kn::gemm_iq_i8_dp4a(1); + if (!kn::gemm_iq_i8_dp4a_on()) { + std::printf("poly_i8m_dp4a_test: %s runs no Turing code (sm_75), skipped\n", prop.name); + return 77; + } + std::printf("%s (sm_%d%d, %d SMs): the card's own pick is %s\n", prop.name, prop.major, prop.minor, prop.multiProcessorCount, + pick ? "dp4a" : "the MMAs"); + const std::vector types = {2, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 29, 39, 40, 42}; + // launches: each matrix's rows (the bounds' steps); the largest says the tile width + const std::vector> launches = {{10, 0, 3, 9}, {30, 17, 0, 25}, {60, 33, 1, 47}, {300, 0, 7, 23, 129}}; + constexpr int kTok = 512; + std::mt19937 rng(7); + std::normal_distribution normal(0.f, 1.f); + int bad = 0, tried = 0; + for (const int K : {2048, 1152}) { + std::vector x((size_t) kTok * K), ch((size_t) K, 1.f); + for (int c = 0; c < K; c += 97) ch[(size_t) c] = 30.f; + for (int r = 0; r < kTok; ++r) { + const float s = std::exp(normal(rng)); + for (int c = 0; c < K; ++c) x[(size_t) r * K + c] = normal(rng) * s * ch[(size_t) c]; + } + float* dx = upload(x); + void* xq = nullptr; + if (!dx || !ok(cudaMalloc(&xq, (size_t) kTok * K * 5 / 4 + 64), "cudaMalloc")) return 1; + kn::gemm_iq_i8_quantize(dx, nullptr, K, kTok, K, xq, nullptr); + for (const int n_out : {200, 512}) { + for (int t : types) { + if (!kn::gemm_iq_i8_takes(t, K)) continue; + const auto ty = (ggml_type) t; + if (K % ggml_blck_size(ty) != 0) continue; + const int n_mats = 5; + ggml_quantize_init(ty); + const size_t row = ggml_row_size(ty, K), mat = row * (size_t) n_out; + std::vector wf((size_t) n_mats * n_out * K), imp((size_t) K); + for (auto& v : wf) v = normal(rng) * 0.02f; + for (auto& v : imp) v = 0.5f + std::fabs(normal(rng)); + std::vector wq(mat * n_mats + 64); + ggml_quantize_chunk(ty, wf.data(), wq.data(), 0, (int64_t) n_mats * n_out, K, imp.data()); + uint8_t* dw = upload(wq); + if (!dw) return 1; + for (const auto& l : launches) { + std::vector bounds(1, 0); + int max_rows = 0; + for (int r : l) { + bounds.push_back(bounds.back() + r); + max_rows = std::max(max_rows, r); + } + const int rows = bounds.back(), nm = (int) l.size(); + std::vector ids((size_t) rows); + for (int i = 0; i < rows; ++i) ids[(size_t) i] = (int32_t) ((i * 37 + 11) % rows); + int32_t* db = upload(bounds); + int32_t* di = upload(ids); + float* dy = nullptr; + if (!db || !di || !ok(cudaMalloc(&dy, (size_t) rows * n_out * sizeof(float)), "cudaMalloc")) return 1; + std::vector y[2]; + float us[2] = {0, 0}; + bool ran = true; + for (int way = 0; way < 2 && ran; ++way) { + kn::gemm_iq_i8_dp4a(way); + cudaMemset(dy, 0xFF, (size_t) rows * n_out * sizeof(float)); + ran = kn::gemm_iq_i8_grouped(t, dw, mat, n_out, K, xq, kTok, db, nm, max_rows, di, dy, n_out, nullptr, + nullptr, nullptr); + if (!ran || !ok(cudaDeviceSynchronize(), "product")) { ran = false; break; } + y[way].resize((size_t) rows * n_out); + cudaMemcpy(y[way].data(), dy, y[way].size() * sizeof(float), cudaMemcpyDeviceToHost); + cudaEvent_t e0, e1; + cudaEventCreate(&e0); + cudaEventCreate(&e1); + float best = 1e30f; + for (int rep = 0; rep < 10; ++rep) { + cudaEventRecord(e0); + kn::gemm_iq_i8_grouped(t, dw, mat, n_out, K, xq, kTok, db, nm, max_rows, di, dy, n_out, nullptr, nullptr, + nullptr); + cudaEventRecord(e1); + cudaEventSynchronize(e1); + float ms = 0; + cudaEventElapsedTime(&ms, e0, e1); + best = std::min(best, ms); + } + cudaEventDestroy(e0); + cudaEventDestroy(e1); + us[way] = best * 1000.f; + } + if (ran) { + ++tried; + size_t diff = 0, nonfinite = 0; + for (size_t i = 0; i < y[0].size(); ++i) { + if (std::memcmp(&y[0][i], &y[1][i], 4) != 0) ++diff; + if (!std::isfinite(y[0][i])) ++nonfinite; + } + if (diff) ++bad; + std::printf("%-8s K %4d out %3d rows %3d (max %3d): %s MMAs %8.1f us dp4a %8.1f us (x%.2f)%s\n", + ggml_type_name(ty), K, n_out, rows, max_rows, diff ? "DIFFERENT" : "same bits", us[0], us[1], + us[1] > 0 ? us[0] / us[1] : 0.0, nonfinite ? " (non-finite values!)" : ""); + if (diff) std::printf(" %zu of %zu values differ\n", diff, y[0].size()); + if (nonfinite) ++bad; + } else { + std::printf("%-8s K %4d out %3d rows %3d: not launched\n", ggml_type_name(ty), K, n_out, rows); + } + cudaFree(db); + cudaFree(di); + cudaFree(dy); + } + cudaFree(dw); + } + } + cudaFree(dx); + cudaFree(xq); + } + kn::gemm_iq_i8_dp4a(-1); + std::printf("%d of %d products differ\n", bad, tried); + return bad || tried == 0 ? 1 : 0; +} diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index c20d37404..eb57818c5 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -231,6 +231,10 @@ void Inkling2::load_geometry() { // the 97.8% a default change needs, so the halves and cuBLAS stay Turing's default (STRATA_PF_I8MMA=0 turns the // kernel off on every card) kernels::gemm_iq_i8_turing(opt_.prompt_int8 == 1); + // A GeForce GTX 16 (Turing without tensor cores) runs those int8 products by dp4a, the same bits as the MMAs, + // which are slow there (gemm_iq_i8_dp4a(): the card's name and a probe; STRATA_I8M_DP4A=0 keeps the MMAs) + if (opt_.prompt_int8 == 1 && kernels::gemm_iq_i8_dp4a_on()) + std::fprintf(stderr, "strata-poly: --prompt-int8's products by dp4a (a card without tensor cores)\n"); // STRATA_PREFILL_IQ1S=int8: IQ1_S (UD-Q2_K_XL's gate|up in 6 layers) on the int8 prompt kernel, as the file's // other types are on Ampere and newer (i8_mma.cuh: activations int8 a 32 values). Opt-in: on a 302-token prompt // it cost more KL a matrix than those types do. Unset or =fp16: the FP16 kernel, as before. Other models never diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a170..7e1d0e4d4 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -15728,6 +15728,72 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_I8M_DP4A", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/kernels/cuda/i8_mma.cuh", + "line": 1028, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "04af1c38db", + "first_date": "2026-11-24", + "since": null, + "last_commit": "04af1c38db", + "last_date": "2026-11-24", + "status": "rollback", + "owner": "", + "note": "Inkling 2 --prompt-int8 on Turing code (sm_75): unset picks dp4a on a GeForce GTX 16 (no tensor cores) where a probe finds the MMAs not faster; 0 never takes dp4a. 1 takes it on any Turing card, as a test of its bits (test only). Same answers.", + "retire_after": "", + "retired_in": "" + }, + { + "name": "STRATA_I8M_DP4A_LOG", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/kernels/cuda/i8_mma.cuh", + "line": 1038, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "off", + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "04af1c38db", + "first_date": "2026-11-24", + "since": null, + "last_commit": "04af1c38db", + "last_date": "2026-11-24", + "status": "research", + "owner": "", + "note": "test only: 1 prints the probe that times the MMAs against dp4a (run it on any Turing card; the pick is unchanged).", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_I8M_RAW", "backends": [