Skip to content

Precompile GLM vision kernels before KV allocation to avoid first-image OOMs - #37539

Closed
ormandj wants to merge 2 commits into
sgl-project:xinyuan/glm-5.3-flash-supportfrom
ormandj:pr/glm5-vision-attention-precompile
Closed

ormandj wants to merge 2 commits into
sgl-project:xinyuan/glm-5.3-flash-supportfrom
ormandj:pr/glm5-vision-attention-precompile

Conversation

@ormandj

@ormandj ormandj commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Superseded by #38214 against main. This PR auto-closed after #36507 merged and its base branch was deleted. The historical description and validation below refer to this original revision.

Motivation

The first image request can run out of GPU memory while compiling or loading vision kernels, after the KV cache and CUDA graphs have consumed the available headroom. A small warmup image also does not necessarily compile the dynamic MLP activation used by larger images.

This PR precompiles GLM-5-Next vision attention and MLP activations after loading the model but before allocating the KV pool. The request-time forward implementation is unchanged.

Modifications

  • Add the existing model-runner precompile hook to Glm5NextForConditionalGeneration.
  • Run the vision block MLP and patch merger at 64 and 4096 tokens so compilation covers both token counts before serving.
  • For the Triton attention backend, launch the tower's attention specialization using small tensors with the production dtype and head geometry.
  • Skip unavailable modules and non-first pipeline ranks; skip attention precompile for non-Triton backends. Log precompile failures at WARNING.

Accuracy Tests

CPU recording subclasses verify the MLP and merger calls, token counts, dtype/device, skip conditions and warning behavior. The attention test substitutes a recording kernel to check its arguments. Author-reported CPU result: 8 passed.

CUDA_VISIBLE_DEVICES=9 PYTHONPATH=python python -m pytest -q test/registered/unit/models/test_glm5_next_vision_precompile.py
PYTHONPATH=python python -m pytest -q test/registered/unit/models/test_glm5_next_vision_precompile_gpu.py

The GPU regression runs the real attention precompile, then a strided three-sequence vision-attention call. It compares outputs with SDPA and checks Triton's cache and device-load hook to require no new specialization or device load on the second call. Author-reported pass on one RTX PRO 6000 Blackwell, Triton 3.7.1; that run preceded the expanded MLP CPU cases.

Author-observed serving failure: a first 3840x2160 image on a fresh compilation cache triggered MLP autotuning and OOM at memory fraction 0.99. No exact-head full-model reproduction log is attached, and the attention GPU regression does not itself validate the MLP autotune path.

Speed Tests and Profiling

The hook adds startup compilation and small forward calls before pool allocation. No isolated startup-duration or serving-speed benchmark for this revision.

Checklist

  • Format changed code and add CPU/GPU regression coverage.
  • Distinguish attention-kernel validation from the MLP serving observation.

Developed with AI assistance.


CI States

Latest PR Test (Base): ❌ Run #33595057897
Latest PR Test (Extra): ❌ Run #33595057749
Latest PR Test (AMD ROCm 7.2): ❌ Run #33595058218

…rtup

Add precompile_kernels_after_loading to Glm5NextForConditionalGeneration.
When the vision attention backend resolves to triton_attn, the hook issues
one context_attention_fwd call with the tower's specialization inputs
(head_dim = hidden_size // num_heads, the tower dtype, kv_group_num=1,
is_causal=False) so the Triton _fwd_kernel is compiled and device-loaded
while ModelRunner.load_model runs, before the KV pool and CUDA graphs claim
device memory. Without it the kernel loads on the first image request, which
can arrive hours into serving with almost no free device memory.

Only the first pipeline rank runs the hook, matching where
general_mm_embed_routine embeds images. The hook synchronizes the device
inside its try so asynchronously reported kernel errors are logged at
WARNING together with synchronous ones; failures never abort loading. The
model runner already calls the hook through
maybe_precompile_model_kernels_after_loading; language-only models,
non-Triton vision backends and later PP ranks return early.

Tests: a CPU test covers the argument contract and the skip conditions; a
GPU test runs the hook against the real kernel, then a representative
multi-sequence strided vision call, and asserts through Triton's kernel
cache and kernel_load_start_hook that no second compile or device load
happens.
@github-actions github-actions Bot added the Multi-modal multi-modal language model label Sep 2, 2026
…ore pools exist

swiglu_clamped is torch.compile'd with dynamo's default shape policy: the
first token count compiles a static kernel and the next distinct count
recompiles a dynamic one, each running inductor's Triton autotune with
benchmark buffers on the device. On a fresh inductor cache the first image
whose token count differs from the warmup image's autotunes during serving;
at mem_fraction_static 0.99 that failed with CUDA OOM on the first 3840x2160
request after a 64x64 warmup image. Run the block MLP and the patch merger
at two token counts in the precompile hook, before the memory pools are
allocated.
@ormandj ormandj changed the title Precompile the GLM-5-Next vision-tower Triton attention kernel at startup Precompile the GLM-5-Next vision-tower attention kernel and compiled MLP activations at startup Sep 2, 2026
@ormandj ormandj changed the title Precompile the GLM-5-Next vision-tower attention kernel and compiled MLP activations at startup Precompile GLM vision kernels before KV allocation to avoid first-image OOMs Sep 6, 2026
@Fridge003
Fridge003 deleted the branch sgl-project:xinyuan/glm-5.3-flash-support September 6, 2026 09:28
@Fridge003 Fridge003 closed this Sep 6, 2026
@ormandj

ormandj commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

Refiled as #38214 against main after #36507 merged and this PR's base branch was deleted. The patch is unchanged (matching stable Git patch ID); author CPU regression checks on the new head passed 8 tests. GPU validation attached here remains tied to the original revision and has not been rerun on the new main-based head. Please continue review on #38214.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Multi-modal multi-modal language model

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants