Skip to content

perf(server): keep SGLang tvm_ffi kernels and reuse kept ninja builds in a new container - #993

Merged
krisztian-gajdar merged 1 commit into
mainfrom
perf/sglang-kernel-cache-tvm-ffi
Nov 19, 2026
Merged

krisztian-gajdar merged 1 commit into
mainfrom
perf/sglang-kernel-cache-tvm-ffi

Conversation

@krisztian-gajdar

@krisztian-gajdar krisztian-gajdar commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

SIE_SGLANG_KERNEL_CACHE_ROOT keeps the compiler caches of an SGLang load, so a later load can reuse them. On the CUDA 12 bundles (SGLang 0.5.10.post1), three gaps make a new container with a kept root compile almost as much as an empty one:

  1. SGLang's own kernels are not kept.
  2. Ninja rebuilds kept kernels whose files come back with new mtimes.
    • tvm_ffi runs ninja in its build directory on every load.
    • Ninja treats an output as dirty when an input is newer than the output, and also when an input is newer than the output mtime recorded in .ninja_log.
    • A shared volume, or a copy, returns the files of an earlier container's build with mtimes that satisfy neither check, so 2 to 5 kernels rebuild in every new container.
  3. The first read of each kept file is slow on a network volume.
    • A new container reads the kept files one at a time as the engine asks for them.
    • On a network-backed volume, a kept Triton cache read that way was as slow as compiling it: 57 s against 52 s.

Change

In adapters/sglang/_server.py:

  • TVM_FFI_CACHE_DIR joins the kept directories, as <namespace>/tvm-ffi. An explicit value still wins.
  • Ninja builds are settled before the engine starts.
    • For every .ninja_log under the namespace, each file of that build gets the oldest output mtime the log recorded.
    • With one mtime that is no later than any logged output, neither of ninja's checks fires for files inside the build.
    • Inputs outside the build, such as installed headers, must be older than the build, which holds for an installed image.
    • Files that already carry that mtime are not touched, so a warm cache writes nothing.
  • Every kept file is read once before the engine starts, with 8 reader threads.
  • Failure handling. A failed pre-read logs a warning and keeps the cache; it never disables it. The log line reports the files and bytes read, the files settled and the time taken.

Caveat. Unlike Triton, DeepGEMM and SGLang 0.5.20's JIT cache, tvm_ffi writes its objects in place rather than publishing them by rename. Two processes that miss the same kernel at once on one shared root both build it in the same directory. Its FileLock (fcntl.flock on <build>/lock) serializes that only where flock reaches both processes.

Evidence

Setup.

  • Hardware: one A10G-class GPU, 2 CPUs and 16 GiB per container.
  • Server: sie-server serve -b <bundle> --preload <model> from this branch.
  • Prebuilt FlashInfer modules (flashinfer-jit-cache 0.6.7.post3+cu129, the package perf(server): ship FlashInfer's prebuilt JIT modules in the CUDA 12 SGLang bundles #992 adds) are on PYTHONPATH in every variant except "nothing kept".
  • Kernel-cache root: on a network-backed shared volume. The FlashInfer workspace, CUDA cache and XDG cache stay container-local.
  • Fresh state per load: an empty HOME and nothing else carried over.
  • Containers per model:
    • one writer, which loads with an empty root and then reuses it;
    • then three new containers that each load first from that root.
  • Metric: load to ready, from server start to the model reporting loaded.
Model (bundle) Nothing kept (s) Prebuilt modules only (s) Kept root, first load in a new container (s) Kept root, later load (s)
tencent/Hy-MT2-1.8B (sglang) 196 to 217 (n=3) 62 to 67 (n=6) 44 to 48 (n=3) 32 to 35 (n=5)
lightonai/LightOnOCR-2-1B (sglang-vision-extract) 304 to 322 (n=3) 83 to 119 (n=6) 75 to 76 (n=2) 44 to 63 (n=5)
  • No compiles in a new container. In every first load from a kept root of the same GPU product, nothing was compiled. The only compiler processes sampled were ninja -v no-op checks and one ptxas --version probe. The 4 (Hy-MT2) and 5 (LightOnOCR) tvm_ffi kernels gained no new files.
  • The pre-read cost:
    • Hy-MT2: 68 files (1.7 MB) in 0.4 to 1.8 s;
    • LightOnOCR: 135 files (4.2 MB) in 1.1 to 1.9 s.
  • Settling happens once. The first reuse in the writer's container settled 32 files (40 for LightOnOCR) to the oldest logged mtime. The shared volume kept those mtimes, so the new containers found nothing to settle.
  • Without settling, the build rebuilds. In an earlier run of the same setup, ninja rebuilt 2 to 5 tvm_ffi kernels in every new container until the files shared one mtime. The ninja test under Validation reproduces that check with a real build.
  • A different product name means a new namespace. One LightOnOCR reader landed on a card that reports NVIDIA A10, while its writer's card reported NVIDIA A10G; both came from the same A10G request. The namespace keys on the product name, so that container found an empty namespace and compiled everything (97 s). Its next load reused what it had written (44 s). It is excluded from the new-container column.
  • First requests:
    • Hy-MT2: the first greedy and sampled requests stay at 0.1 to 0.3 s in every variant.
    • LightOnOCR's first request falls from about 2.0 s with prebuilt modules only to 0.7 to 1.0 s with the kept root.

Validation

  • tests/adapters/test_sglang.py:
    • TVM_FFI_CACHE_DIR is placed under the namespace.
    • A real ninja build whose source and object come back with mtimes later than the logged ones is reused, with "no work to do". This test skips where ninja or cc is missing. It was run with ninja 1.13.0.
    • Settling uses the oldest logged output mtime and leaves non-ninja families alone, and a second pass changes nothing.
    • The pre-read counts every file and byte.
    • A failed pre-read keeps the cache.
  • SGLang adapter tests (-k sglang over tests/adapters/): 1032 passed, 4 skipped.
  • ruff check, ruff format --check and mise run typecheck are clean.

Summary by CodeRabbit

  • Performance
    • Kernel-cache startup now reads cached files concurrently, reducing the time spent preparing cached builds.
    • Cached builds are reused even when file timestamps change, and file timestamps are aligned for more consistent cache handling.
  • Reliability
    • If cache preparation encounters a file access error, setup continues with the available cache configuration rather than failing.

… in a new container

The kernel-cache root now covers TVM_FFI_CACHE_DIR, where SGLang 0.5.10
builds its own kernels through tvm_ffi (default ~/.cache/tvm-ffi).

Before the engine starts, the adapter gives every file of each kept ninja
build the oldest output mtime its .ninja_log recorded, so ninja reuses a
build whose files a shared volume returned with new mtimes. It then reads
every kept file once, so the engine does not wait on each first read.
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The SGLang adapter adds cache-file mtime settling and concurrent pre-reading during kernel-cache setup. It also maps the TVM-FFI cache directory, logs cache-read statistics, and returns cache defaults if settling or pre-reading raises an OSError.

Changes: SGLang kernel cache

Layer / File(s) Summary
Cache directories and file helpers
packages/sie_server/src/sie_server/adapters/sglang/_server.py, packages/sie_server/tests/adapters/test_sglang.py
The adapter maps the TVM-FFI cache directory and adds helpers to align regular files under Ninja builds to the oldest positive output mtime. It reads cache files concurrently with up to eight workers and reports file and byte totals. Tests cover the directory mapping, mtime settling, and file reads.
Kernel-cache setup integration
packages/sie_server/src/sie_server/adapters/sglang/_server.py, packages/sie_server/tests/adapters/test_sglang.py
Kernel-cache setup settles build mtimes and pre-reads cache files after the write probe succeeds. It logs counts and elapsed time, and returns cache defaults if either operation raises an OSError. Tests cover Ninja reuse and the error fallback.

Possibly related PRs

  • superlinked/sie#989: Maps the SGLang JIT cache under the same kernel-cache namespace that this change uses for the TVM-FFI cache directory.

Suggested reviewers: svonava

Merge Risk: Minimal · up to c0b0c

This change keeps SGLang kernel caches reusable across containers and pre-reads cache files before the engine starts. A failed pre-read only logs a warning and does not disable the cache. No concrete merge-blocking defect was found; the one open question is a rare interrupted-rebuild edge case.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: SGLang tvm_ffi kernels are kept and kept Ninja builds are reused in a new container. The raw summary and objectives both describe settling Ninja build mtimes, pre-read…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: SGLang tvm_ffi kernels are kept and kept Ninja builds are reused in a new container. The raw summary and objectives both describe settling Ninja build mtimes, pre-reading kept files, and adding the TVM-FFI cache directory, which the title covers at a high level. The title uses the conventional perf(server) prefix and is specific enough for a teammate scanning history. It omits the pre-read and mtime settling, which is acceptable for a title.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 43739e80-8e3c-42b5-baad-5a03c09648af
Commits

Reviewing files that changed from the base of the PR and between 17ed3d7 and c0b0cc4.

Files selected for processing: 2

  • packages/sie_server/src/sie_server/adapters/sglang/_server.py
  • packages/sie_server/tests/adapters/test_sglang.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@krisztian-gajdar
krisztian-gajdar added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 77461a3 Nov 19, 2026
46 of 47 checks passed
@krisztian-gajdar
krisztian-gajdar deleted the perf/sglang-kernel-cache-tvm-ffi branch November 19, 2026 22:18
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