Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llama/addon/AddonContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ AddonContext::AddonContext(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Ad
if (options.Has("swaFullCache")) {
context_params.swa_full = options.Get("swaFullCache").As<Napi::Boolean>().Value();
}

if (options.Has("kvUnified")) {
context_params.kv_unified = options.Get("kvUnified").As<Napi::Boolean>().Value();
}
}
}
AddonContext::~AddonContext() {
Expand Down
6 changes: 5 additions & 1 deletion src/evaluator/LlamaContext/LlamaContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class LlamaContext {
/** @internal */ private readonly _unusedSequenceIds: number[] = [];
/** @internal */ private readonly _batchingOptions: Required<BatchingOptions>;
/** @internal */ public readonly _swaFullCache: boolean = false;
/** @internal */ private readonly _kvUnified: boolean | undefined = undefined;
/** @internal */ private readonly _queuedDecodeSequenceIds = new Set<number>();
/** @internal */ private readonly _queuedDecodes: InternalQueuedDecode[] = [];
/** @internal */ private readonly _disposeAggregator = new AsyncDisposeAggregator();
Expand Down Expand Up @@ -112,6 +113,7 @@ export class LlamaContext {
itemPrioritizationStrategy: batchingItemsPrioritizationStrategy = "maximumParallelism"
} = {},
swaFullCache = _model.defaultContextSwaFullCache,
kvUnified,
performanceTracking = false,
experimentalKvCacheKeyType,
experimentalKvCacheValueType,
Expand Down Expand Up @@ -155,6 +157,7 @@ export class LlamaContext {
this._kvCacheKeyType = experimentalKvCacheKeyType;
this._kvCacheValueType = experimentalKvCacheValueType;
this._swaFullCache = !!swaFullCache;
this._kvUnified = kvUnified;
this._ctx = new this._llama._bindings.AddonContext(this._model._model, removeNullFields({
contextSize: padSafeContextSize(this._contextSize * this._totalSequences, "up"), // each sequence needs its own <contextSize> of cells
batchSize: this._batchSize + (
Expand All @@ -170,7 +173,8 @@ export class LlamaContext {
performanceTracking: this._performanceTracking,
kvCacheKeyType: this._kvCacheKeyType,
kvCacheValueType: this._kvCacheValueType,
swaFullCache: this._swaFullCache
swaFullCache: this._swaFullCache,
kvUnified: this._kvUnified
}));
this._batchingOptions = {
dispatchSchedule: batchingDispatchSchedule,
Expand Down
15 changes: 15 additions & 0 deletions src/evaluator/LlamaContext/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ export type LlamaContextOptions = {
*/
swaFullCache?: boolean,

/**
* Use a unified KV buffer shared across all sequences when computing attention.
*
* When enabled, llama.cpp uses a single contiguous KV buffer indexed by sequence id,
* which can significantly improve multi-sequence prefill/decode throughput on GPU
* backends by reducing per-sequence buffer juggling.
*
* The llama.cpp default for `kv_unified` depends on whether the number of sequences
* is auto-detected; explicitly setting `kvUnified: true` matches the behavior of
* `llama-server` running with `--kv-unified` (which is the default in many configurations).
*
* Defaults to the llama.cpp default for the context configuration.
*/
kvUnified?: boolean,

/**
* Load the provided LoRA adapters onto the context.
* LoRA adapters are used to modify the weights of a pretrained model to adapt to new tasks or domains
Expand Down