Skip to content

llama : reuse scheduler buffers across requests - #31841

Closed
wsj040303 wants to merge 1 commit into
ggml-org:masterfrom
wsj040303:fix/preserve-scheduler-buffers-31840
Closed

wsj040303 wants to merge 1 commit into
ggml-org:masterfrom
wsj040303:fix/preserve-scheduler-buffers-31840

Conversation

@wsj040303

Copy link
Copy Markdown

Overview

Fixes #31840.

This follows the scheduler-retention approach proposed by @douyamv in #28872. That draft remains open. This continuation applies the approach to the current code, adds a node-capacity growth guard, and adds regression coverage.

With backend sampling, finishing a request detaches its sampler and triggers sched_reserve(). Recreating the scheduler also destroys its allocator and compute buffers, including pinned host buffers. Keep the existing scheduler when its node capacity is sufficient; recreate it when the required capacity grows. Graph invalidation, synchronization, allocation planning, and the explicit pipeline-parallel fallback remain in place.

The new test_sampler_buffer_reuse checks retained compute capacity across sampler removal, replacement, a larger sampling graph, and clearing all samplers. It also checks valid backend tokens and the return to raw logits after detaching all samplers. The test fails before the change and passes afterward on both CPU and CUDA.

Additional information

RTX 5070, Qwen3-0.6B BF16 GGUF, unified FP16 KV, backend sampling, pinned memory enabled throughout. The fixed workload from #31840 contains 256 requests in eight sequential batches of 32 concurrent requests. Each run has an extra 32-request warmup; startup and warmup are excluded.

Complete workload Baseline Patched
Run 1 70.664470 s 59.234996 s
Run 2 70.476790 s 59.109644 s
Run 3 70.521613 s 59.126150 s
Mean 70.554291 s 59.156930 s

Wall time decreased 16.15%. All six runs passed 256/256 length checks, with 133966 output tokens each. No request, model, KV, or sampling parameters were changed between builds.

In a separate first-32-request Nsight diagnostic, measurement-window cudaMallocHost / cudaFreeHost calls fell from 33 / 33 to 0 / 0. Cumulative sched_reserve time fell from 1.435 s to 0.042 s. A full-lifetime trace confirmed successful pinned allocations before measurement and their release after measurement, so this does not disable pinned memory.

Measurements were made against 31424b7c. The final rebase is onto c0ab6e5c; the only intervening upstream change adds the missing pthread link. The CPU/CUDA sampler suites and allocator test were rerun on the final commit.

Validation with the same Qwen3-0.6B model:

build/bin/test-backend-sampler "$MODEL" --device gpu
build/bin/test-backend-sampler "$MODEL" --device cpu
build/bin/test-alloc
  • All 22 sampler cases pass on each backend, including the new regression.
  • test-alloc, EditorConfig checks on the changed files, and git diff --check pass.
  • Two wikitext-2 perplexity chunks give 14.7147 +/- 1.51865 before and after.
  • Three-repeat llama-bench checks show no material change: pp512 38287 -> 38233 tokens/s; tg128 408.31 -> 408.43 tokens/s.
  • Full project CI, multi-GPU pipeline execution, and non-CUDA GPU backends have not been run locally.

The existing allocator retains its high-water buffer capacity until growth requires reallocation or the context is destroyed. This change does not introduce a new allocator or modify kernels/public APIs.

Requirements

  • I have read and agree with the contributing guidelines.
  • AI usage disclosure: YES. AI assisted with implementation, local validation, and report preparation.

Retain the scheduler while its node capacity is sufficient, and recreate it
when the required capacity grows. Cover sampler detach, replacement and
clearing with a compute-buffer capacity regression test.

Builds on the scheduler-retention approach in ggml-org#28872.

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and verified this change on my setup. Thanks for the PR.

@am17an

am17an commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@ggerganov can we merge this? Really nice speed-up when using backend sampling

@ggerganov

Copy link
Copy Markdown
Member

I'll push an alternative version soon.

@ggerganov

Copy link
Copy Markdown
Member

Superseded by #32009

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

Labels

testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: backend sampling repeatedly reallocates pinned host compute buffers as requests finish

3 participants