onnxruntime/js/web/lib/onnxjs/backends/backend-webgl.ts
Yulong Wang 6ea493571e
[js/web] use esbuild to accelerate bundle build (#17745)
### Description

Use esbuild to accelerate bundle build.

This change uses esbuild to replace webpack for onnxruntime-web. Bundle
build time reduced from ~20sec to ~0.6sec on my windows dev box.

A few changes applied:
- import nodejs modules using "node:" prefix
- remove enum declaration inside namespace (EncoderUsage)
- use "fs/promise" to replace the old promisify from "util"
- separate ort-web and test-runner. Previously they are bundled
together, now they are built into 2 files.
- optimize karma runner launch time
- remove unnecessary sourcemap preprocessor. sourcemaps are handled
inside esbuild
- remove unnecessary proxies (because ort-web and test-runner are
separated now, the path are correctly inferred)
    - remove file watcher for test data
- optimize special handling as esbuild plugins:
- polyfill dummy imports for node.js modules when targetting browser.
    - load as content string for ort-wasm-*.worker.js
    - load as content string for ./proxy-worker/main.ts
- a source patch to ort-wasm*-threaded*.js (see details in comments in
code)
- updated debug configurations for sourcemap mapping to ensure
out-of-box good dev experience
2023-10-06 13:37:37 -07:00

96 lines
2.7 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {env} from 'onnxruntime-common';
import {Backend, SessionHandler} from '../backend';
import {Logger} from '../instrument';
import {Session} from '../session';
import {WebGLSessionHandler} from './webgl/session-handler';
import {WebGLContext} from './webgl/webgl-context';
import {createWebGLContext} from './webgl/webgl-context-factory';
/**
* WebGLBackend is the entry point for all WebGL opeartions
* When it starts it created the WebGLRenderingContext
* and other main framework components such as Program and Texture Managers
*/
export class WebGLBackend implements Backend {
glContext: WebGLContext;
get contextId(): 'webgl'|'webgl2'|undefined {
return env.webgl.contextId;
}
set contextId(value: 'webgl'|'webgl2'|undefined) {
env.webgl.contextId = value;
}
get matmulMaxBatchSize(): number|undefined {
return env.webgl.matmulMaxBatchSize;
}
set matmulMaxBatchSize(value: number|undefined) {
env.webgl.matmulMaxBatchSize = value;
}
get textureCacheMode(): 'initializerOnly'|'full'|undefined {
return env.webgl.textureCacheMode;
}
set textureCacheMode(value: 'initializerOnly'|'full'|undefined) {
env.webgl.textureCacheMode = value;
}
get pack(): boolean|undefined {
return env.webgl.pack;
}
set pack(value: boolean|undefined) {
env.webgl.pack = value;
}
get async(): boolean|undefined {
return env.webgl.async;
}
set async(value: boolean|undefined) {
env.webgl.async = value;
}
initialize(): boolean {
try {
this.glContext = createWebGLContext(this.contextId);
if (typeof this.matmulMaxBatchSize !== 'number') {
this.matmulMaxBatchSize = 16;
}
if (typeof this.textureCacheMode !== 'string') {
this.textureCacheMode = 'full';
}
if (typeof this.pack !== 'boolean') {
this.pack = false;
}
if (typeof this.async !== 'boolean') {
this.async = false;
}
Logger.setWithEnv(env);
if (!env.webgl.context) {
Object.defineProperty(env.webgl, 'context', {value: this.glContext.gl});
}
Logger.verbose(
'WebGLBackend',
`Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${
this.matmulMaxBatchSize}; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${
this.async}.`);
return true;
} catch (e) {
Logger.warning('WebGLBackend', `Unable to initialize WebGLBackend. ${e}`);
return false;
}
}
createSessionHandler(context: Session.Context): SessionHandler {
return new WebGLSessionHandler(this, context);
}
dispose(): void {
this.glContext.dispose();
}
}