Skip to content

Commit 729de00

Browse files
committed
fix: switch from "nextTick" to "nextCycle" for the default batch dispatcher
for improved performance
1 parent 275c005 commit 729de00

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/evaluator/LlamaContext/LlamaContext.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class LlamaContext {
7474
flashAttention = _model.defaultContextFlashAttention,
7575
threads,
7676
batching: {
77-
dispatchSchedule: batchingDispatchSchedule = "nextTick",
77+
dispatchSchedule: batchingDispatchSchedule = "nextCycle",
7878
itemPrioritizationStrategy: batchingItemsPrioritizationStrategy = "maximumParallelism"
7979
} = {},
8080
performanceTracking = false,
@@ -675,10 +675,19 @@ export class LlamaContext {
675675

676676
if (this._queuedDecodeSequenceIds.size === this._totalSequences)
677677
dispatch();
678-
if (dispatchSchedule === "nextTick")
679-
setTimeout(dispatch, 0);
680-
else
678+
if (dispatchSchedule === "nextCycle") {
679+
if (typeof setImmediate === "function")
680+
setImmediate(dispatch);
681+
else
682+
setTimeout(dispatch, 0);
683+
} else if (typeof dispatchSchedule === "function")
681684
dispatchSchedule(dispatch);
685+
else {
686+
if (typeof setImmediate === "function")
687+
setImmediate(dispatch);
688+
else
689+
setTimeout(dispatch, 0);
690+
}
682691
}
683692

684693
/** @internal */

src/evaluator/LlamaContext/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ export type LlamaContextSequenceRepeatPenalty = {
215215
export type BatchingOptions = {
216216
/**
217217
* The strategy used to dispatch items to be processed when there are items pending to be processed.
218-
* - **`"nextTick"`** - dispatch the items on the next event loop tick.
218+
* - **`"nextCycle"`** - dispatch the items on the next event loop cycle.
219219
* You can provide a custom function to define a custom dispatch schedule.
220220
*
221-
* Defaults to `"nextTick"`.
221+
* Defaults to `"nextCycle"`.
222222
*/
223-
dispatchSchedule?: "nextTick" | CustomBatchingDispatchSchedule,
223+
dispatchSchedule?: "nextCycle" | CustomBatchingDispatchSchedule,
224224

225225
/**
226226
* The strategy used to prioritize pending items to be processed.

0 commit comments

Comments
 (0)