onnxruntime
194 строки · 7.2 Кб
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4/// <reference lib="webworker" />
5
6//
7// * type hack for "HTMLImageElement"
8//
9// in typescript, the type of "HTMLImageElement" is defined in lib.dom.d.ts, which is conflict with lib.webworker.d.ts.
10// when we use webworker, the lib.webworker.d.ts will be used, which does not have HTMLImageElement defined.
11//
12// we will get the following errors complaining that HTMLImageElement is not defined:
13//
14// ====================================================================================================================
15//
16// ../common/dist/cjs/tensor-factory.d.ts:187:29 - error TS2552: Cannot find name 'HTMLImageElement'. Did you mean
17// 'HTMLLIElement'?
18//
19// 187 fromImage(imageElement: HTMLImageElement, options?: TensorFromImageElementOptions):
20// Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
21// ~~~~~~~~~~~~~~~~
22//
23// node_modules/@webgpu/types/dist/index.d.ts:83:7 - error TS2552: Cannot find name 'HTMLImageElement'. Did you mean
24// 'HTMLLIElement'?
25//
26// 83 | HTMLImageElement
27// ~~~~~~~~~~~~~~~~
28//
29// ====================================================================================================================
30//
31// `HTMLImageElement` is only used in type declaration and not in real code. So we define it as `unknown` here to
32// bypass the type check.
33
34//
35// * type hack for "document"
36//
37// in typescript, the type of "document" is defined in lib.dom.d.ts, so it's not available in webworker.
38//
39// we will get the following errors complaining that document is not defined:
40//
41// ====================================================================================================================
42//
43// lib/wasm/wasm-utils-import.ts:7:33 - error TS2584: Cannot find name 'document'. Do you need to change your target
44// library? Try changing the 'lib' compiler option to include 'dom'.
45//
46// 7 export const scriptSrc = typeof document !== 'undefined' ? (document?.currentScript as HTMLScriptElement)?.src :
47// ~~~~~~~~
48//
49// lib/wasm/wasm-utils-import.ts:7:61 - error TS2584: Cannot find name 'document'. Do you need to change your target
50// library? Try changing the 'lib' compiler option to include 'dom'.
51//
52// 7 export const scriptSrc = typeof document !== 'undefined' ? (document?.currentScript as HTMLScriptElement)?.src :
53// ~~~~~~~~
54//
55// lib/wasm/wasm-utils-import.ts:7:88 - error TS2552: Cannot find name 'HTMLScriptElement'. Did you mean
56// 'HTMLLIElement'?
57//
58// 7 export const scriptSrc = typeof document !== 'undefined' ? (document?.currentScript as HTMLScriptElement)?.src :
59// ~~~~~~~~~~~~~~~~~
60// ====================================================================================================================
61//
62// `document` is used to get the current script URL, which is not available in webworker. This file is served as a
63// "dual" file for entries of both webworker and the esm module.
64//
65declare global {
66type HTMLImageElement = unknown;
67type HTMLScriptElement = { src?: string };
68const document: undefined | { currentScript?: HTMLScriptElement };
69}
70
71/**
72* @summary
73*
74* This file is served as a "dual" file for both entries of the following:
75* - The proxy worker itself.
76* - When used as a worker, it listens to the messages from the main thread and performs the corresponding operations.
77* - Should be imported directly using `new Worker()` in the main thread.
78*
79* - The ESM module that creates the proxy worker (as a worker launcher).
80* - When used as a worker launcher, it creates the proxy worker and returns it.
81* - Should be imported using `import()` in the main thread, with the query parameter `import=1`.
82*
83* This file will be always compiling into ESM format.
84*/
85
86import type { OrtWasmMessage, SerializableTensorMetadata } from '../proxy-messages.js';
87import {
88createSession,
89copyFromExternalBuffer,
90endProfiling,
91extractTransferableBuffers,
92initEp,
93initRuntime,
94releaseSession,
95run,
96} from '../wasm-core-impl.js';
97import { initializeWebAssembly } from '../wasm-factory.js';
98import { scriptSrc } from '../wasm-utils-import.js';
99
100const WORKER_NAME = 'ort-wasm-proxy-worker';
101const isProxyWorker = globalThis.self?.name === WORKER_NAME;
102
103if (isProxyWorker) {
104// Worker thread
105self.onmessage = (ev: MessageEvent<OrtWasmMessage>): void => {
106const { type, in: message } = ev.data;
107try {
108switch (type) {
109case 'init-wasm':
110initializeWebAssembly(message!.wasm).then(
111() => {
112initRuntime(message!).then(
113() => {
114postMessage({ type });
115},
116(err) => {
117postMessage({ type, err });
118},
119);
120},
121(err) => {
122postMessage({ type, err });
123},
124);
125break;
126case 'init-ep': {
127const { epName, env } = message!;
128initEp(env, epName).then(
129() => {
130postMessage({ type });
131},
132(err) => {
133postMessage({ type, err });
134},
135);
136break;
137}
138case 'copy-from': {
139const { buffer } = message!;
140const bufferData = copyFromExternalBuffer(buffer);
141postMessage({ type, out: bufferData } as OrtWasmMessage);
142break;
143}
144case 'create': {
145const { model, options } = message!;
146createSession(model, options).then(
147(sessionMetadata) => {
148postMessage({ type, out: sessionMetadata } as OrtWasmMessage);
149},
150(err) => {
151postMessage({ type, err });
152},
153);
154break;
155}
156case 'release':
157releaseSession(message!);
158postMessage({ type });
159break;
160case 'run': {
161const { sessionId, inputIndices, inputs, outputIndices, options } = message!;
162run(sessionId, inputIndices, inputs, outputIndices, new Array(outputIndices.length).fill(null), options).then(
163(outputs) => {
164if (outputs.some((o) => o[3] !== 'cpu')) {
165postMessage({ type, err: 'Proxy does not support non-cpu tensor location.' });
166} else {
167postMessage(
168{ type, out: outputs } as OrtWasmMessage,
169extractTransferableBuffers([...inputs, ...outputs] as SerializableTensorMetadata[]),
170);
171}
172},
173(err) => {
174postMessage({ type, err });
175},
176);
177break;
178}
179case 'end-profiling':
180endProfiling(message!);
181postMessage({ type });
182break;
183default:
184}
185} catch (err) {
186postMessage({ type, err } as OrtWasmMessage);
187}
188};
189}
190
191export default isProxyWorker
192? null
193: (urlOverride?: string) =>
194new Worker(urlOverride ?? scriptSrc!, { type: BUILD_DEFS.IS_ESM ? 'module' : 'classic', name: WORKER_NAME });
195