From f2ffdd88dff048a89b47a0950fccf6c55e6dcf66 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:48:09 +0900 Subject: [PATCH 1/2] poly: Inkling 2 loads its prompt kernels at the end of the load CUDA loads a file's kernels at the first launch of one of them (lazy loading, the default since CUDA 12.2), so the first prompt of a process waited for the files it launches from: on an RTX 2070 Super 68 ms at its first add-norm (native_mmvq.cu), 50 ms at its first dequantization (iq_kernels.cu, UD-Q2_K_XL's IQ1_S halves), 7 and 5 ms at the first of inkling2.cu's and kernels.cu's, about 130 ms of a 302-token prompt. Each of those files gets a function that asks for one kernel's attributes, which loads the file's kernels, and Inkling 2 calls them after the expert cache is filled (Engine::load_kernels()), so the cache is sized as before. iq_kernels.cu only where a prompt's expert product takes a type without the int8 product. The start line says how long it took and the VRAM it took. Same bits: no launch changes. Other models unchanged (the hook is empty). Co-Authored-By: Claude Opus 5.5 --- engine/include/strata/kernels/iq_kernels.hpp | 2 ++ engine/include/strata/kernels/native_mmvq.hpp | 3 ++ engine/include/strata/poly/kernels.hpp | 2 ++ engine/src/kernels/cuda/iq_kernels.cu | 10 +++++++ engine/src/kernels/cuda/native_mmvq.cu | 11 +++++++ engine/src/poly/engine.cpp | 1 + engine/src/poly/engine.hpp | 3 ++ engine/src/poly/inkling2.cpp | 29 +++++++++++++++++++ engine/src/poly/inkling2.cu | 10 +++++++ engine/src/poly/inkling2.hpp | 3 ++ engine/src/poly/inkling2_kernels.hpp | 2 ++ engine/src/poly/kernels.cu | 10 +++++++ 12 files changed, 86 insertions(+) diff --git a/engine/include/strata/kernels/iq_kernels.hpp b/engine/include/strata/kernels/iq_kernels.hpp index dad973cfc6..e5cc09ced6 100644 --- a/engine/include/strata/kernels/iq_kernels.hpp +++ b/engine/include/strata/kernels/iq_kernels.hpp @@ -40,6 +40,8 @@ void iq_mmvq_ids(int ggml_type, const MmvqIds& ids, int count, int n_in, int n_o /// an NVFP4 expert's s_down, read by the kernel itself so the source may be released right after it). void iq_dequant_f16(int ggml_type, const void* src, int64_t n, uint16_t* dst, void* stream, const float* scale = nullptr); void iq_dequant_f32(int ggml_type, const void* src, int64_t n, float* dst, void* stream); +/// Loads this file's kernels (CUDA loads them at the first launch otherwise), so that the first launch does not wait. +void iq_kernels_load(); /// Rows `tokens[0..n_tok)` (device ids) of a GGUF embedding table (`row_bytes` per row; the table may be mapped /// host memory) dequantized to fp32, `n_embd` per row (a multiple of 256). void iq_embed_rows(int ggml_type, const void* table, size_t row_bytes, const int32_t* tokens, int64_t n_tok, diff --git a/engine/include/strata/kernels/native_mmvq.hpp b/engine/include/strata/kernels/native_mmvq.hpp index d6d733b2de..649c98fd5d 100644 --- a/engine/include/strata/kernels/native_mmvq.hpp +++ b/engine/include/strata/kernels/native_mmvq.hpp @@ -52,6 +52,9 @@ void native_quantize_q8_1(const float* x, void* x_q8_1, int n_in, int ncols, void native_add_rms_norm_q8_1(float* x, const float* add, const float* w, float* y, void* y_q8_1, int n, int rows, float eps, int threads, void* stream); +/// Loads this file's kernels (CUDA loads them at the first launch otherwise), so that the first launch does not wait. +void native_mmvq_load(); + void native_swiglu_quantize_q8_1(const float* gate, const float* up, void* x_q8_1, int n_in, int ncols, void* stream); diff --git a/engine/include/strata/poly/kernels.hpp b/engine/include/strata/poly/kernels.hpp index a13246f3f4..5b8283ce1a 100644 --- a/engine/include/strata/poly/kernels.hpp +++ b/engine/include/strata/poly/kernels.hpp @@ -49,6 +49,8 @@ void scale_rows(float* y, int64_t ld, int width, int rows, const int32_t* place, /// rows) may be null. A row of y starts y_stride values after the one before it (0: n), so two calls can fill /// the two halves of rows twice as long. void rms_norm(const float* x, const float* w, float* y, int n, int rows, float eps, void* stream, int y_stride = 0); +/// Loads this file's kernels (CUDA loads them at the first launch otherwise), so that the first launch does not wait. +void kernels_load(); /// rms_norm() of 1 to 3 inputs of `rows` rows each, every one with its weight (null: none) and output, in one /// launch with the same bits. void rms_norm_many(const float* const* x, const float* const* w, float* const* y, int count, int n, int rows, float eps, diff --git a/engine/src/kernels/cuda/iq_kernels.cu b/engine/src/kernels/cuda/iq_kernels.cu index 390fdde8d5..93c02dfdef 100644 --- a/engine/src/kernels/cuda/iq_kernels.cu +++ b/engine/src/kernels/cuda/iq_kernels.cu @@ -6052,6 +6052,16 @@ void iq_embed_rows(int t, const void* table, size_t row_bytes, const int32_t* to check("iq_embed_rows"); } +// CUDA loads a file's kernels at the first launch of one of them (lazy loading, the default since CUDA 12.2): 50 to +// 70 ms for this file's on an RTX 2070 Super, inside the first prompt. Asking for a kernel's attributes loads them +// here instead. A card the file has no code for answers with an error, which is not kept. +void iq_kernels_load() { +#if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + cudaFuncAttributes a{}; + if (cudaFuncGetAttributes(&a, (const void*) dequant_flat_kernel<__half>) != cudaSuccess) (void) cudaGetLastError(); +#endif +} + void iq_dequant_f32(int t, const void* src, int64_t n, float* dst, void* stream) { if (n % 256 != 0 || !embed_type_supported(t)) { std::fprintf(stderr, "iq_dequant_f32: bad arguments\n"); std::exit(1); } dequant_flat_kernel<<<(unsigned) (n / 256), 32, 0, (cudaStream_t) stream>>>(t, src, dst, nullptr); diff --git a/engine/src/kernels/cuda/native_mmvq.cu b/engine/src/kernels/cuda/native_mmvq.cu index bfebd1ca87..26d764ca70 100644 --- a/engine/src/kernels/cuda/native_mmvq.cu +++ b/engine/src/kernels/cuda/native_mmvq.cu @@ -3709,6 +3709,17 @@ void native_add_rms_norm_q8_1(float* x, const float* add, const float* w, float* launch_check(); } +// CUDA loads a file's kernels at the first launch of one of them (lazy loading, the default since CUDA 12.2): 50 to +// 70 ms for this file's on an RTX 2070 Super, inside the first prompt. Asking for a kernel's attributes loads them +// here instead. A card the file has no code for answers with an error, which is not kept. +void native_mmvq_load() { +#if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + cudaFuncAttributes a{}; + if (cudaFuncGetAttributes(&a, (const void*) native_add_rms_norm_q8_1_kernel<1024>) != cudaSuccess) + (void) cudaGetLastError(); +#endif +} + void native_q5_k_mmvq(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, int ncols, void* stream) { validate_shape(n_in, ncols, QK); diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 35005b37ed..b774eed7e6 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -3674,6 +3674,7 @@ void Engine::load(const Options& o) { start_swaps(); reset(); build_mirror(); + load_kernels(); std::vector> outside; if (opt_.lock_experts && mirror_of_.empty()) { // the mirror's pages are pinned already locked_.assign(layers_.size() * (size_t) n_expert_, 0); diff --git a/engine/src/poly/engine.hpp b/engine/src/poly/engine.hpp index ab0e274541..44954c7c62 100644 --- a/engine/src/poly/engine.hpp +++ b/engine/src/poly/engine.hpp @@ -601,6 +601,9 @@ class Engine { virtual void finish_logits(float* logits, int rows) { (void) logits, (void) rows; } /// What a token's embedding is multiplied by on its way in. virtual float embed_scale() const { return 1.f; } + /// At the end of the load, after the expert cache is filled: load the kernels a prompt launches, which CUDA + /// loads at their first launch otherwise (lazy loading), inside the first prompt. + virtual void load_kernels() {} /// What a sequence has beside its recurrent state and its rows, in bytes: it is saved and brought back with /// the state (save_state(), load_state()). load_extra() gets the position the state is for. virtual size_t extra_bytes() const { return 0; } diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index c20d374044..14e729f716 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -6,12 +6,14 @@ #include "strata/poly/kernels.hpp" #include "strata/kernels/native_mmvq.hpp" #if !defined(STRATA_GPU_METAL) +#include "mmq.hpp" #include "strata/kernels/iq_kernels.hpp" #endif #include #include +#include #include #include #include @@ -96,6 +98,33 @@ Dev Inkling2::msg_query(int il) { return d; } +#if !defined(STRATA_GPU_METAL) +// CUDA loads a file's kernels at the first launch of one of them (lazy loading), and the first prompt waited for +// the files it launches from: on the RTX 2070 Super 68 ms at its first add-norm (native_mmvq.cu), 50 at its first +// dequantization (iq_kernels.cu, UD-Q2_K_XL's IQ1_S halves), 7 and 5 at the first of this model's and poly's own, +// 130 ms of a 302-token prompt in all. They load here, after the expert cache is filled: the cache is sized as +// before, and the first prompt finds them loaded, as the ones after it do. The dequantizers' file only where a +// prompt's expert products take it (a type without the int8 product). +void Inkling2::load_kernels() { + const auto t0 = std::chrono::steady_clock::now(); + size_t free0 = 0, free1 = 0, total = 0; + (void) cudaMemGetInfo(&free0, &total); + kernels::native_mmvq_load(); + kernels_load(); + ink_kernels_load(); + bool halves = false; + for (const Layer& L : layers_) + if (L.moe && mmq_) + halves = halves || !Mmq::int8((int) L.gu_type, n_embd_) || !Mmq::int8((int) L.u_type, n_embd_) || + !Mmq::int8((int) L.d_type, n_ff_exp_); + if (halves) kernels::iq_kernels_load(); + (void) cudaMemGetInfo(&free1, &total); + std::fprintf(stderr, "strata-poly: the prompt's kernels loaded in %.0f ms (%.0f MiB of VRAM)\n", + std::chrono::duration(std::chrono::steady_clock::now() - t0).count(), + free0 > free1 ? (double) (free0 - free1) / (1 << 20) : 0.); +} +#endif + void Inkling2::begin_pass() { blocks_.assign(layers_.size(), Block{}); tok_norm_ = out_query_ = Dev{}; diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index c5d7266393..7f02714e9d 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -2946,6 +2946,16 @@ void ink_gate(const float* cur, const float* msg, const float* gain, const float ink_gate_set(&cur, &msg, &gain, &forget, &out, 1, n, stream); } +// CUDA loads a file's kernels at the first launch of one of them (lazy loading, the default since CUDA 12.2): 50 to +// 70 ms for this file's on an RTX 2070 Super, inside the first prompt. Asking for a kernel's attributes loads them +// here instead. A card the file has no code for answers with an error, which is not kept. +void ink_kernels_load() { +#if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + cudaFuncAttributes a{}; + if (cudaFuncGetAttributes(&a, (const void*) ink_conv_kernel) != cudaSuccess) (void) cudaGetLastError(); +#endif +} + void ink_conv(const float* x, float* state, const float* w, const float* dyn, float* out, int channels, int heads, int taps, int rows, void* stream, float* snap, size_t snap_stride, unsigned snap_bits) { if (taps < 2 || taps > 8) throw std::invalid_argument("ink_conv: 2 to 8 taps"); diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index 9b3b442939..7aa8bcf74c 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -144,6 +144,9 @@ class Inkling2 final : public Engine { // experts' sum comes in after the card's bool moe_sum_later(int n) const override { return n == 1 && whole_rec_ && !bell_; } bool bells() const override { return true; } +#if !defined(STRATA_GPU_METAL) + void load_kernels() override; +#endif #if defined(STRATA_USE_HIP) // the shared expert is one memset (moe_shared()), and a graph launch a layer for it cost an RX 9070 XT's host // more than the memset: with experts on the CPU (no doorbell on HIP) a token's layers are launched one by one diff --git a/engine/src/poly/inkling2_kernels.hpp b/engine/src/poly/inkling2_kernels.hpp index 0026015baa..f3e37b66a9 100644 --- a/engine/src/poly/inkling2_kernels.hpp +++ b/engine/src/poly/inkling2_kernels.hpp @@ -77,6 +77,8 @@ void ink_gate(const float* cur, const float* msg, const float* gain, const float /// state at snap + j * snap_stride floats (a window's snapshots: Engine::snap_at_). void ink_conv(const float* x, float* state, const float* w, const float* dyn, float* out, int channels, int heads, int taps, int rows, void* stream, float* snap = nullptr, size_t snap_stride = 0, unsigned snap_bits = 0); +/// Loads this file's kernels (CUDA loads them at the first launch otherwise), so that the first launch does not wait. +void ink_kernels_load(); /// One token's ink_conv() (no dyn, one head) and its state, out written, then the stream acc updated: mode 0 /// acc = acc + out * a (axpy()), mode 1 keep = acc and then acc = out, mode 2 acc = out. The same bits. diff --git a/engine/src/poly/kernels.cu b/engine/src/poly/kernels.cu index 256f269c10..f13179bae7 100644 --- a/engine/src/poly/kernels.cu +++ b/engine/src/poly/kernels.cu @@ -3564,6 +3564,16 @@ void bf16_to_f32(const uint16_t* w, float* out, size_t n, void* stream) { bf16_to_f32_kernel<<>>(w, out, n); } +// CUDA loads a file's kernels at the first launch of one of them (lazy loading, the default since CUDA 12.2): 50 to +// 70 ms for this file's on an RTX 2070 Super, inside the first prompt. Asking for a kernel's attributes loads them +// here instead. A card the file has no code for answers with an error, which is not kept. +void kernels_load() { +#if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + cudaFuncAttributes a{}; + if (cudaFuncGetAttributes(&a, (const void*) rms_norm_kernel) != cudaSuccess) (void) cudaGetLastError(); +#endif +} + void rms_norm(const float* x, const float* w, float* y, int n, int rows, float eps, void* stream, int y_stride) { rms_norm_kernel<<>>(x, w, y, n, eps, y_stride ? y_stride : n); if (const cudaError_t e = cudaGetLastError(); e != cudaSuccess) From 40281efca6612bf8d114bad41d12b1d72cfe84ba Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:44:28 +0900 Subject: [PATCH 2/2] poly: Inkling 2's kernel loads leave an earlier error alone, and none on HIP Each *_load() returns at once where an earlier launch left an error (cudaPeekAtLastError()), so its own cudaGetLastError() clears only its own. Inkling2::load_kernels() and its start line are CUDA's only: on HIP the loaders are empty, and the line said "loaded in 0 ms". (From review.) Co-Authored-By: Claude Opus 5.5 --- engine/src/kernels/cuda/iq_kernels.cu | 2 ++ engine/src/kernels/cuda/native_mmvq.cu | 2 ++ engine/src/poly/inkling2.cpp | 2 +- engine/src/poly/inkling2.cu | 2 ++ engine/src/poly/inkling2.hpp | 2 +- engine/src/poly/kernels.cu | 2 ++ 6 files changed, 10 insertions(+), 2 deletions(-) diff --git a/engine/src/kernels/cuda/iq_kernels.cu b/engine/src/kernels/cuda/iq_kernels.cu index 93c02dfdef..6b4b61c097 100644 --- a/engine/src/kernels/cuda/iq_kernels.cu +++ b/engine/src/kernels/cuda/iq_kernels.cu @@ -6057,6 +6057,8 @@ void iq_embed_rows(int t, const void* table, size_t row_bytes, const int32_t* to // here instead. A card the file has no code for answers with an error, which is not kept. void iq_kernels_load() { #if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + // an error an earlier launch left is not this call's to clear: it stays for the caller to see + if (cudaPeekAtLastError() != cudaSuccess) return; cudaFuncAttributes a{}; if (cudaFuncGetAttributes(&a, (const void*) dequant_flat_kernel<__half>) != cudaSuccess) (void) cudaGetLastError(); #endif diff --git a/engine/src/kernels/cuda/native_mmvq.cu b/engine/src/kernels/cuda/native_mmvq.cu index 26d764ca70..bc650e4972 100644 --- a/engine/src/kernels/cuda/native_mmvq.cu +++ b/engine/src/kernels/cuda/native_mmvq.cu @@ -3714,6 +3714,8 @@ void native_add_rms_norm_q8_1(float* x, const float* add, const float* w, float* // here instead. A card the file has no code for answers with an error, which is not kept. void native_mmvq_load() { #if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + // an error an earlier launch left is not this call's to clear: it stays for the caller to see + if (cudaPeekAtLastError() != cudaSuccess) return; cudaFuncAttributes a{}; if (cudaFuncGetAttributes(&a, (const void*) native_add_rms_norm_q8_1_kernel<1024>) != cudaSuccess) (void) cudaGetLastError(); diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index 14e729f716..a08c6626cd 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -98,7 +98,7 @@ Dev Inkling2::msg_query(int il) { return d; } -#if !defined(STRATA_GPU_METAL) +#if !defined(STRATA_GPU_METAL) && !defined(STRATA_USE_HIP) // CUDA loads a file's kernels at the first launch of one of them (lazy loading), and the first prompt waited for // the files it launches from: on the RTX 2070 Super 68 ms at its first add-norm (native_mmvq.cu), 50 at its first // dequantization (iq_kernels.cu, UD-Q2_K_XL's IQ1_S halves), 7 and 5 at the first of this model's and poly's own, diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index 7f02714e9d..c723dea3de 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -2951,6 +2951,8 @@ void ink_gate(const float* cur, const float* msg, const float* gain, const float // here instead. A card the file has no code for answers with an error, which is not kept. void ink_kernels_load() { #if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + // an error an earlier launch left is not this call's to clear: it stays for the caller to see + if (cudaPeekAtLastError() != cudaSuccess) return; cudaFuncAttributes a{}; if (cudaFuncGetAttributes(&a, (const void*) ink_conv_kernel) != cudaSuccess) (void) cudaGetLastError(); #endif diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index 7aa8bcf74c..6d0abaead6 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -144,7 +144,7 @@ class Inkling2 final : public Engine { // experts' sum comes in after the card's bool moe_sum_later(int n) const override { return n == 1 && whole_rec_ && !bell_; } bool bells() const override { return true; } -#if !defined(STRATA_GPU_METAL) +#if !defined(STRATA_GPU_METAL) && !defined(STRATA_USE_HIP) void load_kernels() override; #endif #if defined(STRATA_USE_HIP) diff --git a/engine/src/poly/kernels.cu b/engine/src/poly/kernels.cu index f13179bae7..136f546803 100644 --- a/engine/src/poly/kernels.cu +++ b/engine/src/poly/kernels.cu @@ -3569,6 +3569,8 @@ void bf16_to_f32(const uint16_t* w, float* out, size_t n, void* stream) { // here instead. A card the file has no code for answers with an error, which is not kept. void kernels_load() { #if !defined(__HIPCC__) && !defined(STRATA_USE_HIP) // (HIP builds keep the first launch's load) + // an error an earlier launch left is not this call's to clear: it stays for the caller to see + if (cudaPeekAtLastError() != cudaSuccess) return; cudaFuncAttributes a{}; if (cudaFuncGetAttributes(&a, (const void*) rms_norm_kernel) != cudaSuccess) (void) cudaGetLastError(); #endif