Skip to content
Open
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
2 changes: 2 additions & 0 deletions engine/include/strata/kernels/iq_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions engine/include/strata/kernels/native_mmvq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions engine/include/strata/poly/kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions engine/src/kernels/cuda/iq_kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -6052,6 +6052,18 @@ 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)
// 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
}

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<float><<<(unsigned) (n / 256), 32, 0, (cudaStream_t) stream>>>(t, src, dst, nullptr);
Expand Down
13 changes: 13 additions & 0 deletions engine/src/kernels/cuda/native_mmvq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3709,6 +3709,19 @@ 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)
// 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();
#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);
Expand Down
1 change: 1 addition & 0 deletions engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3674,6 +3674,7 @@ void Engine::load(const Options& o) {
start_swaps();
reset();
build_mirror();
load_kernels();
std::vector<std::pair<int, int>> outside;
if (opt_.lock_experts && mirror_of_.empty()) { // the mirror's pages are pinned already
locked_.assign(layers_.size() * (size_t) n_expert_, 0);
Expand Down
3 changes: 3 additions & 0 deletions engine/src/poly/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
29 changes: 29 additions & 0 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cuda.h>

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -96,6 +98,33 @@ Dev Inkling2::msg_query(int il) {
return d;
}

#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,
// 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<double, std::milli>(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{};
Expand Down
12 changes: 12 additions & 0 deletions engine/src/poly/inkling2.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,18 @@ 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)
// 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
}

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");
Expand Down
3 changes: 3 additions & 0 deletions engine/src/poly/inkling2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) && !defined(STRATA_USE_HIP)
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
Expand Down
2 changes: 2 additions & 0 deletions engine/src/poly/inkling2_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions engine/src/poly/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3564,6 +3564,18 @@ void bf16_to_f32(const uint16_t* w, float* out, size_t n, void* stream) {
bf16_to_f32_kernel<<<grid(n, 256), 256, 0, cs(stream)>>>(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)
// 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
}

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<<<rows, sum_threads(n), 0, cs(stream)>>>(x, w, y, n, eps, y_stride ? y_stride : n);
if (const cudaError_t e = cudaGetLastError(); e != cudaSuccess)
Expand Down
Loading