fix(inkling2/cuda): a start-up allocation that does not fit gets a smaller workspace and a clear message, not "CUDA error 2 at ink_cuda.cuh:54" (#6644 #6792)

After the experts are placed, the runner, the K/V and the DSpark drafter allocate their buffers. When one of them did not fit
the engine ended with "strata-inkling CUDA error 2 (out of memory) at ink_cuda.cuh:54", which says neither how much was asked
for nor how much the card had.

- the message now reads "... (allocating 39 MiB; 22 of 11910 MiB free)" (ink_alloc_failed);
- while the backend initialises, a failed allocation throws InkAllocFailure instead of ending the process; the backend then
  retries the runner with the prefill chunk halved (512 > 256 > 128 > 64) and, if the drafter does not fit, runs without it
  ("DSpark drafter turned off ..."); only when even chunk 64 does not fit does start-up end, with a message that names the
  smaller context / --vram-experts-gb / other programs as the way out;
- a memory ledger line is logged: "[InkCUDA] memory: N MiB free after load, runner+KV took N MiB (max_batch=..), DSpark took N MiB, N MiB free now";
- test hook STRATA_INK_TEST_STEAL_MIB=N (holds N MiB after the experts are placed), switch STRATA_INK_OOM_RETRY=0 (old behaviour).

Measured on an RTX 3060 12 GB, SQ-10, 16K context, DSpark on, with N MiB held after the experts were placed (902 MiB free):
- STRATA_INK_OOM_RETRY=0: "CUDA error 2 ... ink_moe_cuda.cuh:131 (allocating 39 MiB; 22 of 11910 MiB free)", the engine ends.
- default: chunk 512 and 256 fail, chunk 128 starts (max_batch=128, 110 MiB left), the answers are the same tokens as an unconstrained run.
With 1102 MiB free the existing rule that lowers max_batch below 1200 MiB already copes (both settings start).
On an unconstrained 3060 / A4000 / 4090 the ledger shows 400-500 MiB free after start-up on a planned 12 GB card, and nothing changes.
This commit is contained in:
Niko1221 committed 2026-11-23 12:20:55 +01:00
1 parent 1178074aa0
commit 859276060a
3 files changed
+87 -6

No files matched your search

+28 -1
View File
@@ -26,6 +26,8 @@
#include <cstdlib>
#include <cstring>
#include <chrono>
#include <stdexcept>
#include <string>
#include "strata/core/device_error_exit.hpp"
#include "strata/platform/cgroup_mem.hpp"
@@ -55,10 +57,35 @@ inline double now_s() {
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
// A device allocation that did not fit. While start-up code runs with ink_alloc_throws() set (the backend's init), a failed
// allocation throws this so the backend can retry with a smaller workspace (#6644 #6792); everywhere else it ends the engine
// the way every other CUDA error does.
struct InkAllocFailure : std::runtime_error {
explicit InkAllocFailure(const std::string& m) : std::runtime_error(m) {}
};
inline bool& ink_alloc_throws() {
static thread_local bool v = false;
return v;
}
// Names the request and the card's free memory ("CUDA error 2 at ink_cuda.cuh:54" alone cannot be acted on).
inline void ink_alloc_failed(cudaError_t e, size_t bytes, const char* file, int line) {
size_t vf = 0, vt = 0;
cudaGetLastError();
cudaMemGetInfo(&vf, &vt);
char _what[300];
std::snprintf(_what, sizeof(_what), "strata-inkling CUDA error %d at %s:%d (allocating %.0f MiB; %.0f of %.0f MiB free)",
(int)e, file, line, (double)bytes / 1048576.0, (double)vf / 1048576.0, (double)vt / 1048576.0);
if (ink_alloc_throws()) throw InkAllocFailure(_what);
strata::core::exit_device_error(_what, cudaGetErrorString(e));
}
template <typename T>
inline T* dalloc(size_t count) {
T* p = nullptr;
if (count > 0) INK_CK(cudaMalloc(&p, count * sizeof(T)));
if (count > 0) {
const cudaError_t e = cudaMalloc(&p, count * sizeof(T));
if (e != cudaSuccess) ink_alloc_failed(e, count * sizeof(T), __FILE__, __LINE__);
}
return p;
}
+55 -4
View File
@@ -230,20 +230,69 @@ public:
}
}
runner_ = std::make_unique<strata::ink::InkCudaRunner>(model_);
const bool vf_graph_ok = strata::core::allow_1024_thread_cuda_graphs(
prop.major, prop.minor, strata::core::volta_forever_active());
runner_->use_cuda_graph = vf_graph_ok && !model_.ep2_active && !model_.ls_active;
if (!vf_graph_ok) {
std::fprintf(stderr, "[inkling-cuda] sm_70 under volta-forever: disabling 1024-thread CUDA Graph capture by default\n");
}
runner_->init(max_slots_, std::max(256, opts.max_ctx), std::max(64, opts.chunk), cuda_kvd, kv_offload_mode);
// Memory ledger (what each start-up step took from the card), so a plan that leaves too little can be seen in the log (#6644 #6792).
auto free_mib = [&]() -> double {
size_t vf = 0, vt = 0;
if (strata::ink::ink_cuda_mem_info(&vf, &vt, gpu_id_) != cudaSuccess) return -1.0;
return (double)vf / 1048576.0;
};
// Test hook: STRATA_INK_TEST_STEAL_MIB=N holds N MiB of the card after the experts are placed, to imitate a plan that left too little.
if (const char* steal = std::getenv("STRATA_INK_TEST_STEAL_MIB")) {
const long long mib = std::atoll(steal);
if (mib > 0 && cudaMalloc(&d_test_steal_, (size_t)mib << 20) != cudaSuccess) { d_test_steal_ = nullptr; cudaGetLastError(); }
}
const double led_loaded = free_mib();
// The experts are already in the card, so a start-up allocation that does not fit is met by a smaller workspace (the
// prefill chunk is halved down to 64) instead of ending the engine (#6644 #6792); the drafter is dropped last of all.
// STRATA_INK_OOM_RETRY=0 turns the retry off (the engine then ends on the first allocation that does not fit, as before).
const char* retry_env = std::getenv("STRATA_INK_OOM_RETRY");
strata::ink::ink_alloc_throws() = !(retry_env && std::atoi(retry_env) == 0);
struct ThrowGuard { ~ThrowGuard() { strata::ink::ink_alloc_throws() = false; } } throw_guard;
int chunk_try = std::max(64, opts.chunk);
for (;;) {
runner_ = std::make_unique<strata::ink::InkCudaRunner>(model_);
runner_->use_cuda_graph = vf_graph_ok && !model_.ep2_active && !model_.ls_active;
try {
runner_->init(max_slots_, std::max(256, opts.max_ctx), chunk_try, cuda_kvd, kv_offload_mode);
break;
} catch (const strata::ink::InkAllocFailure& ex) {
runner_->free_all();
runner_.reset();
cudaGetLastError();
if (chunk_try <= 64) {
err = std::string(ex.what()) + "; not enough free GPU memory for the working buffers even at the smallest size. "
"Use a smaller --max-context, a lower --vram-experts-gb, or close other programs that use the card.";
return false;
}
const int next = std::max(64, chunk_try / 2);
std::fprintf(stderr, "[InkCUDA] not enough free GPU memory for the working buffers at chunk %d (%s); retrying with chunk %d\n",
chunk_try, ex.what(), next);
chunk_try = next;
}
}
eff_max_ctx_ = runner_->max_ctx;
const double led_runner = free_mib();
if (opts.enable_dspark && model_.man.has_dspark && !model_.ls_active) {
dspark_ = std::make_unique<strata::ink::InkDSpark>(*runner_);
dspark_->init(eff_max_ctx_, 512);
try {
dspark_->init(eff_max_ctx_, 512);
} catch (const strata::ink::InkAllocFailure& ex) {
dspark_->free_all();
dspark_.reset();
cudaGetLastError();
std::fprintf(stderr, "[InkCUDA] DSpark drafter turned off: not enough free GPU memory for it (%s)\n", ex.what());
}
}
strata::ink::ink_alloc_throws() = false;
std::fprintf(stderr, "[InkCUDA] memory: %.0f MiB free after load, runner+KV took %.0f MiB (max_batch=%d), DSpark took %.0f MiB, %.0f MiB free now\n",
led_loaded, led_loaded - led_runner, runner_->max_batch, led_runner - free_mib(), free_mib());
// Allocate snapshot buffer (3.83 MB) + selected hidden gather buffers
if (!model_.ls_active) {
@@ -859,6 +908,7 @@ private:
if (dspark_) { dspark_->free_all(); dspark_.reset(); }
if (runner_) { runner_->free_all(); runner_.reset(); }
if (d_sconv_snap_) { cudaFree(d_sconv_snap_); d_sconv_snap_ = nullptr; }
if (d_test_steal_) { cudaFree(d_test_steal_); d_test_steal_ = nullptr; }
if (d_h_sel_) { cudaFree(d_h_sel_); d_h_sel_ = nullptr; }
if (d_sel_idx_) { cudaFree(d_sel_idx_); d_sel_idx_ = nullptr; }
if (h_logits_pin_) { cudaFreeHost(h_logits_pin_); h_logits_pin_ = nullptr; h_logits_cap_ = 0; }
@@ -891,6 +941,7 @@ private:
std::unique_ptr<strata::ink::InkCudaRunner> runner_;
std::unique_ptr<strata::ink::InkDSpark> dspark_;
float* d_sconv_snap_ = nullptr;
void* d_test_steal_ = nullptr; // STRATA_INK_TEST_STEAL_MIB test hook
float* d_h_sel_ = nullptr;
int32_t* d_sel_idx_ = nullptr;
float* h_logits_pin_ = nullptr;
+4 -1
View File
@@ -126,7 +126,10 @@ struct InkMoeWorkspace {
const size_t o_hqg32 = bump(P * (kInkInter / 32) * sizeof(InkQ8_1_G32));
raw_bytes = off;
INK_CK(cudaMalloc(&raw_buf, raw_bytes));
{
const cudaError_t me = cudaMalloc(&raw_buf, raw_bytes);
if (me != cudaSuccess) { raw_buf = nullptr; ink_alloc_failed(me, raw_bytes, __FILE__, __LINE__); }
}
uint8_t* base = (uint8_t*)raw_buf;
tb.cnt = (int32_t*)(base + o_cnt);
tb.off = (int32_t*)(base + o_off);