Add llama KV prefix cache reuse#74
Merged
Merged
Conversation
881ece4 to
7471229
Compare
There was a problem hiding this comment.
Pull request overview
Adds KV-cache reuse to the local llama runtime so consecutive autocomplete requests can reuse the decoded common prompt prefix instead of rebuilding a fresh context each time, and wires lifecycle invalidation/reset paths through the coordinator/router.
Changes:
- Introduces a reusable prompt context + KV trimming/validation path in
LlamaRuntimeCorewith an optionalcachedPrefixByteshint. - Threads deterministic sampling (
randomSeed) and cache-hint tracking through request/config and the llama engine. - Adds a unified
resetCachedGenerationContext()contract and calls it from coordinator lifecycle/focus/settings transitions.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tabbyTests/LlamaPromptRendererTests.swift | Adds unit tests for the prompt cache hint tracker and a helper to build SuggestionRequests. |
| tabby/Support/SuggestionRequestFactory.swift | Plumbs randomSeed from configuration into built requests. |
| tabby/Services/Runtime/SuggestionEngineRouter.swift | Adds router fan-out reset for cached generation context. |
| tabby/Services/Runtime/LlamaSuggestionEngine.swift | Adds cache-hint tracker integration, seed plumbing, and cache reset behavior. |
| tabby/Services/Runtime/LlamaRuntimeManager.swift | Extends generate API with cache hint + seed; adds prompt-cache reset entry point. |
| tabby/Services/Runtime/LlamaRuntimeCore.swift | Implements reusable prompt context, KV trimming, token-level validation, and deterministic sampler seeding. |
| tabby/Services/Runtime/FoundationModelSuggestionEngine.swift | Implements resetCachedGenerationContext() as a no-op for stateless sessions. |
| tabby/Models/SuggestionSubsystemContracts.swift | Extends SuggestionGenerating with resetCachedGenerationContext(). |
| tabby/Models/SuggestionModels.swift | Adds randomSeed to configuration and request models. |
| tabby/App/Coordinators/SuggestionCoordinator+Prediction.swift | Resets cached generation context when disabling predictions. |
| tabby/App/Coordinators/SuggestionCoordinator+Lifecycle.swift | Resets cached generation context on stop/model switch/settings change. |
| tabby/App/Coordinators/SuggestionCoordinator+Input.swift | Resets cached generation context when focused element changes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+127
to
+130
| func resetPromptCache() { | ||
| Task { | ||
| await core.resetPromptCache() | ||
| } |
| latency: Date().timeIntervalSince(startTime) | ||
| ) | ||
| } catch is CancellationError { | ||
| resetCachedGenerationContext() |
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.
Summary
Adds KV-cache reuse to the local llama runtime so consecutive autocomplete requests can reuse the decoded common prompt prefix instead of rebuilding a fresh context every time. The runtime still tokenizes and validates the shared token prefix before trusting cached native state, and coordinator lifecycle paths reset the cache on focus, settings, model, or disabled-state changes.
Validation
xcodebuild build -scheme tabby -project tabby.xcodeproj -destination 'platform=macOS'** BUILD SUCCEEDED **xcodebuild test -scheme tabby -project tabby.xcodeproj -destination 'platform=macOS' CODE_SIGNING_ALLOWED=NO CODE_SIGNING_REQUIRED=NO** TEST SUCCEEDED **, 30 tests, 0 failuresQwen3-0.6B-Q4_K_M.ggufLlamaRuntimeCacheMicrobenchTests.test_tenCharacterTypingSession_reportsCachedPrefixLatencyDeltalocally to quantify the ticket's latency-drop acceptance criterion.** TEST SUCCEEDED **, 1 selected test, 0 failuresLinked issues
Refs #23
Refs #13
Risk / rollout notes
LlamaRuntimeCore, because native llama contexts and KV memory must stay behind the serialized runtime actor.GGML_ASSERT([rsets->data count] == 0)backtrace after test completion on this machine.