Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions engine/cmake/strata_poly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions engine/include/strata/kernels/iq_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions engine/src/kernels/cuda/i8_mma.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <atomic>
#include <cstdlib>
#include <mutex>
#include <string>
#include <type_traits>

#define ggml_cuda_dp4a(a, b, c) STRATA_DP4A((a), (b), (c))
Expand Down Expand Up @@ -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);
Expand Down
246 changes: 239 additions & 7 deletions engine/src/kernels/cuda/i8_mma.cuh

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions engine/src/kernels/cuda/iq_kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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*,
Expand Down
167 changes: 167 additions & 0 deletions engine/src/poly/i8m_dp4a_test.cu
Original file line number Diff line number Diff line change
@@ -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 <cuda_runtime.h>

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <random>
#include <vector>

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 <typename T> T* upload(const std::vector<T>& 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<int> 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<std::vector<int>> 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<float> normal(0.f, 1.f);
int bad = 0, tried = 0;
for (const int K : {2048, 1152}) {
std::vector<float> 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<float> 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<uint8_t> 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<int32_t> 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<int32_t> 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<float> 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;
}
4 changes: 4 additions & 0 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions terminal/data/settings-register.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Loading