Skip to content
Closed
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
4 changes: 4 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions engine/src/inkling2/hip/ink_hip_backend.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions engine/src/inkling2/ink_batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(b) << 16;
Expand Down
3 changes: 2 additions & 1 deletion engine/src/inkling2/ink_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
5 changes: 5 additions & 0 deletions engine/src/inkling2/metal/ink_metal_backend.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
5 changes: 5 additions & 0 deletions engine/src/inkling2/sycl/ink_sycl_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
28 changes: 28 additions & 0 deletions engine/tests/core/kv_dtype_guard_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <string>

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;
}
Loading