Repository navigation
[Engine] Inkling 2: load prompt kernels at startup, not first prompt - #291
Open
okada-shogo wants to merge 2 commits into
Open
okada-shogo wants to merge 2 commits into
okada-shogo wants to merge 2 commits into
Conversation
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 <noreply@anthropic.com>
… 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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem and result
CUDA loads a source file's kernels at the first launch of one of them (lazy loading, the default since CUDA 12.2). So the first prompt of every process waited for those loads. On an RTX 2070 Super, that was 68 ms at the first add-norm (
native_mmvq.cu), 50 ms at the first dequantization (iq_kernels.cu, for UD-Q2_K_XL's IQ1_S halves), and 7 and 5 ms at the first kernels ofinkling2.cuandkernels.cu: about 130 ms of a 302-token prompt.This PR loads those files once at the end of model load, after the expert cache is filled. Loading takes 56-59 ms longer (IQ4_NL) and 88-94 ms longer (UD-Q2_K_XL), and the first prompt no longer waits. The first 302-token prompt is 5.2% faster on IQ4_NL and 13.8% faster on UD-Q2_K_XL. The output is bit-identical: no launch changes.
First request of a fresh process, RTX 2070 Super 8 GB (Ryzen 5 3600XT). Both sides use the same flags:
--prompt-int8plusSTRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1. Each run is one--serveengine with one request; 3 interleaved rounds. Prompt tok/s, mean of 3 runs:Decode tok/s, the same runs, mean of 3:
A request after the first (warm engine) is unchanged, as expected: -0.9% to +0.2% prompt, all within run-to-run noise.
Each run (tok/s), and the warm-engine runs
First request, prompt:
Experts in VRAM at load: the same (IQ4_NL 914-918, UD-Q2_K_XL 1964-1970 in both builds). The kernels took 0 MiB
of VRAM that
cudaMemGetInfocan see (2 MiB steps).Warm engine (a 302-token request first, then the measured one), prompt tok/s, mean of 3:
Related discussion
Split out of #200, as the maintainer asked there:
#200 (comment)
Approach
native_mmvq.cu,iq_kernels.cu,kernels.cuandinkling2.cueach get a*_load()function that callscudaFuncGetAttributes()on one of their kernels. That loads the file's module and that kernel. The file's other kernels still load at their first launch, but that is cheap: in nsys, the first prompt's launches take 22.7 ms in all (IQ4_NL) and 28.3 ms (UD-Q2_K_XL), against 103 and 153 ms on main, and none of them takes over 0.3 ms (main: 67, 49, 8 and 4 ms). So no per-kernel load (cuFuncLoad) is needed.cudaPeekAtLastError()), so it clears only its own error (for a card the file has no code for). On HIP the loaders are empty and Inkling 2 has no override, so nothing is printed.Engine::load_kernels()(empty by default) runs at the end ofload(), after the expert cache and the RAM mirror are set up. Inkling 2's override calls the four loaders, and calls theiq_kernels.cuone only when an expert type has no int8 prompt product (Mmq::int8()). It printsthe prompt's kernels loaded in N ms (M MiB of VRAM).#if !defined(STRATA_GPU_METAL)).Validation
Branch port/warm-kernels 40281ef (the review fixes in 40281ef were checked by the nsys run below). The tests ran the same diff on main 7af9bbd. The branch was rebased since, with no conflicts, and the CUDA build was checked after the rebase.
Passed:
STRATA_GPU_EXPERTS=850), token ids + top-20 of 32 tokens, with the flags above and with defaults: IQ4_NL 1,500 / 8,192 and UD-Q2_K_XL 1,500 tokens, and a 7-request--serverun with checkpoints. All the same as main (port-p4/p4-id.sh, RTX 2070 Super 8 GB). Log:~/inkling-work/p4/pid-wc/progress.log.~/inkling-work/p4/wk-tr1/.Failed:
Not run:
Limits
per run gain every run.
iq_embed_rows) are not loaded ahead: Inkling 2 embeds on the host and never launches them.🤖 Generated with Claude Code