Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Mixed backend #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/api/onnx-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {WebGLBackend} from '../backends/backend-webgl';
import {Environment} from './env';
import {envImpl} from './env-impl';
import {Backend} from './onnx';
import {MixedBackend} from '../backends/backend-mixed';

export * from './env';
export * from './onnx';
Expand All @@ -17,7 +18,8 @@ export * from './inference-session';
export const backend: Backend = {
cpu: new CpuBackend(),
wasm: new WasmBackend(),
webgl: new WebGLBackend()
webgl: new WebGLBackend(),
mixed: new MixedBackend()
};

export const ENV: Environment = envImpl;
2 changes: 1 addition & 1 deletion lib/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const backendsCache: Map<string, Backend> = new Map();
*/
export async function Backend(hint?: string|ReadonlyArray<string>): Promise<Backend> {
if (!hint) {
return Backend(['webgl', 'wasm', 'cpu']);
return Backend(['webgl', 'mixed', 'wasm', 'cpu']);
} else {
const hints = typeof hint === 'string' ? [hint] : hint;

Expand Down
14 changes: 14 additions & 0 deletions lib/backends/backend-mixed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import {SessionHandler} from '../backend';
import {Session} from '../session';
import {MixedSessionHandler} from './mixed-session-handler';
import {WebGLBackend} from './backend-webgl';


export class MixedBackend extends WebGLBackend {
createSessionHandler(context: Session.Context): SessionHandler {
return new MixedSessionHandler(this, context);
}
}
24 changes: 24 additions & 0 deletions lib/backends/mixed-session-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import {WebGLSessionHandler} from './webgl/session-handler';
import {Graph} from '../graph';
import {OpSet, resolveOperator} from '../opset';
import {Operator} from '../operators';
import {CPU_OP_RESOLVE_RULES} from './cpu/op-resolve-rules';
import {Logger} from '../instrument';

export class MixedSessionHandler extends WebGLSessionHandler {
resolve(node: Graph.Node, opsets: ReadonlyArray<OpSet>): Operator {
try {
return super.resolve(node, opsets);
} catch (e) {
Logger.warning(
'MixedSessionHandler',
`Unable to initialize operator '${node.opType}' with webgl. trying with cpu...`);
const op = resolveOperator(node, opsets, CPU_OP_RESOLVE_RULES);
op.initialize(node.attributes);
return op;
}
}
}