From 8a278ff8b41f45794600e2ec457e9ca307af0a2c Mon Sep 17 00:00:00 2001 From: Rob Leidle Date: Tue, 6 Aug 2024 16:18:49 -0700 Subject: [PATCH] Enable async encoding for the GPTEncoder (#1946) This will improve the performance of indexing when using a GPT model. Also, improved the Async interface to also have an async decode method. Note: I was only able to test this method with the test I added as I do not have access to a GPT model. Co-authored-by: Rob Leidle --- core/llm/asyncEncoder.ts | 32 +++++++++++++------ core/llm/llamaTokenizerWorkerPool.mjs | 5 +++ core/llm/tiktokenWorkerPool.mjs | 17 ++++++++++ core/test/llm/asyncEncoder.test.ts | 41 +++++++++++++++++++++++++ extensions/vscode/scripts/prepackage.js | 1 + 5 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 core/llm/tiktokenWorkerPool.mjs create mode 100644 core/test/llm/asyncEncoder.test.ts diff --git a/core/llm/asyncEncoder.ts b/core/llm/asyncEncoder.ts index 722664ead..ed6425df2 100644 --- a/core/llm/asyncEncoder.ts +++ b/core/llm/asyncEncoder.ts @@ -5,22 +5,23 @@ import * as path from "path"; export interface AsyncEncoder { encode(text: string): Promise; - decode(tokens: number[]): string; + decode(tokens: number[]): Promise; + close(): Promise; } export class LlamaAsyncEncoder implements AsyncEncoder { private workerPool: workerpool.Pool; constructor() { - this.workerPool = workerpool.pool(path.join(__dirname, "/llamaTokenizerWorkerPool.mjs")); + this.workerPool = workerpool.pool(workerCodeFilePath("llamaTokenizerWorkerPool.mjs")); } async encode(text: string): Promise { return this.workerPool.exec("encode", [text]); } - decode(tokens: number[]): string { - return llamaTokenizer.decode(tokens); + async decode(tokens: number[]): Promise { + return this.workerPool.exec("decode", [tokens]); } // TODO: this should be called somewhere before exit or potentially with a shutdown hook @@ -31,17 +32,30 @@ export class LlamaAsyncEncoder implements AsyncEncoder { // this class does not yet do anything asynchronous export class GPTAsyncEncoder implements AsyncEncoder { - private tiktokenEncoding: Tiktoken; + private workerPool: workerpool.Pool; constructor() { - this.tiktokenEncoding = _encodingForModel("gpt-4"); + this.workerPool = workerpool.pool(workerCodeFilePath("tiktokenWorkerPool.mjs")); } async encode(text: string): Promise { - return this.tiktokenEncoding.encode(text, "all", []); + return this.workerPool.exec("encode", [text]); } - decode(tokens: number[]): string { - return this.tiktokenEncoding.decode(tokens); + async decode(tokens: number[]): Promise { + return this.workerPool.exec("decode", [tokens]); } + + // TODO: this should be called somewhere before exit or potentially with a shutdown hook + public async close(): Promise { + await this.workerPool.terminate(); + } +} + +function workerCodeFilePath(workerFileName: string): string { + if (process.env.NODE_ENV === "test") { + // `cross-env` seems to make it so __dirname is the root of the project and not the directory containing this file + return path.join(__dirname, "llm", workerFileName); + } + return path.join(__dirname, workerFileName); } \ No newline at end of file diff --git a/core/llm/llamaTokenizerWorkerPool.mjs b/core/llm/llamaTokenizerWorkerPool.mjs index 2131f4a1a..1ef82c872 100644 --- a/core/llm/llamaTokenizerWorkerPool.mjs +++ b/core/llm/llamaTokenizerWorkerPool.mjs @@ -5,6 +5,11 @@ function encode(segment) { return llamaTokenizer.encode(segment); } +function decode(tokens) { + return llamaTokenizer.decode(tokens); +} + workerpool.worker({ + decode, encode, }); \ No newline at end of file diff --git a/core/llm/tiktokenWorkerPool.mjs b/core/llm/tiktokenWorkerPool.mjs new file mode 100644 index 000000000..9e0462f33 --- /dev/null +++ b/core/llm/tiktokenWorkerPool.mjs @@ -0,0 +1,17 @@ +import workerpool from "workerpool"; +import { encodingForModel as _encodingForModel } from "js-tiktoken"; + +const tiktokenEncoding = _encodingForModel("gpt-4"); + +function encode(text) { + return tiktokenEncoding.encode(text, "all", []); +} + +function decode(tokens) { + return tiktokenEncoding.decode(tokens); +} + +workerpool.worker({ + decode, + encode, +}); \ No newline at end of file diff --git a/core/test/llm/asyncEncoder.test.ts b/core/test/llm/asyncEncoder.test.ts new file mode 100644 index 000000000..f181a05ca --- /dev/null +++ b/core/test/llm/asyncEncoder.test.ts @@ -0,0 +1,41 @@ +import { GPTAsyncEncoder, LlamaAsyncEncoder } from "../../llm/asyncEncoder"; + +describe("llama encoder", () => { + var tokenizer: LlamaAsyncEncoder; + + beforeAll(() => { + tokenizer = new LlamaAsyncEncoder(); + }); + + afterAll(() => { + tokenizer.close(); + }); + + test("hello world", async () => { + const input = "the quick brown fox jumped over the lazy dog"; + const output = await tokenizer.encode(input); + expect(output).toEqual([1, 278, 4996, 17354, 1701, 29916, 12500, 287, 975, 278, 17366, 11203]); + const decoded = await tokenizer.decode(output); + expect(decoded).toBe(input); + }); +}); + +describe("tiktoken encoder", () => { + var tokenizer: GPTAsyncEncoder; + + beforeAll(() => { + tokenizer = new GPTAsyncEncoder(); + }); + + afterAll(() => { + tokenizer.close(); + }); + + test("hello world", async () => { + const input = "the quick brown fox jumped over the lazy dog"; + const output = await tokenizer.encode(input); + expect(output).toEqual([1820, 4062, 14198, 39935, 27096, 927, 279, 16053, 5679]); + const decoded = await tokenizer.decode(output); + expect(decoded).toBe(input); + }); +}); \ No newline at end of file diff --git a/extensions/vscode/scripts/prepackage.js b/extensions/vscode/scripts/prepackage.js index 95aaf0010..f7ca0e5d7 100644 --- a/extensions/vscode/scripts/prepackage.js +++ b/extensions/vscode/scripts/prepackage.js @@ -245,6 +245,7 @@ const exe = os === "win32" ? ".exe" : ""; "../../../core/vendor/tree-sitter.wasm", "../../../core/llm/llamaTokenizerWorkerPool.mjs", "../../../core/llm/llamaTokenizer.mjs", + "../../../core/llm/tiktokenWorkerPool.mjs", ]; for (const f of filesToCopy) { fs.copyFileSync(path.join(__dirname, f), path.join(__dirname, "..", "out", path.basename(f)));