Skip to content
Merged
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
11 changes: 4 additions & 7 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,10 @@ export function permuteData(array, dims, axes) {
return [permutedData, shape];
}

export function getDefaultLayout(deviceType) {
if (deviceType.indexOf('cpu') != -1) {
return 'nhwc';
} else if (deviceType.indexOf('gpu') != -1 ||
deviceType.indexOf('npu') != -1) {
return 'nchw';
}
export async function getDefaultLayout(deviceType) {
const context = await navigator.ml.createContext({deviceType});
const limits = context.opSupportLimits();
return limits.preferredInputLayout ?? 'nchw';
}

/**
Expand Down
7 changes: 3 additions & 4 deletions face_recognition/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ $('#backendBtns .btn').on('change', async (e) => {
if (inputType === 'camera') {
await stopCamRender();
}
layout = utils.getDefaultLayout($(e.target).attr('id'));
[backend, deviceType] = $(e.target).attr('id').split('_');
layout = await utils.getDefaultLayout(deviceType);
await main();
});

Expand Down Expand Up @@ -296,8 +297,6 @@ function constructNetObject(type) {
async function main() {
try {
if (fdModelName === '') return;
[backend, deviceType] =
$('input[name="backend"]:checked').attr('id').split('_');
ui.handleClick(disabledSelectors, true);
if (isFirstTimeLoad) $('#hint').hide();
const [numRuns, powerPreference] = utils.getUrlParams();
Expand Down Expand Up @@ -359,7 +358,7 @@ async function main() {
utils.sizeOfShape(frInputOptions.inputShape)));
for (let i = 0; i < numRuns; i++) {
if (numRuns > 1) {
// clear all predicted embeddings for benckmarking
// clear all predicted embeddings for benchmarking
targetEmbeddings = null;
searchEmbeddings = null;
}
Expand Down
5 changes: 2 additions & 3 deletions facial_landmark_detection/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ $('#backendBtns .btn').on('change', async (e) => {
if (inputType === 'camera') {
await stopCamRender();
}
layout = utils.getDefaultLayout($(e.target).attr('id'));
[backend, deviceType] = $(e.target).attr('id').split('_');
layout = await utils.getDefaultLayout(deviceType);
await main();
});

Expand Down Expand Up @@ -232,8 +233,6 @@ function constructNetObject(type) {
async function main() {
try {
if (fdModelName === '') return;
[backend, deviceType] =
$('input[name="backend"]:checked').attr('id').split('_');
ui.handleClick(disabledSelectors, true);
if (isFirstTimeLoad) $('#hint').hide();
const [numRuns, powerPreference] = utils.getUrlParams();
Expand Down
9 changes: 4 additions & 5 deletions image_classification/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,17 @@ $('#backendBtns .btn').on('change', async (e) => {
if (inputType === 'camera') {
await stopCamRender();
}
const backendId = $(e.target).attr('id');
layout = utils.getDefaultLayout(backendId);
[backend, deviceType] = backendId.split('_');
[backend, deviceType] = $(e.target).attr('id').split('_');
layout = await utils.getDefaultLayout(deviceType);
// Only show the supported models for each deviceType. Now fp16 nchw models
// are only supported on gpu/npu.
if (backendId == 'webnn_gpu') {
if (deviceType == 'gpu') {
ui.handleBtnUI('#float16Label', false);
ui.handleBtnUI('#float32Label', false);
ui.handleBtnUI('#uint8Label', true);
$('#float32').click();
utils.displayAvailableModels(modelList, modelIds, deviceType, dataType);
} else if (backendId == 'webnn_npu') {
} else if (deviceType == 'npu') {
ui.handleBtnUI('#float16Label', false);
ui.handleBtnUI('#float32Label', true);
ui.handleBtnUI('#uint8Label', true);
Expand Down
9 changes: 4 additions & 5 deletions object_detection/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ $('#backendBtns .btn').on('change', async (e) => {
if (inputType === 'camera') {
await stopCamRender();
}
const backendId = $(e.target).attr('id');
layout = utils.getDefaultLayout(backendId);
[backend, deviceType] = backendId.split('_');
[backend, deviceType] = $(e.target).attr('id').split('_');
layout = await utils.getDefaultLayout(deviceType);
// Only show the supported models for each deviceType. Now fp16 nchw models
// are only supported on gpu/npu.
if (backendId == 'webnn_gpu') {
if (deviceType == 'gpu') {
ui.handleBtnUI('#float16Label', false);
ui.handleBtnUI('#float32Label', false);
utils.displayAvailableModels(modelList, modelIds, deviceType, dataType);
} else if (backendId == 'webnn_npu') {
} else if (deviceType == 'npu') {
ui.handleBtnUI('#float16Label', false);
ui.handleBtnUI('#float32Label', true);
$('#float16').click();
Expand Down
5 changes: 2 additions & 3 deletions semantic_segmentation/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ $('#backendBtns .btn').on('change', async (e) => {
if (inputType === 'camera') {
await stopCamRender();
}
layout = utils.getDefaultLayout($(e.target).attr('id'));
[backend, deviceType] = $(e.target).attr('id').split('_');
layout = await utils.getDefaultLayout(deviceType);
await main();
});

Expand Down Expand Up @@ -326,8 +327,6 @@ function constructNetObject(type) {
export async function main() {
try {
if (modelName === '') return;
[backend, deviceType] =
$('input[name="backend"]:checked').attr('id').split('_');
ui.handleClick(disabledSelectors, true);
if (isFirstTimeLoad) $('#hint').hide();
let start;
Expand Down
Loading