perf(inkling2/cuda): the prefill chunk is picked from the memory left after the experts (2048 / 1024 / 512) when --chunk is not given

The chunk (prompt tokens per forward step, default 512) limited prefill: RTX 5090, strata-inkling --bench, one stream, 4K prompt, SQ-20: 512 -> 8.1K tok/s,
1024 -> 9.5K, 2048 -> 10.5K, 4096 -> 10.7K (16K prompt 7.0K -> 8.9K at 2048, 32K 6.1K -> 7.5K); UD-Q4_K_XL 4.07K -> 4.47K at 2048, flat after; nothing
is lost up to 8192. A bigger chunk costs working memory: the runner and K/V took 1460 / 1822 / 2710 / 4558 MiB at chunk 512 / 1024 / 2048 / 4096 on an
RTX 4090 (SQ-20 all resident, 5255 MiB free after the experts; the engine caps max_batch at 4096) and 1440 / 1804 MiB at 512 / 1024 on an RTX 3060 Ti
8 GB, where 2048 did not fit and the halve-on-fail retry (859276060) brought it back to 1024.

- strata/core/ink_chunk.hpp: ink_auto_chunk(free_after_load_mib, slots): the largest of 2048 / 1024 that fits (the memory the runner takes at that chunk
  + 1500 MiB margin for K/V growth, the drafter and the desktop), else 512; capped by the slot count: 2048 for 1-3 slots, 1024 for 4-15, 512 from 16.
  The experts are placed first, with their reserve computed for chunk 512, so a bigger chunk never displaces an expert: a hybrid card with little left
  keeps 512, a card whose experts are all resident gets 2048. Test ink_chunk_test (the 4090 and 3060 Ti ledgers, the cap, monotone in memory).
- InkCudaBackend::init resolves the chunk after the experts are placed and logs "[InkCUDA] prefill chunk N (auto: M MiB free after the experts, S slot(s);
  --chunk N sets it)"; effective_chunk() reports what the runner really got; ink_serve (token_budget) and --bench use it. --chunk N still wins,
  STRATA_INK_CHUNK_AUTO=0 keeps 512. HIP, SYCL, Metal and CPU keep 512 (BackendOpts::chunk_auto is read only by the CUDA backend).
- The slot cap comes from the mixed-work case, 32 slots, a 16K prompt arriving among 31 decoders, longest wait of a decoding slot at chunk 512 / 1024 /
  2048: RTX 3060 Ti 8 GB (SQ-10 hybrid) 2322 / 2210 / 43,424 ms (the long prompt read in 91 / 56 / 218 s); RTX 4090 (SQ-20 resident) 2096 / 3974 ms with the
  long prompt at 108 / 110 s; RTX 3060 12 GB 3897 ms at 512, 2804 ms at 1024. Throughput of 32 requests of 512 tokens at once (prompt + output tok/s):
  8 GB 55 (512) / 83 (1024) / 125 (2048); 12 GB 65 / 95; 4090 43 / 51. These ran on boxes whose CPUs were loaded by other jobs: relative numbers only.
- Equivalence: RTX 4090 SQ-20 (resident): greedy tokens identical between the auto chunk (2048) and --chunk 512 for prompts of 15, 200, 3000, 16000 and 30000
  tokens; serve prefill 3000 / 16000 / 30000 tokens 2788 / 2979 / 4608 -> 3088 / 3247 / 5053 tok/s. Hybrid SQ-10 (8 and 12 GB): identical up to 3000
  tokens, different at 16K (and 30K on the 8 GB card) between the two chunks, with the 512 runs of the two cards also differing from each other (expert
  promotion of the hybrid path); a same-chunk repeat is queued.
- No OOM and no retry in any gate run (8 / 12 / 24 GB, 1 and 32 slots).
This commit is contained in:
Niko1221 committed 2026-11-23 21:37:28 +01:00
1 parent 3cab928391
commit faceebb50c
7 files changed
+125 -4

No files matched your search

+4
View File
@@ -338,6 +338,10 @@ if(STRATA_BUILD_TESTS)
target_include_directories(ink_widen_test PRIVATE include)
add_test(NAME ink_widen_test COMMAND ink_widen_test)
# the architecture list the FP4 sm_120 file is built for, from a bare or an inherited CMAKE_CUDA_ARCHITECTURES list (configure-only, no compiler)
# the prefill chunk the CUDA engine picks without --chunk: the memory left after the experts decides, the slot count caps it (no GPU)
add_executable(ink_chunk_test tests/core/ink_chunk_test.cpp)
target_include_directories(ink_chunk_test PRIVATE include)
add_test(NAME ink_chunk_test COMMAND ink_chunk_test)
add_test(NAME ink_sm120a_cmake COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/cmake/ink_sm120a_test.cmake)
# 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)
+48
View File
@@ -0,0 +1,48 @@
// strata-inkling CUDA: the prefill chunk the engine picks when --chunk is not given.
//
// The chunk is how many prompt tokens one forward step takes (ink_serve token_budget, runner max_batch). A bigger chunk reads a prompt faster
// (RTX 5090, bench, one stream, 4K prompt: SQ-20 512 -> 8.1K tok/s, 2048 -> 10.5K, 4096 -> 10.7K; UD-Q4_K_XL 4.07K -> 4.47K at 2048, flat after) and costs
// working memory: the runner and its K/V took 1460 / 1822 / 2710 / 4558 MiB at chunk 512 / 1024 / 2048 / 4096 on an RTX 4090 (SQ-20 all resident, 5255
// MiB free after the experts; the engine caps max_batch at 4096), 1440 / 1804 MiB at 512 / 1024 on an RTX 3060 Ti 8 GB (SQ-10, 2955 MiB free), where
// 2048 did not fit and the engine's halve-on-fail retry brought it back to 1024.
//
// The experts are placed first, with a reserve for chunk 512, so the memory left after they are in (free_after_load) is what a bigger chunk may use:
// a card whose experts are all resident has plenty, a hybrid card keeps 512. Many slots share each step (decode tokens wait for the prefill tokens
// of the others), so the chunk is capped by the slot count.
#pragma once
#include <initializer_list>
namespace strata::core {
constexpr int kInkChunkDefault = 512;
constexpr double kInkChunkBaseMiB = 1460.0; // runner + K/V at chunk 512 (RTX 4090 SQ-20: 1460, RTX 3060 Ti SQ-10: 1440; 1 slot)
constexpr double kInkChunkMiBPerToken = 0.87; // working memory per chunk token above 512 (4090: 1822 / 2710 / 4558 MiB at 1024 / 2048 / 4096, 0.86 MiB per token)
constexpr double kInkChunkMarginMiB = 1500.0; // left free after the runner: K/V growth, the drafter, the desktop
// Memory (MiB) that must be free after the experts are placed for `chunk` to be chosen.
inline double ink_chunk_need_mib(int chunk) {
return kInkChunkBaseMiB + kInkChunkMiBPerToken * (chunk > kInkChunkDefault ? chunk - kInkChunkDefault : 0) + kInkChunkMarginMiB;
}
// Slots that share one step: the more of them, the smaller the prefill share one step may carry (a decoding slot waits for the whole step).
// 32 slots, a 16K prompt arriving among 31 decoders, longest wait of a decoding slot (ms) at chunk 512 / 1024 / 2048: RTX 3060 Ti 8 GB (SQ-10 hybrid)
// 2322 / 2210 / 43,424 (the long prompt read in 91 / 56 / 218 s); RTX 4090 (SQ-20 resident) 2096 / 3974 / -, the long prompt 108 / 110 s: at 32 slots a
// bigger chunk costs the decoders and does not read the long prompt faster. So: 2048 for 1-3 slots, 1024 for 4-15 slots, the old 512 from 16 slots up;
// 4096 is left to --chunk (RTX 5090 bench: 10.7K tok/s against 10.5K at 2048, flat).
inline int ink_chunk_cap_for_slots(int slots) {
if (slots >= 16) return kInkChunkDefault;
return slots >= 4 ? 1024 : 2048;
}
// The largest of 2048 / 1024 / 512 that fits `free_after_load_mib` and the slot cap; 512 when the memory is not known (< 0).
inline int ink_auto_chunk(double free_after_load_mib, int slots) {
if (free_after_load_mib < 0.0) return kInkChunkDefault;
const int cap = ink_chunk_cap_for_slots(slots);
for (int c : {2048, 1024}) {
if (c <= cap && free_after_load_mib >= ink_chunk_need_mib(c)) return c;
}
return kInkChunkDefault;
}
} // namespace strata::core
@@ -11,6 +11,7 @@
#include "ink_runner.cuh"
#include "ink_spec.cuh"
#include "strata/core/arch_probe.hpp"
#include "strata/core/ink_chunk.hpp"
#include "strata/core/volta_forever.hpp"
#include <algorithm>
@@ -82,6 +83,7 @@ public:
}
int effective_max_ctx() const override { return initialized_ ? eff_max_ctx_ : 0; }
int effective_chunk() const override { return initialized_ ? eff_chunk_run_ : 0; }
bool dspark_ready() const override { return initialized_ && dspark_ && dspark_->ready; }
@@ -275,6 +277,15 @@ public:
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);
// No --chunk: the memory left after the experts decides (strata/core/ink_chunk.hpp); STRATA_INK_CHUNK_AUTO=0 keeps the old 512.
if (opts.chunk_auto && !model_.ls_active) {
const char* auto_env = std::getenv("STRATA_INK_CHUNK_AUTO");
if (!(auto_env && std::atoi(auto_env) == 0)) {
chunk_try = std::max(chunk_try, strata::core::ink_auto_chunk(led_loaded, max_slots_));
std::fprintf(stderr, "[InkCUDA] prefill chunk %d (auto: %.0f MiB free after the experts, %d slot(s); --chunk N sets it)\n",
chunk_try, led_loaded, max_slots_);
}
}
for (;;) {
runner_ = std::make_unique<strata::ink::InkCudaRunner>(model_);
runner_->use_cuda_graph = vf_graph_ok && !model_.ep2_active && !model_.ls_active;
@@ -297,6 +308,7 @@ public:
}
}
eff_max_ctx_ = runner_->max_ctx;
eff_chunk_run_ = std::max(1, std::min(chunk_try, runner_->max_batch));
const double led_runner = free_mib();
if (opts.enable_dspark && model_.man.has_dspark && !model_.ls_active) {
@@ -953,6 +965,7 @@ private:
int gpu_id_ = 0;
int max_slots_ = 1;
int eff_max_ctx_ = 4096;
int eff_chunk_run_ = 0; // the prefill chunk the runner really got (after auto / halve-on-fail)
int dspark_slot_ = -1;
KvDtype kv_dtype_ = KvDtype::FP16;
std::string dev_info_;
+3 -1
View File
@@ -17,7 +17,8 @@ struct ImageRec; // defined in ink_vision.h
struct BackendOpts {
int max_ctx = 32768;
int parallel = 1; // 1..64 concurrent slots
int chunk = 512;
int chunk = 512; // prompt tokens per step; the CUDA backend picks a larger one by itself while chunk_auto (see strata/core/ink_chunk.hpp)
bool chunk_auto = true; // false once --chunk is given; only the CUDA backend reads it, every other backend uses chunk as it is
int gpu_index = 0;
int gpus = 1; // 1..4 (--gpus 2 / --gpus 0,1 / --layer-split auto)
std::vector<int> gpu_ids; // optional explicit GPU ordinals (--gpus 0,1 / --devices 0,1 / IK2_GPUS)
@@ -58,6 +59,7 @@ public:
virtual std::string device_info() const = 0;
virtual BackendStats stats() const = 0;
virtual int effective_max_ctx() const { return 0; }
virtual int effective_chunk() const { return 0; } // the prefill chunk the backend really runs with (0: opts.chunk)
// Load Section 1 trunk + DSpark + 3x loop-weighted Section 4 experts into device/host memory
virtual bool init(const InkModelFile& model, const BackendOpts& opts, std::string& err) = 0;
+4 -2
View File
@@ -168,8 +168,9 @@ static int cmd_bench(const std::string& model_path, const BackendOpts& bopts, in
std::vector<float> logits(static_cast<size_t>(P) * V);
StepBatch batch;
double t_pf0 = now_s();
for (int p0 = 0; p0 < prompt_len; p0 += bo.chunk) {
int len = std::min(bo.chunk, prompt_len - p0);
const int chunk_run = std::max(1, backend->effective_chunk() > 0 ? backend->effective_chunk() : bo.chunk);
for (int p0 = 0; p0 < prompt_len; p0 += chunk_run) {
int len = std::min(chunk_run, prompt_len - p0);
batch.clear();
batch.capture_taps = use_dspark;
for (int s = 0; s < P; ++s) {
@@ -412,6 +413,7 @@ int main(int argc, char** argv) {
cfg.bopts.parallel = std::max(1, std::min(kMaxParallelSlots, std::atoi(next().c_str())));
} else if (a == "--chunk") {
cfg.bopts.chunk = std::max(1, std::atoi(next().c_str()));
cfg.bopts.chunk_auto = false;
} else if (a == "--kv-dtype" || a == "--kv") {
std::string kv = next();
if (kv == "fp8" || kv == "fp8_e4m3") { cfg.bopts.kv_dtype = KvDtype::FP8_E4M3; cfg.bopts.kv_dtype_auto = false; }
+1 -1
View File
@@ -442,7 +442,7 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend&
const InkCfg& c = mf.m.c;
const int V = c.vocab_unpadded;
const int P = std::max(1, std::min(kMaxParallelSlots, cfg.bopts.parallel));
const int chunk_sz = std::max(1, cfg.bopts.chunk);
const int chunk_sz = std::max(1, backend.effective_chunk() > 0 ? backend.effective_chunk() : cfg.bopts.chunk);
const bool use_dspark = (cfg.draft_mode != "off") && mf.m.dspark.present && backend.dspark_ready();
// Probe Vision shard lazily (zero VRAM/RAM until first image request)
+52
View File
@@ -0,0 +1,52 @@
// The prefill chunk strata-inkling picks without --chunk (strata/core/ink_chunk.hpp): the memory left after the experts decides, the slot count caps it.
#include "strata/core/ink_chunk.hpp"
#include <cstdio>
using namespace strata::core;
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() {
// measured: RTX 3060 Ti 8 GB, SQ-10, 2955 MiB free after the experts: 512 (chunk 1024 would take 1804 MiB and leave 1151 MiB, below the margin)
CHECK(ink_auto_chunk(2955.0, 1) == 512);
// the ledger of an RTX 4090 (SQ-20, all resident, 5255 MiB free after the experts): runner + K/V took 1460 / 1822 / 2710 / 4558 MiB at 512 / 1024 / 2048 / 4096
CHECK(ink_chunk_need_mib(4096) - ink_chunk_need_mib(512) > (4558.0 - 1460.0) * 0.97);
CHECK(ink_chunk_need_mib(4096) - ink_chunk_need_mib(512) < (4558.0 - 1460.0) * 1.06);
CHECK(ink_chunk_need_mib(2048) - ink_chunk_need_mib(512) > (2710.0 - 1460.0) * 0.97);
CHECK(ink_chunk_need_mib(1024) - ink_chunk_need_mib(512) > (1822.0 - 1460.0));
CHECK(ink_auto_chunk(5255.0, 1) == 2048); // that 4090: 2048 leaves 2545 MiB
// a card that kept room: bigger chunks, one step at a time; never above 2048 by itself (4096 is for --chunk)
CHECK(ink_auto_chunk(3500.0, 1) == 1024);
CHECK(ink_auto_chunk(4400.0, 1) == 2048);
CHECK(ink_auto_chunk(6200.0, 1) == 2048);
CHECK(ink_auto_chunk(12000.0, 1) == 2048); // an RTX 5090 with every expert resident
CHECK(ink_auto_chunk(1.0e9, 1) == 2048);
// never below the old default, never from an unknown amount
CHECK(ink_auto_chunk(0.0, 1) == 512);
CHECK(ink_auto_chunk(-1.0, 1) == 512);
// slots cap the chunk: a decoding slot waits for the prefill tokens of the others in the same step (8 GB card: 43 s stall at 2048 with 32 slots)
CHECK(ink_auto_chunk(12000.0, 2) == 2048);
CHECK(ink_auto_chunk(12000.0, 3) == 2048);
CHECK(ink_auto_chunk(12000.0, 4) == 1024);
CHECK(ink_auto_chunk(12000.0, 8) == 1024);
CHECK(ink_auto_chunk(12000.0, 15) == 1024);
CHECK(ink_auto_chunk(12000.0, 16) == 512); // heavy parallel keeps the old chunk
CHECK(ink_auto_chunk(12000.0, 32) == 512);
CHECK(ink_auto_chunk(12000.0, 64) == 512);
CHECK(ink_auto_chunk(3500.0, 8) == 1024);
CHECK(ink_auto_chunk(2000.0, 8) == 512);
// monotone: more free memory never gives a smaller chunk
int last = 0;
for (double f = 0.0; f <= 20000.0; f += 50.0) {
const int c = ink_auto_chunk(f, 1);
CHECK(c >= last);
last = c;
}
if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; }
std::puts("ink_chunk_test: ok");
return 0;
}