-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve speed of text trimming (#157)
* refactor: improve speed of text trimming I added a benchmark for a heavy case of pruning. The changes in this PR reduce the runtime from ~17 seconds to ~0.2 seconds. There are more optimizations that can be done, but an ~80x speedup is a good start. Refs microsoft/vscode-copilot-release#4985 * up version
- Loading branch information
1 parent
a887ff7
commit ef03f8a
Showing
6 changed files
with
138 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,3 +133,5 @@ dist | |
.pnp.* | ||
|
||
src/base/htmlTracerSrc.ts | ||
|
||
*.cpuprofile |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { existsSync, readFileSync } from 'fs'; | ||
import { Bench } from 'tinybench'; | ||
import { Cl100KBaseTokenizer } from '../tokenizer/cl100kBaseTokenizer'; | ||
import type * as promptTsx from '..'; | ||
import assert = require('assert'); | ||
|
||
const comparePathVar = 'PROMPT_TSX_COMPARE_PATH'; | ||
const tsxComparePath = | ||
process.env[comparePathVar] || | ||
`${__dirname}/../../../../vscode-copilot/node_modules/@vscode/prompt-tsx`; | ||
const canCompare = existsSync(tsxComparePath); | ||
if (!canCompare) { | ||
console.error( | ||
`$${comparePathVar} was not set / ${tsxComparePath} doesn't exist, so the benchmark will not compare to past behavior` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const numberOfRepeats = 1; | ||
const sampleText = readFileSync(`${__dirname}/renderer.test.tsx`, 'utf-8'); | ||
const sampleTextLines = readFileSync(`${__dirname}/renderer.test.tsx`, 'utf-8').split('\n'); | ||
const tokenizer = new Cl100KBaseTokenizer(); | ||
const bench = new Bench({ | ||
name: `trim ${tokenizer.tokenLength(sampleText) * numberOfRepeats}->1k tokens`, | ||
time: 100, | ||
}); | ||
|
||
async function benchTokenizationTrim({ | ||
PromptRenderer, | ||
PromptElement, | ||
UserMessage, | ||
TextChunk, | ||
}: typeof promptTsx) { | ||
const r = await new PromptRenderer( | ||
{ modelMaxPromptTokens: 1000 }, | ||
class extends PromptElement { | ||
render() { | ||
return ( | ||
<> | ||
{Array.from({ length: numberOfRepeats }, () => ( | ||
<UserMessage> | ||
{sampleTextLines.map(l => ( | ||
<TextChunk>{l}</TextChunk> | ||
))} | ||
</UserMessage> | ||
))} | ||
</> | ||
); | ||
} | ||
}, | ||
{}, | ||
tokenizer | ||
).render(); | ||
assert(r.tokenCount <= 1000); | ||
assert(r.tokenCount > 100); | ||
} | ||
|
||
bench.add('current', () => benchTokenizationTrim(require('..'))); | ||
if (canCompare) { | ||
const fn = require(tsxComparePath); | ||
bench.add('previous', () => benchTokenizationTrim(fn)); | ||
} | ||
|
||
bench.run().then(() => { | ||
console.log(bench.name); | ||
console.table(bench.table()); | ||
}); |