diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index a2c0ac4be16..a91f2e78e75 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -311,6 +311,10 @@ if(STRATA_BUILD_TESTS) add_executable(kv_grow_rules_test tests/core/kv_grow_rules_test.cpp) target_include_directories(kv_grow_rules_test PRIVATE include) add_test(NAME kv_grow_rules_test COMMAND kv_grow_rules_test) + # #6681: which KV dtypes the fp16-only GPU pools must reject at init (the guard's truth table, no GPU) + add_executable(kv_dtype_guard_test tests/core/kv_dtype_guard_test.cpp) + target_include_directories(kv_dtype_guard_test PRIVATE src/inkling2) + add_test(NAME kv_dtype_guard_test COMMAND kv_dtype_guard_test) # how the prompt path's K/V append reaches the host copy under KV streaming: the switch both ways, the backend defaults, no pool (no GPU) add_executable(kv_host_dma_test tests/core/kv_host_dma_test.cpp) target_include_directories(kv_host_dma_test PRIVATE include) diff --git a/engine/src/inkling2/hip/ink_hip_backend.hip.cpp b/engine/src/inkling2/hip/ink_hip_backend.hip.cpp index ab0f57de1e9..35f0102b943 100644 --- a/engine/src/inkling2/hip/ink_hip_backend.hip.cpp +++ b/engine/src/inkling2/hip/ink_hip_backend.hip.cpp @@ -568,6 +568,11 @@ class HipBackend final : public InkBackend { err = "[ink-hip] no HIP devices visible"; return false; } + if (kv_dtype_is_int_quant(opts.kv_dtype)) { // #6681: the hip pool has no integer KV path, fp16-sized pages regardless + err = "[ink-hip] --kv k8v4/k8v8 is not implemented on the HIP backend (see #6681): the KV pool allocates " + "fp16-sized pages regardless of dtype. Use --kv fp8 (halves KV memory) or fp16."; + return false; + } gpu0_id_ = (!opts.gpu_ids.empty()) ? opts.gpu_ids[0] : opts.gpu_index; gpu0_id_ = std::max(0, std::min(gpu0_id_, dev_count - 1)); gpu1_id_ = -1; diff --git a/engine/src/inkling2/ink_batch.h b/engine/src/inkling2/ink_batch.h index a1b0080c065..08e9de16946 100644 --- a/engine/src/inkling2/ink_batch.h +++ b/engine/src/inkling2/ink_batch.h @@ -30,6 +30,15 @@ inline const char* kv_dtype_name(KvDtype dt) noexcept { } } +// #6681: the integer quantized formats (K8V8/K8V4, #6520) have real KV storage on the CPU (the pool init takes the +// dtype and the selfcheck verifies the round-trip) and on CUDA (its own InkKvDtype path). The HIP, Metal and SYCL +// pools have no integer path at all: they allocate fp16-sized pages and ignore the dtype in alloc_slot, so asking +// for k8v4/k8v8 there pays the quantize cost for fp16-class memory. A backend without the integer storage rejects +// these at init instead of silently mis-sizing the KV. +inline bool kv_dtype_is_int_quant(KvDtype dt) noexcept { + return dt == KvDtype::K8V8 || dt == KvDtype::K8V4; +} + // ---- Scalar FP16 / BF16 / FP8 E4M3FN conversion helpers (portable across CPU & GPU host prep) ---- inline float bf16_to_f32(uint16_t b) noexcept { uint32_t u = static_cast(b) << 16; diff --git a/engine/src/inkling2/ink_main.cpp b/engine/src/inkling2/ink_main.cpp index e9ab31b8f42..64ed7601222 100644 --- a/engine/src/inkling2/ink_main.cpp +++ b/engine/src/inkling2/ink_main.cpp @@ -417,7 +417,8 @@ int main(int argc, char** argv) { else if (kv == "fp16" || kv == "f16") { cfg.bopts.kv_dtype = KvDtype::FP16; cfg.bopts.kv_dtype_auto = false; } else if (kv == "k8v8" || kv == "i8" || kv == "q8") { cfg.bopts.kv_dtype = KvDtype::K8V8; cfg.bopts.kv_dtype_auto = false; } else if (kv == "k8v4") { cfg.bopts.kv_dtype = KvDtype::K8V4; cfg.bopts.kv_dtype_auto = false; } - else { cfg.bopts.kv_dtype_auto = true; } + else if (kv == "auto") { cfg.bopts.kv_dtype_auto = true; } + else { fprintf(stderr, "unknown --kv value '%s' (use: auto, fp8, fp16, k8v8, k8v4)\n", kv.c_str()); std::exit(2); } } else if (a == "--kv-offload") { cfg.bopts.kv_offload = next(); } else if (a == "--draft") { diff --git a/engine/src/inkling2/metal/ink_metal_backend.mm b/engine/src/inkling2/metal/ink_metal_backend.mm index a70fbe0516c..2317ec00740 100644 --- a/engine/src/inkling2/metal/ink_metal_backend.mm +++ b/engine/src/inkling2/metal/ink_metal_backend.mm @@ -90,6 +90,11 @@ BackendStats stats() const override { } bool init(const InkModelFile& model, const BackendOpts& opts, std::string& err) override { + if (kv_dtype_is_int_quant(opts.kv_dtype)) { // #6681: the metal pool ignores the dtype, fp16-sized pages + err = "metal: --kv k8v4/k8v8 is not implemented on the Metal backend (see #6681): the KV pool ignores " + "the dtype and allocates fp16-sized pages. Use --kv fp8 or fp16."; + return false; + } kv_dtype_ = opts.kv_dtype; const char* force_mf = std::getenv("INK_METAL_FORCE_MODEL_FILE"); if (model.kind == ModelSourceKind::Strata && !(force_mf && force_mf[0] == '1')) { diff --git a/engine/src/inkling2/sycl/ink_sycl_backend.cpp b/engine/src/inkling2/sycl/ink_sycl_backend.cpp index 5bf95f23785..8ff952c144f 100644 --- a/engine/src/inkling2/sycl/ink_sycl_backend.cpp +++ b/engine/src/inkling2/sycl/ink_sycl_backend.cpp @@ -63,6 +63,11 @@ class SyclInkBackend final : public InkBackend { } bool init(const InkModelFile& model, const BackendOpts& opts, std::string& err) override { + if (kv_dtype_is_int_quant(opts.kv_dtype)) { // #6681: the sycl pool ignores the dtype, fp16-sized pages + err = "sycl: --kv k8v4/k8v8 is not implemented on the SYCL backend (see #6681): the KV pool ignores the " + "dtype and allocates fp16-sized pages. Use --kv fp8 or fp16."; + return false; + } ink_sycl::set_primary_gpu_index(opts.gpu_index); kv_dtype_ = opts.kv_dtype; const int par = std::max(1, std::min(64, opts.parallel)); diff --git a/engine/tests/core/kv_dtype_guard_test.cpp b/engine/tests/core/kv_dtype_guard_test.cpp new file mode 100644 index 00000000000..295dd0a5764 --- /dev/null +++ b/engine/tests/core/kv_dtype_guard_test.cpp @@ -0,0 +1,28 @@ +// #6681: the truth table behind the hip/metal/sycl init guard. fp16 and fp8 every backend; the integer quantized +// formats (k8v8/k8v4) only where the pool really stores them (CPU, CUDA) — the fp16-only pools reject them at init +// instead of silently allocating fp16-sized pages. +#include "ink_batch.h" + +#include +#include + +using namespace ink2; + +namespace { +int failed = 0; +#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) +} // namespace + +int main() { + CHECK(!kv_dtype_is_int_quant(KvDtype::FP16)); + CHECK(!kv_dtype_is_int_quant(KvDtype::FP8_E4M3)); + CHECK(kv_dtype_is_int_quant(KvDtype::K8V8)); + CHECK(kv_dtype_is_int_quant(KvDtype::K8V4)); + // the names the reject message and the bench line print (the strings the user sees after #6681's guard fires) + CHECK(std::string(kv_dtype_name(KvDtype::K8V8)) == "k8v8"); + CHECK(std::string(kv_dtype_name(KvDtype::K8V4)) == "k8v4"); + CHECK(std::string(kv_dtype_name(KvDtype::FP8_E4M3)) == "fp8"); + if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; } + std::printf("ok\n"); + return 0; +}