Skip to content

[Engine] Inkling 2: load prompt kernels at startup, not first prompt - #291

Open
okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/warm-kernels
Open

okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/warm-kernels

Conversation

@okada-shogo

@okada-shogo okada-shogo commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 of inkling2.cu and kernels.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-int8 plus STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1. Each run is one --serve engine with one request; 3 interleaved rounds. Prompt tok/s, mean of 3 runs:

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL first prompt 302 (tok/s) 260.9 274.4 +5.2%
RTX 2070 Super 8 GB IQ4_NL first prompt 2K (tok/s) 647.8 658.9 +1.7%
RTX 2070 Super 8 GB IQ4_NL first prompt 8K (tok/s) 974.1 979.8 +0.6%
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 302 (tok/s) 336.3 382.8 +13.8%
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 2K (tok/s) 896.0 931.5 +4.0%
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 8K (tok/s) 950.4 960.2 +1.0%

Decode tok/s, the same runs, mean of 3:

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL decode after 302 (tok/s) 42.2 42.4 +0.3%
RTX 2070 Super 8 GB IQ4_NL decode after 8K (tok/s) 34.6 34.6 -0.1%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 302 (tok/s) 47.8 48.0 +0.6%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K (tok/s) 41.8 41.7 -0.1%

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:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL first prompt 302 main 258.4 259.9 264.4
RTX 2070 Super 8 GB IQ4_NL first prompt 302 this PR alone 273.9 277.0 272.4
RTX 2070 Super 8 GB IQ4_NL first prompt 2K main 647.3 649.0 647.2
RTX 2070 Super 8 GB IQ4_NL first prompt 2K this PR alone 659.0 658.7 659.1
RTX 2070 Super 8 GB IQ4_NL first prompt 8K main 974.2 973.8 974.3
RTX 2070 Super 8 GB IQ4_NL first prompt 8K this PR alone 979.9 979.9 979.6
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 302 main 332.9 338.3 337.6
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 302 this PR alone 382.7 384.6 381.2
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 2K main 894.9 895.5 897.5
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 2K this PR alone 931.3 932.1 931.2
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 8K main 950.2 949.8 951.1
RTX 2070 Super 8 GB UD-Q2_K_XL first prompt 8K this PR alone 959.9 960.3 960.3

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 cudaMemGetInfo can see (2 MiB steps).

Warm engine (a 302-token request first, then the measured one), prompt tok/s, mean of 3:

Card Model Measure main this PR alone
RTX 2070 Super 8 GB IQ4_NL prompt 302 289.1 286.5
RTX 2070 Super 8 GB IQ4_NL prompt 2K 666.1 666.3
RTX 2070 Super 8 GB IQ4_NL prompt 8K 974.4 974.6
RTX 2070 Super 8 GB IQ4_NL prompt 32K 1036.6 1035.7
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 302 397.0 395.8
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 2K 867.7 866.9
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K 917.5 919.4
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K 892.0 892.9

Related discussion

Split out of #200, as the maintainer asked there:
#200 (comment)

Approach

  • native_mmvq.cu, iq_kernels.cu, kernels.cu and inkling2.cu each get a *_load() function that calls cudaFuncGetAttributes() 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.
  • A loader returns at once if an earlier launch left an error (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 of load(), after the expert cache and the RAM mirror are set up. Inkling 2's override calls the four loaders, and calls the iq_kernels.cu one only when an expert type has no int8 prompt product (Mmq::int8()). It prints the prompt's kernels loaded in N ms (M MiB of VRAM).
  • The loads run after the cache is filled, so the cache is sized exactly as before (the same experts at load).
  • Other models: unchanged (the hook is empty). Metal: not built there (#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:

  • Engine identity, experts frozen and capped (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 --serve run 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.
  • End-to-end first request and warm engine, in the tables above.
  • nsys of the first request, main vs this PR, IQ4_NL and UD-Q2_K_XL, 302 tokens: no launch over 0.3 ms is left in the prompt. Log: ~/inkling-work/p4/wk-tr1/.
  • CUDA build for sm 75;86 (CUDA 12.9).

Failed:

  • none observed

Not run:

  • HIP run. The HIP build compiles (ROCm 7.2.4). On HIP the four functions are empty, so it keeps loading at first launch.
  • Metal (untouched).

Limits

  • Only the first request of a process gains. A server that stays up gains once; benchmarks that start a fresh process
    per run gain every run.
  • Model load is 56-94 ms longer.
  • A cold first prompt is still ~40-60 ms slower than a warm one, because of other first-request costs (not kernel loads).
  • The IQ embedding kernels (iq_embed_rows) are not loaded ahead: Inkling 2 embeds on the host and never launches them.

🤖 Generated with Claude Code

okada-shogo and others added 2 commits November 24, 2026 11:28
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>
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: load the prompt kernels at the end of the load, not in the first prompt [Engine] Inkling 2: load prompt kernels during model load instead of on the first prompt Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: load prompt kernels during model load instead of on the first prompt [Engine] Inkling 2: load prompt kernels during model load instead of on the first prompt (first prompt up to 13.8% faster) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: load prompt kernels during model load instead of on the first prompt (first prompt up to 13.8% faster) [Engine] Inkling 2: load prompt kernels at model load (first prompt up to 14% faster) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2: load prompt kernels at model load (first prompt up to 14% faster) [Engine] Inkling 2: load prompt kernels at startup, not first prompt Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant