-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
105 additions
and
6 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
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,3 @@ | ||
const isWebWindowRuntime = typeof window !== 'undefined' | ||
|
||
export const useWebWorker = isWebWindowRuntime |
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,7 @@ | ||
export interface MLWorkerMessagePair<T extends string = string, A extends {} = {}, R extends {} = {}> { | ||
type: T | ||
request: { id: number; type: T; args: A } | ||
response: { id: number; result: R } | ||
} | ||
|
||
export interface MLWorkerEmbedTextMessage extends MLWorkerMessagePair<'embedText', string, Float32Array> {} |
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,27 @@ | ||
/// <reference lib="webworker" /> | ||
|
||
import { embedTextInThisScope } from '../corpus/search/embeddings' | ||
import { MLWorkerEmbedTextMessage, MLWorkerMessagePair } from './api' | ||
|
||
declare var self: DedicatedWorkerGlobalScope | ||
|
||
onRequest<MLWorkerEmbedTextMessage>( | ||
'embedText', | ||
async (text: string): Promise<Float32Array> => embedTextInThisScope(text) | ||
) | ||
|
||
// Tell our host we are ready. | ||
self.postMessage('ready') | ||
|
||
function onRequest<P extends MLWorkerMessagePair>( | ||
type: P['type'], | ||
handler: (args: P['request']['args']) => Promise<P['response']['result']> | ||
): void { | ||
self.addEventListener('message', async event => { | ||
const request = event.data as P['request'] | ||
if (request.type === type) { | ||
const response: P['response'] = { id: request.id, result: await handler(request.args) } | ||
self.postMessage(response) | ||
} | ||
}) | ||
} |
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,54 @@ | ||
import { type embedTextInThisScope } from '../corpus/search/embeddings' | ||
import { type MLWorkerEmbedTextMessage, type MLWorkerMessagePair } from './api' | ||
|
||
export const embedTextOnWorker: typeof embedTextInThisScope = async (text: string): Promise<Float32Array> => | ||
sendMessage<MLWorkerEmbedTextMessage>('embedText', text) | ||
|
||
async function sendMessage<P extends MLWorkerMessagePair>( | ||
type: P['type'], | ||
args: P['request']['args'] | ||
): Promise<P['response']['result']> { | ||
const worker = await acquireWorker() | ||
const id = nextID() | ||
worker.postMessage({ id, type, args } satisfies P['request']) | ||
return new Promise<P['response']['result']>(resolve => { | ||
const onMessage = (event: MessageEvent): void => { | ||
const response = event.data as P['response'] | ||
if (response.id === id) { | ||
resolve(response.result) | ||
worker.removeEventListener('message', onMessage) | ||
} | ||
} | ||
worker.addEventListener('message', onMessage) | ||
}) | ||
} | ||
|
||
const NUM_WORKERS = navigator.hardwareConcurrency || 2 | ||
|
||
const workers: (Promise<Worker> | undefined)[] = [] | ||
let workerSeq = 0 | ||
|
||
/** | ||
* Acquire a worker from the pool. Currently the acquisition is round-robin. | ||
*/ | ||
async function acquireWorker(): Promise<Worker> { | ||
const workerID = workerSeq++ % NUM_WORKERS | ||
let workerInstance = workers[workerID] | ||
if (!workerInstance) { | ||
workerInstance = new Promise<Worker>(resolve => { | ||
const worker = new Worker(new URL('./webWorker.ts', import.meta.url), { type: 'module' }) | ||
|
||
// Wait for worker to become ready. It sends a message when it is ready. The actual message | ||
// doesn't matter. | ||
worker.addEventListener('message', () => resolve(worker)) | ||
}) | ||
console.log('worker', workerID, 'is ready') | ||
workers[workerID] = workerInstance | ||
} | ||
return workerInstance | ||
} | ||
|
||
let id = 1 | ||
function nextID(): number { | ||
return id++ | ||
} |