onnxruntime/js/web/lib/onnxjs/backends/webgl/session-handler.ts
Yulong Wang 4ebc9c3b5e
[JS] onnxruntime-web (#7394)
* add web

* add script and test

* fix lint

* add test/data/ops

* add test/data/node/ to gitignore

* modify scripts

* add onnxjs

* fix tests

* fix test-runner

* fix sourcemap

* fix onnxjs profiling

* update test list

* update README

* resolve comments

* set wasm as default backend

* rename package

* update copyright header

* do not use class "Buffer" in browser context

* revise readme
2021-04-27 00:04:25 -07:00

68 lines
2.8 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {SessionHandler} from '../../backend';
import {Graph} from '../../graph';
import {Logger} from '../../instrument';
import {Operator} from '../../operators';
import {OpSet, resolveOperator} from '../../opset';
import {Session} from '../../session';
import {Tensor} from '../../tensor';
import {WebGLBackend} from '../backend-webgl';
import {WebGLInferenceHandler} from './inference-handler';
import {WEBGL_OP_RESOLVE_RULES} from './op-resolve-rules';
import {ProgramManager} from './program-manager';
import {PreferLogicalStrategy, TextureLayoutStrategy} from './texture-layout-strategy';
import {TextureManager} from './texture-manager';
import {TextureData, WebGLOperator} from './types';
export class WebGLSessionHandler implements SessionHandler {
programManager: ProgramManager;
textureManager: TextureManager;
layoutStrategy: TextureLayoutStrategy;
textureDataCache: Map<Tensor.Id, TextureData>;
initializers: Set<Tensor.Id>;
packOpCache: Map<string, WebGLOperator>;
unpackOpCache: Map<string, WebGLOperator>;
constructor(public readonly backend: WebGLBackend, public readonly context: Session.Context) {
this.layoutStrategy = new PreferLogicalStrategy(backend.glContext.maxTextureSize);
this.programManager = new ProgramManager(this.context.profiler, backend.glContext, this.layoutStrategy);
this.textureManager = new TextureManager(
backend.glContext, this.layoutStrategy, this.context.profiler,
{reuseTextures: backend.textureCacheMode === 'full'});
this.textureDataCache = new Map();
this.packOpCache = new Map();
this.unpackOpCache = new Map();
}
createInferenceHandler() {
return new WebGLInferenceHandler(this);
}
onGraphInitialized(graph: Graph): void {
const initializers = graph.getValues().filter(v => v.from === -1 && v.tensor).map(v => v.tensor!.dataId);
this.initializers = new Set(initializers);
}
isInitializer(tensorId: Tensor.Id): boolean {
return this.initializers ? this.initializers.has(tensorId) : false;
}
getTextureData(tensorId: Tensor.Id): TextureData|undefined {
return this.textureDataCache.get(tensorId);
}
setTextureData(tensorId: Tensor.Id, textureData: TextureData): void {
Logger.verbose('WebGLSessionHandler', 'Storing Texture data in cache');
this.textureDataCache.set(tensorId, textureData);
}
dispose(): void {
this.programManager.dispose();
this.textureManager.clearActiveTextures();
this.textureDataCache.forEach(td => this.textureManager.releaseTexture(td, true));
this.textureDataCache = new Map();
}
resolve(node: Graph.Node, opsets: readonly OpSet[], graph: Graph): Operator {
const op = resolveOperator(node, opsets, WEBGL_OP_RESOLVE_RULES);
op.initialize(node.attributes, node, graph);
return op;
}
}