Skip to content

Commit

Permalink
[WebNN] Enable npm Unit tests
Browse files Browse the repository at this point in the history
- Support more test cases for WebNN EP in suite-test-list.jsonc
- Add DISABLE_WEBNN flag in build.ts as preparing for WebNN EP release
- Add test option: '--webnn-device-type' in test-runner-args-cli.ts to support running WebNN 'gpu' deviceType
- Use Chrome Stable as default browser for WebNN testing to unblock the CI limitation.
  • Loading branch information
zesongw committed Nov 17, 2023
1 parent 885bf35 commit f1a43ac
Show file tree
Hide file tree
Showing 9 changed files with 1,065 additions and 93 deletions.
4 changes: 4 additions & 0 deletions js/web/lib/build-def.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ interface BuildDefinitions {
* defines whether to disable the whole WebGpu backend in the build.
*/
readonly DISABLE_WEBGPU: boolean;
/**
* defines whether to disable the whole WebNN backend in the build.
*/
readonly DISABLE_WEBNN: boolean;
/**
* defines whether to disable the whole WebAssembly backend in the build.
*/
Expand Down
4 changes: 3 additions & 1 deletion js/web/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ if (!BUILD_DEFS.DISABLE_WASM) {
registerBackend('wasm', wasmBackend, 10);
if (BUILD_DEFS.DISABLE_TRAINING) {
registerBackend('xnnpack', wasmBackend, 9);
registerBackend('webnn', wasmBackend, 9);
if (!BUILD_DEFS.DISABLE_WEBNN) {
registerBackend('webnn', wasmBackend, 9);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions js/web/script/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const SOURCE_ROOT_FOLDER = path.join(__dirname, '../..'); // <ORT_ROOT>/js/
const DEFAULT_DEFINE = {
'BUILD_DEFS.DISABLE_WEBGL': 'false',
'BUILD_DEFS.DISABLE_WEBGPU': 'false',
'BUILD_DEFS.DISABLE_WEBNN': 'true', // Disable WebNN until CI is ready.
'BUILD_DEFS.DISABLE_WASM': 'false',
'BUILD_DEFS.DISABLE_WASM_PROXY': 'false',
'BUILD_DEFS.DISABLE_WASM_THREAD': 'false',
Expand Down Expand Up @@ -370,6 +371,7 @@ async function main() {
await addBuildTask(buildOrt({
outputBundleName: 'ort.all',
format: 'iife',
define: {...DEFAULT_DEFINE, 'BUILD_DEFS.DISABLE_WEBNN': 'false'}
}));
}

Expand Down
12 changes: 12 additions & 0 deletions js/web/script/test-runner-cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Options:
--webgl-texture-cache-mode Set the WebGL texture cache mode (initializerOnly/full)
--webgl-texture-pack-mode Set the WebGL texture pack mode (true/false)
--webgpu-profiling-mode Set the WebGPU profiling mode (off/default)
--webnn-device-type Set the WebNN device type (cpu/gpu)
*** Browser Options ***
Expand Down Expand Up @@ -174,6 +175,7 @@ export interface TestRunnerCliArgs {
cudaFlags?: Record<string, unknown>;
wasmOptions?: InferenceSession.WebAssemblyExecutionProviderOption;
webglOptions?: InferenceSession.WebGLExecutionProviderOption;
webnnOptions?: InferenceSession.WebNNExecutionProviderOption;
globalEnvFlags?: Test.Options['globalEnvFlags'];
noSandbox?: boolean;
chromiumFlags: string[];
Expand Down Expand Up @@ -335,6 +337,14 @@ function parseWebgpuFlags(args: minimist.ParsedArgs): Partial<Env.WebGpuFlags> {
return {profilingMode, validateInputContent};
}

function parseWebNNOptions(args: minimist.ParsedArgs): InferenceSession.WebNNExecutionProviderOption {
const deviceType = args['webnn-device-type'];
if (deviceType !== undefined && deviceType !== 'cpu' && deviceType !== 'gpu') {
throw new Error('Flag "webnn-device-type" is invalid');
}
return {name: 'webnn', deviceType};
}

function parseGlobalEnvFlags(args: minimist.ParsedArgs): NonNullable<TestRunnerCliArgs['globalEnvFlags']> {
const wasm = parseWasmFlags(args);
const webgl = parseWebglFlags(args);
Expand Down Expand Up @@ -449,6 +459,7 @@ export function parseTestRunnerCliArgs(cmdlineArgs: string[]): TestRunnerCliArgs
const wasmOptions = parseWasmOptions(args);

const webglOptions = parseWebglOptions(args);
const webnnOptions = parseWebNNOptions(args);

// Option: --no-sandbox
const noSandbox = !!args['no-sandbox'];
Expand Down Expand Up @@ -487,6 +498,7 @@ export function parseTestRunnerCliArgs(cmdlineArgs: string[]): TestRunnerCliArgs
fileCache,
cpuOptions,
webglOptions,
webnnOptions,
wasmOptions,
globalEnvFlags,
noSandbox,
Expand Down
14 changes: 6 additions & 8 deletions js/web/script/test-runner-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ async function main() {
debug: args.debug,
cpuOptions: args.cpuOptions,
webglOptions: args.webglOptions,
webnnOptions: args.webnnOptions,
wasmOptions: args.wasmOptions,
globalEnvFlags: args.globalEnvFlags
}
Expand Down Expand Up @@ -499,7 +500,7 @@ async function main() {
args.bundleMode === 'perf' ? 'perf' :
args.debug ? 'debug' :
'test',
webgpu, webnn);
webgpu);
const karmaArgs = ['karma', 'start', `--browsers ${browser}`];
const chromiumFlags = ['--enable-features=SharedArrayBuffer', ...args.chromiumFlags];
if (args.debug) {
Expand Down Expand Up @@ -614,11 +615,10 @@ async function main() {
fs.writeJSONSync(path.join(TEST_ROOT, './testdata-config.json'), config);
}

function getBrowserNameFromEnv(
env: TestRunnerCliArgs['env'], mode: 'debug'|'perf'|'test', webgpu: boolean, webnn: boolean) {
function getBrowserNameFromEnv(env: TestRunnerCliArgs['env'], mode: 'debug'|'perf'|'test', webgpu: boolean) {
switch (env) {
case 'chrome':
return selectChromeBrowser(mode, webgpu, webnn);
return selectChromeBrowser(mode, webgpu);
case 'edge':
return 'EdgeTest';
case 'firefox':
Expand All @@ -634,10 +634,8 @@ async function main() {
}
}

function selectChromeBrowser(mode: 'debug'|'perf'|'test', webgpu: boolean, webnn: boolean) {
if (webnn) {
return 'ChromeCanaryTest';
} else if (webgpu) {
function selectChromeBrowser(mode: 'debug'|'perf'|'test', webgpu: boolean) {
if (webgpu) {
return 'ChromeTest';
} else {
switch (mode) {
Expand Down
Loading

0 comments on commit f1a43ac

Please sign in to comment.